Author: miguel
Date: 2005-04-27 18:11:04 -0400 (Wed, 27 Apr 2005)
New Revision: 43691
Modified:
branches/mono-1-1-6/mcs/class/corlib/System.Threading/Thread.cs
branches/mono-1-1-6/mono/mono/metadata/object-internals.h
branches/mono-1-1-6/mono/mono/metadata/threads.c
Log:
Exit patch
Modified: branches/mono-1-1-6/mcs/class/corlib/System.Threading/Thread.cs
===================================================================
--- branches/mono-1-1-6/mcs/class/corlib/System.Threading/Thread.cs
2005-04-27 22:05:12 UTC (rev 43690)
+++ branches/mono-1-1-6/mcs/class/corlib/System.Threading/Thread.cs
2005-04-27 22:11:04 UTC (rev 43691)
@@ -82,6 +82,7 @@
private IntPtr appdomain_refs;
private bool interruption_requested;
private IntPtr suspend_event;
+ private IntPtr suspended_event;
private IntPtr resume_event;
private object synch_lock = new Object();
private IntPtr serialized_culture_info;
Modified: branches/mono-1-1-6/mono/mono/metadata/object-internals.h
===================================================================
--- branches/mono-1-1-6/mono/mono/metadata/object-internals.h 2005-04-27
22:05:12 UTC (rev 43690)
+++ branches/mono-1-1-6/mono/mono/metadata/object-internals.h 2005-04-27
22:11:04 UTC (rev 43691)
@@ -239,6 +239,7 @@
GSList *appdomain_refs;
MonoBoolean interruption_requested;
gpointer suspend_event;
+ gpointer suspended_event;
gpointer resume_event;
MonoObject *synch_lock;
guint8* serialized_culture_info;
Modified: branches/mono-1-1-6/mono/mono/metadata/threads.c
===================================================================
--- branches/mono-1-1-6/mono/mono/metadata/threads.c 2005-04-27 22:05:12 UTC
(rev 43690)
+++ branches/mono-1-1-6/mono/mono/metadata/threads.c 2005-04-27 22:11:04 UTC
(rev 43691)
@@ -1798,13 +1798,22 @@
LeaveCriticalSection (&threads_mutex);
}
-static void suspend_thread (gpointer key, gpointer value, gpointer user)
+static void
+collect_threads (gpointer key, gpointer value, gpointer user_data)
{
- MonoThread *thread=(MonoThread *)value;
- guint32 self=GPOINTER_TO_UINT (user);
-
- if ((thread->tid!=self) && !mono_gc_is_finalizer_thread (thread))
- SuspendThread (thread->handle);
+ MonoThread *thread = (MonoThread*)value;
+ struct wait_data *wait = (struct wait_data*)user_data;
+ HANDLE handle;
+
+ if (wait->num<MAXIMUM_WAIT_OBJECTS) {
+ handle = OpenThread (THREAD_ALL_ACCESS, TRUE, thread->tid);
+ if (handle == NULL)
+ return;
+
+ wait->handles [wait->num] = handle;
+ wait->threads [wait->num] = thread;
+ wait->num++;
+ }
}
/*
@@ -1814,12 +1823,64 @@
*/
void mono_thread_suspend_all_other_threads (void)
{
- guint32 self=GetCurrentThreadId ();
+ struct wait_data *wait = g_new0 (struct wait_data, 1);
+ int i, waitnum;
+ guint32 self = GetCurrentThreadId ();
- EnterCriticalSection (&threads_mutex);
- mono_g_hash_table_foreach (threads, suspend_thread,
- GUINT_TO_POINTER (self));
- LeaveCriticalSection (&threads_mutex);
+ do {
+ /*
+ * Make a copy of the hashtable since we can't do anything with
+ * threads while threads_mutex is held.
+ */
+ EnterCriticalSection (&threads_mutex);
+ mono_g_hash_table_foreach (threads, collect_threads, wait);
+ LeaveCriticalSection (&threads_mutex);
+
+ waitnum = 0;
+ for (i = 0; i < wait->num; ++i) {
+ MonoThread *thread = wait->threads [i];
+
+ if ((thread->tid == self) ||
mono_gc_is_finalizer_thread (thread)) {
+ //CloseHandle (wait->handles [i]);
+ continue;
+ }
+
+ mono_monitor_enter (thread->synch_lock);
+
+ if ((thread->state & ThreadState_Suspended) != 0 ||
+ (thread->state & ThreadState_SuspendRequested)
!= 0 ||
+ (thread->state & ThreadState_StopRequested) !=
0 ||
+ (thread->state & ThreadState_Stopped) != 0) {
+ mono_monitor_exit (thread->synch_lock);
+ CloseHandle (wait->handles [i]);
+ continue;
+ }
+
+ thread->state |= ThreadState_SuspendRequested;
+
+ //printf ("S: %d\n", thread->state);
+ thread->suspended_event = CreateEvent (NULL, TRUE,
FALSE, NULL);
+
+ mono_monitor_exit (thread->synch_lock);
+
+ /* Signal the thread to suspend */
+ signal_thread_state_change (thread);
+
+ /* Wait for it to become suspended */
+ //printf ("W1\n");
+ WaitForSingleObject (thread->suspended_event, INFINITE);
+ //printf ("W2\n");
+
+ CloseHandle (thread->suspended_event);
+ thread->suspended_event = NULL;
+
+ CloseHandle (wait->handles [i]);
+
+ waitnum ++;
+ }
+ } while (waitnum > 0);
+
+ g_free (wait);
}
/*
@@ -2267,6 +2328,8 @@
thread->state &= ~ThreadState_SuspendRequested;
thread->state |= ThreadState_Suspended;
thread->suspend_event = CreateEvent (NULL, TRUE, FALSE, NULL);
+ if (thread->suspended_event)
+ SetEvent (thread->suspended_event);
mono_monitor_exit (thread->synch_lock);
WaitForSingleObject (thread->suspend_event, INFINITE);
@@ -2328,7 +2391,7 @@
thread->interruption_requested = TRUE;
mono_monitor_exit (thread->synch_lock);
-
+
/* this will awake the thread if it is in WaitForSingleObject
or similar */
QueueUserAPC (dummy_apc, thread->handle, NULL);
@@ -2357,7 +2420,7 @@
MonoThread *thread = mono_thread_current ();
/* The thread may already be stopping */
- if (thread == NULL)
+ if (thread == NULL)
return;
if (thread->interruption_requested && (bypass_abort_protection ||
!is_running_protected_wrapper ())) {
_______________________________________________
Mono-patches maillist - [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches