[io-layer] Extract file (#4255)
[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/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 void
162 mono_w32event_set (gpointer handle)
163 {
164         ves_icall_System_Threading_Events_SetEvent_internal (handle);
165 }
166
167 void
168 mono_w32event_reset (gpointer handle)
169 {
170         ves_icall_System_Threading_Events_ResetEvent_internal (handle);
171 }
172
173 static gpointer event_handle_create (MonoW32HandleEvent *event_handle, MonoW32HandleType type, gboolean manual, gboolean initial)
174 {
175         gpointer handle;
176
177         event_handle->manual = manual;
178         event_handle->set_count = (initial && !manual) ? 1 : 0;
179
180         handle = mono_w32handle_new (type, event_handle);
181         if (handle == INVALID_HANDLE_VALUE) {
182                 g_warning ("%s: error creating %s handle",
183                         __func__, mono_w32handle_get_typename (type));
184                 SetLastError (ERROR_GEN_FAILURE);
185                 return NULL;
186         }
187
188         mono_w32handle_lock_handle (handle);
189
190         if (initial)
191                 mono_w32handle_set_signal_state (handle, TRUE, FALSE);
192
193         mono_w32handle_unlock_handle (handle);
194
195         mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: created %s handle %p",
196                 __func__, mono_w32handle_get_typename (type), handle);
197
198         return handle;
199 }
200
201 static gpointer event_create (gboolean manual, gboolean initial)
202 {
203         MonoW32HandleEvent event_handle;
204         mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: creating %s handle",
205                 __func__, mono_w32handle_get_typename (MONO_W32HANDLE_EVENT));
206         return event_handle_create (&event_handle, MONO_W32HANDLE_EVENT, manual, initial);
207 }
208
209 static gpointer namedevent_create (gboolean manual, gboolean initial, const gunichar2 *name G_GNUC_UNUSED)
210 {
211         gpointer handle;
212         gchar *utf8_name;
213
214         mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: creating %s handle",
215                 __func__, mono_w32handle_get_typename (MONO_W32HANDLE_NAMEDEVENT));
216
217         /* w32 seems to guarantee that opening named objects can't race each other */
218         mono_w32handle_namespace_lock ();
219
220         utf8_name = g_utf16_to_utf8 (name, -1, NULL, NULL, NULL);
221
222         handle = mono_w32handle_namespace_search_handle (MONO_W32HANDLE_NAMEDEVENT, utf8_name);
223         if (handle == INVALID_HANDLE_VALUE) {
224                 /* The name has already been used for a different object. */
225                 handle = NULL;
226                 SetLastError (ERROR_INVALID_HANDLE);
227         } else if (handle) {
228                 /* Not an error, but this is how the caller is informed that the event wasn't freshly created */
229                 SetLastError (ERROR_ALREADY_EXISTS);
230
231                 /* mono_w32handle_namespace_search_handle already adds a ref to the 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
272         if (handle == NULL) {
273                 SetLastError (ERROR_INVALID_HANDLE);
274                 return(FALSE);
275         }
276
277         switch (type = mono_w32handle_get_type (handle)) {
278         case MONO_W32HANDLE_EVENT:
279         case MONO_W32HANDLE_NAMEDEVENT:
280                 break;
281         default:
282                 SetLastError (ERROR_INVALID_HANDLE);
283                 return FALSE;
284         }
285
286         if (!mono_w32handle_lookup (handle, type, (gpointer *)&event_handle)) {
287                 g_warning ("%s: error looking up %s handle %p",
288                         __func__, mono_w32handle_get_typename (type), handle);
289                 return FALSE;
290         }
291
292         mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: setting %s handle %p",
293                 __func__, mono_w32handle_get_typename (type), handle);
294
295         mono_w32handle_lock_handle (handle);
296
297         if (!event_handle->manual) {
298                 event_handle->set_count = 1;
299                 mono_w32handle_set_signal_state (handle, TRUE, FALSE);
300         } else {
301                 mono_w32handle_set_signal_state (handle, TRUE, TRUE);
302         }
303
304         mono_w32handle_unlock_handle (handle);
305
306         return TRUE;
307 }
308
309 gboolean
310 ves_icall_System_Threading_Events_ResetEvent_internal (gpointer handle)
311 {
312         MonoW32HandleType type;
313         MonoW32HandleEvent *event_handle;
314
315         SetLastError (ERROR_SUCCESS);
316
317         if (handle == NULL) {
318                 SetLastError (ERROR_INVALID_HANDLE);
319                 return(FALSE);
320         }
321
322         switch (type = mono_w32handle_get_type (handle)) {
323         case MONO_W32HANDLE_EVENT:
324         case MONO_W32HANDLE_NAMEDEVENT:
325                 break;
326         default:
327                 SetLastError (ERROR_INVALID_HANDLE);
328                 return FALSE;
329         }
330
331         if (!mono_w32handle_lookup (handle, type, (gpointer *)&event_handle)) {
332                 g_warning ("%s: error looking up %s handle %p",
333                         __func__, mono_w32handle_get_typename (type), handle);
334                 return FALSE;
335         }
336
337         mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: resetting %s handle %p",
338                 __func__, mono_w32handle_get_typename (type), handle);
339
340         mono_w32handle_lock_handle (handle);
341
342         if (!mono_w32handle_issignalled (handle)) {
343                 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: no need to reset %s handle %p",
344                         __func__, mono_w32handle_get_typename (type), handle);
345         } else {
346                 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: obtained write lock on %s handle %p",
347                         __func__, mono_w32handle_get_typename (type), handle);
348
349                 mono_w32handle_set_signal_state (handle, FALSE, FALSE);
350         }
351
352         event_handle->set_count = 0;
353
354         mono_w32handle_unlock_handle (handle);
355
356         return TRUE;
357 }
358
359 void
360 ves_icall_System_Threading_Events_CloseEvent_internal (gpointer handle)
361 {
362         CloseHandle (handle);
363 }
364
365 gpointer
366 ves_icall_System_Threading_Events_OpenEvent_internal (MonoString *name, gint32 rights G_GNUC_UNUSED, gint32 *error)
367 {
368         gpointer handle;
369         gchar *utf8_name;
370
371         *error = ERROR_SUCCESS;
372
373         /* w32 seems to guarantee that opening named objects can't race each other */
374         mono_w32handle_namespace_lock ();
375
376         utf8_name = g_utf16_to_utf8 (mono_string_chars (name), -1, NULL, NULL, NULL);
377
378         mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Opening named event [%s]", __func__, utf8_name);
379
380         handle = mono_w32handle_namespace_search_handle (MONO_W32HANDLE_NAMEDEVENT, utf8_name);
381         if (handle == INVALID_HANDLE_VALUE) {
382                 /* The name has already been used for a different object. */
383                 *error = ERROR_INVALID_HANDLE;
384                 goto cleanup;
385         } else if (!handle) {
386                 /* This name doesn't exist */
387                 *error = ERROR_FILE_NOT_FOUND;
388                 goto cleanup;
389         }
390
391         mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: returning named event handle %p", __func__, handle);
392
393 cleanup:
394         g_free (utf8_name);
395
396         mono_w32handle_namespace_unlock ();
397
398         return handle;
399 }
400
401 MonoW32HandleNamespace*
402 mono_w32event_get_namespace (MonoW32HandleNamedEvent *event)
403 {
404         return &event->sharedns;
405 }