summaryrefslogtreecommitdiffstats
path: root/lib/isc/win32/condition.c
blob: ca11bcea7115254c876d3b75fef5fc5ac1f98d99 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/*
 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
 *
 * SPDX-License-Identifier: MPL-2.0
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0.  If a copy of the MPL was not distributed with this
 * file, you can obtain one at https://mozilla.org/MPL/2.0/.
 *
 * See the COPYRIGHT file distributed with this work for additional
 * information regarding copyright ownership.
 */

#include <inttypes.h>
#include <stdbool.h>

#include <isc/assertions.h>
#include <isc/condition.h>
#include <isc/error.h>
#include <isc/strerr.h>
#include <isc/thread.h>
#include <isc/time.h>
#include <isc/util.h>

#define LSIGNAL	   0
#define LBROADCAST 1

void
isc_condition_init(isc_condition_t *cond) {
	HANDLE h;

	REQUIRE(cond != NULL);

	cond->waiters = 0;
	/*
	 * This handle is shared across all threads
	 */
	h = CreateEvent(NULL, FALSE, FALSE, NULL);
	if (h == NULL) {
		char strbuf[ISC_STRERRORSIZE];
		DWORD err = GetLastError();
		strerror_r(err, strbuf, sizeof(strbuf));
		isc_error_fatal(__FILE__, __LINE__, "CreateEvent failed: %s",
				strbuf);
	}
	cond->events[LSIGNAL] = h;

	/*
	 * The threadlist will hold the actual events needed
	 * for the wait condition
	 */
	ISC_LIST_INIT(cond->threadlist);
}

/*
 * Add the thread to the threadlist along with the required events
 */
static isc_result_t
register_thread(unsigned long thrd, isc_condition_t *gblcond,
		isc_condition_thread_t **localcond) {
	HANDLE hc;
	isc_condition_thread_t *newthread;

	REQUIRE(localcond != NULL && *localcond == NULL);

	newthread = malloc(sizeof(isc_condition_thread_t));
	if (newthread == NULL) {
		return (ISC_R_NOMEMORY);
	}

	/*
	 * Create the thread-specific handle
	 */
	hc = CreateEvent(NULL, FALSE, FALSE, NULL);
	if (hc == NULL) {
		free(newthread);
		return (ISC_R_UNEXPECTED);
	}

	/*
	 * Add the thread ID and handles to list of threads for broadcast
	 */
	newthread->handle[LSIGNAL] = gblcond->events[LSIGNAL];
	newthread->handle[LBROADCAST] = hc;
	newthread->th = thrd;

	/*
	 * The thread is holding the manager lock so this is safe
	 */
	ISC_LINK_INIT(newthread, link);
	ISC_LIST_APPEND(gblcond->threadlist, newthread, link);
	*localcond = newthread;
	return (ISC_R_SUCCESS);
}

static isc_result_t
find_thread_condition(uintptr_t thrd, isc_condition_t *cond,
		      isc_condition_thread_t **threadcondp) {
	isc_condition_thread_t *threadcond;

	REQUIRE(threadcondp != NULL && *threadcondp == NULL);

	/*
	 * Look for the thread ID.
	 */
	for (threadcond = ISC_LIST_HEAD(cond->threadlist); threadcond != NULL;
	     threadcond = ISC_LIST_NEXT(threadcond, link))
	{
		if (threadcond->th == thrd) {
			*threadcondp = threadcond;
			return (ISC_R_SUCCESS);
		}
	}

	/*
	 * Not found, so add it.
	 */
	return (register_thread(thrd, cond, threadcondp));
}

isc_result_t
isc_condition_signal(isc_condition_t *cond) {
	/*
	 * Unlike pthreads, the caller MUST hold the lock associated with
	 * the condition variable when calling us.
	 */
	REQUIRE(cond != NULL);

	if (!SetEvent(cond->events[LSIGNAL])) {
		/* XXX */
		return (ISC_R_UNEXPECTED);
	}
	return (ISC_R_SUCCESS);
}

isc_result_t
isc_condition_broadcast(isc_condition_t *cond) {
	isc_condition_thread_t *threadcond;
	bool failed = false;

	/*
	 * Unlike pthreads, the caller MUST hold the lock associated with
	 * the condition variable when calling us.
	 */
	REQUIRE(cond != NULL);

	/*
	 * Notify every thread registered for this
	 */
	for (threadcond = ISC_LIST_HEAD(cond->threadlist); threadcond != NULL;
	     threadcond = ISC_LIST_NEXT(threadcond, link))
	{
		if (!SetEvent(threadcond->handle[LBROADCAST])) {
			failed = true;
		}
	}

	if (failed) {
		return (ISC_R_UNEXPECTED);
	}

	return (ISC_R_SUCCESS);
}

isc_result_t
isc_condition_destroy(isc_condition_t *cond) {
	isc_condition_thread_t *next, *threadcond;

	REQUIRE(cond != NULL);
	REQUIRE(cond->waiters == 0);

	(void)CloseHandle(cond->events[LSIGNAL]);

	/*
	 * Delete the threadlist
	 */
	threadcond = ISC_LIST_HEAD(cond->threadlist);

	while (threadcond != NULL) {
		next = ISC_LIST_NEXT(threadcond, link);
		DEQUEUE(cond->threadlist, threadcond, link);
		(void)CloseHandle(threadcond->handle[LBROADCAST]);
		free(threadcond);
		threadcond = next;
	}

	return (ISC_R_SUCCESS);
}

/*
 * This is always called when the mutex (lock) is held, but because
 * we are waiting we need to release it and reacquire it as soon as the wait
 * is over. This allows other threads to make use of the object guarded
 * by the mutex but it should never try to delete it as long as the
 * number of waiters > 0. Always reacquire the mutex regardless of the
 * result of the wait. Note that EnterCriticalSection will wait to acquire
 * the mutex.
 */
static isc_result_t
wait(isc_condition_t *cond, isc_mutex_t *mutex, DWORD milliseconds) {
	DWORD result;
	isc_result_t tresult;
	isc_condition_thread_t *threadcond = NULL;

	/*
	 * Get the thread events needed for the wait
	 */
	tresult = find_thread_condition(isc_thread_self(), cond, &threadcond);
	if (tresult != ISC_R_SUCCESS) {
		return (tresult);
	}

	cond->waiters++;
	LeaveCriticalSection(mutex);
	result = WaitForMultipleObjects(2, threadcond->handle, FALSE,
					milliseconds);
	EnterCriticalSection(mutex);
	cond->waiters--;
	if (result == WAIT_FAILED) {
		/* XXX */
		return (ISC_R_UNEXPECTED);
	}
	if (result == WAIT_TIMEOUT) {
		return (ISC_R_TIMEDOUT);
	}

	return (ISC_R_SUCCESS);
}

isc_result_t
isc_condition_wait(isc_condition_t *cond, isc_mutex_t *mutex) {
	return (wait(cond, mutex, INFINITE));
}

isc_result_t
isc_condition_waituntil(isc_condition_t *cond, isc_mutex_t *mutex,
			isc_time_t *t) {
	DWORD milliseconds;
	uint64_t microseconds;
	isc_time_t now;

	if (isc_time_now(&now) != ISC_R_SUCCESS) {
		/* XXX */
		return (ISC_R_UNEXPECTED);
	}

	microseconds = isc_time_microdiff(t, &now);
	if (microseconds > 0xFFFFFFFFi64 * 1000) {
		milliseconds = 0xFFFFFFFF;
	} else {
		milliseconds = (DWORD)(microseconds / 1000);
	}

	return (wait(cond, mutex, milliseconds));
}