NaCl runtime fixes
[mono.git] / mono / io-layer / shared.c
index bf966d202c3c01f1a37590693c7e8bb104c465d0..ed9bb1fb21178c6d5ec1f8a653ac85c56729e8d0 100644 (file)
@@ -18,7 +18,7 @@
 #include <string.h>
 #include <unistd.h>
 
-#ifdef HAVE_SYS_SEM_H
+#if defined(HAVE_SYS_SEM_H) && !(defined(__native_client__) && defined(__GLIBC__))
 #  include <sys/sem.h>
 #else
 #  define DISABLE_SHARED_HANDLES
@@ -46,6 +46,8 @@ static mono_mutex_t noshm_sems[_WAPI_SHARED_SEM_COUNT];
 
 gboolean _wapi_shm_disabled = TRUE;
 
+static gpointer wapi_storage [16];
+
 static void
 noshm_semaphores_init (void)
 {
@@ -125,20 +127,36 @@ _wapi_shm_sem_unlock (int sem)
 gpointer
 _wapi_shm_attach (_wapi_shm_t type)
 {
-       guint32 size;
+       gpointer res;
 
        switch(type) {
        case WAPI_SHM_DATA:
-               return g_malloc0 (sizeof(struct _WapiHandleSharedLayout));
-               
+               res = g_malloc0 (sizeof(struct _WapiHandleSharedLayout));
+               break;
        case WAPI_SHM_FILESHARE:
-               return g_malloc0 (sizeof(struct _WapiFileShareLayout));
-
+               res = g_malloc0 (sizeof(struct _WapiFileShareLayout));
+               break;
        default:
                g_error ("Invalid type in _wapi_shm_attach ()");
                return NULL;
        }
+
+       wapi_storage [type] = res;
+       return res;
+}
+
+void
+_wapi_shm_detach (_wapi_shm_t type)
+{
+       g_free (wapi_storage [type]);
+}
+
+gboolean
+_wapi_shm_enabled (void)
+{
+       return FALSE;
 }
+
 #else
 /*
  * Use POSIX shared memory if possible, it is simpler, and it has the advantage that 
@@ -384,8 +402,8 @@ try_again:
        return fd;
 }
 
-static gboolean
-check_disabled (void)
+gboolean
+_wapi_shm_enabled (void)
 {
        static gboolean env_checked;
 
@@ -395,7 +413,7 @@ check_disabled (void)
                env_checked = TRUE;
        }
 
-       return _wapi_shm_disabled;
+       return !_wapi_shm_disabled;
 }
 
 /*
@@ -427,8 +445,9 @@ _wapi_shm_attach (_wapi_shm_t type)
                return NULL;
        }
 
-       if (check_disabled ()) {
-               return g_malloc0 (size);
+       if (!_wapi_shm_enabled ()) {
+               wapi_storage [type] = g_malloc0 (size);
+               return wapi_storage [type];
        }
 
 #ifdef USE_SHM
@@ -471,6 +490,13 @@ _wapi_shm_attach (_wapi_shm_t type)
        return shm_seg;
 }
 
+void
+_wapi_shm_detach (_wapi_shm_t type)
+{
+       if (!_wapi_shm_enabled ())
+               g_free (wapi_storage [type]);
+}
+
 static void
 shm_semaphores_init (void)
 {
@@ -815,7 +841,7 @@ shm_sem_unlock (int sem)
 void
 _wapi_shm_semaphores_init (void)
 {
-       if (check_disabled ()) 
+       if (!_wapi_shm_enabled ())
                noshm_semaphores_init ();
        else
                shm_semaphores_init ();