2 * events.c: Event handles
5 * Dick Porter (dick@ximian.com)
7 * (C) 2002 Ximian, Inc.
15 #include <mono/io-layer/wapi.h>
16 #include <mono/io-layer/wapi-private.h>
17 #include <mono/io-layer/handles-private.h>
18 #include <mono/io-layer/event-private.h>
19 #include <mono/io-layer/io-trace.h>
20 #include <mono/utils/mono-once.h>
21 #include <mono/utils/mono-logger-internals.h>
23 static void event_signal(gpointer handle);
24 static gboolean event_own (gpointer handle);
26 static void namedevent_signal (gpointer handle);
27 static gboolean namedevent_own (gpointer handle);
29 struct _WapiHandleOps _wapi_event_ops = {
31 event_signal, /* signal */
34 NULL, /* special_wait */
38 struct _WapiHandleOps _wapi_namedevent_ops = {
40 namedevent_signal, /* signal */
41 namedevent_own, /* own */
45 static gboolean event_pulse (gpointer handle);
46 static gboolean event_reset (gpointer handle);
47 static gboolean event_set (gpointer handle);
49 static gboolean namedevent_pulse (gpointer handle);
50 static gboolean namedevent_reset (gpointer handle);
51 static gboolean namedevent_set (gpointer handle);
55 gboolean (*pulse)(gpointer handle);
56 gboolean (*reset)(gpointer handle);
57 gboolean (*set)(gpointer handle);
58 } event_ops[WAPI_HANDLE_COUNT] = {
65 {event_pulse, event_reset, event_set},
72 {namedevent_pulse, namedevent_reset, namedevent_set},
75 void _wapi_event_details (gpointer handle_info)
77 struct _WapiHandle_event *event = (struct _WapiHandle_event *)handle_info;
79 g_print ("manual: %s", event->manual?"TRUE":"FALSE");
82 static mono_once_t event_ops_once=MONO_ONCE_INIT;
84 static void event_ops_init (void)
86 _wapi_handle_register_capabilities (WAPI_HANDLE_EVENT,
87 (WapiHandleCapability)(WAPI_HANDLE_CAP_WAIT | WAPI_HANDLE_CAP_SIGNAL));
88 _wapi_handle_register_capabilities (WAPI_HANDLE_NAMEDEVENT,
89 (WapiHandleCapability)(WAPI_HANDLE_CAP_WAIT | WAPI_HANDLE_CAP_SIGNAL));
92 static void event_signal(gpointer handle)
97 static gboolean event_own (gpointer handle)
99 struct _WapiHandle_event *event_handle;
102 ok=_wapi_lookup_handle (handle, WAPI_HANDLE_EVENT,
103 (gpointer *)&event_handle);
105 g_warning ("%s: error looking up event handle %p", __func__,
110 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: owning event handle %p", __func__, handle);
112 if(event_handle->manual==FALSE) {
113 g_assert (event_handle->set_count > 0);
115 if (--event_handle->set_count == 0) {
116 _wapi_handle_set_signal_state (handle, FALSE, FALSE);
123 static void namedevent_signal (gpointer handle)
128 /* NB, always called with the shared handle lock held */
129 static gboolean namedevent_own (gpointer handle)
131 struct _WapiHandle_namedevent *namedevent_handle;
134 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: owning named event handle %p", __func__, handle);
136 ok = _wapi_lookup_handle (handle, WAPI_HANDLE_NAMEDEVENT,
137 (gpointer *)&namedevent_handle);
139 g_warning ("%s: error looking up named event handle %p",
144 if (namedevent_handle->manual == FALSE) {
145 g_assert (namedevent_handle->set_count > 0);
147 if (--namedevent_handle->set_count == 0) {
148 _wapi_handle_set_signal_state (handle, FALSE, FALSE);
154 static gpointer event_create (WapiSecurityAttributes *security G_GNUC_UNUSED,
155 gboolean manual, gboolean initial)
157 struct _WapiHandle_event event_handle = {0};
161 /* Need to blow away any old errors here, because code tests
162 * for ERROR_ALREADY_EXISTS on success (!) to see if an event
163 * was freshly created
165 SetLastError (ERROR_SUCCESS);
167 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Creating unnamed event", __func__);
169 event_handle.manual = manual;
170 event_handle.set_count = 0;
172 if (initial == TRUE) {
173 if (manual == FALSE) {
174 event_handle.set_count = 1;
178 handle = _wapi_handle_new (WAPI_HANDLE_EVENT, &event_handle);
179 if (handle == _WAPI_HANDLE_INVALID) {
180 g_warning ("%s: error creating event handle", __func__);
181 SetLastError (ERROR_GEN_FAILURE);
185 thr_ret = _wapi_handle_lock_handle (handle);
186 g_assert (thr_ret == 0);
188 if (initial == TRUE) {
189 _wapi_handle_set_signal_state (handle, TRUE, FALSE);
192 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: created new event handle %p", __func__, handle);
194 thr_ret = _wapi_handle_unlock_handle (handle);
195 g_assert (thr_ret == 0);
200 static gpointer namedevent_create (WapiSecurityAttributes *security G_GNUC_UNUSED,
201 gboolean manual, gboolean initial,
202 const gunichar2 *name G_GNUC_UNUSED)
204 struct _WapiHandle_namedevent namedevent_handle = {{{0}}, 0};
209 /* w32 seems to guarantee that opening named objects can't
212 thr_ret = _wapi_namespace_lock ();
213 g_assert (thr_ret == 0);
215 /* Need to blow away any old errors here, because code tests
216 * for ERROR_ALREADY_EXISTS on success (!) to see if an event
217 * was freshly created
219 SetLastError (ERROR_SUCCESS);
221 utf8_name = g_utf16_to_utf8 (name, -1, NULL, NULL, NULL);
223 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Creating named event [%s]", __func__, utf8_name);
225 handle = _wapi_search_handle_namespace (WAPI_HANDLE_NAMEDEVENT,
227 if (handle == _WAPI_HANDLE_INVALID) {
228 /* The name has already been used for a different
231 SetLastError (ERROR_INVALID_HANDLE);
234 /* Not an error, but this is how the caller is
235 * informed that the event wasn't freshly created
237 SetLastError (ERROR_ALREADY_EXISTS);
239 /* A new named event, so create both the private and
243 strncpy (&namedevent_handle.sharedns.name [0], utf8_name, MAX_PATH);
244 namedevent_handle.sharedns.name [MAX_PATH] = '\0';
246 namedevent_handle.manual = manual;
247 namedevent_handle.set_count = 0;
249 if (initial == TRUE) {
250 if (manual == FALSE) {
251 namedevent_handle.set_count = 1;
255 handle = _wapi_handle_new (WAPI_HANDLE_NAMEDEVENT,
258 if (handle == _WAPI_HANDLE_INVALID) {
259 g_warning ("%s: error creating event handle", __func__);
260 SetLastError (ERROR_GEN_FAILURE);
264 /* Set the initial state, as this is a completely new
267 thr_ret = _wapi_handle_lock_handle (handle);
268 g_assert (thr_ret == 0);
270 if (initial == TRUE) {
271 _wapi_handle_set_signal_state (handle, TRUE, FALSE);
274 thr_ret = _wapi_handle_unlock_handle (handle);
275 g_assert (thr_ret == 0);
278 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: returning event handle %p", __func__, handle);
283 _wapi_namespace_unlock (NULL);
292 * @security: Ignored for now.
293 * @manual: Specifies whether the new event handle has manual or auto
295 * @initial: Specifies whether the new event handle is initially
297 * @name:Pointer to a string specifying the name of this name, or
298 * %NULL. Currently ignored.
300 * Creates a new event handle.
302 * An event handle is signalled with SetEvent(). If the new handle is
303 * a manual reset event handle, it remains signalled until it is reset
304 * with ResetEvent(). An auto reset event remains signalled until a
305 * single thread has waited for it, at which time the event handle is
306 * automatically reset to unsignalled.
308 * Return value: A new handle, or %NULL on error.
310 gpointer CreateEvent(WapiSecurityAttributes *security G_GNUC_UNUSED,
311 gboolean manual, gboolean initial,
312 const gunichar2 *name G_GNUC_UNUSED)
314 mono_once (&event_ops_once, event_ops_init);
317 return(event_create (security, manual, initial));
319 return(namedevent_create (security, manual, initial, name));
323 static gboolean event_pulse (gpointer handle)
325 struct _WapiHandle_event *event_handle;
329 ok = _wapi_lookup_handle (handle, WAPI_HANDLE_EVENT,
330 (gpointer *)&event_handle);
332 g_warning ("%s: error looking up event handle %p", __func__,
337 thr_ret = _wapi_handle_unlock_handle (handle);
338 g_assert (thr_ret == 0);
340 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Pulsing event handle %p", __func__, handle);
342 if (event_handle->manual == TRUE) {
343 _wapi_handle_set_signal_state (handle, TRUE, TRUE);
345 event_handle->set_count = 1;
346 _wapi_handle_set_signal_state (handle, TRUE, FALSE);
349 thr_ret = _wapi_handle_unlock_handle (handle);
350 g_assert (thr_ret == 0);
352 if (event_handle->manual == TRUE) {
353 /* For a manual-reset event, we're about to try and
354 * get the handle lock again, so give other threads a
359 /* Reset the handle signal state */
360 /* I'm not sure whether or not we need a barrier here
361 * to make sure that all threads waiting on the event
362 * have proceeded. Currently we rely on broadcasting
365 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Obtained write lock on event handle %p",
368 thr_ret = _wapi_handle_lock_handle (handle);
369 g_assert (thr_ret == 0);
371 _wapi_handle_set_signal_state (handle, FALSE, FALSE);
373 thr_ret = _wapi_handle_unlock_handle (handle);
374 g_assert (thr_ret == 0);
380 static gboolean namedevent_pulse (gpointer handle)
382 struct _WapiHandle_namedevent *namedevent_handle;
386 ok = _wapi_lookup_handle (handle, WAPI_HANDLE_NAMEDEVENT,
387 (gpointer *)&namedevent_handle);
389 g_warning ("%s: error looking up named event handle %p",
394 thr_ret = _wapi_handle_unlock_handle (handle);
395 g_assert (thr_ret == 0);
397 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Pulsing named event handle %p", __func__, handle);
399 if (namedevent_handle->manual == TRUE) {
400 _wapi_handle_set_signal_state (handle, TRUE, TRUE);
402 namedevent_handle->set_count = 1;
403 _wapi_handle_set_signal_state (handle, TRUE, FALSE);
406 thr_ret = _wapi_handle_unlock_handle (handle);
407 g_assert (thr_ret == 0);
409 if (namedevent_handle->manual == TRUE) {
410 /* For a manual-reset event, we're about to try and
411 * get the handle lock again, so give other processes
414 _wapi_handle_spin (200);
416 /* Reset the handle signal state */
417 /* I'm not sure whether or not we need a barrier here
418 * to make sure that all threads waiting on the event
419 * have proceeded. Currently we rely on waiting for
420 * twice the shared handle poll interval.
422 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Obtained write lock on event handle %p",
425 thr_ret = _wapi_handle_lock_handle (handle);
426 g_assert (thr_ret == 0);
428 _wapi_handle_set_signal_state (handle, FALSE, FALSE);
430 thr_ret = _wapi_handle_unlock_handle (handle);
431 g_assert (thr_ret == 0);
439 * @handle: The event handle.
441 * Sets the event handle @handle to the signalled state, and then
442 * resets it to unsignalled after informing any waiting threads.
444 * If @handle is a manual reset event, all waiting threads that can be
445 * released immediately are released. @handle is then reset. If
446 * @handle is an auto reset event, one waiting thread is released even
447 * if multiple threads are waiting.
449 * Return value: %TRUE on success, %FALSE otherwise. (Currently only
450 * ever returns %TRUE).
452 gboolean PulseEvent(gpointer handle)
456 if (handle == NULL) {
457 SetLastError (ERROR_INVALID_HANDLE);
461 type = _wapi_handle_type (handle);
463 if (event_ops[type].pulse == NULL) {
464 SetLastError (ERROR_INVALID_HANDLE);
468 return(event_ops[type].pulse (handle));
471 static gboolean event_reset (gpointer handle)
473 struct _WapiHandle_event *event_handle;
477 ok = _wapi_lookup_handle (handle, WAPI_HANDLE_EVENT,
478 (gpointer *)&event_handle);
480 g_warning ("%s: error looking up event handle %p",
485 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Resetting event handle %p", __func__, handle);
487 thr_ret = _wapi_handle_lock_handle (handle);
488 g_assert (thr_ret == 0);
490 if (_wapi_handle_issignalled (handle) == FALSE) {
491 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: No need to reset event handle %p", __func__,
494 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Obtained write lock on event handle %p",
497 _wapi_handle_set_signal_state (handle, FALSE, FALSE);
500 event_handle->set_count = 0;
502 thr_ret = _wapi_handle_unlock_handle (handle);
503 g_assert (thr_ret == 0);
508 static gboolean namedevent_reset (gpointer handle)
510 struct _WapiHandle_namedevent *namedevent_handle;
514 ok = _wapi_lookup_handle (handle, WAPI_HANDLE_NAMEDEVENT,
515 (gpointer *)&namedevent_handle);
517 g_warning ("%s: error looking up named event handle %p",
522 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Resetting named event handle %p", __func__, handle);
524 thr_ret = _wapi_handle_lock_handle (handle);
525 g_assert (thr_ret == 0);
527 if (_wapi_handle_issignalled (handle) == FALSE) {
528 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: No need to reset named event handle %p",
531 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Obtained write lock on named event handle %p",
534 _wapi_handle_set_signal_state (handle, FALSE, FALSE);
537 namedevent_handle->set_count = 0;
539 thr_ret = _wapi_handle_unlock_handle (handle);
540 g_assert (thr_ret == 0);
547 * @handle: The event handle.
549 * Resets the event handle @handle to the unsignalled state.
551 * Return value: %TRUE on success, %FALSE otherwise. (Currently only
552 * ever returns %TRUE).
554 gboolean ResetEvent(gpointer handle)
558 if (handle == NULL) {
559 SetLastError (ERROR_INVALID_HANDLE);
563 type = _wapi_handle_type (handle);
565 if (event_ops[type].reset == NULL) {
566 SetLastError (ERROR_INVALID_HANDLE);
570 return(event_ops[type].reset (handle));
573 static gboolean event_set (gpointer handle)
575 struct _WapiHandle_event *event_handle;
579 ok = _wapi_lookup_handle (handle, WAPI_HANDLE_EVENT,
580 (gpointer *)&event_handle);
582 g_warning ("%s: error looking up event handle %p", __func__,
587 thr_ret = _wapi_handle_lock_handle (handle);
588 g_assert (thr_ret == 0);
590 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Setting event handle %p", __func__, handle);
592 if (event_handle->manual == TRUE) {
593 _wapi_handle_set_signal_state (handle, TRUE, TRUE);
595 event_handle->set_count = 1;
596 _wapi_handle_set_signal_state (handle, TRUE, FALSE);
599 thr_ret = _wapi_handle_unlock_handle (handle);
600 g_assert (thr_ret == 0);
605 static gboolean namedevent_set (gpointer handle)
607 struct _WapiHandle_namedevent *namedevent_handle;
611 ok = _wapi_lookup_handle (handle, WAPI_HANDLE_NAMEDEVENT,
612 (gpointer *)&namedevent_handle);
614 g_warning ("%s: error looking up named event handle %p",
619 thr_ret = _wapi_handle_lock_handle (handle);
620 g_assert (thr_ret == 0);
622 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Setting named event handle %p", __func__, handle);
624 if (namedevent_handle->manual == TRUE) {
625 _wapi_handle_set_signal_state (handle, TRUE, TRUE);
627 namedevent_handle->set_count = 1;
628 _wapi_handle_set_signal_state (handle, TRUE, TRUE);
631 thr_ret = _wapi_handle_unlock_handle (handle);
632 g_assert (thr_ret == 0);
639 * @handle: The event handle
641 * Sets the event handle @handle to the signalled state.
643 * If @handle is a manual reset event, it remains signalled until it
644 * is reset with ResetEvent(). An auto reset event remains signalled
645 * until a single thread has waited for it, at which time @handle is
646 * automatically reset to unsignalled.
648 * Return value: %TRUE on success, %FALSE otherwise. (Currently only
649 * ever returns %TRUE).
651 gboolean SetEvent(gpointer handle)
655 if (handle == NULL) {
656 SetLastError (ERROR_INVALID_HANDLE);
660 type = _wapi_handle_type (handle);
662 if (event_ops[type].set == NULL) {
663 SetLastError (ERROR_INVALID_HANDLE);
667 return(event_ops[type].set (handle));
670 gpointer OpenEvent (guint32 access G_GNUC_UNUSED, gboolean inherit G_GNUC_UNUSED, const gunichar2 *name)
676 mono_once (&event_ops_once, event_ops_init);
678 /* w32 seems to guarantee that opening named objects can't
681 thr_ret = _wapi_namespace_lock ();
682 g_assert (thr_ret == 0);
684 utf8_name = g_utf16_to_utf8 (name, -1, NULL, NULL, NULL);
686 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Opening named event [%s]", __func__, utf8_name);
688 handle = _wapi_search_handle_namespace (WAPI_HANDLE_NAMEDEVENT,
690 if (handle == _WAPI_HANDLE_INVALID) {
691 /* The name has already been used for a different
694 SetLastError (ERROR_INVALID_HANDLE);
696 } else if (!handle) {
697 /* This name doesn't exist */
698 SetLastError (ERROR_FILE_NOT_FOUND); /* yes, really */
702 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: returning named event handle %p", __func__, handle);
707 _wapi_namespace_unlock (NULL);