First set of licensing changes
[mono.git] / mono / utils / mono-mmap.c
index bc574bfe750a3e7854449da96ec774ba8908a076..7fa483ce593747f586e4842fed1a720ffeff2dee 100644 (file)
@@ -5,6 +5,7 @@
  *   Mono Team (mono-list@lists.ximian.com)
  *
  * Copyright 2001-2008 Novell, Inc.
+ * Licensed under the MIT license. See LICENSE file in the project root for full license information.
  */
 
 #include "config.h"
 #endif
 
 #include "mono-mmap.h"
-#include "mono-mmap-internal.h"
+#include "mono-mmap-internals.h"
 #include "mono-proclib.h"
+#include <mono/utils/mono-threads.h>
+
+
+#define BEGIN_CRITICAL_SECTION do { \
+       MonoThreadInfo *__info = mono_thread_info_current_unchecked (); \
+       if (__info) __info->inside_critical_region = TRUE;      \
+
+#define END_CRITICAL_SECTION \
+       if (__info) __info->inside_critical_region = FALSE;     \
+} while (0)    \
 
 #ifndef MAP_ANONYMOUS
 #define MAP_ANONYMOUS MAP_ANON
@@ -310,6 +321,7 @@ mono_valloc (void *addr, size_t length, int flags)
        mflags |= MAP_ANONYMOUS;
        mflags |= MAP_PRIVATE;
 
+       BEGIN_CRITICAL_SECTION;
        ptr = mmap (addr, length, prot, mflags, -1, 0);
        if (ptr == MAP_FAILED) {
                int fd = open ("/dev/zero", O_RDONLY);
@@ -317,9 +329,11 @@ mono_valloc (void *addr, size_t length, int flags)
                        ptr = mmap (addr, length, prot, mflags, fd, 0);
                        close (fd);
                }
-               if (ptr == MAP_FAILED)
-                       return NULL;
        }
+       END_CRITICAL_SECTION;
+
+       if (ptr == MAP_FAILED)
+               return NULL;
        return ptr;
 }
 
@@ -335,7 +349,11 @@ mono_valloc (void *addr, size_t length, int flags)
 int
 mono_vfree (void *addr, size_t length)
 {
-       return munmap (addr, length);
+       int res;
+       BEGIN_CRITICAL_SECTION;
+       res = munmap (addr, length);
+       END_CRITICAL_SECTION;
+       return res;
 }
 
 /**
@@ -370,7 +388,9 @@ mono_file_map (size_t length, int flags, int fd, guint64 offset, void **ret_hand
        if (flags & MONO_MMAP_32BIT)
                mflags |= MAP_32BIT;
 
+       BEGIN_CRITICAL_SECTION;
        ptr = mmap (0, length, prot, mflags, fd, offset);
+       END_CRITICAL_SECTION;
        if (ptr == MAP_FAILED)
                return NULL;
        *ret_handle = (void*)length;
@@ -390,7 +410,13 @@ mono_file_map (size_t length, int flags, int fd, guint64 offset, void **ret_hand
 int
 mono_file_unmap (void *addr, void *handle)
 {
-       return munmap (addr, (size_t)handle);
+       int res;
+
+       BEGIN_CRITICAL_SECTION;
+       res = munmap (addr, (size_t)handle);
+       END_CRITICAL_SECTION;
+
+       return res;
 }
 
 /**
@@ -597,7 +623,10 @@ mono_shared_area (void)
                shm_unlink (buf);
                close (fd);
        }
+       BEGIN_CRITICAL_SECTION;
        res = mmap (NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
+       END_CRITICAL_SECTION;
+
        if (res == MAP_FAILED) {
                shm_unlink (buf);
                close (fd);
@@ -649,7 +678,10 @@ mono_shared_area_for_pid (void *pid)
        fd = shm_open (buf, O_RDONLY, S_IRUSR|S_IRGRP);
        if (fd == -1)
                return NULL;
+       BEGIN_CRITICAL_SECTION;
        res = mmap (NULL, size, PROT_READ, MAP_SHARED, fd, 0);
+       END_CRITICAL_SECTION;
+
        if (res == MAP_FAILED) {
                close (fd);
                return NULL;
@@ -664,7 +696,9 @@ void
 mono_shared_area_unload  (void *area)
 {
        /* FIXME: currently we load only a page */
+       BEGIN_CRITICAL_SECTION;
        munmap (area, mono_pagesize ());
+       END_CRITICAL_SECTION;
 }
 
 int