From 5eb363dbf19658aa7665185c8521d5e4f2d46966 Mon Sep 17 00:00:00 2001 From: Ludovic Henry Date: Thu, 2 Mar 2017 21:26:00 -0500 Subject: [PATCH] [threads] Remove ThreadState_StopRequested (#4462) --- mcs/class/corlib/System.Threading/Thread.cs | 2 +- mono/metadata/gc.c | 2 +- mono/metadata/monitor.c | 4 +- mono/metadata/object-internals.h | 2 +- mono/metadata/threadpool-io-epoll.c | 1 - mono/metadata/threadpool-io-kqueue.c | 1 - mono/metadata/threadpool-io-poll.c | 1 - mono/metadata/threadpool-worker-default.c | 1 - mono/metadata/threads-types.h | 3 - mono/metadata/threads.c | 66 ++++----------------- mono/metadata/w32socket-win32.c | 4 -- mono/metadata/w32socket.c | 4 +- 12 files changed, 17 insertions(+), 74 deletions(-) diff --git a/mcs/class/corlib/System.Threading/Thread.cs b/mcs/class/corlib/System.Threading/Thread.cs index 6d96fe1b367..c6564db3473 100644 --- a/mcs/class/corlib/System.Threading/Thread.cs +++ b/mcs/class/corlib/System.Threading/Thread.cs @@ -81,7 +81,7 @@ namespace System.Threading { internal int managed_id; private int small_id; private IntPtr manage_callback; - private IntPtr interrupt_on_stop; + private IntPtr unused4; private IntPtr flags; private IntPtr thread_pinning_ref; private IntPtr abort_protected_block_count; diff --git a/mono/metadata/gc.c b/mono/metadata/gc.c index 499e3840063..f3bc6aba7c5 100644 --- a/mono/metadata/gc.c +++ b/mono/metadata/gc.c @@ -478,7 +478,7 @@ mono_domain_finalize (MonoDomain *domain, guint32 timeout) if (res == MONO_SEM_TIMEDWAIT_RET_SUCCESS) { break; } else if (res == MONO_SEM_TIMEDWAIT_RET_ALERTED) { - if ((thread->state & (ThreadState_StopRequested | ThreadState_SuspendRequested)) != 0) { + if ((thread->state & (ThreadState_AbortRequested | ThreadState_SuspendRequested)) != 0) { ret = FALSE; break; } diff --git a/mono/metadata/monitor.c b/mono/metadata/monitor.c index dd2cc0eb9a7..676400c152e 100644 --- a/mono/metadata/monitor.c +++ b/mono/metadata/monitor.c @@ -891,7 +891,7 @@ retry_contended: * */ if (allow_interruption) { - if (!mono_thread_test_and_set_state (thread, (MonoThreadState)(ThreadState_StopRequested | ThreadState_AbortRequested), ThreadState_WaitSleepJoin)) { + if (!mono_thread_test_and_set_state (thread, ThreadState_AbortRequested, ThreadState_WaitSleepJoin)) { wait_ret = MONO_SEM_TIMEDWAIT_RET_ALERTED; goto done_waiting; } @@ -918,7 +918,7 @@ done_waiting: * We have to obey a stop/suspend request even if * allow_interruption is FALSE to avoid hangs at shutdown. */ - if (!mono_thread_test_state (mono_thread_internal_current (), (MonoThreadState)(ThreadState_StopRequested | ThreadState_SuspendRequested | ThreadState_AbortRequested))) { + if (!mono_thread_test_state (mono_thread_internal_current (), ThreadState_SuspendRequested | ThreadState_AbortRequested)) { if (ms != MONO_INFINITE_WAIT) { now = mono_msec_ticks (); diff --git a/mono/metadata/object-internals.h b/mono/metadata/object-internals.h index 958d25ac914..3e6c7f3e47b 100644 --- a/mono/metadata/object-internals.h +++ b/mono/metadata/object-internals.h @@ -388,7 +388,7 @@ struct _MonoInternalThread { gint32 managed_id; guint32 small_id; MonoThreadManageCallback manage_callback; - gpointer interrupt_on_stop; + gpointer unused4; gsize flags; gpointer thread_pinning_ref; gsize __abort_protected_block_count; diff --git a/mono/metadata/threadpool-io-epoll.c b/mono/metadata/threadpool-io-epoll.c index 2bc99e105b9..82d9b07ad14 100644 --- a/mono/metadata/threadpool-io-epoll.c +++ b/mono/metadata/threadpool-io-epoll.c @@ -93,7 +93,6 @@ epoll_event_wait (void (*callback) (gint fd, gint events, gpointer user_data), g if (ready == -1) { switch (errno) { case EINTR: - mono_thread_internal_check_for_interruption_critical (mono_thread_internal_current ()); ready = 0; break; default: diff --git a/mono/metadata/threadpool-io-kqueue.c b/mono/metadata/threadpool-io-kqueue.c index 4422b668e8f..47f346a2891 100644 --- a/mono/metadata/threadpool-io-kqueue.c +++ b/mono/metadata/threadpool-io-kqueue.c @@ -90,7 +90,6 @@ kqueue_event_wait (void (*callback) (gint fd, gint events, gpointer user_data), if (ready == -1) { switch (errno) { case EINTR: - mono_thread_internal_check_for_interruption_critical (mono_thread_internal_current ()); ready = 0; break; default: diff --git a/mono/metadata/threadpool-io-poll.c b/mono/metadata/threadpool-io-poll.c index 3d32130d3b5..d7eff1c210c 100644 --- a/mono/metadata/threadpool-io-poll.c +++ b/mono/metadata/threadpool-io-poll.c @@ -163,7 +163,6 @@ poll_event_wait (void (*callback) (gint fd, gint events, gpointer user_data), gp { case EINTR: { - mono_thread_internal_check_for_interruption_critical (mono_thread_internal_current ()); ready = 0; break; } diff --git a/mono/metadata/threadpool-worker-default.c b/mono/metadata/threadpool-worker-default.c index c1f4a878e99..3a87574aab1 100644 --- a/mono/metadata/threadpool-worker-default.c +++ b/mono/metadata/threadpool-worker-default.c @@ -743,7 +743,6 @@ monitor_thread (gpointer unused) break; interval_left -= mono_msec_ticks () - ts; - g_assert (!(internal->state & ThreadState_StopRequested)); mono_thread_interruption_checkpoint (); } while (interval_left > 0 && ++awake < 10); diff --git a/mono/metadata/threads-types.h b/mono/metadata/threads-types.h index cc1ee27d936..25571e7f896 100644 --- a/mono/metadata/threads-types.h +++ b/mono/metadata/threads-types.h @@ -23,7 +23,6 @@ /* This is a copy of System.Threading.ThreadState */ typedef enum { ThreadState_Running = 0x00000000, - ThreadState_StopRequested = 0x00000001, ThreadState_SuspendRequested = 0x00000002, ThreadState_Background = 0x00000004, ThreadState_Unstarted = 0x00000008, @@ -183,8 +182,6 @@ void ves_icall_System_Runtime_Remoting_Contexts_Context_ReleaseContext (MonoAppC MonoInternalThread *mono_thread_internal_current (void); -void mono_thread_internal_check_for_interruption_critical (MonoInternalThread *thread); - void mono_thread_internal_abort (MonoInternalThread *thread); void mono_thread_internal_suspend_for_shutdown (MonoInternalThread *thread); diff --git a/mono/metadata/threads.c b/mono/metadata/threads.c index 5f290cf3dc2..57e1d9addf3 100644 --- a/mono/metadata/threads.c +++ b/mono/metadata/threads.c @@ -2352,9 +2352,7 @@ request_thread_abort (MonoInternalThread *thread, MonoObject *state) { LOCK_THREAD (thread); - if ((thread->state & ThreadState_AbortRequested) != 0 || - (thread->state & ThreadState_StopRequested) != 0 || - (thread->state & ThreadState_Stopped) != 0) + if (thread->state & (ThreadState_AbortRequested | ThreadState_Stopped)) { UNLOCK_THREAD (thread); return FALSE; @@ -2506,17 +2504,13 @@ mono_thread_suspend (MonoInternalThread *thread) { LOCK_THREAD (thread); - if ((thread->state & ThreadState_Unstarted) != 0 || - (thread->state & ThreadState_Aborted) != 0 || - (thread->state & ThreadState_Stopped) != 0) + if (thread->state & (ThreadState_Unstarted | ThreadState_Aborted | ThreadState_Stopped)) { UNLOCK_THREAD (thread); return FALSE; } - if ((thread->state & ThreadState_Suspended) != 0 || - (thread->state & ThreadState_SuspendRequested) != 0 || - (thread->state & ThreadState_StopRequested) != 0) + if (thread->state & (ThreadState_Suspended | ThreadState_SuspendRequested | ThreadState_AbortRequested)) { UNLOCK_THREAD (thread); return TRUE; @@ -2635,21 +2629,8 @@ mono_thread_stop (MonoThread *thread) { MonoInternalThread *internal = thread->internal_thread; - LOCK_THREAD (internal); - - if (internal->state & (ThreadState_StopRequested | ThreadState_Stopped)) - { - UNLOCK_THREAD (internal); + if (!request_thread_abort (internal, NULL)) return; - } - - /* Make sure the internal is awake */ - mono_thread_resume (internal); - - internal->state |= ThreadState_StopRequested; - internal->state &= ~ThreadState_AbortRequested; - - UNLOCK_THREAD (internal); if (internal == mono_thread_internal_current ()) { MonoError error; @@ -3221,13 +3202,10 @@ mono_threads_set_shutting_down (void) LOCK_THREAD (current_thread); - if ((current_thread->state & ThreadState_SuspendRequested) || - (current_thread->state & ThreadState_AbortRequested) || - (current_thread->state & ThreadState_StopRequested)) { + if (current_thread->state & (ThreadState_SuspendRequested | ThreadState_AbortRequested)) { UNLOCK_THREAD (current_thread); mono_thread_execute_interruption (); } else { - current_thread->state |= ThreadState_Stopped; UNLOCK_THREAD (current_thread); } @@ -3410,9 +3388,7 @@ void mono_thread_suspend_all_other_threads (void) LOCK_THREAD (thread); - if ((thread->state & ThreadState_Suspended) != 0 || - (thread->state & ThreadState_StopRequested) != 0 || - (thread->state & ThreadState_Stopped) != 0) { + if (thread->state & (ThreadState_Suspended | ThreadState_Stopped)) { UNLOCK_THREAD (thread); mono_threads_close_thread_handle (wait->handles [i]); wait->threads [i] = NULL; @@ -4426,7 +4402,7 @@ mono_thread_execute_interruption (void) UNLOCK_THREAD (thread); return exc; - } else if ((thread->state & ThreadState_AbortRequested) != 0) { + } else if (thread->state & (ThreadState_AbortRequested)) { UNLOCK_THREAD (thread); g_assert (sys_thread->pending_exception == NULL); if (thread->abort_exc == NULL) { @@ -4437,19 +4413,10 @@ mono_thread_execute_interruption (void) MONO_OBJECT_SETREF (thread, abort_exc, mono_get_exception_thread_abort ()); } return thread->abort_exc; - } - else if ((thread->state & ThreadState_SuspendRequested) != 0) { + } else if (thread->state & (ThreadState_SuspendRequested)) { /* calls UNLOCK_THREAD (thread) */ self_suspend_internal (); return NULL; - } - else if ((thread->state & ThreadState_StopRequested) != 0) { - /* FIXME: do this through the JIT? */ - - UNLOCK_THREAD (thread); - - mono_thread_exit (); - return NULL; } else if (thread->thread_interrupt_requested) { thread->thread_interrupt_requested = FALSE; @@ -4480,12 +4447,6 @@ mono_thread_request_interruption (gboolean running_managed) if (thread == NULL) return NULL; -#ifdef HOST_WIN32 - if (thread->interrupt_on_stop && - thread->state & ThreadState_StopRequested && - thread->state & ThreadState_Background) - ExitThread (1); -#endif if (!mono_thread_set_interruption_requested (thread)) return NULL; InterlockedIncrement (&thread_interruption_requested); @@ -4523,7 +4484,7 @@ mono_thread_resume_interruption (void) return NULL; LOCK_THREAD (thread); - still_aborting = (thread->state & (ThreadState_AbortRequested|ThreadState_StopRequested)) != 0; + still_aborting = (thread->state & (ThreadState_AbortRequested)) != 0; UNLOCK_THREAD (thread); /*This can happen if the protected block called Thread::ResetAbort*/ @@ -5083,13 +5044,6 @@ mono_thread_join (gpointer tid) #endif } -void -mono_thread_internal_check_for_interruption_critical (MonoInternalThread *thread) -{ - if ((thread->state & (ThreadState_StopRequested | ThreadState_SuspendRequested)) != 0) - mono_thread_interruption_checkpoint (); -} - void mono_thread_internal_unhandled_exception (MonoObject* exc) { @@ -5236,7 +5190,7 @@ mono_threads_is_ready_to_be_interrupted (void) thread = mono_thread_internal_current (); LOCK_THREAD (thread); - if (thread->state & (MonoThreadState)(ThreadState_StopRequested | ThreadState_SuspendRequested | ThreadState_AbortRequested)) { + if (thread->state & (ThreadState_SuspendRequested | ThreadState_AbortRequested)) { UNLOCK_THREAD (thread); return FALSE; } diff --git a/mono/metadata/w32socket-win32.c b/mono/metadata/w32socket-win32.c index 4d71777f06d..1c6f8fd8307 100644 --- a/mono/metadata/w32socket-win32.c +++ b/mono/metadata/w32socket-win32.c @@ -136,9 +136,7 @@ SOCKET mono_w32socket_accept (SOCKET s, struct sockaddr *addr, socklen_t *addrle { MonoInternalThread *curthread = mono_thread_internal_current (); SOCKET newsock = INVALID_SOCKET; - curthread->interrupt_on_stop = (gpointer)TRUE; ALERTABLE_SOCKET_CALL (FD_ACCEPT_BIT, blocking, TRUE, newsock, accept, s, addr, addrlen); - curthread->interrupt_on_stop = (gpointer)FALSE; return newsock; } @@ -154,9 +152,7 @@ int mono_w32socket_recv (SOCKET s, char *buf, int len, int flags, gboolean block { MonoInternalThread *curthread = mono_thread_internal_current (); int ret = SOCKET_ERROR; - curthread->interrupt_on_stop = (gpointer)TRUE; ALERTABLE_SOCKET_CALL (FD_READ_BIT, blocking, TRUE, ret, recv, s, buf, len, flags); - curthread->interrupt_on_stop = (gpointer)FALSE; return ret; } diff --git a/mono/metadata/w32socket.c b/mono/metadata/w32socket.c index 5312790ad64..8bc8b49a8c5 100644 --- a/mono/metadata/w32socket.c +++ b/mono/metadata/w32socket.c @@ -1270,7 +1270,7 @@ ves_icall_System_Net_Sockets_Socket_Poll_internal (gsize sock, gint mode, } if (ret == -1 && errno == EINTR) { - if (mono_thread_test_state (thread, (MonoThreadState)(ThreadState_AbortRequested | ThreadState_StopRequested))) { + if (mono_thread_test_state (thread, ThreadState_AbortRequested)) { g_free (pfds); return FALSE; } @@ -1777,7 +1777,7 @@ ves_icall_System_Net_Sockets_Socket_Select_internal (MonoArray **sockets, gint32 } if (ret == -1 && errno == EINTR) { - if (mono_thread_test_state (thread, (MonoThreadState)(ThreadState_AbortRequested | ThreadState_StopRequested))) { + if (mono_thread_test_state (thread, ThreadState_AbortRequested)) { g_free (pfds); *sockets = NULL; return; -- 2.25.1