Merge pull request #2338 from BogdanovKirill/httpwritefix3
[mono.git] / mono / utils / hazard-pointer.c
index b412e77d7d535ed088203c81a839caa74c545133..606387c073ef9e93a815366808aee01bbe3d0cd6 100644 (file)
@@ -6,16 +6,24 @@
 
 #include <config.h>
 
+#include <string.h>
+
 #include <mono/utils/hazard-pointer.h>
 #include <mono/utils/mono-membar.h>
 #include <mono/utils/mono-memory-model.h>
-#include <mono/utils/mono-mmap.h>
 #include <mono/utils/monobitset.h>
-#include <mono/utils/mono-threads.h>
 #include <mono/utils/lock-free-array-queue.h>
-#include <mono/utils/mono-counters.h>
 #include <mono/utils/atomic.h>
+#include <mono/utils/mono-os-mutex.h>
+#ifdef SGEN_WITHOUT_MONO
+#include <mono/sgen/sgen-gc.h>
+#include <mono/sgen/sgen-client.h>
+#else
+#include <mono/utils/mono-mmap.h>
+#include <mono/utils/mono-threads.h>
+#include <mono/utils/mono-counters.h>
 #include <mono/io-layer/io-layer.h>
+#endif
 
 typedef struct {
        gpointer p;
@@ -46,7 +54,7 @@ static volatile gint32 overflow_busy [HAZARD_TABLE_OVERFLOW];
 static MonoLockFreeArrayQueue delayed_free_queue = MONO_LOCK_FREE_ARRAY_QUEUE_INIT (sizeof (DelayedFreeItem));
 
 /* The table for small ID assignment */
-static CRITICAL_SECTION small_id_mutex;
+static mono_mutex_t small_id_mutex;
 static int small_id_next;
 static int highest_small_id = -1;
 static MonoBitSet *small_id_table;
@@ -63,7 +71,7 @@ mono_thread_small_id_alloc (void)
 {
        int i, id = -1;
 
-       EnterCriticalSection (&small_id_mutex);
+       mono_os_mutex_lock (&small_id_mutex);
 
        if (!small_id_table)
                small_id_table = mono_bitset_new (1, 0);
@@ -101,7 +109,7 @@ mono_thread_small_id_alloc (void)
                int num_pages = (hazard_table_size * sizeof (MonoThreadHazardPointers) + pagesize - 1) / pagesize;
 
                if (hazard_table == NULL) {
-                       hazard_table = mono_valloc (NULL,
+                       hazard_table = (MonoThreadHazardPointers *volatile) mono_valloc (NULL,
                                sizeof (MonoThreadHazardPointers) * HAZARD_TABLE_MAX_SIZE,
                                MONO_MMAP_NONE);
                }
@@ -125,7 +133,7 @@ mono_thread_small_id_alloc (void)
                mono_memory_write_barrier ();
        }
 
-       LeaveCriticalSection (&small_id_mutex);
+       mono_os_mutex_unlock (&small_id_mutex);
 
        return id;
 }
@@ -134,13 +142,13 @@ void
 mono_thread_small_id_free (int id)
 {
        /* MonoBitSet operations are not atomic. */
-       EnterCriticalSection (&small_id_mutex);
+       mono_os_mutex_lock (&small_id_mutex);
 
        g_assert (id >= 0 && id < small_id_table->size);
        g_assert (mono_bitset_test_fast (small_id_table, id));
        mono_bitset_clear_fast (small_id_table, id);
 
-       LeaveCriticalSection (&small_id_mutex);
+       mono_os_mutex_unlock (&small_id_mutex);
 }
 
 static gboolean
@@ -345,7 +353,7 @@ mono_thread_smr_init (void)
 {
        int i;
 
-       InitializeCriticalSection(&small_id_mutex);
+       mono_os_mutex_init_recursive(&small_id_mutex);
        mono_counters_register ("Hazardous pointers", MONO_COUNTER_JIT | MONO_COUNTER_INT, &hazardous_pointer_count);
 
        for (i = 0; i < HAZARD_TABLE_OVERFLOW; ++i) {