#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)
{
{
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
#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)
{
_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);
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;
}
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) {
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;
}
/*
struct stat statbuf;
gchar *filename = _wapi_shm_file (type), *shm_name;
guint32 size;
-
+
switch(type) {
case WAPI_SHM_DATA:
size = sizeof(struct _WapiHandleSharedLayout);
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
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)
{
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
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++;
void
_wapi_shm_semaphores_init (void)
{
- if (check_disabled ())
+ if (!_wapi_shm_enabled ())
noshm_semaphores_init ();
else
shm_semaphores_init ();