4 * Copyright (C) 2004, 2006, 2007 Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (C) 1998-2001 Internet Software Consortium.
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
12 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17 * PERFORMANCE OF THIS SOFTWARE.
20 /* Id: condition.c,v 1.23 2007/06/18 23:47:49 tbox Exp */
24 #include <isc/condition.h>
25 #include <isc/assertions.h>
27 #include <isc/thread.h>
34 isc_condition_init(isc_condition_t
*cond
) {
37 REQUIRE(cond
!= NULL
);
41 * This handle is shared across all threads
43 h
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
46 return (ISC_R_UNEXPECTED
);
48 cond
->events
[LSIGNAL
] = h
;
51 * The threadlist will hold the actual events needed
52 * for the wait condition
54 ISC_LIST_INIT(cond
->threadlist
);
56 return (ISC_R_SUCCESS
);
60 * Add the thread to the threadlist along with the required events
63 register_thread(unsigned long thrd
, isc_condition_t
*gblcond
,
64 isc_condition_thread_t
**localcond
)
67 isc_condition_thread_t
*newthread
;
69 REQUIRE(localcond
!= NULL
&& *localcond
== NULL
);
71 newthread
= malloc(sizeof(isc_condition_thread_t
));
72 if (newthread
== NULL
)
73 return (ISC_R_NOMEMORY
);
76 * Create the thread-specific handle
78 hc
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
81 return (ISC_R_UNEXPECTED
);
85 * Add the thread ID and handles to list of threads for broadcast
87 newthread
->handle
[LSIGNAL
] = gblcond
->events
[LSIGNAL
];
88 newthread
->handle
[LBROADCAST
] = hc
;
92 * The thread is holding the manager lock so this is safe
94 ISC_LIST_APPEND(gblcond
->threadlist
, newthread
, link
);
95 *localcond
= newthread
;
96 return (ISC_R_SUCCESS
);
100 find_thread_condition(unsigned long thrd
, isc_condition_t
*cond
,
101 isc_condition_thread_t
**threadcondp
)
103 isc_condition_thread_t
*threadcond
;
105 REQUIRE(threadcondp
!= NULL
&& *threadcondp
== NULL
);
108 * Look for the thread ID.
110 for (threadcond
= ISC_LIST_HEAD(cond
->threadlist
);
112 threadcond
= ISC_LIST_NEXT(threadcond
, link
)) {
114 if (threadcond
->th
== thrd
) {
115 *threadcondp
= threadcond
;
116 return (ISC_R_SUCCESS
);
121 * Not found, so add it.
123 return (register_thread(thrd
, cond
, threadcondp
));
127 isc_condition_signal(isc_condition_t
*cond
) {
130 * Unlike pthreads, the caller MUST hold the lock associated with
131 * the condition variable when calling us.
133 REQUIRE(cond
!= NULL
);
135 if (!SetEvent(cond
->events
[LSIGNAL
])) {
137 return (ISC_R_UNEXPECTED
);
140 return (ISC_R_SUCCESS
);
144 isc_condition_broadcast(isc_condition_t
*cond
) {
146 isc_condition_thread_t
*threadcond
;
147 isc_boolean_t failed
= ISC_FALSE
;
150 * Unlike pthreads, the caller MUST hold the lock associated with
151 * the condition variable when calling us.
153 REQUIRE(cond
!= NULL
);
156 * Notify every thread registered for this
158 for (threadcond
= ISC_LIST_HEAD(cond
->threadlist
);
160 threadcond
= ISC_LIST_NEXT(threadcond
, link
)) {
162 if (!SetEvent(threadcond
->handle
[LBROADCAST
]))
167 return (ISC_R_UNEXPECTED
);
169 return (ISC_R_SUCCESS
);
173 isc_condition_destroy(isc_condition_t
*cond
) {
175 isc_condition_thread_t
*next
, *threadcond
;
177 REQUIRE(cond
!= NULL
);
178 REQUIRE(cond
->waiters
== 0);
180 (void)CloseHandle(cond
->events
[LSIGNAL
]);
183 * Delete the threadlist
185 threadcond
= ISC_LIST_HEAD(cond
->threadlist
);
187 while (threadcond
!= NULL
) {
188 next
= ISC_LIST_NEXT(threadcond
, link
);
189 DEQUEUE(cond
->threadlist
, threadcond
, link
);
190 (void) CloseHandle(threadcond
->handle
[LBROADCAST
]);
195 return (ISC_R_SUCCESS
);
199 * This is always called when the mutex (lock) is held, but because
200 * we are waiting we need to release it and reacquire it as soon as the wait
201 * is over. This allows other threads to make use of the object guarded
202 * by the mutex but it should never try to delete it as long as the
203 * number of waiters > 0. Always reacquire the mutex regardless of the
204 * result of the wait. Note that EnterCriticalSection will wait to acquire
208 wait(isc_condition_t
*cond
, isc_mutex_t
*mutex
, DWORD milliseconds
) {
210 isc_result_t tresult
;
211 isc_condition_thread_t
*threadcond
= NULL
;
214 * Get the thread events needed for the wait
216 tresult
= find_thread_condition(isc_thread_self(), cond
, &threadcond
);
217 if (tresult
!= ISC_R_SUCCESS
)
221 LeaveCriticalSection(mutex
);
222 result
= WaitForMultipleObjects(2, threadcond
->handle
, FALSE
,
224 EnterCriticalSection(mutex
);
226 if (result
== WAIT_FAILED
) {
228 return (ISC_R_UNEXPECTED
);
230 if (result
== WAIT_TIMEOUT
)
231 return (ISC_R_TIMEDOUT
);
233 return (ISC_R_SUCCESS
);
237 isc_condition_wait(isc_condition_t
*cond
, isc_mutex_t
*mutex
) {
238 return (wait(cond
, mutex
, INFINITE
));
242 isc_condition_waituntil(isc_condition_t
*cond
, isc_mutex_t
*mutex
,
245 isc_uint64_t microseconds
;
248 if (isc_time_now(&now
) != ISC_R_SUCCESS
) {
250 return (ISC_R_UNEXPECTED
);
253 microseconds
= isc_time_microdiff(t
, &now
);
254 if (microseconds
> 0xFFFFFFFFi
64 * 1000)
255 milliseconds
= 0xFFFFFFFF;
257 milliseconds
= (DWORD
)(microseconds
/ 1000);
259 return (wait(cond
, mutex
, milliseconds
));