2 * The thread module provides support for thread creation and management.
4 * Copyright: Copyright Sean Kelly 2005 - 2012.
5 * License: Distributed under the
6 * $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost Software License 1.0).
7 * (See accompanying file LICENSE)
8 * Authors: Sean Kelly, Walter Bright, Alex Rønne Petersen, Martin Nowak
9 * Source: $(DRUNTIMESRC core/_thread.d)
12 /* NOTE: This file has been patched from the original DMD distribution to
13 * work with the GDC compiler.
18 public import core.time; // for Duration
19 import core.exception : onOutOfMemoryError;
27 else version (WatchOS)
32 // interface to rt.tlsgc
33 import core.internal.traits : externDFunc;
35 alias rt_tlsgc_init = externDFunc!("rt.tlsgc.init", void* function() nothrow @nogc);
36 alias rt_tlsgc_destroy = externDFunc!("rt.tlsgc.destroy", void function(void*) nothrow @nogc);
38 alias ScanDg = void delegate(void* pstart, void* pend) nothrow;
40 externDFunc!("rt.tlsgc.scan", void function(void*, scope ScanDg) nothrow);
42 alias rt_tlsgc_processGCMarks =
43 externDFunc!("rt.tlsgc.processGCMarks", void function(void*, scope IsMarkedDg) nothrow);
48 import core.sys.solaris.sys.priocntl;
49 import core.sys.solaris.sys.types;
52 // this should be true for most architectures
53 version (GNU_StackGrowsDown)
54 version = StackGrowsDown;
57 * Returns the process ID of the calling process, which is guaranteed to be
58 * unique on the system. This call is always successful.
62 * writefln("Current process id: %s", getpid());
67 alias getpid = core.sys.posix.unistd.getpid;
69 else version (Windows)
71 alias getpid = core.sys.windows.windows.GetCurrentProcessId;
75 ///////////////////////////////////////////////////////////////////////////////
76 // Thread and Fiber Exceptions
77 ///////////////////////////////////////////////////////////////////////////////
81 * Base class for thread exceptions.
83 class ThreadException : Exception
85 @safe pure nothrow this(string msg, string file = __FILE__, size_t line = __LINE__, Throwable next = null)
87 super(msg, file, line, next);
90 @safe pure nothrow this(string msg, Throwable next, string file = __FILE__, size_t line = __LINE__)
92 super(msg, file, line, next);
98 * Base class for thread errors to be used for function inside GC when allocations are unavailable.
100 class ThreadError : Error
102 @safe pure nothrow this(string msg, string file = __FILE__, size_t line = __LINE__, Throwable next = null)
104 super(msg, file, line, next);
107 @safe pure nothrow this(string msg, Throwable next, string file = __FILE__, size_t line = __LINE__)
109 super(msg, file, line, next);
115 import core.atomic, core.memory, core.sync.mutex;
117 // Handling unaligned mutexes are not supported on all platforms, so we must
118 // ensure that the address of all shared data are appropriately aligned.
119 import core.internal.traits : classInstanceAlignment;
121 enum mutexAlign = classInstanceAlignment!Mutex;
122 enum mutexClassInstanceSize = __traits(classInstanceSize, Mutex);
125 // exposed by compiler runtime
127 extern (C) void rt_moduleTlsCtor();
128 extern (C) void rt_moduleTlsDtor();
131 * Hook for whatever EH implementation is used to save/restore some data
135 * newContext = The return value of the prior call to this function
136 * where the stack was last swapped out, or null when a fiber stack
137 * is switched in for the first time.
139 extern(C) void* _d_eh_swapContext(void* newContext) nothrow @nogc;
141 version (DigitalMars)
144 alias swapContext = _d_eh_swapContext;
147 extern(C) void* _d_eh_swapContextDwarf(void* newContext) nothrow @nogc;
149 void* swapContext(void* newContext) nothrow @nogc
151 /* Detect at runtime which scheme is being used.
152 * Eventually, determine it statically.
154 static int which = 0;
159 assert(newContext == null);
160 auto p = _d_eh_swapContext(newContext);
161 auto pdwarf = _d_eh_swapContextDwarf(newContext);
175 return _d_eh_swapContext(newContext);
177 return _d_eh_swapContextDwarf(newContext);
183 alias swapContext = _d_eh_swapContext;
187 ///////////////////////////////////////////////////////////////////////////////
188 // Thread Entry Point and Signal Handlers
189 ///////////////////////////////////////////////////////////////////////////////
196 import core.stdc.stdint : uintptr_t; // for _beginthreadex decl below
197 import core.stdc.stdlib; // for malloc, atexit
198 import core.sys.windows.windows;
199 import core.sys.windows.threadaux; // for OpenThreadHandle
201 extern (Windows) alias btex_fptr = uint function(void*);
202 extern (C) uintptr_t _beginthreadex(void*, uint, btex_fptr, void*, uint, uint*) nothrow;
205 // Entry point for Windows threads
207 extern (Windows) uint thread_entryPoint( void* arg ) nothrow
209 Thread obj = cast(Thread) arg;
212 assert( obj.m_curr is &obj.m_main );
213 obj.m_main.bstack = getStackBottom();
214 obj.m_main.tstack = obj.m_main.bstack;
215 obj.m_tlsgcdata = rt_tlsgc_init();
223 Thread.add(&obj.m_main);
225 // NOTE: No GC allocations may occur until the stack pointers have
226 // been set and Thread.getThis returns a valid reference to
227 // this thread object (this latter condition is not strictly
228 // necessary on Windows but it should be followed for the
229 // sake of consistency).
231 // TODO: Consider putting an auto exception object here (using
232 // alloca) forOutOfMemoryError plus something to track
233 // whether an exception is in-flight?
235 void append( Throwable t )
237 if ( obj.m_unhandled is null )
241 Throwable last = obj.m_unhandled;
242 while ( last.next !is null )
248 version (D_InlineAsm_X86)
250 asm nothrow @nogc { fninit; }
260 catch ( Throwable t )
266 catch ( Throwable t )
274 HANDLE GetCurrentThreadHandle() nothrow @nogc
276 const uint DUPLICATE_SAME_ACCESS = 0x00000002;
278 HANDLE curr = GetCurrentThread(),
279 proc = GetCurrentProcess(),
282 DuplicateHandle( proc, curr, proc, &hndl, 0, TRUE, DUPLICATE_SAME_ACCESS );
291 import core.stdc.errno;
292 import core.sys.posix.semaphore;
293 import core.sys.posix.stdlib; // for malloc, valloc, free, atexit
294 import core.sys.posix.pthread;
295 import core.sys.posix.signal;
296 import core.sys.posix.time;
300 import core.sys.darwin.mach.thread_act;
301 import core.sys.darwin.pthread : pthread_mach_thread_np;
310 // Entry point for POSIX threads
312 extern (C) void* thread_entryPoint( void* arg ) nothrow
317 Thread obj = cast(Thread)(cast(void**)arg)[0];
318 auto loadedLibraries = (cast(void**)arg)[1];
323 Thread obj = cast(Thread)arg;
327 // loadedLibraries need to be inherited from parent thread
328 // before initilizing GC for TLS (rt_tlsgc_init)
329 version (Shared) inheritLoadedLibraries(loadedLibraries);
331 assert( obj.m_curr is &obj.m_main );
332 obj.m_main.bstack = getStackBottom();
333 obj.m_main.tstack = obj.m_main.bstack;
334 obj.m_tlsgcdata = rt_tlsgc_init();
336 atomicStore!(MemoryOrder.raw)(obj.m_isRunning, true);
337 Thread.setThis(obj); // allocates lazy TLS (see Issue 11981)
338 Thread.add(obj); // can only receive signals from here on
342 atomicStore!(MemoryOrder.raw)(obj.m_isRunning, false);
344 Thread.add(&obj.m_main);
346 static extern (C) void thread_cleanupHandler( void* arg ) nothrow @nogc
348 Thread obj = cast(Thread) arg;
351 // NOTE: If the thread terminated abnormally, just set it as
352 // not running and let thread_suspendAll remove it from
353 // the thread list. This is safer and is consistent
354 // with the Windows thread code.
355 atomicStore!(MemoryOrder.raw)(obj.m_isRunning,false);
358 // NOTE: Using void to skip the initialization here relies on
359 // knowledge of how pthread_cleanup is implemented. It may
360 // not be appropriate for all platforms. However, it does
361 // avoid the need to link the pthread module. If any
362 // implementation actually requires default initialization
363 // then pthread_cleanup should be restructured to maintain
364 // the current lack of a link dependency.
365 static if ( __traits( compiles, pthread_cleanup ) )
367 pthread_cleanup cleanup = void;
368 cleanup.push( &thread_cleanupHandler, cast(void*) obj );
370 else static if ( __traits( compiles, pthread_cleanup_push ) )
372 pthread_cleanup_push( &thread_cleanupHandler, cast(void*) obj );
376 static assert( false, "Platform not supported." );
379 // NOTE: No GC allocations may occur until the stack pointers have
380 // been set and Thread.getThis returns a valid reference to
381 // this thread object (this latter condition is not strictly
382 // necessary on Windows but it should be followed for the
383 // sake of consistency).
385 // TODO: Consider putting an auto exception object here (using
386 // alloca) forOutOfMemoryError plus something to track
387 // whether an exception is in-flight?
389 void append( Throwable t )
391 if ( obj.m_unhandled is null )
395 Throwable last = obj.m_unhandled;
396 while ( last.next !is null )
409 catch ( Throwable t )
414 version (Shared) cleanupLoadedLibraries();
416 catch ( Throwable t )
421 // NOTE: Normal cleanup is handled by scope(exit).
423 static if ( __traits( compiles, pthread_cleanup ) )
427 else static if ( __traits( compiles, pthread_cleanup_push ) )
429 pthread_cleanup_pop( 0 );
437 // Used to track the number of suspended threads
439 __gshared sem_t suspendCount;
442 extern (C) void thread_suspendHandler( int sig ) nothrow
445 assert( sig == suspendSignalNumber );
449 void op(void* sp) nothrow
451 // NOTE: Since registers are being pushed and popped from the
452 // stack, any other stack data used by this function should
453 // be gone before the stack cleanup code is called below.
454 Thread obj = Thread.getThis();
455 assert(obj !is null);
459 obj.m_curr.tstack = getStackTop();
462 sigset_t sigres = void;
465 status = sigfillset( &sigres );
466 assert( status == 0 );
468 status = sigdelset( &sigres, resumeSignalNumber );
469 assert( status == 0 );
471 version (FreeBSD) obj.m_suspendagain = false;
472 status = sem_post( &suspendCount );
473 assert( status == 0 );
475 sigsuspend( &sigres );
479 obj.m_curr.tstack = obj.m_curr.bstack;
483 // avoid deadlocks on FreeBSD, see Issue 13416
486 auto obj = Thread.getThis();
487 if (THR_IN_CRITICAL(obj.m_addr))
489 obj.m_suspendagain = true;
490 if (sem_post(&suspendCount)) assert(0);
495 callWithStackShell(&op);
499 extern (C) void thread_resumeHandler( int sig ) nothrow
502 assert( sig == resumeSignalNumber );
509 // HACK libthr internal (thr_private.h) macro, used to
510 // avoid deadlocks in signal handler, see Issue 13416
511 version (FreeBSD) bool THR_IN_CRITICAL(pthread_t p) nothrow @nogc
513 import core.sys.posix.config : c_long;
514 import core.sys.posix.sys.types : lwpid_t;
516 // If the begin of pthread would be changed in libthr (unlikely)
517 // we'll run into undefined behavior, compare with thr_private.h.
518 static struct pthread
521 static struct umutex { lwpid_t owner; uint flags; uint[2] ceilings; uint[4] spare; }
528 auto priv = cast(pthread*)p;
529 return priv.locklevel > 0 || priv.critical_count > 0;
535 // NOTE: This is the only place threading versions are checked. If a new
536 // version is added, the module code will need to be searched for
537 // places where version-specific code may be required. This can be
538 // easily accomlished by searching for 'Windows' or 'Posix'.
539 static assert( false, "Unknown threading implementation." );
543 ///////////////////////////////////////////////////////////////////////////////
545 ///////////////////////////////////////////////////////////////////////////////
549 * This class encapsulates all threading functionality for the D
550 * programming language. As thread manipulation is a required facility
551 * for garbage collection, all user threads should derive from this
552 * class, and instances of this class should never be explicitly deleted.
553 * A new thread may be created using either derivation or composition, as
554 * in the following example.
558 ///////////////////////////////////////////////////////////////////////////
560 ///////////////////////////////////////////////////////////////////////////
564 * Initializes a thread object which is associated with a static
568 * fn = The thread function.
569 * sz = The stack size for this thread.
572 * fn must not be null.
574 this( void function() fn, size_t sz = 0 ) @safe pure nothrow @nogc
582 () @trusted { m_fn = fn; }();
589 * Initializes a thread object which is associated with a dynamic
593 * dg = The thread function.
594 * sz = The stack size for this thread.
597 * dg must not be null.
599 this( void delegate() dg, size_t sz = 0 ) @safe pure nothrow @nogc
607 () @trusted { m_dg = dg; }();
614 * Cleans up any remaining resources used by this object.
616 ~this() nothrow @nogc
618 if ( m_addr == m_addr.init )
625 m_addr = m_addr.init;
626 CloseHandle( m_hndl );
627 m_hndl = m_hndl.init;
631 pthread_detach( m_addr );
632 m_addr = m_addr.init;
636 m_tmach = m_tmach.init;
638 rt_tlsgc_destroy( m_tlsgcdata );
643 ///////////////////////////////////////////////////////////////////////////
645 ///////////////////////////////////////////////////////////////////////////
649 * Starts the thread and invokes the function or delegate passed upon
653 * This routine may only be called once per thread instance.
656 * ThreadException if the thread fails to start.
658 final Thread start() nothrow
661 assert( !next && !prev );
665 auto wasThreaded = multiThreadedFlag;
666 multiThreadedFlag = true;
670 multiThreadedFlag = false;
673 version (Windows) {} else
678 if ( pthread_attr_init( &attr ) )
679 onThreadError( "Error initializing thread attributes" );
680 if ( m_sz && pthread_attr_setstacksize( &attr, m_sz ) )
681 onThreadError( "Error initializing thread stack size" );
686 // NOTE: If a thread is just executing DllMain()
687 // while another thread is started here, it holds an OS internal
688 // lock that serializes DllMain with CreateThread. As the code
689 // might request a synchronization on slock (e.g. in thread_findByAddr()),
690 // we cannot hold that lock while creating the thread without
691 // creating a deadlock
693 // Solution: Create the thread in suspended state and then
694 // add and resume it with slock acquired
695 assert(m_sz <= uint.max, "m_sz must be less than or equal to uint.max");
696 m_hndl = cast(HANDLE) _beginthreadex( null, cast(uint) m_sz, &thread_entryPoint, cast(void*) this, CREATE_SUSPENDED, &m_addr );
697 if ( cast(size_t) m_hndl == 0 )
698 onThreadError( "Error creating thread" );
701 slock.lock_nothrow();
702 scope(exit) slock.unlock_nothrow();
705 pAboutToStart = cast(Thread*)realloc(pAboutToStart, Thread.sizeof * nAboutToStart);
706 pAboutToStart[nAboutToStart - 1] = this;
709 if ( ResumeThread( m_hndl ) == -1 )
710 onThreadError( "Error resuming thread" );
714 // NOTE: This is also set to true by thread_entryPoint, but set it
715 // here as well so the calling thread will see the isRunning
716 // state immediately.
717 atomicStore!(MemoryOrder.raw)(m_isRunning, true);
718 scope( failure ) atomicStore!(MemoryOrder.raw)(m_isRunning, false);
723 auto libs = pinLoadedLibraries();
724 auto ps = cast(void**).malloc(2 * size_t.sizeof);
725 if (ps is null) onOutOfMemoryError();
726 ps[0] = cast(void*)this;
727 ps[1] = cast(void*)libs;
728 if ( pthread_create( &m_addr, &attr, &thread_entryPoint, ps ) != 0 )
730 unpinLoadedLibraries(libs);
732 onThreadError( "Error creating thread" );
737 if ( pthread_create( &m_addr, &attr, &thread_entryPoint, cast(void*) this ) != 0 )
738 onThreadError( "Error creating thread" );
743 m_tmach = pthread_mach_thread_np( m_addr );
744 if ( m_tmach == m_tmach.init )
745 onThreadError( "Error creating thread" );
753 * Waits for this thread to complete. If the thread terminated as the
754 * result of an unhandled exception, this exception will be rethrown.
757 * rethrow = Rethrow any unhandled exception which may have caused this
758 * thread to terminate.
761 * ThreadException if the operation fails.
762 * Any exception not handled by the joined thread.
765 * Any exception not handled by this thread if rethrow = false, null
768 final Throwable join( bool rethrow = true )
772 if ( WaitForSingleObject( m_hndl, INFINITE ) != WAIT_OBJECT_0 )
773 throw new ThreadException( "Unable to join thread" );
774 // NOTE: m_addr must be cleared before m_hndl is closed to avoid
775 // a race condition with isRunning. The operation is done
776 // with atomicStore to prevent compiler reordering.
777 atomicStore!(MemoryOrder.raw)(*cast(shared)&m_addr, m_addr.init);
778 CloseHandle( m_hndl );
779 m_hndl = m_hndl.init;
783 if ( pthread_join( m_addr, null ) != 0 )
784 throw new ThreadException( "Unable to join thread" );
785 // NOTE: pthread_join acts as a substitute for pthread_detach,
786 // which is normally called by the dtor. Setting m_addr
787 // to zero ensures that pthread_detach will not be called
788 // on object destruction.
789 m_addr = m_addr.init;
801 ///////////////////////////////////////////////////////////////////////////
802 // General Properties
803 ///////////////////////////////////////////////////////////////////////////
807 * Gets the OS identifier for this thread.
810 * If the thread hasn't been started yet, returns $(LREF ThreadID)$(D.init).
811 * Otherwise, returns the result of $(D GetCurrentThreadId) on Windows,
812 * and $(D pthread_self) on POSIX.
814 * The value is unique for the current process.
816 final @property ThreadID id() @safe @nogc
826 * Gets the user-readable label for this thread.
829 * The name of this thread.
831 final @property string name() @safe @nogc
841 * Sets the user-readable label for this thread.
844 * val = The new name of this thread.
846 final @property void name( string val ) @safe @nogc
856 * Gets the daemon status for this thread. While the runtime will wait for
857 * all normal threads to complete before tearing down the process, daemon
858 * threads are effectively ignored and thus will not prevent the process
859 * from terminating. In effect, daemon threads will be terminated
860 * automatically by the OS when the process exits.
863 * true if this is a daemon thread.
865 final @property bool isDaemon() @safe @nogc
875 * Sets the daemon status for this thread. While the runtime will wait for
876 * all normal threads to complete before tearing down the process, daemon
877 * threads are effectively ignored and thus will not prevent the process
878 * from terminating. In effect, daemon threads will be terminated
879 * automatically by the OS when the process exits.
882 * val = The new daemon status for this thread.
884 final @property void isDaemon( bool val ) @safe @nogc
894 * Tests whether this thread is running.
897 * true if the thread is running, false if not.
899 final @property bool isRunning() nothrow @nogc
901 if ( m_addr == m_addr.init )
909 GetExitCodeThread( m_hndl, &ecode );
910 return ecode == STILL_ACTIVE;
914 return atomicLoad(m_isRunning);
919 ///////////////////////////////////////////////////////////////////////////
920 // Thread Priority Actions
921 ///////////////////////////////////////////////////////////////////////////
925 @property static int PRIORITY_MIN() @nogc nothrow pure @safe
927 return THREAD_PRIORITY_IDLE;
930 @property static const(int) PRIORITY_MAX() @nogc nothrow pure @safe
932 return THREAD_PRIORITY_TIME_CRITICAL;
935 @property static int PRIORITY_DEFAULT() @nogc nothrow pure @safe
937 return THREAD_PRIORITY_NORMAL;
942 private struct Priority
944 int PRIORITY_MIN = int.min;
945 int PRIORITY_DEFAULT = int.min;
946 int PRIORITY_MAX = int.min;
950 Lazily loads one of the members stored in a hidden global variable of
951 type `Priority`. Upon the first access of either member, the entire
952 `Priority` structure is initialized. Multiple initializations from
953 different threads calling this function are tolerated.
955 `which` must be one of `PRIORITY_MIN`, `PRIORITY_DEFAULT`,
958 private static int loadGlobal(string which)()
960 static shared Priority cache;
961 auto local = atomicLoad(mixin("cache." ~ which));
962 if (local != local.min) return local;
963 // There will be benign races
964 cache = loadPriorities;
965 return atomicLoad(mixin("cache." ~ which));
969 Loads all priorities and returns them as a `Priority` structure. This
970 function is thread-neutral.
972 private static Priority loadPriorities() @nogc nothrow @trusted
980 pcParms.pc_cid = PC_CLNULL;
981 if (priocntl(idtype_t.P_PID, P_MYID, PC_GETPARMS, &pcParms) == -1)
982 assert( 0, "Unable to get scheduling class" );
984 pcInfo.pc_cid = pcParms.pc_cid;
985 // PC_GETCLINFO ignores the first two args, use dummy values
986 if (priocntl(idtype_t.P_PID, 0, PC_GETCLINFO, &pcInfo) == -1)
987 assert( 0, "Unable to get scheduling class info" );
989 pri_t* clparms = cast(pri_t*)&pcParms.pc_clparms;
990 pri_t* clinfo = cast(pri_t*)&pcInfo.pc_clinfo;
992 result.PRIORITY_MAX = clparms[0];
994 if (pcInfo.pc_clname == "RT")
998 // For RT class, just assume it can't be changed
999 result.PRIORITY_MIN = clparms[0];
1000 result.PRIORITY_DEFAULT = clparms[0];
1004 m_isRTClass = false;
1006 // For all other scheduling classes, there are
1007 // two key values -- uprilim and maxupri.
1008 // maxupri is the maximum possible priority defined
1009 // for the scheduling class, and valid priorities
1010 // range are in [-maxupri, maxupri].
1012 // However, uprilim is an upper limit that the
1013 // current thread can set for the current scheduling
1014 // class, which can be less than maxupri. As such,
1015 // use this value for priorityMax since this is
1016 // the effective maximum.
1019 result.PRIORITY_MIN = -clinfo[0];
1021 result.PRIORITY_DEFAULT = 0;
1024 else version (Posix)
1028 pthread_getschedparam( pthread_self(), &policy, ¶m ) == 0
1029 || assert(0, "Internal error in pthread_getschedparam");
1031 result.PRIORITY_MIN = sched_get_priority_min( policy );
1032 result.PRIORITY_MIN != -1
1033 || assert(0, "Internal error in sched_get_priority_min");
1034 result.PRIORITY_DEFAULT = param.sched_priority;
1035 result.PRIORITY_MAX = sched_get_priority_max( policy );
1036 result.PRIORITY_MAX != -1 ||
1037 assert(0, "Internal error in sched_get_priority_max");
1041 static assert(0, "Your code here.");
1047 * The minimum scheduling priority that may be set for a thread. On
1048 * systems where multiple scheduling policies are defined, this value
1049 * represents the minimum valid priority for the scheduling policy of
1052 @property static int PRIORITY_MIN() @nogc nothrow pure @trusted
1054 return (cast(int function() @nogc nothrow pure @safe)
1055 &loadGlobal!"PRIORITY_MIN")();
1059 * The maximum scheduling priority that may be set for a thread. On
1060 * systems where multiple scheduling policies are defined, this value
1061 * represents the maximum valid priority for the scheduling policy of
1064 @property static const(int) PRIORITY_MAX() @nogc nothrow pure @trusted
1066 return (cast(int function() @nogc nothrow pure @safe)
1067 &loadGlobal!"PRIORITY_MAX")();
1071 * The default scheduling priority that is set for a thread. On
1072 * systems where multiple scheduling policies are defined, this value
1073 * represents the default priority for the scheduling policy of
1076 @property static int PRIORITY_DEFAULT() @nogc nothrow pure @trusted
1078 return (cast(int function() @nogc nothrow pure @safe)
1079 &loadGlobal!"PRIORITY_DEFAULT")();
1085 //NetBSD does not support priority for default policy
1086 // and it is not possible change policy without root access
1087 int fakePriority = int.max;
1091 * Gets the scheduling priority for the associated thread.
1093 * Note: Getting the priority of a thread that already terminated
1094 * might return the default priority.
1097 * The scheduling priority of this thread.
1099 final @property int priority()
1103 return GetThreadPriority( m_hndl );
1105 else version (NetBSD)
1107 return fakePriority==int.max? PRIORITY_DEFAULT : fakePriority;
1109 else version (Posix)
1114 if (auto err = pthread_getschedparam(m_addr, &policy, ¶m))
1116 // ignore error if thread is not running => Bugzilla 8960
1117 if (!atomicLoad(m_isRunning)) return PRIORITY_DEFAULT;
1118 throw new ThreadException("Unable to get thread priority");
1120 return param.sched_priority;
1126 * Sets the scheduling priority for the associated thread.
1128 * Note: Setting the priority of a thread that already terminated
1129 * might have no effect.
1132 * val = The new scheduling priority of this thread.
1134 final @property void priority( int val )
1137 assert(val >= PRIORITY_MIN);
1138 assert(val <= PRIORITY_MAX);
1144 if ( !SetThreadPriority( m_hndl, val ) )
1145 throw new ThreadException( "Unable to set thread priority" );
1147 else version (Solaris)
1149 // the pthread_setschedprio(3c) and pthread_setschedparam functions
1150 // are broken for the default (TS / time sharing) scheduling class.
1151 // instead, we use priocntl(2) which gives us the desired behavior.
1153 // We hardcode the min and max priorities to the current value
1154 // so this is a no-op for RT threads.
1160 pcparm.pc_cid = PC_CLNULL;
1161 if (priocntl(idtype_t.P_LWPID, P_MYID, PC_GETPARMS, &pcparm) == -1)
1162 throw new ThreadException( "Unable to get scheduling class" );
1164 pri_t* clparms = cast(pri_t*)&pcparm.pc_clparms;
1166 // clparms is filled in by the PC_GETPARMS call, only necessary
1167 // to adjust the element that contains the thread priority
1168 clparms[1] = cast(pri_t) val;
1170 if (priocntl(idtype_t.P_LWPID, P_MYID, PC_SETPARMS, &pcparm) == -1)
1171 throw new ThreadException( "Unable to set scheduling class" );
1173 else version (NetBSD)
1177 else version (Posix)
1179 static if (__traits(compiles, pthread_setschedprio))
1181 if (auto err = pthread_setschedprio(m_addr, val))
1183 // ignore error if thread is not running => Bugzilla 8960
1184 if (!atomicLoad(m_isRunning)) return;
1185 throw new ThreadException("Unable to set thread priority");
1190 // NOTE: pthread_setschedprio is not implemented on Darwin or FreeBSD, so use
1191 // the more complicated get/set sequence below.
1195 if (auto err = pthread_getschedparam(m_addr, &policy, ¶m))
1197 // ignore error if thread is not running => Bugzilla 8960
1198 if (!atomicLoad(m_isRunning)) return;
1199 throw new ThreadException("Unable to set thread priority");
1201 param.sched_priority = val;
1202 if (auto err = pthread_setschedparam(m_addr, policy, ¶m))
1204 // ignore error if thread is not running => Bugzilla 8960
1205 if (!atomicLoad(m_isRunning)) return;
1206 throw new ThreadException("Unable to set thread priority");
1215 auto thr = Thread.getThis();
1216 immutable prio = thr.priority;
1217 scope (exit) thr.priority = prio;
1219 assert(prio == PRIORITY_DEFAULT);
1220 assert(prio >= PRIORITY_MIN && prio <= PRIORITY_MAX);
1221 thr.priority = PRIORITY_MIN;
1222 assert(thr.priority == PRIORITY_MIN);
1223 thr.priority = PRIORITY_MAX;
1224 assert(thr.priority == PRIORITY_MAX);
1227 unittest // Bugzilla 8960
1229 import core.sync.semaphore;
1231 auto thr = new Thread({});
1233 Thread.sleep(1.msecs); // wait a little so the thread likely has finished
1234 thr.priority = PRIORITY_MAX; // setting priority doesn't cause error
1235 auto prio = thr.priority; // getting priority doesn't cause error
1236 assert(prio >= PRIORITY_MIN && prio <= PRIORITY_MAX);
1239 ///////////////////////////////////////////////////////////////////////////
1240 // Actions on Calling Thread
1241 ///////////////////////////////////////////////////////////////////////////
1245 * Suspends the calling thread for at least the supplied period. This may
1246 * result in multiple OS calls if period is greater than the maximum sleep
1247 * duration supported by the operating system.
1250 * val = The minimum duration the calling thread should be suspended.
1253 * period must be non-negative.
1256 * ------------------------------------------------------------------------
1258 * Thread.sleep( dur!("msecs")( 50 ) ); // sleep for 50 milliseconds
1259 * Thread.sleep( dur!("seconds")( 5 ) ); // sleep for 5 seconds
1261 * ------------------------------------------------------------------------
1263 static void sleep( Duration val ) @nogc nothrow
1266 assert( !val.isNegative );
1272 auto maxSleepMillis = dur!("msecs")( uint.max - 1 );
1274 // avoid a non-zero time to be round down to 0
1275 if ( val > dur!"msecs"( 0 ) && val < dur!"msecs"( 1 ) )
1276 val = dur!"msecs"( 1 );
1278 // NOTE: In instances where all other threads in the process have a
1279 // lower priority than the current thread, the current thread
1280 // will not yield with a sleep time of zero. However, unlike
1281 // yield(), the user is not asking for a yield to occur but
1282 // only for execution to suspend for the requested interval.
1283 // Therefore, expected performance may not be met if a yield
1284 // is forced upon the user.
1285 while ( val > maxSleepMillis )
1288 maxSleepMillis.total!"msecs" );
1289 val -= maxSleepMillis;
1291 Sleep( cast(uint) val.total!"msecs" );
1293 else version (Posix)
1295 timespec tin = void;
1296 timespec tout = void;
1298 val.split!("seconds", "nsecs")(tin.tv_sec, tin.tv_nsec);
1299 if ( val.total!"seconds" > tin.tv_sec.max )
1300 tin.tv_sec = tin.tv_sec.max;
1303 if ( !nanosleep( &tin, &tout ) )
1305 if ( errno != EINTR )
1306 assert(0, "Unable to sleep for the specified duration");
1314 * Forces a context switch to occur away from the calling thread.
1316 static void yield() @nogc nothrow
1320 else version (Posix)
1325 ///////////////////////////////////////////////////////////////////////////
1327 ///////////////////////////////////////////////////////////////////////////
1330 * Provides a reference to the calling thread.
1333 * The thread object representing the calling thread. The result of
1334 * deleting this object is undefined. If the current thread is not
1335 * attached to the runtime, a null reference is returned.
1337 static Thread getThis() @safe nothrow @nogc
1339 // NOTE: This function may not be called until thread_init has
1340 // completed. See thread_suspendAll for more information
1341 // on why this might occur.
1347 * Provides a list of all threads currently being tracked by the system.
1348 * Note that threads in the returned array might no longer run (see
1349 * $(D Thread.)$(LREF isRunning)).
1352 * An array containing references to all threads currently being
1353 * tracked by the system. The result of deleting any contained
1354 * objects is undefined.
1356 static Thread[] getAll()
1358 static void resize(ref Thread[] buf, size_t nlen)
1362 return getAllImpl!resize();
1367 * Operates on all threads currently being tracked by the system. The
1368 * result of deleting any Thread object is undefined.
1369 * Note that threads passed to the callback might no longer run (see
1370 * $(D Thread.)$(LREF isRunning)).
1373 * dg = The supplied code as a delegate.
1376 * Zero if all elemented are visited, nonzero if not.
1378 static int opApply(scope int delegate(ref Thread) dg)
1380 import core.stdc.stdlib : free, realloc;
1382 static void resize(ref Thread[] buf, size_t nlen)
1384 buf = (cast(Thread*)realloc(buf.ptr, nlen * Thread.sizeof))[0 .. nlen];
1386 auto buf = getAllImpl!resize;
1387 scope(exit) if (buf.ptr) free(buf.ptr);
1391 if (auto res = dg(t))
1399 auto t1 = new Thread({
1400 foreach (_; 0 .. 20)
1403 auto t2 = new Thread({
1404 foreach (_; 0 .. 20)
1411 private static Thread[] getAllImpl(alias resize)()
1418 immutable len = atomicLoad!(MemoryOrder.raw)(*cast(shared)&sm_tlen);
1420 assert(buf.length == len);
1421 synchronized (slock)
1426 for (Thread t = sm_tbeg; t; t = t.next)
1434 ///////////////////////////////////////////////////////////////////////////
1435 // Stuff That Should Go Away
1436 ///////////////////////////////////////////////////////////////////////////
1441 // Initializes a thread object which has no associated executable function.
1442 // This is used for the main thread initialized in thread_init().
1444 this(size_t sz = 0) @safe pure nothrow @nogc
1450 // stack size must be a multiple of PAGESIZE
1452 sz -= sz % PAGESIZE;
1453 // and at least PTHREAD_STACK_MIN
1454 if (PTHREAD_STACK_MIN > sz)
1455 sz = PTHREAD_STACK_MIN;
1465 // Thread entry point. Invokes the function or delegate passed on
1466 // construction (if any).
1486 // The type of routine passed on thread construction.
1501 alias TLSKey = uint;
1503 else version (Posix)
1505 alias TLSKey = pthread_key_t;
1512 static Thread sm_this;
1516 // Main process thread
1518 __gshared Thread sm_main;
1522 // set when suspend failed and should be retried, see Issue 13416
1523 shared bool m_suspendagain;
1528 // Standard thread data
1534 else version (Darwin)
1536 mach_port_t m_tmach;
1543 void function() m_fn;
1544 void delegate() m_dg;
1549 shared bool m_isRunning;
1552 bool m_isInCriticalRegion;
1553 Throwable m_unhandled;
1557 __gshared bool m_isRTClass;
1561 ///////////////////////////////////////////////////////////////////////////
1562 // Storage of Active Thread
1563 ///////////////////////////////////////////////////////////////////////////
1567 // Sets a thread-local reference to the current thread object.
1569 static void setThis( Thread t ) nothrow @nogc
1576 ///////////////////////////////////////////////////////////////////////////
1577 // Thread Context and GC Scanning Support
1578 ///////////////////////////////////////////////////////////////////////////
1581 final void pushContext( Context* c ) nothrow @nogc
1584 assert( !c.within );
1588 m_curr.ehContext = swapContext(c.ehContext);
1594 final void popContext() nothrow @nogc
1597 assert( m_curr && m_curr.within );
1601 Context* c = m_curr;
1603 c.ehContext = swapContext(m_curr.ehContext);
1608 final Context* topContext() nothrow @nogc
1619 static struct Context
1624 /// Slot for the EH implementation to keep some state for each stack
1625 /// (will be necessary for exception chaining, etc.). Opaque as far as
1626 /// we are concerned here.
1644 uint[8] m_reg; // edi,esi,ebp,esp,ebx,edx,ecx,eax
1646 else version (X86_64)
1648 ulong[16] m_reg; // rdi,rsi,rbp,rsp,rbx,rdx,rcx,rax
1649 // r8,r9,r10,r11,r12,r13,r14,r15
1653 static assert(false, "Architecture not supported." );
1656 else version (Darwin)
1660 uint[8] m_reg; // edi,esi,ebp,esp,ebx,edx,ecx,eax
1662 else version (X86_64)
1664 ulong[16] m_reg; // rdi,rsi,rbp,rsp,rbx,rdx,rcx,rax
1665 // r8,r9,r10,r11,r12,r13,r14,r15
1669 static assert(false, "Architecture not supported." );
1675 ///////////////////////////////////////////////////////////////////////////
1676 // GC Scanning Support
1677 ///////////////////////////////////////////////////////////////////////////
1680 // NOTE: The GC scanning process works like so:
1682 // 1. Suspend all threads.
1683 // 2. Scan the stacks of all suspended threads for roots.
1684 // 3. Resume all threads.
1686 // Step 1 and 3 require a list of all threads in the system, while
1687 // step 2 requires a list of all thread stacks (each represented by
1688 // a Context struct). Traditionally, there was one stack per thread
1689 // and the Context structs were not necessary. However, Fibers have
1690 // changed things so that each thread has its own 'main' stack plus
1691 // an arbitrary number of nested stacks (normally referenced via
1692 // m_curr). Also, there may be 'free-floating' stacks in the system,
1693 // which are Fibers that are not currently executing on any specific
1694 // thread but are still being processed and still contain valid
1697 // To support all of this, the Context struct has been created to
1698 // represent a stack range, and a global list of Context structs has
1699 // been added to enable scanning of these stack ranges. The lifetime
1700 // (and presence in the Context list) of a thread's 'main' stack will
1701 // be equivalent to the thread's lifetime. So the Ccontext will be
1702 // added to the list on thread entry, and removed from the list on
1703 // thread exit (which is essentially the same as the presence of a
1704 // Thread object in its own global list). The lifetime of a Fiber's
1705 // context, however, will be tied to the lifetime of the Fiber object
1706 // itself, and Fibers are expected to add/remove their Context struct
1707 // on construction/deletion.
1711 // All use of the global thread lists/array should synchronize on this lock.
1713 // Careful as the GC acquires this lock after the GC lock to suspend all
1714 // threads any GC usage with slock held can result in a deadlock through
1715 // lock order inversion.
1716 @property static Mutex slock() nothrow @nogc
1718 return cast(Mutex)_slock.ptr;
1721 @property static Mutex criticalRegionLock() nothrow @nogc
1723 return cast(Mutex)_criticalRegionLock.ptr;
1726 __gshared align(mutexAlign) void[mutexClassInstanceSize] _slock;
1727 __gshared align(mutexAlign) void[mutexClassInstanceSize] _criticalRegionLock;
1729 static void initLocks()
1731 _slock[] = typeid(Mutex).initializer[];
1732 (cast(Mutex)_slock.ptr).__ctor();
1734 _criticalRegionLock[] = typeid(Mutex).initializer[];
1735 (cast(Mutex)_criticalRegionLock.ptr).__ctor();
1738 static void termLocks()
1740 (cast(Mutex)_slock.ptr).__dtor();
1741 (cast(Mutex)_criticalRegionLock.ptr).__dtor();
1744 __gshared Context* sm_cbeg;
1746 __gshared Thread sm_tbeg;
1747 __gshared size_t sm_tlen;
1749 // can't use rt.util.array in public code
1750 __gshared Thread* pAboutToStart;
1751 __gshared size_t nAboutToStart;
1754 // Used for ordering threads in the global thread list.
1760 ///////////////////////////////////////////////////////////////////////////
1761 // Global Context List Operations
1762 ///////////////////////////////////////////////////////////////////////////
1766 // Add a context to the global context list.
1768 static void add( Context* c ) nothrow @nogc
1772 assert( !c.next && !c.prev );
1776 slock.lock_nothrow();
1777 scope(exit) slock.unlock_nothrow();
1778 assert(!suspendDepth); // must be 0 b/c it's only set with slock held
1790 // Remove a context from the global context list.
1792 // This assumes slock being acquired. This isn't done here to
1793 // avoid double locking when called from remove(Thread)
1794 static void remove( Context* c ) nothrow @nogc
1798 assert( c.next || c.prev );
1803 c.prev.next = c.next;
1805 c.next.prev = c.prev;
1808 // NOTE: Don't null out c.next or c.prev because opApply currently
1809 // follows c.next after removing a node. This could be easily
1810 // addressed by simply returning the next node from this
1811 // function, however, a context should never be re-added to the
1812 // list anyway and having next and prev be non-null is a good way
1817 ///////////////////////////////////////////////////////////////////////////
1818 // Global Thread List Operations
1819 ///////////////////////////////////////////////////////////////////////////
1823 // Add a thread to the global thread list.
1825 static void add( Thread t, bool rmAboutToStart = true ) nothrow @nogc
1829 assert( !t.next && !t.prev );
1833 slock.lock_nothrow();
1834 scope(exit) slock.unlock_nothrow();
1835 assert(t.isRunning); // check this with slock to ensure pthread_create already returned
1836 assert(!suspendDepth); // must be 0 b/c it's only set with slock held
1841 foreach (i, thr; pAboutToStart[0 .. nAboutToStart])
1850 import core.stdc.string : memmove;
1851 memmove(pAboutToStart + idx, pAboutToStart + idx + 1, Thread.sizeof * (nAboutToStart - idx - 1));
1853 cast(Thread*)realloc(pAboutToStart, Thread.sizeof * --nAboutToStart);
1867 // Remove a thread from the global thread list.
1869 static void remove( Thread t ) nothrow @nogc
1876 // Thread was already removed earlier, might happen b/c of thread_detachInstance
1877 if (!t.next && !t.prev)
1879 slock.lock_nothrow();
1881 // NOTE: When a thread is removed from the global thread list its
1882 // main context is invalid and should be removed as well.
1883 // It is possible that t.m_curr could reference more
1884 // than just the main context if the thread exited abnormally
1885 // (if it was terminated), but we must assume that the user
1886 // retains a reference to them and that they may be re-used
1887 // elsewhere. Therefore, it is the responsibility of any
1888 // object that creates contexts to clean them up properly
1889 // when it is done with them.
1890 remove( &t.m_main );
1893 t.prev.next = t.next;
1895 t.next.prev = t.prev;
1898 t.prev = t.next = null;
1901 // NOTE: Don't null out t.next or t.prev because opApply currently
1902 // follows t.next after removing a node. This could be easily
1903 // addressed by simply returning the next node from this
1904 // function, however, a thread should never be re-added to the
1905 // list anyway and having next and prev be non-null is a good way
1907 slock.unlock_nothrow();
1914 class DerivedThread : Thread
1924 // Derived thread running.
1930 // Composed thread running.
1933 // create and start instances of each type
1934 auto derived = new DerivedThread().start();
1935 auto composed = new Thread(&threadFunc).start();
1937 // Codes to run in the newly created thread.
1955 enum MSG = "Test message.";
1962 throw new Exception( MSG );
1964 assert( false, "Expected rethrown exception." );
1966 catch ( Throwable t )
1968 assert( t.msg == MSG );
1973 ///////////////////////////////////////////////////////////////////////////////
1974 // GC Support Routines
1975 ///////////////////////////////////////////////////////////////////////////////
1980 * Instruct the thread module, when initialized, to use a different set of
1981 * signals besides SIGUSR1 and SIGUSR2 for suspension and resumption of threads.
1982 * This function should be called at most once, prior to thread_init().
1983 * This function is Posix-only.
1985 extern (C) void thread_setGCSignals(int suspendSignalNo, int resumeSignalNo) nothrow @nogc
1989 else version (Posix)
1991 extern (C) void thread_setGCSignals(int suspendSignalNo, int resumeSignalNo) nothrow @nogc
1994 assert(suspendSignalNumber == 0);
1995 assert(resumeSignalNumber == 0);
1996 assert(suspendSignalNo != 0);
1997 assert(resumeSignalNo != 0);
2001 assert(suspendSignalNumber != 0);
2002 assert(resumeSignalNumber != 0);
2006 suspendSignalNumber = suspendSignalNo;
2007 resumeSignalNumber = resumeSignalNo;
2013 __gshared int suspendSignalNumber;
2014 __gshared int resumeSignalNumber;
2018 * Initializes the thread module. This function must be called by the
2019 * garbage collector on startup and before any other thread routines
2022 extern (C) void thread_init()
2024 // NOTE: If thread_init itself performs any allocations then the thread
2025 // routines reserved for garbage collector use may be called while
2026 // thread_init is being processed. However, since no memory should
2027 // exist to be scanned at this point, it is sufficient for these
2028 // functions to detect the condition and return immediately.
2031 // The Android VM runtime intercepts SIGUSR1 and apparently doesn't allow
2032 // its signal handler to run, so swap the two signals on Android, since
2033 // thread_resumeHandler does nothing.
2034 version (Android) thread_setGCSignals(SIGUSR2, SIGUSR1);
2039 else version (Posix)
2041 if ( suspendSignalNumber == 0 )
2043 suspendSignalNumber = SIGUSR1;
2046 if ( resumeSignalNumber == 0 )
2048 resumeSignalNumber = SIGUSR2;
2052 sigaction_t sigusr1 = void;
2053 sigaction_t sigusr2 = void;
2055 // This is a quick way to zero-initialize the structs without using
2056 // memset or creating a link dependency on their static initializer.
2057 (cast(byte*) &sigusr1)[0 .. sigaction_t.sizeof] = 0;
2058 (cast(byte*) &sigusr2)[0 .. sigaction_t.sizeof] = 0;
2060 // NOTE: SA_RESTART indicates that system calls should restart if they
2061 // are interrupted by a signal, but this is not available on all
2062 // Posix systems, even those that support multithreading.
2063 static if ( __traits( compiles, SA_RESTART ) )
2064 sigusr1.sa_flags = SA_RESTART;
2066 sigusr1.sa_flags = 0;
2067 sigusr1.sa_handler = &thread_suspendHandler;
2068 // NOTE: We want to ignore all signals while in this handler, so fill
2069 // sa_mask to indicate this.
2070 status = sigfillset( &sigusr1.sa_mask );
2071 assert( status == 0 );
2073 // NOTE: Since resumeSignalNumber should only be issued for threads within the
2074 // suspend handler, we don't want this signal to trigger a
2076 sigusr2.sa_flags = 0;
2077 sigusr2.sa_handler = &thread_resumeHandler;
2078 // NOTE: We want to ignore all signals while in this handler, so fill
2079 // sa_mask to indicate this.
2080 status = sigfillset( &sigusr2.sa_mask );
2081 assert( status == 0 );
2083 status = sigaction( suspendSignalNumber, &sigusr1, null );
2084 assert( status == 0 );
2086 status = sigaction( resumeSignalNumber, &sigusr2, null );
2087 assert( status == 0 );
2089 status = sem_init( &suspendCount, 0, 0 );
2090 assert( status == 0 );
2092 Thread.sm_main = thread_attachThis();
2097 * Terminates the thread module. No other thread routine may be called
2100 extern (C) void thread_term()
2102 assert(Thread.sm_tbeg && Thread.sm_tlen == 1);
2103 assert(!Thread.nAboutToStart);
2104 if (Thread.pAboutToStart) // in case realloc(p, 0) doesn't return null
2106 free(Thread.pAboutToStart);
2107 Thread.pAboutToStart = null;
2116 extern (C) bool thread_isMainThread() nothrow @nogc
2118 return Thread.getThis() is Thread.sm_main;
2123 * Registers the calling thread for use with the D Runtime. If this routine
2124 * is called for a thread which is already registered, no action is performed.
2126 * NOTE: This routine does not run thread-local static constructors when called.
2127 * If full functionality as a D thread is desired, the following function
2128 * must be called after thread_attachThis:
2130 * extern (C) void rt_moduleTlsCtor();
2132 extern (C) Thread thread_attachThis()
2134 GC.disable(); scope(exit) GC.enable();
2136 if (auto t = Thread.getThis())
2139 Thread thisThread = new Thread();
2140 Thread.Context* thisContext = &thisThread.m_main;
2141 assert( thisContext == thisThread.m_curr );
2145 thisThread.m_addr = GetCurrentThreadId();
2146 thisThread.m_hndl = GetCurrentThreadHandle();
2147 thisContext.bstack = getStackBottom();
2148 thisContext.tstack = thisContext.bstack;
2150 else version (Posix)
2152 thisThread.m_addr = pthread_self();
2153 thisContext.bstack = getStackBottom();
2154 thisContext.tstack = thisContext.bstack;
2156 atomicStore!(MemoryOrder.raw)(thisThread.m_isRunning, true);
2158 thisThread.m_isDaemon = true;
2159 thisThread.m_tlsgcdata = rt_tlsgc_init();
2160 Thread.setThis( thisThread );
2164 thisThread.m_tmach = pthread_mach_thread_np( thisThread.m_addr );
2165 assert( thisThread.m_tmach != thisThread.m_tmach.init );
2168 Thread.add( thisThread, false );
2169 Thread.add( thisContext );
2170 if ( Thread.sm_main !is null )
2171 multiThreadedFlag = true;
2178 // NOTE: These calls are not safe on Posix systems that use signals to
2179 // perform garbage collection. The suspendHandler uses getThis()
2180 // to get the thread handle so getThis() must be a simple call.
2181 // Mutexes can't safely be acquired inside signal handlers, and
2182 // even if they could, the mutex needed (Thread.slock) is held by
2183 // thread_suspendAll(). So in short, these routines will remain
2184 // Windows-specific. If they are truly needed elsewhere, the
2185 // suspendHandler will need a way to call a version of getThis()
2186 // that only does the TLS lookup without the fancy fallback stuff.
2189 extern (C) Thread thread_attachByAddr( ThreadID addr )
2191 return thread_attachByAddrB( addr, getThreadStackBottom( addr ) );
2196 extern (C) Thread thread_attachByAddrB( ThreadID addr, void* bstack )
2198 GC.disable(); scope(exit) GC.enable();
2200 if (auto t = thread_findByAddr(addr))
2203 Thread thisThread = new Thread();
2204 Thread.Context* thisContext = &thisThread.m_main;
2205 assert( thisContext == thisThread.m_curr );
2207 thisThread.m_addr = addr;
2208 thisContext.bstack = bstack;
2209 thisContext.tstack = thisContext.bstack;
2211 thisThread.m_isDaemon = true;
2213 if ( addr == GetCurrentThreadId() )
2215 thisThread.m_hndl = GetCurrentThreadHandle();
2216 thisThread.m_tlsgcdata = rt_tlsgc_init();
2217 Thread.setThis( thisThread );
2221 thisThread.m_hndl = OpenThreadHandle( addr );
2222 impersonate_thread(addr,
2224 thisThread.m_tlsgcdata = rt_tlsgc_init();
2225 Thread.setThis( thisThread );
2229 Thread.add( thisThread, false );
2230 Thread.add( thisContext );
2231 if ( Thread.sm_main !is null )
2232 multiThreadedFlag = true;
2239 * Deregisters the calling thread from use with the runtime. If this routine
2240 * is called for a thread which is not registered, the result is undefined.
2242 * NOTE: This routine does not run thread-local static destructors when called.
2243 * If full functionality as a D thread is desired, the following function
2244 * must be called after thread_detachThis, particularly if the thread is
2245 * being detached at some indeterminate time before program termination:
2247 * $(D extern(C) void rt_moduleTlsDtor();)
2249 extern (C) void thread_detachThis() nothrow @nogc
2251 if (auto t = Thread.getThis())
2257 * Deregisters the given thread from use with the runtime. If this routine
2258 * is called for a thread which is not registered, the result is undefined.
2260 * NOTE: This routine does not run thread-local static destructors when called.
2261 * If full functionality as a D thread is desired, the following function
2262 * must be called by the detached thread, particularly if the thread is
2263 * being detached at some indeterminate time before program termination:
2265 * $(D extern(C) void rt_moduleTlsDtor();)
2267 extern (C) void thread_detachByAddr( ThreadID addr )
2269 if ( auto t = thread_findByAddr( addr ) )
2275 extern (C) void thread_detachInstance( Thread t ) nothrow @nogc
2283 import core.sync.semaphore;
2284 auto sem = new Semaphore();
2286 auto t = new Thread(
2289 Thread.sleep(100.msecs);
2292 sem.wait(); // thread cannot be detached while being started
2293 thread_detachInstance(t);
2294 foreach (t2; Thread)
2301 * Search the list of all threads for a thread with the given thread identifier.
2304 * addr = The thread identifier to search for.
2306 * The thread object associated with the thread identifier, null if not found.
2308 static Thread thread_findByAddr( ThreadID addr )
2310 Thread.slock.lock_nothrow();
2311 scope(exit) Thread.slock.unlock_nothrow();
2313 // also return just spawned thread so that
2314 // DLL_THREAD_ATTACH knows it's a D thread
2315 foreach (t; Thread.pAboutToStart[0 .. Thread.nAboutToStart])
2316 if (t.m_addr == addr)
2320 if (t.m_addr == addr)
2328 * Sets the current thread to a specific reference. Only to be used
2329 * when dealing with externally-created threads (in e.g. C code).
2330 * The primary use of this function is when Thread.getThis() must
2331 * return a sensible value in, for example, TLS destructors. In
2332 * other words, don't touch this unless you know what you're doing.
2335 * t = A reference to the current thread. May be null.
2337 extern (C) void thread_setThis(Thread t) nothrow @nogc
2344 * Joins all non-daemon threads that are currently running. This is done by
2345 * performing successive scans through the thread list until a scan consists
2346 * of only daemon threads.
2348 extern (C) void thread_joinAll()
2351 Thread.slock.lock_nothrow();
2352 // wait for just spawned threads
2353 if (Thread.nAboutToStart)
2355 Thread.slock.unlock_nothrow();
2360 // join all non-daemon threads, the main thread is also a daemon
2361 auto t = Thread.sm_tbeg;
2370 else if (t.isDaemon)
2376 Thread.slock.unlock_nothrow();
2377 t.join(); // might rethrow
2378 goto Lagain; // must restart iteration b/c of unlock
2381 Thread.slock.unlock_nothrow();
2386 * Performs intermediate shutdown of the thread module.
2388 shared static ~this()
2390 // NOTE: The functionality related to garbage collection must be minimally
2391 // operable after this dtor completes. Therefore, only minimal
2392 // cleanup may occur.
2393 auto t = Thread.sm_tbeg;
2404 // Used for needLock below.
2405 private __gshared bool multiThreadedFlag = false;
2407 version (PPC64) version = ExternStackShell;
2409 version (ExternStackShell)
2411 extern(D) public void callWithStackShell(scope void delegate(void* sp) nothrow fn) nothrow;
2415 // Calls the given delegate, passing the current thread's stack pointer to it.
2416 private void callWithStackShell(scope void delegate(void* sp) nothrow fn) nothrow
2423 // The purpose of the 'shell' is to ensure all the registers get
2424 // put on the stack so they'll be scanned. We only need to push
2425 // the callee-save registers.
2430 __builtin_unwind_init();
2433 else version (AsmX86_Posix)
2435 size_t[3] regs = void;
2436 asm pure nothrow @nogc
2438 mov [regs + 0 * 4], EBX;
2439 mov [regs + 1 * 4], ESI;
2440 mov [regs + 2 * 4], EDI;
2445 else version (AsmX86_Windows)
2447 size_t[3] regs = void;
2448 asm pure nothrow @nogc
2450 mov [regs + 0 * 4], EBX;
2451 mov [regs + 1 * 4], ESI;
2452 mov [regs + 2 * 4], EDI;
2457 else version (AsmX86_64_Posix)
2459 size_t[5] regs = void;
2460 asm pure nothrow @nogc
2462 mov [regs + 0 * 8], RBX;
2463 mov [regs + 1 * 8], R12;
2464 mov [regs + 2 * 8], R13;
2465 mov [regs + 3 * 8], R14;
2466 mov [regs + 4 * 8], R15;
2471 else version (AsmX86_64_Windows)
2473 size_t[7] regs = void;
2474 asm pure nothrow @nogc
2476 mov [regs + 0 * 8], RBX;
2477 mov [regs + 1 * 8], RSI;
2478 mov [regs + 2 * 8], RDI;
2479 mov [regs + 3 * 8], R12;
2480 mov [regs + 4 * 8], R13;
2481 mov [regs + 5 * 8], R14;
2482 mov [regs + 6 * 8], R15;
2489 static assert(false, "Architecture not supported.");
2496 // Used for suspendAll/resumeAll below.
2497 private __gshared uint suspendDepth = 0;
2500 * Suspend the specified thread and load stack and register information for
2501 * use by thread_scanAll. If the supplied thread is the calling thread,
2502 * stack and register information will be loaded but the thread will not
2503 * be suspended. If the suspend operation fails and the thread is not
2504 * running then it will be removed from the global thread list, otherwise
2505 * an exception will be thrown.
2508 * t = The thread to suspend.
2511 * ThreadError if the suspend operation fails for a running thread.
2513 * Whether the thread is now suspended (true) or terminated (false).
2515 private bool suspend( Thread t ) nothrow
2517 Duration waittime = dur!"usecs"(10);
2524 else if (t.m_isInCriticalRegion)
2526 Thread.criticalRegionLock.unlock_nothrow();
2527 Thread.sleep(waittime);
2528 if (waittime < dur!"msecs"(10)) waittime *= 2;
2529 Thread.criticalRegionLock.lock_nothrow();
2535 if ( t.m_addr != GetCurrentThreadId() && SuspendThread( t.m_hndl ) == 0xFFFFFFFF )
2542 onThreadError( "Unable to suspend thread" );
2545 CONTEXT context = void;
2546 context.ContextFlags = CONTEXT_INTEGER | CONTEXT_CONTROL;
2548 if ( !GetThreadContext( t.m_hndl, &context ) )
2549 onThreadError( "Unable to load thread context" );
2553 t.m_curr.tstack = cast(void*) context.Esp;
2554 // eax,ebx,ecx,edx,edi,esi,ebp,esp
2555 t.m_reg[0] = context.Eax;
2556 t.m_reg[1] = context.Ebx;
2557 t.m_reg[2] = context.Ecx;
2558 t.m_reg[3] = context.Edx;
2559 t.m_reg[4] = context.Edi;
2560 t.m_reg[5] = context.Esi;
2561 t.m_reg[6] = context.Ebp;
2562 t.m_reg[7] = context.Esp;
2564 else version (X86_64)
2567 t.m_curr.tstack = cast(void*) context.Rsp;
2568 // rax,rbx,rcx,rdx,rdi,rsi,rbp,rsp
2569 t.m_reg[0] = context.Rax;
2570 t.m_reg[1] = context.Rbx;
2571 t.m_reg[2] = context.Rcx;
2572 t.m_reg[3] = context.Rdx;
2573 t.m_reg[4] = context.Rdi;
2574 t.m_reg[5] = context.Rsi;
2575 t.m_reg[6] = context.Rbp;
2576 t.m_reg[7] = context.Rsp;
2577 // r8,r9,r10,r11,r12,r13,r14,r15
2578 t.m_reg[8] = context.R8;
2579 t.m_reg[9] = context.R9;
2580 t.m_reg[10] = context.R10;
2581 t.m_reg[11] = context.R11;
2582 t.m_reg[12] = context.R12;
2583 t.m_reg[13] = context.R13;
2584 t.m_reg[14] = context.R14;
2585 t.m_reg[15] = context.R15;
2589 static assert(false, "Architecture not supported." );
2592 else version (Darwin)
2594 if ( t.m_addr != pthread_self() && thread_suspend( t.m_tmach ) != KERN_SUCCESS )
2601 onThreadError( "Unable to suspend thread" );
2606 x86_thread_state32_t state = void;
2607 mach_msg_type_number_t count = x86_THREAD_STATE32_COUNT;
2609 if ( thread_get_state( t.m_tmach, x86_THREAD_STATE32, &state, &count ) != KERN_SUCCESS )
2610 onThreadError( "Unable to load thread state" );
2612 t.m_curr.tstack = cast(void*) state.esp;
2613 // eax,ebx,ecx,edx,edi,esi,ebp,esp
2614 t.m_reg[0] = state.eax;
2615 t.m_reg[1] = state.ebx;
2616 t.m_reg[2] = state.ecx;
2617 t.m_reg[3] = state.edx;
2618 t.m_reg[4] = state.edi;
2619 t.m_reg[5] = state.esi;
2620 t.m_reg[6] = state.ebp;
2621 t.m_reg[7] = state.esp;
2623 else version (X86_64)
2625 x86_thread_state64_t state = void;
2626 mach_msg_type_number_t count = x86_THREAD_STATE64_COUNT;
2628 if ( thread_get_state( t.m_tmach, x86_THREAD_STATE64, &state, &count ) != KERN_SUCCESS )
2629 onThreadError( "Unable to load thread state" );
2631 t.m_curr.tstack = cast(void*) state.rsp;
2632 // rax,rbx,rcx,rdx,rdi,rsi,rbp,rsp
2633 t.m_reg[0] = state.rax;
2634 t.m_reg[1] = state.rbx;
2635 t.m_reg[2] = state.rcx;
2636 t.m_reg[3] = state.rdx;
2637 t.m_reg[4] = state.rdi;
2638 t.m_reg[5] = state.rsi;
2639 t.m_reg[6] = state.rbp;
2640 t.m_reg[7] = state.rsp;
2641 // r8,r9,r10,r11,r12,r13,r14,r15
2642 t.m_reg[8] = state.r8;
2643 t.m_reg[9] = state.r9;
2644 t.m_reg[10] = state.r10;
2645 t.m_reg[11] = state.r11;
2646 t.m_reg[12] = state.r12;
2647 t.m_reg[13] = state.r13;
2648 t.m_reg[14] = state.r14;
2649 t.m_reg[15] = state.r15;
2653 static assert(false, "Architecture not supported." );
2656 else version (Posix)
2658 if ( t.m_addr != pthread_self() )
2660 if ( pthread_kill( t.m_addr, suspendSignalNumber ) != 0 )
2667 onThreadError( "Unable to suspend thread" );
2670 else if ( !t.m_lock )
2672 t.m_curr.tstack = getStackTop();
2679 * Suspend all threads but the calling thread for "stop the world" garbage
2680 * collection runs. This function may be called multiple times, and must
2681 * be followed by a matching number of calls to thread_resumeAll before
2682 * processing is resumed.
2685 * ThreadError if the suspend operation fails for a running thread.
2687 extern (C) void thread_suspendAll() nothrow
2689 // NOTE: We've got an odd chicken & egg problem here, because while the GC
2690 // is required to call thread_init before calling any other thread
2691 // routines, thread_init may allocate memory which could in turn
2692 // trigger a collection. Thus, thread_suspendAll, thread_scanAll,
2693 // and thread_resumeAll must be callable before thread_init
2694 // completes, with the assumption that no other GC memory has yet
2695 // been allocated by the system, and thus there is no risk of losing
2696 // data if the global thread list is empty. The check of
2697 // Thread.sm_tbeg below is done to ensure thread_init has completed,
2698 // and therefore that calling Thread.getThis will not result in an
2699 // error. For the short time when Thread.sm_tbeg is null, there is
2700 // no reason not to simply call the multithreaded code below, with
2701 // the expectation that the foreach loop will never be entered.
2702 if ( !multiThreadedFlag && Thread.sm_tbeg )
2704 if ( ++suspendDepth == 1 )
2705 suspend( Thread.getThis() );
2710 Thread.slock.lock_nothrow();
2712 if ( ++suspendDepth > 1 )
2715 Thread.criticalRegionLock.lock_nothrow();
2716 scope (exit) Thread.criticalRegionLock.unlock_nothrow();
2718 auto t = Thread.sm_tbeg;
2729 else version (Posix)
2731 // subtract own thread
2735 // wait for semaphore notifications
2738 while (sem_wait(&suspendCount) != 0)
2741 onThreadError("Unable to wait for semaphore");
2747 // avoid deadlocks, see Issue 13416
2752 if (t.m_suspendagain && suspend(t))
2764 * Resume the specified thread and unload stack and register information.
2765 * If the supplied thread is the calling thread, stack and register
2766 * information will be unloaded but the thread will not be resumed. If
2767 * the resume operation fails and the thread is not running then it will
2768 * be removed from the global thread list, otherwise an exception will be
2772 * t = The thread to resume.
2775 * ThreadError if the resume fails for a running thread.
2777 private void resume( Thread t ) nothrow
2781 if ( t.m_addr != GetCurrentThreadId() && ResumeThread( t.m_hndl ) == 0xFFFFFFFF )
2788 onThreadError( "Unable to resume thread" );
2792 t.m_curr.tstack = t.m_curr.bstack;
2793 t.m_reg[0 .. $] = 0;
2795 else version (Darwin)
2797 if ( t.m_addr != pthread_self() && thread_resume( t.m_tmach ) != KERN_SUCCESS )
2804 onThreadError( "Unable to resume thread" );
2808 t.m_curr.tstack = t.m_curr.bstack;
2809 t.m_reg[0 .. $] = 0;
2811 else version (Posix)
2813 if ( t.m_addr != pthread_self() )
2815 if ( pthread_kill( t.m_addr, resumeSignalNumber ) != 0 )
2822 onThreadError( "Unable to resume thread" );
2825 else if ( !t.m_lock )
2827 t.m_curr.tstack = t.m_curr.bstack;
2833 * Resume all threads but the calling thread for "stop the world" garbage
2834 * collection runs. This function must be called once for each preceding
2835 * call to thread_suspendAll before the threads are actually resumed.
2838 * This routine must be preceded by a call to thread_suspendAll.
2841 * ThreadError if the resume operation fails for a running thread.
2843 extern (C) void thread_resumeAll() nothrow
2846 assert( suspendDepth > 0 );
2850 // NOTE: See thread_suspendAll for the logic behind this.
2851 if ( !multiThreadedFlag && Thread.sm_tbeg )
2853 if ( --suspendDepth == 0 )
2854 resume( Thread.getThis() );
2858 scope(exit) Thread.slock.unlock_nothrow();
2860 if ( --suspendDepth > 0 )
2863 for ( Thread t = Thread.sm_tbeg; t; t = t.next )
2865 // NOTE: We do not need to care about critical regions at all
2866 // here. thread_suspendAll takes care of everything.
2873 * Indicates the kind of scan being performed by $(D thread_scanAllType).
2877 stack, /// The stack and/or registers are being scanned.
2878 tls, /// TLS data is being scanned.
2881 alias ScanAllThreadsFn = void delegate(void*, void*) nothrow; /// The scanning function.
2882 alias ScanAllThreadsTypeFn = void delegate(ScanType, void*, void*) nothrow; /// ditto
2885 * The main entry point for garbage collection. The supplied delegate
2886 * will be passed ranges representing both stack and register values.
2889 * scan = The scanner function. It should scan from p1 through p2 - 1.
2892 * This routine must be preceded by a call to thread_suspendAll.
2894 extern (C) void thread_scanAllType( scope ScanAllThreadsTypeFn scan ) nothrow
2897 assert( suspendDepth > 0 );
2901 callWithStackShell(sp => scanAllTypeImpl(scan, sp));
2905 private void scanAllTypeImpl( scope ScanAllThreadsTypeFn scan, void* curStackTop ) nothrow
2907 Thread thisThread = null;
2908 void* oldStackTop = null;
2910 if ( Thread.sm_tbeg )
2912 thisThread = Thread.getThis();
2913 if ( !thisThread.m_lock )
2915 oldStackTop = thisThread.m_curr.tstack;
2916 thisThread.m_curr.tstack = curStackTop;
2922 if ( Thread.sm_tbeg )
2924 if ( !thisThread.m_lock )
2926 thisThread.m_curr.tstack = oldStackTop;
2931 // NOTE: Synchronizing on Thread.slock is not needed because this
2932 // function may only be called after all other threads have
2933 // been suspended from within the same lock.
2934 if (Thread.nAboutToStart)
2935 scan(ScanType.stack, Thread.pAboutToStart, Thread.pAboutToStart + Thread.nAboutToStart);
2937 for ( Thread.Context* c = Thread.sm_cbeg; c; c = c.next )
2939 version (StackGrowsDown)
2941 // NOTE: We can't index past the bottom of the stack
2942 // so don't do the "+1" for StackGrowsDown.
2943 if ( c.tstack && c.tstack < c.bstack )
2944 scan( ScanType.stack, c.tstack, c.bstack );
2948 if ( c.bstack && c.bstack < c.tstack )
2949 scan( ScanType.stack, c.bstack, c.tstack + 1 );
2953 for ( Thread t = Thread.sm_tbeg; t; t = t.next )
2957 // Ideally, we'd pass ScanType.regs or something like that, but this
2958 // would make portability annoying because it only makes sense on Windows.
2959 scan( ScanType.stack, t.m_reg.ptr, t.m_reg.ptr + t.m_reg.length );
2962 if (t.m_tlsgcdata !is null)
2963 rt_tlsgc_scan(t.m_tlsgcdata, (p1, p2) => scan(ScanType.tls, p1, p2));
2968 * The main entry point for garbage collection. The supplied delegate
2969 * will be passed ranges representing both stack and register values.
2972 * scan = The scanner function. It should scan from p1 through p2 - 1.
2975 * This routine must be preceded by a call to thread_suspendAll.
2977 extern (C) void thread_scanAll( scope ScanAllThreadsFn scan ) nothrow
2979 thread_scanAllType((type, p1, p2) => scan(p1, p2));
2984 * Signals that the code following this call is a critical region. Any code in
2985 * this region must finish running before the calling thread can be suspended
2986 * by a call to thread_suspendAll.
2988 * This function is, in particular, meant to help maintain garbage collector
2989 * invariants when a lock is not used.
2991 * A critical region is exited with thread_exitCriticalRegion.
2994 * Using critical regions is extremely error-prone. For instance, using locks
2995 * inside a critical region can easily result in a deadlock when another thread
2996 * holding the lock already got suspended.
2998 * The term and concept of a 'critical region' comes from
2999 * $(LINK2 https://github.com/mono/mono/blob/521f4a198e442573c400835ef19bbb36b60b0ebb/mono/metadata/sgen-gc.h#L925 Mono's SGen garbage collector).
3002 * The calling thread must be attached to the runtime.
3004 extern (C) void thread_enterCriticalRegion() @nogc
3007 assert(Thread.getThis());
3011 synchronized (Thread.criticalRegionLock)
3012 Thread.getThis().m_isInCriticalRegion = true;
3017 * Signals that the calling thread is no longer in a critical region. Following
3018 * a call to this function, the thread can once again be suspended.
3021 * The calling thread must be attached to the runtime.
3023 extern (C) void thread_exitCriticalRegion() @nogc
3026 assert(Thread.getThis());
3030 synchronized (Thread.criticalRegionLock)
3031 Thread.getThis().m_isInCriticalRegion = false;
3036 * Returns true if the current thread is in a critical region; otherwise, false.
3039 * The calling thread must be attached to the runtime.
3041 extern (C) bool thread_inCriticalRegion() @nogc
3044 assert(Thread.getThis());
3048 synchronized (Thread.criticalRegionLock)
3049 return Thread.getThis().m_isInCriticalRegion;
3054 * A callback for thread errors in D during collections. Since an allocation is not possible
3055 * a preallocated ThreadError will be used as the Error instance
3060 private void onThreadError(string msg = null, Throwable next = null) nothrow
3062 __gshared ThreadError error = new ThreadError(null);
3065 import core.exception : SuppressTraceInfo;
3066 error.info = SuppressTraceInfo.instance;
3073 assert(!thread_inCriticalRegion());
3076 thread_enterCriticalRegion();
3079 thread_exitCriticalRegion();
3081 assert(thread_inCriticalRegion());
3084 assert(!thread_inCriticalRegion());
3089 // NOTE: This entire test is based on the assumption that no
3090 // memory is allocated after the child thread is
3091 // started. If an allocation happens, a collection could
3092 // trigger, which would cause the synchronization below
3093 // to cause a deadlock.
3094 // NOTE: DO NOT USE LOCKS IN CRITICAL REGIONS IN NORMAL CODE.
3096 import core.sync.semaphore;
3098 auto sema = new Semaphore(),
3099 semb = new Semaphore();
3101 auto thr = new Thread(
3103 thread_enterCriticalRegion();
3104 assert(thread_inCriticalRegion());
3108 assert(thread_inCriticalRegion());
3110 thread_exitCriticalRegion();
3111 assert(!thread_inCriticalRegion());
3115 assert(!thread_inCriticalRegion());
3121 synchronized (Thread.criticalRegionLock)
3122 assert(thr.m_isInCriticalRegion);
3126 synchronized (Thread.criticalRegionLock)
3127 assert(!thr.m_isInCriticalRegion);
3135 import core.sync.semaphore;
3137 shared bool inCriticalRegion;
3138 auto sema = new Semaphore(),
3139 semb = new Semaphore();
3141 auto thr = new Thread(
3143 thread_enterCriticalRegion();
3144 inCriticalRegion = true;
3148 Thread.sleep(dur!"msecs"(1));
3149 inCriticalRegion = false;
3150 thread_exitCriticalRegion();
3155 assert(inCriticalRegion);
3158 thread_suspendAll();
3159 assert(!inCriticalRegion);
3164 * Indicates whether an address has been marked by the GC.
3168 no, /// Address is not marked.
3169 yes, /// Address is marked.
3170 unknown, /// Address is not managed by the GC.
3173 alias IsMarkedDg = int delegate( void* addr ) nothrow; /// The isMarked callback function.
3176 * This routine allows the runtime to process any special per-thread handling
3177 * for the GC. This is needed for taking into account any memory that is
3178 * referenced by non-scanned pointers but is about to be freed. That currently
3179 * means the array append cache.
3182 * isMarked = The function used to check if $(D addr) is marked.
3185 * This routine must be called just prior to resuming all threads.
3187 extern(C) void thread_processGCMarks( scope IsMarkedDg isMarked ) nothrow
3189 for ( Thread t = Thread.sm_tbeg; t; t = t.next )
3191 /* Can be null if collection was triggered between adding a
3192 * thread and calling rt_tlsgc_init.
3194 if (t.m_tlsgcdata !is null)
3195 rt_tlsgc_processGCMarks(t.m_tlsgcdata, isMarked);
3200 extern (C) @nogc nothrow
3202 version (CRuntime_Glibc) int pthread_getattr_np(pthread_t thread, pthread_attr_t* attr);
3203 version (FreeBSD) int pthread_attr_get_np(pthread_t thread, pthread_attr_t* attr);
3204 version (NetBSD) int pthread_attr_get_np(pthread_t thread, pthread_attr_t* attr);
3205 version (Solaris) int thr_stksegment(stack_t* stk);
3206 version (CRuntime_Bionic) int pthread_getattr_np(pthread_t thid, pthread_attr_t* attr);
3210 private void* getStackTop() nothrow @nogc
3212 version (D_InlineAsm_X86)
3213 asm pure nothrow @nogc { naked; mov EAX, ESP; ret; }
3214 else version (D_InlineAsm_X86_64)
3215 asm pure nothrow @nogc { naked; mov RAX, RSP; ret; }
3217 return __builtin_frame_address(0);
3219 static assert(false, "Architecture not supported.");
3223 private void* getStackBottom() nothrow @nogc
3227 version (D_InlineAsm_X86)
3228 asm pure nothrow @nogc { naked; mov EAX, FS:4; ret; }
3229 else version (D_InlineAsm_X86_64)
3230 asm pure nothrow @nogc
3236 else version (GNU_InlineAsm)
3241 asm pure nothrow @nogc { "movl %%fs:4, %0;" : "=r" bottom; }
3242 else version (X86_64)
3243 asm pure nothrow @nogc { "movq %%gs:8, %0;" : "=r" bottom; }
3245 static assert(false, "Platform not supported.");
3250 static assert(false, "Architecture not supported.");
3252 else version (Darwin)
3254 import core.sys.darwin.pthread;
3255 return pthread_get_stackaddr_np(pthread_self());
3257 else version (CRuntime_Glibc)
3259 pthread_attr_t attr;
3260 void* addr; size_t size;
3262 pthread_getattr_np(pthread_self(), &attr);
3263 pthread_attr_getstack(&attr, &addr, &size);
3264 pthread_attr_destroy(&attr);
3265 version (StackGrowsDown)
3269 else version (FreeBSD)
3271 pthread_attr_t attr;
3272 void* addr; size_t size;
3274 pthread_attr_init(&attr);
3275 pthread_attr_get_np(pthread_self(), &attr);
3276 pthread_attr_getstack(&attr, &addr, &size);
3277 pthread_attr_destroy(&attr);
3278 version (StackGrowsDown)
3282 else version (NetBSD)
3284 pthread_attr_t attr;
3285 void* addr; size_t size;
3287 pthread_attr_init(&attr);
3288 pthread_attr_get_np(pthread_self(), &attr);
3289 pthread_attr_getstack(&attr, &addr, &size);
3290 pthread_attr_destroy(&attr);
3291 version (StackGrowsDown)
3295 else version (Solaris)
3299 thr_stksegment(&stk);
3302 else version (CRuntime_Bionic)
3304 pthread_attr_t attr;
3305 void* addr; size_t size;
3307 pthread_getattr_np(pthread_self(), &attr);
3308 pthread_attr_getstack(&attr, &addr, &size);
3309 pthread_attr_destroy(&attr);
3310 version (StackGrowsDown)
3315 static assert(false, "Platform not supported.");
3320 * Returns the stack top of the currently active stack within the calling
3324 * The calling thread must be attached to the runtime.
3327 * The address of the stack top.
3329 extern (C) void* thread_stackTop() nothrow @nogc
3332 // Not strictly required, but it gives us more flexibility.
3333 assert(Thread.getThis());
3337 return getStackTop();
3342 * Returns the stack bottom of the currently active stack within the calling
3346 * The calling thread must be attached to the runtime.
3349 * The address of the stack bottom.
3351 extern (C) void* thread_stackBottom() nothrow @nogc
3354 assert(Thread.getThis());
3358 return Thread.getThis().topContext().bstack;
3362 ///////////////////////////////////////////////////////////////////////////////
3364 ///////////////////////////////////////////////////////////////////////////////
3368 * This class is intended to simplify certain common programming techniques.
3373 * Creates and starts a new Thread object that executes fn and adds it to
3374 * the list of tracked threads.
3377 * fn = The thread function.
3380 * A reference to the newly created thread.
3382 final Thread create( void function() fn )
3384 Thread t = new Thread( fn ).start();
3386 synchronized( this )
3395 * Creates and starts a new Thread object that executes dg and adds it to
3396 * the list of tracked threads.
3399 * dg = The thread function.
3402 * A reference to the newly created thread.
3404 final Thread create( void delegate() dg )
3406 Thread t = new Thread( dg ).start();
3408 synchronized( this )
3417 * Add t to the list of tracked threads if it is not already being tracked.
3420 * t = The thread to add.
3423 * t must not be null.
3425 final void add( Thread t )
3432 synchronized( this )
3440 * Removes t from the list of tracked threads. No operation will be
3441 * performed if t is not currently being tracked by this object.
3444 * t = The thread to remove.
3447 * t must not be null.
3449 final void remove( Thread t )
3456 synchronized( this )
3464 * Operates on all threads currently tracked by this object.
3466 final int opApply( scope int delegate( ref Thread ) dg )
3468 synchronized( this )
3472 // NOTE: This loop relies on the knowledge that m_all uses the
3473 // Thread object for both the key and the mapped value.
3474 foreach ( Thread t; m_all.keys )
3486 * Iteratively joins all tracked threads. This function will block add,
3487 * remove, and opApply until it completes.
3490 * rethrow = Rethrow any unhandled exception which may have caused the
3491 * current thread to terminate.
3494 * Any exception not handled by the joined threads.
3496 final void joinAll( bool rethrow = true )
3498 synchronized( this )
3500 // NOTE: This loop relies on the knowledge that m_all uses the
3501 // Thread object for both the key and the mapped value.
3502 foreach ( Thread t; m_all.keys )
3511 Thread[Thread] m_all;
3515 ///////////////////////////////////////////////////////////////////////////////
3516 // Fiber Platform Detection and Memory Allocation
3517 ///////////////////////////////////////////////////////////////////////////////
3522 version (D_InlineAsm_X86)
3525 version = AsmX86_Windows;
3526 else version (Posix)
3527 version = AsmX86_Posix;
3530 version = AlignFiberStackTo16Byte;
3532 else version (D_InlineAsm_X86_64)
3536 version = AsmX86_64_Windows;
3537 version = AlignFiberStackTo16Byte;
3539 else version (Posix)
3541 version = AsmX86_64_Posix;
3542 version = AlignFiberStackTo16Byte;
3547 version = AsmExternal;
3551 version = GNU_AsmX86_Windows;
3552 version = AlignFiberStackTo16Byte;
3554 else version (Posix)
3556 version = AsmX86_Posix;
3558 version = AlignFiberStackTo16Byte;
3561 else version (X86_64)
3565 // let X32 be handled by ucontext swapcontext
3569 version = AsmExternal;
3570 version = AlignFiberStackTo16Byte;
3573 version = GNU_AsmX86_64_Windows;
3574 else version (Posix)
3575 version = AsmX86_64_Posix;
3582 version = AsmPPC_Posix;
3583 version = AsmExternal;
3586 else version (PPC64)
3590 version = AlignFiberStackTo16Byte;
3593 else version (MIPS_O32)
3597 version = AsmMIPS_O32_Posix;
3598 version = AsmExternal;
3601 else version (AArch64)
3605 version = AsmAArch64_Posix;
3606 version = AsmExternal;
3607 version = AlignFiberStackTo16Byte;
3614 version = AsmARM_Posix;
3615 version = AsmExternal;
3618 else version (SPARC)
3620 // NOTE: The SPARC ABI specifies only doubleword alignment.
3621 version = AlignFiberStackTo16Byte;
3623 else version (SPARC64)
3625 version = AlignFiberStackTo16Byte;
3630 import core.sys.posix.unistd; // for sysconf
3632 version (AsmX86_Windows) {} else
3633 version (AsmX86_Posix) {} else
3634 version (AsmX86_64_Windows) {} else
3635 version (AsmX86_64_Posix) {} else
3636 version (AsmExternal) {} else
3638 // NOTE: The ucontext implementation requires architecture specific
3639 // data definitions to operate so testing for it must be done
3640 // by checking for the existence of ucontext_t rather than by
3641 // a version identifier. Please note that this is considered
3642 // an obsolescent feature according to the POSIX spec, so a
3643 // custom solution is still preferred.
3644 import core.sys.posix.ucontext;
3648 static immutable size_t PAGESIZE;
3649 version (Posix) static immutable size_t PTHREAD_STACK_MIN;
3653 shared static this()
3658 GetSystemInfo(&info);
3660 PAGESIZE = info.dwPageSize;
3661 assert(PAGESIZE < int.max);
3663 else version (Posix)
3665 PAGESIZE = cast(size_t)sysconf(_SC_PAGESIZE);
3666 PTHREAD_STACK_MIN = cast(size_t)sysconf(_SC_THREAD_STACK_MIN);
3670 static assert(0, "unimplemented");
3675 ///////////////////////////////////////////////////////////////////////////////
3676 // Fiber Entry Point and Context Switch
3677 ///////////////////////////////////////////////////////////////////////////////
3682 extern (C) void fiber_entryPoint() nothrow
3684 Fiber obj = Fiber.getThis();
3687 assert( Thread.getThis().m_curr is obj.m_ctxt );
3688 atomicStore!(MemoryOrder.raw)(*cast(shared)&Thread.getThis().m_lock, false);
3689 obj.m_ctxt.tstack = obj.m_ctxt.bstack;
3690 obj.m_state = Fiber.State.EXEC;
3696 catch ( Throwable t )
3698 obj.m_unhandled = t;
3701 static if ( __traits( compiles, ucontext_t ) )
3702 obj.m_ucur = &obj.m_utxt;
3704 obj.m_state = Fiber.State.TERM;
3708 // Look above the definition of 'class Fiber' for some information about the implementation of this routine
3709 version (AsmExternal)
3711 extern (C) void fiber_switchContext( void** oldp, void* newp ) nothrow @nogc;
3713 extern (C) void fiber_trampoline() nothrow;
3716 extern (C) void fiber_switchContext( void** oldp, void* newp ) nothrow @nogc
3718 // NOTE: The data pushed and popped in this routine must match the
3719 // default stack created by Fiber.initStack or the initial
3720 // switch into a new context will fail.
3722 version (AsmX86_Windows)
3724 asm pure nothrow @nogc
3728 // save current stack state
3734 push dword ptr FS:[0];
3735 push dword ptr FS:[4];
3736 push dword ptr FS:[8];
3739 // store oldp again with more accurate address
3740 mov EAX, dword ptr 8[EBP];
3742 // load newp to begin context switch
3743 mov ESP, dword ptr 12[EBP];
3745 // load saved state from new stack
3747 pop dword ptr FS:[8];
3748 pop dword ptr FS:[4];
3749 pop dword ptr FS:[0];
3755 // 'return' to complete switch
3760 else version (AsmX86_64_Windows)
3762 asm pure nothrow @nogc
3766 // save current stack state
3767 // NOTE: When changing the layout of registers on the stack,
3768 // make sure that the XMM registers are still aligned.
3769 // On function entry, the stack is guaranteed to not
3770 // be aligned to 16 bytes because of the return address
3780 // 7 registers = 56 bytes; stack is now aligned to 16 bytes
3782 movdqa [RSP + 144], XMM6;
3783 movdqa [RSP + 128], XMM7;
3784 movdqa [RSP + 112], XMM8;
3785 movdqa [RSP + 96], XMM9;
3786 movdqa [RSP + 80], XMM10;
3787 movdqa [RSP + 64], XMM11;
3788 movdqa [RSP + 48], XMM12;
3789 movdqa [RSP + 32], XMM13;
3790 movdqa [RSP + 16], XMM14;
3791 movdqa [RSP], XMM15;
3794 push qword ptr GS:[RAX];
3795 push qword ptr GS:8[RAX];
3796 push qword ptr GS:16[RAX];
3800 // load newp to begin context switch
3803 // load saved state from new stack
3804 pop qword ptr GS:16[RAX];
3805 pop qword ptr GS:8[RAX];
3806 pop qword ptr GS:[RAX];
3808 movdqa XMM15, [RSP];
3809 movdqa XMM14, [RSP + 16];
3810 movdqa XMM13, [RSP + 32];
3811 movdqa XMM12, [RSP + 48];
3812 movdqa XMM11, [RSP + 64];
3813 movdqa XMM10, [RSP + 80];
3814 movdqa XMM9, [RSP + 96];
3815 movdqa XMM8, [RSP + 112];
3816 movdqa XMM7, [RSP + 128];
3817 movdqa XMM6, [RSP + 144];
3827 // 'return' to complete switch
3832 else version (AsmX86_Posix)
3834 asm pure nothrow @nogc
3838 // save current stack state
3846 // store oldp again with more accurate address
3847 mov EAX, dword ptr 8[EBP];
3849 // load newp to begin context switch
3850 mov ESP, dword ptr 12[EBP];
3852 // load saved state from new stack
3859 // 'return' to complete switch
3864 else version (AsmX86_64_Posix)
3866 asm pure nothrow @nogc
3870 // save current stack state
3881 // load newp to begin context switch
3884 // load saved state from new stack
3892 // 'return' to complete switch
3897 else static if ( __traits( compiles, ucontext_t ) )
3899 Fiber cfib = Fiber.getThis();
3900 void* ucur = cfib.m_ucur;
3903 swapcontext( **(cast(ucontext_t***) oldp),
3904 *(cast(ucontext_t**) newp) );
3907 static assert(0, "Not implemented");
3912 ///////////////////////////////////////////////////////////////////////////////
3914 ///////////////////////////////////////////////////////////////////////////////
3916 * Documentation of Fiber internals:
3918 * The main routines to implement when porting Fibers to new architectures are
3919 * fiber_switchContext and initStack. Some version constants have to be defined
3920 * for the new platform as well, search for "Fiber Platform Detection and Memory Allocation".
3922 * Fibers are based on a concept called 'Context'. A Context describes the execution
3923 * state of a Fiber or main thread which is fully described by the stack, some
3924 * registers and a return address at which the Fiber/Thread should continue executing.
3925 * Please note that not only each Fiber has a Context, but each thread also has got a
3926 * Context which describes the threads stack and state. If you call Fiber fib; fib.call
3927 * the first time in a thread you switch from Threads Context into the Fibers Context.
3928 * If you call fib.yield in that Fiber you switch out of the Fibers context and back
3929 * into the Thread Context. (However, this is not always the case. You can call a Fiber
3930 * from within another Fiber, then you switch Contexts between the Fibers and the Thread
3931 * Context is not involved)
3933 * In all current implementations the registers and the return address are actually
3934 * saved on a Contexts stack.
3936 * The fiber_switchContext routine has got two parameters:
3937 * void** a: This is the _location_ where we have to store the current stack pointer,
3938 * the stack pointer of the currently executing Context (Fiber or Thread).
3939 * void* b: This is the pointer to the stack of the Context which we want to switch into.
3940 * Note that we get the same pointer here as the one we stored into the void** a
3941 * in a previous call to fiber_switchContext.
3943 * In the simplest case, a fiber_switchContext rountine looks like this:
3944 * fiber_switchContext:
3945 * push {return Address}
3947 * copy {stack pointer} into {location pointed to by a}
3948 * //We have now switch to the stack of a different Context!
3949 * copy {b} into {stack pointer}
3951 * pop {return Address}
3952 * jump to {return Address}
3954 * The GC uses the value returned in parameter a to scan the Fibers stack. It scans from
3955 * the stack base to that value. As the GC dislikes false pointers we can actually optimize
3956 * this a little: By storing registers which can not contain references to memory managed
3957 * by the GC outside of the region marked by the stack base pointer and the stack pointer
3958 * saved in fiber_switchContext we can prevent the GC from scanning them.
3959 * Such registers are usually floating point registers and the return address. In order to
3960 * implement this, we return a modified stack pointer from fiber_switchContext. However,
3961 * we have to remember that when we restore the registers from the stack!
3963 * --------------------------- <= Stack Base
3964 * | Frame | <= Many other stack frames
3966 * |-------------------------| <= The last stack frame. This one is created by fiber_switchContext
3967 * | registers with pointers |
3968 * | | <= Stack pointer. GC stops scanning here
3969 * | return address |
3970 * |floating point registers |
3971 * --------------------------- <= Real Stack End
3973 * fiber_switchContext:
3974 * push {registers with pointers}
3975 * copy {stack pointer} into {location pointed to by a}
3976 * push {return Address}
3977 * push {Floating point registers}
3978 * //We have now switch to the stack of a different Context!
3979 * copy {b} into {stack pointer}
3980 * //We now have to adjust the stack pointer to point to 'Real Stack End' so we can pop
3981 * //the FP registers
3982 * //+ or - depends on if your stack grows downwards or upwards
3983 * {stack pointer} = {stack pointer} +- ({FPRegisters}.sizeof + {return address}.sizeof}
3984 * pop {Floating point registers}
3985 * pop {return Address}
3986 * pop {registers with pointers}
3987 * jump to {return Address}
3989 * So the question now is which registers need to be saved? This depends on the specific
3990 * architecture ABI of course, but here are some general guidelines:
3991 * - If a register is callee-save (if the callee modifies the register it must saved and
3992 * restored by the callee) it needs to be saved/restored in switchContext
3993 * - If a register is caller-save it needn't be saved/restored. (Calling fiber_switchContext
3994 * is a function call and the compiler therefore already must save these registers before
3995 * calling fiber_switchContext)
3996 * - Argument registers used for passing parameters to functions needn't be saved/restored
3997 * - The return register needn't be saved/restored (fiber_switchContext hasn't got a return type)
3998 * - All scratch registers needn't be saved/restored
3999 * - The link register usually needn't be saved/restored (but sometimes it must be cleared -
4000 * see below for details)
4001 * - The frame pointer register - if it exists - is usually callee-save
4002 * - All current implementations do not save control registers
4004 * What happens on the first switch into a Fiber? We never saved a state for this fiber before,
4005 * but the initial state is prepared in the initStack routine. (This routine will also be called
4006 * when a Fiber is being resetted). initStack must produce exactly the same stack layout as the
4007 * part of fiber_switchContext which saves the registers. Pay special attention to set the stack
4008 * pointer correctly if you use the GC optimization mentioned before. the return Address saved in
4009 * initStack must be the address of fiber_entrypoint.
4011 * There's now a small but important difference between the first context switch into a fiber and
4012 * further context switches. On the first switch, Fiber.call is used and the returnAddress in
4013 * fiber_switchContext will point to fiber_entrypoint. The important thing here is that this jump
4014 * is a _function call_, we call fiber_entrypoint by jumping before it's function prologue. On later
4015 * calls, the user used yield() in a function, and therefore the return address points into a user
4016 * function, after the yield call. So here the jump in fiber_switchContext is a _function return_,
4017 * not a function call!
4019 * The most important result of this is that on entering a function, i.e. fiber_entrypoint, we
4020 * would have to provide a return address / set the link register once fiber_entrypoint
4021 * returns. Now fiber_entrypoint does never return and therefore the actual value of the return
4022 * address / link register is never read/used and therefore doesn't matter. When fiber_switchContext
4023 * performs a _function return_ the value in the link register doesn't matter either.
4024 * However, the link register will still be saved to the stack in fiber_entrypoint and some
4025 * exception handling / stack unwinding code might read it from this stack location and crash.
4026 * The exact solution depends on your architecture, but see the ARM implementation for a way
4027 * to deal with this issue.
4029 * The ARM implementation is meant to be used as a kind of documented example implementation.
4030 * Look there for a concrete example.
4032 * FIXME: fiber_entrypoint might benefit from a @noreturn attribute, but D doesn't have one.
4036 * This class provides a cooperative concurrency mechanism integrated with the
4037 * threading and garbage collection functionality. Calling a fiber may be
4038 * considered a blocking operation that returns when the fiber yields (via
4039 * Fiber.yield()). Execution occurs within the context of the calling thread
4040 * so synchronization is not necessary to guarantee memory visibility so long
4041 * as the same thread calls the fiber each time. Please note that there is no
4042 * requirement that a fiber be bound to one specific thread. Rather, fibers
4043 * may be freely passed between threads so long as they are not currently
4044 * executing. Like threads, a new fiber thread may be created using either
4045 * derivation or composition, as in the following example.
4048 * Status registers are not saved by the current implementations. This means
4049 * floating point exception status bits (overflow, divide by 0), rounding mode
4050 * and similar stuff is set per-thread, not per Fiber!
4053 * On ARM FPU registers are not saved if druntime was compiled as ARM_SoftFloat.
4054 * If such a build is used on a ARM_SoftFP system which actually has got a FPU
4055 * and other libraries are using the FPU registers (other code is compiled
4056 * as ARM_SoftFP) this can cause problems. Druntime must be compiled as
4057 * ARM_SoftFP in this case.
4060 * ----------------------------------------------------------------------
4062 * class DerivedFiber : Fiber
4072 * printf( "Derived fiber running.\n" );
4078 * printf( "Composed fiber running.\n" );
4080 * printf( "Composed fiber running.\n" );
4083 * // create instances of each type
4084 * Fiber derived = new DerivedFiber();
4085 * Fiber composed = new Fiber( &fiberFunc );
4087 * // call both fibers once
4090 * printf( "Execution returned to calling context.\n" );
4093 * // since each fiber has run to completion, each should have state TERM
4094 * assert( derived.state == Fiber.State.TERM );
4095 * assert( composed.state == Fiber.State.TERM );
4097 * ----------------------------------------------------------------------
4099 * Authors: Based on a design by Mikola Lysenko.
4103 ///////////////////////////////////////////////////////////////////////////
4105 ///////////////////////////////////////////////////////////////////////////
4109 * Initializes a fiber object which is associated with a static
4113 * fn = The fiber function.
4114 * sz = The stack size for this fiber.
4115 * guardPageSize = size of the guard page to trap fiber's stack
4119 * fn must not be null.
4121 this( void function() fn, size_t sz = PAGESIZE*4,
4122 size_t guardPageSize = PAGESIZE ) nothrow
4129 allocStack( sz, guardPageSize );
4135 * Initializes a fiber object which is associated with a dynamic
4139 * dg = The fiber function.
4140 * sz = The stack size for this fiber.
4141 * guardPageSize = size of the guard page to trap fiber's stack
4145 * dg must not be null.
4147 this( void delegate() dg, size_t sz = PAGESIZE*4,
4148 size_t guardPageSize = PAGESIZE ) nothrow
4155 allocStack( sz, guardPageSize);
4161 * Cleans up any remaining resources used by this object.
4163 ~this() nothrow @nogc
4165 // NOTE: A live reference to this object will exist on its associated
4166 // stack from the first time its call() method has been called
4167 // until its execution completes with State.TERM. Thus, the only
4168 // times this dtor should be called are either if the fiber has
4169 // terminated (and therefore has no active stack) or if the user
4170 // explicitly deletes this object. The latter case is an error
4171 // but is not easily tested for, since State.HOLD may imply that
4172 // the fiber was just created but has never been run. There is
4173 // not a compelling case to create a State.INIT just to offer a
4174 // means of ensuring the user isn't violating this object's
4175 // contract, so for now this requirement will be enforced by
4176 // documentation only.
4181 ///////////////////////////////////////////////////////////////////////////
4183 ///////////////////////////////////////////////////////////////////////////
4187 * Transfers execution to this fiber object. The calling context will be
4188 * suspended until the fiber calls Fiber.yield() or until it terminates
4189 * via an unhandled exception.
4192 * rethrow = Rethrow any unhandled exception which may have caused this
4193 * fiber to terminate.
4196 * This fiber must be in state HOLD.
4199 * Any exception not handled by the joined thread.
4202 * Any exception not handled by this fiber if rethrow = false, null
4205 // Not marked with any attributes, even though `nothrow @nogc` works
4206 // because it calls arbitrary user code. Most of the implementation
4207 // is already `@nogc nothrow`, but in order for `Fiber.call` to
4208 // propagate the attributes of the user's function, the Fiber
4209 // class needs to be templated.
4210 final Throwable call( Rethrow rethrow = Rethrow.yes )
4212 return rethrow ? call!(Rethrow.yes)() : call!(Rethrow.no);
4216 final Throwable call( Rethrow rethrow )()
4221 Throwable t = m_unhandled;
4223 static if ( rethrow )
4232 deprecated("Please pass Fiber.Rethrow.yes or .no instead of a boolean.")
4233 final Throwable call( bool rethrow )
4235 return rethrow ? call!(Rethrow.yes)() : call!(Rethrow.no);
4238 private void callImpl() nothrow @nogc
4241 assert( m_state == State.HOLD );
4245 Fiber cur = getThis();
4247 static if ( __traits( compiles, ucontext_t ) )
4248 m_ucur = cur ? &cur.m_utxt : &Fiber.sm_utxt;
4254 static if ( __traits( compiles, ucontext_t ) )
4257 // NOTE: If the fiber has terminated then the stack pointers must be
4258 // reset. This ensures that the stack for this fiber is not
4259 // scanned if the fiber has terminated. This is necessary to
4260 // prevent any references lingering on the stack from delaying
4261 // the collection of otherwise dead objects. The most notable
4262 // being the current object, which is referenced at the top of
4263 // fiber_entryPoint.
4264 if ( m_state == State.TERM )
4266 m_ctxt.tstack = m_ctxt.bstack;
4270 /// Flag to control rethrow behavior of $(D $(LREF call))
4271 enum Rethrow : bool { no, yes }
4274 * Resets this fiber so that it may be re-used, optionally with a
4275 * new function/delegate. This routine should only be called for
4276 * fibers that have terminated, as doing otherwise could result in
4277 * scope-dependent functionality that is not executed.
4278 * Stack-based classes, for example, may not be cleaned up
4279 * properly if a fiber is reset before it has terminated.
4282 * This fiber must be in state TERM or HOLD.
4284 final void reset() nothrow @nogc
4287 assert( m_state == State.TERM || m_state == State.HOLD );
4291 m_ctxt.tstack = m_ctxt.bstack;
4292 m_state = State.HOLD;
4298 final void reset( void function() fn ) nothrow @nogc
4306 final void reset( void delegate() dg ) nothrow @nogc
4313 ///////////////////////////////////////////////////////////////////////////
4314 // General Properties
4315 ///////////////////////////////////////////////////////////////////////////
4319 * A fiber may occupy one of three states: HOLD, EXEC, and TERM. The HOLD
4320 * state applies to any fiber that is suspended and ready to be called.
4321 * The EXEC state will be set for any fiber that is currently executing.
4322 * And the TERM state is set when a fiber terminates. Once a fiber
4323 * terminates, it must be reset before it may be called again.
4334 * Gets the current state of this fiber.
4337 * The state of this fiber as an enumerated value.
4339 final @property State state() const @safe pure nothrow @nogc
4345 ///////////////////////////////////////////////////////////////////////////
4346 // Actions on Calling Fiber
4347 ///////////////////////////////////////////////////////////////////////////
4351 * Forces a context switch to occur away from the calling fiber.
4353 static void yield() nothrow @nogc
4355 Fiber cur = getThis();
4356 assert( cur, "Fiber.yield() called with no active fiber" );
4357 assert( cur.m_state == State.EXEC );
4359 static if ( __traits( compiles, ucontext_t ) )
4360 cur.m_ucur = &cur.m_utxt;
4362 cur.m_state = State.HOLD;
4364 cur.m_state = State.EXEC;
4369 * Forces a context switch to occur away from the calling fiber and then
4370 * throws obj in the calling fiber.
4373 * t = The object to throw.
4376 * t must not be null.
4378 static void yieldAndThrow( Throwable t ) nothrow @nogc
4385 Fiber cur = getThis();
4386 assert( cur, "Fiber.yield() called with no active fiber" );
4387 assert( cur.m_state == State.EXEC );
4389 static if ( __traits( compiles, ucontext_t ) )
4390 cur.m_ucur = &cur.m_utxt;
4392 cur.m_unhandled = t;
4393 cur.m_state = State.HOLD;
4395 cur.m_state = State.EXEC;
4399 ///////////////////////////////////////////////////////////////////////////
4401 ///////////////////////////////////////////////////////////////////////////
4405 * Provides a reference to the calling fiber or null if no fiber is
4409 * The fiber object representing the calling fiber or null if no fiber
4410 * is currently active within this thread. The result of deleting this object is undefined.
4412 static Fiber getThis() @safe nothrow @nogc
4418 ///////////////////////////////////////////////////////////////////////////
4419 // Static Initialization
4420 ///////////////////////////////////////////////////////////////////////////
4427 static if ( __traits( compiles, ucontext_t ) )
4429 int status = getcontext( &sm_utxt );
4430 assert( status == 0 );
4437 // Initializes a fiber object which has no associated executable function.
4439 this() @safe pure nothrow @nogc
4446 // Fiber entry point. Invokes the function or delegate passed on
4447 // construction (if any).
4467 // The type of routine passed on fiber construction.
4478 // Standard fiber data
4483 void function() m_fn;
4484 void delegate() m_dg;
4487 Throwable m_unhandled;
4492 ///////////////////////////////////////////////////////////////////////////
4494 ///////////////////////////////////////////////////////////////////////////
4498 // Allocate a new stack for this fiber.
4500 final void allocStack( size_t sz, size_t guardPageSize ) nothrow
4503 assert( !m_pmem && !m_ctxt );
4507 // adjust alloc size to a multiple of PAGESIZE
4509 sz -= sz % PAGESIZE;
4511 // NOTE: This instance of Thread.Context is dynamic so Fiber objects
4512 // can be collected by the GC so long as no user level references
4513 // to the object exist. If m_ctxt were not dynamic then its
4514 // presence in the global context list would be enough to keep
4515 // this object alive indefinitely. An alternative to allocating
4516 // room for this struct explicitly would be to mash it into the
4517 // base of the stack being allocated below. However, doing so
4518 // requires too much special logic to be worthwhile.
4519 m_ctxt = new Thread.Context;
4521 static if ( __traits( compiles, VirtualAlloc ) )
4523 // reserve memory for stack
4524 m_pmem = VirtualAlloc( null,
4529 onOutOfMemoryError();
4531 version (StackGrowsDown)
4533 void* stack = m_pmem + guardPageSize;
4534 void* guard = m_pmem;
4535 void* pbase = stack + sz;
4539 void* stack = m_pmem;
4540 void* guard = m_pmem + sz;
4541 void* pbase = stack;
4544 // allocate reserved stack segment
4545 stack = VirtualAlloc( stack,
4550 onOutOfMemoryError();
4554 // allocate reserved guard page
4555 guard = VirtualAlloc( guard,
4558 PAGE_READWRITE | PAGE_GUARD );
4560 onOutOfMemoryError();
4563 m_ctxt.bstack = pbase;
4564 m_ctxt.tstack = pbase;
4569 version (Posix) import core.sys.posix.sys.mman; // mmap
4570 version (FreeBSD) import core.sys.freebsd.sys.mman : MAP_ANON;
4571 version (NetBSD) import core.sys.netbsd.sys.mman : MAP_ANON;
4572 version (CRuntime_Glibc) import core.sys.linux.sys.mman : MAP_ANON;
4573 version (Darwin) import core.sys.darwin.sys.mman : MAP_ANON;
4575 static if ( __traits( compiles, mmap ) )
4577 // Allocate more for the memory guard
4578 sz += guardPageSize;
4580 m_pmem = mmap( null,
4582 PROT_READ | PROT_WRITE,
4583 MAP_PRIVATE | MAP_ANON,
4586 if ( m_pmem == MAP_FAILED )
4589 else static if ( __traits( compiles, valloc ) )
4591 m_pmem = valloc( sz );
4593 else static if ( __traits( compiles, malloc ) )
4595 m_pmem = malloc( sz );
4603 onOutOfMemoryError();
4605 version (StackGrowsDown)
4607 m_ctxt.bstack = m_pmem + sz;
4608 m_ctxt.tstack = m_pmem + sz;
4609 void* guard = m_pmem;
4613 m_ctxt.bstack = m_pmem;
4614 m_ctxt.tstack = m_pmem;
4615 void* guard = m_pmem + sz - guardPageSize;
4619 static if ( __traits( compiles, mmap ) )
4623 // protect end of stack
4624 if ( mprotect(guard, guardPageSize, PROT_NONE) == -1 )
4630 // Supported only for mmap allocated memory - results are
4631 // undefined if applied to memory not obtained by mmap
4635 Thread.add( m_ctxt );
4640 // Free this fiber's stack.
4642 final void freeStack() nothrow @nogc
4645 assert( m_pmem && m_ctxt );
4649 // NOTE: m_ctxt is guaranteed to be alive because it is held in the
4650 // global context list.
4651 Thread.slock.lock_nothrow();
4652 scope(exit) Thread.slock.unlock_nothrow();
4653 Thread.remove( m_ctxt );
4655 static if ( __traits( compiles, VirtualAlloc ) )
4657 VirtualFree( m_pmem, 0, MEM_RELEASE );
4661 import core.sys.posix.sys.mman; // munmap
4663 static if ( __traits( compiles, mmap ) )
4665 munmap( m_pmem, m_size );
4667 else static if ( __traits( compiles, valloc ) )
4671 else static if ( __traits( compiles, malloc ) )
4682 // Initialize the allocated stack.
4683 // Look above the definition of 'class Fiber' for some information about the implementation of this routine
4685 final void initStack() nothrow @nogc
4688 assert( m_ctxt.tstack && m_ctxt.tstack == m_ctxt.bstack );
4689 assert( cast(size_t) m_ctxt.bstack % (void*).sizeof == 0 );
4693 void* pstack = m_ctxt.tstack;
4694 scope( exit ) m_ctxt.tstack = pstack;
4696 void push( size_t val ) nothrow
4698 version (StackGrowsDown)
4700 pstack -= size_t.sizeof;
4701 *(cast(size_t*) pstack) = val;
4705 pstack += size_t.sizeof;
4706 *(cast(size_t*) pstack) = val;
4710 // NOTE: On OS X the stack must be 16-byte aligned according
4711 // to the IA-32 call spec. For x86_64 the stack also needs to
4712 // be aligned to 16-byte according to SysV AMD64 ABI.
4713 version (AlignFiberStackTo16Byte)
4715 version (StackGrowsDown)
4717 pstack = cast(void*)(cast(size_t)(pstack) - (cast(size_t)(pstack) & 0x0F));
4721 pstack = cast(void*)(cast(size_t)(pstack) + (cast(size_t)(pstack) & 0x0F));
4725 version (AsmX86_Windows)
4727 version (StackGrowsDown) {} else static assert( false );
4729 // On Windows Server 2008 and 2008 R2, an exploit mitigation
4730 // technique known as SEHOP is activated by default. To avoid
4731 // hijacking of the exception handler chain, the presence of a
4732 // Windows-internal handler (ntdll.dll!FinalExceptionHandler) at
4733 // its end is tested by RaiseException. If it is not present, all
4734 // handlers are disregarded, and the program is thus aborted
4735 // (see http://blogs.technet.com/b/srd/archive/2009/02/02/
4736 // preventing-the-exploitation-of-seh-overwrites-with-sehop.aspx).
4737 // For new threads, this handler is installed by Windows immediately
4738 // after creation. To make exception handling work in fibers, we
4739 // have to insert it for our new stacks manually as well.
4741 // To do this, we first determine the handler by traversing the SEH
4742 // chain of the current thread until its end, and then construct a
4743 // registration block for the last handler on the newly created
4744 // thread. We then continue to push all the initial register values
4745 // for the first context switch as for the other implementations.
4747 // Note that this handler is never actually invoked, as we install
4748 // our own one on top of it in the fiber entry point function.
4749 // Thus, it should not have any effects on OSes not implementing
4750 // exception chain verification.
4752 alias fp_t = void function(); // Actual signature not relevant.
4753 static struct EXCEPTION_REGISTRATION
4755 EXCEPTION_REGISTRATION* next; // sehChainEnd if last one.
4758 enum sehChainEnd = cast(EXCEPTION_REGISTRATION*) 0xFFFFFFFF;
4760 __gshared static fp_t finalHandler = null;
4761 if ( finalHandler is null )
4763 static EXCEPTION_REGISTRATION* fs0() nothrow
4765 asm pure nothrow @nogc
4773 while ( reg.next != sehChainEnd ) reg = reg.next;
4775 // Benign races are okay here, just to avoid re-lookup on every
4777 finalHandler = reg.handler;
4780 // When linking with /safeseh (supported by LDC, but not DMD)
4781 // the exception chain must not extend to the very top
4782 // of the stack, otherwise the exception chain is also considered
4783 // invalid. Reserving additional 4 bytes at the top of the stack will
4784 // keep the EXCEPTION_REGISTRATION below that limit
4785 size_t reserve = EXCEPTION_REGISTRATION.sizeof + 4;
4787 *(cast(EXCEPTION_REGISTRATION*)pstack) =
4788 EXCEPTION_REGISTRATION( sehChainEnd, finalHandler );
4790 push( cast(size_t) &fiber_entryPoint ); // EIP
4791 push( cast(size_t) m_ctxt.bstack - reserve ); // EBP
4792 push( 0x00000000 ); // EDI
4793 push( 0x00000000 ); // ESI
4794 push( 0x00000000 ); // EBX
4795 push( cast(size_t) m_ctxt.bstack - reserve ); // FS:[0]
4796 push( cast(size_t) m_ctxt.bstack ); // FS:[4]
4797 push( cast(size_t) m_ctxt.bstack - m_size ); // FS:[8]
4798 push( 0x00000000 ); // EAX
4800 else version (AsmX86_64_Windows)
4802 // Using this trampoline instead of the raw fiber_entryPoint
4803 // ensures that during context switches, source and destination
4804 // stacks have the same alignment. Otherwise, the stack would need
4805 // to be shifted by 8 bytes for the first call, as fiber_entryPoint
4806 // is an actual function expecting a stack which is not aligned
4808 static void trampoline()
4810 asm pure nothrow @nogc
4813 sub RSP, 32; // Shadow space (Win64 calling convention)
4814 call fiber_entryPoint;
4815 xor RCX, RCX; // This should never be reached, as
4816 jmp RCX; // fiber_entryPoint must never return.
4820 push( cast(size_t) &trampoline ); // RIP
4821 push( 0x00000000_00000000 ); // RBP
4822 push( 0x00000000_00000000 ); // R12
4823 push( 0x00000000_00000000 ); // R13
4824 push( 0x00000000_00000000 ); // R14
4825 push( 0x00000000_00000000 ); // R15
4826 push( 0x00000000_00000000 ); // RDI
4827 push( 0x00000000_00000000 ); // RSI
4828 push( 0x00000000_00000000 ); // XMM6 (high)
4829 push( 0x00000000_00000000 ); // XMM6 (low)
4830 push( 0x00000000_00000000 ); // XMM7 (high)
4831 push( 0x00000000_00000000 ); // XMM7 (low)
4832 push( 0x00000000_00000000 ); // XMM8 (high)
4833 push( 0x00000000_00000000 ); // XMM8 (low)
4834 push( 0x00000000_00000000 ); // XMM9 (high)
4835 push( 0x00000000_00000000 ); // XMM9 (low)
4836 push( 0x00000000_00000000 ); // XMM10 (high)
4837 push( 0x00000000_00000000 ); // XMM10 (low)
4838 push( 0x00000000_00000000 ); // XMM11 (high)
4839 push( 0x00000000_00000000 ); // XMM11 (low)
4840 push( 0x00000000_00000000 ); // XMM12 (high)
4841 push( 0x00000000_00000000 ); // XMM12 (low)
4842 push( 0x00000000_00000000 ); // XMM13 (high)
4843 push( 0x00000000_00000000 ); // XMM13 (low)
4844 push( 0x00000000_00000000 ); // XMM14 (high)
4845 push( 0x00000000_00000000 ); // XMM14 (low)
4846 push( 0x00000000_00000000 ); // XMM15 (high)
4847 push( 0x00000000_00000000 ); // XMM15 (low)
4848 push( 0x00000000_00000000 ); // RBX
4849 push( 0xFFFFFFFF_FFFFFFFF ); // GS:[0]
4850 version (StackGrowsDown)
4852 push( cast(size_t) m_ctxt.bstack ); // GS:[8]
4853 push( cast(size_t) m_ctxt.bstack - m_size ); // GS:[16]
4857 push( cast(size_t) m_ctxt.bstack ); // GS:[8]
4858 push( cast(size_t) m_ctxt.bstack + m_size ); // GS:[16]
4861 else version (AsmX86_Posix)
4863 push( 0x00000000 ); // Return address of fiber_entryPoint call
4864 push( cast(size_t) &fiber_entryPoint ); // EIP
4865 push( cast(size_t) m_ctxt.bstack ); // EBP
4866 push( 0x00000000 ); // EDI
4867 push( 0x00000000 ); // ESI
4868 push( 0x00000000 ); // EBX
4869 push( 0x00000000 ); // EAX
4871 else version (AsmX86_64_Posix)
4873 push( 0x00000000_00000000 ); // Return address of fiber_entryPoint call
4874 push( cast(size_t) &fiber_entryPoint ); // RIP
4875 push( cast(size_t) m_ctxt.bstack ); // RBP
4876 push( 0x00000000_00000000 ); // RBX
4877 push( 0x00000000_00000000 ); // R12
4878 push( 0x00000000_00000000 ); // R13
4879 push( 0x00000000_00000000 ); // R14
4880 push( 0x00000000_00000000 ); // R15
4882 else version (AsmPPC_Posix)
4884 version (StackGrowsDown)
4886 pstack -= int.sizeof * 5;
4890 pstack += int.sizeof * 5;
4893 push( cast(size_t) &fiber_entryPoint ); // link register
4894 push( 0x00000000 ); // control register
4895 push( 0x00000000 ); // old stack pointer
4898 version (StackGrowsDown)
4900 pstack -= int.sizeof * 20;
4904 pstack += int.sizeof * 20;
4907 assert( (cast(size_t) pstack & 0x0f) == 0 );
4909 else version (AsmMIPS_O32_Posix)
4911 version (StackGrowsDown) {}
4912 else static assert(0);
4914 /* We keep the FP registers and the return address below
4915 * the stack pointer, so they don't get scanned by the
4916 * GC. The last frame before swapping the stack pointer is
4917 * organized like the following.
4919 * |-----------|<= frame pointer
4922 * |-----------|<= stack pointer
4929 enum SZ_GP = 10 * size_t.sizeof; // $gp + $s0-8
4930 enum SZ_RA = size_t.sizeof; // $ra
4931 version (MIPS_HardFloat)
4933 enum SZ_FP = 6 * 8; // $f20-30
4934 enum ALIGN = -(SZ_FP + SZ_RA) & (8 - 1);
4942 enum BELOW = SZ_FP + ALIGN + SZ_RA;
4944 enum SZ = BELOW + ABOVE;
4946 (cast(ubyte*)pstack - SZ)[0 .. SZ] = 0;
4948 *cast(size_t*)(pstack - SZ_RA) = cast(size_t)&fiber_entryPoint;
4950 else version (AsmAArch64_Posix)
4952 // Like others, FP registers and return address (lr) are kept
4953 // below the saved stack top (tstack) to hide from GC scanning.
4954 // fiber_switchContext expects newp sp to look like this:
4957 // 9: x29 (fp) <-- newp tstack
4958 // 8: x30 (lr) [&fiber_entryPoint]
4963 version (StackGrowsDown) {}
4965 static assert(false, "Only full descending stacks supported on AArch64");
4967 // Only need to set return address (lr). Everything else is fine
4968 // zero initialized.
4969 pstack -= size_t.sizeof * 11; // skip past x19-x29
4970 push(cast(size_t) &fiber_trampoline); // see threadasm.S for docs
4971 pstack += size_t.sizeof; // adjust sp (newp) above lr
4973 else version (AsmARM_Posix)
4975 /* We keep the FP registers and the return address below
4976 * the stack pointer, so they don't get scanned by the
4977 * GC. The last frame before swapping the stack pointer is
4978 * organized like the following.
4980 * | |-----------|<= 'frame starts here'
4981 * | | fp | (the actual frame pointer, r11 isn't
4982 * | | r10-r4 | updated and still points to the previous frame)
4983 * | |-----------|<= stack pointer
4986 * | | d15-d8 |(if FP supported)
4989 * stack grows down: The pointer value here is smaller than some lines above
4991 // frame pointer can be zero, r10-r4 also zero initialized
4992 version (StackGrowsDown)
4993 pstack -= int.sizeof * 8;
4995 static assert(false, "Only full descending stacks supported on ARM");
4998 push( cast(size_t) &fiber_entryPoint );
5000 * We do not push padding and d15-d8 as those are zero initialized anyway
5001 * Position the stack pointer above the lr register
5003 pstack += int.sizeof * 1;
5005 else version (GNU_AsmX86_Windows)
5007 version (StackGrowsDown) {} else static assert( false );
5009 // Currently, MinGW doesn't utilize SEH exceptions.
5010 // See DMD AsmX86_Windows If this code ever becomes fails and SEH is used.
5012 push( 0x00000000 ); // Return address of fiber_entryPoint call
5013 push( cast(size_t) &fiber_entryPoint ); // EIP
5014 push( 0x00000000 ); // EBP
5015 push( 0x00000000 ); // EDI
5016 push( 0x00000000 ); // ESI
5017 push( 0x00000000 ); // EBX
5018 push( 0xFFFFFFFF ); // FS:[0] - Current SEH frame
5019 push( cast(size_t) m_ctxt.bstack ); // FS:[4] - Top of stack
5020 push( cast(size_t) m_ctxt.bstack - m_size ); // FS:[8] - Bottom of stack
5021 push( 0x00000000 ); // EAX
5023 else version (GNU_AsmX86_64_Windows)
5025 push( 0x00000000_00000000 ); // Return address of fiber_entryPoint call
5026 push( cast(size_t) &fiber_entryPoint ); // RIP
5027 push( 0x00000000_00000000 ); // RBP
5028 push( 0x00000000_00000000 ); // RBX
5029 push( 0x00000000_00000000 ); // R12
5030 push( 0x00000000_00000000 ); // R13
5031 push( 0x00000000_00000000 ); // R14
5032 push( 0x00000000_00000000 ); // R15
5033 push( 0xFFFFFFFF_FFFFFFFF ); // GS:[0] - Current SEH frame
5034 version (StackGrowsDown)
5036 push( cast(size_t) m_ctxt.bstack ); // GS:[8] - Top of stack
5037 push( cast(size_t) m_ctxt.bstack - m_size ); // GS:[16] - Bottom of stack
5041 push( cast(size_t) m_ctxt.bstack ); // GS:[8] - Top of stack
5042 push( cast(size_t) m_ctxt.bstack + m_size ); // GS:[16] - Bottom of stack
5045 else static if ( __traits( compiles, ucontext_t ) )
5047 getcontext( &m_utxt );
5048 m_utxt.uc_stack.ss_sp = m_pmem;
5049 m_utxt.uc_stack.ss_size = m_size;
5050 makecontext( &m_utxt, &fiber_entryPoint, 0 );
5051 // NOTE: If ucontext is being used then the top of the stack will
5052 // be a pointer to the ucontext_t struct for that fiber.
5053 push( cast(size_t) &m_utxt );
5056 static assert(0, "Not implemented");
5060 Thread.Context* m_ctxt;
5064 static if ( __traits( compiles, ucontext_t ) )
5066 // NOTE: The static ucontext instance is used to represent the context
5067 // of the executing thread.
5068 static ucontext_t sm_utxt = void;
5069 ucontext_t m_utxt = void;
5070 ucontext_t* m_ucur = null;
5075 ///////////////////////////////////////////////////////////////////////////
5076 // Storage of Active Fiber
5077 ///////////////////////////////////////////////////////////////////////////
5081 // Sets a thread-local reference to the current fiber object.
5083 static void setThis( Fiber f ) nothrow @nogc
5088 static Fiber sm_this;
5092 ///////////////////////////////////////////////////////////////////////////
5093 // Context Switching
5094 ///////////////////////////////////////////////////////////////////////////
5098 // Switches into the stack held by this fiber.
5100 final void switchIn() nothrow @nogc
5102 Thread tobj = Thread.getThis();
5103 void** oldp = &tobj.m_curr.tstack;
5104 void* newp = m_ctxt.tstack;
5106 // NOTE: The order of operations here is very important. The current
5107 // stack top must be stored before m_lock is set, and pushContext
5108 // must not be called until after m_lock is set. This process
5109 // is intended to prevent a race condition with the suspend
5110 // mechanism used for garbage collection. If it is not followed,
5111 // a badly timed collection could cause the GC to scan from the
5112 // bottom of one stack to the top of another, or to miss scanning
5113 // a stack that still contains valid data. The old stack pointer
5114 // oldp will be set again before the context switch to guarantee
5115 // that it points to exactly the correct stack location so the
5116 // successive pop operations will succeed.
5117 *oldp = getStackTop();
5118 atomicStore!(MemoryOrder.raw)(*cast(shared)&tobj.m_lock, true);
5119 tobj.pushContext( m_ctxt );
5121 fiber_switchContext( oldp, newp );
5123 // NOTE: As above, these operations must be performed in a strict order
5124 // to prevent Bad Things from happening.
5126 atomicStore!(MemoryOrder.raw)(*cast(shared)&tobj.m_lock, false);
5127 tobj.m_curr.tstack = tobj.m_curr.bstack;
5132 // Switches out of the current stack and into the enclosing stack.
5134 final void switchOut() nothrow @nogc
5136 Thread tobj = Thread.getThis();
5137 void** oldp = &m_ctxt.tstack;
5138 void* newp = tobj.m_curr.within.tstack;
5140 // NOTE: The order of operations here is very important. The current
5141 // stack top must be stored before m_lock is set, and pushContext
5142 // must not be called until after m_lock is set. This process
5143 // is intended to prevent a race condition with the suspend
5144 // mechanism used for garbage collection. If it is not followed,
5145 // a badly timed collection could cause the GC to scan from the
5146 // bottom of one stack to the top of another, or to miss scanning
5147 // a stack that still contains valid data. The old stack pointer
5148 // oldp will be set again before the context switch to guarantee
5149 // that it points to exactly the correct stack location so the
5150 // successive pop operations will succeed.
5151 *oldp = getStackTop();
5152 atomicStore!(MemoryOrder.raw)(*cast(shared)&tobj.m_lock, true);
5154 fiber_switchContext( oldp, newp );
5156 // NOTE: As above, these operations must be performed in a strict order
5157 // to prevent Bad Things from happening.
5158 // NOTE: If use of this fiber is multiplexed across threads, the thread
5159 // executing here may be different from the one above, so get the
5160 // current thread handle before unlocking, etc.
5161 tobj = Thread.getThis();
5162 atomicStore!(MemoryOrder.raw)(*cast(shared)&tobj.m_lock, false);
5163 tobj.m_curr.tstack = tobj.m_curr.bstack;
5170 class TestFiber : Fiber
5179 foreach (i; 0 .. 1000)
5186 enum expSum = 1000 * 999 / 2;
5193 foreach (ref fib; fibs)
5194 fib = new TestFiber();
5199 foreach (fib; fibs) {
5200 if (fib.state == Fiber.State.HOLD)
5203 cont |= fib.state != Fiber.State.TERM;
5210 assert(fib.sum == TestFiber.expSum);
5216 // Single thread running separate fibers
5223 // Multiple threads running separate fibers
5226 auto group = new ThreadGroup();
5229 group.create(&runTen);
5235 // Multiple threads running shared fibers
5238 shared bool[10] locks;
5246 foreach (idx; 0 .. 10)
5248 if (cas(&locks[idx], false, true))
5250 if (fibs[idx].state == Fiber.State.HOLD)
5253 cont |= fibs[idx].state != Fiber.State.TERM;
5265 foreach (ref fib; fibs)
5267 fib = new TestFiber();
5270 auto group = new ThreadGroup();
5273 group.create(&runShared);
5279 assert(fib.sum == TestFiber.expSum);
5284 // Test exception handling inside fibers.
5286 // broken on win32 under windows server 2012: bug 13821
5288 enum MSG = "Test message.";
5293 throw new Exception(MSG);
5300 assert(caughtMsg == MSG);
5316 new Fiber({}).call!(Fiber.Rethrow.no)();
5321 new Fiber({}).call(Fiber.Rethrow.yes);
5322 new Fiber({}).call(Fiber.Rethrow.no);
5327 new Fiber({}).call(true);
5328 new Fiber({}).call(false);
5332 // broken on win32 under windows server 2012: bug 13821
5334 enum MSG = "Test message.";
5339 throw new Exception( MSG );
5341 assert( false, "Expected rethrown exception." );
5343 catch ( Throwable t )
5345 assert( t.msg == MSG );
5349 // Test exception chaining when switching contexts in finally blocks.
5352 static void throwAndYield(string msg) {
5354 throw new Exception(msg);
5360 static void fiber(string name) {
5363 throwAndYield(name ~ ".1");
5365 throwAndYield(name ~ ".2");
5367 } catch (Exception e) {
5368 assert(e.msg == name ~ ".1");
5370 assert(e.next.msg == name ~ ".2");
5371 assert(!e.next.next);
5375 auto first = new Fiber(() => fiber("first"));
5376 auto second = new Fiber(() => fiber("second"));
5383 assert(first.state == Fiber.State.TERM);
5384 assert(second.state == Fiber.State.TERM);
5387 // Test Fiber resetting
5390 static string method;
5402 static void expect(Fiber fib, string s)
5404 assert(fib.state == Fiber.State.HOLD);
5406 assert(fib.state == Fiber.State.TERM);
5407 assert(method == s); method = null;
5409 auto fib = new Fiber(&foo);
5421 fib.reset(function void(){method = "function";});
5422 expect(fib, "function");
5424 fib.reset(delegate void(){method = "delegate";});
5425 expect(fib, "delegate");
5428 // Test unsafe reset in hold state
5431 auto fib = new Fiber(function {ubyte[2048] buf = void; Fiber.yield();}, 4096);
5432 foreach (_; 0 .. 10)
5435 assert(fib.state == Fiber.State.HOLD);
5440 // stress testing GC stack scanning
5445 static void unreferencedThreadObject()
5447 static void sleep() { Thread.sleep(dur!"msecs"(100)); }
5448 auto thread = new Thread(&sleep).start();
5450 unreferencedThreadObject();
5468 static void collect()
5470 auto foo = new Foo(2);
5471 assert(foo.bar() == 2);
5475 assert(foo.bar() == 2);
5478 auto fiber = new Fiber(&collect);
5485 auto foo = new Foo(2);
5489 assert(foo.bar() == 2);
5493 assert(foo.bar() == 2);
5496 fiber = new Fiber(&collect2);
5502 static void recurse(size_t cnt)
5508 auto fib = new Fiber(() { recurse(cnt); });
5514 fiber = new Fiber(() { recurse(20); });
5519 version (AsmX86_64_Windows)
5521 // Test Windows x64 calling convention
5524 void testNonvolatileRegister(alias REG)()
5526 auto zeroRegister = new Fiber(() {
5527 mixin("asm pure nothrow @nogc { naked; xor "~REG~", "~REG~"; ret; }");
5531 mixin("asm pure nothrow @nogc { mov "~REG~", 0xFFFFFFFFFFFFFFFF; }");
5532 zeroRegister.call();
5533 mixin("asm pure nothrow @nogc { mov after, "~REG~"; }");
5535 assert(after == -1);
5538 void testNonvolatileRegisterSSE(alias REG)()
5540 auto zeroRegister = new Fiber(() {
5541 mixin("asm pure nothrow @nogc { naked; xorpd "~REG~", "~REG~"; ret; }");
5543 long[2] before = [0xFFFFFFFF_FFFFFFFF, 0xFFFFFFFF_FFFFFFFF], after;
5545 mixin("asm pure nothrow @nogc { movdqu "~REG~", before; }");
5546 zeroRegister.call();
5547 mixin("asm pure nothrow @nogc { movdqu after, "~REG~"; }");
5549 assert(before == after);
5552 testNonvolatileRegister!("R12")();
5553 testNonvolatileRegister!("R13")();
5554 testNonvolatileRegister!("R14")();
5555 testNonvolatileRegister!("R15")();
5556 testNonvolatileRegister!("RDI")();
5557 testNonvolatileRegister!("RSI")();
5558 testNonvolatileRegister!("RBX")();
5560 testNonvolatileRegisterSSE!("XMM6")();
5561 testNonvolatileRegisterSSE!("XMM7")();
5562 testNonvolatileRegisterSSE!("XMM8")();
5563 testNonvolatileRegisterSSE!("XMM9")();
5564 testNonvolatileRegisterSSE!("XMM10")();
5565 testNonvolatileRegisterSSE!("XMM11")();
5566 testNonvolatileRegisterSSE!("XMM12")();
5567 testNonvolatileRegisterSSE!("XMM13")();
5568 testNonvolatileRegisterSSE!("XMM14")();
5569 testNonvolatileRegisterSSE!("XMM15")();
5574 version (D_InlineAsm_X86_64)
5578 void testStackAlignment()
5581 asm pure nothrow @nogc
5585 assert((cast(size_t)pRSP & 0xF) == 0);
5588 auto fib = new Fiber(&testStackAlignment);
5593 // regression test for Issue 13416
5594 version (FreeBSD) unittest
5598 pthread_attr_t attr;
5599 pthread_attr_init(&attr);
5600 auto thr = pthread_self();
5601 foreach (i; 0 .. 50)
5602 pthread_attr_get_np(thr, &attr);
5603 pthread_attr_destroy(&attr);
5606 auto thr = new Thread(&loop).start();
5607 foreach (i; 0 .. 50)
5609 thread_suspendAll();
5617 // use >PAGESIZE to avoid stack overflow (e.g. in an syscall)
5618 auto thr = new Thread(function{}, 4096 + 1).start();
5623 * Represents the ID of a thread, as returned by $(D Thread.)$(LREF id).
5624 * The exact type varies from platform to platform.
5627 alias ThreadID = uint;
5630 alias ThreadID = pthread_t;