2 * Copyright (C) 2004, 2006, 2007 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 1998-2001 Internet Software Consortium.
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15 * PERFORMANCE OF THIS SOFTWARE.
18 /* $Id: condition.c,v 1.23 2007/06/18 23:47:49 tbox Exp $ */
22 #include <isc/condition.h>
23 #include <isc/assertions.h>
25 #include <isc/thread.h>
32 isc_condition_init(isc_condition_t *cond) {
35 REQUIRE(cond != NULL);
39 * This handle is shared across all threads
41 h = CreateEvent(NULL, FALSE, FALSE, NULL);
44 return (ISC_R_UNEXPECTED);
46 cond->events[LSIGNAL] = h;
49 * The threadlist will hold the actual events needed
50 * for the wait condition
52 ISC_LIST_INIT(cond->threadlist);
54 return (ISC_R_SUCCESS);
58 * Add the thread to the threadlist along with the required events
61 register_thread(unsigned long thrd, isc_condition_t *gblcond,
62 isc_condition_thread_t **localcond)
65 isc_condition_thread_t *newthread;
67 REQUIRE(localcond != NULL && *localcond == NULL);
69 newthread = malloc(sizeof(isc_condition_thread_t));
70 if (newthread == NULL)
71 return (ISC_R_NOMEMORY);
74 * Create the thread-specific handle
76 hc = CreateEvent(NULL, FALSE, FALSE, NULL);
79 return (ISC_R_UNEXPECTED);
83 * Add the thread ID and handles to list of threads for broadcast
85 newthread->handle[LSIGNAL] = gblcond->events[LSIGNAL];
86 newthread->handle[LBROADCAST] = hc;
90 * The thread is holding the manager lock so this is safe
92 ISC_LIST_APPEND(gblcond->threadlist, newthread, link);
93 *localcond = newthread;
94 return (ISC_R_SUCCESS);
98 find_thread_condition(unsigned long thrd, isc_condition_t *cond,
99 isc_condition_thread_t **threadcondp)
101 isc_condition_thread_t *threadcond;
103 REQUIRE(threadcondp != NULL && *threadcondp == NULL);
106 * Look for the thread ID.
108 for (threadcond = ISC_LIST_HEAD(cond->threadlist);
110 threadcond = ISC_LIST_NEXT(threadcond, link)) {
112 if (threadcond->th == thrd) {
113 *threadcondp = threadcond;
114 return (ISC_R_SUCCESS);
119 * Not found, so add it.
121 return (register_thread(thrd, cond, threadcondp));
125 isc_condition_signal(isc_condition_t *cond) {
128 * Unlike pthreads, the caller MUST hold the lock associated with
129 * the condition variable when calling us.
131 REQUIRE(cond != NULL);
133 if (!SetEvent(cond->events[LSIGNAL])) {
135 return (ISC_R_UNEXPECTED);
138 return (ISC_R_SUCCESS);
142 isc_condition_broadcast(isc_condition_t *cond) {
144 isc_condition_thread_t *threadcond;
145 isc_boolean_t failed = ISC_FALSE;
148 * Unlike pthreads, the caller MUST hold the lock associated with
149 * the condition variable when calling us.
151 REQUIRE(cond != NULL);
154 * Notify every thread registered for this
156 for (threadcond = ISC_LIST_HEAD(cond->threadlist);
158 threadcond = ISC_LIST_NEXT(threadcond, link)) {
160 if (!SetEvent(threadcond->handle[LBROADCAST]))
165 return (ISC_R_UNEXPECTED);
167 return (ISC_R_SUCCESS);
171 isc_condition_destroy(isc_condition_t *cond) {
173 isc_condition_thread_t *next, *threadcond;
175 REQUIRE(cond != NULL);
176 REQUIRE(cond->waiters == 0);
178 (void)CloseHandle(cond->events[LSIGNAL]);
181 * Delete the threadlist
183 threadcond = ISC_LIST_HEAD(cond->threadlist);
185 while (threadcond != NULL) {
186 next = ISC_LIST_NEXT(threadcond, link);
187 DEQUEUE(cond->threadlist, threadcond, link);
188 (void) CloseHandle(threadcond->handle[LBROADCAST]);
193 return (ISC_R_SUCCESS);
197 * This is always called when the mutex (lock) is held, but because
198 * we are waiting we need to release it and reacquire it as soon as the wait
199 * is over. This allows other threads to make use of the object guarded
200 * by the mutex but it should never try to delete it as long as the
201 * number of waiters > 0. Always reacquire the mutex regardless of the
202 * result of the wait. Note that EnterCriticalSection will wait to acquire
206 wait(isc_condition_t *cond, isc_mutex_t *mutex, DWORD milliseconds) {
208 isc_result_t tresult;
209 isc_condition_thread_t *threadcond = NULL;
212 * Get the thread events needed for the wait
214 tresult = find_thread_condition(isc_thread_self(), cond, &threadcond);
215 if (tresult != ISC_R_SUCCESS)
219 LeaveCriticalSection(mutex);
220 result = WaitForMultipleObjects(2, threadcond->handle, FALSE,
222 EnterCriticalSection(mutex);
224 if (result == WAIT_FAILED) {
226 return (ISC_R_UNEXPECTED);
228 if (result == WAIT_TIMEOUT)
229 return (ISC_R_TIMEDOUT);
231 return (ISC_R_SUCCESS);
235 isc_condition_wait(isc_condition_t *cond, isc_mutex_t *mutex) {
236 return (wait(cond, mutex, INFINITE));
240 isc_condition_waituntil(isc_condition_t *cond, isc_mutex_t *mutex,
243 isc_uint64_t microseconds;
246 if (isc_time_now(&now) != ISC_R_SUCCESS) {
248 return (ISC_R_UNEXPECTED);
251 microseconds = isc_time_microdiff(t, &now);
252 if (microseconds > 0xFFFFFFFFi64 * 1000)
253 milliseconds = 0xFFFFFFFF;
255 milliseconds = (DWORD)(microseconds / 1000);
257 return (wait(cond, mutex, milliseconds));