1*a466cc55SCy Schubert /*
2*a466cc55SCy Schubert * Copyright (C) 2004, 2006, 2007 Internet Systems Consortium, Inc. ("ISC")
3*a466cc55SCy Schubert * Copyright (C) 1998-2001 Internet Software Consortium.
4*a466cc55SCy Schubert *
5*a466cc55SCy Schubert * Permission to use, copy, modify, and/or distribute this software for any
6*a466cc55SCy Schubert * purpose with or without fee is hereby granted, provided that the above
7*a466cc55SCy Schubert * copyright notice and this permission notice appear in all copies.
8*a466cc55SCy Schubert *
9*a466cc55SCy Schubert * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10*a466cc55SCy Schubert * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11*a466cc55SCy Schubert * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12*a466cc55SCy Schubert * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13*a466cc55SCy Schubert * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14*a466cc55SCy Schubert * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15*a466cc55SCy Schubert * PERFORMANCE OF THIS SOFTWARE.
16*a466cc55SCy Schubert */
17*a466cc55SCy Schubert
18*a466cc55SCy Schubert /* $Id: condition.c,v 1.23 2007/06/18 23:47:49 tbox Exp $ */
19*a466cc55SCy Schubert
20*a466cc55SCy Schubert #include <config.h>
21*a466cc55SCy Schubert
22*a466cc55SCy Schubert #include <isc/condition.h>
23*a466cc55SCy Schubert #include <isc/assertions.h>
24*a466cc55SCy Schubert #include <isc/util.h>
25*a466cc55SCy Schubert #include <isc/thread.h>
26*a466cc55SCy Schubert #include <isc/time.h>
27*a466cc55SCy Schubert
28*a466cc55SCy Schubert #define LSIGNAL 0
29*a466cc55SCy Schubert #define LBROADCAST 1
30*a466cc55SCy Schubert
31*a466cc55SCy Schubert isc_result_t
isc_condition_init(isc_condition_t * cond)32*a466cc55SCy Schubert isc_condition_init(isc_condition_t *cond) {
33*a466cc55SCy Schubert HANDLE h;
34*a466cc55SCy Schubert
35*a466cc55SCy Schubert REQUIRE(cond != NULL);
36*a466cc55SCy Schubert
37*a466cc55SCy Schubert cond->waiters = 0;
38*a466cc55SCy Schubert /*
39*a466cc55SCy Schubert * This handle is shared across all threads
40*a466cc55SCy Schubert */
41*a466cc55SCy Schubert h = CreateEvent(NULL, FALSE, FALSE, NULL);
42*a466cc55SCy Schubert if (h == NULL) {
43*a466cc55SCy Schubert /* XXX */
44*a466cc55SCy Schubert return (ISC_R_UNEXPECTED);
45*a466cc55SCy Schubert }
46*a466cc55SCy Schubert cond->events[LSIGNAL] = h;
47*a466cc55SCy Schubert
48*a466cc55SCy Schubert /*
49*a466cc55SCy Schubert * The threadlist will hold the actual events needed
50*a466cc55SCy Schubert * for the wait condition
51*a466cc55SCy Schubert */
52*a466cc55SCy Schubert ISC_LIST_INIT(cond->threadlist);
53*a466cc55SCy Schubert
54*a466cc55SCy Schubert return (ISC_R_SUCCESS);
55*a466cc55SCy Schubert }
56*a466cc55SCy Schubert
57*a466cc55SCy Schubert /*
58*a466cc55SCy Schubert * Add the thread to the threadlist along with the required events
59*a466cc55SCy Schubert */
60*a466cc55SCy Schubert static isc_result_t
register_thread(unsigned long thrd,isc_condition_t * gblcond,isc_condition_thread_t ** localcond)61*a466cc55SCy Schubert register_thread(unsigned long thrd, isc_condition_t *gblcond,
62*a466cc55SCy Schubert isc_condition_thread_t **localcond)
63*a466cc55SCy Schubert {
64*a466cc55SCy Schubert HANDLE hc;
65*a466cc55SCy Schubert isc_condition_thread_t *newthread;
66*a466cc55SCy Schubert
67*a466cc55SCy Schubert REQUIRE(localcond != NULL && *localcond == NULL);
68*a466cc55SCy Schubert
69*a466cc55SCy Schubert newthread = malloc(sizeof(isc_condition_thread_t));
70*a466cc55SCy Schubert if (newthread == NULL)
71*a466cc55SCy Schubert return (ISC_R_NOMEMORY);
72*a466cc55SCy Schubert
73*a466cc55SCy Schubert /*
74*a466cc55SCy Schubert * Create the thread-specific handle
75*a466cc55SCy Schubert */
76*a466cc55SCy Schubert hc = CreateEvent(NULL, FALSE, FALSE, NULL);
77*a466cc55SCy Schubert if (hc == NULL) {
78*a466cc55SCy Schubert free(newthread);
79*a466cc55SCy Schubert return (ISC_R_UNEXPECTED);
80*a466cc55SCy Schubert }
81*a466cc55SCy Schubert
82*a466cc55SCy Schubert /*
83*a466cc55SCy Schubert * Add the thread ID and handles to list of threads for broadcast
84*a466cc55SCy Schubert */
85*a466cc55SCy Schubert newthread->handle[LSIGNAL] = gblcond->events[LSIGNAL];
86*a466cc55SCy Schubert newthread->handle[LBROADCAST] = hc;
87*a466cc55SCy Schubert newthread->th = thrd;
88*a466cc55SCy Schubert
89*a466cc55SCy Schubert /*
90*a466cc55SCy Schubert * The thread is holding the manager lock so this is safe
91*a466cc55SCy Schubert */
92*a466cc55SCy Schubert ISC_LIST_APPEND(gblcond->threadlist, newthread, link);
93*a466cc55SCy Schubert *localcond = newthread;
94*a466cc55SCy Schubert return (ISC_R_SUCCESS);
95*a466cc55SCy Schubert }
96*a466cc55SCy Schubert
97*a466cc55SCy Schubert static isc_result_t
find_thread_condition(unsigned long thrd,isc_condition_t * cond,isc_condition_thread_t ** threadcondp)98*a466cc55SCy Schubert find_thread_condition(unsigned long thrd, isc_condition_t *cond,
99*a466cc55SCy Schubert isc_condition_thread_t **threadcondp)
100*a466cc55SCy Schubert {
101*a466cc55SCy Schubert isc_condition_thread_t *threadcond;
102*a466cc55SCy Schubert
103*a466cc55SCy Schubert REQUIRE(threadcondp != NULL && *threadcondp == NULL);
104*a466cc55SCy Schubert
105*a466cc55SCy Schubert /*
106*a466cc55SCy Schubert * Look for the thread ID.
107*a466cc55SCy Schubert */
108*a466cc55SCy Schubert for (threadcond = ISC_LIST_HEAD(cond->threadlist);
109*a466cc55SCy Schubert threadcond != NULL;
110*a466cc55SCy Schubert threadcond = ISC_LIST_NEXT(threadcond, link)) {
111*a466cc55SCy Schubert
112*a466cc55SCy Schubert if (threadcond->th == thrd) {
113*a466cc55SCy Schubert *threadcondp = threadcond;
114*a466cc55SCy Schubert return (ISC_R_SUCCESS);
115*a466cc55SCy Schubert }
116*a466cc55SCy Schubert }
117*a466cc55SCy Schubert
118*a466cc55SCy Schubert /*
119*a466cc55SCy Schubert * Not found, so add it.
120*a466cc55SCy Schubert */
121*a466cc55SCy Schubert return (register_thread(thrd, cond, threadcondp));
122*a466cc55SCy Schubert }
123*a466cc55SCy Schubert
124*a466cc55SCy Schubert isc_result_t
isc_condition_signal(isc_condition_t * cond)125*a466cc55SCy Schubert isc_condition_signal(isc_condition_t *cond) {
126*a466cc55SCy Schubert
127*a466cc55SCy Schubert /*
128*a466cc55SCy Schubert * Unlike pthreads, the caller MUST hold the lock associated with
129*a466cc55SCy Schubert * the condition variable when calling us.
130*a466cc55SCy Schubert */
131*a466cc55SCy Schubert REQUIRE(cond != NULL);
132*a466cc55SCy Schubert
133*a466cc55SCy Schubert if (!SetEvent(cond->events[LSIGNAL])) {
134*a466cc55SCy Schubert /* XXX */
135*a466cc55SCy Schubert return (ISC_R_UNEXPECTED);
136*a466cc55SCy Schubert }
137*a466cc55SCy Schubert
138*a466cc55SCy Schubert return (ISC_R_SUCCESS);
139*a466cc55SCy Schubert }
140*a466cc55SCy Schubert
141*a466cc55SCy Schubert isc_result_t
isc_condition_broadcast(isc_condition_t * cond)142*a466cc55SCy Schubert isc_condition_broadcast(isc_condition_t *cond) {
143*a466cc55SCy Schubert
144*a466cc55SCy Schubert isc_condition_thread_t *threadcond;
145*a466cc55SCy Schubert isc_boolean_t failed = ISC_FALSE;
146*a466cc55SCy Schubert
147*a466cc55SCy Schubert /*
148*a466cc55SCy Schubert * Unlike pthreads, the caller MUST hold the lock associated with
149*a466cc55SCy Schubert * the condition variable when calling us.
150*a466cc55SCy Schubert */
151*a466cc55SCy Schubert REQUIRE(cond != NULL);
152*a466cc55SCy Schubert
153*a466cc55SCy Schubert /*
154*a466cc55SCy Schubert * Notify every thread registered for this
155*a466cc55SCy Schubert */
156*a466cc55SCy Schubert for (threadcond = ISC_LIST_HEAD(cond->threadlist);
157*a466cc55SCy Schubert threadcond != NULL;
158*a466cc55SCy Schubert threadcond = ISC_LIST_NEXT(threadcond, link)) {
159*a466cc55SCy Schubert
160*a466cc55SCy Schubert if (!SetEvent(threadcond->handle[LBROADCAST]))
161*a466cc55SCy Schubert failed = ISC_TRUE;
162*a466cc55SCy Schubert }
163*a466cc55SCy Schubert
164*a466cc55SCy Schubert if (failed)
165*a466cc55SCy Schubert return (ISC_R_UNEXPECTED);
166*a466cc55SCy Schubert
167*a466cc55SCy Schubert return (ISC_R_SUCCESS);
168*a466cc55SCy Schubert }
169*a466cc55SCy Schubert
170*a466cc55SCy Schubert isc_result_t
isc_condition_destroy(isc_condition_t * cond)171*a466cc55SCy Schubert isc_condition_destroy(isc_condition_t *cond) {
172*a466cc55SCy Schubert
173*a466cc55SCy Schubert isc_condition_thread_t *next, *threadcond;
174*a466cc55SCy Schubert
175*a466cc55SCy Schubert REQUIRE(cond != NULL);
176*a466cc55SCy Schubert REQUIRE(cond->waiters == 0);
177*a466cc55SCy Schubert
178*a466cc55SCy Schubert (void)CloseHandle(cond->events[LSIGNAL]);
179*a466cc55SCy Schubert
180*a466cc55SCy Schubert /*
181*a466cc55SCy Schubert * Delete the threadlist
182*a466cc55SCy Schubert */
183*a466cc55SCy Schubert threadcond = ISC_LIST_HEAD(cond->threadlist);
184*a466cc55SCy Schubert
185*a466cc55SCy Schubert while (threadcond != NULL) {
186*a466cc55SCy Schubert next = ISC_LIST_NEXT(threadcond, link);
187*a466cc55SCy Schubert DEQUEUE(cond->threadlist, threadcond, link);
188*a466cc55SCy Schubert (void) CloseHandle(threadcond->handle[LBROADCAST]);
189*a466cc55SCy Schubert free(threadcond);
190*a466cc55SCy Schubert threadcond = next;
191*a466cc55SCy Schubert }
192*a466cc55SCy Schubert
193*a466cc55SCy Schubert return (ISC_R_SUCCESS);
194*a466cc55SCy Schubert }
195*a466cc55SCy Schubert
196*a466cc55SCy Schubert /*
197*a466cc55SCy Schubert * This is always called when the mutex (lock) is held, but because
198*a466cc55SCy Schubert * we are waiting we need to release it and reacquire it as soon as the wait
199*a466cc55SCy Schubert * is over. This allows other threads to make use of the object guarded
200*a466cc55SCy Schubert * by the mutex but it should never try to delete it as long as the
201*a466cc55SCy Schubert * number of waiters > 0. Always reacquire the mutex regardless of the
202*a466cc55SCy Schubert * result of the wait. Note that EnterCriticalSection will wait to acquire
203*a466cc55SCy Schubert * the mutex.
204*a466cc55SCy Schubert */
205*a466cc55SCy Schubert static isc_result_t
wait(isc_condition_t * cond,isc_mutex_t * mutex,DWORD milliseconds)206*a466cc55SCy Schubert wait(isc_condition_t *cond, isc_mutex_t *mutex, DWORD milliseconds) {
207*a466cc55SCy Schubert DWORD result;
208*a466cc55SCy Schubert isc_result_t tresult;
209*a466cc55SCy Schubert isc_condition_thread_t *threadcond = NULL;
210*a466cc55SCy Schubert
211*a466cc55SCy Schubert /*
212*a466cc55SCy Schubert * Get the thread events needed for the wait
213*a466cc55SCy Schubert */
214*a466cc55SCy Schubert tresult = find_thread_condition(isc_thread_self(), cond, &threadcond);
215*a466cc55SCy Schubert if (tresult != ISC_R_SUCCESS)
216*a466cc55SCy Schubert return (tresult);
217*a466cc55SCy Schubert
218*a466cc55SCy Schubert cond->waiters++;
219*a466cc55SCy Schubert LeaveCriticalSection(mutex);
220*a466cc55SCy Schubert result = WaitForMultipleObjects(2, threadcond->handle, FALSE,
221*a466cc55SCy Schubert milliseconds);
222*a466cc55SCy Schubert EnterCriticalSection(mutex);
223*a466cc55SCy Schubert cond->waiters--;
224*a466cc55SCy Schubert if (result == WAIT_FAILED) {
225*a466cc55SCy Schubert /* XXX */
226*a466cc55SCy Schubert return (ISC_R_UNEXPECTED);
227*a466cc55SCy Schubert }
228*a466cc55SCy Schubert if (result == WAIT_TIMEOUT)
229*a466cc55SCy Schubert return (ISC_R_TIMEDOUT);
230*a466cc55SCy Schubert
231*a466cc55SCy Schubert return (ISC_R_SUCCESS);
232*a466cc55SCy Schubert }
233*a466cc55SCy Schubert
234*a466cc55SCy Schubert isc_result_t
isc_condition_wait(isc_condition_t * cond,isc_mutex_t * mutex)235*a466cc55SCy Schubert isc_condition_wait(isc_condition_t *cond, isc_mutex_t *mutex) {
236*a466cc55SCy Schubert return (wait(cond, mutex, INFINITE));
237*a466cc55SCy Schubert }
238*a466cc55SCy Schubert
239*a466cc55SCy Schubert isc_result_t
isc_condition_waituntil(isc_condition_t * cond,isc_mutex_t * mutex,isc_time_t * t)240*a466cc55SCy Schubert isc_condition_waituntil(isc_condition_t *cond, isc_mutex_t *mutex,
241*a466cc55SCy Schubert isc_time_t *t) {
242*a466cc55SCy Schubert DWORD milliseconds;
243*a466cc55SCy Schubert isc_uint64_t microseconds;
244*a466cc55SCy Schubert isc_time_t now;
245*a466cc55SCy Schubert
246*a466cc55SCy Schubert if (isc_time_now(&now) != ISC_R_SUCCESS) {
247*a466cc55SCy Schubert /* XXX */
248*a466cc55SCy Schubert return (ISC_R_UNEXPECTED);
249*a466cc55SCy Schubert }
250*a466cc55SCy Schubert
251*a466cc55SCy Schubert microseconds = isc_time_microdiff(t, &now);
252*a466cc55SCy Schubert if (microseconds > 0xFFFFFFFFi64 * 1000)
253*a466cc55SCy Schubert milliseconds = 0xFFFFFFFF;
254*a466cc55SCy Schubert else
255*a466cc55SCy Schubert milliseconds = (DWORD)(microseconds / 1000);
256*a466cc55SCy Schubert
257*a466cc55SCy Schubert return (wait(cond, mutex, milliseconds));
258*a466cc55SCy Schubert }
259