1 /**
2 * \file
3 * Runtime support for managed Event on Unix
4 *
5 * Author:
6 * Ludovic Henry (luhenry@microsoft.com)
7 *
8 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
9 */
10
11 #include "w32event.h"
12
13 #include "w32error.h"
14 #include "w32handle-namespace.h"
15 #include "mono/utils/mono-error-internals.h"
16 #include "mono/utils/mono-logger-internals.h"
17 #include "mono/metadata/handle.h"
18 #include "mono/metadata/object-internals.h"
19 #include "mono/metadata/w32handle.h"
20
21 #define MAX_PATH 260
22
23 static gpointer
24 mono_w32event_create_full (MonoBoolean manual, MonoBoolean initial, const gchar *name, gint32 *err);
25
26 static gpointer
27 mono_w32event_open (const gchar *utf8_name, gint32 rights G_GNUC_UNUSED, gint32 *error);
28
29 typedef struct {
30 gboolean manual;
31 guint32 set_count;
32 } MonoW32HandleEvent;
33
34 struct MonoW32HandleNamedEvent {
35 MonoW32HandleEvent e;
36 MonoW32HandleNamespace sharedns;
37 };
38
event_handle_signal(MonoW32Handle * handle_data)39 static void event_handle_signal (MonoW32Handle *handle_data)
40 {
41 MonoW32HandleEvent *event_handle;
42
43 event_handle = (MonoW32HandleEvent*) handle_data->specific;
44
45 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_EVENT, "%s: signalling %s handle %p",
46 __func__, mono_w32handle_get_typename (handle_data->type), handle_data);
47
48 if (!event_handle->manual) {
49 event_handle->set_count = 1;
50 mono_w32handle_set_signal_state (handle_data, TRUE, FALSE);
51 } else {
52 mono_w32handle_set_signal_state (handle_data, TRUE, TRUE);
53 }
54 }
55
event_handle_own(MonoW32Handle * handle_data,gboolean * abandoned)56 static gboolean event_handle_own (MonoW32Handle *handle_data, gboolean *abandoned)
57 {
58 MonoW32HandleEvent *event_handle;
59
60 *abandoned = FALSE;
61
62 event_handle = (MonoW32HandleEvent*) handle_data->specific;
63
64 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_EVENT, "%s: owning %s handle %p",
65 __func__, mono_w32handle_get_typename (handle_data->type), handle_data);
66
67 if (!event_handle->manual) {
68 g_assert (event_handle->set_count > 0);
69 event_handle->set_count --;
70
71 if (event_handle->set_count == 0)
72 mono_w32handle_set_signal_state (handle_data, FALSE, FALSE);
73 }
74
75 return TRUE;
76 }
77
event_details(MonoW32Handle * handle_data)78 static void event_details (MonoW32Handle *handle_data)
79 {
80 MonoW32HandleEvent *event = (MonoW32HandleEvent *)handle_data->specific;
81 g_print ("manual: %s, set_count: %d",
82 event->manual ? "TRUE" : "FALSE", event->set_count);
83 }
84
namedevent_details(MonoW32Handle * handle_data)85 static void namedevent_details (MonoW32Handle *handle_data)
86 {
87 MonoW32HandleNamedEvent *namedevent = (MonoW32HandleNamedEvent *)handle_data->specific;
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
event_typename(void)92 static const gchar* event_typename (void)
93 {
94 return "Event";
95 }
96
event_typesize(void)97 static gsize event_typesize (void)
98 {
99 return sizeof (MonoW32HandleEvent);
100 }
101
namedevent_typename(void)102 static const gchar* namedevent_typename (void)
103 {
104 return "N.Event";
105 }
106
namedevent_typesize(void)107 static gsize namedevent_typesize (void)
108 {
109 return sizeof (MonoW32HandleNamedEvent);
110 }
111
112 void
mono_w32event_init(void)113 mono_w32event_init (void)
114 {
115 static MonoW32HandleOps event_ops = {
116 NULL, /* close */
117 event_handle_signal, /* signal */
118 event_handle_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 event_handle_signal, /* signal */
130 event_handle_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_W32TYPE_EVENT, &event_ops);
140 mono_w32handle_register_ops (MONO_W32TYPE_NAMEDEVENT, &namedevent_ops);
141
142 mono_w32handle_register_capabilities (MONO_W32TYPE_EVENT,
143 (MonoW32HandleCapability)(MONO_W32HANDLE_CAP_WAIT | MONO_W32HANDLE_CAP_SIGNAL));
144 mono_w32handle_register_capabilities (MONO_W32TYPE_NAMEDEVENT,
145 (MonoW32HandleCapability)(MONO_W32HANDLE_CAP_WAIT | MONO_W32HANDLE_CAP_SIGNAL));
146 }
147
148 gpointer
mono_w32event_create(gboolean manual,gboolean initial)149 mono_w32event_create (gboolean manual, gboolean initial)
150 {
151 gpointer handle;
152 gint32 error;
153
154 handle = mono_w32event_create_full (manual, initial, NULL, &error);
155 if (error != ERROR_SUCCESS)
156 g_assert (!handle);
157
158 return handle;
159 }
160
161 gboolean
mono_w32event_close(gpointer handle)162 mono_w32event_close (gpointer handle)
163 {
164 return mono_w32handle_close (handle);
165 }
166
167 void
mono_w32event_set(gpointer handle)168 mono_w32event_set (gpointer handle)
169 {
170 ves_icall_System_Threading_Events_SetEvent_internal (handle);
171 }
172
173 void
mono_w32event_reset(gpointer handle)174 mono_w32event_reset (gpointer handle)
175 {
176 ves_icall_System_Threading_Events_ResetEvent_internal (handle);
177 }
178
event_handle_create(MonoW32HandleEvent * event_handle,MonoW32Type type,gboolean manual,gboolean initial)179 static gpointer event_handle_create (MonoW32HandleEvent *event_handle, MonoW32Type type, gboolean manual, gboolean initial)
180 {
181 MonoW32Handle *handle_data;
182 gpointer handle;
183
184 event_handle->manual = manual;
185 event_handle->set_count = (initial && !manual) ? 1 : 0;
186
187 handle = mono_w32handle_new (type, event_handle);
188 if (handle == INVALID_HANDLE_VALUE) {
189 g_warning ("%s: error creating %s handle",
190 __func__, mono_w32handle_get_typename (type));
191 mono_w32error_set_last (ERROR_GEN_FAILURE);
192 return NULL;
193 }
194
195 if (!mono_w32handle_lookup_and_ref (handle, &handle_data))
196 g_error ("%s: unkown handle %p", __func__, handle);
197
198 if (handle_data->type != type)
199 g_error ("%s: unknown event handle %p", __func__, handle);
200
201 mono_w32handle_lock (handle_data);
202
203 if (initial)
204 mono_w32handle_set_signal_state (handle_data, TRUE, FALSE);
205
206 mono_w32handle_unlock (handle_data);
207
208 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_EVENT, "%s: created %s handle %p",
209 __func__, mono_w32handle_get_typename (type), handle);
210
211 mono_w32handle_unref (handle_data);
212
213 return handle;
214 }
215
event_create(gboolean manual,gboolean initial)216 static gpointer event_create (gboolean manual, gboolean initial)
217 {
218 MonoW32HandleEvent event_handle;
219 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_EVENT, "%s: creating %s handle",
220 __func__, mono_w32handle_get_typename (MONO_W32TYPE_EVENT));
221 return event_handle_create (&event_handle, MONO_W32TYPE_EVENT, manual, initial);
222 }
223
namedevent_create(gboolean manual,gboolean initial,const gchar * utf8_name G_GNUC_UNUSED)224 static gpointer namedevent_create (gboolean manual, gboolean initial, const gchar *utf8_name G_GNUC_UNUSED)
225 {
226 gpointer handle;
227
228 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_EVENT, "%s: creating %s handle",
229 __func__, mono_w32handle_get_typename (MONO_W32TYPE_NAMEDEVENT));
230
231 /* w32 seems to guarantee that opening named objects can't race each other */
232 mono_w32handle_namespace_lock ();
233
234 glong utf8_len = strlen (utf8_name);
235
236 handle = mono_w32handle_namespace_search_handle (MONO_W32TYPE_NAMEDEVENT, utf8_name);
237 if (handle == INVALID_HANDLE_VALUE) {
238 /* The name has already been used for a different object. */
239 handle = NULL;
240 mono_w32error_set_last (ERROR_INVALID_HANDLE);
241 } else if (handle) {
242 /* Not an error, but this is how the caller is informed that the event wasn't freshly created */
243 mono_w32error_set_last (ERROR_ALREADY_EXISTS);
244
245 /* mono_w32handle_namespace_search_handle already adds a ref to the handle */
246 } else {
247 /* A new named event */
248 MonoW32HandleNamedEvent namedevent_handle;
249
250 size_t len = utf8_len < MAX_PATH ? utf8_len : MAX_PATH;
251 memcpy (&namedevent_handle.sharedns.name [0], utf8_name, len);
252 namedevent_handle.sharedns.name [len] = '\0';
253
254 handle = event_handle_create ((MonoW32HandleEvent*) &namedevent_handle, MONO_W32TYPE_NAMEDEVENT, manual, initial);
255 }
256
257 mono_w32handle_namespace_unlock ();
258
259 return handle;
260 }
261
262 gpointer
mono_w32event_create_full(MonoBoolean manual,MonoBoolean initial,const gchar * name,gint32 * error)263 mono_w32event_create_full (MonoBoolean manual, MonoBoolean initial, const gchar *name, gint32 *error)
264 {
265 gpointer event;
266
267 /* Need to blow away any old errors here, because code tests
268 * for ERROR_ALREADY_EXISTS on success (!) to see if an event
269 * was freshly created */
270 mono_w32error_set_last (ERROR_SUCCESS);
271
272 event = name ? namedevent_create (manual, initial, name) : event_create (manual, initial);
273
274 *error = mono_w32error_get_last ();
275
276 return event;
277 }
278
279 gpointer
ves_icall_System_Threading_Events_CreateEvent_internal(MonoBoolean manual,MonoBoolean initial,MonoStringHandle name,gint32 * err,MonoError * error)280 ves_icall_System_Threading_Events_CreateEvent_internal (MonoBoolean manual, MonoBoolean initial, MonoStringHandle name, gint32 *err, MonoError *error)
281 {
282 error_init (error);
283 gchar *utf8_name = mono_string_handle_to_utf8 (name, error);
284 return_val_if_nok (error, NULL);
285 gpointer result = mono_w32event_create_full (manual, initial, utf8_name, err);
286 g_free (utf8_name);
287 return result;
288 }
289
290 gboolean
ves_icall_System_Threading_Events_SetEvent_internal(gpointer handle)291 ves_icall_System_Threading_Events_SetEvent_internal (gpointer handle)
292 {
293 MonoW32Handle *handle_data;
294 MonoW32HandleEvent *event_handle;
295
296 if (!mono_w32handle_lookup_and_ref (handle, &handle_data)) {
297 g_warning ("%s: unkown handle %p", __func__, handle);
298 mono_w32error_set_last (ERROR_INVALID_HANDLE);
299 return FALSE;
300 }
301
302 if (handle_data->type != MONO_W32TYPE_EVENT && handle_data->type != MONO_W32TYPE_NAMEDEVENT) {
303 g_warning ("%s: unkown event handle %p", __func__, handle);
304 mono_w32error_set_last (ERROR_INVALID_HANDLE);
305 mono_w32handle_unref (handle_data);
306 return FALSE;
307 }
308
309 event_handle = (MonoW32HandleEvent*) handle_data->specific;
310
311 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_EVENT, "%s: setting %s handle %p",
312 __func__, mono_w32handle_get_typename (handle_data->type), handle);
313
314 mono_w32handle_lock (handle_data);
315
316 if (!event_handle->manual) {
317 event_handle->set_count = 1;
318 mono_w32handle_set_signal_state (handle_data, TRUE, FALSE);
319 } else {
320 mono_w32handle_set_signal_state (handle_data, TRUE, TRUE);
321 }
322
323 mono_w32handle_unlock (handle_data);
324
325 mono_w32handle_unref (handle_data);
326 return TRUE;
327 }
328
329 gboolean
ves_icall_System_Threading_Events_ResetEvent_internal(gpointer handle)330 ves_icall_System_Threading_Events_ResetEvent_internal (gpointer handle)
331 {
332 MonoW32Handle *handle_data;
333 MonoW32HandleEvent *event_handle;
334
335 mono_w32error_set_last (ERROR_SUCCESS);
336
337 if (!mono_w32handle_lookup_and_ref (handle, &handle_data)) {
338 g_warning ("%s: unkown handle %p", __func__, handle);
339 mono_w32error_set_last (ERROR_INVALID_HANDLE);
340 return FALSE;
341 }
342
343 if (handle_data->type != MONO_W32TYPE_EVENT && handle_data->type != MONO_W32TYPE_NAMEDEVENT) {
344 g_warning ("%s: unkown event handle %p", __func__, handle);
345 mono_w32error_set_last (ERROR_INVALID_HANDLE);
346 mono_w32handle_unref (handle_data);
347 return FALSE;
348 }
349
350 event_handle = (MonoW32HandleEvent*) handle_data->specific;
351
352 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_EVENT, "%s: resetting %s handle %p",
353 __func__, mono_w32handle_get_typename (handle_data->type), handle);
354
355 mono_w32handle_lock (handle_data);
356
357 if (!mono_w32handle_issignalled (handle_data)) {
358 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_EVENT, "%s: no need to reset %s handle %p",
359 __func__, mono_w32handle_get_typename (handle_data->type), handle);
360 } else {
361 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_EVENT, "%s: obtained write lock on %s handle %p",
362 __func__, mono_w32handle_get_typename (handle_data->type), handle);
363
364 mono_w32handle_set_signal_state (handle_data, FALSE, FALSE);
365 }
366
367 event_handle->set_count = 0;
368
369 mono_w32handle_unlock (handle_data);
370
371 mono_w32handle_unref (handle_data);
372 return TRUE;
373 }
374
375 void
ves_icall_System_Threading_Events_CloseEvent_internal(gpointer handle)376 ves_icall_System_Threading_Events_CloseEvent_internal (gpointer handle)
377 {
378 mono_w32handle_close (handle);
379 }
380
381 gpointer
ves_icall_System_Threading_Events_OpenEvent_internal(MonoStringHandle name,gint32 rights G_GNUC_UNUSED,gint32 * err,MonoError * error)382 ves_icall_System_Threading_Events_OpenEvent_internal (MonoStringHandle name, gint32 rights G_GNUC_UNUSED, gint32 *err, MonoError *error)
383 {
384 error_init (error);
385 gchar *utf8_name = mono_string_handle_to_utf8 (name, error);
386 return_val_if_nok (error, NULL);
387 gpointer handle = mono_w32event_open (utf8_name, rights, err);
388 g_free (utf8_name);
389 return handle;
390 }
391
392 gpointer
mono_w32event_open(const gchar * utf8_name,gint32 rights G_GNUC_UNUSED,gint32 * error)393 mono_w32event_open (const gchar *utf8_name, gint32 rights G_GNUC_UNUSED, gint32 *error)
394 {
395 gpointer handle;
396 *error = ERROR_SUCCESS;
397
398 /* w32 seems to guarantee that opening named objects can't race each other */
399 mono_w32handle_namespace_lock ();
400
401 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_EVENT, "%s: Opening named event [%s]", __func__, utf8_name);
402
403 handle = mono_w32handle_namespace_search_handle (MONO_W32TYPE_NAMEDEVENT, utf8_name);
404 if (handle == INVALID_HANDLE_VALUE) {
405 /* The name has already been used for a different object. */
406 *error = ERROR_INVALID_HANDLE;
407 goto cleanup;
408 } else if (!handle) {
409 /* This name doesn't exist */
410 *error = ERROR_FILE_NOT_FOUND;
411 goto cleanup;
412 }
413
414 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_EVENT, "%s: returning named event handle %p", __func__, handle);
415
416 cleanup:
417 mono_w32handle_namespace_unlock ();
418
419 return handle;
420 }
421
422 MonoW32HandleNamespace*
mono_w32event_get_namespace(MonoW32HandleNamedEvent * event)423 mono_w32event_get_namespace (MonoW32HandleNamedEvent *event)
424 {
425 return &event->sharedns;
426 }
427