[io-layer] Extract error (#4279)
[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         utf8_name = g_utf16_to_utf8 (name, -1, NULL, NULL, NULL);
227
228         handle = mono_w32handle_namespace_search_handle (MONO_W32HANDLE_NAMEDEVENT, utf8_name);
229         if (handle == INVALID_HANDLE_VALUE) {
230                 /* The name has already been used for a different object. */
231                 handle = NULL;
232                 mono_w32error_set_last (ERROR_INVALID_HANDLE);
233         } else if (handle) {
234                 /* Not an error, but this is how the caller is informed that the event wasn't freshly created */
235                 mono_w32error_set_last (ERROR_ALREADY_EXISTS);
236
237                 /* mono_w32handle_namespace_search_handle already adds a ref to the handle */
238         } else {
239                 /* A new named event */
240                 MonoW32HandleNamedEvent namedevent_handle;
241
242                 strncpy (&namedevent_handle.sharedns.name [0], utf8_name, MAX_PATH);
243                 namedevent_handle.sharedns.name [MAX_PATH] = '\0';
244
245                 handle = event_handle_create ((MonoW32HandleEvent*) &namedevent_handle, MONO_W32HANDLE_NAMEDEVENT, manual, initial);
246         }
247
248         g_free (utf8_name);
249
250         mono_w32handle_namespace_unlock ();
251
252         return handle;
253 }
254
255 gpointer
256 ves_icall_System_Threading_Events_CreateEvent_internal (MonoBoolean manual, MonoBoolean initial, MonoString *name, gint32 *error)
257 {
258         gpointer event;
259
260         /* Need to blow away any old errors here, because code tests
261          * for ERROR_ALREADY_EXISTS on success (!) to see if an event
262          * was freshly created */
263         mono_w32error_set_last (ERROR_SUCCESS);
264
265         event = name ? namedevent_create (manual, initial, mono_string_chars (name)) : event_create (manual, initial);
266
267         *error = mono_w32error_get_last ();
268
269         return event;
270 }
271
272 gboolean
273 ves_icall_System_Threading_Events_SetEvent_internal (gpointer handle)
274 {
275         MonoW32HandleType type;
276         MonoW32HandleEvent *event_handle;
277
278         if (handle == NULL) {
279                 mono_w32error_set_last (ERROR_INVALID_HANDLE);
280                 return(FALSE);
281         }
282
283         switch (type = mono_w32handle_get_type (handle)) {
284         case MONO_W32HANDLE_EVENT:
285         case MONO_W32HANDLE_NAMEDEVENT:
286                 break;
287         default:
288                 mono_w32error_set_last (ERROR_INVALID_HANDLE);
289                 return FALSE;
290         }
291
292         if (!mono_w32handle_lookup (handle, type, (gpointer *)&event_handle)) {
293                 g_warning ("%s: error looking up %s handle %p",
294                         __func__, mono_w32handle_get_typename (type), handle);
295                 return FALSE;
296         }
297
298         mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: setting %s handle %p",
299                 __func__, mono_w32handle_get_typename (type), handle);
300
301         mono_w32handle_lock_handle (handle);
302
303         if (!event_handle->manual) {
304                 event_handle->set_count = 1;
305                 mono_w32handle_set_signal_state (handle, TRUE, FALSE);
306         } else {
307                 mono_w32handle_set_signal_state (handle, TRUE, TRUE);
308         }
309
310         mono_w32handle_unlock_handle (handle);
311
312         return TRUE;
313 }
314
315 gboolean
316 ves_icall_System_Threading_Events_ResetEvent_internal (gpointer handle)
317 {
318         MonoW32HandleType type;
319         MonoW32HandleEvent *event_handle;
320
321         mono_w32error_set_last (ERROR_SUCCESS);
322
323         if (handle == NULL) {
324                 mono_w32error_set_last (ERROR_INVALID_HANDLE);
325                 return(FALSE);
326         }
327
328         switch (type = mono_w32handle_get_type (handle)) {
329         case MONO_W32HANDLE_EVENT:
330         case MONO_W32HANDLE_NAMEDEVENT:
331                 break;
332         default:
333                 mono_w32error_set_last (ERROR_INVALID_HANDLE);
334                 return FALSE;
335         }
336
337         if (!mono_w32handle_lookup (handle, type, (gpointer *)&event_handle)) {
338                 g_warning ("%s: error looking up %s handle %p",
339                         __func__, mono_w32handle_get_typename (type), handle);
340                 return FALSE;
341         }
342
343         mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: resetting %s handle %p",
344                 __func__, mono_w32handle_get_typename (type), handle);
345
346         mono_w32handle_lock_handle (handle);
347
348         if (!mono_w32handle_issignalled (handle)) {
349                 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: no need to reset %s handle %p",
350                         __func__, mono_w32handle_get_typename (type), handle);
351         } else {
352                 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: obtained write lock on %s handle %p",
353                         __func__, mono_w32handle_get_typename (type), handle);
354
355                 mono_w32handle_set_signal_state (handle, FALSE, FALSE);
356         }
357
358         event_handle->set_count = 0;
359
360         mono_w32handle_unlock_handle (handle);
361
362         return TRUE;
363 }
364
365 void
366 ves_icall_System_Threading_Events_CloseEvent_internal (gpointer handle)
367 {
368         mono_w32handle_close (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 }