Merge pull request #2274 from esdrubal/udpclientreceive
[mono.git] / mono / io-layer / semaphores.c
index 73fda73552db2218fbe39388cf08279f08fe85d1..01179f4d18244f0e3469aeecc5d6a1a0a90de149 100644 (file)
 
 #include <mono/io-layer/wapi.h>
 #include <mono/io-layer/wapi-private.h>
-#include <mono/io-layer/misc-private.h>
 #include <mono/io-layer/handles-private.h>
-#include <mono/io-layer/mono-mutex.h>
 #include <mono/io-layer/semaphore-private.h>
+#include <mono/utils/mono-once.h>
 
-#undef DEBUG
+#if 0
+#define DEBUG(...) g_message(__VA_ARGS__)
+#else
+#define DEBUG(...)
+#endif
 
 static void sema_signal(gpointer handle);
 static gboolean sema_own (gpointer handle);
@@ -84,11 +87,9 @@ static mono_once_t sem_ops_once=MONO_ONCE_INIT;
 static void sem_ops_init (void)
 {
        _wapi_handle_register_capabilities (WAPI_HANDLE_SEM,
-                                           WAPI_HANDLE_CAP_WAIT |
-                                           WAPI_HANDLE_CAP_SIGNAL);
+               (WapiHandleCapability)(WAPI_HANDLE_CAP_WAIT | WAPI_HANDLE_CAP_SIGNAL));
        _wapi_handle_register_capabilities (WAPI_HANDLE_NAMEDSEM,
-                                           WAPI_HANDLE_CAP_WAIT |
-                                           WAPI_HANDLE_CAP_SIGNAL);
+               (WapiHandleCapability)(WAPI_HANDLE_CAP_WAIT | WAPI_HANDLE_CAP_SIGNAL));
 }
 
 static void sema_signal(gpointer handle)
@@ -109,15 +110,11 @@ static gboolean sema_own (gpointer handle)
                return(FALSE);
        }
        
-#ifdef DEBUG
-       g_message("%s: owning sem handle %p", __func__, handle);
-#endif
+       DEBUG("%s: owning sem handle %p", __func__, handle);
 
        sem_handle->val--;
        
-#ifdef DEBUG
-       g_message ("%s: sem %p val now %d", __func__, handle, sem_handle->val);
-#endif
+       DEBUG ("%s: sem %p val now %d", __func__, handle, sem_handle->val);
 
        if(sem_handle->val==0) {
                _wapi_handle_set_signal_state (handle, FALSE, FALSE);
@@ -137,9 +134,7 @@ static gboolean namedsema_own (gpointer handle)
        struct _WapiHandle_namedsem *namedsem_handle;
        gboolean ok;
        
-#ifdef DEBUG
-       g_message ("%s: owning named sem handle %p", __func__, handle);
-#endif
+       DEBUG ("%s: owning named sem handle %p", __func__, handle);
 
        ok = _wapi_lookup_handle (handle, WAPI_HANDLE_NAMEDSEM,
                                  (gpointer *)&namedsem_handle);
@@ -151,10 +146,8 @@ static gboolean namedsema_own (gpointer handle)
        
        namedsem_handle->val--;
        
-#ifdef DEBUG
-       g_message ("%s: named sem %p val now %d", __func__, handle,
+       DEBUG ("%s: named sem %p val now %d", __func__, handle,
                   namedsem_handle->val);
-#endif
 
        if (namedsem_handle->val == 0) {
                _wapi_shared_handle_set_signal_state (handle, FALSE);
@@ -185,8 +178,6 @@ static gpointer sem_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);
        
@@ -194,14 +185,11 @@ static gpointer sem_create (WapiSecurityAttributes *security G_GNUC_UNUSED,
                _wapi_handle_set_signal_state (handle, TRUE, FALSE);
        }
 
-#ifdef DEBUG
-       g_message ("%s: Created semaphore handle %p initial %d max %d",
+       DEBUG ("%s: Created semaphore handle %p initial %d max %d",
                   __func__, handle, initial, max);
-#endif
 
        thr_ret = _wapi_handle_unlock_handle (handle);
        g_assert (thr_ret == 0);
-       pthread_cleanup_pop (0);
 
        return(handle);
 }
@@ -230,9 +218,7 @@ static gpointer namedsem_create (WapiSecurityAttributes *security G_GNUC_UNUSED,
 
        utf8_name = g_utf16_to_utf8 (name, -1, NULL, NULL, NULL);
        
-#ifdef DEBUG
-       g_message ("%s: Creating named sem [%s]", __func__, utf8_name);
-#endif
+       DEBUG ("%s: Creating named sem [%s]", __func__, utf8_name);
 
        offset = _wapi_search_handle_namespace (WAPI_HANDLE_NAMEDSEM,
                                                utf8_name);
@@ -296,9 +282,7 @@ static gpointer namedsem_create (WapiSecurityAttributes *security G_GNUC_UNUSED,
                _wapi_handle_unlock_shared_handles ();
        }
        
-#ifdef DEBUG
-       g_message ("%s: returning named sem handle %p", __func__, handle);
-#endif
+       DEBUG ("%s: returning named sem handle %p", __func__, handle);
 
 cleanup:
        g_free (utf8_name);
@@ -332,18 +316,14 @@ gpointer CreateSemaphore(WapiSecurityAttributes *security G_GNUC_UNUSED, gint32
        mono_once (&sem_ops_once, sem_ops_init);
        
        if (max <= 0) {
-#ifdef DEBUG
-               g_message ("%s: max <= 0", __func__);
-#endif
+               DEBUG ("%s: max <= 0", __func__);
 
                SetLastError (ERROR_INVALID_PARAMETER);
                return(NULL);
        }
        
        if (initial > max || initial < 0) {
-#ifdef DEBUG
-               g_message ("%s: initial>max or < 0", __func__);
-#endif
+               DEBUG ("%s: initial>max or < 0", __func__);
 
                SetLastError (ERROR_INVALID_PARAMETER);
                return(NULL);
@@ -371,15 +351,11 @@ static gboolean sem_release (gpointer handle, gint32 count, gint32 *prevcount)
                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: sem %p val %d count %d", __func__, handle,
+       DEBUG ("%s: sem %p val %d count %d", __func__, handle,
                   sem_handle->val, count);
-#endif
        
        /* Do this before checking for count overflow, because overflowing max
         * is a listed technique for finding the current value
@@ -390,9 +366,7 @@ static gboolean sem_release (gpointer handle, gint32 count, gint32 *prevcount)
        
        /* No idea why max is signed, but thats the spec :-( */
        if (sem_handle->val + count > (guint32)sem_handle->max) {
-#ifdef DEBUG
-               g_message ("%s: sem %p max value would be exceeded: max %d current %d count %d", __func__, handle, sem_handle->max, sem_handle->val, count);
-#endif
+               DEBUG ("%s: sem %p max value would be exceeded: max %d current %d count %d", __func__, handle, sem_handle->max, sem_handle->val, count);
 
                goto end;
        }
@@ -402,14 +376,11 @@ static gboolean sem_release (gpointer handle, gint32 count, gint32 *prevcount)
        
        ret = TRUE;
 
-#ifdef DEBUG
-       g_message ("%s: sem %p val now %d", __func__, handle, sem_handle->val);
-#endif
+       DEBUG ("%s: sem %p val now %d", __func__, handle, sem_handle->val);
        
 end:
        thr_ret = _wapi_handle_unlock_handle (handle);
        g_assert (thr_ret == 0);
-       pthread_cleanup_pop (0);
 
        return(ret);
 }
@@ -433,10 +404,8 @@ static gboolean namedsem_release (gpointer handle, gint32 count,
        thr_ret = _wapi_handle_lock_shared_handles ();
        g_assert (thr_ret == 0);
 
-#ifdef DEBUG
-       g_message("%s: named sem %p val %d count %d", __func__, handle,
+       DEBUG("%s: named sem %p val %d count %d", __func__, handle,
                  sem_handle->val, count);
-#endif
        
        /* Do this before checking for count overflow, because overflowing max
         * is a listed technique for finding the current value
@@ -447,9 +416,7 @@ static gboolean namedsem_release (gpointer handle, gint32 count,
        
        /* No idea why max is signed, but thats the spec :-( */
        if (sem_handle->val + count > (guint32)sem_handle->max) {
-#ifdef DEBUG
-               g_message ("%s: named sem %p max value would be exceeded: max %d current %d count %d", __func__, handle, sem_handle->max, sem_handle->val, count);
-#endif
+               DEBUG ("%s: named sem %p max value would be exceeded: max %d current %d count %d", __func__, handle, sem_handle->max, sem_handle->val, count);
 
                goto end;
        }
@@ -459,10 +426,8 @@ static gboolean namedsem_release (gpointer handle, gint32 count,
        
        ret = TRUE;
 
-#ifdef DEBUG
-       g_message("%s: named sem %p val now %d", __func__, handle,
+       DEBUG("%s: named sem %p val now %d", __func__, handle,
                  sem_handle->val);
-#endif
        
 end:
        _wapi_handle_unlock_shared_handles ();
@@ -520,9 +485,7 @@ gpointer OpenSemaphore (guint32 access G_GNUC_UNUSED, gboolean inherit G_GNUC_UN
        
        utf8_name = g_utf16_to_utf8 (name, -1, NULL, NULL, NULL);
        
-#ifdef DEBUG
-       g_message ("%s: Opening named sem [%s]", __func__, utf8_name);
-#endif
+       DEBUG ("%s: Opening named sem [%s]", __func__, utf8_name);
 
        offset = _wapi_search_handle_namespace (WAPI_HANDLE_NAMEDSEM,
                                                utf8_name);
@@ -551,9 +514,7 @@ gpointer OpenSemaphore (guint32 access G_GNUC_UNUSED, gboolean inherit G_GNUC_UN
        }
        ret = handle;
        
-#ifdef DEBUG
-       g_message ("%s: returning named sem handle %p", __func__, handle);
-#endif
+       DEBUG ("%s: returning named sem handle %p", __func__, handle);
 
 cleanup:
        g_free (utf8_name);