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