bottleneck testcase based on rubbos
[bottlenecks.git] / rubbos / app / httpd-2.0.64 / srclib / apr / locks / beos / 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 "beos/apr_arch_thread_mutex.h"
18 #include "beos/apr_arch_thread_cond.h"
19 #include "apr_strings.h"
20 #include "apr_portable.h"
21
22 static apr_status_t thread_cond_cleanup(void *data)
23 {
24     struct waiter *w;
25     apr_thread_cond_t *cond = (apr_thread_cond_t *)data;
26
27     acquire_sem(cond->lock);
28 /*
29     APR_RING_FOREACH(w, &cond->alist, waiter_t, link) {
30         delete_sem(w->sem);
31     }
32 */
33     delete_sem(cond->lock);
34
35     return APR_SUCCESS;
36 }
37
38 static struct waiter_t *make_waiter(apr_pool_t *pool)
39 {
40     struct waiter_t *w = (struct waiter_t*)
41                        apr_palloc(pool, sizeof(struct waiter_t));
42     if (w == NULL)
43         return NULL;
44       
45     w->sem  = create_sem(0, "apr conditional waiter");
46     if (w->sem < 0)
47         return NULL;
48
49     APR_RING_ELEM_INIT(w, link);
50     
51     return w;
52 }
53   
54 APR_DECLARE(apr_status_t) apr_thread_cond_create(apr_thread_cond_t **cond,
55                                                  apr_pool_t *pool)
56 {
57     apr_thread_cond_t *new_cond;
58     sem_id rv;
59     int i;
60
61     new_cond = (apr_thread_cond_t *)apr_palloc(pool, sizeof(apr_thread_cond_t));
62
63     if (new_cond == NULL)
64         return APR_ENOMEM;
65
66     if ((rv = create_sem(1, "apr conditional lock")) < B_OK)
67         return rv;
68     
69     new_cond->lock = rv;
70     new_cond->pool = pool;
71     APR_RING_INIT(&new_cond->alist, waiter_t, link);
72     APR_RING_INIT(&new_cond->flist, waiter_t, link);
73         
74     for (i=0;i < 10 ;i++) {
75         struct waiter_t *nw = make_waiter(pool);
76         APR_RING_INSERT_TAIL(&new_cond->flist, nw, waiter_t, link);
77     }
78
79     apr_pool_cleanup_register(new_cond->pool,
80                               (void *)new_cond, thread_cond_cleanup,
81                               apr_pool_cleanup_null);
82
83     *cond = new_cond;
84     return APR_SUCCESS;
85 }
86
87
88 static apr_status_t do_wait(apr_thread_cond_t *cond, apr_thread_mutex_t *mutex,
89                             int timeout)
90 {
91     struct waiter_t *wait;
92     thread_id cth = find_thread(NULL);
93     apr_status_t rv;
94     int flags = B_RELATIVE_TIMEOUT;
95     
96     /* We must be the owner of the mutex or we can't do this... */    
97     if (mutex->owner != cth) {
98         /* What should we return??? */
99         return APR_EINVAL;
100     }
101
102     acquire_sem(cond->lock);
103     wait = APR_RING_FIRST(&cond->flist);
104     if (wait)
105         APR_RING_REMOVE(wait, link);
106     else
107         wait = make_waiter(cond->pool);   
108     APR_RING_INSERT_TAIL(&cond->alist, wait, waiter_t, link);
109     cond->condlock = mutex;
110     release_sem(cond->lock);
111        
112     apr_thread_mutex_unlock(cond->condlock);
113
114     if (timeout == 0)
115         flags = 0;
116         
117     rv = acquire_sem_etc(wait->sem, 1, flags, timeout);
118
119     apr_thread_mutex_lock(cond->condlock);
120     
121     if (rv != B_OK)
122         if (rv == B_TIMED_OUT)
123             return APR_TIMEUP;
124         return rv;       
125
126     acquire_sem(cond->lock);
127     APR_RING_REMOVE(wait, link);
128     APR_RING_INSERT_TAIL(&cond->flist, wait, waiter_t, link);
129     release_sem(cond->lock);
130     
131     return APR_SUCCESS;
132 }
133
134 APR_DECLARE(apr_status_t) apr_thread_cond_wait(apr_thread_cond_t *cond,
135                                                apr_thread_mutex_t *mutex)
136 {
137     return do_wait(cond, mutex, 0);
138 }
139
140 APR_DECLARE(apr_status_t) apr_thread_cond_timedwait(apr_thread_cond_t *cond,
141                                                     apr_thread_mutex_t *mutex,
142                                                     apr_interval_time_t timeout)
143 {
144     return do_wait(cond, mutex, timeout);
145 }
146
147 APR_DECLARE(apr_status_t) apr_thread_cond_signal(apr_thread_cond_t *cond)
148 {
149     struct waiter_t *wake;
150
151     acquire_sem(cond->lock);    
152     if (!APR_RING_EMPTY(&cond->alist, waiter_t, link)) {
153         wake = APR_RING_FIRST(&cond->alist);
154         APR_RING_REMOVE(wake, link);
155         release_sem(wake->sem);
156         APR_RING_INSERT_TAIL(&cond->flist, wake, waiter_t, link);
157     }
158     release_sem(cond->lock);
159     
160     return APR_SUCCESS;
161 }
162
163 APR_DECLARE(apr_status_t) apr_thread_cond_broadcast(apr_thread_cond_t *cond)
164 {
165     struct waiter_t *wake;
166     
167     acquire_sem(cond->lock);
168     while (! APR_RING_EMPTY(&cond->alist, waiter_t, link)) {
169         wake = APR_RING_FIRST(&cond->alist);
170         APR_RING_REMOVE(wake, link);
171         release_sem(wake->sem);
172         APR_RING_INSERT_TAIL(&cond->flist, wake, waiter_t, link);
173     }
174     release_sem(cond->lock);
175     
176     return APR_SUCCESS;
177 }
178
179 APR_DECLARE(apr_status_t) apr_thread_cond_destroy(apr_thread_cond_t *cond)
180 {
181     apr_status_t stat;
182     if ((stat = thread_cond_cleanup(cond)) == APR_SUCCESS) {
183         apr_pool_cleanup_kill(cond->pool, cond, thread_cond_cleanup);
184         return APR_SUCCESS;
185     }
186     return stat;
187 }
188
189 APR_POOL_IMPLEMENT_ACCESSOR(thread_cond)
190