* Rodrigo Kumpera
*
* Copyright 2014 Xamarin Inc (http://www.xamarin.com)
+ * Licensed under the MIT license. See LICENSE file in the project root for full license information.
*/
#include <config.h>
-#ifndef TARGET_WIN32
+#ifndef HOST_WIN32
#include <glib.h>
#include <string.h>
#include <mono/utils/atomic.h>
#include <mono/utils/mono-memory-model.h>
#include <mono/utils/mono-mmap.h>
+#include <mono/utils/mono-coop-mutex.h>
+#include <mono/utils/mono-threads.h>
typedef struct {
int kind;
#endif
static int mmap_init_state;
-static mono_mutex_t named_regions_mutex;
+static MonoCoopMutex named_regions_mutex;
static GHashTable *named_regions;
if (InterlockedCompareExchange (&mmap_init_state, 1, 0) != 0)
goto retry;
named_regions = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, NULL);
- mono_mutex_init (&named_regions_mutex);
+ mono_coop_mutex_init (&named_regions_mutex);
mono_atomic_store_release (&mmap_init_state, 2);
break;
case 1:
do {
- g_usleep (1000); /* Been init'd by other threads, this is very rare. */
+ mono_thread_info_sleep (1, NULL); /* Been init'd by other threads, this is very rare. */
} while (mmap_init_state != 2);
break;
case 2:
named_regions_lock (void)
{
file_mmap_init ();
- mono_mutex_lock (&named_regions_mutex);
+ mono_coop_mutex_lock (&named_regions_mutex);
}
static void
named_regions_unlock (void)
{
- mono_mutex_unlock (&named_regions_mutex);
+ mono_coop_mutex_unlock (&named_regions_mutex);
}
*error = COULD_NOT_MAP_MEMORY;
goto done;
}
- file_name = alloca (alloc_size);
+ file_name = (char *)alloca (alloc_size);
strcpy (file_name, tmp_dir);
strcat (file_name, MONO_ANON_FILE_TEMPLATE);
*error = FILE_ALREADY_EXISTS;
handle = NULL;
} else {
- handle = open_file_map (path, -1, mode, capacity, access, options, error);
+ handle = (MmapHandle *)open_file_map (path, -1, mode, capacity, access, options, error);
if (handle) {
handle->name = g_strdup (c_mapName);
g_hash_table_insert (named_regions, handle->name, handle);
mono_mmap_open_handle (void *input_fd, MonoString *mapName, gint64 *capacity, int access, int options, int *error)
{
MmapHandle *handle;
- char *c_mapName = mono_string_to_utf8 (mapName);
-
- named_regions_lock ();
- handle = (MmapHandle*)g_hash_table_lookup (named_regions, c_mapName);
- if (handle) {
- *error = FILE_ALREADY_EXISTS;
- handle = NULL;
+ if (!mapName) {
+ handle = (MmapHandle *)open_file_map (NULL, GPOINTER_TO_INT (input_fd), FILE_MODE_OPEN, capacity, access, options, error);
} else {
- //XXX we're exploiting wapi HANDLE == FD equivalence. THIS IS FRAGILE, create a _wapi_handle_to_fd call
- handle = open_file_map (NULL, GPOINTER_TO_INT (input_fd), FILE_MODE_OPEN, capacity, access, options, error);
- handle->name = g_strdup (c_mapName);
- g_hash_table_insert (named_regions, handle->name, handle);
- }
- named_regions_unlock ();
+ char *c_mapName = mono_string_to_utf8 (mapName);
- g_free (c_mapName);
+ named_regions_lock ();
+ handle = (MmapHandle*)g_hash_table_lookup (named_regions, c_mapName);
+ if (handle) {
+ *error = FILE_ALREADY_EXISTS;
+ handle = NULL;
+ } else {
+ //XXX we're exploiting wapi HANDLE == FD equivalence. THIS IS FRAGILE, create a _wapi_handle_to_fd call
+ handle = (MmapHandle *)open_file_map (NULL, GPOINTER_TO_INT (input_fd), FILE_MODE_OPEN, capacity, access, options, error);
+ handle->name = g_strdup (c_mapName);
+ g_hash_table_insert (named_regions, handle->name, handle);
+ }
+ named_regions_unlock ();
+
+ g_free (c_mapName);
+ }
return handle;
}
void
mono_mmap_close (void *mmap_handle)
{
- MmapHandle *handle = mmap_handle;
+ MmapHandle *handle = (MmapHandle *)mmap_handle;
named_regions_lock ();
--handle->ref_count;
void
mono_mmap_configure_inheritability (void *mmap_handle, gboolean inheritability)
{
- MmapHandle *h = mmap_handle;
+ MmapHandle *h = (MmapHandle *)mmap_handle;
int fd, flags;
fd = h->fd;
void
mono_mmap_flush (void *mmap_handle)
{
- MmapInstance *h = mmap_handle;
+ MmapInstance *h = (MmapInstance *)mmap_handle;
if (h)
msync (h->address, h->length, MS_SYNC);
mono_mmap_map (void *handle, gint64 offset, gint64 *size, int access, void **mmap_handle, void **base_address)
{
gint64 mmap_offset = 0;
- MmapHandle *fh = handle;
+ MmapHandle *fh = (MmapHandle *)handle;
MmapInstance res = { 0 };
size_t eff_size = *size;
struct stat buf = { 0 };
mono_mmap_unmap (void *mmap_handle)
{
int res = 0;
- MmapInstance *h = mmap_handle;
+ MmapInstance *h = (MmapInstance *)mmap_handle;
res = mono_file_unmap (h->address, h->free_handle);