Merge pull request #3678 from mono/seq-read
[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 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_get_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_get_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
175         event_handle->manual = manual;
176         event_handle->set_count = (initial && !manual) ? 1 : 0;
177
178         handle = mono_w32handle_new (type, event_handle);
179         if (handle == INVALID_HANDLE_VALUE) {
180                 g_warning ("%s: error creating %s handle",
181                         __func__, mono_w32handle_get_typename (type));
182                 SetLastError (ERROR_GEN_FAILURE);
183                 return NULL;
184         }
185
186         mono_w32handle_lock_handle (handle);
187
188         if (initial)
189                 mono_w32handle_set_signal_state (handle, TRUE, FALSE);
190
191         mono_w32handle_unlock_handle (handle);
192
193         mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: created %s handle %p",
194                 __func__, mono_w32handle_get_typename (type), handle);
195
196         return handle;
197 }
198
199 static gpointer event_create (gboolean manual, gboolean initial)
200 {
201         MonoW32HandleEvent event_handle;
202         mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: creating %s handle",
203                 __func__, mono_w32handle_get_typename (MONO_W32HANDLE_EVENT));
204         return event_handle_create (&event_handle, MONO_W32HANDLE_EVENT, manual, initial);
205 }
206
207 static gpointer namedevent_create (gboolean manual, gboolean initial, const gunichar2 *name G_GNUC_UNUSED)
208 {
209         gpointer handle;
210         gchar *utf8_name;
211
212         mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: creating %s handle",
213                 __func__, mono_w32handle_get_typename (MONO_W32HANDLE_NAMEDEVENT));
214
215         /* w32 seems to guarantee that opening named objects can't race each other */
216         mono_w32handle_namespace_lock ();
217
218         utf8_name = g_utf16_to_utf8 (name, -1, NULL, NULL, NULL);
219
220         handle = mono_w32handle_namespace_search_handle (MONO_W32HANDLE_NAMEDEVENT, utf8_name);
221         if (handle == INVALID_HANDLE_VALUE) {
222                 /* The name has already been used for a different object. */
223                 handle = NULL;
224                 SetLastError (ERROR_INVALID_HANDLE);
225         } else if (handle) {
226                 /* Not an error, but this is how the caller is informed that the event wasn't freshly created */
227                 SetLastError (ERROR_ALREADY_EXISTS);
228
229                 /* mono_w32handle_namespace_search_handle already adds a ref to the handle */
230         } else {
231                 /* A new named event */
232                 MonoW32HandleNamedEvent namedevent_handle;
233
234                 strncpy (&namedevent_handle.sharedns.name [0], utf8_name, MAX_PATH);
235                 namedevent_handle.sharedns.name [MAX_PATH] = '\0';
236
237                 handle = event_handle_create ((MonoW32HandleEvent*) &namedevent_handle, MONO_W32HANDLE_NAMEDEVENT, manual, initial);
238         }
239
240         g_free (utf8_name);
241
242         mono_w32handle_namespace_unlock ();
243
244         return handle;
245 }
246
247 gpointer
248 ves_icall_System_Threading_Events_CreateEvent_internal (MonoBoolean manual, MonoBoolean initial, MonoString *name, gint32 *error)
249 {
250         gpointer event;
251
252         /* Need to blow away any old errors here, because code tests
253          * for ERROR_ALREADY_EXISTS on success (!) to see if an event
254          * was freshly created */
255         SetLastError (ERROR_SUCCESS);
256
257         event = name ? namedevent_create (manual, initial, mono_string_chars (name)) : event_create (manual, initial);
258
259         *error = GetLastError ();
260
261         return event;
262 }
263
264 gboolean
265 ves_icall_System_Threading_Events_SetEvent_internal (gpointer handle)
266 {
267         MonoW32HandleType type;
268         MonoW32HandleEvent *event_handle;
269
270         if (handle == NULL) {
271                 SetLastError (ERROR_INVALID_HANDLE);
272                 return(FALSE);
273         }
274
275         switch (type = mono_w32handle_get_type (handle)) {
276         case MONO_W32HANDLE_EVENT:
277         case MONO_W32HANDLE_NAMEDEVENT:
278                 break;
279         default:
280                 SetLastError (ERROR_INVALID_HANDLE);
281                 return FALSE;
282         }
283
284         if (!mono_w32handle_lookup (handle, type, (gpointer *)&event_handle)) {
285                 g_warning ("%s: error looking up %s handle %p",
286                         __func__, mono_w32handle_get_typename (type), handle);
287                 return FALSE;
288         }
289
290         mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: setting %s handle %p",
291                 __func__, mono_w32handle_get_typename (type), handle);
292
293         mono_w32handle_lock_handle (handle);
294
295         if (!event_handle->manual) {
296                 event_handle->set_count = 1;
297                 mono_w32handle_set_signal_state (handle, TRUE, FALSE);
298         } else {
299                 mono_w32handle_set_signal_state (handle, TRUE, TRUE);
300         }
301
302         mono_w32handle_unlock_handle (handle);
303
304         return TRUE;
305 }
306
307 gboolean
308 ves_icall_System_Threading_Events_ResetEvent_internal (gpointer handle)
309 {
310         MonoW32HandleType type;
311         MonoW32HandleEvent *event_handle;
312
313         SetLastError (ERROR_SUCCESS);
314
315         if (handle == NULL) {
316                 SetLastError (ERROR_INVALID_HANDLE);
317                 return(FALSE);
318         }
319
320         switch (type = mono_w32handle_get_type (handle)) {
321         case MONO_W32HANDLE_EVENT:
322         case MONO_W32HANDLE_NAMEDEVENT:
323                 break;
324         default:
325                 SetLastError (ERROR_INVALID_HANDLE);
326                 return FALSE;
327         }
328
329         if (!mono_w32handle_lookup (handle, type, (gpointer *)&event_handle)) {
330                 g_warning ("%s: error looking up %s handle %p",
331                         __func__, mono_w32handle_get_typename (type), handle);
332                 return FALSE;
333         }
334
335         mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: resetting %s handle %p",
336                 __func__, mono_w32handle_get_typename (type), handle);
337
338         mono_w32handle_lock_handle (handle);
339
340         if (!mono_w32handle_issignalled (handle)) {
341                 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: no need to reset %s handle %p",
342                         __func__, mono_w32handle_get_typename (type), handle);
343         } else {
344                 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: obtained write lock on %s handle %p",
345                         __func__, mono_w32handle_get_typename (type), handle);
346
347                 mono_w32handle_set_signal_state (handle, FALSE, FALSE);
348         }
349
350         event_handle->set_count = 0;
351
352         mono_w32handle_unlock_handle (handle);
353
354         return TRUE;
355 }
356
357 void
358 ves_icall_System_Threading_Events_CloseEvent_internal (gpointer handle)
359 {
360         CloseHandle (handle);
361 }
362
363 gpointer
364 ves_icall_System_Threading_Events_OpenEvent_internal (MonoString *name, gint32 rights G_GNUC_UNUSED, gint32 *error)
365 {
366         gpointer handle;
367         gchar *utf8_name;
368
369         *error = ERROR_SUCCESS;
370
371         /* w32 seems to guarantee that opening named objects can't race each other */
372         mono_w32handle_namespace_lock ();
373
374         utf8_name = g_utf16_to_utf8 (mono_string_chars (name), -1, NULL, NULL, NULL);
375
376         mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Opening named event [%s]", __func__, utf8_name);
377
378         handle = mono_w32handle_namespace_search_handle (MONO_W32HANDLE_NAMEDEVENT, utf8_name);
379         if (handle == INVALID_HANDLE_VALUE) {
380                 /* The name has already been used for a different object. */
381                 *error = ERROR_INVALID_HANDLE;
382                 goto cleanup;
383         } else if (!handle) {
384                 /* This name doesn't exist */
385                 *error = ERROR_FILE_NOT_FOUND;
386                 goto cleanup;
387         }
388
389         mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: returning named event handle %p", __func__, handle);
390
391 cleanup:
392         g_free (utf8_name);
393
394         mono_w32handle_namespace_unlock ();
395
396         return handle;
397 }
398
399 MonoW32HandleNamespace*
400 mono_w32event_get_namespace (MonoW32HandleNamedEvent *event)
401 {
402         return &event->sharedns;
403 }