6 * Ludovic Henry (luhenry@microsoft.com)
8 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
17 #include "mono-os-wait.h"
20 mono_os_event_init (MonoOSEvent *event, gboolean initial)
24 event->handle = CreateEvent (NULL, TRUE, initial, NULL);
25 if (G_UNLIKELY (!event->handle))
26 g_error ("%s: CreateEvent failed with error %d", __func__, GetLastError ());
30 mono_os_event_destroy (MonoOSEvent *event)
35 g_assert (event->handle);
37 res = CloseHandle (event->handle);
38 if (G_UNLIKELY (res == 0))
39 g_error ("%s: CloseHandle failed with error %d", __func__, GetLastError ());
43 mono_os_event_set (MonoOSEvent *event)
48 g_assert (event->handle);
50 res = SetEvent (event->handle);
51 if (G_UNLIKELY (res == 0))
52 g_error ("%s: SetEvent failed with error %d", __func__, GetLastError ());
56 mono_os_event_reset (MonoOSEvent *event)
61 g_assert (event->handle);
63 res = ResetEvent (event->handle);
64 if (G_UNLIKELY (res == 0))
65 g_error ("%s: ResetEvent failed with error %d", __func__, GetLastError ());
69 mono_os_event_wait_one (MonoOSEvent *event, guint32 timeout, gboolean alertable)
74 g_assert (event->handle);
76 res = mono_win32_wait_for_single_object_ex (event->handle, timeout, alertable);
77 if (res == WAIT_OBJECT_0)
78 return MONO_OS_EVENT_WAIT_RET_SUCCESS_0;
79 else if (res == WAIT_IO_COMPLETION)
80 return MONO_OS_EVENT_WAIT_RET_ALERTED;
81 else if (res == WAIT_TIMEOUT)
82 return MONO_OS_EVENT_WAIT_RET_TIMEOUT;
83 else if (res == WAIT_FAILED)
84 g_error ("%s: mono_thread_win32_wait_one_handle failed with error %d", __func__, GetLastError ());
86 g_error ("%s: unknown res value %d", __func__, res);
90 mono_os_event_wait_multiple (MonoOSEvent **events, gsize nevents, gboolean waitall, guint32 timeout, gboolean alertable)
93 gpointer handles [MONO_OS_EVENT_WAIT_MAXIMUM_OBJECTS];
97 g_assert (nevents > 0);
98 g_assert (nevents <= MONO_OS_EVENT_WAIT_MAXIMUM_OBJECTS);
101 return mono_os_event_wait_one (events [0], timeout, alertable);
103 for (i = 0; i < nevents; ++i) {
104 g_assert (events [i]);
105 g_assert (events [i]->handle);
106 handles [i] = events [i]->handle;
109 res = mono_win32_wait_for_multiple_objects_ex ((DWORD)nevents, handles, waitall, timeout, alertable);
110 if (res >= WAIT_OBJECT_0 && res < WAIT_OBJECT_0 + MONO_OS_EVENT_WAIT_MAXIMUM_OBJECTS)
111 return MONO_OS_EVENT_WAIT_RET_SUCCESS_0 + (res - WAIT_OBJECT_0);
112 else if (res == WAIT_IO_COMPLETION)
113 return MONO_OS_EVENT_WAIT_RET_ALERTED;
114 else if (res == WAIT_TIMEOUT)
115 return MONO_OS_EVENT_WAIT_RET_TIMEOUT;
116 else if (res == WAIT_FAILED)
117 g_error ("%s: mono_thread_win32_wait_multiple_handle failed with error %d", __func__, GetLastError ());
119 g_error ("%s: unknown res value %d", __func__, res);