Merge pull request #3559 from mistressofjellyfish/httpcookie
[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                 /* mono_w32handle_namespace_search_handle already adds a ref to the handle */
231         } else {
232                 /* A new named event */
233                 MonoW32HandleNamedEvent namedevent_handle;
234
235                 strncpy (&namedevent_handle.sharedns.name [0], utf8_name, MAX_PATH);
236                 namedevent_handle.sharedns.name [MAX_PATH] = '\0';
237
238                 handle = event_handle_create ((MonoW32HandleEvent*) &namedevent_handle, MONO_W32HANDLE_NAMEDEVENT, manual, initial);
239         }
240
241         g_free (utf8_name);
242
243         mono_w32handle_namespace_unlock ();
244
245         return handle;
246 }
247
248 gpointer
249 ves_icall_System_Threading_Events_CreateEvent_internal (MonoBoolean manual, MonoBoolean initial, MonoString *name, gint32 *error)
250 {
251         gpointer event;
252
253         /* Need to blow away any old errors here, because code tests
254          * for ERROR_ALREADY_EXISTS on success (!) to see if an event
255          * was freshly created */
256         SetLastError (ERROR_SUCCESS);
257
258         event = name ? namedevent_create (manual, initial, mono_string_chars (name)) : event_create (manual, initial);
259
260         *error = GetLastError ();
261
262         return event;
263 }
264
265 gboolean
266 ves_icall_System_Threading_Events_SetEvent_internal (gpointer handle)
267 {
268         MonoW32HandleType type;
269         MonoW32HandleEvent *event_handle;
270         int thr_ret;
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_ops_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_ops_typename (type), handle);
294
295         thr_ret = mono_w32handle_lock_handle (handle);
296         g_assert (thr_ret == 0);
297
298         if (!event_handle->manual) {
299                 event_handle->set_count = 1;
300                 mono_w32handle_set_signal_state (handle, TRUE, FALSE);
301         } else {
302                 mono_w32handle_set_signal_state (handle, TRUE, TRUE);
303         }
304
305         thr_ret = mono_w32handle_unlock_handle (handle);
306         g_assert (thr_ret == 0);
307
308         return TRUE;
309 }
310
311 gboolean
312 ves_icall_System_Threading_Events_ResetEvent_internal (gpointer handle)
313 {
314         MonoW32HandleType type;
315         MonoW32HandleEvent *event_handle;
316         int thr_ret;
317
318         SetLastError (ERROR_SUCCESS);
319
320         if (handle == NULL) {
321                 SetLastError (ERROR_INVALID_HANDLE);
322                 return(FALSE);
323         }
324
325         switch (type = mono_w32handle_get_type (handle)) {
326         case MONO_W32HANDLE_EVENT:
327         case MONO_W32HANDLE_NAMEDEVENT:
328                 break;
329         default:
330                 SetLastError (ERROR_INVALID_HANDLE);
331                 return FALSE;
332         }
333
334         if (!mono_w32handle_lookup (handle, type, (gpointer *)&event_handle)) {
335                 g_warning ("%s: error looking up %s handle %p",
336                         __func__, mono_w32handle_ops_typename (type), handle);
337                 return FALSE;
338         }
339
340         mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: resetting %s handle %p",
341                 __func__, mono_w32handle_ops_typename (type), handle);
342
343         thr_ret = mono_w32handle_lock_handle (handle);
344         g_assert (thr_ret == 0);
345
346         if (!mono_w32handle_issignalled (handle)) {
347                 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: no need to reset %s handle %p",
348                         __func__, mono_w32handle_ops_typename (type), handle);
349         } else {
350                 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: obtained write lock on %s handle %p",
351                         __func__, mono_w32handle_ops_typename (type), handle);
352
353                 mono_w32handle_set_signal_state (handle, FALSE, FALSE);
354         }
355
356         event_handle->set_count = 0;
357
358         thr_ret = mono_w32handle_unlock_handle (handle);
359         g_assert (thr_ret == 0);
360
361         return TRUE;
362 }
363
364 void
365 ves_icall_System_Threading_Events_CloseEvent_internal (gpointer handle)
366 {
367         CloseHandle (handle);
368 }
369
370 gpointer
371 ves_icall_System_Threading_Events_OpenEvent_internal (MonoString *name, gint32 rights G_GNUC_UNUSED, gint32 *error)
372 {
373         gpointer handle;
374         gchar *utf8_name;
375
376         *error = ERROR_SUCCESS;
377
378         /* w32 seems to guarantee that opening named objects can't race each other */
379         mono_w32handle_namespace_lock ();
380
381         utf8_name = g_utf16_to_utf8 (mono_string_chars (name), -1, NULL, NULL, NULL);
382
383         mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Opening named event [%s]", __func__, utf8_name);
384
385         handle = mono_w32handle_namespace_search_handle (MONO_W32HANDLE_NAMEDEVENT, utf8_name);
386         if (handle == INVALID_HANDLE_VALUE) {
387                 /* The name has already been used for a different object. */
388                 *error = ERROR_INVALID_HANDLE;
389                 goto cleanup;
390         } else if (!handle) {
391                 /* This name doesn't exist */
392                 *error = ERROR_FILE_NOT_FOUND;
393                 goto cleanup;
394         }
395
396         mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: returning named event handle %p", __func__, handle);
397
398 cleanup:
399         g_free (utf8_name);
400
401         mono_w32handle_namespace_unlock ();
402
403         return handle;
404 }
405
406 MonoW32HandleNamespace*
407 mono_w32event_get_namespace (MonoW32HandleNamedEvent *event)
408 {
409         return &event->sharedns;
410 }