upload http
[bottlenecks.git] / rubbos / app / httpd-2.0.64 / srclib / apr / locks / unix / thread_cond.c
1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2  * contributor license agreements.  See the NOTICE file distributed with
3  * this work for additional information regarding copyright ownership.
4  * The ASF licenses this file to You under the Apache License, Version 2.0
5  * (the "License"); you may not use this file except in compliance with
6  * the License.  You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "apr.h"
18
19 #if APR_HAS_THREADS
20
21 #include "apr_arch_thread_mutex.h"
22 #include "apr_arch_thread_cond.h"
23
24 static apr_status_t thread_cond_cleanup(void *data)
25 {
26     apr_thread_cond_t *cond = (apr_thread_cond_t *)data;
27     apr_status_t rv;
28
29     rv = pthread_cond_destroy(cond->cond);
30 #ifdef PTHREAD_SETS_ERRNO
31     if (rv) {
32         rv = errno;
33     }
34 #endif
35     return rv;
36
37
38 APR_DECLARE(apr_status_t) apr_thread_cond_create(apr_thread_cond_t **cond,
39                                                  apr_pool_t *pool)
40 {
41     apr_thread_cond_t *new_cond;
42     apr_status_t rv;
43
44     new_cond = (apr_thread_cond_t *)apr_pcalloc(pool,
45                                                 sizeof(apr_thread_cond_t));
46
47     if (new_cond == NULL) {
48         return APR_ENOMEM;
49     }
50
51     new_cond->pool = pool;
52     new_cond->cond = (pthread_cond_t *)apr_palloc(pool, 
53                                                   sizeof(pthread_cond_t));
54
55     if (new_cond->cond == NULL) {
56         return APR_ENOMEM;
57     }
58
59     if ((rv = pthread_cond_init(new_cond->cond, NULL))) {
60 #ifdef PTHREAD_SETS_ERRNO
61         rv = errno;
62 #endif
63         thread_cond_cleanup(new_cond);
64         return rv;
65     }
66
67     apr_pool_cleanup_register(new_cond->pool,
68                               (void *)new_cond, thread_cond_cleanup,
69                               apr_pool_cleanup_null);
70
71     *cond = new_cond;
72     return APR_SUCCESS;
73 }
74
75 APR_DECLARE(apr_status_t) apr_thread_cond_wait(apr_thread_cond_t *cond,
76                                                apr_thread_mutex_t *mutex)
77 {
78     apr_status_t rv;
79
80     rv = pthread_cond_wait(cond->cond, &mutex->mutex);
81 #ifdef PTHREAD_SETS_ERRNO
82     if (rv) {
83         rv = errno;
84     }
85 #endif
86     return rv;
87 }
88
89 APR_DECLARE(apr_status_t) apr_thread_cond_timedwait(apr_thread_cond_t *cond,
90                                                     apr_thread_mutex_t *mutex,
91                                                     apr_interval_time_t timeout)
92 {
93     apr_status_t rv;
94     apr_time_t then;
95     struct timespec abstime;
96
97     then = apr_time_now() + timeout;
98     abstime.tv_sec = apr_time_sec(then);
99     abstime.tv_nsec = apr_time_usec(then) * 1000; /* nanoseconds */
100
101     rv = pthread_cond_timedwait(cond->cond, &mutex->mutex, &abstime);
102 #ifdef PTHREAD_SETS_ERRNO
103     if (rv) {
104         rv = errno;
105     }
106 #endif
107     if (ETIMEDOUT == rv) {
108         return APR_TIMEUP;
109     }
110     return rv;
111 }
112
113
114 APR_DECLARE(apr_status_t) apr_thread_cond_signal(apr_thread_cond_t *cond)
115 {
116     apr_status_t rv;
117
118     rv = pthread_cond_signal(cond->cond);
119 #ifdef PTHREAD_SETS_ERRNO
120     if (rv) {
121         rv = errno;
122     }
123 #endif
124     return rv;
125 }
126
127 APR_DECLARE(apr_status_t) apr_thread_cond_broadcast(apr_thread_cond_t *cond)
128 {
129     apr_status_t rv;
130
131     rv = pthread_cond_broadcast(cond->cond);
132 #ifdef PTHREAD_SETS_ERRNO
133     if (rv) {
134         rv = errno;
135     }
136 #endif
137     return rv;
138 }
139
140 APR_DECLARE(apr_status_t) apr_thread_cond_destroy(apr_thread_cond_t *cond)
141 {
142     apr_status_t rv;
143     if ((rv = thread_cond_cleanup(cond)) == APR_SUCCESS) {
144         apr_pool_cleanup_kill(cond->pool, cond, thread_cond_cleanup);
145         return APR_SUCCESS;
146     }
147     return rv;
148 }
149
150 APR_POOL_IMPLEMENT_ACCESSOR(thread_cond)
151
152 #endif /* APR_HAS_THREADS */