xref: /netbsd-src/external/bsd/ntp/dist/libntp/lib/isc/win32/condition.c (revision eabc0478de71e4e011a5b4e0392741e01d491794)
1*eabc0478Schristos /*	$NetBSD: condition.c,v 1.2 2024/08/18 20:47:16 christos Exp $	*/
2897be3a4Schristos 
3897be3a4Schristos /*
4897be3a4Schristos  * Copyright (C) 2004, 2006, 2007  Internet Systems Consortium, Inc. ("ISC")
5897be3a4Schristos  * Copyright (C) 1998-2001  Internet Software Consortium.
6897be3a4Schristos  *
7897be3a4Schristos  * Permission to use, copy, modify, and/or distribute this software for any
8897be3a4Schristos  * purpose with or without fee is hereby granted, provided that the above
9897be3a4Schristos  * copyright notice and this permission notice appear in all copies.
10897be3a4Schristos  *
11897be3a4Schristos  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
12897be3a4Schristos  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13897be3a4Schristos  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14897be3a4Schristos  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15897be3a4Schristos  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16897be3a4Schristos  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17897be3a4Schristos  * PERFORMANCE OF THIS SOFTWARE.
18897be3a4Schristos  */
19897be3a4Schristos 
20897be3a4Schristos /* Id: condition.c,v 1.23 2007/06/18 23:47:49 tbox Exp  */
21897be3a4Schristos 
22897be3a4Schristos #include <config.h>
23897be3a4Schristos 
24897be3a4Schristos #include <isc/condition.h>
25897be3a4Schristos #include <isc/assertions.h>
26897be3a4Schristos #include <isc/util.h>
27897be3a4Schristos #include <isc/thread.h>
28897be3a4Schristos #include <isc/time.h>
29897be3a4Schristos 
30897be3a4Schristos #define LSIGNAL		0
31897be3a4Schristos #define LBROADCAST	1
32897be3a4Schristos 
33897be3a4Schristos isc_result_t
34897be3a4Schristos isc_condition_init(isc_condition_t *cond) {
35897be3a4Schristos 	HANDLE h;
36897be3a4Schristos 
37897be3a4Schristos 	REQUIRE(cond != NULL);
38897be3a4Schristos 
39897be3a4Schristos 	cond->waiters = 0;
40897be3a4Schristos 	/*
41897be3a4Schristos 	 * This handle is shared across all threads
42897be3a4Schristos 	 */
43897be3a4Schristos 	h = CreateEvent(NULL, FALSE, FALSE, NULL);
44897be3a4Schristos 	if (h == NULL) {
45897be3a4Schristos 		/* XXX */
46897be3a4Schristos 		return (ISC_R_UNEXPECTED);
47897be3a4Schristos 	}
48897be3a4Schristos 	cond->events[LSIGNAL] = h;
49897be3a4Schristos 
50897be3a4Schristos 	/*
51897be3a4Schristos 	 * The threadlist will hold the actual events needed
52897be3a4Schristos 	 * for the wait condition
53897be3a4Schristos 	 */
54897be3a4Schristos 	ISC_LIST_INIT(cond->threadlist);
55897be3a4Schristos 
56897be3a4Schristos 	return (ISC_R_SUCCESS);
57897be3a4Schristos }
58897be3a4Schristos 
59897be3a4Schristos /*
60897be3a4Schristos  * Add the thread to the threadlist along with the required events
61897be3a4Schristos  */
62897be3a4Schristos static isc_result_t
63897be3a4Schristos register_thread(unsigned long thrd, isc_condition_t *gblcond,
64897be3a4Schristos 		isc_condition_thread_t **localcond)
65897be3a4Schristos {
66897be3a4Schristos 	HANDLE hc;
67897be3a4Schristos 	isc_condition_thread_t *newthread;
68897be3a4Schristos 
69897be3a4Schristos 	REQUIRE(localcond != NULL && *localcond == NULL);
70897be3a4Schristos 
71897be3a4Schristos 	newthread = malloc(sizeof(isc_condition_thread_t));
72897be3a4Schristos 	if (newthread == NULL)
73897be3a4Schristos 		return (ISC_R_NOMEMORY);
74897be3a4Schristos 
75897be3a4Schristos 	/*
76897be3a4Schristos 	 * Create the thread-specific handle
77897be3a4Schristos 	 */
78897be3a4Schristos 	hc = CreateEvent(NULL, FALSE, FALSE, NULL);
79897be3a4Schristos 	if (hc == NULL) {
80897be3a4Schristos 		free(newthread);
81897be3a4Schristos 		return (ISC_R_UNEXPECTED);
82897be3a4Schristos 	}
83897be3a4Schristos 
84897be3a4Schristos 	/*
85897be3a4Schristos 	 * Add the thread ID and handles to list of threads for broadcast
86897be3a4Schristos 	 */
87897be3a4Schristos 	newthread->handle[LSIGNAL] = gblcond->events[LSIGNAL];
88897be3a4Schristos 	newthread->handle[LBROADCAST] = hc;
89897be3a4Schristos 	newthread->th = thrd;
90897be3a4Schristos 
91897be3a4Schristos 	/*
92897be3a4Schristos 	 * The thread is holding the manager lock so this is safe
93897be3a4Schristos 	 */
94897be3a4Schristos 	ISC_LIST_APPEND(gblcond->threadlist, newthread, link);
95897be3a4Schristos 	*localcond = newthread;
96897be3a4Schristos 	return (ISC_R_SUCCESS);
97897be3a4Schristos }
98897be3a4Schristos 
99897be3a4Schristos static isc_result_t
100897be3a4Schristos find_thread_condition(unsigned long thrd, isc_condition_t *cond,
101897be3a4Schristos 		      isc_condition_thread_t **threadcondp)
102897be3a4Schristos {
103897be3a4Schristos 	isc_condition_thread_t *threadcond;
104897be3a4Schristos 
105897be3a4Schristos 	REQUIRE(threadcondp != NULL && *threadcondp == NULL);
106897be3a4Schristos 
107897be3a4Schristos 	/*
108897be3a4Schristos 	 * Look for the thread ID.
109897be3a4Schristos 	 */
110897be3a4Schristos 	for (threadcond = ISC_LIST_HEAD(cond->threadlist);
111897be3a4Schristos 	     threadcond != NULL;
112897be3a4Schristos 	     threadcond = ISC_LIST_NEXT(threadcond, link)) {
113897be3a4Schristos 
114897be3a4Schristos 		if (threadcond->th == thrd) {
115897be3a4Schristos 			*threadcondp = threadcond;
116897be3a4Schristos 			return (ISC_R_SUCCESS);
117897be3a4Schristos 		}
118897be3a4Schristos 	}
119897be3a4Schristos 
120897be3a4Schristos 	/*
121897be3a4Schristos 	 * Not found, so add it.
122897be3a4Schristos 	 */
123897be3a4Schristos 	return (register_thread(thrd, cond, threadcondp));
124897be3a4Schristos }
125897be3a4Schristos 
126897be3a4Schristos isc_result_t
127897be3a4Schristos isc_condition_signal(isc_condition_t *cond) {
128897be3a4Schristos 
129897be3a4Schristos 	/*
130897be3a4Schristos 	 * Unlike pthreads, the caller MUST hold the lock associated with
131897be3a4Schristos 	 * the condition variable when calling us.
132897be3a4Schristos 	 */
133897be3a4Schristos 	REQUIRE(cond != NULL);
134897be3a4Schristos 
135897be3a4Schristos 	if (!SetEvent(cond->events[LSIGNAL])) {
136897be3a4Schristos 		/* XXX */
137897be3a4Schristos 		return (ISC_R_UNEXPECTED);
138897be3a4Schristos 	}
139897be3a4Schristos 
140897be3a4Schristos 	return (ISC_R_SUCCESS);
141897be3a4Schristos }
142897be3a4Schristos 
143897be3a4Schristos isc_result_t
144897be3a4Schristos isc_condition_broadcast(isc_condition_t *cond) {
145897be3a4Schristos 
146897be3a4Schristos 	isc_condition_thread_t *threadcond;
147897be3a4Schristos 	isc_boolean_t failed = ISC_FALSE;
148897be3a4Schristos 
149897be3a4Schristos 	/*
150897be3a4Schristos 	 * Unlike pthreads, the caller MUST hold the lock associated with
151897be3a4Schristos 	 * the condition variable when calling us.
152897be3a4Schristos 	 */
153897be3a4Schristos 	REQUIRE(cond != NULL);
154897be3a4Schristos 
155897be3a4Schristos 	/*
156897be3a4Schristos 	 * Notify every thread registered for this
157897be3a4Schristos 	 */
158897be3a4Schristos 	for (threadcond = ISC_LIST_HEAD(cond->threadlist);
159897be3a4Schristos 	     threadcond != NULL;
160897be3a4Schristos 	     threadcond = ISC_LIST_NEXT(threadcond, link)) {
161897be3a4Schristos 
162897be3a4Schristos 		if (!SetEvent(threadcond->handle[LBROADCAST]))
163897be3a4Schristos 			failed = ISC_TRUE;
164897be3a4Schristos 	}
165897be3a4Schristos 
166897be3a4Schristos 	if (failed)
167897be3a4Schristos 		return (ISC_R_UNEXPECTED);
168897be3a4Schristos 
169897be3a4Schristos 	return (ISC_R_SUCCESS);
170897be3a4Schristos }
171897be3a4Schristos 
172897be3a4Schristos isc_result_t
173897be3a4Schristos isc_condition_destroy(isc_condition_t *cond) {
174897be3a4Schristos 
175897be3a4Schristos 	isc_condition_thread_t *next, *threadcond;
176897be3a4Schristos 
177897be3a4Schristos 	REQUIRE(cond != NULL);
178897be3a4Schristos 	REQUIRE(cond->waiters == 0);
179897be3a4Schristos 
180897be3a4Schristos 	(void)CloseHandle(cond->events[LSIGNAL]);
181897be3a4Schristos 
182897be3a4Schristos 	/*
183897be3a4Schristos 	 * Delete the threadlist
184897be3a4Schristos 	 */
185897be3a4Schristos 	threadcond = ISC_LIST_HEAD(cond->threadlist);
186897be3a4Schristos 
187897be3a4Schristos 	while (threadcond != NULL) {
188897be3a4Schristos 		next = ISC_LIST_NEXT(threadcond, link);
189897be3a4Schristos 		DEQUEUE(cond->threadlist, threadcond, link);
190897be3a4Schristos 		(void) CloseHandle(threadcond->handle[LBROADCAST]);
191897be3a4Schristos 		free(threadcond);
192897be3a4Schristos 		threadcond = next;
193897be3a4Schristos 	}
194897be3a4Schristos 
195897be3a4Schristos 	return (ISC_R_SUCCESS);
196897be3a4Schristos }
197897be3a4Schristos 
198897be3a4Schristos /*
199897be3a4Schristos  * This is always called when the mutex (lock) is held, but because
200897be3a4Schristos  * we are waiting we need to release it and reacquire it as soon as the wait
201897be3a4Schristos  * is over. This allows other threads to make use of the object guarded
202897be3a4Schristos  * by the mutex but it should never try to delete it as long as the
203897be3a4Schristos  * number of waiters > 0. Always reacquire the mutex regardless of the
204897be3a4Schristos  * result of the wait. Note that EnterCriticalSection will wait to acquire
205897be3a4Schristos  * the mutex.
206897be3a4Schristos  */
207897be3a4Schristos static isc_result_t
208897be3a4Schristos wait(isc_condition_t *cond, isc_mutex_t *mutex, DWORD milliseconds) {
209897be3a4Schristos 	DWORD result;
210897be3a4Schristos 	isc_result_t tresult;
211897be3a4Schristos 	isc_condition_thread_t *threadcond = NULL;
212897be3a4Schristos 
213897be3a4Schristos 	/*
214897be3a4Schristos 	 * Get the thread events needed for the wait
215897be3a4Schristos 	 */
216897be3a4Schristos 	tresult = find_thread_condition(isc_thread_self(), cond, &threadcond);
217897be3a4Schristos 	if (tresult !=  ISC_R_SUCCESS)
218897be3a4Schristos 		return (tresult);
219897be3a4Schristos 
220897be3a4Schristos 	cond->waiters++;
221897be3a4Schristos 	LeaveCriticalSection(mutex);
222897be3a4Schristos 	result = WaitForMultipleObjects(2, threadcond->handle, FALSE,
223897be3a4Schristos 					milliseconds);
224897be3a4Schristos 	EnterCriticalSection(mutex);
225897be3a4Schristos 	cond->waiters--;
226897be3a4Schristos 	if (result == WAIT_FAILED) {
227897be3a4Schristos 		/* XXX */
228897be3a4Schristos 		return (ISC_R_UNEXPECTED);
229897be3a4Schristos 	}
230897be3a4Schristos 	if (result == WAIT_TIMEOUT)
231897be3a4Schristos 		return (ISC_R_TIMEDOUT);
232897be3a4Schristos 
233897be3a4Schristos 	return (ISC_R_SUCCESS);
234897be3a4Schristos }
235897be3a4Schristos 
236897be3a4Schristos isc_result_t
237897be3a4Schristos isc_condition_wait(isc_condition_t *cond, isc_mutex_t *mutex) {
238897be3a4Schristos 	return (wait(cond, mutex, INFINITE));
239897be3a4Schristos }
240897be3a4Schristos 
241897be3a4Schristos isc_result_t
242897be3a4Schristos isc_condition_waituntil(isc_condition_t *cond, isc_mutex_t *mutex,
243897be3a4Schristos 			isc_time_t *t) {
244897be3a4Schristos 	DWORD milliseconds;
245897be3a4Schristos 	isc_uint64_t microseconds;
246897be3a4Schristos 	isc_time_t now;
247897be3a4Schristos 
248897be3a4Schristos 	if (isc_time_now(&now) != ISC_R_SUCCESS) {
249897be3a4Schristos 		/* XXX */
250897be3a4Schristos 		return (ISC_R_UNEXPECTED);
251897be3a4Schristos 	}
252897be3a4Schristos 
253897be3a4Schristos 	microseconds = isc_time_microdiff(t, &now);
254897be3a4Schristos 	if (microseconds > 0xFFFFFFFFi64 * 1000)
255897be3a4Schristos 		milliseconds = 0xFFFFFFFF;
256897be3a4Schristos 	else
257897be3a4Schristos 		milliseconds = (DWORD)(microseconds / 1000);
258897be3a4Schristos 
259897be3a4Schristos 	return (wait(cond, mutex, milliseconds));
260897be3a4Schristos }
261