1
/* Copyright 2000-2005 The Apache Software Foundation or its licensors, as
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* 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 "apr_arch_thread_mutex.h"
18
#include "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);
28
delete_sem(cond->lock);
33
static struct waiter_t *make_waiter(apr_pool_t *pool)
35
struct waiter_t *w = (struct waiter_t*)
36
apr_palloc(pool, sizeof(struct waiter_t));
40
w->sem = create_sem(0, "apr conditional waiter");
44
APR_RING_ELEM_INIT(w, link);
49
APR_DECLARE(apr_status_t) apr_thread_cond_create(apr_thread_cond_t **cond,
52
apr_thread_cond_t *new_cond;
56
new_cond = (apr_thread_cond_t *)apr_palloc(pool, sizeof(apr_thread_cond_t));
61
if ((rv = create_sem(1, "apr conditional lock")) < B_OK)
65
new_cond->pool = pool;
66
APR_RING_INIT(&new_cond->alist, waiter_t, link);
67
APR_RING_INIT(&new_cond->flist, waiter_t, link);
69
for (i=0;i < 10 ;i++) {
70
struct waiter_t *nw = make_waiter(pool);
71
APR_RING_INSERT_TAIL(&new_cond->flist, nw, waiter_t, link);
74
apr_pool_cleanup_register(new_cond->pool,
75
(void *)new_cond, thread_cond_cleanup,
76
apr_pool_cleanup_null);
83
static apr_status_t do_wait(apr_thread_cond_t *cond, apr_thread_mutex_t *mutex,
86
struct waiter_t *wait;
87
thread_id cth = find_thread(NULL);
89
int flags = B_RELATIVE_TIMEOUT;
91
/* We must be the owner of the mutex or we can't do this... */
92
if (mutex->owner != cth) {
93
/* What should we return??? */
97
acquire_sem(cond->lock);
98
wait = APR_RING_FIRST(&cond->flist);
100
APR_RING_REMOVE(wait, link);
102
wait = make_waiter(cond->pool);
103
APR_RING_INSERT_TAIL(&cond->alist, wait, waiter_t, link);
104
cond->condlock = mutex;
105
release_sem(cond->lock);
107
apr_thread_mutex_unlock(cond->condlock);
112
rv = acquire_sem_etc(wait->sem, 1, flags, timeout);
114
apr_thread_mutex_lock(cond->condlock);
117
if (rv == B_TIMED_OUT)
121
acquire_sem(cond->lock);
122
APR_RING_REMOVE(wait, link);
123
APR_RING_INSERT_TAIL(&cond->flist, wait, waiter_t, link);
124
release_sem(cond->lock);
129
APR_DECLARE(apr_status_t) apr_thread_cond_wait(apr_thread_cond_t *cond,
130
apr_thread_mutex_t *mutex)
132
return do_wait(cond, mutex, 0);
135
APR_DECLARE(apr_status_t) apr_thread_cond_timedwait(apr_thread_cond_t *cond,
136
apr_thread_mutex_t *mutex,
137
apr_interval_time_t timeout)
139
return do_wait(cond, mutex, timeout);
142
APR_DECLARE(apr_status_t) apr_thread_cond_signal(apr_thread_cond_t *cond)
144
struct waiter_t *wake;
146
acquire_sem(cond->lock);
147
if (!APR_RING_EMPTY(&cond->alist, waiter_t, link)) {
148
wake = APR_RING_FIRST(&cond->alist);
149
APR_RING_REMOVE(wake, link);
150
release_sem(wake->sem);
151
APR_RING_INSERT_TAIL(&cond->flist, wake, waiter_t, link);
153
release_sem(cond->lock);
158
APR_DECLARE(apr_status_t) apr_thread_cond_broadcast(apr_thread_cond_t *cond)
160
struct waiter_t *wake;
162
acquire_sem(cond->lock);
163
while (! APR_RING_EMPTY(&cond->alist, waiter_t, link)) {
164
wake = APR_RING_FIRST(&cond->alist);
165
APR_RING_REMOVE(wake, link);
166
release_sem(wake->sem);
167
APR_RING_INSERT_TAIL(&cond->flist, wake, waiter_t, link);
169
release_sem(cond->lock);
174
APR_DECLARE(apr_status_t) apr_thread_cond_destroy(apr_thread_cond_t *cond)
177
if ((stat = thread_cond_cleanup(cond)) == APR_SUCCESS) {
178
apr_pool_cleanup_kill(cond->pool, cond, thread_cond_cleanup);
184
APR_POOL_IMPLEMENT_ACCESSOR(thread_cond)