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