Fix arm thunk creation in dynamic methods. Fixes #673828.
[mono.git] / mono / io-layer / semaphores.c
index 9a213e1c48d4863a4dc63c2e5a1ca143619ac492..73fda73552db2218fbe39388cf08279f08fe85d1 100644 (file)
+/*
+ * semaphores.c:  Semaphore handles
+ *
+ * Author:
+ *     Dick Porter (dick@ximian.com)
+ *
+ * (C) 2002 Ximian, Inc.
+ */
+
 #include <config.h>
 #include <glib.h>
 #include <pthread.h>
+#ifdef HAVE_SEMAPHORE_H
 #include <semaphore.h>
+#endif
 #include <errno.h>
 #include <string.h>
 #include <sys/time.h>
 
-#include "mono/io-layer/wapi.h"
-#include "wapi-private.h"
-#include "wait-private.h"
-#include "misc-private.h"
-#include "handles-private.h"
-
-#include "mono-mutex.h"
+#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>
 
 #undef DEBUG
 
-/* emulate sem_t, so that we can prod the internal state more easily */
-struct _WapiHandle_sem
+static void sema_signal(gpointer handle);
+static gboolean sema_own (gpointer handle);
+
+static void namedsema_signal (gpointer handle);
+static gboolean namedsema_own (gpointer handle);
+
+struct _WapiHandleOps _wapi_sem_ops = {
+       NULL,                   /* close */
+       sema_signal,            /* signal */
+       sema_own,               /* own */
+       NULL,                   /* is_owned */
+       NULL,                   /* special_wait */
+       NULL                    /* prewait */
+};
+
+void _wapi_sem_details (gpointer handle_info)
 {
-       WapiHandle handle;
-       guint32 val;
-       gint32 max;
+       struct _WapiHandle_sem *sem = (struct _WapiHandle_sem *)handle_info;
+       
+       g_print ("val: %5u, max: %5d", sem->val, sem->max);
+}
+
+struct _WapiHandleOps _wapi_namedsem_ops = {
+       NULL,                   /* close */
+       namedsema_signal,       /* signal */
+       namedsema_own,          /* own */
+       NULL,                   /* is_owned */
+       NULL,                   /* special_wait */
+       NULL                    /* prewait */
 };
 
-/* This mutex controls access to _all_ semaphores and should not be
- * locked for long periods.
- *
- * This global mutex and cond is really for wait_multiple, so we dont
- * have to try and lock multiple handle mutexes and conditions.
- */
-static mono_mutex_t sem_mutex=MONO_MUTEX_INITIALIZER;
-static pthread_cond_t sem_cond=PTHREAD_COND_INITIALIZER;
-
-static void sema_close(WapiHandle *handle);
-static gboolean sema_wait(WapiHandle *handle, WapiHandle *signal, guint32 ms);
-static guint32 sema_wait_multiple(gpointer data);
-static void sema_signal(WapiHandle *handle);
-
-static struct _WapiHandleOps sem_ops = {
-       sema_close,             /* close */
-       NULL,                   /* getfiletype */
-       NULL,                   /* readfile */
-       NULL,                   /* writefile */
-       NULL,                   /* flushfile */
-       NULL,                   /* seek */
-       NULL,                   /* setendoffile */
-       NULL,                   /* getfilesize */
-       NULL,                   /* getfiletime */
-       NULL,                   /* setfiletime */
-       sema_wait,              /* wait */
-       sema_wait_multiple,     /* wait_multiple */
-       sema_signal,            /* signal */
+static gboolean sem_release (gpointer handle, gint32 count, gint32 *prev);
+static gboolean namedsem_release (gpointer handle, gint32 count, gint32 *prev);
+
+static struct 
+{
+       gboolean (*release)(gpointer handle, gint32 count, gint32 *prev);
+} sem_ops[WAPI_HANDLE_COUNT] = {
+       {NULL},
+       {NULL},
+       {NULL},
+       {NULL},
+       {sem_release},
+       {NULL},
+       {NULL},
+       {NULL},
+       {NULL},
+       {NULL},
+       {NULL},
+       {NULL},
+       {namedsem_release},
 };
 
-static void sema_close(WapiHandle *handle G_GNUC_UNUSED)
+static mono_once_t sem_ops_once=MONO_ONCE_INIT;
+
+static void sem_ops_init (void)
 {
-       /* Not really much to do here */
-#ifdef DEBUG
-       g_message(G_GNUC_PRETTY_FUNCTION ": closing sem handle %p", handle);
-#endif
+       _wapi_handle_register_capabilities (WAPI_HANDLE_SEM,
+                                           WAPI_HANDLE_CAP_WAIT |
+                                           WAPI_HANDLE_CAP_SIGNAL);
+       _wapi_handle_register_capabilities (WAPI_HANDLE_NAMEDSEM,
+                                           WAPI_HANDLE_CAP_WAIT |
+                                           WAPI_HANDLE_CAP_SIGNAL);
 }
 
-static gboolean sema_wait(WapiHandle *handle, WapiHandle *signal, guint32 ms)
+static void sema_signal(gpointer handle)
 {
-       struct _WapiHandle_sem *sem_handle=(struct _WapiHandle_sem *)handle;
-       gboolean waited;
-       int ret;
-       
-       mono_mutex_lock(&sem_mutex);
+       ReleaseSemaphore(handle, 1, NULL);
+}
+
+static gboolean sema_own (gpointer handle)
+{
+       struct _WapiHandle_sem *sem_handle;
+       gboolean ok;
        
-       /* Signal this handle after we have obtained the semaphore
-        * global lock
-        */
-       if(signal!=NULL) {
-               signal->ops->signal(signal);
+       ok=_wapi_lookup_handle (handle, WAPI_HANDLE_SEM,
+                               (gpointer *)&sem_handle);
+       if(ok==FALSE) {
+               g_warning ("%s: error looking up sem handle %p", __func__,
+                          handle);
+               return(FALSE);
        }
        
-       /* Shortcut when ms==0 */
-       if(ms==0) {
-               /* Just poll */
 #ifdef DEBUG
-               g_message(G_GNUC_PRETTY_FUNCTION ": Polling");
+       g_message("%s: owning sem handle %p", __func__, handle);
 #endif
-               if(sem_handle->val>0) {
-                       waited=TRUE;
-               } else {
-                       waited=FALSE;
-               }
-               goto end;
-       }
-       
-       /* Check state first */
-       if(sem_handle->val>0) {
-               waited=TRUE;
-               goto end;
-       }
-       
-       if(ms==INFINITE) {
-               ret=mono_cond_wait(&sem_cond, &sem_mutex);
-               waited=TRUE;
-       } else {
-               struct timespec timeout;
 
-               _wapi_calc_timeout(&timeout, ms);
-               
-               ret=mono_cond_timedwait(&sem_cond, &sem_mutex, &timeout);
-               if(ret==0) {
-                       waited=TRUE;
-               } else {
-                       /* ret might be ETIMEDOUT for timeout, or
-                        * other for error */
-                       waited=FALSE;
-               }
-       }
+       sem_handle->val--;
        
-end:
-       if(waited==TRUE) {
-               sem_handle->val--;
+#ifdef DEBUG
+       g_message ("%s: sem %p val now %d", __func__, handle, sem_handle->val);
+#endif
+
+       if(sem_handle->val==0) {
+               _wapi_handle_set_signal_state (handle, FALSE, FALSE);
        }
-       
-       mono_mutex_unlock(&sem_mutex);
-       return(waited);
+
+       return(TRUE);
 }
 
-static guint32 sema_wait_multiple(gpointer data G_GNUC_UNUSED)
+static void namedsema_signal (gpointer handle)
 {
-       WaitQueueItem *item=(WaitQueueItem *)data;
-       guint32 numhandles, count;
-       struct timespec timeout;
-       guint32 i;
-       int ret;
-       
-       numhandles=item->handles[WAPI_HANDLE_SEM]->len;
+       ReleaseSemaphore (handle, 1, NULL);
+}
+
+/* NB, always called with the shared handle lock held */
+static gboolean namedsema_own (gpointer handle)
+{
+       struct _WapiHandle_namedsem *namedsem_handle;
+       gboolean ok;
        
 #ifdef DEBUG
-       g_message(G_GNUC_PRETTY_FUNCTION
-                 ": waiting on %d sem handles for %d ms", numhandles,
-                 item->timeout);
+       g_message ("%s: owning named sem handle %p", __func__, handle);
 #endif
 
-       mono_mutex_lock(&sem_mutex);
-       
-       /* First, check if any of the handles are already signalled.
-        * If waitall is specified we only return if all handles have
-        * been signalled.
-        */
-       for(count=0, i=0; i<numhandles; i++) {
-               struct _WapiHandle_sem *sem_handle;
-               
-               sem_handle=g_ptr_array_index(item->handles[WAPI_HANDLE_SEM],
-                                            i);
-               if(sem_handle->val>0) {
-                       count++;
-               }
+       ok = _wapi_lookup_handle (handle, WAPI_HANDLE_NAMEDSEM,
+                                 (gpointer *)&namedsem_handle);
+       if (ok == FALSE) {
+               g_warning ("%s: error looking up named sem handle %p",
+                          __func__, handle);
+               return (FALSE);
        }
        
+       namedsem_handle->val--;
+       
 #ifdef DEBUG
-       g_message(G_GNUC_PRETTY_FUNCTION
-                 ": Preliminary check found %d handles signalled", count);
+       g_message ("%s: named sem %p val now %d", __func__, handle,
+                  namedsem_handle->val);
 #endif
 
-       if((item->waitall==TRUE && count==numhandles) ||
-          (item->waitall==FALSE && count>0)) {
-               goto success;
+       if (namedsem_handle->val == 0) {
+               _wapi_shared_handle_set_signal_state (handle, FALSE);
        }
        
-       /* OK, we need to wait for some */
-       if(item->timeout!=INFINITE) {
-               _wapi_calc_timeout(&timeout, item->timeout);
-       }
+       return (TRUE);
+}
+static gpointer sem_create (WapiSecurityAttributes *security G_GNUC_UNUSED,
+                           gint32 initial, gint32 max)
+{
+       struct _WapiHandle_sem sem_handle = {0};
+       gpointer handle;
+       int thr_ret;
        
-       /* We can restart from here without resetting the timeout,
-        * because it is calculated from absolute time, not an offset.
+       /* Need to blow away any old errors here, because code tests
+        * for ERROR_ALREADY_EXISTS on success (!) to see if a
+        * semaphore was freshly created
         */
-again:
-       if(item->timeout==INFINITE) {
-               ret=mono_cond_wait(&sem_cond, &sem_mutex);
-       } else {
-               ret=mono_cond_timedwait(&sem_cond, &sem_mutex, &timeout);
-       }
+       SetLastError (ERROR_SUCCESS);
        
-       if(ret==ETIMEDOUT) {
-#ifdef DEBUG
-               g_message(G_GNUC_PRETTY_FUNCTION ": Wait timed out");
-#endif
+       sem_handle.val = initial;
+       sem_handle.max = max;
+
+       handle = _wapi_handle_new (WAPI_HANDLE_SEM, &sem_handle);
+       if (handle == _WAPI_HANDLE_INVALID) {
+               g_warning ("%s: error creating semaphore handle", __func__);
+               SetLastError (ERROR_GEN_FAILURE);
+               return(NULL);
+       }
 
-               goto success;
+       pthread_cleanup_push ((void(*)(void *))_wapi_handle_unlock_handle,
+                             handle);
+       thr_ret = _wapi_handle_lock_handle (handle);
+       g_assert (thr_ret == 0);
+       
+       if (initial != 0) {
+               _wapi_handle_set_signal_state (handle, TRUE, FALSE);
        }
 
 #ifdef DEBUG
-       g_message(G_GNUC_PRETTY_FUNCTION ": Sem posted, checking status");
+       g_message ("%s: Created semaphore handle %p initial %d max %d",
+                  __func__, handle, initial, max);
 #endif
 
-       /* A semaphore was posted, so see if it was one we are
-        * interested in
+       thr_ret = _wapi_handle_unlock_handle (handle);
+       g_assert (thr_ret == 0);
+       pthread_cleanup_pop (0);
+
+       return(handle);
+}
+
+static gpointer namedsem_create (WapiSecurityAttributes *security G_GNUC_UNUSED, gint32 initial, gint32 max, const gunichar2 *name G_GNUC_UNUSED)
+{
+       struct _WapiHandle_namedsem namedsem_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
         */
-       for(count=0, i=0; i<numhandles; i++) {
-               struct _WapiHandle_sem *sem_handle;
-               
-               sem_handle=g_ptr_array_index(item->handles[WAPI_HANDLE_SEM],
-                                            i);
-               if(sem_handle->val>0) {
-                       count++;
-               }
-       }
+       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 a
+        * semaphore was freshly created
+        */
+       SetLastError (ERROR_SUCCESS);
 
+       utf8_name = g_utf16_to_utf8 (name, -1, NULL, NULL, NULL);
+       
 #ifdef DEBUG
-       g_message(G_GNUC_PRETTY_FUNCTION
-                 ": Check after sem post found %d handles signalled", count);
+       g_message ("%s: Creating named sem [%s]", __func__, utf8_name);
 #endif
 
-       if((item->waitall==TRUE && count==numhandles) ||
-          (item->waitall==FALSE && count>0)) {
-               goto success;
+       offset = _wapi_search_handle_namespace (WAPI_HANDLE_NAMEDSEM,
+                                               utf8_name);
+       if (offset == -1) {
+               /* The name has already been used for a different
+                * object.
+                */
+               SetLastError (ERROR_INVALID_HANDLE);
+               goto cleanup;
+       } else if (offset != 0) {
+               /* Not an error, but this is how the caller is
+                * informed that the semaphore wasn't freshly created
+                */
+               SetLastError (ERROR_ALREADY_EXISTS);
        }
+       /* Fall through to create the semaphore handle */
+
+       if (offset == 0) {
+               /* A new named semaphore, so create both the private
+                * and shared parts
+                */
+               if (strlen (utf8_name) < MAX_PATH) {
+                       namelen = strlen (utf8_name);
+               } else {
+                       namelen = MAX_PATH;
+               }
        
-       /* Either we have waitall set with more handles to wait for, or
-        * the sem that was posted wasn't interesting to us
-        */
-#ifdef DEBUG
-       g_message(G_GNUC_PRETTY_FUNCTION ": Waiting a bit longer");
-#endif
+               memcpy (&namedsem_handle.sharedns.name, utf8_name, namelen);
+       
+               namedsem_handle.val = initial;
+               namedsem_handle.max = max;
 
-       goto again;
-       
-success:
-       item->waited[WAPI_HANDLE_SEM]=TRUE;
-       item->waitcount[WAPI_HANDLE_SEM]=count;
-
-       if((item->waitall==TRUE && count==numhandles) ||
-          (item->waitall==FALSE && count>0)) {
-               /* Decrease all waited semaphores */
-               for(i=0; i<numhandles; i++) {
-                       struct _WapiHandle_sem *sem_handle;
-                       guint32 idx;
-                       
-                       sem_handle=g_ptr_array_index(
-                               item->handles[WAPI_HANDLE_SEM], i);
-
-                       idx=g_array_index(item->waitindex[WAPI_HANDLE_SEM],
-                                         guint32, i);
-                       _wapi_handle_set_lowest(item, idx);
-                       
-                       if(sem_handle->val>0) {
-                               sem_handle->val--;
-                       }
+               handle = _wapi_handle_new (WAPI_HANDLE_NAMEDSEM,
+                                          &namedsem_handle);
+       } else {
+               /* A new reference to an existing named semaphore, so
+                * just create the private part
+                */
+               handle = _wapi_handle_new_from_offset (WAPI_HANDLE_NAMEDSEM,
+                                                      offset, TRUE);
+       }
+       
+       if (handle == _WAPI_HANDLE_INVALID) {
+               g_warning ("%s: error creating named sem handle", __func__);
+               SetLastError (ERROR_GEN_FAILURE);
+               goto cleanup;
+       }
+       ret = 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 != 0) {
+                       _wapi_shared_handle_set_signal_state (handle, TRUE);
                }
+               
+               _wapi_handle_unlock_shared_handles ();
        }
+       
+#ifdef DEBUG
+       g_message ("%s: returning named sem handle %p", __func__, handle);
+#endif
 
-       mono_mutex_unlock(&sem_mutex);
-
-       return(count);
+cleanup:
+       g_free (utf8_name);
+       
+       _wapi_namespace_unlock (NULL);
+       
+       return (ret);
 }
 
-static void sema_signal(WapiHandle *handle)
-{
-       ReleaseSemaphore(handle, 1, NULL);
-}
 
 /**
  * CreateSemaphore:
@@ -277,40 +327,147 @@ static void sema_signal(WapiHandle *handle)
  *
  * Return value: a new handle, or NULL
  */
-WapiHandle *CreateSemaphore(WapiSecurityAttributes *security G_GNUC_UNUSED, gint32 initial, gint32 max, const guchar *name G_GNUC_UNUSED)
+gpointer CreateSemaphore(WapiSecurityAttributes *security G_GNUC_UNUSED, gint32 initial, gint32 max, const gunichar2 *name)
 {
-       struct _WapiHandle_sem *sem_handle;
-       WapiHandle *handle;
+       mono_once (&sem_ops_once, sem_ops_init);
        
-       if(max<=0) {
+       if (max <= 0) {
 #ifdef DEBUG
-               g_message(G_GNUC_PRETTY_FUNCTION ": max <= 0");
+               g_message ("%s: max <= 0", __func__);
 #endif
 
+               SetLastError (ERROR_INVALID_PARAMETER);
                return(NULL);
        }
        
-       if(initial>max || initial<0) {
+       if (initial > max || initial < 0) {
 #ifdef DEBUG
-               g_message(G_GNUC_PRETTY_FUNCTION ": initial>max or < 0");
+               g_message ("%s: initial>max or < 0", __func__);
 #endif
 
+               SetLastError (ERROR_INVALID_PARAMETER);
                return(NULL);
        }
+
+       if (name == NULL) {
+               return (sem_create (security, initial, max));
+       } else {
+               return (namedsem_create (security, initial, max, name));
+       }
+}
+
+static gboolean sem_release (gpointer handle, gint32 count, gint32 *prevcount)
+{
+       struct _WapiHandle_sem *sem_handle;
+       gboolean ok;
+       gboolean ret=FALSE;
+       int thr_ret;
        
-       sem_handle=(struct _WapiHandle_sem *)g_new0(struct _WapiHandle_sem, 1);
-       handle=(WapiHandle *)sem_handle;
-       _WAPI_HANDLE_INIT(handle, WAPI_HANDLE_SEM, sem_ops);
+       ok = _wapi_lookup_handle (handle, WAPI_HANDLE_SEM,
+                                 (gpointer *)&sem_handle);
+       if (ok == FALSE) {
+               g_warning ("%s: error looking up sem handle %p", __func__,
+                          handle);
+               return(FALSE);
+       }
 
-       sem_handle->val=initial;
-       sem_handle->max=max;
+       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(G_GNUC_PRETTY_FUNCTION ": Created semaphore handle %p",
-                 handle);
+       g_message ("%s: sem %p val %d count %d", __func__, handle,
+                  sem_handle->val, count);
 #endif
        
-       return(handle);
+       /* Do this before checking for count overflow, because overflowing max
+        * is a listed technique for finding the current value
+        */
+       if (prevcount != NULL) {
+               *prevcount = sem_handle->val;
+       }
+       
+       /* 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
+
+               goto end;
+       }
+       
+       sem_handle->val += count;
+       _wapi_handle_set_signal_state (handle, TRUE, TRUE);
+       
+       ret = TRUE;
+
+#ifdef DEBUG
+       g_message ("%s: sem %p val now %d", __func__, handle, sem_handle->val);
+#endif
+       
+end:
+       thr_ret = _wapi_handle_unlock_handle (handle);
+       g_assert (thr_ret == 0);
+       pthread_cleanup_pop (0);
+
+       return(ret);
+}
+
+static gboolean namedsem_release (gpointer handle, gint32 count,
+                                 gint32 *prevcount)
+{
+       struct _WapiHandle_namedsem *sem_handle;
+       gboolean ok;
+       gboolean ret=FALSE;
+       int thr_ret;
+       
+       ok = _wapi_lookup_handle (handle, WAPI_HANDLE_NAMEDSEM,
+                                 (gpointer *)&sem_handle);
+       if (ok == FALSE) {
+               g_warning ("%s: error looking up sem handle %p", __func__,
+                          handle);
+               return(FALSE);
+       }
+
+       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,
+                 sem_handle->val, count);
+#endif
+       
+       /* Do this before checking for count overflow, because overflowing max
+        * is a listed technique for finding the current value
+        */
+       if (prevcount != NULL) {
+               *prevcount = sem_handle->val;
+       }
+       
+       /* 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
+
+               goto end;
+       }
+       
+       sem_handle->val += count;
+       _wapi_shared_handle_set_signal_state (handle, TRUE);
+       
+       ret = TRUE;
+
+#ifdef DEBUG
+       g_message("%s: named sem %p val now %d", __func__, handle,
+                 sem_handle->val);
+#endif
+       
+end:
+       _wapi_handle_unlock_shared_handles ();
+
+       return(ret);
 }
 
 /**
@@ -325,36 +482,83 @@ WapiHandle *CreateSemaphore(WapiSecurityAttributes *security G_GNUC_UNUSED, gint
  *
  * Return value: %TRUE on success, %FALSE otherwise.
  */
-gboolean ReleaseSemaphore(WapiHandle *handle, gint32 count, gint32 *prevcount)
+gboolean ReleaseSemaphore(gpointer handle, gint32 count, gint32 *prevcount)
 {
-       struct _WapiHandle_sem *sem_handle=(struct _WapiHandle_sem *)handle;
-       gboolean ret=FALSE;
+       WapiHandleType type;
        
+       if (handle == NULL) {
+               SetLastError (ERROR_INVALID_HANDLE);
+               return (FALSE);
+       }
        
-       mono_mutex_lock(&sem_mutex);
+       type = _wapi_handle_type (handle);
        
-       /* Do this before checking for count overflow, because overflowing max
-        * is a listed technique for finding the current value
-        */
-       if(prevcount!=NULL) {
-               *prevcount=sem_handle->val;
+       if (sem_ops[type].release == NULL) {
+               SetLastError (ERROR_INVALID_HANDLE);
+               return (FALSE);
        }
        
-       /* No idea why max is signed, but thats the spec :-( */
-       if(sem_handle->val+count > (guint32)sem_handle->max) {
+       return (sem_ops[type].release (handle, count, prevcount));
+}
+
+gpointer OpenSemaphore (guint32 access G_GNUC_UNUSED, gboolean inherit G_GNUC_UNUSED,
+                       const gunichar2 *name)
+{
+       gpointer handle;
+       gchar *utf8_name;
+       int thr_ret;
+       gpointer ret = NULL;
+       gint32 offset;
+
+       mono_once (&sem_ops_once, sem_ops_init);
+       
+       /* w32 seems to guarantee that opening named objects can't
+        * race each other
+        */
+       thr_ret = _wapi_namespace_lock ();
+       g_assert (thr_ret == 0);
+       
+       utf8_name = g_utf16_to_utf8 (name, -1, NULL, NULL, NULL);
+       
 #ifdef DEBUG
-               g_message(G_GNUC_PRETTY_FUNCTION ": sem %p max value would be exceeded: max %d current %d count %d",
-                         handle, sem_handle->max, sem_handle->val, count);
+       g_message ("%s: Opening named sem [%s]", __func__, utf8_name);
 #endif
 
-               goto end;
+       offset = _wapi_search_handle_namespace (WAPI_HANDLE_NAMEDSEM,
+                                               utf8_name);
+       if (offset == -1) {
+               /* The name has already been used for a different
+                * object.
+                */
+               SetLastError (ERROR_INVALID_HANDLE);
+               goto cleanup;
+       } else if (offset == 0) {
+               /* This name doesn't exist */
+               SetLastError (ERROR_FILE_NOT_FOUND);    /* yes, really */
+               goto cleanup;
        }
+
+       /* A new reference to an existing named semaphore, so just
+        * create the private part
+        */
+       handle = _wapi_handle_new_from_offset (WAPI_HANDLE_NAMEDSEM, offset,
+                                              TRUE);
        
-       sem_handle->val+=count;
-       pthread_cond_broadcast(&sem_cond);
-       ret=TRUE;
+       if (handle == _WAPI_HANDLE_INVALID) {
+               g_warning ("%s: error opening named sem handle", __func__);
+               SetLastError (ERROR_GEN_FAILURE);
+               goto cleanup;
+       }
+       ret = handle;
        
-end:
-       mono_mutex_unlock(&sem_mutex);
-       return(ret);
+#ifdef DEBUG
+       g_message ("%s: returning named sem handle %p", __func__, handle);
+#endif
+
+cleanup:
+       g_free (utf8_name);
+       
+       _wapi_namespace_unlock (NULL);
+       
+       return (ret);
 }