1998-12-12 20:48:14 +00:00
|
|
|
/*
|
2004-03-05 05:14:21 +00:00
|
|
|
* Copyright (C) 2004 Internet Systems Consortium, Inc. ("ISC")
|
2001-01-09 22:01:04 +00:00
|
|
|
* Copyright (C) 1998-2001 Internet Software Consortium.
|
2000-08-01 01:33:37 +00:00
|
|
|
*
|
1998-12-12 20:48:14 +00:00
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
2000-08-01 01:33:37 +00:00
|
|
|
*
|
2004-03-05 05:14:21 +00:00
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
|
|
|
|
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
|
|
* AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
|
|
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
|
|
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
|
|
|
|
* OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
|
|
* PERFORMANCE OF THIS SOFTWARE.
|
1998-12-12 20:48:14 +00:00
|
|
|
*/
|
1998-10-23 06:02:07 +00:00
|
|
|
|
2004-03-05 05:14:21 +00:00
|
|
|
/* $Id: condition.c,v 1.18 2004/03/05 05:11:56 marka Exp $ */
|
2000-06-22 22:00:42 +00:00
|
|
|
|
1998-12-12 19:25:20 +00:00
|
|
|
#include <config.h>
|
|
|
|
|
1998-10-23 06:02:07 +00:00
|
|
|
#include <isc/condition.h>
|
|
|
|
#include <isc/assertions.h>
|
2001-07-06 05:08:39 +00:00
|
|
|
#include <isc/util.h>
|
|
|
|
#include <isc/time.h>
|
1998-10-23 06:02:07 +00:00
|
|
|
|
2001-07-06 05:08:39 +00:00
|
|
|
#define LSIGNAL 0
|
|
|
|
#define LBROADCAST 1
|
1998-10-23 06:02:07 +00:00
|
|
|
|
|
|
|
isc_result_t
|
|
|
|
isc_condition_init(isc_condition_t *cond) {
|
|
|
|
HANDLE h;
|
2000-08-01 01:33:37 +00:00
|
|
|
|
1998-10-23 06:02:07 +00:00
|
|
|
REQUIRE(cond != NULL);
|
|
|
|
|
|
|
|
cond->waiters = 0;
|
|
|
|
h = CreateEvent(NULL, FALSE, FALSE, NULL);
|
|
|
|
if (h == NULL) {
|
|
|
|
/* XXX */
|
|
|
|
return (ISC_R_UNEXPECTED);
|
|
|
|
}
|
2001-07-06 05:08:39 +00:00
|
|
|
cond->events[LSIGNAL] = h;
|
1998-10-23 06:02:07 +00:00
|
|
|
h = CreateEvent(NULL, TRUE, FALSE, NULL);
|
|
|
|
if (h == NULL) {
|
2001-07-06 05:08:39 +00:00
|
|
|
(void)CloseHandle(cond->events[LSIGNAL]);
|
1998-10-23 06:02:07 +00:00
|
|
|
/* XXX */
|
|
|
|
return (ISC_R_UNEXPECTED);
|
|
|
|
}
|
2001-07-06 05:08:39 +00:00
|
|
|
cond->events[LBROADCAST] = h;
|
1998-10-23 06:02:07 +00:00
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
isc_result_t
|
|
|
|
isc_condition_signal(isc_condition_t *cond) {
|
2000-08-01 01:33:37 +00:00
|
|
|
|
1998-10-23 18:22:22 +00:00
|
|
|
/*
|
|
|
|
* Unlike pthreads, the caller MUST hold the lock associated with
|
|
|
|
* the condition variable when calling us.
|
|
|
|
*/
|
1998-10-23 06:02:07 +00:00
|
|
|
REQUIRE(cond != NULL);
|
|
|
|
|
|
|
|
if (cond->waiters > 0 &&
|
2001-07-06 05:08:39 +00:00
|
|
|
!SetEvent(cond->events[LSIGNAL])) {
|
1998-10-23 06:02:07 +00:00
|
|
|
/* XXX */
|
|
|
|
return (ISC_R_UNEXPECTED);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
isc_result_t
|
|
|
|
isc_condition_broadcast(isc_condition_t *cond) {
|
2000-08-01 01:33:37 +00:00
|
|
|
|
1998-10-23 18:22:22 +00:00
|
|
|
/*
|
|
|
|
* Unlike pthreads, the caller MUST hold the lock associated with
|
|
|
|
* the condition variable when calling us.
|
|
|
|
*/
|
1998-10-23 06:02:07 +00:00
|
|
|
REQUIRE(cond != NULL);
|
|
|
|
|
|
|
|
if (cond->waiters > 0 &&
|
2001-07-06 05:08:39 +00:00
|
|
|
!SetEvent(cond->events[LBROADCAST])) {
|
1998-10-23 06:02:07 +00:00
|
|
|
/* XXX */
|
|
|
|
return (ISC_R_UNEXPECTED);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
1998-10-23 05:45:44 +00:00
|
|
|
}
|
1998-10-23 06:02:07 +00:00
|
|
|
|
|
|
|
isc_result_t
|
|
|
|
isc_condition_destroy(isc_condition_t *cond) {
|
2000-08-01 01:33:37 +00:00
|
|
|
|
1998-10-23 06:02:07 +00:00
|
|
|
REQUIRE(cond != NULL);
|
|
|
|
|
2001-07-06 05:08:39 +00:00
|
|
|
(void)CloseHandle(cond->events[LSIGNAL]);
|
|
|
|
(void)CloseHandle(cond->events[LBROADCAST]);
|
1998-10-23 06:02:07 +00:00
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
1998-10-23 18:22:22 +00:00
|
|
|
static isc_result_t
|
1998-10-23 06:02:07 +00:00
|
|
|
wait(isc_condition_t *cond, isc_mutex_t *mutex, DWORD milliseconds) {
|
|
|
|
DWORD result;
|
|
|
|
|
|
|
|
cond->waiters++;
|
|
|
|
LeaveCriticalSection(mutex);
|
|
|
|
result = WaitForMultipleObjects(2, cond->events, FALSE, milliseconds);
|
|
|
|
if (result == WAIT_FAILED) {
|
|
|
|
/* XXX */
|
|
|
|
return (ISC_R_UNEXPECTED);
|
|
|
|
}
|
|
|
|
EnterCriticalSection(mutex);
|
|
|
|
cond->waiters--;
|
|
|
|
if (cond->waiters == 0 &&
|
2001-07-06 05:08:39 +00:00
|
|
|
!ResetEvent(cond->events[LBROADCAST])) {
|
1998-10-23 06:02:07 +00:00
|
|
|
/* XXX */
|
|
|
|
LeaveCriticalSection(mutex);
|
|
|
|
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
|
1998-10-23 05:45:44 +00:00
|
|
|
isc_condition_waituntil(isc_condition_t *cond, isc_mutex_t *mutex,
|
2001-07-08 05:09:35 +00:00
|
|
|
isc_time_t *t) {
|
1998-10-23 06:02:07 +00:00
|
|
|
DWORD milliseconds;
|
1999-10-09 02:39:53 +00:00
|
|
|
isc_uint64_t microseconds;
|
|
|
|
isc_time_t now;
|
1998-10-23 06:02:07 +00:00
|
|
|
|
1999-09-23 18:06:47 +00:00
|
|
|
if (isc_time_now(&now) != ISC_R_SUCCESS) {
|
1998-10-27 03:12:07 +00:00
|
|
|
/* XXX */
|
|
|
|
return (ISC_R_UNEXPECTED);
|
|
|
|
}
|
1999-10-09 02:39:53 +00:00
|
|
|
|
|
|
|
microseconds = isc_time_microdiff(t, &now);
|
|
|
|
if (microseconds > 0xFFFFFFFFi64 * 1000)
|
|
|
|
milliseconds = 0xFFFFFFFF;
|
|
|
|
else
|
|
|
|
milliseconds = (DWORD)(microseconds / 1000);
|
|
|
|
|
1998-10-23 06:02:07 +00:00
|
|
|
return (wait(cond, mutex, milliseconds));
|
1998-10-23 05:45:44 +00:00
|
|
|
}
|