Merge pull request #2274 from esdrubal/udpclientreceive
[mono.git] / mono / io-layer / events.c
index 40687cbcd4eeb062f299679319ceeb3905f9379c..3ad606bcebc95ebdf277e88344cc714b93237138 100644 (file)
 #include <mono/io-layer/wapi.h>
 #include <mono/io-layer/wapi-private.h>
 #include <mono/io-layer/handles-private.h>
-#include <mono/io-layer/misc-private.h>
-
-#include <mono/io-layer/mono-mutex.h>
-
 #include <mono/io-layer/event-private.h>
+#include <mono/utils/mono-once.h>
 
-#undef DEBUG
+#if 0
+#define DEBUG(...) g_message(__VA_ARGS__)
+#else
+#define DEBUG(...)
+#endif
 
 static void event_signal(gpointer handle);
 static gboolean event_own (gpointer handle);
@@ -87,16 +88,14 @@ 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)
 {
-       ResetEvent(handle);
+       SetEvent(handle);
 }
 
 static gboolean event_own (gpointer handle)
@@ -112,9 +111,7 @@ static gboolean event_own (gpointer handle)
                return (FALSE);
        }
        
-#ifdef DEBUG
-       g_message("%s: owning event handle %p", __func__, handle);
-#endif
+       DEBUG("%s: owning event handle %p", __func__, handle);
 
        if(event_handle->manual==FALSE) {
                g_assert (event_handle->set_count > 0);
@@ -129,7 +126,7 @@ static gboolean event_own (gpointer handle)
 
 static void namedevent_signal (gpointer handle)
 {
-       ResetEvent (handle);
+       SetEvent (handle);
 }
 
 /* NB, always called with the shared handle lock held */
@@ -138,9 +135,7 @@ static gboolean namedevent_own (gpointer handle)
        struct _WapiHandle_namedevent *namedevent_handle;
        gboolean ok;
        
-#ifdef DEBUG
-       g_message ("%s: owning named event handle %p", __func__, handle);
-#endif
+       DEBUG ("%s: owning named event handle %p", __func__, handle);
 
        ok = _wapi_lookup_handle (handle, WAPI_HANDLE_NAMEDEVENT,
                                  (gpointer *)&namedevent_handle);
@@ -173,9 +168,7 @@ static gpointer event_create (WapiSecurityAttributes *security G_GNUC_UNUSED,
         */
        SetLastError (ERROR_SUCCESS);
 
-#ifdef DEBUG
-       g_message ("%s: Creating unnamed event", __func__);
-#endif
+       DEBUG ("%s: Creating unnamed event", __func__);
        
        event_handle.manual = manual;
        event_handle.set_count = 0;
@@ -193,8 +186,6 @@ static gpointer event_create (WapiSecurityAttributes *security G_GNUC_UNUSED,
                return(NULL);
        }
 
-       pthread_cleanup_push ((void(*)(void *))_wapi_handle_unlock_handle,
-                             handle);
        thr_ret = _wapi_handle_lock_handle (handle);
        g_assert (thr_ret == 0);
        
@@ -202,13 +193,10 @@ static gpointer event_create (WapiSecurityAttributes *security G_GNUC_UNUSED,
                _wapi_handle_set_signal_state (handle, TRUE, FALSE);
        }
        
-#ifdef DEBUG
-       g_message("%s: created new event handle %p", __func__, handle);
-#endif
+       DEBUG("%s: created new event handle %p", __func__, handle);
 
        thr_ret = _wapi_handle_unlock_handle (handle);
        g_assert (thr_ret == 0);
-       pthread_cleanup_pop (0);
 
        return(handle);
 }
@@ -239,9 +227,7 @@ static gpointer namedevent_create (WapiSecurityAttributes *security G_GNUC_UNUSE
        
        utf8_name = g_utf16_to_utf8 (name, -1, NULL, NULL, NULL);
        
-#ifdef DEBUG
-       g_message ("%s: Creating named event [%s]", __func__, utf8_name);
-#endif
+       DEBUG ("%s: Creating named event [%s]", __func__, utf8_name);
        
        offset = _wapi_search_handle_namespace (WAPI_HANDLE_NAMEDEVENT,
                                                utf8_name);
@@ -312,9 +298,7 @@ static gpointer namedevent_create (WapiSecurityAttributes *security G_GNUC_UNUSE
                _wapi_handle_unlock_shared_handles ();
        }
        
-#ifdef DEBUG
-       g_message ("%s: returning event handle %p", __func__, handle);
-#endif
+       DEBUG ("%s: returning event handle %p", __func__, handle);
 
 cleanup:
        g_free (utf8_name);
@@ -373,14 +357,10 @@ static gboolean event_pulse (gpointer handle)
                return(FALSE);
        }
        
-       pthread_cleanup_push ((void(*)(void *))_wapi_handle_unlock_handle,
-                             handle);
        thr_ret = _wapi_handle_lock_handle (handle);
        g_assert (thr_ret == 0);
 
-#ifdef DEBUG
-       g_message ("%s: Pulsing event handle %p", __func__, handle);
-#endif
+       DEBUG ("%s: Pulsing event handle %p", __func__, handle);
 
        if (event_handle->manual == TRUE) {
                _wapi_handle_set_signal_state (handle, TRUE, TRUE);
@@ -392,8 +372,6 @@ static gboolean event_pulse (gpointer handle)
        thr_ret = _wapi_handle_unlock_handle (handle);
        g_assert (thr_ret == 0);
        
-       pthread_cleanup_pop (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
@@ -407,12 +385,9 @@ static gboolean event_pulse (gpointer handle)
                 * have proceeded.  Currently we rely on broadcasting
                 * a condition.
                 */
-#ifdef DEBUG
-               g_message ("%s: Obtained write lock on event handle %p",
+               DEBUG ("%s: Obtained write lock on event handle %p",
                           __func__, handle);
-#endif
 
-               pthread_cleanup_push ((void(*)(void *))_wapi_handle_unlock_handle, handle);
                thr_ret = _wapi_handle_lock_handle (handle);
                g_assert (thr_ret == 0);
                
@@ -420,7 +395,6 @@ static gboolean event_pulse (gpointer handle)
 
                thr_ret = _wapi_handle_unlock_handle (handle);
                g_assert (thr_ret == 0);
-               pthread_cleanup_pop (0);
        }
 
        return(TRUE);
@@ -443,9 +417,7 @@ static gboolean namedevent_pulse (gpointer handle)
        thr_ret = _wapi_handle_lock_shared_handles ();
        g_assert (thr_ret == 0);
 
-#ifdef DEBUG
-       g_message ("%s: Pulsing named event handle %p", __func__, handle);
-#endif
+       DEBUG ("%s: Pulsing named event handle %p", __func__, handle);
 
        if (namedevent_handle->manual == TRUE) {
                _wapi_shared_handle_set_signal_state (handle, TRUE);
@@ -469,10 +441,8 @@ static gboolean namedevent_pulse (gpointer handle)
                 * have proceeded.  Currently we rely on waiting for
                 * twice the shared handle poll interval.
                 */
-#ifdef DEBUG
-               g_message ("%s: Obtained write lock on event handle %p",
+               DEBUG ("%s: Obtained write lock on event handle %p",
                           __func__, handle);
-#endif
 
                thr_ret = _wapi_handle_lock_shared_handles ();
                g_assert (thr_ret == 0);
@@ -533,25 +503,17 @@ static gboolean event_reset (gpointer handle)
                return(FALSE);
        }
 
-#ifdef DEBUG
-       g_message ("%s: Resetting event handle %p", __func__, handle);
-#endif
+       DEBUG ("%s: Resetting event handle %p", __func__, handle);
 
-       pthread_cleanup_push ((void(*)(void *))_wapi_handle_unlock_handle,
-                             handle);
        thr_ret = _wapi_handle_lock_handle (handle);
        g_assert (thr_ret == 0);
        
        if (_wapi_handle_issignalled (handle) == FALSE) {
-#ifdef DEBUG
-               g_message ("%s: No need to reset event handle %p", __func__,
+               DEBUG ("%s: No need to reset event handle %p", __func__,
                           handle);
-#endif
        } else {
-#ifdef DEBUG
-               g_message ("%s: Obtained write lock on event handle %p",
+               DEBUG ("%s: Obtained write lock on event handle %p",
                           __func__, handle);
-#endif
 
                _wapi_handle_set_signal_state (handle, FALSE, FALSE);
        }
@@ -561,8 +523,6 @@ static gboolean event_reset (gpointer handle)
        thr_ret = _wapi_handle_unlock_handle (handle);
        g_assert (thr_ret == 0);
        
-       pthread_cleanup_pop (0);
-       
        return(TRUE);
 }
 
@@ -580,23 +540,17 @@ static gboolean namedevent_reset (gpointer handle)
                return(FALSE);
        }
 
-#ifdef DEBUG
-       g_message ("%s: Resetting named event handle %p", __func__, handle);
-#endif
+       DEBUG ("%s: Resetting named event handle %p", __func__, handle);
 
        thr_ret = _wapi_handle_lock_shared_handles ();
        g_assert (thr_ret == 0);
        
        if (_wapi_handle_issignalled (handle) == FALSE) {
-#ifdef DEBUG
-               g_message ("%s: No need to reset named event handle %p",
+               DEBUG ("%s: No need to reset named event handle %p",
                           __func__, handle);
-#endif
        } else {
-#ifdef DEBUG
-               g_message ("%s: Obtained write lock on named event handle %p",
+               DEBUG ("%s: Obtained write lock on named event handle %p",
                           __func__, handle);
-#endif
 
                _wapi_shared_handle_set_signal_state (handle, FALSE);
        }
@@ -650,14 +604,10 @@ static gboolean event_set (gpointer handle)
                return(FALSE);
        }
        
-       pthread_cleanup_push ((void(*)(void *))_wapi_handle_unlock_handle,
-                             handle);
        thr_ret = _wapi_handle_lock_handle (handle);
        g_assert (thr_ret == 0);
 
-#ifdef DEBUG
-       g_message ("%s: Setting event handle %p", __func__, handle);
-#endif
+       DEBUG ("%s: Setting event handle %p", __func__, handle);
 
        if (event_handle->manual == TRUE) {
                _wapi_handle_set_signal_state (handle, TRUE, TRUE);
@@ -669,8 +619,6 @@ static gboolean event_set (gpointer handle)
        thr_ret = _wapi_handle_unlock_handle (handle);
        g_assert (thr_ret == 0);
        
-       pthread_cleanup_pop (0);
-
        return(TRUE);
 }
 
@@ -691,9 +639,7 @@ static gboolean namedevent_set (gpointer handle)
        thr_ret = _wapi_handle_lock_shared_handles ();
        g_assert (thr_ret == 0);
 
-#ifdef DEBUG
-       g_message ("%s: Setting named event handle %p", __func__, handle);
-#endif
+       DEBUG ("%s: Setting named event handle %p", __func__, handle);
 
        if (namedevent_handle->manual == TRUE) {
                _wapi_shared_handle_set_signal_state (handle, TRUE);
@@ -758,9 +704,7 @@ gpointer OpenEvent (guint32 access G_GNUC_UNUSED, gboolean inherit G_GNUC_UNUSED
 
        utf8_name = g_utf16_to_utf8 (name, -1, NULL, NULL, NULL);
        
-#ifdef DEBUG
-       g_message ("%s: Opening named event [%s]", __func__, utf8_name);
-#endif
+       DEBUG ("%s: Opening named event [%s]", __func__, utf8_name);
        
        offset = _wapi_search_handle_namespace (WAPI_HANDLE_NAMEDEVENT,
                                                utf8_name);
@@ -789,9 +733,7 @@ gpointer OpenEvent (guint32 access G_GNUC_UNUSED, gboolean inherit G_GNUC_UNUSED
        }
        ret = handle;
 
-#ifdef DEBUG
-       g_message ("%s: returning named event handle %p", __func__, handle);
-#endif
+       DEBUG ("%s: returning named event handle %p", __func__, handle);
 
 cleanup:
        g_free (utf8_name);