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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
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"
22 static apr_status_t thread_cond_cleanup(void *data)
25 apr_thread_cond_t *cond = (apr_thread_cond_t *)data;
27 acquire_sem(cond->lock);
29 APR_RING_FOREACH(w, &cond->alist, waiter_t, link) {
33 delete_sem(cond->lock);
38 static struct waiter_t *make_waiter(apr_pool_t *pool)
40 struct waiter_t *w = (struct waiter_t*)
41 apr_palloc(pool, sizeof(struct waiter_t));
45 w->sem = create_sem(0, "apr conditional waiter");
49 APR_RING_ELEM_INIT(w, link);
54 APR_DECLARE(apr_status_t) apr_thread_cond_create(apr_thread_cond_t **cond,
57 apr_thread_cond_t *new_cond;
61 new_cond = (apr_thread_cond_t *)apr_palloc(pool, sizeof(apr_thread_cond_t));
66 if ((rv = create_sem(1, "apr conditional lock")) < B_OK)
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);
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);
79 apr_pool_cleanup_register(new_cond->pool,
80 (void *)new_cond, thread_cond_cleanup,
81 apr_pool_cleanup_null);
88 static apr_status_t do_wait(apr_thread_cond_t *cond, apr_thread_mutex_t *mutex,
91 struct waiter_t *wait;
92 thread_id cth = find_thread(NULL);
94 int flags = B_RELATIVE_TIMEOUT;
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??? */
102 acquire_sem(cond->lock);
103 wait = APR_RING_FIRST(&cond->flist);
105 APR_RING_REMOVE(wait, link);
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);
112 apr_thread_mutex_unlock(cond->condlock);
117 rv = acquire_sem_etc(wait->sem, 1, flags, timeout);
119 apr_thread_mutex_lock(cond->condlock);
122 if (rv == B_TIMED_OUT)
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);
134 APR_DECLARE(apr_status_t) apr_thread_cond_wait(apr_thread_cond_t *cond,
135 apr_thread_mutex_t *mutex)
137 return do_wait(cond, mutex, 0);
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)
144 return do_wait(cond, mutex, timeout);
147 APR_DECLARE(apr_status_t) apr_thread_cond_signal(apr_thread_cond_t *cond)
149 struct waiter_t *wake;
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);
158 release_sem(cond->lock);
163 APR_DECLARE(apr_status_t) apr_thread_cond_broadcast(apr_thread_cond_t *cond)
165 struct waiter_t *wake;
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);
174 release_sem(cond->lock);
179 APR_DECLARE(apr_status_t) apr_thread_cond_destroy(apr_thread_cond_t *cond)
182 if ((stat = thread_cond_cleanup(cond)) == APR_SUCCESS) {
183 apr_pool_cleanup_kill(cond->pool, cond, thread_cond_cleanup);
189 APR_POOL_IMPLEMENT_ACCESSOR(thread_cond)