Merge pull request #4169 from evincarofautumn/fix-xmm-scanning-mac-x86
[mono.git] / mono / metadata / w32event-unix.c
1 /*
2  * w32event-unix.c: Runtime support for managed Event on Unix
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 "w32event.h"
11
12 #include "w32error.h"
13 #include "w32handle-namespace.h"
14 #include "mono/utils/mono-logger-internals.h"
15 #include "mono/metadata/w32handle.h"
16
17 #define MAX_PATH 260
18
19 typedef struct {
20         gboolean manual;
21         guint32 set_count;
22 } MonoW32HandleEvent;
23
24 struct MonoW32HandleNamedEvent {
25         MonoW32HandleEvent e;
26         MonoW32HandleNamespace sharedns;
27 };
28
29 static gboolean event_handle_own (gpointer handle, MonoW32HandleType type, gboolean *abandoned)
30 {
31         MonoW32HandleEvent *event_handle;
32         gboolean ok;
33
34         *abandoned = FALSE;
35
36         ok = mono_w32handle_lookup (handle, type, (gpointer *)&event_handle);
37         if (!ok) {
38                 g_warning ("%s: error looking up %s handle %p",
39                         __func__, mono_w32handle_get_typename (type), handle);
40                 return FALSE;
41         }
42
43         mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: owning %s handle %p",
44                 __func__, mono_w32handle_get_typename (type), handle);
45
46         if (!event_handle->manual) {
47                 g_assert (event_handle->set_count > 0);
48                 event_handle->set_count --;
49
50                 if (event_handle->set_count == 0)
51                         mono_w32handle_set_signal_state (handle, FALSE, FALSE);
52         }
53
54         return TRUE;
55 }
56
57 static void event_signal(gpointer handle)
58 {
59         ves_icall_System_Threading_Events_SetEvent_internal (handle);
60 }
61
62 static gboolean event_own (gpointer handle, gboolean *abandoned)
63 {
64         return event_handle_own (handle, MONO_W32HANDLE_EVENT, abandoned);
65 }
66
67 static void namedevent_signal (gpointer handle)
68 {
69         ves_icall_System_Threading_Events_SetEvent_internal (handle);
70 }
71
72 /* NB, always called with the shared handle lock held */
73 static gboolean namedevent_own (gpointer handle, gboolean *abandoned)
74 {
75         return event_handle_own (handle, MONO_W32HANDLE_NAMEDEVENT, abandoned);
76 }
77
78 static void event_details (gpointer data)
79 {
80         MonoW32HandleEvent *event = (MonoW32HandleEvent *)data;
81         g_print ("manual: %s, set_count: %d",
82                 event->manual ? "TRUE" : "FALSE", event->set_count);
83 }
84
85 static void namedevent_details (gpointer data)
86 {
87         MonoW32HandleNamedEvent *namedevent = (MonoW32HandleNamedEvent *)data;
88         g_print ("manual: %s, set_count: %d, name: \"%s\"",
89                 namedevent->e.manual ? "TRUE" : "FALSE", namedevent->e.set_count, namedevent->sharedns.name);
90 }
91
92 static const gchar* event_typename (void)
93 {
94         return "Event";
95 }
96
97 static gsize event_typesize (void)
98 {
99         return sizeof (MonoW32HandleEvent);
100 }
101
102 static const gchar* namedevent_typename (void)
103 {
104         return "N.Event";
105 }
106
107 static gsize namedevent_typesize (void)
108 {
109         return sizeof (MonoW32HandleNamedEvent);
110 }
111
112 void
113 mono_w32event_init (void)
114 {
115         static MonoW32HandleOps event_ops = {
116                 NULL,                   /* close */
117                 event_signal,           /* signal */
118                 event_own,              /* own */
119                 NULL,                   /* is_owned */
120                 NULL,                   /* special_wait */
121                 NULL,                   /* prewait */
122                 event_details,  /* details */
123                 event_typename, /* typename */
124                 event_typesize, /* typesize */
125         };
126
127         static MonoW32HandleOps namedevent_ops = {
128                 NULL,                   /* close */
129                 namedevent_signal,      /* signal */
130                 namedevent_own,         /* own */
131                 NULL,                   /* is_owned */
132                 NULL,                   /* special_wait */
133                 NULL,                   /* prewait */
134                 namedevent_details,     /* details */
135                 namedevent_typename, /* typename */
136                 namedevent_typesize, /* typesize */
137         };
138
139         mono_w32handle_register_ops (MONO_W32HANDLE_EVENT,      &event_ops);
140         mono_w32handle_register_ops (MONO_W32HANDLE_NAMEDEVENT, &namedevent_ops);
141
142         mono_w32handle_register_capabilities (MONO_W32HANDLE_EVENT,
143                 (MonoW32HandleCapability)(MONO_W32HANDLE_CAP_WAIT | MONO_W32HANDLE_CAP_SIGNAL));
144         mono_w32handle_register_capabilities (MONO_W32HANDLE_NAMEDEVENT,
145                 (MonoW32HandleCapability)(MONO_W32HANDLE_CAP_WAIT | MONO_W32HANDLE_CAP_SIGNAL));
146 }
147
148 gpointer
149 mono_w32event_create (gboolean manual, gboolean initial)
150 {
151         gpointer handle;
152         gint32 error;
153
154         handle = ves_icall_System_Threading_Events_CreateEvent_internal (manual, initial, NULL, &error);
155         if (error != ERROR_SUCCESS)
156                 g_assert (!handle);
157
158         return handle;
159 }
160
161 gboolean
162 mono_w32event_close (gpointer handle)
163 {
164         return mono_w32handle_close (handle);
165 }
166
167 void
168 mono_w32event_set (gpointer handle)
169 {
170         ves_icall_System_Threading_Events_SetEvent_internal (handle);
171 }
172
173 void
174 mono_w32event_reset (gpointer handle)
175 {
176         ves_icall_System_Threading_Events_ResetEvent_internal (handle);
177 }
178
179 static gpointer event_handle_create (MonoW32HandleEvent *event_handle, MonoW32HandleType type, gboolean manual, gboolean initial)
180 {
181         gpointer handle;
182
183         event_handle->manual = manual;
184         event_handle->set_count = (initial && !manual) ? 1 : 0;
185
186         handle = mono_w32handle_new (type, event_handle);
187         if (handle == INVALID_HANDLE_VALUE) {
188                 g_warning ("%s: error creating %s handle",
189                         __func__, mono_w32handle_get_typename (type));
190                 mono_w32error_set_last (ERROR_GEN_FAILURE);
191                 return NULL;
192         }
193
194         mono_w32handle_lock_handle (handle);
195
196         if (initial)
197                 mono_w32handle_set_signal_state (handle, TRUE, FALSE);
198
199         mono_w32handle_unlock_handle (handle);
200
201         mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: created %s handle %p",
202                 __func__, mono_w32handle_get_typename (type), handle);
203
204         return handle;
205 }
206
207 static gpointer event_create (gboolean manual, gboolean initial)
208 {
209         MonoW32HandleEvent event_handle;
210         mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: creating %s handle",
211                 __func__, mono_w32handle_get_typename (MONO_W32HANDLE_EVENT));
212         return event_handle_create (&event_handle, MONO_W32HANDLE_EVENT, manual, initial);
213 }
214
215 static gpointer namedevent_create (gboolean manual, gboolean initial, const gunichar2 *name G_GNUC_UNUSED)
216 {
217         gpointer handle;
218         gchar *utf8_name;
219
220         mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: creating %s handle",
221                 __func__, mono_w32handle_get_typename (MONO_W32HANDLE_NAMEDEVENT));
222
223         /* w32 seems to guarantee that opening named objects can't race each other */
224         mono_w32handle_namespace_lock ();
225
226         glong utf8_len = 0;
227         utf8_name = g_utf16_to_utf8 (name, -1, NULL, &utf8_len, NULL);
228
229         handle = mono_w32handle_namespace_search_handle (MONO_W32HANDLE_NAMEDEVENT, utf8_name);
230         if (handle == INVALID_HANDLE_VALUE) {
231                 /* The name has already been used for a different object. */
232                 handle = NULL;
233                 mono_w32error_set_last (ERROR_INVALID_HANDLE);
234         } else if (handle) {
235                 /* Not an error, but this is how the caller is informed that the event wasn't freshly created */
236                 mono_w32error_set_last (ERROR_ALREADY_EXISTS);
237
238                 /* mono_w32handle_namespace_search_handle already adds a ref to the handle */
239         } else {
240                 /* A new named event */
241                 MonoW32HandleNamedEvent namedevent_handle;
242
243                 size_t len = utf8_len < MAX_PATH ? utf8_len : MAX_PATH;
244                 memcpy (&namedevent_handle.sharedns.name [0], utf8_name, len);
245                 namedevent_handle.sharedns.name [len] = '\0';
246
247                 handle = event_handle_create ((MonoW32HandleEvent*) &namedevent_handle, MONO_W32HANDLE_NAMEDEVENT, manual, initial);
248         }
249
250         g_free (utf8_name);
251
252         mono_w32handle_namespace_unlock ();
253
254         return handle;
255 }
256
257 gpointer
258 ves_icall_System_Threading_Events_CreateEvent_internal (MonoBoolean manual, MonoBoolean initial, MonoString *name, gint32 *error)
259 {
260         gpointer event;
261
262         /* Need to blow away any old errors here, because code tests
263          * for ERROR_ALREADY_EXISTS on success (!) to see if an event
264          * was freshly created */
265         mono_w32error_set_last (ERROR_SUCCESS);
266
267         event = name ? namedevent_create (manual, initial, mono_string_chars (name)) : event_create (manual, initial);
268
269         *error = mono_w32error_get_last ();
270
271         return event;
272 }
273
274 gboolean
275 ves_icall_System_Threading_Events_SetEvent_internal (gpointer handle)
276 {
277         MonoW32HandleType type;
278         MonoW32HandleEvent *event_handle;
279
280         if (handle == NULL) {
281                 mono_w32error_set_last (ERROR_INVALID_HANDLE);
282                 return(FALSE);
283         }
284
285         switch (type = mono_w32handle_get_type (handle)) {
286         case MONO_W32HANDLE_EVENT:
287         case MONO_W32HANDLE_NAMEDEVENT:
288                 break;
289         default:
290                 mono_w32error_set_last (ERROR_INVALID_HANDLE);
291                 return FALSE;
292         }
293
294         if (!mono_w32handle_lookup (handle, type, (gpointer *)&event_handle)) {
295                 g_warning ("%s: error looking up %s handle %p",
296                         __func__, mono_w32handle_get_typename (type), handle);
297                 return FALSE;
298         }
299
300         mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: setting %s handle %p",
301                 __func__, mono_w32handle_get_typename (type), handle);
302
303         mono_w32handle_lock_handle (handle);
304
305         if (!event_handle->manual) {
306                 event_handle->set_count = 1;
307                 mono_w32handle_set_signal_state (handle, TRUE, FALSE);
308         } else {
309                 mono_w32handle_set_signal_state (handle, TRUE, TRUE);
310         }
311
312         mono_w32handle_unlock_handle (handle);
313
314         return TRUE;
315 }
316
317 gboolean
318 ves_icall_System_Threading_Events_ResetEvent_internal (gpointer handle)
319 {
320         MonoW32HandleType type;
321         MonoW32HandleEvent *event_handle;
322
323         mono_w32error_set_last (ERROR_SUCCESS);
324
325         if (handle == NULL) {
326                 mono_w32error_set_last (ERROR_INVALID_HANDLE);
327                 return(FALSE);
328         }
329
330         switch (type = mono_w32handle_get_type (handle)) {
331         case MONO_W32HANDLE_EVENT:
332         case MONO_W32HANDLE_NAMEDEVENT:
333                 break;
334         default:
335                 mono_w32error_set_last (ERROR_INVALID_HANDLE);
336                 return FALSE;
337         }
338
339         if (!mono_w32handle_lookup (handle, type, (gpointer *)&event_handle)) {
340                 g_warning ("%s: error looking up %s handle %p",
341                         __func__, mono_w32handle_get_typename (type), handle);
342                 return FALSE;
343         }
344
345         mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: resetting %s handle %p",
346                 __func__, mono_w32handle_get_typename (type), handle);
347
348         mono_w32handle_lock_handle (handle);
349
350         if (!mono_w32handle_issignalled (handle)) {
351                 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: no need to reset %s handle %p",
352                         __func__, mono_w32handle_get_typename (type), handle);
353         } else {
354                 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: obtained write lock on %s handle %p",
355                         __func__, mono_w32handle_get_typename (type), handle);
356
357                 mono_w32handle_set_signal_state (handle, FALSE, FALSE);
358         }
359
360         event_handle->set_count = 0;
361
362         mono_w32handle_unlock_handle (handle);
363
364         return TRUE;
365 }
366
367 void
368 ves_icall_System_Threading_Events_CloseEvent_internal (gpointer handle)
369 {
370         mono_w32handle_close (handle);
371 }
372
373 gpointer
374 ves_icall_System_Threading_Events_OpenEvent_internal (MonoString *name, gint32 rights G_GNUC_UNUSED, gint32 *error)
375 {
376         gpointer handle;
377         gchar *utf8_name;
378
379         *error = ERROR_SUCCESS;
380
381         /* w32 seems to guarantee that opening named objects can't race each other */
382         mono_w32handle_namespace_lock ();
383
384         utf8_name = g_utf16_to_utf8 (mono_string_chars (name), -1, NULL, NULL, NULL);
385
386         mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Opening named event [%s]", __func__, utf8_name);
387
388         handle = mono_w32handle_namespace_search_handle (MONO_W32HANDLE_NAMEDEVENT, utf8_name);
389         if (handle == INVALID_HANDLE_VALUE) {
390                 /* The name has already been used for a different object. */
391                 *error = ERROR_INVALID_HANDLE;
392                 goto cleanup;
393         } else if (!handle) {
394                 /* This name doesn't exist */
395                 *error = ERROR_FILE_NOT_FOUND;
396                 goto cleanup;
397         }
398
399         mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: returning named event handle %p", __func__, handle);
400
401 cleanup:
402         g_free (utf8_name);
403
404         mono_w32handle_namespace_unlock ();
405
406         return handle;
407 }
408
409 MonoW32HandleNamespace*
410 mono_w32event_get_namespace (MonoW32HandleNamedEvent *event)
411 {
412         return &event->sharedns;
413 }