3 * Runtime support for managed Event on Unix
6 * Ludovic Henry (luhenry@microsoft.com)
8 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
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"
24 mono_w32event_create_full (MonoBoolean manual, MonoBoolean initial, const gchar *name, gint32 *err);
32 struct MonoW32HandleNamedEvent {
34 MonoW32HandleNamespace sharedns;
37 static gboolean event_handle_own (gpointer handle, MonoW32HandleType type, gboolean *abandoned)
39 MonoW32HandleEvent *event_handle;
44 ok = mono_w32handle_lookup (handle, type, (gpointer *)&event_handle);
46 g_warning ("%s: error looking up %s handle %p",
47 __func__, mono_w32handle_get_typename (type), handle);
51 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: owning %s handle %p",
52 __func__, mono_w32handle_get_typename (type), handle);
54 if (!event_handle->manual) {
55 g_assert (event_handle->set_count > 0);
56 event_handle->set_count --;
58 if (event_handle->set_count == 0)
59 mono_w32handle_set_signal_state (handle, FALSE, FALSE);
65 static void event_signal(gpointer handle)
67 ves_icall_System_Threading_Events_SetEvent_internal (handle);
70 static gboolean event_own (gpointer handle, gboolean *abandoned)
72 return event_handle_own (handle, MONO_W32HANDLE_EVENT, abandoned);
75 static void namedevent_signal (gpointer handle)
77 ves_icall_System_Threading_Events_SetEvent_internal (handle);
80 /* NB, always called with the shared handle lock held */
81 static gboolean namedevent_own (gpointer handle, gboolean *abandoned)
83 return event_handle_own (handle, MONO_W32HANDLE_NAMEDEVENT, abandoned);
86 static void event_details (gpointer data)
88 MonoW32HandleEvent *event = (MonoW32HandleEvent *)data;
89 g_print ("manual: %s, set_count: %d",
90 event->manual ? "TRUE" : "FALSE", event->set_count);
93 static void namedevent_details (gpointer data)
95 MonoW32HandleNamedEvent *namedevent = (MonoW32HandleNamedEvent *)data;
96 g_print ("manual: %s, set_count: %d, name: \"%s\"",
97 namedevent->e.manual ? "TRUE" : "FALSE", namedevent->e.set_count, namedevent->sharedns.name);
100 static const gchar* event_typename (void)
105 static gsize event_typesize (void)
107 return sizeof (MonoW32HandleEvent);
110 static const gchar* namedevent_typename (void)
115 static gsize namedevent_typesize (void)
117 return sizeof (MonoW32HandleNamedEvent);
121 mono_w32event_init (void)
123 static MonoW32HandleOps event_ops = {
125 event_signal, /* signal */
128 NULL, /* special_wait */
130 event_details, /* details */
131 event_typename, /* typename */
132 event_typesize, /* typesize */
135 static MonoW32HandleOps namedevent_ops = {
137 namedevent_signal, /* signal */
138 namedevent_own, /* own */
140 NULL, /* special_wait */
142 namedevent_details, /* details */
143 namedevent_typename, /* typename */
144 namedevent_typesize, /* typesize */
147 mono_w32handle_register_ops (MONO_W32HANDLE_EVENT, &event_ops);
148 mono_w32handle_register_ops (MONO_W32HANDLE_NAMEDEVENT, &namedevent_ops);
150 mono_w32handle_register_capabilities (MONO_W32HANDLE_EVENT,
151 (MonoW32HandleCapability)(MONO_W32HANDLE_CAP_WAIT | MONO_W32HANDLE_CAP_SIGNAL));
152 mono_w32handle_register_capabilities (MONO_W32HANDLE_NAMEDEVENT,
153 (MonoW32HandleCapability)(MONO_W32HANDLE_CAP_WAIT | MONO_W32HANDLE_CAP_SIGNAL));
157 mono_w32event_create (gboolean manual, gboolean initial)
162 handle = mono_w32event_create_full (manual, initial, NULL, &error);
163 if (error != ERROR_SUCCESS)
170 mono_w32event_close (gpointer handle)
172 return mono_w32handle_close (handle);
176 mono_w32event_set (gpointer handle)
178 ves_icall_System_Threading_Events_SetEvent_internal (handle);
182 mono_w32event_reset (gpointer handle)
184 ves_icall_System_Threading_Events_ResetEvent_internal (handle);
187 static gpointer event_handle_create (MonoW32HandleEvent *event_handle, MonoW32HandleType type, gboolean manual, gboolean initial)
191 event_handle->manual = manual;
192 event_handle->set_count = (initial && !manual) ? 1 : 0;
194 handle = mono_w32handle_new (type, event_handle);
195 if (handle == INVALID_HANDLE_VALUE) {
196 g_warning ("%s: error creating %s handle",
197 __func__, mono_w32handle_get_typename (type));
198 mono_w32error_set_last (ERROR_GEN_FAILURE);
202 mono_w32handle_lock_handle (handle);
205 mono_w32handle_set_signal_state (handle, TRUE, FALSE);
207 mono_w32handle_unlock_handle (handle);
209 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: created %s handle %p",
210 __func__, mono_w32handle_get_typename (type), handle);
215 static gpointer event_create (gboolean manual, gboolean initial)
217 MonoW32HandleEvent event_handle;
218 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: creating %s handle",
219 __func__, mono_w32handle_get_typename (MONO_W32HANDLE_EVENT));
220 return event_handle_create (&event_handle, MONO_W32HANDLE_EVENT, manual, initial);
223 static gpointer namedevent_create (gboolean manual, gboolean initial, const gchar *utf8_name G_GNUC_UNUSED)
227 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: creating %s handle",
228 __func__, mono_w32handle_get_typename (MONO_W32HANDLE_NAMEDEVENT));
230 /* w32 seems to guarantee that opening named objects can't race each other */
231 mono_w32handle_namespace_lock ();
233 glong utf8_len = strlen (utf8_name);
235 handle = mono_w32handle_namespace_search_handle (MONO_W32HANDLE_NAMEDEVENT, utf8_name);
236 if (handle == INVALID_HANDLE_VALUE) {
237 /* The name has already been used for a different object. */
239 mono_w32error_set_last (ERROR_INVALID_HANDLE);
241 /* Not an error, but this is how the caller is informed that the event wasn't freshly created */
242 mono_w32error_set_last (ERROR_ALREADY_EXISTS);
244 /* mono_w32handle_namespace_search_handle already adds a ref to the handle */
246 /* A new named event */
247 MonoW32HandleNamedEvent namedevent_handle;
249 size_t len = utf8_len < MAX_PATH ? utf8_len : MAX_PATH;
250 memcpy (&namedevent_handle.sharedns.name [0], utf8_name, len);
251 namedevent_handle.sharedns.name [len] = '\0';
253 handle = event_handle_create ((MonoW32HandleEvent*) &namedevent_handle, MONO_W32HANDLE_NAMEDEVENT, manual, initial);
256 mono_w32handle_namespace_unlock ();
262 mono_w32event_create_full (MonoBoolean manual, MonoBoolean initial, const gchar *name, gint32 *error)
266 /* Need to blow away any old errors here, because code tests
267 * for ERROR_ALREADY_EXISTS on success (!) to see if an event
268 * was freshly created */
269 mono_w32error_set_last (ERROR_SUCCESS);
271 event = name ? namedevent_create (manual, initial, name) : event_create (manual, initial);
273 *error = mono_w32error_get_last ();
279 ves_icall_System_Threading_Events_CreateEvent_internal (MonoBoolean manual, MonoBoolean initial, MonoStringHandle name, gint32 *err, MonoError *error)
282 gchar *utf8_name = mono_string_handle_to_utf8 (name, error);
283 return_val_if_nok (error, NULL);
284 gpointer result = mono_w32event_create_full (manual, initial, utf8_name, err);
290 ves_icall_System_Threading_Events_SetEvent_internal (gpointer handle)
292 MonoW32HandleType type;
293 MonoW32HandleEvent *event_handle;
295 if (handle == NULL) {
296 mono_w32error_set_last (ERROR_INVALID_HANDLE);
300 switch (type = mono_w32handle_get_type (handle)) {
301 case MONO_W32HANDLE_EVENT:
302 case MONO_W32HANDLE_NAMEDEVENT:
305 mono_w32error_set_last (ERROR_INVALID_HANDLE);
309 if (!mono_w32handle_lookup (handle, type, (gpointer *)&event_handle)) {
310 g_warning ("%s: error looking up %s handle %p",
311 __func__, mono_w32handle_get_typename (type), handle);
315 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: setting %s handle %p",
316 __func__, mono_w32handle_get_typename (type), handle);
318 mono_w32handle_lock_handle (handle);
320 if (!event_handle->manual) {
321 event_handle->set_count = 1;
322 mono_w32handle_set_signal_state (handle, TRUE, FALSE);
324 mono_w32handle_set_signal_state (handle, TRUE, TRUE);
327 mono_w32handle_unlock_handle (handle);
333 ves_icall_System_Threading_Events_ResetEvent_internal (gpointer handle)
335 MonoW32HandleType type;
336 MonoW32HandleEvent *event_handle;
338 mono_w32error_set_last (ERROR_SUCCESS);
340 if (handle == NULL) {
341 mono_w32error_set_last (ERROR_INVALID_HANDLE);
345 switch (type = mono_w32handle_get_type (handle)) {
346 case MONO_W32HANDLE_EVENT:
347 case MONO_W32HANDLE_NAMEDEVENT:
350 mono_w32error_set_last (ERROR_INVALID_HANDLE);
354 if (!mono_w32handle_lookup (handle, type, (gpointer *)&event_handle)) {
355 g_warning ("%s: error looking up %s handle %p",
356 __func__, mono_w32handle_get_typename (type), handle);
360 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: resetting %s handle %p",
361 __func__, mono_w32handle_get_typename (type), handle);
363 mono_w32handle_lock_handle (handle);
365 if (!mono_w32handle_issignalled (handle)) {
366 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: no need to reset %s handle %p",
367 __func__, mono_w32handle_get_typename (type), handle);
369 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: obtained write lock on %s handle %p",
370 __func__, mono_w32handle_get_typename (type), handle);
372 mono_w32handle_set_signal_state (handle, FALSE, FALSE);
375 event_handle->set_count = 0;
377 mono_w32handle_unlock_handle (handle);
383 ves_icall_System_Threading_Events_CloseEvent_internal (gpointer handle)
385 mono_w32handle_close (handle);
389 ves_icall_System_Threading_Events_OpenEvent_internal (MonoString *name, gint32 rights G_GNUC_UNUSED, gint32 *error)
394 *error = ERROR_SUCCESS;
396 /* w32 seems to guarantee that opening named objects can't race each other */
397 mono_w32handle_namespace_lock ();
399 utf8_name = g_utf16_to_utf8 (mono_string_chars (name), -1, NULL, NULL, NULL);
401 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Opening named event [%s]", __func__, utf8_name);
403 handle = mono_w32handle_namespace_search_handle (MONO_W32HANDLE_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;
408 } else if (!handle) {
409 /* This name doesn't exist */
410 *error = ERROR_FILE_NOT_FOUND;
414 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: returning named event handle %p", __func__, handle);
419 mono_w32handle_namespace_unlock ();
424 MonoW32HandleNamespace*
425 mono_w32event_get_namespace (MonoW32HandleNamedEvent *event)
427 return &event->sharedns;