X-Git-Url: http://wien.tomnetworks.com/gitweb/?a=blobdiff_plain;f=mono%2Fio-layer%2Fshared.c;h=f8539039cad103441bc63f977c3331251ff5df2c;hb=3572dc9101024332ede058d1de5b3d71cd8e0329;hp=467caf5e9ffebccb0e67b752353cb6e41b591aa8;hpb=97c7774712e011dec2f641526382c260da034687;p=mono.git diff --git a/mono/io-layer/shared.c b/mono/io-layer/shared.c index 467caf5e9ff..f8539039cad 100644 --- a/mono/io-layer/shared.c +++ b/mono/io-layer/shared.c @@ -18,7 +18,7 @@ #include #include -#ifdef HAVE_SYS_SEM_H +#if defined(HAVE_SYS_SEM_H) && !(defined(__native_client__) && defined(__GLIBC__)) # include #else # define DISABLE_SHARED_HANDLES @@ -44,19 +44,15 @@ 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, *dir, *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); @@ -259,14 +275,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; } @@ -279,6 +287,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) { @@ -387,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; } /* @@ -429,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 @@ -473,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) { @@ -480,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 @@ -519,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++; @@ -806,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 ();