Revert "[threads] Make OSEvent alertable to fix bug #51653" (#4346)
[mono.git] / mono / utils / os-event-win32.c
1 /*
2  * os-event-win32.c: MonoOSEvent on Win32
3  *
4  * Author:
5  *      Ludovic Henry (luhenry@microsoft.com)
6  *
7  * Licensed under the MIT license. See LICENSE file in the project root for full license information.
8  */
9
10 #include "os-event.h"
11
12 #include <windows.h>
13 #include <winbase.h>
14
15 #include "atomic.h"
16
17 void
18 mono_os_event_init (MonoOSEvent *event, gboolean initial)
19 {
20         g_assert (event);
21
22         event->handle = CreateEvent (NULL, TRUE, initial, NULL);
23         if (G_UNLIKELY (!event->handle))
24                 g_error ("%s: CreateEvent failed with error %d", __func__, GetLastError ());
25 }
26
27 void
28 mono_os_event_destroy (MonoOSEvent *event)
29 {
30         BOOL res;
31
32         g_assert (event);
33         g_assert (event->handle);
34
35         res = CloseHandle (event->handle);
36         if (G_UNLIKELY (res == 0))
37                 g_error ("%s: CloseHandle failed with error %d", __func__, GetLastError ());
38 }
39
40 void
41 mono_os_event_set (MonoOSEvent *event)
42 {
43         BOOL res;
44
45         g_assert (event);
46         g_assert (event->handle);
47
48         res = SetEvent (event->handle);
49         if (G_UNLIKELY (res == 0))
50                 g_error ("%s: SetEvent failed with error %d", __func__, GetLastError ());
51 }
52
53 void
54 mono_os_event_reset (MonoOSEvent *event)
55 {
56         BOOL res;
57
58         g_assert (event);
59         g_assert (event->handle);
60
61         res = ResetEvent (event->handle);
62         if (G_UNLIKELY (res == 0))
63                 g_error ("%s: ResetEvent failed with error %d", __func__, GetLastError ());
64 }
65
66 MonoOSEventWaitRet
67 mono_os_event_wait_one (MonoOSEvent *event, guint32 timeout)
68 {
69         DWORD res;
70
71         g_assert (event);
72         g_assert (event->handle);
73
74         res = WaitForSingleObjectEx (event->handle, timeout, TRUE);
75         if (res == WAIT_OBJECT_0)
76                 return MONO_OS_EVENT_WAIT_RET_SUCCESS_0;
77         else if (res == WAIT_IO_COMPLETION)
78                 return MONO_OS_EVENT_WAIT_RET_ALERTED;
79         else if (res == WAIT_TIMEOUT)
80                 return MONO_OS_EVENT_WAIT_RET_TIMEOUT;
81         else if (res == WAIT_FAILED)
82                 g_error ("%s: WaitForSingleObjectEx failed with error %d", __func__, GetLastError ());
83         else
84                 g_error ("%s: unknown res value %d", __func__, res);
85 }
86
87 MonoOSEventWaitRet
88 mono_os_event_wait_multiple (MonoOSEvent **events, gsize nevents, gboolean waitall, guint32 timeout)
89 {
90         DWORD res;
91         gpointer handles [MONO_OS_EVENT_WAIT_MAXIMUM_OBJECTS];
92         gint i;
93
94         g_assert (events);
95         g_assert (nevents > 0);
96         g_assert (nevents <= MONO_OS_EVENT_WAIT_MAXIMUM_OBJECTS);
97
98         if (nevents == 1)
99                 return mono_os_event_wait_one (events [0], timeout);
100
101         for (i = 0; i < nevents; ++i) {
102                 g_assert (events [i]);
103                 g_assert (events [i]->handle);
104                 handles [i] = events [i]->handle;
105         }
106
107         res = WaitForMultipleObjectsEx (nevents, handles, waitall, timeout, TRUE);
108         if (res >= WAIT_OBJECT_0 && res < WAIT_OBJECT_0 + MONO_OS_EVENT_WAIT_MAXIMUM_OBJECTS)
109                 return MONO_OS_EVENT_WAIT_RET_SUCCESS_0 + (res - WAIT_OBJECT_0);
110         else if (res == WAIT_IO_COMPLETION)
111                 return MONO_OS_EVENT_WAIT_RET_ALERTED;
112         else if (res == WAIT_TIMEOUT)
113                 return MONO_OS_EVENT_WAIT_RET_TIMEOUT;
114         else if (res == WAIT_FAILED)
115                 g_error ("%s: WaitForSingleObjectEx failed with error %d", __func__, GetLastError ());
116         else
117                 g_error ("%s: unknown res value %d", __func__, res);
118 }