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/utils/w32handle.h"
22 struct MonoW32HandleNamedEvent {
24 MonoW32HandleNamespace sharedns;
27 static gboolean event_handle_own (gpointer handle, MonoW32HandleType type)
29 MonoW32HandleEvent *event_handle;
32 ok = mono_w32handle_lookup (handle, type, (gpointer *)&event_handle);
34 g_warning ("%s: error looking up %s handle %p",
35 __func__, mono_w32handle_ops_typename (type), handle);
39 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: owning %s handle %p",
40 __func__, mono_w32handle_ops_typename (type), handle);
42 if (!event_handle->manual) {
43 g_assert (event_handle->set_count > 0);
44 event_handle->set_count --;
46 if (event_handle->set_count == 0)
47 mono_w32handle_set_signal_state (handle, FALSE, FALSE);
53 static void event_signal(gpointer handle)
55 ves_icall_System_Threading_Events_SetEvent_internal (handle);
58 static gboolean event_own (gpointer handle)
60 return event_handle_own (handle, MONO_W32HANDLE_EVENT);
63 static void namedevent_signal (gpointer handle)
65 ves_icall_System_Threading_Events_SetEvent_internal (handle);
68 /* NB, always called with the shared handle lock held */
69 static gboolean namedevent_own (gpointer handle)
71 return event_handle_own (handle, MONO_W32HANDLE_NAMEDEVENT);
74 static void event_details (gpointer data)
76 MonoW32HandleEvent *event = (MonoW32HandleEvent *)data;
77 g_print ("manual: %s, set_count: %d",
78 event->manual ? "TRUE" : "FALSE", event->set_count);
81 static void namedevent_details (gpointer data)
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);
88 static const gchar* event_typename (void)
93 static gsize event_typesize (void)
95 return sizeof (MonoW32HandleEvent);
98 static const gchar* namedevent_typename (void)
103 static gsize namedevent_typesize (void)
105 return sizeof (MonoW32HandleNamedEvent);
109 mono_w32event_init (void)
111 static MonoW32HandleOps event_ops = {
113 event_signal, /* signal */
116 NULL, /* special_wait */
118 event_details, /* details */
119 event_typename, /* typename */
120 event_typesize, /* typesize */
123 static MonoW32HandleOps namedevent_ops = {
125 namedevent_signal, /* signal */
126 namedevent_own, /* own */
128 NULL, /* special_wait */
130 namedevent_details, /* details */
131 namedevent_typename, /* typename */
132 namedevent_typesize, /* typesize */
135 mono_w32handle_register_ops (MONO_W32HANDLE_EVENT, &event_ops);
136 mono_w32handle_register_ops (MONO_W32HANDLE_NAMEDEVENT, &namedevent_ops);
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));
145 mono_w32event_create (gboolean manual, gboolean initial)
150 handle = ves_icall_System_Threading_Events_CreateEvent_internal (manual, initial, NULL, &error);
151 if (error != ERROR_SUCCESS)
158 mono_w32event_set (gpointer handle)
160 ves_icall_System_Threading_Events_SetEvent_internal (handle);
164 mono_w32event_reset (gpointer handle)
166 ves_icall_System_Threading_Events_ResetEvent_internal (handle);
169 static gpointer event_handle_create (MonoW32HandleEvent *event_handle, MonoW32HandleType type, gboolean manual, gboolean initial)
174 event_handle->manual = manual;
175 event_handle->set_count = (initial && !manual) ? 1 : 0;
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);
185 thr_ret = mono_w32handle_lock_handle (handle);
186 g_assert (thr_ret == 0);
189 mono_w32handle_set_signal_state (handle, TRUE, FALSE);
191 thr_ret = mono_w32handle_unlock_handle (handle);
192 g_assert (thr_ret == 0);
194 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: created %s handle %p",
195 __func__, mono_w32handle_ops_typename (type), handle);
200 static gpointer event_create (gboolean manual, gboolean initial)
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);
208 static gpointer namedevent_create (gboolean manual, gboolean initial, const gunichar2 *name G_GNUC_UNUSED)
213 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: creating %s handle",
214 __func__, mono_w32handle_ops_typename (MONO_W32HANDLE_NAMEDEVENT));
216 /* w32 seems to guarantee that opening named objects can't race each other */
217 mono_w32handle_namespace_lock ();
219 utf8_name = g_utf16_to_utf8 (name, -1, NULL, NULL, NULL);
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. */
225 SetLastError (ERROR_INVALID_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);
230 /* this is used as creating a new handle */
231 mono_w32handle_ref (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;
273 if (handle == NULL) {
274 SetLastError (ERROR_INVALID_HANDLE);
278 switch (type = mono_w32handle_get_type (handle)) {
279 case MONO_W32HANDLE_EVENT:
280 case MONO_W32HANDLE_NAMEDEVENT:
283 SetLastError (ERROR_INVALID_HANDLE);
287 if (!mono_w32handle_lookup (handle, type, (gpointer *)&event_handle)) {
288 g_warning ("%s: error looking up %s handle %p",
289 __func__, mono_w32handle_ops_typename (type), handle);
293 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: setting %s handle %p",
294 __func__, mono_w32handle_ops_typename (type), handle);
296 thr_ret = mono_w32handle_lock_handle (handle);
297 g_assert (thr_ret == 0);
299 if (!event_handle->manual) {
300 event_handle->set_count = 1;
301 mono_w32handle_set_signal_state (handle, TRUE, FALSE);
303 mono_w32handle_set_signal_state (handle, TRUE, TRUE);
306 thr_ret = mono_w32handle_unlock_handle (handle);
307 g_assert (thr_ret == 0);
313 ves_icall_System_Threading_Events_ResetEvent_internal (gpointer handle)
315 MonoW32HandleType type;
316 MonoW32HandleEvent *event_handle;
319 SetLastError (ERROR_SUCCESS);
321 if (handle == NULL) {
322 SetLastError (ERROR_INVALID_HANDLE);
326 switch (type = mono_w32handle_get_type (handle)) {
327 case MONO_W32HANDLE_EVENT:
328 case MONO_W32HANDLE_NAMEDEVENT:
331 SetLastError (ERROR_INVALID_HANDLE);
335 if (!mono_w32handle_lookup (handle, type, (gpointer *)&event_handle)) {
336 g_warning ("%s: error looking up %s handle %p",
337 __func__, mono_w32handle_ops_typename (type), handle);
341 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: resetting %s handle %p",
342 __func__, mono_w32handle_ops_typename (type), handle);
344 thr_ret = mono_w32handle_lock_handle (handle);
345 g_assert (thr_ret == 0);
347 if (!mono_w32handle_issignalled (handle)) {
348 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: no need to reset %s handle %p",
349 __func__, mono_w32handle_ops_typename (type), handle);
351 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: obtained write lock on %s handle %p",
352 __func__, mono_w32handle_ops_typename (type), handle);
354 mono_w32handle_set_signal_state (handle, FALSE, FALSE);
357 event_handle->set_count = 0;
359 thr_ret = mono_w32handle_unlock_handle (handle);
360 g_assert (thr_ret == 0);
366 ves_icall_System_Threading_Events_CloseEvent_internal (gpointer handle)
368 CloseHandle (handle);
372 ves_icall_System_Threading_Events_OpenEvent_internal (MonoString *name, gint32 rights G_GNUC_UNUSED, gint32 *error)
377 *error = ERROR_SUCCESS;
379 /* w32 seems to guarantee that opening named objects can't race each other */
380 mono_w32handle_namespace_lock ();
382 utf8_name = g_utf16_to_utf8 (mono_string_chars (name), -1, NULL, NULL, NULL);
384 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Opening named event [%s]", __func__, utf8_name);
386 handle = mono_w32handle_namespace_search_handle (MONO_W32HANDLE_NAMEDEVENT, utf8_name);
387 if (handle == INVALID_HANDLE_VALUE) {
388 /* The name has already been used for a different object. */
389 *error = ERROR_INVALID_HANDLE;
391 } else if (!handle) {
392 /* This name doesn't exist */
393 *error = ERROR_FILE_NOT_FOUND;
397 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: returning named event handle %p", __func__, handle);
402 mono_w32handle_namespace_unlock ();
407 MonoW32HandleNamespace*
408 mono_w32event_get_namespace (MonoW32HandleNamedEvent *event)
410 return &event->sharedns;