private static uint StartThread(IntPtr parameter) { GCHandle threadHandle = (GCHandle)parameter; RuntimeThread thread = (RuntimeThread)threadHandle.Target; Delegate threadStart = thread._threadStart; // Get the value before clearing the ThreadState.Unstarted bit object threadStartArg = thread._threadStartArg; try { t_currentThread = thread; System.Threading.ManagedThreadId.SetForCurrentThread(thread._managedThreadId); } catch (OutOfMemoryException) { #if PLATFORM_UNIX // This should go away once OnThreadExit stops using t_currentThread to signal // shutdown of the thread on Unix. thread._stopped.Set(); #endif // Terminate the current thread. The creator thread will throw a ThreadStartException. return(0); } // Report success to the creator thread, which will free threadHandle and _threadStartArg thread.ClearThreadStateBit(ThreadState.Unstarted); try { // The Thread cannot be started more than once, so we may clean up the delegate thread._threadStart = null; #if ENABLE_WINRT // If this call fails, COM and WinRT calls on this thread will fail with CO_E_NOTINITIALIZED. // We may continue and fail on the actual call. Interop.WinRT.RoInitialize(Interop.WinRT.RO_INIT_TYPE.RO_INIT_MULTITHREADED); #endif ParameterizedThreadStart paramThreadStart = threadStart as ParameterizedThreadStart; if (paramThreadStart != null) { paramThreadStart(threadStartArg); } else { ((ThreadStart)threadStart)(); } } finally { thread.SetThreadStateBit(ThreadState.Stopped); } return(0); }
private static void OnThreadExit() { // Set the Stopped bit and signal the current thread as stopped RuntimeThread currentThread = t_currentThread; if (currentThread != null) { int state = currentThread._threadState; if ((state & (int)(ThreadState.Stopped | ThreadState.Aborted)) == 0) { currentThread.SetThreadStateBit(ThreadState.Stopped); } currentThread._stopped.Set(); } }
private static void StartThread(IntPtr parameter) { GCHandle threadHandle = (GCHandle)parameter; RuntimeThread thread = (RuntimeThread)threadHandle.Target; Delegate threadStart = thread._threadStart; // Get the value before clearing the ThreadState.Unstarted bit object threadStartArg = thread._threadStartArg; try { t_currentThread = thread; System.Threading.ManagedThreadId.SetForCurrentThread(thread._managedThreadId); thread.InitializeComOnNewThread(); } catch (OutOfMemoryException) { #if PLATFORM_UNIX // This should go away once OnThreadExit stops using t_currentThread to signal // shutdown of the thread on Unix. thread._stopped.Set(); #endif // Terminate the current thread. The creator thread will throw a ThreadStartException. return; } // Report success to the creator thread, which will free threadHandle and _threadStartArg thread.ClearThreadStateBit(ThreadState.Unstarted); try { // The Thread cannot be started more than once, so we may clean up the delegate thread._threadStart = null; ParameterizedThreadStart paramThreadStart = threadStart as ParameterizedThreadStart; if (paramThreadStart != null) { paramThreadStart(threadStartArg); } else { ((ThreadStart)threadStart)(); } } finally { thread.SetThreadStateBit(ThreadState.Stopped); } }
private static void OnThreadExit() { RuntimeThread currentThread = t_currentThread; if (currentThread != null) { // Inform the wait subsystem that the thread is exiting. For instance, this would abandon any mutexes locked by // the thread. WaitSubsystem.OnThreadExiting(currentThread); // Set the Stopped bit and signal the current thread as stopped int state = currentThread._threadState; if ((state & (int)(ThreadState.Stopped | ThreadState.Aborted)) == 0) { currentThread.SetThreadStateBit(ThreadState.Stopped); } currentThread._stopped.Set(); } }
private static uint StartThread(IntPtr parameter) { GCHandle threadHandle = (GCHandle)parameter; RuntimeThread thread = (RuntimeThread)threadHandle.Target; // TODO: OOM hardening t_currentThread = thread; System.Threading.ManagedThreadId.SetForCurrentThread(thread._managedThreadId); threadHandle.Free(); thread.ClearThreadStateBit(ThreadState.Unstarted); try { Delegate threadStart = thread._threadStart; object threadStartArg = thread._threadStartArg; thread._threadStart = null; thread._threadStartArg = null; #if ENABLE_WINRT Interop.WinRT.RoInitialize(Interop.WinRT.RO_INIT_TYPE.RO_INIT_MULTITHREADED); #endif ParameterizedThreadStart paramThreadStart = threadStart as ParameterizedThreadStart; if (paramThreadStart != null) { paramThreadStart(threadStartArg); } else { ((ThreadStart)threadStart)(); } } finally { thread.SetThreadStateBit(ThreadState.Stopped); } return(0); }