[io-layer] Remove Sleep and SleepEx
[mono.git] / mono / io-layer / shared.c
index 0759daf11fead4ed83e597af8e2d9a1ad6be8599..f8539039cad103441bc63f977c3331251ff5df2c 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];
 
-#ifdef DISABLE_SHARED_HANDLES
-gboolean _wapi_shm_disabled = TRUE;
-#else
-gboolean _wapi_shm_disabled = FALSE;
-#endif
+static gpointer wapi_storage [16];
 
 static void
 noshm_semaphores_init (void)
 {
-       int i;
+       int i;
 
-       for (i = 0; i < _WAPI_SHARED_SEM_COUNT; i++) 
-               mono_mutex_init (&noshm_sems [i], NULL);
+       for (i = 0; i < _WAPI_SHARED_SEM_COUNT; i++) 
+               mono_mutex_init (&noshm_sems [i]);
 }
 
 static int
@@ -129,21 +125,38 @@ _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;
 }
-#else
+
+void
+_wapi_shm_detach (_wapi_shm_t type)
+{
+       g_free (wapi_storage [type]);
+}
+
+gboolean
+_wapi_shm_enabled_internal (void)
+{
+       return FALSE;
+}
+
+#else /* DISABLE_SHARED_HANDLES */
+
 /*
  * Use POSIX shared memory if possible, it is simpler, and it has the advantage that 
  * writes to the shared area does not need to be written to disk, avoiding spinning up 
@@ -153,6 +166,8 @@ _wapi_shm_attach (_wapi_shm_t type)
 #define USE_SHM 1
 #endif
 
+static gboolean _wapi_shm_disabled = TRUE;
+
 static gchar *
 _wapi_shm_base_name (_wapi_shm_t type)
 {
@@ -240,7 +255,8 @@ static gchar *
 _wapi_shm_file (_wapi_shm_t type)
 {
        static gchar file[_POSIX_PATH_MAX];
-       gchar *name = NULL, *filename, *wapi_dir;
+       gchar *name = NULL, *filename;
+       const gchar *wapi_dir;
 
        name = _wapi_shm_base_name (type);
 
@@ -248,7 +264,7 @@ _wapi_shm_file (_wapi_shm_t type)
         * nfs mounts breaks, then there should be an option to set
         * the directory.
         */
-       wapi_dir = getenv ("MONO_SHARED_DIR");
+       wapi_dir = g_getenv ("MONO_SHARED_DIR");
        if (wapi_dir == NULL) {
                filename = g_build_filename (g_get_home_dir (), ".wapi", name,
                                             NULL);
@@ -388,17 +404,18 @@ try_again:
        return fd;
 }
 
-static gboolean
-check_disabled (void)
+gboolean
+_wapi_shm_enabled_internal (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;
 }
 
 /*
@@ -430,8 +447,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
@@ -474,6 +492,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)
 {
@@ -481,6 +506,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
@@ -520,7 +547,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++;
@@ -807,7 +843,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 ();