2 * w32event-unix.c: Runtime support for managed Event on Unix
5 * Ludovic Henry (luhenry@microsoft.com)
7 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
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"
24 struct MonoW32HandleNamedEvent {
26 MonoW32HandleNamespace sharedns;
29 static gboolean event_handle_own (gpointer handle, MonoW32HandleType type, gboolean *abandoned)
31 MonoW32HandleEvent *event_handle;
36 ok = mono_w32handle_lookup (handle, type, (gpointer *)&event_handle);
38 g_warning ("%s: error looking up %s handle %p",
39 __func__, mono_w32handle_get_typename (type), handle);
43 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: owning %s handle %p",
44 __func__, mono_w32handle_get_typename (type), handle);
46 if (!event_handle->manual) {
47 g_assert (event_handle->set_count > 0);
48 event_handle->set_count --;
50 if (event_handle->set_count == 0)
51 mono_w32handle_set_signal_state (handle, FALSE, FALSE);
57 static void event_signal(gpointer handle)
59 ves_icall_System_Threading_Events_SetEvent_internal (handle);
62 static gboolean event_own (gpointer handle, gboolean *abandoned)
64 return event_handle_own (handle, MONO_W32HANDLE_EVENT, abandoned);
67 static void namedevent_signal (gpointer handle)
69 ves_icall_System_Threading_Events_SetEvent_internal (handle);
72 /* NB, always called with the shared handle lock held */
73 static gboolean namedevent_own (gpointer handle, gboolean *abandoned)
75 return event_handle_own (handle, MONO_W32HANDLE_NAMEDEVENT, abandoned);
78 static void event_details (gpointer data)
80 MonoW32HandleEvent *event = (MonoW32HandleEvent *)data;
81 g_print ("manual: %s, set_count: %d",
82 event->manual ? "TRUE" : "FALSE", event->set_count);
85 static void namedevent_details (gpointer data)
87 MonoW32HandleNamedEvent *namedevent = (MonoW32HandleNamedEvent *)data;
88 g_print ("manual: %s, set_count: %d, name: \"%s\"",
89 namedevent->e.manual ? "TRUE" : "FALSE", namedevent->e.set_count, namedevent->sharedns.name);
92 static const gchar* event_typename (void)
97 static gsize event_typesize (void)
99 return sizeof (MonoW32HandleEvent);
102 static const gchar* namedevent_typename (void)
107 static gsize namedevent_typesize (void)
109 return sizeof (MonoW32HandleNamedEvent);
113 mono_w32event_init (void)
115 static MonoW32HandleOps event_ops = {
117 event_signal, /* signal */
120 NULL, /* special_wait */
122 event_details, /* details */
123 event_typename, /* typename */
124 event_typesize, /* typesize */
127 static MonoW32HandleOps namedevent_ops = {
129 namedevent_signal, /* signal */
130 namedevent_own, /* own */
132 NULL, /* special_wait */
134 namedevent_details, /* details */
135 namedevent_typename, /* typename */
136 namedevent_typesize, /* typesize */
139 mono_w32handle_register_ops (MONO_W32HANDLE_EVENT, &event_ops);
140 mono_w32handle_register_ops (MONO_W32HANDLE_NAMEDEVENT, &namedevent_ops);
142 mono_w32handle_register_capabilities (MONO_W32HANDLE_EVENT,
143 (MonoW32HandleCapability)(MONO_W32HANDLE_CAP_WAIT | MONO_W32HANDLE_CAP_SIGNAL));
144 mono_w32handle_register_capabilities (MONO_W32HANDLE_NAMEDEVENT,
145 (MonoW32HandleCapability)(MONO_W32HANDLE_CAP_WAIT | MONO_W32HANDLE_CAP_SIGNAL));
149 mono_w32event_create (gboolean manual, gboolean initial)
154 handle = ves_icall_System_Threading_Events_CreateEvent_internal (manual, initial, NULL, &error);
155 if (error != ERROR_SUCCESS)
162 mono_w32event_set (gpointer handle)
164 ves_icall_System_Threading_Events_SetEvent_internal (handle);
168 mono_w32event_reset (gpointer handle)
170 ves_icall_System_Threading_Events_ResetEvent_internal (handle);
173 static gpointer event_handle_create (MonoW32HandleEvent *event_handle, MonoW32HandleType type, gboolean manual, gboolean initial)
177 event_handle->manual = manual;
178 event_handle->set_count = (initial && !manual) ? 1 : 0;
180 handle = mono_w32handle_new (type, event_handle);
181 if (handle == INVALID_HANDLE_VALUE) {
182 g_warning ("%s: error creating %s handle",
183 __func__, mono_w32handle_get_typename (type));
184 SetLastError (ERROR_GEN_FAILURE);
188 mono_w32handle_lock_handle (handle);
191 mono_w32handle_set_signal_state (handle, TRUE, FALSE);
193 mono_w32handle_unlock_handle (handle);
195 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: created %s handle %p",
196 __func__, mono_w32handle_get_typename (type), handle);
201 static gpointer event_create (gboolean manual, gboolean initial)
203 MonoW32HandleEvent event_handle;
204 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: creating %s handle",
205 __func__, mono_w32handle_get_typename (MONO_W32HANDLE_EVENT));
206 return event_handle_create (&event_handle, MONO_W32HANDLE_EVENT, manual, initial);
209 static gpointer namedevent_create (gboolean manual, gboolean initial, const gunichar2 *name G_GNUC_UNUSED)
214 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: creating %s handle",
215 __func__, mono_w32handle_get_typename (MONO_W32HANDLE_NAMEDEVENT));
217 /* w32 seems to guarantee that opening named objects can't race each other */
218 mono_w32handle_namespace_lock ();
220 utf8_name = g_utf16_to_utf8 (name, -1, NULL, NULL, NULL);
222 handle = mono_w32handle_namespace_search_handle (MONO_W32HANDLE_NAMEDEVENT, utf8_name);
223 if (handle == INVALID_HANDLE_VALUE) {
224 /* The name has already been used for a different object. */
226 SetLastError (ERROR_INVALID_HANDLE);
228 /* Not an error, but this is how the caller is informed that the event wasn't freshly created */
229 SetLastError (ERROR_ALREADY_EXISTS);
231 /* mono_w32handle_namespace_search_handle already adds a ref to the handle */
233 /* A new named event */
234 MonoW32HandleNamedEvent namedevent_handle;
236 strncpy (&namedevent_handle.sharedns.name [0], utf8_name, MAX_PATH);
237 namedevent_handle.sharedns.name [MAX_PATH] = '\0';
239 handle = event_handle_create ((MonoW32HandleEvent*) &namedevent_handle, MONO_W32HANDLE_NAMEDEVENT, manual, initial);
244 mono_w32handle_namespace_unlock ();
250 ves_icall_System_Threading_Events_CreateEvent_internal (MonoBoolean manual, MonoBoolean initial, MonoString *name, gint32 *error)
254 /* Need to blow away any old errors here, because code tests
255 * for ERROR_ALREADY_EXISTS on success (!) to see if an event
256 * was freshly created */
257 SetLastError (ERROR_SUCCESS);
259 event = name ? namedevent_create (manual, initial, mono_string_chars (name)) : event_create (manual, initial);
261 *error = GetLastError ();
267 ves_icall_System_Threading_Events_SetEvent_internal (gpointer handle)
269 MonoW32HandleType type;
270 MonoW32HandleEvent *event_handle;
272 if (handle == NULL) {
273 SetLastError (ERROR_INVALID_HANDLE);
277 switch (type = mono_w32handle_get_type (handle)) {
278 case MONO_W32HANDLE_EVENT:
279 case MONO_W32HANDLE_NAMEDEVENT:
282 SetLastError (ERROR_INVALID_HANDLE);
286 if (!mono_w32handle_lookup (handle, type, (gpointer *)&event_handle)) {
287 g_warning ("%s: error looking up %s handle %p",
288 __func__, mono_w32handle_get_typename (type), handle);
292 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: setting %s handle %p",
293 __func__, mono_w32handle_get_typename (type), handle);
295 mono_w32handle_lock_handle (handle);
297 if (!event_handle->manual) {
298 event_handle->set_count = 1;
299 mono_w32handle_set_signal_state (handle, TRUE, FALSE);
301 mono_w32handle_set_signal_state (handle, TRUE, TRUE);
304 mono_w32handle_unlock_handle (handle);
310 ves_icall_System_Threading_Events_ResetEvent_internal (gpointer handle)
312 MonoW32HandleType type;
313 MonoW32HandleEvent *event_handle;
315 SetLastError (ERROR_SUCCESS);
317 if (handle == NULL) {
318 SetLastError (ERROR_INVALID_HANDLE);
322 switch (type = mono_w32handle_get_type (handle)) {
323 case MONO_W32HANDLE_EVENT:
324 case MONO_W32HANDLE_NAMEDEVENT:
327 SetLastError (ERROR_INVALID_HANDLE);
331 if (!mono_w32handle_lookup (handle, type, (gpointer *)&event_handle)) {
332 g_warning ("%s: error looking up %s handle %p",
333 __func__, mono_w32handle_get_typename (type), handle);
337 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: resetting %s handle %p",
338 __func__, mono_w32handle_get_typename (type), handle);
340 mono_w32handle_lock_handle (handle);
342 if (!mono_w32handle_issignalled (handle)) {
343 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: no need to reset %s handle %p",
344 __func__, mono_w32handle_get_typename (type), handle);
346 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: obtained write lock on %s handle %p",
347 __func__, mono_w32handle_get_typename (type), handle);
349 mono_w32handle_set_signal_state (handle, FALSE, FALSE);
352 event_handle->set_count = 0;
354 mono_w32handle_unlock_handle (handle);
360 ves_icall_System_Threading_Events_CloseEvent_internal (gpointer handle)
362 CloseHandle (handle);
366 ves_icall_System_Threading_Events_OpenEvent_internal (MonoString *name, gint32 rights G_GNUC_UNUSED, gint32 *error)
371 *error = ERROR_SUCCESS;
373 /* w32 seems to guarantee that opening named objects can't race each other */
374 mono_w32handle_namespace_lock ();
376 utf8_name = g_utf16_to_utf8 (mono_string_chars (name), -1, NULL, NULL, NULL);
378 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Opening named event [%s]", __func__, utf8_name);
380 handle = mono_w32handle_namespace_search_handle (MONO_W32HANDLE_NAMEDEVENT, utf8_name);
381 if (handle == INVALID_HANDLE_VALUE) {
382 /* The name has already been used for a different object. */
383 *error = ERROR_INVALID_HANDLE;
385 } else if (!handle) {
386 /* This name doesn't exist */
387 *error = ERROR_FILE_NOT_FOUND;
391 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: returning named event handle %p", __func__, handle);
396 mono_w32handle_namespace_unlock ();
401 MonoW32HandleNamespace*
402 mono_w32event_get_namespace (MonoW32HandleNamedEvent *event)
404 return &event->sharedns;