static MonoClassField *wait_handle_os_handle_field = NULL;
/* Controls access to the 'threads' hash table */
+#define mono_threads_lock() EnterCriticalSection (&threads_mutex)
+#define mono_threads_unlock() LeaveCriticalSection (&threads_mutex)
static CRITICAL_SECTION threads_mutex;
/* Controls access to context static data */
+#define mono_contexts_lock() EnterCriticalSection (&contexts_mutex)
+#define mono_contexts_unlock() LeaveCriticalSection (&contexts_mutex)
static CRITICAL_SECTION contexts_mutex;
/* Holds current status of static data heap */
static void mono_thread_start (MonoThread *thread);
/* Spin lock for InterlockedXXX 64 bit functions */
+#define mono_interlocked_lock() EnterCriticalSection (&interlocked_mutex)
+#define mono_interlocked_unlock() LeaveCriticalSection (&interlocked_mutex)
static CRITICAL_SECTION interlocked_mutex;
/* global count of thread interruptions requested */
*/
static void handle_store(MonoThread *thread)
{
- EnterCriticalSection(&threads_mutex);
+ mono_threads_lock ();
THREAD_DEBUG (g_message (G_GNUC_PRETTY_FUNCTION ": thread %p ID %"G_GSIZE_FORMAT, thread, (gsize)thread->tid));
*/
mono_g_hash_table_insert(threads, (gpointer)(gsize)(thread->tid),
thread);
- LeaveCriticalSection(&threads_mutex);
+
+ mono_threads_unlock ();
}
static void handle_remove(gsize tid)
{
THREAD_DEBUG (g_message (G_GNUC_PRETTY_FUNCTION ": thread ID %"G_GSIZE_FORMAT, tid));
- EnterCriticalSection(&threads_mutex);
+ mono_threads_lock ();
+
if (threads)
mono_g_hash_table_remove (threads, (gpointer)tid);
- LeaveCriticalSection(&threads_mutex);
+ mono_threads_unlock ();
/* Don't close the handle here, wait for the object finalizer
* to do it. Otherwise, the following race condition applies:
{
guint32 (*start_func)(void *);
struct StartInfo *start_info;
- MonoMethod *im;
HANDLE thread;
gsize tid;
MONO_ARCH_SAVE_REGS;
- EnterCriticalSection(&interlocked_mutex);
+ mono_interlocked_lock ();
ret = ++ *location;
- LeaveCriticalSection(&interlocked_mutex);
+ mono_interlocked_unlock ();
return ret;
MONO_ARCH_SAVE_REGS;
- EnterCriticalSection(&interlocked_mutex);
+ mono_interlocked_lock ();
ret = -- *location;
- LeaveCriticalSection(&interlocked_mutex);
+ mono_interlocked_unlock ();
return ret;
}
* According to MSDN, this function is only atomic with regards to the
* other Interlocked functions on 32 bit platforms.
*/
- EnterCriticalSection(&interlocked_mutex);
+ mono_interlocked_lock ();
res = *location;
*location = value;
- LeaveCriticalSection(&interlocked_mutex);
+ mono_interlocked_unlock ();
return res;
#endif
* According to MSDN, this function is only atomic with regards to the
* other Interlocked functions on 32 bit platforms.
*/
- EnterCriticalSection(&interlocked_mutex);
+ mono_interlocked_lock ();
res = *location;
*location = value;
- LeaveCriticalSection(&interlocked_mutex);
+ mono_interlocked_unlock ();
return res;
#endif
#else
gdouble old;
- EnterCriticalSection(&interlocked_mutex);
+ mono_interlocked_lock ();
old = *location;
if (old == comparand)
*location = value;
- LeaveCriticalSection(&interlocked_mutex);
+ mono_interlocked_unlock ();
return old;
#endif
#else
gint64 old;
- EnterCriticalSection(&interlocked_mutex);
+ mono_interlocked_lock ();
old = *location;
if (old == comparand)
*location = value;
- LeaveCriticalSection(&interlocked_mutex);
+ mono_interlocked_unlock ();
return old;
#endif
#else
gint32 orig;
- EnterCriticalSection(&interlocked_mutex);
+ mono_interlocked_lock ();
orig = *location;
*location = orig + value;
- LeaveCriticalSection(&interlocked_mutex);
+ mono_interlocked_unlock ();
return orig;
#endif
#else
gint64 orig;
- EnterCriticalSection(&interlocked_mutex);
+ mono_interlocked_lock ();
orig = *location;
*location = orig + value;
- LeaveCriticalSection(&interlocked_mutex);
+ mono_interlocked_unlock ();
return orig;
#endif
#else
gint64 res;
- EnterCriticalSection(&interlocked_mutex);
+ mono_interlocked_lock ();
res = *location;
- LeaveCriticalSection(&interlocked_mutex);
+ mono_interlocked_unlock ();
return res;
#endif
}
+void
+ves_icall_System_Threading_Thread_MemoryBarrier (void)
+{
+ /* Should be implemented as a JIT intrinsic */
+ mono_raise_exception (mono_get_exception_not_implemented (NULL));
+}
+
void
ves_icall_System_Threading_Thread_ClrState (MonoThread* this, guint32 state)
{
mono_monitor_enter (thread->synch_lock);
if ((thread->state & ThreadState_AbortRequested) != 0 ||
- (thread->state & ThreadState_StopRequested) != 0)
+ (thread->state & ThreadState_StopRequested) != 0 ||
+ (thread->state & ThreadState_Stopped) != 0)
{
mono_monitor_exit (thread->synch_lock);
return;
if (ret < wait->num) {
gsize tid = wait->threads[ret]->tid;
- EnterCriticalSection (&threads_mutex);
+ mono_threads_lock ();
if (mono_g_hash_table_lookup (threads, (gpointer)tid)!=NULL) {
/* See comment in wait_for_tids about thread cleanup */
- LeaveCriticalSection (&threads_mutex);
+ mono_threads_unlock ();
THREAD_DEBUG (g_message (G_GNUC_PRETTY_FUNCTION
": cleaning up after thread %"G_GSIZE_FORMAT, tid));
thread_cleanup (wait->threads [ret]);
- }
- else
- LeaveCriticalSection (&threads_mutex);
+ } else
+ mono_threads_unlock ();
}
}
MonoThread *thread = (MonoThread *) value;
HANDLE handle;
+ if (wait->num >= MAXIMUM_WAIT_OBJECTS)
+ return FALSE;
+
/* The finalizer thread is not a background thread */
if (thread->tid != self && (thread->state & ThreadState_Background) != 0) {
return(TRUE);
}
+ /* printf ("A: %d\n", wait->num); */
wait->handles[wait->num]=thread->handle;
wait->threads[wait->num]=thread;
wait->num++;
/* join each thread that's still running */
THREAD_DEBUG (g_message (G_GNUC_PRETTY_FUNCTION ": Joining each running thread..."));
- EnterCriticalSection (&threads_mutex);
+ mono_threads_lock ();
if(threads==NULL) {
THREAD_DEBUG (g_message(G_GNUC_PRETTY_FUNCTION ": No threads"));
- LeaveCriticalSection (&threads_mutex);
+ mono_threads_unlock ();
return;
}
- LeaveCriticalSection (&threads_mutex);
+ mono_threads_unlock ();
do {
- EnterCriticalSection (&threads_mutex);
+ mono_threads_lock ();
THREAD_DEBUG (g_message (G_GNUC_PRETTY_FUNCTION
":There are %d threads to join", mono_g_hash_table_size (threads));
mono_g_hash_table_foreach (threads, print_tids, NULL));
ResetEvent (background_change_event);
wait->num=0;
mono_g_hash_table_foreach (threads, build_wait_tids, wait);
- LeaveCriticalSection (&threads_mutex);
+ mono_threads_unlock ();
if(wait->num>0) {
/* Something to wait for */
wait_for_tids_or_state_change (wait, INFINITE);
THREAD_DEBUG (g_message ("threadpool cleanup"));
mono_thread_pool_cleanup ();
- EnterCriticalSection(&threads_mutex);
-
/*
* Remove everything but the finalizer thread and self.
* Also abort all the background threads
* */
- wait->num = 0;
- mono_g_hash_table_foreach_remove (threads, remove_and_abort_threads, wait);
+ do {
+ mono_threads_lock ();
- LeaveCriticalSection(&threads_mutex);
+ wait->num = 0;
+ mono_g_hash_table_foreach_remove (threads, remove_and_abort_threads, wait);
- THREAD_DEBUG (g_message ("wait->num is now %d", wait->num));
- if(wait->num>0) {
- /* Something to wait for */
- wait_for_tids (wait, INFINITE);
- }
+ mono_threads_unlock ();
+
+ THREAD_DEBUG (g_message ("wait->num is now %d", wait->num));
+ if(wait->num>0) {
+ /* Something to wait for */
+ wait_for_tids (wait, INFINITE);
+ }
+ } while (wait->num > 0);
+
/*
* give the subthreads a chance to really quit (this is mainly needed
* to get correct user and system times from getrusage/wait/time(1)).
{
gsize self = GetCurrentThreadId ();
- EnterCriticalSection (&threads_mutex);
+ mono_threads_lock ();
THREAD_DEBUG (g_message (G_GNUC_PRETTY_FUNCTION ":There are %d threads to abort",
mono_g_hash_table_size (threads));
mono_g_hash_table_foreach (threads, print_tids, NULL));
mono_g_hash_table_foreach (threads, terminate_thread, (gpointer)self);
- LeaveCriticalSection (&threads_mutex);
+ mono_threads_unlock ();
}
static void
* Make a copy of the hashtable since we can't do anything with
* threads while threads_mutex is held.
*/
- EnterCriticalSection (&threads_mutex);
+ mono_threads_lock ();
mono_g_hash_table_foreach (threads, collect_threads, wait);
- LeaveCriticalSection (&threads_mutex);
+ mono_threads_unlock ();
events = g_new0 (gpointer, wait->num);
waitnum = 0;
if (thread) {
/* printf ("PUSH REF: %"G_GSIZE_FORMAT" -> %s.\n", (gsize)thread->tid, domain->friendly_name); */
- EnterCriticalSection (&threads_mutex);
+ mono_threads_lock ();
thread->appdomain_refs = g_slist_prepend (thread->appdomain_refs, domain);
- LeaveCriticalSection (&threads_mutex);
+ mono_threads_unlock ();
}
}
if (thread) {
/* printf ("POP REF: %"G_GSIZE_FORMAT" -> %s.\n", (gsize)thread->tid, ((MonoDomain*)(thread->appdomain_refs->data))->friendly_name); */
- EnterCriticalSection (&threads_mutex);
+ mono_threads_lock ();
/* FIXME: How can the list be empty ? */
if (thread->appdomain_refs)
thread->appdomain_refs = g_slist_remove (thread->appdomain_refs, thread->appdomain_refs->data);
- LeaveCriticalSection (&threads_mutex);
+ mono_threads_unlock ();
}
}
mono_thread_has_appdomain_ref (MonoThread *thread, MonoDomain *domain)
{
gboolean res;
- EnterCriticalSection (&threads_mutex);
+ mono_threads_lock ();
res = g_slist_find (thread->appdomain_refs, domain) != NULL;
- LeaveCriticalSection (&threads_mutex);
+ mono_threads_unlock ();
return res;
}
start_time = GetTickCount ();
do {
- EnterCriticalSection (&threads_mutex);
+ mono_threads_lock ();
user_data.domain = domain;
user_data.wait.num = 0;
mono_g_hash_table_foreach (threads, abort_appdomain_thread, &user_data);
- LeaveCriticalSection (&threads_mutex);
+ mono_threads_unlock ();
if (user_data.wait.num > 0)
/*
void
mono_threads_clear_cached_culture (MonoDomain *domain)
{
- EnterCriticalSection (&threads_mutex);
+ mono_threads_lock ();
mono_g_hash_table_foreach (threads, clear_cached_culture, domain);
- LeaveCriticalSection (&threads_mutex);
+ mono_threads_unlock ();
}
/*
{
guint32 offset;
- EnterCriticalSection (&threads_mutex);
+ mono_threads_lock ();
if (thread_static_info.offset || thread_static_info.idx > 0) {
/* get the current allocated size */
offset = thread_static_info.offset | ((thread_static_info.idx + 1) << 24);
mono_alloc_static_data (&(thread->static_data), offset);
}
- LeaveCriticalSection (&threads_mutex);
+ mono_threads_unlock ();
}
static void
guint32 offset;
if (static_type == SPECIAL_STATIC_THREAD)
{
- EnterCriticalSection (&threads_mutex);
+ mono_threads_lock ();
offset = mono_alloc_static_data_slot (&thread_static_info, size, align);
/* This can be called during startup */
if (threads != NULL)
mono_g_hash_table_foreach (threads, alloc_thread_static_data_helper, GUINT_TO_POINTER (offset));
- LeaveCriticalSection (&threads_mutex);
+ mono_threads_unlock ();
}
else
{
g_assert (static_type == SPECIAL_STATIC_CONTEXT);
- EnterCriticalSection (&contexts_mutex);
+ mono_contexts_lock ();
offset = mono_alloc_static_data_slot (&context_static_info, size, align);
- LeaveCriticalSection (&contexts_mutex);
+ mono_contexts_unlock ();
offset |= 0x80000000; /* Set the high bit to indicate context static data */
}
return offset;
*/
MonoAppContext *context = mono_context_get ();
if (!context->static_data || !context->static_data [idx]) {
- EnterCriticalSection (&contexts_mutex);
+ mono_contexts_lock ();
mono_alloc_static_data (&(context->static_data), offset);
- LeaveCriticalSection (&contexts_mutex);
+ mono_contexts_unlock ();
}
return ((char*) context->static_data [idx]) + (offset & 0xffffff);
}
LIBGC_DEBUG (g_message (G_GNUC_PRETTY_FUNCTION ": %"G_GSIZE_FORMAT" - %p", self, threads));
- EnterCriticalSection (&threads_mutex);
+ mono_threads_lock ();
if (threads != NULL)
mono_g_hash_table_foreach (threads, gc_stop_world, (gpointer)self);
- LeaveCriticalSection (&threads_mutex);
+ mono_threads_unlock ();
}
static void gc_start_world (gpointer key, gpointer value, gpointer user)
LIBGC_DEBUG (g_message (G_GNUC_PRETTY_FUNCTION ": %"G_GSIZE_FORMAT" - %p", self, threads));
- EnterCriticalSection (&threads_mutex);
+ mono_threads_lock ();
if (threads != NULL)
mono_g_hash_table_foreach (threads, gc_start_world, (gpointer)self);
- LeaveCriticalSection (&threads_mutex);
+ mono_threads_unlock ();
}
#ifdef __MINGW32__
LIBGC_DEBUG (g_message (G_GNUC_PRETTY_FUNCTION ": %"G_GSIZE_FORMAT" - %p", self, threads));
- EnterCriticalSection (&threads_mutex);
+ mono_threads_lock ();
if (threads != NULL)
mono_g_hash_table_foreach (threads, gc_push_all_stacks, &self);
- LeaveCriticalSection (&threads_mutex);
+ mono_threads_unlock ();
}
#endif /* WITH_INCLUDED_LIBGC */