/
os-event-win32.c
118 lines (96 loc) · 2.98 KB
/
os-event-win32.c
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
/*
* os-event-win32.c: MonoOSEvent on Win32
*
* Author:
* Ludovic Henry (luhenry@microsoft.com)
*
* Licensed under the MIT license. See LICENSE file in the project root for full license information.
*/
#include "os-event.h"
#include <windows.h>
#include <winbase.h>
#include "atomic.h"
void
mono_os_event_init (MonoOSEvent *event, gboolean initial)
{
g_assert (event);
event->handle = CreateEvent (NULL, TRUE, initial, NULL);
if (G_UNLIKELY (!event->handle))
g_error ("%s: CreateEvent failed with error %d", __func__, GetLastError ());
}
void
mono_os_event_destroy (MonoOSEvent *event)
{
BOOL res;
g_assert (event);
g_assert (event->handle);
res = CloseHandle (event->handle);
if (G_UNLIKELY (res == 0))
g_error ("%s: CloseHandle failed with error %d", __func__, GetLastError ());
}
void
mono_os_event_set (MonoOSEvent *event)
{
BOOL res;
g_assert (event);
g_assert (event->handle);
res = SetEvent (event->handle);
if (G_UNLIKELY (res == 0))
g_error ("%s: SetEvent failed with error %d", __func__, GetLastError ());
}
void
mono_os_event_reset (MonoOSEvent *event)
{
BOOL res;
g_assert (event);
g_assert (event->handle);
res = ResetEvent (event->handle);
if (G_UNLIKELY (res == 0))
g_error ("%s: ResetEvent failed with error %d", __func__, GetLastError ());
}
MonoOSEventWaitRet
mono_os_event_wait_one (MonoOSEvent *event, guint32 timeout, gboolean alertable)
{
DWORD res;
g_assert (event);
g_assert (event->handle);
res = WaitForSingleObjectEx (event->handle, timeout, alertable);
if (res == WAIT_OBJECT_0)
return MONO_OS_EVENT_WAIT_RET_SUCCESS_0;
else if (res == WAIT_IO_COMPLETION)
return MONO_OS_EVENT_WAIT_RET_ALERTED;
else if (res == WAIT_TIMEOUT)
return MONO_OS_EVENT_WAIT_RET_TIMEOUT;
else if (res == WAIT_FAILED)
g_error ("%s: WaitForSingleObjectEx failed with error %d", __func__, GetLastError ());
else
g_error ("%s: unknown res value %d", __func__, res);
}
MonoOSEventWaitRet
mono_os_event_wait_multiple (MonoOSEvent **events, gsize nevents, gboolean waitall, guint32 timeout, gboolean alertable)
{
DWORD res;
gpointer handles [MONO_OS_EVENT_WAIT_MAXIMUM_OBJECTS];
gint i;
g_assert (events);
g_assert (nevents > 0);
g_assert (nevents <= MONO_OS_EVENT_WAIT_MAXIMUM_OBJECTS);
if (nevents == 1)
return mono_os_event_wait_one (events [0], timeout, alertable);
for (i = 0; i < nevents; ++i) {
g_assert (events [i]);
g_assert (events [i]->handle);
handles [i] = events [i]->handle;
}
res = WaitForMultipleObjectsEx (nevents, handles, waitall, timeout, alertable);
if (res >= WAIT_OBJECT_0 && res < WAIT_OBJECT_0 + MONO_OS_EVENT_WAIT_MAXIMUM_OBJECTS)
return MONO_OS_EVENT_WAIT_RET_SUCCESS_0 + (res - WAIT_OBJECT_0);
else if (res == WAIT_IO_COMPLETION)
return MONO_OS_EVENT_WAIT_RET_ALERTED;
else if (res == WAIT_TIMEOUT)
return MONO_OS_EVENT_WAIT_RET_TIMEOUT;
else if (res == WAIT_FAILED)
g_error ("%s: WaitForSingleObjectEx failed with error %d", __func__, GetLastError ());
else
g_error ("%s: unknown res value %d", __func__, res);
}