[io-layer] Make _WAPI_PRIVATE_MAX_SLOTS and _WAPI_HANDLE_INITIAL_COUNT definition...
[mono.git] / mono / io-layer / events.c
index 2d10fd52ba0edc0c277a074412ac2ef02b7d1b5e..b80697d946d46afeb3e835990c0c8ceb6cd0023d 100644 (file)
 #include <mono/io-layer/wapi-private.h>
 #include <mono/io-layer/handles-private.h>
 #include <mono/io-layer/event-private.h>
+#include <mono/io-layer/io-trace.h>
 #include <mono/utils/mono-once.h>
-
-#if 0
-#define DEBUG(...) g_message(__VA_ARGS__)
-#else
-#define DEBUG(...)
-#endif
+#include <mono/utils/mono-logger-internals.h>
 
 static void event_signal(gpointer handle);
 static gboolean event_own (gpointer handle);
@@ -46,36 +42,6 @@ struct _WapiHandleOps _wapi_namedevent_ops = {
        NULL,                   /* is_owned */
 };
 
-static gboolean event_pulse (gpointer handle);
-static gboolean event_reset (gpointer handle);
-static gboolean event_set (gpointer handle);
-
-static gboolean namedevent_pulse (gpointer handle);
-static gboolean namedevent_reset (gpointer handle);
-static gboolean namedevent_set (gpointer handle);
-
-static struct
-{
-       gboolean (*pulse)(gpointer handle);
-       gboolean (*reset)(gpointer handle);
-       gboolean (*set)(gpointer handle);
-} event_ops[WAPI_HANDLE_COUNT] = {
-               {NULL},
-               {NULL},
-               {NULL},
-               {NULL},
-               {NULL},
-               {NULL},
-               {event_pulse, event_reset, event_set},
-               {NULL},
-               {NULL},
-               {NULL},
-               {NULL},
-               {NULL},
-               {NULL},
-               {namedevent_pulse, namedevent_reset, namedevent_set},
-};
-
 void _wapi_event_details (gpointer handle_info)
 {
        struct _WapiHandle_event *event = (struct _WapiHandle_event *)handle_info;
@@ -88,42 +54,55 @@ static mono_once_t event_ops_once=MONO_ONCE_INIT;
 static void event_ops_init (void)
 {
        _wapi_handle_register_capabilities (WAPI_HANDLE_EVENT,
-                                           WAPI_HANDLE_CAP_WAIT |
-                                           WAPI_HANDLE_CAP_SIGNAL);
+               (WapiHandleCapability)(WAPI_HANDLE_CAP_WAIT | WAPI_HANDLE_CAP_SIGNAL));
        _wapi_handle_register_capabilities (WAPI_HANDLE_NAMEDEVENT,
-                                           WAPI_HANDLE_CAP_WAIT |
-                                           WAPI_HANDLE_CAP_SIGNAL);
+               (WapiHandleCapability)(WAPI_HANDLE_CAP_WAIT | WAPI_HANDLE_CAP_SIGNAL));
 }
 
-static void event_signal(gpointer handle)
+static const char* event_handle_type_to_string (WapiHandleType type)
 {
-       SetEvent(handle);
+       switch (type) {
+       case WAPI_HANDLE_EVENT: return "event";
+       case WAPI_HANDLE_NAMEDEVENT: return "named event";
+       default:
+               g_assert_not_reached ();
+       }
 }
 
-static gboolean event_own (gpointer handle)
+static gboolean event_handle_own (gpointer handle, WapiHandleType type)
 {
        struct _WapiHandle_event *event_handle;
        gboolean ok;
-       
-       ok=_wapi_lookup_handle (handle, WAPI_HANDLE_EVENT,
-                               (gpointer *)&event_handle);
-       if(ok==FALSE) {
-               g_warning ("%s: error looking up event handle %p", __func__,
-                          handle);
-               return (FALSE);
+
+       ok = _wapi_lookup_handle (handle, type, (gpointer *)&event_handle);
+       if (!ok) {
+               g_warning ("%s: error looking up %s handle %p",
+                       __func__, event_handle_type_to_string (type), handle);
+               return FALSE;
        }
-       
-       DEBUG("%s: owning event handle %p", __func__, handle);
 
-       if(event_handle->manual==FALSE) {
+       MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: owning %s handle %p",
+               __func__, event_handle_type_to_string (type), handle);
+
+       if (!event_handle->manual) {
                g_assert (event_handle->set_count > 0);
-               
-               if (--event_handle->set_count == 0) {
+               event_handle->set_count --;
+
+               if (event_handle->set_count == 0)
                        _wapi_handle_set_signal_state (handle, FALSE, FALSE);
-               }
        }
 
-       return(TRUE);
+       return TRUE;
+}
+
+static void event_signal(gpointer handle)
+{
+       SetEvent(handle);
+}
+
+static gboolean event_own (gpointer handle)
+{
+       return event_handle_own (handle, WAPI_HANDLE_EVENT);
 }
 
 static void namedevent_signal (gpointer handle)
@@ -134,181 +113,90 @@ static void namedevent_signal (gpointer handle)
 /* NB, always called with the shared handle lock held */
 static gboolean namedevent_own (gpointer handle)
 {
-       struct _WapiHandle_namedevent *namedevent_handle;
-       gboolean ok;
-       
-       DEBUG ("%s: owning named event handle %p", __func__, handle);
-
-       ok = _wapi_lookup_handle (handle, WAPI_HANDLE_NAMEDEVENT,
-                                 (gpointer *)&namedevent_handle);
-       if (ok == FALSE) {
-               g_warning ("%s: error looking up named event handle %p",
-                          __func__, handle);
-               return(FALSE);
-       }
-       
-       if (namedevent_handle->manual == FALSE) {
-               g_assert (namedevent_handle->set_count > 0);
-               
-               if (--namedevent_handle->set_count == 0) {
-                       _wapi_shared_handle_set_signal_state (handle, FALSE);
-               }
-       }
-       
-       return (TRUE);
+       return event_handle_own (handle, WAPI_HANDLE_NAMEDEVENT);
 }
-static gpointer event_create (WapiSecurityAttributes *security G_GNUC_UNUSED,
-                             gboolean manual, gboolean initial)
+
+static gpointer event_handle_create (struct _WapiHandle_event *event_handle, WapiHandleType type, gboolean manual, gboolean initial)
 {
-       struct _WapiHandle_event event_handle = {0};
        gpointer handle;
        int thr_ret;
-       
-       /* Need to blow away any old errors here, because code tests
-        * for ERROR_ALREADY_EXISTS on success (!) to see if an event
-        * was freshly created
-        */
-       SetLastError (ERROR_SUCCESS);
 
-       DEBUG ("%s: Creating unnamed event", __func__);
-       
-       event_handle.manual = manual;
-       event_handle.set_count = 0;
+       event_handle->manual = manual;
+       event_handle->set_count = (initial && !manual) ? 1 : 0;
 
-       if (initial == TRUE) {
-               if (manual == FALSE) {
-                       event_handle.set_count = 1;
-               }
-       }
-       
-       handle = _wapi_handle_new (WAPI_HANDLE_EVENT, &event_handle);
+       handle = _wapi_handle_new (type, event_handle);
        if (handle == _WAPI_HANDLE_INVALID) {
-               g_warning ("%s: error creating event handle", __func__);
+               g_warning ("%s: error creating %s handle",
+                       __func__, event_handle_type_to_string (type));
                SetLastError (ERROR_GEN_FAILURE);
-               return(NULL);
+               return NULL;
        }
 
        thr_ret = _wapi_handle_lock_handle (handle);
        g_assert (thr_ret == 0);
-       
-       if (initial == TRUE) {
+
+       if (initial)
                _wapi_handle_set_signal_state (handle, TRUE, FALSE);
-       }
-       
-       DEBUG("%s: created new event handle %p", __func__, handle);
 
        thr_ret = _wapi_handle_unlock_handle (handle);
        g_assert (thr_ret == 0);
 
-       return(handle);
+       MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: created %s handle %p",
+               __func__, event_handle_type_to_string (type), handle);
+
+       return handle;
+}
+
+static gpointer event_create (gboolean manual, gboolean initial)
+{
+       struct _WapiHandle_event event_handle;
+       MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: creating %s handle",
+               __func__, event_handle_type_to_string (WAPI_HANDLE_EVENT));
+       return event_handle_create (&event_handle, WAPI_HANDLE_EVENT, manual, initial);
 }
 
-static gpointer namedevent_create (WapiSecurityAttributes *security G_GNUC_UNUSED,
-                                  gboolean manual, gboolean initial,
-                                  const gunichar2 *name G_GNUC_UNUSED)
+static gpointer namedevent_create (gboolean manual, gboolean initial, const gunichar2 *name G_GNUC_UNUSED)
 {
-       struct _WapiHandle_namedevent namedevent_handle = {{{0}}, 0};
        gpointer handle;
        gchar *utf8_name;
        int thr_ret;
-       gpointer ret = NULL;
-       guint32 namelen;
-       gint32 offset;
-       
-       /* w32 seems to guarantee that opening named objects can't
-        * race each other
-        */
+
+       MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: creating %s handle",
+               __func__, event_handle_type_to_string (WAPI_HANDLE_NAMEDEVENT));
+
+       /* w32 seems to guarantee that opening named objects can't race each other */
        thr_ret = _wapi_namespace_lock ();
        g_assert (thr_ret == 0);
 
-       /* Need to blow away any old errors here, because code tests
-        * for ERROR_ALREADY_EXISTS on success (!) to see if an event
-        * was freshly created
-        */
-       SetLastError (ERROR_SUCCESS);
-       
        utf8_name = g_utf16_to_utf8 (name, -1, NULL, NULL, NULL);
-       
-       DEBUG ("%s: Creating named event [%s]", __func__, utf8_name);
-       
-       offset = _wapi_search_handle_namespace (WAPI_HANDLE_NAMEDEVENT,
-                                               utf8_name);
-       if (offset == -1) {
-               /* The name has already been used for a different
-                * object.
-                */
+
+       handle = _wapi_search_handle_namespace (WAPI_HANDLE_NAMEDEVENT, utf8_name);
+       if (handle == _WAPI_HANDLE_INVALID) {
+               /* The name has already been used for a different object. */
+               handle = NULL;
                SetLastError (ERROR_INVALID_HANDLE);
-               goto cleanup;
-       } else if (offset != 0) {
-               /* Not an error, but this is how the caller is
-                * informed that the event wasn't freshly created
-                */
+       } else if (handle) {
+               /* Not an error, but this is how the caller is informed that the event wasn't freshly created */
                SetLastError (ERROR_ALREADY_EXISTS);
-       }
-       /* Fall through to create the event handle. */
 
-       if (offset == 0) {
-               /* A new named event, so create both the private and
-                * shared parts
-                */
-       
-               if (strlen (utf8_name) < MAX_PATH) {
-                       namelen = strlen (utf8_name);
-               } else {
-                       namelen = MAX_PATH;
-               }
-       
-               memcpy (&namedevent_handle.sharedns.name, utf8_name, namelen);
-
-               namedevent_handle.manual = manual;
-               namedevent_handle.set_count = 0;
-               
-               if (initial == TRUE) {
-                       if (manual == FALSE) {
-                               namedevent_handle.set_count = 1;
-                       }
-               }
-               
-               handle = _wapi_handle_new (WAPI_HANDLE_NAMEDEVENT,
-                                          &namedevent_handle);
+               /* this is used as creating a new handle */
+               _wapi_handle_ref (handle);
        } else {
-               /* A new reference to an existing named event, so just
-                * create the private part
-                */
-               handle = _wapi_handle_new_from_offset (WAPI_HANDLE_NAMEDEVENT,
-                                                      offset, TRUE);
-       }
-       
-       if (handle == _WAPI_HANDLE_INVALID) {
-               g_warning ("%s: error creating event handle", __func__);
-               SetLastError (ERROR_GEN_FAILURE);
-               goto cleanup;
-       }
-       ret = handle;
+               /* A new named event */
+               struct _WapiHandle_namedevent namedevent_handle;
 
-       if (offset == 0) {
-               /* Set the initial state, as this is a completely new
-                * handle
-                */
-               thr_ret = _wapi_handle_lock_shared_handles ();
-               g_assert (thr_ret == 0);
-       
-               if (initial == TRUE) {
-                       _wapi_shared_handle_set_signal_state (handle, TRUE);
-               }
+               strncpy (&namedevent_handle.sharedns.name [0], utf8_name, MAX_PATH);
+               namedevent_handle.sharedns.name [MAX_PATH] = '\0';
 
-               _wapi_handle_unlock_shared_handles ();
+               handle = event_handle_create ((struct _WapiHandle_event*) &namedevent_handle, WAPI_HANDLE_NAMEDEVENT, manual, initial);
        }
-       
-       DEBUG ("%s: returning event handle %p", __func__, handle);
 
-cleanup:
        g_free (utf8_name);
 
-       _wapi_namespace_unlock (NULL);
-       
-       return(ret);
+       thr_ret = _wapi_namespace_unlock (NULL);
+       g_assert (thr_ret == 0);
 
+       return handle;
 }
 
 
@@ -338,123 +226,13 @@ gpointer CreateEvent(WapiSecurityAttributes *security G_GNUC_UNUSED,
 {
        mono_once (&event_ops_once, event_ops_init);
 
-       if (name == NULL) {
-               return(event_create (security, manual, initial));
-       } else {
-               return(namedevent_create (security, manual, initial, name));
-       }
-}
-
-static gboolean event_pulse (gpointer handle)
-{
-       struct _WapiHandle_event *event_handle;
-       gboolean ok;
-       int thr_ret;
-       
-       ok = _wapi_lookup_handle (handle, WAPI_HANDLE_EVENT,
-                                 (gpointer *)&event_handle);
-       if (ok == FALSE) {
-               g_warning ("%s: error looking up event handle %p", __func__,
-                          handle);
-               return(FALSE);
-       }
-       
-       thr_ret = _wapi_handle_lock_handle (handle);
-       g_assert (thr_ret == 0);
-
-       DEBUG ("%s: Pulsing event handle %p", __func__, handle);
-
-       if (event_handle->manual == TRUE) {
-               _wapi_handle_set_signal_state (handle, TRUE, TRUE);
-       } else {
-               event_handle->set_count = 1;
-               _wapi_handle_set_signal_state (handle, TRUE, FALSE);
-       }
-
-       thr_ret = _wapi_handle_unlock_handle (handle);
-       g_assert (thr_ret == 0);
-       
-       if (event_handle->manual == TRUE) {
-               /* For a manual-reset event, we're about to try and
-                * get the handle lock again, so give other threads a
-                * chance
-                */
-               sched_yield ();
-
-               /* Reset the handle signal state */
-               /* I'm not sure whether or not we need a barrier here
-                * to make sure that all threads waiting on the event
-                * have proceeded.  Currently we rely on broadcasting
-                * a condition.
-                */
-               DEBUG ("%s: Obtained write lock on event handle %p",
-                          __func__, handle);
-
-               thr_ret = _wapi_handle_lock_handle (handle);
-               g_assert (thr_ret == 0);
-               
-               _wapi_handle_set_signal_state (handle, FALSE, FALSE);
-
-               thr_ret = _wapi_handle_unlock_handle (handle);
-               g_assert (thr_ret == 0);
-       }
-
-       return(TRUE);
-}
-
-static gboolean namedevent_pulse (gpointer handle)
-{
-       struct _WapiHandle_namedevent *namedevent_handle;
-       gboolean ok;
-       int thr_ret;
-       
-       ok = _wapi_lookup_handle (handle, WAPI_HANDLE_NAMEDEVENT,
-                                 (gpointer *)&namedevent_handle);
-       if (ok == FALSE) {
-               g_warning ("%s: error looking up named event handle %p",
-                          __func__, handle);
-               return(FALSE);
-       }
-       
-       thr_ret = _wapi_handle_lock_shared_handles ();
-       g_assert (thr_ret == 0);
-
-       DEBUG ("%s: Pulsing named event handle %p", __func__, handle);
-
-       if (namedevent_handle->manual == TRUE) {
-               _wapi_shared_handle_set_signal_state (handle, TRUE);
-       } else {
-               namedevent_handle->set_count = 1;
-               _wapi_shared_handle_set_signal_state (handle, TRUE);
-       }
-
-       _wapi_handle_unlock_shared_handles ();
-       
-       if (namedevent_handle->manual == TRUE) {
-               /* For a manual-reset event, we're about to try and
-                * get the handle lock again, so give other processes
-                * a chance
-                */
-               _wapi_handle_spin (200);
-
-               /* Reset the handle signal state */
-               /* I'm not sure whether or not we need a barrier here
-                * to make sure that all threads waiting on the event
-                * have proceeded.  Currently we rely on waiting for
-                * twice the shared handle poll interval.
-                */
-               DEBUG ("%s: Obtained write lock on event handle %p",
-                          __func__, handle);
-
-               thr_ret = _wapi_handle_lock_shared_handles ();
-               g_assert (thr_ret == 0);
-               
-               _wapi_shared_handle_set_signal_state (handle, FALSE);
-
-               _wapi_handle_unlock_shared_handles ();
-       }
+       /* Need to blow away any old errors here, because code tests
+        * for ERROR_ALREADY_EXISTS on success (!) to see if an event
+        * was freshly created
+        */
+       SetLastError (ERROR_SUCCESS);
 
-       return(TRUE);
+       return name ? namedevent_create (manual, initial, name) : event_create (manual, initial);
 }
 
 /**
@@ -475,93 +253,64 @@ static gboolean namedevent_pulse (gpointer handle)
 gboolean PulseEvent(gpointer handle)
 {
        WapiHandleType type;
-       
+       struct _WapiHandle_event *event_handle;
+       int thr_ret;
+
        if (handle == NULL) {
                SetLastError (ERROR_INVALID_HANDLE);
                return(FALSE);
        }
-       
-       type = _wapi_handle_type (handle);
-       
-       if (event_ops[type].pulse == NULL) {
+
+       switch (type = _wapi_handle_type (handle)) {
+       case WAPI_HANDLE_EVENT:
+       case WAPI_HANDLE_NAMEDEVENT:
+               break;
+       default:
                SetLastError (ERROR_INVALID_HANDLE);
-               return(FALSE);
+               return FALSE;
        }
-       
-       return(event_ops[type].pulse (handle));
-}
 
-static gboolean event_reset (gpointer handle)
-{
-       struct _WapiHandle_event *event_handle;
-       gboolean ok;
-       int thr_ret;
-       
-       ok = _wapi_lookup_handle (handle, WAPI_HANDLE_EVENT,
-                                 (gpointer *)&event_handle);
-       if (ok == FALSE) {
-               g_warning ("%s: error looking up event handle %p",
-                          __func__, handle);
-               return(FALSE);
+       if (!_wapi_lookup_handle (handle, type, (gpointer *)&event_handle)) {
+               g_warning ("%s: error looking up %s handle %p",
+                       __func__, event_handle_type_to_string (type), handle);
+               return FALSE;
        }
 
-       DEBUG ("%s: Resetting event handle %p", __func__, handle);
+       MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: pulsing %s handle %p",
+               __func__, event_handle_type_to_string (type), handle);
 
        thr_ret = _wapi_handle_lock_handle (handle);
        g_assert (thr_ret == 0);
-       
-       if (_wapi_handle_issignalled (handle) == FALSE) {
-               DEBUG ("%s: No need to reset event handle %p", __func__,
-                          handle);
+
+       if (!event_handle->manual) {
+               event_handle->set_count = 1;
+               _wapi_handle_set_signal_state (handle, TRUE, FALSE);
        } else {
-               DEBUG ("%s: Obtained write lock on event handle %p",
-                          __func__, handle);
+               _wapi_handle_set_signal_state (handle, TRUE, TRUE);
 
-               _wapi_handle_set_signal_state (handle, FALSE, FALSE);
-       }
-       
-       event_handle->set_count = 0;
-       
-       thr_ret = _wapi_handle_unlock_handle (handle);
-       g_assert (thr_ret == 0);
-       
-       return(TRUE);
-}
+               thr_ret = _wapi_handle_unlock_handle (handle);
+               g_assert (thr_ret == 0);
 
-static gboolean namedevent_reset (gpointer handle)
-{
-       struct _WapiHandle_namedevent *namedevent_handle;
-       gboolean ok;
-       int thr_ret;
-       
-       ok = _wapi_lookup_handle (handle, WAPI_HANDLE_NAMEDEVENT,
-                                 (gpointer *)&namedevent_handle);
-       if (ok == FALSE) {
-               g_warning ("%s: error looking up named event handle %p",
-                          __func__, handle);
-               return(FALSE);
-       }
+               /* For a manual-reset event, we're about to try and get the handle
+                * lock again, so give other threads a chance */
+               sched_yield ();
 
-       DEBUG ("%s: Resetting named event handle %p", __func__, handle);
+               /* Reset the handle signal state */
 
-       thr_ret = _wapi_handle_lock_shared_handles ();
-       g_assert (thr_ret == 0);
-       
-       if (_wapi_handle_issignalled (handle) == FALSE) {
-               DEBUG ("%s: No need to reset named event handle %p",
-                          __func__, handle);
-       } else {
-               DEBUG ("%s: Obtained write lock on named event handle %p",
-                          __func__, handle);
+               /* I'm not sure whether or not we need a barrier here to make sure
+                * that all threads waiting on the event have proceeded. Currently
+                * we rely on broadcasting a condition. */
+
+               thr_ret = _wapi_handle_lock_handle (handle);
+               g_assert (thr_ret == 0);
 
-               _wapi_shared_handle_set_signal_state (handle, FALSE);
+               _wapi_handle_set_signal_state (handle, FALSE, FALSE);
        }
-       
-       namedevent_handle->set_count = 0;
-       
-       _wapi_handle_unlock_shared_handles ();
-       
-       return(TRUE);
+
+       thr_ret = _wapi_handle_unlock_handle (handle);
+       g_assert (thr_ret == 0);
+
+       return TRUE;
 }
 
 /**
@@ -576,83 +325,53 @@ static gboolean namedevent_reset (gpointer handle)
 gboolean ResetEvent(gpointer handle)
 {
        WapiHandleType type;
+       struct _WapiHandle_event *event_handle;
+       int thr_ret;
+
+       SetLastError (ERROR_SUCCESS);
        
        if (handle == NULL) {
                SetLastError (ERROR_INVALID_HANDLE);
                return(FALSE);
        }
        
-       type = _wapi_handle_type (handle);
-       
-       if (event_ops[type].reset == NULL) {
+       switch (type = _wapi_handle_type (handle)) {
+       case WAPI_HANDLE_EVENT:
+       case WAPI_HANDLE_NAMEDEVENT:
+               break;
+       default:
                SetLastError (ERROR_INVALID_HANDLE);
-               return(FALSE);
+               return FALSE;
        }
-       
-       return(event_ops[type].reset (handle));
-}
 
-static gboolean event_set (gpointer handle)
-{
-       struct _WapiHandle_event *event_handle;
-       gboolean ok;
-       int thr_ret;
-       
-       ok = _wapi_lookup_handle (handle, WAPI_HANDLE_EVENT,
-                                 (gpointer *)&event_handle);
-       if (ok == FALSE) {
-               g_warning ("%s: error looking up event handle %p", __func__,
-                          handle);
-               return(FALSE);
+       if (!_wapi_lookup_handle (handle, type, (gpointer *)&event_handle)) {
+               g_warning ("%s: error looking up %s handle %p",
+                       __func__, event_handle_type_to_string (type), handle);
+               return FALSE;
        }
-       
+
+       MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: resetting %s handle %p",
+               __func__, event_handle_type_to_string (type), handle);
+
        thr_ret = _wapi_handle_lock_handle (handle);
        g_assert (thr_ret == 0);
 
-       DEBUG ("%s: Setting event handle %p", __func__, handle);
-
-       if (event_handle->manual == TRUE) {
-               _wapi_handle_set_signal_state (handle, TRUE, TRUE);
+       if (!_wapi_handle_issignalled (handle)) {
+               MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: no need to reset %s handle %p",
+                       __func__, event_handle_type_to_string (type), handle);
        } else {
-               event_handle->set_count = 1;
-               _wapi_handle_set_signal_state (handle, TRUE, FALSE);
-       }
-
-       thr_ret = _wapi_handle_unlock_handle (handle);
-       g_assert (thr_ret == 0);
-       
-       return(TRUE);
-}
+               MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: obtained write lock on %s handle %p",
+                       __func__, event_handle_type_to_string (type), handle);
 
-static gboolean namedevent_set (gpointer handle)
-{
-       struct _WapiHandle_namedevent *namedevent_handle;
-       gboolean ok;
-       int thr_ret;
-       
-       ok = _wapi_lookup_handle (handle, WAPI_HANDLE_NAMEDEVENT,
-                                 (gpointer *)&namedevent_handle);
-       if (ok == FALSE) {
-               g_warning ("%s: error looking up named event handle %p",
-                          __func__, handle);
-               return(FALSE);
+               _wapi_handle_set_signal_state (handle, FALSE, FALSE);
        }
-       
-       thr_ret = _wapi_handle_lock_shared_handles ();
-       g_assert (thr_ret == 0);
 
-       DEBUG ("%s: Setting named event handle %p", __func__, handle);
-
-       if (namedevent_handle->manual == TRUE) {
-               _wapi_shared_handle_set_signal_state (handle, TRUE);
-       } else {
-               namedevent_handle->set_count = 1;
-               _wapi_shared_handle_set_signal_state (handle, TRUE);
-       }
+       event_handle->set_count = 0;
 
-       _wapi_handle_unlock_shared_handles ();
+       thr_ret = _wapi_handle_unlock_handle (handle);
+       g_assert (thr_ret == 0);
 
-       return(TRUE);
+       return TRUE;
 }
 
 /**
@@ -672,20 +391,46 @@ static gboolean namedevent_set (gpointer handle)
 gboolean SetEvent(gpointer handle)
 {
        WapiHandleType type;
+       struct _WapiHandle_event *event_handle;
+       int thr_ret;
        
        if (handle == NULL) {
                SetLastError (ERROR_INVALID_HANDLE);
                return(FALSE);
        }
        
-       type = _wapi_handle_type (handle);
-       
-       if (event_ops[type].set == NULL) {
+       switch (type = _wapi_handle_type (handle)) {
+       case WAPI_HANDLE_EVENT:
+       case WAPI_HANDLE_NAMEDEVENT:
+               break;
+       default:
                SetLastError (ERROR_INVALID_HANDLE);
-               return(FALSE);
+               return FALSE;
        }
-       
-       return(event_ops[type].set (handle));
+
+       if (!_wapi_lookup_handle (handle, type, (gpointer *)&event_handle)) {
+               g_warning ("%s: error looking up %s handle %p",
+                       __func__, event_handle_type_to_string (type), handle);
+               return FALSE;
+       }
+
+       MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: setting %s handle %p",
+               __func__, event_handle_type_to_string (type), handle);
+
+       thr_ret = _wapi_handle_lock_handle (handle);
+       g_assert (thr_ret == 0);
+
+       if (!event_handle->manual) {
+               event_handle->set_count = 1;
+               _wapi_handle_set_signal_state (handle, TRUE, FALSE);
+       } else {
+               _wapi_handle_set_signal_state (handle, TRUE, TRUE);
+       }
+
+       thr_ret = _wapi_handle_unlock_handle (handle);
+       g_assert (thr_ret == 0);
+
+       return TRUE;
 }
 
 gpointer OpenEvent (guint32 access G_GNUC_UNUSED, gboolean inherit G_GNUC_UNUSED, const gunichar2 *name)
@@ -693,8 +438,6 @@ gpointer OpenEvent (guint32 access G_GNUC_UNUSED, gboolean inherit G_GNUC_UNUSED
        gpointer handle;
        gchar *utf8_name;
        int thr_ret;
-       gpointer ret = NULL;
-       gint32 offset;
        
        mono_once (&event_ops_once, event_ops_init);
 
@@ -706,42 +449,29 @@ gpointer OpenEvent (guint32 access G_GNUC_UNUSED, gboolean inherit G_GNUC_UNUSED
 
        utf8_name = g_utf16_to_utf8 (name, -1, NULL, NULL, NULL);
        
-       DEBUG ("%s: Opening named event [%s]", __func__, utf8_name);
+       MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Opening named event [%s]", __func__, utf8_name);
        
-       offset = _wapi_search_handle_namespace (WAPI_HANDLE_NAMEDEVENT,
+       handle = _wapi_search_handle_namespace (WAPI_HANDLE_NAMEDEVENT,
                                                utf8_name);
-       if (offset == -1) {
+       if (handle == _WAPI_HANDLE_INVALID) {
                /* The name has already been used for a different
                 * object.
                 */
                SetLastError (ERROR_INVALID_HANDLE);
                goto cleanup;
-       } else if (offset == 0) {
+       } else if (!handle) {
                /* This name doesn't exist */
                SetLastError (ERROR_FILE_NOT_FOUND);    /* yes, really */
                goto cleanup;
        }
 
-       /* A new reference to an existing named event, so just create
-        * the private part
-        */
-       handle = _wapi_handle_new_from_offset (WAPI_HANDLE_NAMEDEVENT, offset,
-                                              TRUE);
-       
-       if (handle == _WAPI_HANDLE_INVALID) {
-               g_warning ("%s: error opening named event handle", __func__);
-               SetLastError (ERROR_GEN_FAILURE);
-               goto cleanup;
-       }
-       ret = handle;
-
-       DEBUG ("%s: returning named event handle %p", __func__, handle);
+       MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: returning named event handle %p", __func__, handle);
 
 cleanup:
        g_free (utf8_name);
 
        _wapi_namespace_unlock (NULL);
        
-       return(ret);
+       return handle;
 
 }