condition.c 5.82 KB
Newer Older
Bob Halley's avatar
Bob Halley committed
1
/*
2
 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
3
 *
4
5
6
 * 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 http://mozilla.org/MPL/2.0/.
7
8
9
 *
 * See the COPYRIGHT file distributed with this work for additional
 * information regarding copyright ownership.
Bob Halley's avatar
Bob Halley committed
10
 */
Bob Halley's avatar
Bob Halley committed
11

Bob Halley's avatar
Bob Halley committed
12
13
#include <config.h>

14
#include <inttypes.h>
15
#include <stdbool.h>
16

Bob Halley's avatar
Bob Halley committed
17
18
#include <isc/condition.h>
#include <isc/assertions.h>
19
#include <isc/error.h>
Danny Mayer's avatar
Danny Mayer committed
20
#include <isc/util.h>
21
#include <isc/strerr.h>
22
#include <isc/thread.h>
Danny Mayer's avatar
Danny Mayer committed
23
#include <isc/time.h>
Bob Halley's avatar
Bob Halley committed
24

Danny Mayer's avatar
Danny Mayer committed
25
26
#define LSIGNAL		0
#define LBROADCAST	1
Bob Halley's avatar
Bob Halley committed
27

28
void
Bob Halley's avatar
Bob Halley committed
29
30
isc_condition_init(isc_condition_t *cond) {
	HANDLE h;
31

Bob Halley's avatar
Bob Halley committed
32
33
34
	REQUIRE(cond != NULL);

	cond->waiters = 0;
35
36
37
	/*
	 * This handle is shared across all threads
	 */
Bob Halley's avatar
Bob Halley committed
38
39
	h = CreateEvent(NULL, FALSE, FALSE, NULL);
	if (h == NULL) {
40
41
42
		char strbuf[ISC_STRERRORSIZE];
		DWORD err = GetLastError();
		strerror_r(err, strbuf, sizeof(strbuf));
Ondřej Surý's avatar
Ondřej Surý committed
43
		isc_error_fatal(__FILE__, __LINE__,
44
				"CreateEvent failed: %s", strbuf);
Bob Halley's avatar
Bob Halley committed
45
	}
Danny Mayer's avatar
Danny Mayer committed
46
	cond->events[LSIGNAL] = h;
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

	/*
	 * 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);
Bob Halley's avatar
Bob Halley committed
77
78
79
		return (ISC_R_UNEXPECTED);
	}

80
81
82
83
84
85
86
87
88
89
	/*
	 * 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
	 */
90
	ISC_LINK_INIT(newthread, link);
91
92
	ISC_LIST_APPEND(gblcond->threadlist, newthread, link);
	*localcond = newthread;
Bob Halley's avatar
Bob Halley committed
93
94
95
	return (ISC_R_SUCCESS);
}

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
static isc_result_t
find_thread_condition(unsigned long 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));
}

Bob Halley's avatar
Bob Halley committed
123
124
isc_result_t
isc_condition_signal(isc_condition_t *cond) {
125

126
127
128
129
	/*
	 * Unlike pthreads, the caller MUST hold the lock associated with
	 * the condition variable when calling us.
	 */
Bob Halley's avatar
Bob Halley committed
130
131
	REQUIRE(cond != NULL);

132
	if (!SetEvent(cond->events[LSIGNAL])) {
Bob Halley's avatar
Bob Halley committed
133
134
135
136
137
138
139
140
		/* XXX */
		return (ISC_R_UNEXPECTED);
	}
	return (ISC_R_SUCCESS);
}

isc_result_t
isc_condition_broadcast(isc_condition_t *cond) {
141

142
	isc_condition_thread_t *threadcond;
143
	bool failed = false;
144

145
146
147
148
	/*
	 * Unlike pthreads, the caller MUST hold the lock associated with
	 * the condition variable when calling us.
	 */
Bob Halley's avatar
Bob Halley committed
149
150
	REQUIRE(cond != NULL);

151
152
153
154
155
156
157
158
	/*
	 * 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]))
159
			failed = true;
Bob Halley's avatar
Bob Halley committed
160
161
	}

162
163
164
	if (failed)
		return (ISC_R_UNEXPECTED);

Bob Halley's avatar
Bob Halley committed
165
	return (ISC_R_SUCCESS);
Bob Halley's avatar
add    
Bob Halley committed
166
}
Bob Halley's avatar
Bob Halley committed
167
168
169

isc_result_t
isc_condition_destroy(isc_condition_t *cond) {
170

171
172
	isc_condition_thread_t *next, *threadcond;

Bob Halley's avatar
Bob Halley committed
173
	REQUIRE(cond != NULL);
174
	REQUIRE(cond->waiters == 0);
Bob Halley's avatar
Bob Halley committed
175

Danny Mayer's avatar
Danny Mayer committed
176
	(void)CloseHandle(cond->events[LSIGNAL]);
177
178
179
180
181
182
183
184
185
186
187
188
189

	/*
	 * 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;
	}
Bob Halley's avatar
Bob Halley committed
190
191
192
193

	return (ISC_R_SUCCESS);
}

194
195
196
197
198
199
200
201
202
/*
 * 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.
 */
203
static isc_result_t
Bob Halley's avatar
Bob Halley committed
204
205
wait(isc_condition_t *cond, isc_mutex_t *mutex, DWORD milliseconds) {
	DWORD result;
206
207
208
209
210
211
212
213
214
	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);
Bob Halley's avatar
Bob Halley committed
215
216
217

	cond->waiters++;
	LeaveCriticalSection(mutex);
218
219
	result = WaitForMultipleObjects(2, threadcond->handle, FALSE,
					milliseconds);
220
221
	EnterCriticalSection(mutex);
	cond->waiters--;
Bob Halley's avatar
Bob Halley committed
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
	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
Bob Halley's avatar
add    
Bob Halley committed
238
isc_condition_waituntil(isc_condition_t *cond, isc_mutex_t *mutex,
Danny Mayer's avatar
Danny Mayer committed
239
			isc_time_t *t) {
Bob Halley's avatar
Bob Halley committed
240
	DWORD milliseconds;
241
	uint64_t microseconds;
242
	isc_time_t now;
Bob Halley's avatar
Bob Halley committed
243

244
	if (isc_time_now(&now) != ISC_R_SUCCESS) {
Bob Halley's avatar
update    
Bob Halley committed
245
246
247
		/* XXX */
		return (ISC_R_UNEXPECTED);
	}
248
249
250
251
252
253
254

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

Bob Halley's avatar
Bob Halley committed
255
	return (wait(cond, mutex, milliseconds));
Bob Halley's avatar
add    
Bob Halley committed
256
}