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