NaCl runtime fixes
[mono.git] / mono / io-layer / shared.c
index e8b4fb33e889167e96c23e068f480f11fce8dd48..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
 
 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)
 {
@@ -119,6 +123,40 @@ _wapi_shm_sem_unlock (int sem)
 {
        return noshm_sem_unlock (sem);
 }
+
+gpointer
+_wapi_shm_attach (_wapi_shm_t type)
+{
+       gpointer res;
+
+       switch(type) {
+       case WAPI_SHM_DATA:
+               res = g_malloc0 (sizeof(struct _WapiHandleSharedLayout));
+               break;
+       case WAPI_SHM_FILESHARE:
+               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 
@@ -129,12 +167,6 @@ _wapi_shm_sem_unlock (int sem)
 #define USE_SHM 1
 #endif
 
-#ifdef DISABLE_SHARED_HANDLES
-gboolean _wapi_shm_disabled = TRUE;
-#else
-gboolean _wapi_shm_disabled = FALSE;
-#endif
-
 static gchar *
 _wapi_shm_base_name (_wapi_shm_t type)
 {
@@ -222,7 +254,7 @@ static gchar *
 _wapi_shm_file (_wapi_shm_t type)
 {
        static gchar file[_POSIX_PATH_MAX];
-       gchar *name = NULL, *filename, *dir, *wapi_dir;
+       gchar *name = NULL, *filename, *wapi_dir;
 
        name = _wapi_shm_base_name (type);
 
@@ -241,14 +273,6 @@ _wapi_shm_file (_wapi_shm_t type)
 
        g_snprintf (file, _POSIX_PATH_MAX, "%s", filename);
        g_free (filename);
-               
-       /* No need to check if the dir already exists or check
-        * mkdir() errors, because on any error the open() call will
-        * report the problem.
-        */
-       dir = g_path_get_dirname (file);
-       mkdir (dir, 0755);
-       g_free (dir);
        
        return file;
 }
@@ -261,6 +285,15 @@ _wapi_shm_file_open (const gchar *filename, guint32 wanted_size)
        int ret, tries = 0;
        gboolean created = FALSE;
        mode_t oldmask;
+       gchar *dir;
+               
+       /* No need to check if the dir already exists or check
+        * mkdir() errors, because on any error the open() call will
+        * report the problem.
+        */
+       dir = g_path_get_dirname (filename);
+       mkdir (dir, 0755);
+       g_free (dir);
 
 try_again:
        if (tries++ > 10) {
@@ -369,17 +402,18 @@ try_again:
        return fd;
 }
 
-static gboolean
-check_disabled (void)
+gboolean
+_wapi_shm_enabled (void)
 {
-       if (_wapi_shm_disabled || g_getenv ("MONO_DISABLE_SHM")) {
-               const char* val = g_getenv ("MONO_DISABLE_SHM");
-               if (val == NULL || *val == '1' || *val == 'y' || *val == 'Y') {
-                       _wapi_shm_disabled = TRUE;
-               }
+       static gboolean env_checked;
+
+       if (!env_checked) {
+               if (g_getenv ("MONO_ENABLE_SHM"))
+                       _wapi_shm_disabled = FALSE;
+               env_checked = TRUE;
        }
 
-       return _wapi_shm_disabled;
+       return !_wapi_shm_disabled;
 }
 
 /*
@@ -397,7 +431,7 @@ _wapi_shm_attach (_wapi_shm_t type)
        struct stat statbuf;
        gchar *filename = _wapi_shm_file (type), *shm_name;
        guint32 size;
-
+       
        switch(type) {
        case WAPI_SHM_DATA:
                size = sizeof(struct _WapiHandleSharedLayout);
@@ -411,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
@@ -455,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)
 {
@@ -462,6 +504,8 @@ shm_semaphores_init (void)
        key_t oldkey;
        int thr_ret;
        struct _WapiHandleSharedLayout *tmp_shared;
+       gchar *ftmp;
+       gchar *filename;
        
        /*
         * Yet more barmy API - this union is a well-defined parameter
@@ -501,7 +545,16 @@ shm_semaphores_init (void)
        tmp_shared = _wapi_shm_attach (WAPI_SHM_DATA);
        g_assert (tmp_shared != NULL);
        
-       key = ftok (_wapi_shm_file (WAPI_SHM_DATA), 'M');
+#ifdef USE_SHM
+       ftmp=_wapi_shm_shm_name (WAPI_SHM_DATA);
+       filename = g_build_filename ("/dev/shm", ftmp, NULL);
+       g_assert (filename!=NULL);
+       key = ftok (filename, 'M');
+       g_free (ftmp);
+       g_free (filename);
+#else
+       key = ftok ( _wapi_shm_file (WAPI_SHM_DATA), 'M');
+#endif
 
 again:
        retries++;
@@ -788,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 ();