Merge pull request #725 from knocte/threadpool_init
[mono.git] / mono / io-layer / semaphores.c
index 19b388f43cf2df8c644fb7b3d2cd467cc45a00c0..229e313070fafd1dca012819feedce3f8864e04a 100644 (file)
 #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>
 
-#undef DEBUG
+#include <mono/utils/mono-mutex.h>
+
+#if 0
+#define DEBUG(...) g_message(__VA_ARGS__)
+#else
+#define DEBUG(...)
+#endif
 
 static void sema_signal(gpointer handle);
 static gboolean sema_own (gpointer handle);
@@ -37,6 +42,8 @@ struct _WapiHandleOps _wapi_sem_ops = {
        sema_signal,            /* signal */
        sema_own,               /* own */
        NULL,                   /* is_owned */
+       NULL,                   /* special_wait */
+       NULL                    /* prewait */
 };
 
 void _wapi_sem_details (gpointer handle_info)
@@ -51,6 +58,8 @@ struct _WapiHandleOps _wapi_namedsem_ops = {
        namedsema_signal,       /* signal */
        namedsema_own,          /* own */
        NULL,                   /* is_owned */
+       NULL,                   /* special_wait */
+       NULL                    /* prewait */
 };
 
 static gboolean sem_release (gpointer handle, gint32 count, gint32 *prev);
@@ -105,15 +114,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);
@@ -133,9 +138,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);
@@ -147,10 +150,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);
@@ -190,10 +191,8 @@ 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);
@@ -226,9 +225,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);
@@ -292,9 +289,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);
@@ -328,18 +323,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);
@@ -372,10 +363,8 @@ static gboolean sem_release (gpointer handle, gint32 count, gint32 *prevcount)
        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
@@ -386,9 +375,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;
        }
@@ -398,9 +385,7 @@ 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);
@@ -429,10 +414,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
@@ -443,9 +426,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;
        }
@@ -455,10 +436,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 ();
@@ -516,9 +495,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);
@@ -547,9 +524,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);