#include <mono/metadata/monitor.h>
#include <mono/metadata/threads-types.h>
#include <mono/metadata/exception.h>
+#include <mono/metadata/threads.h>
#include <mono/io-layer/io-layer.h>
+#include <mono/metadata/object-internals.h>
+#include <mono/metadata/gc-internal.h>
-#include <mono/os/gc_wrapper.h>
-
-#undef THREAD_LOCK_DEBUG
+/*#define LOCK_DEBUG(a) do { a; } while (0)*/
+#define LOCK_DEBUG(a)
/*
* The monitor implementation here is based on
* an object.
*/
+struct _MonoThreadsSync
+{
+ gsize owner; /* thread ID */
+ guint32 nest;
+ volatile guint32 entry_count;
+ HANDLE entry_sem;
+ GSList *wait_list;
+ void *data;
+};
+
+typedef struct _MonitorArray MonitorArray;
+
+struct _MonitorArray {
+ MonitorArray *next;
+ int num_monitors;
+ MonoThreadsSync monitors [MONO_ZERO_LEN_ARRAY];
+};
+
+static CRITICAL_SECTION monitor_mutex;
+static MonoThreadsSync *monitor_freelist;
+static MonitorArray *monitor_allocated;
+static int array_size = 16;
+
+void
+mono_monitor_init (void)
+{
+ InitializeCriticalSection (&monitor_mutex);
+}
-static void mon_finalize (void *o, void *unused)
+/* LOCKING: this is called with monitor_mutex held */
+static void
+mon_finalize (MonoThreadsSync *mon)
{
- MonoThreadsSync *mon=(MonoThreadsSync *)o;
-
-#ifdef THREAD_LOCK_DEBUG
- g_message (G_GNUC_PRETTY_FUNCTION ": Finalizing sync %p", mon);
-#endif
+ LOCK_DEBUG (g_message (G_GNUC_PRETTY_FUNCTION ": Finalizing sync %p", mon));
- if(mon->entry_sem!=NULL) {
+ if (mon->entry_sem != NULL) {
CloseHandle (mon->entry_sem);
+ mon->entry_sem = NULL;
}
/* If this isn't empty then something is seriously broken - it
* means a thread is still waiting on the object that owned
* this lock, but the object has been finalized.
*/
- g_assert (mon->wait_list==NULL);
+ g_assert (mon->wait_list == NULL);
+
+ mon->entry_count = 0;
+ /* owner and nest are set in mon_new, no need to zero them out */
+
+ mon->data = monitor_freelist;
+ monitor_freelist = mon;
}
-static MonoThreadsSync *mon_new(guint32 id)
+/* LOCKING: this is called with monitor_mutex held */
+static MonoThreadsSync *
+mon_new (gsize id)
{
MonoThreadsSync *new;
+
+ if (!monitor_freelist) {
+ MonitorArray *marray;
+ int i;
+ /* see if any sync block has been collected */
+ new = NULL;
+ for (marray = monitor_allocated; marray; marray = marray->next) {
+ for (i = 0; i < marray->num_monitors; ++i) {
+ if (marray->monitors [i].data == NULL) {
+ new = &marray->monitors [i];
+ new->data = monitor_freelist;
+ monitor_freelist = new;
+ }
+ }
+ /* small perf tweak to avoid scanning all the blocks */
+ if (new)
+ break;
+ }
+ /* need to allocate a new array of monitors */
+ if (!monitor_freelist) {
+ MonitorArray *last;
+ LOCK_DEBUG (g_message (G_GNUC_PRETTY_FUNCTION ": allocating more monitors: %d", array_size));
+ marray = g_malloc0 (sizeof (MonoArray) + array_size * sizeof (MonoThreadsSync));
+ marray->num_monitors = array_size;
+ array_size *= 2;
+ /* link into the freelist */
+ for (i = 0; i < marray->num_monitors - 1; ++i) {
+ marray->monitors [i].data = &marray->monitors [i + 1];
+ }
+ marray->monitors [i].data = NULL; /* the last one */
+ monitor_freelist = &marray->monitors [0];
+ /* we happend the marray instead of prepending so that
+ * the collecting loop above will need to scan smaller arrays first
+ */
+ if (!monitor_allocated) {
+ monitor_allocated = marray;
+ } else {
+ last = monitor_allocated;
+ while (last->next)
+ last = last->next;
+ last->next = marray;
+ }
+ }
+ }
+
+ new = monitor_freelist;
+ monitor_freelist = new->data;
+
+ new->owner = id;
+ new->nest = 1;
-#if HAVE_BOEHM_GC
- new=(MonoThreadsSync *)GC_MALLOC (sizeof(MonoThreadsSync));
- GC_REGISTER_FINALIZER (new, mon_finalize, NULL, NULL, NULL);
-#else
- /* This should be freed when the object that owns it is
- * deleted
- */
- new=(MonoThreadsSync *)g_new0 (MonoThreadsSync, 1);
-#endif
- new->owner=id;
- new->nest=1;
-
- return(new);
+ return new;
}
-gboolean mono_monitor_try_enter (MonoObject *obj, guint32 ms)
+/* If allow_interruption==TRUE, the method will be interrumped if abort or suspend
+ * is requested. In this case it returns -1.
+ */
+static gint32
+mono_monitor_try_enter_internal (MonoObject *obj, guint32 ms, gboolean allow_interruption)
{
MonoThreadsSync *mon;
- guint32 id=GetCurrentThreadId ();
+ gsize id = GetCurrentThreadId ();
HANDLE sem;
- guint32 then, now, delta;
+ guint32 then = 0, now, delta;
guint32 waitms;
guint32 ret;
-#ifdef THREAD_LOCK_DEBUG
- g_message(G_GNUC_PRETTY_FUNCTION
- ": (%d) Trying to lock object %p (%d ms)", id, obj, ms);
-#endif
+ LOCK_DEBUG (g_message(G_GNUC_PRETTY_FUNCTION
+ ": (%d) Trying to lock object %p (%d ms)", id, obj, ms));
retry:
- mon=obj->synchronisation;
+ mon = obj->synchronisation;
/* If the object has never been locked... */
- if(mon==NULL) {
- mon=mon_new(id);
- if(InterlockedCompareExchangePointer ((gpointer*)&obj->synchronisation, mon, NULL)==NULL) {
+ if (mon == NULL) {
+ EnterCriticalSection (&monitor_mutex);
+ mon = mon_new (id);
+ if (InterlockedCompareExchangePointer ((gpointer*)&obj->synchronisation, mon, NULL) == NULL) {
+ mono_gc_weak_link_add (&mon->data, obj);
+ LeaveCriticalSection (&monitor_mutex);
/* Successfully locked */
- return(TRUE);
+ return 1;
} else {
- /* Another thread got in first, so try again.
- * GC will take care of the monitor record
- */
-#ifndef HAVE_BOEHM_GC
- mon_finalize (mon, NULL);
-#endif
+ mon_finalize (mon);
+ LeaveCriticalSection (&monitor_mutex);
goto retry;
}
}
/* If the object is currently locked by this thread... */
- if(mon->owner==id) {
+ if (mon->owner == id) {
mon->nest++;
- return(TRUE);
+ return 1;
}
/* If the object has previously been locked but isn't now... */
/* This case differs from Dice's case 3 because we don't
* deflate locks or cache unused lock records
*/
- if(mon->owner==0) {
+ if (mon->owner == 0) {
/* Try to install our ID in the owner field, nest
* should have been left at 1 by the previous unlock
* operation
*/
- if(InterlockedCompareExchange (&mon->owner, id, 0)==0) {
+ if (InterlockedCompareExchangePointer ((gpointer *)&mon->owner, (gpointer)id, 0) == 0) {
/* Success */
- g_assert (mon->nest==1);
- return(TRUE);
+ g_assert (mon->nest == 1);
+ return 1;
} else {
/* Trumped again! */
goto retry;
/* The object must be locked by someone else... */
/* If ms is 0 we don't block, but just fail straight away */
- if(ms==0) {
-#ifdef THREAD_LOCK_DEBUG
- g_message (G_GNUC_PRETTY_FUNCTION
- ": (%d) timed out, returning FALSE", id);
-#endif
-
- return(FALSE);
+ if (ms == 0) {
+ LOCK_DEBUG (g_message (G_GNUC_PRETTY_FUNCTION ": (%d) timed out, returning FALSE", id));
+ return 0;
}
/* The slow path begins here. We need to make sure theres a
* semaphore handle (creating it if necessary), and block on
* it
*/
- if(mon->entry_sem==NULL) {
+ if (mon->entry_sem == NULL) {
/* Create the semaphore */
- sem=CreateSemaphore (NULL, 0, 0x7fffffff, NULL);
- if(InterlockedCompareExchangePointer ((gpointer*)&mon->entry_sem, sem, NULL)!=NULL) {
+ sem = CreateSemaphore (NULL, 0, 0x7fffffff, NULL);
+ if (InterlockedCompareExchangePointer ((gpointer*)&mon->entry_sem, sem, NULL) != NULL) {
/* Someone else just put a handle here */
CloseHandle (sem);
}
* handle technique from pulse/wait would involve locking the
* lock struct and therefore slowing down the fast path.
*/
- if(ms!=INFINITE) {
- then=GetTickCount ();
- if(ms<100) {
- waitms=ms;
+ if (ms != INFINITE) {
+ then = GetTickCount ();
+ if (ms < 100) {
+ waitms = ms;
} else {
- waitms=100;
+ waitms = 100;
}
} else {
- waitms=100;
+ waitms = 100;
}
InterlockedIncrement (&mon->entry_count);
- ret=WaitForSingleObjectEx (mon->entry_sem, waitms, TRUE);
+ ret = WaitForSingleObjectEx (mon->entry_sem, waitms, allow_interruption);
InterlockedDecrement (&mon->entry_count);
- if(ms!=INFINITE) {
- now=GetTickCount ();
+ if (ms != INFINITE) {
+ now = GetTickCount ();
- if(now<then) {
+ if (now < then) {
/* The counter must have wrapped around */
-#ifdef THREAD_LOCK_DEBUG
- g_message (G_GNUC_PRETTY_FUNCTION
- ": wrapped around! now=0x%x then=0x%x",
- now, then);
-#endif
+ LOCK_DEBUG (g_message (G_GNUC_PRETTY_FUNCTION
+ ": wrapped around! now=0x%x then=0x%x", now, then));
- now+=(0xffffffff - then);
- then=0;
+ now += (0xffffffff - then);
+ then = 0;
-#ifdef THREAD_LOCK_DEBUG
- g_message (G_GNUC_PRETTY_FUNCTION ": wrap rejig: now=0x%x then=0x%x delta=0x%x", now, then, now-then);
-#endif
+ LOCK_DEBUG (g_message (G_GNUC_PRETTY_FUNCTION ": wrap rejig: now=0x%x then=0x%x delta=0x%x", now, then, now-then));
}
- delta=now-then;
- if(delta >= ms) {
- ms=0;
+ delta = now - then;
+ if (delta >= ms) {
+ ms = 0;
} else {
- ms-=delta;
+ ms -= delta;
}
- if(ret==WAIT_TIMEOUT && ms>0) {
+ if ((ret == WAIT_TIMEOUT || (ret == WAIT_IO_COMPLETION && !allow_interruption)) && ms > 0) {
/* More time left */
goto retry;
}
} else {
- if(ret==WAIT_TIMEOUT) {
+ if (ret == WAIT_TIMEOUT || (ret == WAIT_IO_COMPLETION && !allow_interruption)) {
/* Infinite wait, so just try again */
goto retry;
}
}
- if(ret==WAIT_OBJECT_0) {
+ if (ret == WAIT_OBJECT_0) {
/* retry from the top */
goto retry;
}
/* We must have timed out */
-#ifdef THREAD_LOCK_DEBUG
- g_message (G_GNUC_PRETTY_FUNCTION
- ": (%d) timed out waiting, returning FALSE", id);
-#endif
+ LOCK_DEBUG (g_message (G_GNUC_PRETTY_FUNCTION ": (%d) timed out waiting, returning FALSE", id));
- return(FALSE);
+ if (ret == WAIT_IO_COMPLETION)
+ return -1;
+ else
+ return 0;
+}
+
+gboolean
+mono_monitor_enter (MonoObject *obj)
+{
+ return mono_monitor_try_enter_internal (obj, INFINITE, FALSE) == 1;
+}
+
+gboolean
+mono_monitor_try_enter (MonoObject *obj, guint32 ms)
+{
+ return mono_monitor_try_enter_internal (obj, ms, FALSE) == 1;
}
-void mono_monitor_exit (MonoObject *obj)
+void
+mono_monitor_exit (MonoObject *obj)
{
MonoThreadsSync *mon;
guint32 nest;
-#ifdef THREAD_LOCK_DEBUG
- g_message(G_GNUC_PRETTY_FUNCTION ": (%d) Unlocking %p",
- GetCurrentThreadId (), obj);
-#endif
+ LOCK_DEBUG (g_message (G_GNUC_PRETTY_FUNCTION ": (%d) Unlocking %p", GetCurrentThreadId (), obj));
- mon=obj->synchronisation;
+ mon = obj->synchronisation;
- if(mon==NULL) {
- mono_raise_exception (mono_get_exception_synchronization_lock ("Not locked"));
+ if (mon == NULL) {
+ /* No one ever used Enter. Just ignore the Exit request as MS does */
return;
}
- if(mon->owner!=GetCurrentThreadId ()) {
+ if (mon->owner != GetCurrentThreadId ()) {
return;
}
- nest=mon->nest-1;
- if(nest==0) {
-#ifdef THREAD_LOCK_DEBUG
- g_message(G_GNUC_PRETTY_FUNCTION
- ": (%d) Object %p is now unlocked",
- GetCurrentThreadId (), obj);
-#endif
+ nest = mon->nest - 1;
+ if (nest == 0) {
+ LOCK_DEBUG (g_message (G_GNUC_PRETTY_FUNCTION
+ ": (%d) Object %p is now unlocked", GetCurrentThreadId (), obj));
/* object is now unlocked, leave nest==1 so we don't
* need to set it when the lock is reacquired
*/
- mon->owner=0;
+ mon->owner = 0;
/* Do the wakeup stuff. It's possible that the last
* blocking thread gave up waiting just before we
* it means we don't have to waste time locking the
* struct.
*/
- if(mon->entry_count>0) {
+ if (mon->entry_count > 0) {
ReleaseSemaphore (mon->entry_sem, 1, NULL);
}
} else {
-#ifdef THREAD_LOCK_DEBUG
- g_message(G_GNUC_PRETTY_FUNCTION
- ": (%d) Object %p is now locked %d times",
- GetCurrentThreadId (), obj,
- nest);
-#endif
- mon->nest=nest;
+ LOCK_DEBUG (g_message (G_GNUC_PRETTY_FUNCTION
+ ": (%d) Object %p is now locked %d times", GetCurrentThreadId (), obj, nest));
+ mon->nest = nest;
}
}
-gboolean ves_icall_System_Threading_Monitor_Monitor_try_enter(MonoObject *obj,
- guint32 ms)
+gboolean
+ves_icall_System_Threading_Monitor_Monitor_try_enter (MonoObject *obj, guint32 ms)
{
- MONO_ARCH_SAVE_REGS;
+ gint32 res;
- return(mono_monitor_try_enter (obj, ms));
+ do {
+ res = mono_monitor_try_enter_internal (obj, ms, TRUE);
+ if (res == -1)
+ mono_thread_interruption_checkpoint ();
+ } while (res == -1);
+
+ return res == 1;
}
-void ves_icall_System_Threading_Monitor_Monitor_exit(MonoObject *obj)
+void
+ves_icall_System_Threading_Monitor_Monitor_exit (MonoObject *obj)
{
- MONO_ARCH_SAVE_REGS;
-
mono_monitor_exit (obj);
}
-gboolean ves_icall_System_Threading_Monitor_Monitor_test_owner(MonoObject *obj)
+gboolean
+ves_icall_System_Threading_Monitor_Monitor_test_owner (MonoObject *obj)
{
MonoThreadsSync *mon;
- MONO_ARCH_SAVE_REGS;
+ LOCK_DEBUG (g_message (G_GNUC_PRETTY_FUNCTION
+ ": Testing if %p is owned by thread %d", obj, GetCurrentThreadId()));
-#ifdef THREAD_LOCK_DEBUG
- g_message(G_GNUC_PRETTY_FUNCTION
- ": Testing if %p is owned by thread %d", obj,
- GetCurrentThreadId());
-#endif
-
- mon=obj->synchronisation;
- if(mon==NULL) {
- return(FALSE);
+ mon = obj->synchronisation;
+ if (mon == NULL) {
+ return FALSE;
}
if(mon->owner==GetCurrentThreadId ()) {
return(FALSE);
}
-gboolean ves_icall_System_Threading_Monitor_Monitor_test_synchronised(MonoObject *obj)
+gboolean
+ves_icall_System_Threading_Monitor_Monitor_test_synchronised (MonoObject *obj)
{
MonoThreadsSync *mon;
-
- MONO_ARCH_SAVE_REGS;
-#ifdef THREAD_LOCK_DEBUG
- g_message(G_GNUC_PRETTY_FUNCTION
- ": (%d) Testing if %p is owned by any thread",
- GetCurrentThreadId (), obj);
-#endif
+ LOCK_DEBUG (g_message(G_GNUC_PRETTY_FUNCTION
+ ": (%d) Testing if %p is owned by any thread", GetCurrentThreadId (), obj));
- mon=obj->synchronisation;
- if(mon==NULL) {
- return(FALSE);
+ mon = obj->synchronisation;
+ if (mon == NULL) {
+ return FALSE;
}
- if(mon->owner!=0) {
- return(TRUE);
+ if (mon->owner != 0) {
+ return TRUE;
}
- return(FALSE);
+ return FALSE;
}
/* All wait list manipulation in the pulse, pulseall and wait
* any extra struct locking
*/
-void ves_icall_System_Threading_Monitor_Monitor_pulse(MonoObject *obj)
+void
+ves_icall_System_Threading_Monitor_Monitor_pulse (MonoObject *obj)
{
MonoThreadsSync *mon;
- MONO_ARCH_SAVE_REGS;
-
-#ifdef THREAD_LOCK_DEBUG
- g_message(G_GNUC_PRETTY_FUNCTION ": (%d) Pulsing %p",
- GetCurrentThreadId (), obj);
-#endif
+ LOCK_DEBUG (g_message (G_GNUC_PRETTY_FUNCTION ": (%d) Pulsing %p",
+ GetCurrentThreadId (), obj));
- mon=obj->synchronisation;
- if(mon==NULL) {
+ mon = obj->synchronisation;
+ if (mon == NULL) {
mono_raise_exception (mono_get_exception_synchronization_lock ("Not locked"));
return;
}
- if(mon->owner!=GetCurrentThreadId ()) {
+ if (mon->owner != GetCurrentThreadId ()) {
mono_raise_exception (mono_get_exception_synchronization_lock ("Not locked by this thread"));
return;
}
-#ifdef THREAD_LOCK_DEBUG
- g_message(G_GNUC_PRETTY_FUNCTION ": (%d) %d threads waiting",
- GetCurrentThreadId (), g_slist_length (mon->wait_list));
-#endif
+ LOCK_DEBUG (g_message (G_GNUC_PRETTY_FUNCTION ": (%d) %d threads waiting",
+ GetCurrentThreadId (), g_slist_length (mon->wait_list)));
- if(mon->wait_list!=NULL) {
-#ifdef THREAD_LOCK_DEBUG
- g_message(G_GNUC_PRETTY_FUNCTION
+ if (mon->wait_list != NULL) {
+ LOCK_DEBUG (g_message (G_GNUC_PRETTY_FUNCTION
": (%d) signalling and dequeuing handle %p",
- GetCurrentThreadId (), mon->wait_list->data);
-#endif
+ GetCurrentThreadId (), mon->wait_list->data));
SetEvent (mon->wait_list->data);
- mon->wait_list=g_slist_remove (mon->wait_list,
- mon->wait_list->data);
+ mon->wait_list = g_slist_remove (mon->wait_list, mon->wait_list->data);
}
}
-void ves_icall_System_Threading_Monitor_Monitor_pulse_all(MonoObject *obj)
+void
+ves_icall_System_Threading_Monitor_Monitor_pulse_all (MonoObject *obj)
{
MonoThreadsSync *mon;
- MONO_ARCH_SAVE_REGS;
+ LOCK_DEBUG (g_message(G_GNUC_PRETTY_FUNCTION ": (%d) Pulsing all %p",
+ GetCurrentThreadId (), obj));
-#ifdef THREAD_LOCK_DEBUG
- g_message(G_GNUC_PRETTY_FUNCTION ": (%d) Pulsing all %p",
- GetCurrentThreadId (), obj);
-#endif
-
- mon=obj->synchronisation;
- if(mon==NULL) {
+ mon = obj->synchronisation;
+ if (mon == NULL) {
mono_raise_exception (mono_get_exception_synchronization_lock ("Not locked"));
return;
}
- if(mon->owner!=GetCurrentThreadId ()) {
+ if (mon->owner != GetCurrentThreadId ()) {
mono_raise_exception (mono_get_exception_synchronization_lock ("Not locked by this thread"));
return;
}
-#ifdef THREAD_LOCK_DEBUG
- g_message(G_GNUC_PRETTY_FUNCTION ": (%d) %d threads waiting",
- GetCurrentThreadId (), g_slist_length (mon->wait_list));
-#endif
-
- while(mon->wait_list!=NULL) {
-#ifdef THREAD_LOCK_DEBUG
- g_message(G_GNUC_PRETTY_FUNCTION
+ LOCK_DEBUG (g_message (G_GNUC_PRETTY_FUNCTION ": (%d) %d threads waiting",
+ GetCurrentThreadId (), g_slist_length (mon->wait_list)));
+
+ while (mon->wait_list != NULL) {
+ LOCK_DEBUG (g_message (G_GNUC_PRETTY_FUNCTION
": (%d) signalling and dequeuing handle %p",
- GetCurrentThreadId (), mon->wait_list->data);
-#endif
+ GetCurrentThreadId (), mon->wait_list->data));
SetEvent (mon->wait_list->data);
- mon->wait_list=g_slist_remove (mon->wait_list,
- mon->wait_list->data);
+ mon->wait_list = g_slist_remove (mon->wait_list, mon->wait_list->data);
}
}
-gboolean ves_icall_System_Threading_Monitor_Monitor_wait(MonoObject *obj,
- guint32 ms)
+gboolean
+ves_icall_System_Threading_Monitor_Monitor_wait (MonoObject *obj, guint32 ms)
{
MonoThreadsSync *mon;
HANDLE event;
guint32 nest;
guint32 ret;
- gboolean success=FALSE, regain;
-
- MONO_ARCH_SAVE_REGS;
+ gboolean success = FALSE;
+ gint32 regain;
+ MonoThread *thread = mono_thread_current ();
-#ifdef THREAD_LOCK_DEBUG
- g_message(G_GNUC_PRETTY_FUNCTION
+ LOCK_DEBUG (g_message (G_GNUC_PRETTY_FUNCTION
": (%d) Trying to wait for %p with timeout %dms",
- GetCurrentThreadId (), obj, ms);
-#endif
+ GetCurrentThreadId (), obj, ms));
- mon=obj->synchronisation;
- if(mon==NULL) {
+ mon = obj->synchronisation;
+ if (mon == NULL) {
mono_raise_exception (mono_get_exception_synchronization_lock ("Not locked"));
- return(FALSE);
+ return FALSE;
}
- if(mon->owner!=GetCurrentThreadId ()) {
+ if (mon->owner != GetCurrentThreadId ()) {
mono_raise_exception (mono_get_exception_synchronization_lock ("Not locked by this thread"));
- return(FALSE);
+ return FALSE;
}
- event=CreateEvent (NULL, FALSE, FALSE, NULL);
- if(event==NULL) {
+ event = CreateEvent (NULL, FALSE, FALSE, NULL);
+ if (event == NULL) {
mono_raise_exception (mono_get_exception_synchronization_lock ("Failed to set up wait event"));
- return(FALSE);
+ return FALSE;
}
-#ifdef THREAD_LOCK_DEBUG
- g_message(G_GNUC_PRETTY_FUNCTION ": (%d) queuing handle %p",
- GetCurrentThreadId (), event);
-#endif
+ LOCK_DEBUG (g_message (G_GNUC_PRETTY_FUNCTION ": (%d) queuing handle %p",
+ GetCurrentThreadId (), event));
- mon->wait_list=g_slist_append (mon->wait_list, event);
+ mono_monitor_enter (thread->synch_lock);
+ thread->state |= ThreadState_WaitSleepJoin;
+ mono_monitor_exit (thread->synch_lock);
+
+ mon->wait_list = g_slist_append (mon->wait_list, event);
/* Save the nest count, and release the lock */
- nest=mon->nest;
- mon->nest=1;
+ nest = mon->nest;
+ mon->nest = 1;
mono_monitor_exit (obj);
-#ifdef THREAD_LOCK_DEBUG
- g_message(G_GNUC_PRETTY_FUNCTION ": (%d) Unlocked %p lock %p",
- GetCurrentThreadId (), obj, mon);
-#endif
+ LOCK_DEBUG (g_message (G_GNUC_PRETTY_FUNCTION ": (%d) Unlocked %p lock %p",
+ GetCurrentThreadId (), obj, mon));
/* There's no race between unlocking mon and waiting for the
* event, because auto reset events are sticky, and this event
* is private to this thread. Therefore even if the event was
* signalled before we wait, we still succeed.
*/
- ret=WaitForSingleObjectEx (event, ms, TRUE);
+ ret = WaitForSingleObjectEx (event, ms, TRUE);
+
+ /* Reset the thread state fairly early, so we don't have to worry
+ * about the monitor error checking
+ */
+ mono_monitor_enter (thread->synch_lock);
+ thread->state &= ~ThreadState_WaitSleepJoin;
+ mono_monitor_exit (thread->synch_lock);
if (mono_thread_interruption_requested ()) {
CloseHandle (event);
- return(FALSE);
+ return FALSE;
}
/* Regain the lock with the previous nest count */
- regain=mono_monitor_try_enter (obj, INFINITE);
-
- if(regain==FALSE) {
+ do {
+ regain = mono_monitor_try_enter_internal (obj, INFINITE, TRUE);
+ if (regain == -1)
+ mono_thread_interruption_checkpoint ();
+ } while (regain == -1);
+
+ if (regain == 0) {
/* Something went wrong, so throw a
* SynchronizationLockException
*/
CloseHandle (event);
mono_raise_exception (mono_get_exception_synchronization_lock ("Failed to regain lock"));
- return(FALSE);
+ return FALSE;
}
- mon->nest=nest;
+ mon->nest = nest;
-#ifdef THREAD_LOCK_DEBUG
- g_message(G_GNUC_PRETTY_FUNCTION ": (%d) Regained %p lock %p",
- GetCurrentThreadId (), obj, mon);
-#endif
+ LOCK_DEBUG (g_message (G_GNUC_PRETTY_FUNCTION ": (%d) Regained %p lock %p",
+ GetCurrentThreadId (), obj, mon));
- if(ret==WAIT_TIMEOUT) {
+ if (ret == WAIT_TIMEOUT) {
/* Poll the event again, just in case it was signalled
* while we were trying to regain the monitor lock
*/
- ret=WaitForSingleObjectEx (event, 0, FALSE);
+ ret = WaitForSingleObjectEx (event, 0, FALSE);
}
/* Pulse will have popped our event from the queue if it signalled
* thread.
*/
- if(ret==WAIT_OBJECT_0) {
-#ifdef THREAD_LOCK_DEBUG
- g_message(G_GNUC_PRETTY_FUNCTION ": (%d) Success",
- GetCurrentThreadId ());
-#endif
- success=TRUE;
+ if (ret == WAIT_OBJECT_0) {
+ LOCK_DEBUG (g_message (G_GNUC_PRETTY_FUNCTION ": (%d) Success",
+ GetCurrentThreadId ()));
+ success = TRUE;
} else {
-#ifdef THREAD_LOCK_DEBUG
- g_message(G_GNUC_PRETTY_FUNCTION ": (%d) Wait failed",
- GetCurrentThreadId ());
- g_message(G_GNUC_PRETTY_FUNCTION ": (%d) dequeuing handle %p",
- GetCurrentThreadId (), event);
-#endif
+ LOCK_DEBUG (g_message (G_GNUC_PRETTY_FUNCTION ": (%d) Wait failed, dequeuing handle %p",
+ GetCurrentThreadId (), event));
/* No pulse, so we have to remove ourself from the
* wait queue
*/
- mon->wait_list=g_slist_remove (mon->wait_list, event);
+ mon->wait_list = g_slist_remove (mon->wait_list, event);
}
CloseHandle (event);
- return(success);
+ return success;
}