| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830 | /** * The thread module provides support for thread creation and management. * * If AtomicSuspendCount is used for speed reasons all signals are sent together. * When debugging gdb funnels all signals through one single handler, and if * the signals arrive quickly enough they will be coalesced in a single signal, * (discarding the second) thus it is possible to loose signals, which blocks * the program. Thus when debugging it is better to use the slower SuspendOneAtTime * version. * * Copyright: Copyright (C) 2005-2006 Sean Kelly, Fawzi. All rights reserved. * License: BSD style: $(LICENSE) * Authors: Sean Kelly, Fawzi Mohamed */ module tango.core.Thread; import tango.core.sync.Atomic; debug(Thread) import tango.stdc.stdio : printf; // this should be true for most architectures version = StackGrowsDown; version(darwin){ version=AtomicSuspendCount; } version(linux){ version=AtomicSuspendCount; } public { // import tango.core.TimeSpan; } private { import tango.core.Exception; extern (C) void _d_monitorenter(Object); extern (C) void _d_monitorexit(Object); // // exposed by compiler runtime // extern (C) void* rt_stackBottom(); extern (C) void* rt_stackTop(); void* getStackBottom() { return rt_stackBottom(); } void* getStackTop() { version( D_InlineAsm_X86 ) { asm { naked; mov EAX, ESP; ret; } } else { return rt_stackTop(); } } version(D_InlineAsm_X86){ uint getEBX(){ uint retVal; asm{ mov retVal,EBX; } return retVal; } } } //////////////////////////////////////////////////////////////////////////////// // Thread Entry Point and Signal Handlers //////////////////////////////////////////////////////////////////////////////// version( Win32 ) { private { import tango.stdc.stdint : uintptr_t; // for _beginthreadex decl below import tango.sys.win32.UserGdi; const DWORD TLS_OUT_OF_INDEXES = 0xFFFFFFFF; // // avoid multiple imports via tango.sys.windows.process // extern (Windows) alias uint function(void*) btex_fptr; extern (C) uintptr_t _beginthreadex(void*, uint, btex_fptr, void*, uint, uint*); // // entry point for Windows threads // extern (Windows) uint thread_entryPoint( void* arg ) { Thread obj = cast(Thread) arg; assert( obj ); scope( exit ) Thread.remove( obj ); assert( obj.m_curr is &obj.m_main ); obj.m_main.bstack = getStackBottom(); obj.m_main.tstack = obj.m_main.bstack; Thread.add( &obj.m_main ); Thread.setThis( obj ); // NOTE: No GC allocations may occur until the stack pointers have // been set and Thread.getThis returns a valid reference to // this thread object (this latter condition is not strictly // necessary on Win32 but it should be followed for the sake // of consistency). // TODO: Consider putting an auto exception object here (using // alloca) forOutOfMemoryError plus something to track // whether an exception is in-flight? try { obj.run(); } catch( Object o ) { obj.m_unhandled = o; } return 0; } // // copy of the same-named function in phobos.std.thread--it uses the // Windows naming convention to be consistent with GetCurrentThreadId // HANDLE GetCurrentThreadHandle() { const uint DUPLICATE_SAME_ACCESS = 0x00000002; HANDLE curr = GetCurrentThread(), proc = GetCurrentProcess(), hndl; DuplicateHandle( proc, curr, proc, &hndl, 0, TRUE, DUPLICATE_SAME_ACCESS ); return hndl; } } } else version( Posix ) { private { import tango.stdc.posix.semaphore; import tango.stdc.posix.pthread; import tango.stdc.posix.signal; import tango.stdc.posix.time; import tango.stdc.errno; extern (C) int getErrno(); version( GNU ) { import gcc.builtins; } // // entry point for POSIX threads // extern (C) void* thread_entryPoint( void* arg ) { Thread obj = cast(Thread) arg; assert( obj ); scope( exit ) { // NOTE: isRunning should be set to false after the thread is // removed or a double-removal could occur between this // function and thread_suspendAll. Thread.remove( obj ); obj.m_isRunning = false; } static extern (C) void thread_cleanupHandler( void* arg ) { Thread obj = cast(Thread) arg; assert( obj ); // NOTE: If the thread terminated abnormally, just set it as // not running and let thread_suspendAll remove it from // the thread list. This is safer and is consistent // with the Windows thread code. obj.m_isRunning = false; } // NOTE: Using void to skip the initialization here relies on // knowledge of how pthread_cleanup is implemented. It may // not be appropriate for all platforms. However, it does // avoid the need to link the pthread module. If any // implementation actually requires default initialization // then pthread_cleanup should be restructured to maintain // the current lack of a link dependency. version( linux ) { pthread_cleanup cleanup = void; cleanup.push( &thread_cleanupHandler, cast(void*) obj ); } else version( darwin ) { pthread_cleanup cleanup = void; cleanup.push( &thread_cleanupHandler, cast(void*) obj ); } else version( solaris ) { pthread_cleanup cleanup = void; cleanup.push( &thread_cleanupHandler, cast(void*) obj ); } else { pthread_cleanup_push( &thread_cleanupHandler, cast(void*) obj ); } // NOTE: For some reason this does not always work for threads. //obj.m_main.bstack = getStackBottom(); version( D_InlineAsm_X86 ) { static void* getBasePtr() { asm { naked; mov EAX, EBP; ret; } } obj.m_main.bstack = getBasePtr(); } else version( StackGrowsDown ) obj.m_main.bstack = &obj + 1; else obj.m_main.bstack = &obj; obj.m_main.tstack = obj.m_main.bstack; assert( obj.m_curr == &obj.m_main ); Thread.add( &obj.m_main ); Thread.setThis( obj ); // NOTE: No GC allocations may occur until the stack pointers have // been set and Thread.getThis returns a valid reference to // this thread object (this latter condition is not strictly // necessary on Win32 but it should be followed for the sake // of consistency). // TODO: Consider putting an auto exception object here (using // alloca) forOutOfMemoryError plus something to track // whether an exception is in-flight? try { obj.run(); } catch( Object o ) { obj.m_unhandled = o; } return null; } // // used to track the number of suspended threads // version(AtomicSuspendCount){ int suspendCount; } else { sem_t suspendCount; } extern (C) void thread_suspendHandler( int sig ) in { assert( sig == SIGUSR1 ); } body { version( LDC) { version(X86) { uint eax,ecx,edx,ebx,ebp,esi,edi; asm { mov eax[EBP], EAX ; mov ecx[EBP], ECX ; mov edx[EBP], EDX ; mov ebx[EBP], EBX ; mov ebp[EBP], EBP ; mov esi[EBP], ESI ; mov edi[EBP], EDI ; } } else version (X86_64) { ulong rax,rbx,rcx,rdx,rbp,rsi,rdi,rsp,r8,r9,r10,r11,r12,r13,r14,r15; asm { movq rax[RBP], RAX ; movq rbx[RBP], RBX ; movq rcx[RBP], RCX ; movq rdx[RBP], RDX ; movq rbp[RBP], RBP ; movq rsi[RBP], RSI ; movq rdi[RBP], RDI ; movq rsp[RBP], RSP ; movq r8 [RBP], R8 ; movq r9 [RBP], R9 ; movq r10[RBP], R10 ; movq r11[RBP], R11 ; movq r12[RBP], R12 ; movq r13[RBP], R13 ; movq r14[RBP], R14 ; movq r15[RBP], R15 ; } } else { static assert( false, "Architecture not supported." ); } } else version( D_InlineAsm_X86 ) { asm { pushad; } } else version( GNU ) { __builtin_unwind_init(); } else version ( D_InlineAsm_X86_64 ) { asm { // Not sure what goes here, pushad is invalid in 64 bit code push RAX ; push RBX ; push RCX ; push RDX ; push RSI ; push RDI ; push RBP ; push R8 ; push R9 ; push R10 ; push R11 ; push R12 ; push R13 ; push R14 ; push R15 ; push RAX ; // 16 byte align the stack } } else { static assert( false, "Architecture not supported." ); } // NOTE: Since registers are being pushed and popped from the stack, // any other stack data used by this function should be gone // before the stack cleanup code is called below. { Thread obj = Thread.getThis(); // NOTE: The thread reference returned by getThis is set within // the thread startup code, so it is possible that this // handler may be called before the reference is set. In // this case it is safe to simply suspend and not worry // about the stack pointers as the thread will not have // any references to GC-managed data. if( obj && !obj.m_lock ) { obj.m_curr.tstack = getStackTop(); } sigset_t sigres = void; int status; status = sigfillset( &sigres ); assert( status == 0 ); status = sigdelset( &sigres, SIGUSR2 ); assert( status == 0 ); version (AtomicSuspendCount){ auto oldV=flagAdd(suspendCount,1); } else { status = sem_post( &suspendCount ); assert( status == 0 ); } // here one could do some work (like scan the current stack in this thread...) sigsuspend( &sigres ); if( obj && !obj.m_lock ) { obj.m_curr.tstack = obj.m_curr.bstack; } } version( LDC) { // nothing to pop } else version( D_InlineAsm_X86 ) { asm { popad; } } else version( GNU ) { // registers will be popped automatically } else version ( D_InlineAsm_X86_64 ) { asm { // Not sure what goes here, popad is invalid in 64 bit code pop RAX ; // 16 byte align the stack pop R15 ; pop R14 ; pop R13 ; pop R12 ; pop R11 ; pop R10 ; pop R9 ; pop R8 ; pop RBP ; pop RDI ; pop RSI ; pop RDX ; pop RCX ; pop RBX ; pop RAX ; } } else { static assert( false, "Architecture not supported." ); } } extern (C) void thread_resumeHandler( int sig ) in { assert( sig == SIGUSR2 ); } body { int status; version (AtomicSuspendCount){ auto oldV=flagAdd(suspendCount,-1); } else { status = sem_post( &suspendCount ); } assert( status == 0 ); } } alias void function(int) sHandler; sHandler _thread_abortHandler=null; extern (C) void thread_abortHandler( int sig ){ if (_thread_abortHandler!is null){ _thread_abortHandler(sig); } else { exit(-1); } } extern (C) void setthread_abortHandler(sHandler f){ _thread_abortHandler=f; } } else { // NOTE: This is the only place threading versions are checked. If a new // version is added, the module code will need to be searched for // places where version-specific code may be required. This can be // easily accomlished by searching for 'Windows' or 'Posix'. static assert( false, "Unknown threading implementation." ); } //////////////////////////////////////////////////////////////////////////////// // Thread //////////////////////////////////////////////////////////////////////////////// /** * This class encapsulates all threading functionality for the D * programming language. As thread manipulation is a required facility * for garbage collection, all user threads should derive from this * class, and instances of this class should never be explicitly deleted. * A new thread may be created using either derivation or composition, as * in the following example. * * Example: * ----------------------------------------------------------------------------- * class DerivedThread : Thread * { * this() * { * super( &run ); * } * * private : * void run() * { * printf( "Derived thread running.\n" ); * } * } * * void threadFunc() * { * printf( "Composed thread running.\n" ); * } * * // create instances of each type * Thread derived = new DerivedThread(); * Thread composed = new Thread( &threadFunc ); * * // start both threads * derived.start(); * composed.start(); * ----------------------------------------------------------------------------- */ class Thread { //////////////////////////////////////////////////////////////////////////// // Initialization //////////////////////////////////////////////////////////////////////////// /** * Initializes a thread object which is associated with a static * D function. * * Params: * fn = The thread function. * sz = The stack size for this thread. * * In: * fn must not be null. */ this( void function() fn, size_t sz = 0 ) in { assert( fn ); } body { m_fn = fn; m_sz = sz; m_call = Call.FN; m_curr = &m_main; } /** * Initializes a thread object which is associated with a dynamic * D function. * * Params: * dg = The thread function. * sz = The stack size for this thread. * * In: * dg must not be null. */ this( void delegate() dg, size_t sz = 0 ) in { assert( dg ); } body { m_dg = dg; m_sz = sz; m_call = Call.DG; m_curr = &m_main; } /** * Cleans up any remaining resources used by this object. */ ~this() { if( m_addr == m_addr.init ) { return; } version( Win32 ) { m_addr = m_addr.init; CloseHandle( m_hndl ); m_hndl = m_hndl.init; } else version( Posix ) { pthread_detach( m_addr ); m_addr = m_addr.init; } } //////////////////////////////////////////////////////////////////////////// // General Actions //////////////////////////////////////////////////////////////////////////// /** * Starts the thread and invokes the function or delegate passed upon * construction. * * In: * This routine may only be called once per thread instance. * * Throws: * ThreadException if the thread fails to start. */ final void start() in { assert( !next && !prev ); } body { version( Win32 ) {} else version( Posix ) { pthread_attr_t attr; if( pthread_attr_init( &attr ) ) throw new ThreadException( "Error initializing thread attributes" ); if( m_sz && pthread_attr_setstacksize( &attr, m_sz ) ) throw new ThreadException( "Error initializing thread stack size" ); if( pthread_attr_setdetachstate( &attr, PTHREAD_CREATE_JOINABLE ) ) throw new ThreadException( "Error setting thread joinable" ); } // NOTE: This operation needs to be synchronized to avoid a race // condition with the GC. Without this lock, the thread // could start and allocate memory before being added to // the global thread list, preventing it from being scanned // and causing memory to be collected that is still in use. synchronized( slock ) { volatile multiThreadedFlag = true; version( Win32 ) { m_hndl = cast(HANDLE) _beginthreadex( null, m_sz, &thread_entryPoint, cast(void*) this, 0, &m_addr ); if( cast(size_t) m_hndl == 0 ) throw new ThreadException( "Error creating thread" ); } else version( Posix ) { m_isRunning = true; scope( failure ) m_isRunning = false; if( pthread_create( &m_addr, &attr, &thread_entryPoint, cast(void*) this ) != 0 ) throw new ThreadException( "Error creating thread" ); } add( this ); } } /** * Waits for this thread to complete. If the thread terminated as the * result of an unhandled exception, this exception will be rethrown. * * Params: * rethrow = Rethrow any unhandled exception which may have caused this * thread to terminate. * * Throws: * ThreadException if the operation fails. * Any exception not handled by the joined thread. * * Returns: * Any exception not handled by this thread if rethrow = false, null * otherwise. */ final Object join( bool rethrow = true ) { if(!isRunning()) return null; version( Win32 ) { if( WaitForSingleObject( m_hndl, INFINITE ) != WAIT_OBJECT_0 ) throw new ThreadException( "Unable to join thread" ); // NOTE: m_addr must be cleared before m_hndl is closed to avoid // a race condition with isRunning. The operation is labeled // volatile to prevent compiler reordering. volatile m_addr = m_addr.init; CloseHandle( m_hndl ); m_hndl = m_hndl.init; } else version( Posix ) { if( pthread_join( m_addr, null ) != 0 ) throw new ThreadException( "Unable to join thread" ); // NOTE: pthread_join acts as a substitute for pthread_detach, // which is normally called by the dtor. Setting m_addr // to zero ensures that pthread_detach will not be called // on object destruction. volatile m_addr = m_addr.init; } if( m_unhandled ) { if( rethrow ) throw m_unhandled; return m_unhandled; } return null; } //////////////////////////////////////////////////////////////////////////// // General Properties //////////////////////////////////////////////////////////////////////////// /** * Gets the user-readable label for this thread. * * Returns: * The name of this thread. */ final char[] name() { synchronized( this ) { return m_name; } } /** * Sets the user-readable label for this thread. * * Params: * val = The new name of this thread. */ final void name( char[] val ) { synchronized( this ) { m_name = val.dup; } } /** * Gets the daemon status for this thread. While the runtime will wait for * all normal threads to complete before tearing down the process, daemon * threads are effectively ignored and thus will not prevent the process * from terminating. In effect, daemon threads will be terminated * automatically by the OS when the process exits. * * Returns: * true if this is a daemon thread. */ final bool isDaemon() { synchronized( this ) { return m_isDaemon; } } /** * Sets the daemon status for this thread. While the runtime will wait for * all normal threads to complete before tearing down the process, daemon * threads are effectively ignored and thus will not prevent the process * from terminating. In effect, daemon threads will be terminated * automatically by the OS when the process exits. * * Params: * val = The new daemon status for this thread. */ final void isDaemon( bool val ) { synchronized( this ) { m_isDaemon = val; } } /** * Tests whether this thread is running. * * Returns: * true if the thread is running, false if not. */ final bool isRunning() { if( m_addr == m_addr.init ) { return false; } version( Win32 ) { uint ecode = 0; GetExitCodeThread( m_hndl, &ecode ); return ecode == STILL_ACTIVE; } else version( Posix ) { // NOTE: It should be safe to access this value without // memory barriers because word-tearing and such // really isn't an issue for boolean values. return m_isRunning; } } //////////////////////////////////////////////////////////////////////////// // Thread Priority Actions //////////////////////////////////////////////////////////////////////////// /** * The minimum scheduling priority that may be set for a thread. On * systems where multiple scheduling policies are defined, this value * represents the minimum valid priority for the scheduling policy of * the process. */ static const int PRIORITY_MIN; /** * The maximum scheduling priority that may be set for a thread. On * systems where multiple scheduling policies are defined, this value * represents the minimum valid priority for the scheduling policy of * the process. */ static const int PRIORITY_MAX; /** * Gets the scheduling priority for the associated thread. * * Returns: * The scheduling priority of this thread. */ final int priority() { version( Win32 ) { return GetThreadPriority( m_hndl ); } else version( Posix ) { int policy; sched_param param; if( pthread_getschedparam( m_addr, &policy, ¶m ) ) throw new ThreadException( "Unable to get thread priority" ); return param.sched_priority; } } /** * Sets the scheduling priority for the associated thread. * * Params: * val = The new scheduling priority of this thread. */ final void priority( int val ) { version( Win32 ) { if( !SetThreadPriority( m_hndl, val ) ) throw new ThreadException( "Unable to set thread priority" ); } else version( Posix ) { // NOTE: pthread_setschedprio is not implemented on linux, so use // the more complicated get/set sequence below. //if( pthread_setschedprio( m_addr, val ) ) // throw new ThreadException( "Unable to set thread priority" ); int policy; sched_param param; if( pthread_getschedparam( m_addr, &policy, ¶m ) ) throw new ThreadException( "Unable to set thread priority" ); param.sched_priority = val; if( pthread_setschedparam( m_addr, policy, ¶m ) ) throw new ThreadException( "Unable to set thread priority" ); } } //////////////////////////////////////////////////////////////////////////// // Actions on Calling Thread //////////////////////////////////////////////////////////////////////////// /** * Suspends the calling thread for at least the supplied time, up to a * maximum of (uint.max - 1) milliseconds. * * Params: * period = The minimum duration the calling thread should be suspended, * in seconds. Sub-second durations are specified as fractional * values. * * In: * period must be less than (uint.max - 1) milliseconds. * * Example: * ------------------------------------------------------------------------- * Thread.sleep( 0.05 ); // sleep for 50 milliseconds * Thread.sleep( 5 ); // sleep for 5 seconds * ------------------------------------------------------------------------- */ static void sleep( double period ) in { // NOTE: The fractional value added to period is to correct fp error. assert( period * 1000 + 0.1 < uint.max - 1 ); } body { version( Win32 ) { Sleep( cast(uint)( period * 1000 + 0.1 ) ); } else version( Posix ) { timespec tin = void; timespec tout = void; period += 0.000_000_000_1; if( tin.tv_sec.max < period ) { tin.tv_sec = tin.tv_sec.max; tin.tv_nsec = 0; } else { tin.tv_sec = cast(typeof(tin.tv_sec)) period; tin.tv_nsec = cast(typeof(tin.tv_nsec)) ((period % 1.0) * 1_000_000_000); } while( true ) { if( !nanosleep( &tin, &tout ) ) return; if( getErrno() != EINTR ) throw new ThreadException( "Unable to sleep for specified duration" ); tin = tout; } } } /+ /** * Suspends the calling thread for at least the supplied time, up to a * maximum of (uint.max - 1) milliseconds. * * Params: * period = The minimum duration the calling thread should be suspended. * * In: * period must be less than (uint.max - 1) milliseconds. * * Example: * ------------------------------------------------------------------------- * Thread.sleep( TimeSpan.milliseconds( 50 ) ); // sleep for 50 milliseconds * Thread.sleep( TimeSpan.seconds( 5 ) ); // sleep for 5 seconds * ------------------------------------------------------------------------- */ static void sleep( TimeSpan period ) in { assert( period.milliseconds < uint.max - 1 ); } body { version( Win32 ) { Sleep( cast(uint)( period.milliseconds ) ); } else version( Posix ) { timespec tin = void; timespec tout = void; if( tin.tv_sec.max < period.seconds ) { tin.tv_sec = tin.tv_sec.max; tin.tv_nsec = 0; } else { tin.tv_sec = cast(typeof(tin.tv_sec)) period.seconds; tin.tv_nsec = cast(typeof(tin.tv_nsec)) period.nanoseconds % 1_000_000_000; } while( true ) { if( !nanosleep( &tin, &tout ) ) return; if( getErrno() != EINTR ) throw new ThreadException( "Unable to sleep for specified duration" ); tin = tout; } } } /** * Suspends the calling thread for at least the supplied time, up to a * maximum of (uint.max - 1) milliseconds. * * Params: * period = The minimum duration the calling thread should be suspended, * in seconds. Sub-second durations are specified as fractional * values. Please note that because period is a floating-point * number, some accuracy may be lost for certain intervals. For * this reason, the TimeSpan overload is preferred in instances * where an exact interval is required. * * In: * period must be less than (uint.max - 1) milliseconds. * * Example: * ------------------------------------------------------------------------- * Thread.sleep( 0.05 ); // sleep for 50 milliseconds * Thread.sleep( 5 ); // sleep for 5 seconds * ------------------------------------------------------------------------- */ static void sleep( double period ) { sleep( TimeSpan.interval( period ) ); } +/ /** * Forces a context switch to occur away from the calling thread. */ static void yield() { version( Win32 ) { // NOTE: Sleep(1) is necessary because Sleep(0) does not give // lower priority threads any timeslice, so looping on // Sleep(0) could be resource-intensive in some cases. Sleep( 1 ); } else version( Posix ) { sched_yield(); } } //////////////////////////////////////////////////////////////////////////// // Thread Accessors //////////////////////////////////////////////////////////////////////////// /** * Provides a reference to the calling thread. * * Returns: * The thread object representing the calling thread. The result of * deleting this object is undefined. */ static Thread getThis() { // NOTE: This function may not be called until thread_init has // completed. See thread_suspendAll for more information // on why this might occur. version( Win32 ) { return cast(Thread) TlsGetValue( sm_this ); } else version( Posix ) { return cast(Thread) pthread_getspecific( sm_this ); } } /** * Provides a list of all threads currently being tracked by the system. * * Returns: * An array containing references to all threads currently being * tracked by the system. The result of deleting any contained * objects is undefined. */ static Thread[] getAll() { Thread[] buf; while(1){ if (buf) delete buf; buf = new Thread[sm_tlen]; synchronized( slock ) { size_t pos = 0; if (buf.length<sm_tlen) { continue; } else { buf.length=sm_tlen; } foreach( Thread t; Thread ) { buf[pos++] = t; } return buf; } } } /** * Operates on all threads currently being tracked by the system. The * result of deleting any Thread object is undefined. * * Params: * dg = The supplied code as a delegate. * * Returns: * Zero if all elemented are visited, nonzero if not. */ static int opApply( int delegate( ref Thread ) dg ) { synchronized( slock ) { int ret = 0; for( Thread t = sm_tbeg; t; t = t.next ) { ret = dg( t ); if( ret ) break; } return ret; } } //////////////////////////////////////////////////////////////////////////// // Local Storage Actions //////////////////////////////////////////////////////////////////////////// /** * Indicates the number of local storage pointers available at program * startup. It is recommended that this number be at least 64. */ static const uint LOCAL_MAX = 64; /** * Reserves a local storage pointer for use and initializes this location * to null for all running threads. * * Returns: * A key representing the array offset of this memory location. */ static uint createLocal() { synchronized( slock ) { foreach( uint key, ref bool set; sm_local ) { if( !set ) { //foreach( Thread t; sm_tbeg ) Bug in GDC 0.24 SVN (r139) for( Thread t = sm_tbeg; t; t = t.next ) { t.m_local[key] = null; } set = true; return key; } } throw new ThreadException( "No more local storage slots available" ); } } /** * Marks the supplied key as available and sets the associated location * to null for all running threads. It is assumed that any key passed * to this function is valid. The result of calling this function for * a key which is still in use is undefined. * * Params: * key = The key to delete. */ static void deleteLocal( uint key ) { synchronized( slock ) { sm_local[key] = false; // foreach( Thread t; sm_tbeg ) Bug in GDC 0.24 SVN (r139) for( Thread t = sm_tbeg; t; t = t.next ) { t.m_local[key] = null; } } } /** * Loads the value stored at key within a thread-local static array. It is * assumed that any key passed to this function is valid. * * Params: * key = The location which holds the desired data. * * Returns: * The data associated with the supplied key. */ static void* getLocal( uint key ) { return getThis().m_local[key]; } /** * Stores the supplied value at key within a thread-local static array. It * is assumed that any key passed to this function is valid. * * Params: * key = The location to store the supplied data. * val = The data to store. * * Returns: * A copy of the data which has just been stored. */ static void* setLocal( uint key, void* val ) { return getThis().m_local[key] = val; } //////////////////////////////////////////////////////////////////////////// // Static Initalizer //////////////////////////////////////////////////////////////////////////// /** * This initializer is used to set thread constants. All functional * initialization occurs within thread_init(). */ static this() { version( Win32 ) { PRIORITY_MIN = -15; PRIORITY_MAX = 15; } else version( Posix ) { int policy; sched_param param; pthread_t self = pthread_self(); int status = pthread_getschedparam( self, &policy, ¶m ); assert( status == 0 ); PRIORITY_MIN = sched_get_priority_min( policy ); assert( PRIORITY_MIN != -1 ); PRIORITY_MAX = sched_get_priority_max( policy ); assert( PRIORITY_MAX != -1 ); } } private: // // Initializes a thread object which has no associated executable function. // This is used for the main thread initialized in thread_init(). // this() { m_call = Call.NO; m_curr = &m_main; } // // Thread entry point. Invokes the function or delegate passed on // construction (if any). // final void run() { switch( m_call ) { case Call.FN: m_fn(); break; case Call.DG: m_dg(); break; default: break; } } private: // // The type of routine passed on thread construction. // enum Call { NO, FN, DG } // // Standard types // version( Win32 ) { alias uint TLSKey; alias uint ThreadAddr; } else version( Posix ) { alias pthread_key_t TLSKey; alias pthread_t ThreadAddr; } // // Local storage // static bool[LOCAL_MAX] sm_local; static TLSKey sm_this; void*[LOCAL_MAX] m_local; // // Standard thread data // version( Win32 ) { HANDLE m_hndl; } public ThreadAddr m_addr; Call m_call; char[] m_name; union { void function() m_fn; void delegate() m_dg; } size_t m_sz; version( Posix ) { bool m_isRunning; } bool m_isDaemon; public Object m_unhandled; private: //////////////////////////////////////////////////////////////////////////// // Storage of Active Thread //////////////////////////////////////////////////////////////////////////// // // Sets a thread-local reference to the current thread object. // static void setThis( Thread t ) { version( Win32 ) { TlsSetValue( sm_this, cast(void*) t ); } else version( Posix ) { pthread_setspecific( sm_this, cast(void*) t ); } } private: //////////////////////////////////////////////////////////////////////////// // Thread Context and GC Scanning Support //////////////////////////////////////////////////////////////////////////// final void pushContext( Context* c ) in { assert( !c.within ); } body { c.within = m_curr; m_curr = c; } final void popContext() in { assert( m_curr && m_curr.within ); } body { Context* c = m_curr; m_curr = c.within; c.within = null; } public final Context* topContext() in { assert( m_curr ); } body { return m_curr; } public static struct Context { void* bstack, tstack; Context* within; Context* next, prev; } Context m_main; Context* m_curr; bool m_lock; version( Win32 ) { uint[8] m_reg; // edi,esi,ebp,esp,ebx,edx,ecx,eax } private: //////////////////////////////////////////////////////////////////////////// // GC Scanning Support //////////////////////////////////////////////////////////////////////////// // NOTE: The GC scanning process works like so: // // 1. Suspend all threads. // 2. Scan the stacks of all suspended threads for roots. // 3. Resume all threads. // // Step 1 and 3 require a list of all threads in the system, while // step 2 requires a list of all thread stacks (each represented by // a Context struct). Traditionally, there was one stack per thread // and the Context structs were not necessary. However, Fibers have // changed things so that each thread has its own 'main' stack plus // an arbitrary number of nested stacks (normally referenced via // m_curr). Also, there may be 'free-floating' stacks in the system, // which are Fibers that are not currently executing on any specific // thread but are still being processed and still contain valid // roots. // // To support all of this, the Context struct has been created to // represent a stack range, and a global list of Context structs has // been added to enable scanning of these stack ranges. The lifetime // (and presence in the Context list) of a thread's 'main' stack will // be equivalent to the thread's lifetime. So the Ccontext will be // added to the list on thread entry, and removed from the list on // thread exit (which is essentially the same as the presence of a // Thread object in its own global list). The lifetime of a Fiber's // context, however, will be tied to the lifetime of the Fiber object // itself, and Fibers are expected to add/remove their Context struct // on construction/deletion. // // All use of the global lists should synchronize on this lock. // static Object slock() { return Thread.classinfo; } static Context* sm_cbeg; static size_t sm_clen; static Thread sm_tbeg; static size_t sm_tlen; // // Used for ordering threads in the global thread list. // Thread prev; Thread next; //////////////////////////////////////////////////////////////////////////// // Global Context List Operations //////////////////////////////////////////////////////////////////////////// // // Add a context to the global context list. // static void add( Context* c ) in { assert( c ); assert( !c.next && !c.prev ); } body { synchronized( slock ) { if( sm_cbeg ) { c.next = sm_cbeg; sm_cbeg.prev = c; } sm_cbeg = c; ++sm_clen; } } // // Remove a context from the global context list. // static void remove( Context* c ) in { assert( c ); assert( c.next || c.prev ); } body { synchronized( slock ) { if( c.prev ) c.prev.next = c.next; if( c.next ) c.next.prev = c.prev; if( sm_cbeg == c ) sm_cbeg = c.next; --sm_clen; } // NOTE: Don't null out c.next or c.prev because opApply currently // follows c.next after removing a node. This could be easily // addressed by simply returning the next node from this function, // however, a context should never be re-added to the list anyway // and having next and prev be non-null is a good way to // ensure that. } //////////////////////////////////////////////////////////////////////////// // Global Thread List Operations //////////////////////////////////////////////////////////////////////////// // // Add a thread to the global thread list. // static void add( Thread t ) in { assert( t ); assert( !t.next && !t.prev ); assert( t.isRunning ); } body { synchronized( slock ) { if( sm_tbeg ) { t.next = sm_tbeg; sm_tbeg.prev = t; } sm_tbeg = t; ++sm_tlen; } } // // Remove a thread from the global thread list. // static void remove( Thread t ) in { assert( t ); assert( t.next || t.prev ); } body { synchronized( slock ) { // NOTE: When a thread is removed from the global thread list its // main context is invalid and should be removed as well. // It is possible that t.m_curr could reference more // than just the main context if the thread exited abnormally // (if it was terminated), but we must assume that the user // retains a reference to them and that they may be re-used // elsewhere. Therefore, it is the responsibility of any // object that creates contexts to clean them up properly // when it is done with them. remove( &t.m_main ); if( t.prev ) t.prev.next = t.next; if( t.next ) t.next.prev = t.prev; if( sm_tbeg == t ) sm_tbeg = t.next; --sm_tlen; } // NOTE: Don't null out t.next or t.prev because opApply currently // follows t.next after removing a node. This could be easily // addressed by simply returning the next node from this function, // however, a thread should never be re-added to the list anyway // and having next and prev be non-null is a good way to // ensure that. } } //////////////////////////////////////////////////////////////////////////////// // GC Support Routines //////////////////////////////////////////////////////////////////////////////// /** * Initializes the thread module. This function must be called by the * garbage collector on startup and before any other thread routines * are called. */ extern (C) void thread_init() { // NOTE: If thread_init itself performs any allocations then the thread // routines reserved for garbage collector use may be called while // thread_init is being processed. However, since no memory should // exist to be scanned at this point, it is sufficient for these // functions to detect the condition and return immediately. version( Win32 ) { Thread.sm_this = TlsAlloc(); assert( Thread.sm_this != TLS_OUT_OF_INDEXES ); Fiber.sm_this = TlsAlloc(); assert( Thread.sm_this != TLS_OUT_OF_INDEXES ); } else version( Posix ) { int status; sigaction_t sigusr1 = void; sigaction_t sigusr2 = void; sigaction_t sigabrt = void; // This is a quick way to zero-initialize the structs without using // memset or creating a link dependency on their static initializer. (cast(byte*) &sigusr1)[0 .. sigaction_t.sizeof] = 0; (cast(byte*) &sigusr2)[0 .. sigaction_t.sizeof] = 0; (cast(byte*) &sigabrt)[0 .. sigaction_t.sizeof] = 0; // NOTE: SA_RESTART indicates that system calls should restart if they // are interrupted by a signal, but this is not available on all // Posix systems, even those that support multithreading. static if( is( typeof( SA_RESTART ) ) ) sigusr1.sa_flags = SA_RESTART; else sigusr1.sa_flags = 0; sigusr1.sa_handler = &thread_suspendHandler; // NOTE: We want to ignore all signals while in this handler, so fill // sa_mask to indicate this. status = sigfillset( &sigusr1.sa_mask ); assert( status == 0 ); status = sigdelset( &sigusr1.sa_mask , SIGABRT); assert( status == 0 ); // NOTE: Since SIGUSR2 should only be issued for threads within the // suspend handler, we don't want this signal to trigger a // restart. sigusr2.sa_flags = 0; sigusr2.sa_handler = &thread_resumeHandler; // NOTE: We want to ignore all signals while in this handler, so fill // sa_mask to indicate this. status = sigfillset( &sigusr2.sa_mask ); assert( status == 0 ); status = sigdelset( &sigusr2.sa_mask , SIGABRT); assert( status == 0 ); status = sigaction( SIGUSR1, &sigusr1, null ); assert( status == 0 ); status = sigaction( SIGUSR2, &sigusr2, null ); assert( status == 0 ); // NOTE: SA_RESTART indicates that system calls should restart if they // are interrupted by a signal, but this is not available on all // Posix systems, even those that support multithreading. static if( is( typeof( SA_RESTART ) ) ) sigabrt.sa_flags = SA_RESTART; else sigabrt.sa_flags = 0; sigabrt.sa_handler = &thread_abortHandler; // NOTE: We want to ignore all signals while in this handler, so fill // sa_mask to indicate this. status = sigfillset( &sigabrt.sa_mask ); assert( status == 0 ); status = sigaction( SIGABRT, &sigabrt, null ); assert( status == 0 ); version(AtomicSuspendCount){ suspendCount=0; } else { status = sem_init( &suspendCount, 0, 0 ); } assert( status == 0 ); status = pthread_key_create( &Thread.sm_this, null ); assert( status == 0 ); status = pthread_key_create( &Fiber.sm_this, null ); assert( status == 0 ); } thread_attachThis(); } /** * Registers the calling thread for use with Tango. If this routine is called * for a thread which is already registered, the result is undefined. */ extern (C) void thread_attachThis() { version( Win32 ) { Thread thisThread = new Thread(); Thread.Context* thisContext = &thisThread.m_main; assert( thisContext == thisThread.m_curr ); thisThread.m_addr = GetCurrentThreadId(); thisThread.m_hndl = GetCurrentThreadHandle(); thisContext.bstack = getStackBottom(); thisContext.tstack = thisContext.bstack; thisThread.m_isDaemon = true; Thread.setThis( thisThread ); } else version( Posix ) { Thread thisThread = new Thread(); Thread.Context* thisContext = thisThread.m_curr; assert( thisContext == &thisThread.m_main ); thisThread.m_addr = pthread_self(); thisContext.bstack = getStackBottom(); thisContext.tstack = thisContext.bstack; thisThread.m_isRunning = true; thisThread.m_isDaemon = true; Thread.setThis( thisThread ); } Thread.add( thisThread ); Thread.add( thisContext ); } /** * Deregisters the calling thread from use with Tango. If this routine is * called for a thread which is already registered, the result is undefined. */ extern (C) void thread_detachThis() { Thread.remove( Thread.getThis() ); } /** * Joins all non-daemon threads that are currently running. This is done by * performing successive scans through the thread list until a scan consists * of only daemon threads. */ extern (C) void thread_joinAll() { while( true ) { Thread nonDaemon = null; foreach( t; Thread ) { if( !t.isDaemon ) { nonDaemon = t; break; } } if( nonDaemon is null ) return; nonDaemon.join(); } } /** * Performs intermediate shutdown of the thread module. */ static ~this() { // NOTE: The functionality related to garbage collection must be minimally // operable after this dtor completes. Therefore, only minimal // cleanup may occur. for( Thread t = Thread.sm_tbeg; t; t = t.next ) { if( !t.isRunning ) Thread.remove( t ); } } // Used for needLock below private bool multiThreadedFlag = false; /** * This function is used to determine whether the the process is * multi-threaded. Optimizations may only be performed on this * value if the programmer can guarantee that no path from the * enclosed code will start a thread. * * Returns: * True if Thread.start() has been called in this process. */ extern (C) bool thread_needLock() { return multiThreadedFlag; } // Used for suspendAll/resumeAll below private uint suspendDepth = 0; /** * Suspend all threads but the calling thread for "stop the world" garbage * collection runs. This function may be called multiple times, and must * be followed by a matching number of calls to thread_resumeAll before * processing is resumed. * * Throws: * ThreadException if the suspend operation fails for a running thread. */ extern (C) void thread_suspendAll() { int suspendedCount=0; /** * Suspend the specified thread and load stack and register information for * use by thread_scanAll. If the supplied thread is the calling thread, * stack and register information will be loaded but the thread will not * be suspended. If the suspend operation fails and the thread is not * running then it will be removed from the global thread list, otherwise * an exception will be thrown. * * Params: * t = The thread to suspend. * * Throws: * ThreadException if the suspend operation fails for a running thread. */ void suspend( Thread t ) { version( Win32 ) { if( t.m_addr != GetCurrentThreadId() && SuspendThread( t.m_hndl ) == 0xFFFFFFFF ) { if( !t.isRunning ) { Thread.remove( t ); return; } throw new ThreadException( "Unable to suspend thread" ); } CONTEXT context = void; context.ContextFlags = CONTEXT_INTEGER | CONTEXT_CONTROL; if( !GetThreadContext( t.m_hndl, &context ) ) throw new ThreadException( "Unable to load thread context" ); if( !t.m_lock ) t.m_curr.tstack = cast(void*) context.Esp; // edi,esi,ebp,esp,ebx,edx,ecx,eax t.m_reg[0] = context.Edi; t.m_reg[1] = context.Esi; t.m_reg[2] = context.Ebp; t.m_reg[3] = context.Esp; t.m_reg[4] = context.Ebx; t.m_reg[5] = context.Edx; t.m_reg[6] = context.Ecx; t.m_reg[7] = context.Eax; } else version( Posix ) { if( t.m_addr != pthread_self() ) { if( pthread_kill( t.m_addr, SIGUSR1 ) != 0 ) { if( !t.isRunning ) { Thread.remove( t ); return; } throw new ThreadException( "Unable to suspend thread" ); } version (AtomicSuspendCount){ ++suspendedCount; version(AtomicSuspendCount){ version(SuspendOneAtTime){ // when debugging suspending all threads at once might give "lost" signals int icycle=0; suspendLoop: while (flagGet(suspendCount)!=suspendedCount){ for (size_t i=1000;i!=0;--i){ if (flagGet(suspendCount)==suspendedCount) break suspendLoop; if (++icycle==100_000){ debug(Thread) printf("waited %d cycles for thread suspension, suspendCount=%d, should be %d\nAtomic ops do not work?\nContinuing wait...\n",icycle,suspendCount,suspendedCount); } Thread.yield(); } Thread.sleep(0.0001); } } } } else { sem_wait( &suspendCount ); // shouldn't the return be checked and maybe a loop added for further interrupts // as in Semaphore.d ? } } else if( !t.m_lock ) { t.m_curr.tstack = getStackTop(); } } } // NOTE: We've got an odd chicken & egg problem here, because while the GC // is required to call thread_init before calling any other thread // routines, thread_init may allocate memory which could in turn // trigger a collection. Thus, thread_suspendAll, thread_scanAll, // and thread_resumeAll must be callable before thread_init completes, // with the assumption that no other GC memory has yet been allocated // by the system, and thus there is no risk of losing data if the // global thread list is empty. The check of Thread.sm_tbeg // below is done to ensure thread_init has completed, and therefore // that calling Thread.getThis will not result in an error. For the // short time when Thread.sm_tbeg is null, there is no reason // not to simply call the multithreaded code below, with the // expectation that the foreach loop will never be entered. if( !multiThreadedFlag && Thread.sm_tbeg ) { if( ++suspendDepth == 1 ) { suspend( Thread.getThis() ); } return; } _d_monitorenter(Thread.slock); { if( ++suspendDepth > 1 ) return; // NOTE: I'd really prefer not to check isRunning within this loop but // not doing so could be problematic if threads are termianted // abnormally and a new thread is created with the same thread // address before the next GC run. This situation might cause // the same thread to be suspended twice, which would likely // cause the second suspend to fail, the garbage collection to // abort, and Bad Things to occur. for( Thread t = Thread.sm_tbeg; t; t = t.next ) { if( t.isRunning ){ suspend( t ); } else Thread.remove( t ); } version( Posix ) { version(AtomicSuspendCount){ int icycle=0; suspendLoop2: while (flagGet(suspendCount)!=suspendedCount){ for (size_t i=1000;i!=0;--i){ if (flagGet(suspendCount)==suspendedCount) break suspendLoop2; if (++icycle==1000_000){ debug(Thread) printf("waited %d cycles for thread suspension, suspendCount=%d, should be %d\nAtomic ops do not work?\nContinuing wait...\n",icycle,suspendCount,suspendedCount); } Thread.yield(); } Thread.sleep(0.0001); } } } } } /** * Resume all threads but the calling thread for "stop the world" garbage * collection runs. This function must be called once for each preceding * call to thread_suspendAll before the threads are actually resumed. * * In: * This routine must be preceded by a call to thread_suspendAll. * * Throws: * ThreadException if the resume operation fails for a running thread. */ extern (C) void thread_resumeAll() in { assert( suspendDepth > 0 ); } body { version(AtomicSuspendCount) version(SuspendOneAtTime) auto suspendedCount=flagGet(suspendCount); /** * Resume the specified thread and unload stack and register information. * If the supplied thread is the calling thread, stack and register * information will be unloaded but the thread will not be resumed. If * the resume operation fails and the thread is not running then it will * be removed from the global thread list, otherwise an exception will be * thrown. * * Params: * t = The thread to resume. * * Throws: * ThreadException if the resume fails for a running thread. */ void resume( Thread t ) { version( Win32 ) { if( t.m_addr != GetCurrentThreadId() && ResumeThread( t.m_hndl ) == 0xFFFFFFFF ) { if( !t.isRunning ) { Thread.remove( t ); return; } throw new ThreadException( "Unable to resume thread" ); } if( !t.m_lock ) t.m_curr.tstack = t.m_curr.bstack; t.m_reg[0 .. $] = 0; } else version( Posix ) { if( t.m_addr != pthread_self() ) { if( pthread_kill( t.m_addr, SIGUSR2 ) != 0 ) { if( !t.isRunning ) { Thread.remove( t ); return; } throw new ThreadException( "Unable to resume thread" ); } version (AtomicSuspendCount){ version(SuspendOneAtTime){ // when debugging suspending all threads at once might give "lost" signals --suspendedCount; int icycle=0; recoverLoop: while(flagGet(suspendCount)>suspendedCount){ for (size_t i=1000;i!=0;--i){ if (flagGet(suspendCount)==suspendedCount) break recoverLoop; if (++icycle==100_000){ debug(Thread) printf("waited %d cycles for thread recover, suspendCount=%d, should be %d\nAtomic ops do not work?\nContinuing wait...\n",icycle,suspendCount,suspendedCount); } Thread.yield(); } Thread.sleep(0.0001); } } } else { sem_wait( &suspendCount ); // shouldn't the return be checked and maybe a loop added for further interrupts // as in Semaphore.d ? } } else if( !t.m_lock ) { t.m_curr.tstack = t.m_curr.bstack; } } } // NOTE: See thread_suspendAll for the logic behind this. if( !multiThreadedFlag && Thread.sm_tbeg ) { if( --suspendDepth == 0 ) resume( Thread.getThis() ); return; } { scope(exit) _d_monitorexit(Thread.slock); if( --suspendDepth > 0 ) return; { for( Thread t = Thread.sm_tbeg; t; t = t.next ) { resume( t ); } version(AtomicSuspendCount){ int icycle=0; recoverLoop2: while(flagGet(suspendCount)>0){ for (size_t i=1000;i!=0;--i){ Thread.yield(); if (flagGet(suspendCount)==0) break recoverLoop2; if (++icycle==100_000){ debug(Thread) printf("waited %d cycles for thread recovery, suspendCount=%d, should be %d\nAtomic ops do not work?\nContinuing wait...\n",icycle,suspendCount,0); } } Thread.sleep(0.0001); } } } } } private alias void delegate( void*, void* ) scanAllThreadsFn; /** * The main entry point for garbage collection. The supplied delegate * will be passed ranges representing both stack and register values. * * Params: * scan = The scanner function. It should scan from p1 through p2 - 1. * curStackTop = An optional pointer to the top of the calling thread's stack. * * In: * This routine must be preceded by a call to thread_suspendAll. */ extern (C) void thread_scanAll( scanAllThreadsFn scan, void* curStackTop = null ) in { assert( suspendDepth > 0 ); } body { Thread thisThread = null; void* oldStackTop = null; if( curStackTop && Thread.sm_tbeg ) { thisThread = Thread.getThis(); if( thisThread && (!thisThread.m_lock) ) { oldStackTop = thisThread.m_curr.tstack; thisThread.m_curr.tstack = curStackTop; } } scope( exit ) { if( curStackTop && Thread.sm_tbeg ) { if( thisThread && (!thisThread.m_lock) ) { thisThread.m_curr.tstack = oldStackTop; } } } // NOTE: Synchronizing on Thread.slock is not needed because this // function may only be called after all other threads have // been suspended from within the same lock. for( Thread.Context* c = Thread.sm_cbeg; c; c = c.next ) { version( StackGrowsDown ) { // NOTE: We can't index past the bottom of the stack // so don't do the "+1" for StackGrowsDown. if( c.tstack && c.tstack < c.bstack ) scan( c.tstack, c.bstack ); } else { if( c.bstack && c.bstack < c.tstack ) scan( c.bstack, c.tstack + 1 ); } } version( Win32 ) { for( Thread t = Thread.sm_tbeg; t; t = t.next ) { scan( &t.m_reg[0], &t.m_reg[0] + t.m_reg.length ); } } } //////////////////////////////////////////////////////////////////////////////// // Thread Local //////////////////////////////////////////////////////////////////////////////// /** * This class encapsulates the operations required to initialize, access, and * destroy thread local data. */ class ThreadLocal( T ) { //////////////////////////////////////////////////////////////////////////// // Initialization //////////////////////////////////////////////////////////////////////////// /** * Initializes thread local storage for the indicated value which will be * initialized to def for all threads. * * Params: * def = The default value to return if no value has been explicitly set. */ this( T def = T.init ) { m_def = def; m_key = Thread.createLocal(); } ~this() { Thread.deleteLocal( m_key ); } //////////////////////////////////////////////////////////////////////////// // Accessors //////////////////////////////////////////////////////////////////////////// /** * Gets the value last set by the calling thread, or def if no such value * has been set. * * Returns: * The stored value or def if no value is stored. */ T val() { Wrap* wrap = cast(Wrap*) Thread.getLocal( m_key ); return wrap ? wrap.val : m_def; } /** * Copies newval to a location specific to the calling thread, and returns * newval. * * Params: * newval = The value to set. * * Returns: * The value passed to this function. */ T val( T newval ) { Wrap* wrap = cast(Wrap*) Thread.getLocal( m_key ); if( wrap is null ) { wrap = new Wrap; Thread.setLocal( m_key, wrap ); } wrap.val = newval; return newval; } private: // // A wrapper for the stored data. This is needed for determining whether // set has ever been called for this thread (and therefore whether the // default value should be returned) and also to flatten the differences // between data that is smaller and larger than (void*).sizeof. The // obvious tradeoff here is an extra per-thread allocation for each // ThreadLocal value as compared to calling the Thread routines directly. // struct Wrap { T val; } T m_def; uint m_key; } //////////////////////////////////////////////////////////////////////////////// // Thread Group //////////////////////////////////////////////////////////////////////////////// /** * This class is intended to simplify certain common programming techniques. */ class ThreadGroup { /** * Creates and starts a new Thread object that executes fn and adds it to * the list of tracked threads. * * Params: * fn = The thread function. * * Returns: * A reference to the newly created thread. */ final Thread create( void function() fn ) { Thread t = new Thread( fn ); t.start(); synchronized( this ) { m_all[t] = t; } return t; } /** * Creates and starts a new Thread object that executes dg and adds it to * the list of tracked threads. * * Params: * dg = The thread function. * * Returns: * A reference to the newly created thread. */ final Thread create( void delegate() dg ) { Thread t = new Thread( dg ); t.start(); synchronized( this ) { m_all[t] = t; } return t; } /** * Add t to the list of tracked threads if it is not already being tracked. * * Params: * t = The thread to add. * * In: * t must not be null. */ final void add( Thread t ) in { assert( t ); } body { synchronized( this ) { m_all[t] = t; } } /** * Removes t from the list of tracked threads. No operation will be * performed if t is not currently being tracked by this object. * * Params: * t = The thread to remove. * * In: * t must not be null. */ final void remove( Thread t ) in { assert( t ); } body { synchronized( this ) { m_all.remove( t ); } } /** * Operates on all threads currently tracked by this object. */ final int opApply( int delegate( ref Thread ) dg ) { synchronized( this ) { int ret = 0; // NOTE: This loop relies on the knowledge that m_all uses the // Thread object for both the key and the mapped value. foreach( Thread t; m_all.keys ) { ret = dg( t ); if( ret ) break; } return ret; } } /** * Iteratively joins all tracked threads. This function will block add, * remove, and opApply until it completes. * * Params: * rethrow = Rethrow any unhandled exception which may have caused the * current thread to terminate. * * Throws: * Any exception not handled by the joined threads. */ final void joinAll( bool rethrow = true ) { synchronized( this ) { // NOTE: This loop relies on the knowledge that m_all uses the // Thread object for both the key and the mapped value. foreach( Thread t; m_all.keys ) { t.join( rethrow ); } } } private: Thread[Thread] m_all; } //////////////////////////////////////////////////////////////////////////////// // Fiber Platform Detection and Memory Allocation //////////////////////////////////////////////////////////////////////////////// private { version( D_InlineAsm_X86 ) { version( X86_64 ) { // Shouldn't an x64 compiler be setting D_InlineAsm_X86_64 instead? } else { version( Win32 ) version = AsmX86_Win32; else version( Posix ) version = AsmX86_Posix; } } else version( D_InlineAsm_X86_64 ) { version( Posix ) version = AsmX86_64_Posix; } else version( PPC ) { version( Posix ) version = AsmPPC_Posix; } version( Posix ) { import tango.stdc.posix.unistd; // for sysconf import tango.stdc.posix.sys.mman; // for mmap import tango.stdc.posix.stdlib; // for malloc, valloc, free version( AsmX86_Win32 ) {} else version( AsmX86_Posix ) {} else version( AsmX86_64_Posix ) {} else version( AsmPPC_Posix ) {} else { // NOTE: The ucontext implementation requires architecture specific // data definitions to operate so testing for it must be done // by checking for the existence of ucontext_t rather than by // a version identifier. Please note that this is considered // an obsolescent feature according to the POSIX spec, so a // custom solution is still preferred. import tango.stdc.posix.ucontext; static assert( is( ucontext_t ), "Unknown fiber implementation"); } } const size_t PAGESIZE; } static this() { static if( is( typeof( GetSystemInfo ) ) ) { SYSTEM_INFO info; GetSystemInfo( &info ); PAGESIZE = info.dwPageSize; assert( PAGESIZE < int.max ); } else static if( is( typeof( sysconf ) ) && is( typeof( _SC_PAGESIZE ) ) ) { PAGESIZE = cast(size_t) sysconf( _SC_PAGESIZE ); assert( PAGESIZE < int.max ); } else { version( PPC ) PAGESIZE = 8192; else PAGESIZE = 4096; } } //////////////////////////////////////////////////////////////////////////////// // Fiber Entry Point and Context Switch //////////////////////////////////////////////////////////////////////////////// private { extern (C) void fiber_entryPoint() { Fiber obj = Fiber.getThis(); assert( obj ); assert( Thread.getThis().m_curr is obj.m_ctxt ); volatile Thread.getThis().m_lock = false; obj.m_ctxt.tstack = obj.m_ctxt.bstack; obj.m_state = Fiber.State.EXEC; try { obj.run(); } catch( Object o ) { obj.m_unhandled = o; } static if( is( ucontext_t ) ) obj.m_ucur = &obj.m_utxt; obj.m_state = Fiber.State.TERM; obj.switchOut(); } // NOTE: If AsmPPC_Posix is defined then the context switch routine will // be defined externally until GDC supports inline PPC ASM. version( AsmPPC_Posix ) extern (C) void fiber_switchContext( void** oldp, void* newp ); else extern (C) void fiber_switchContext( void** oldp, void* newp ) { // NOTE: The data pushed and popped in this routine must match the // default stack created by Fiber.initStack or the initial // switch into a new context will fail. version( AsmX86_Win32 ) { asm { naked; // save current stack state push EBP; mov EBP, ESP; push EAX; push dword ptr FS:[0]; push dword ptr FS:[4]; push dword ptr FS:[8]; push EBX; push ESI; push EDI; // store oldp again with more accurate address mov EAX, dword ptr 8[EBP]; mov [EAX], ESP; // load newp to begin context switch mov ESP, dword ptr 12[EBP]; // load saved state from new stack pop EDI; pop ESI; pop EBX; pop dword ptr FS:[8]; pop dword ptr FS:[4]; pop dword ptr FS:[0]; pop EAX; pop EBP; // 'return' to complete switch ret; } } else version( AsmX86_Posix ) { asm { naked; // save current stack state push EBP; mov EBP, ESP; push EAX; push EBX; push ECX; push ESI; push EDI; // store oldp again with more accurate address mov EAX, dword ptr 8[EBP]; mov [EAX], ESP; // load newp to begin context switch mov ESP, dword ptr 12[EBP]; // load saved state from new stack pop EDI; pop ESI; pop ECX; pop EBX; pop EAX; pop EBP; // 'return' to complete switch ret; } } else version( AsmX86_64_Posix ) { version( DigitalMars ) const dmdgdc = true; else version (GNU) const dmdgdc = true; else const dmdgdc = false; static if (dmdgdc == true) asm { naked; // save current stack state push RBP; mov RBP, RSP; push RBX; push R12; push R13; push R14; push R15; sub RSP, 4; stmxcsr [RSP]; sub RSP, 4; //version(SynchroFloatExcept){ fstcw [RSP]; fwait; //} else { // fnstcw [RSP]; // fnclex; //} // store oldp again with more accurate address mov [RDI], RSP; // load newp to begin context switch mov RSP, RSI; // load saved state from new stack fldcw [RSP]; add RSP, 4; ldmxcsr [RSP]; add RSP, 4; pop R15; pop R14; pop R13; pop R12; pop RBX; pop RBP; // 'return' to complete switch ret; } else asm { naked; // save current stack state pushq RBP; mov RBP, RSP; pushq RBX; pushq R12; pushq R13; pushq R14; pushq R15; sub RSP, 4; stmxcsr [RSP]; sub RSP, 4; //version(SynchroFloatExcept){ fstcw [RSP]; fwait; //} else { // fnstcw [RSP]; // fnclex; //} // store oldp again with more accurate address mov [RDI], RSP; // load newp to begin context switch mov RSP, RSI; // load saved state from new stack fldcw [RSP]; add RSP, 4; ldmxcsr [RSP]; add RSP, 4; popq R15; popq R14; popq R13; popq R12; popq RBX; popq RBP; // 'return' to complete switch ret; } } else static if( is( ucontext_t ) ) { Fiber cfib = Fiber.getThis(); void* ucur = cfib.m_ucur; *oldp = &ucur; swapcontext( **(cast(ucontext_t***) oldp), *(cast(ucontext_t**) newp) ); } } } //////////////////////////////////////////////////////////////////////////////// // Fiber //////////////////////////////////////////////////////////////////////////////// private char[] ptrToStr(size_t addr,char[]buf){ char[] digits="0123456789ABCDEF"; enum{ nDigits=size_t.sizeof*2 } if (nDigits>buf.length) assert(0); char[] res=buf[0..nDigits]; size_t addrAtt=addr; for (int i=nDigits;i!=0;--i){ res[i-1]=digits[addrAtt&0xF]; addrAtt>>=4; } return res; } /** * This class provides a cooperative concurrency mechanism integrated with the * threading and garbage collection functionality. Calling a fiber may be * considered a blocking operation that returns when the fiber yields (via * Fiber.yield()). Execution occurs within the context of the calling thread * so synchronization is not necessary to guarantee memory visibility so long * as the same thread calls the fiber each time. Please note that there is no * requirement that a fiber be bound to one specific thread. Rather, fibers * may be freely passed between threads so long as they are not currently * executing. Like threads, a new fiber thread may be created using either * derivation or composition, as in the following example. * * Example: * ---------------------------------------------------------------------- * class DerivedFiber : Fiber * { * this() * { * super( &run ); * } * * private : * void run() * { * printf( "Derived fiber running.\n" ); * } * } * * void fiberFunc() * { * printf( "Composed fiber running.\n" ); * Fiber.yield(); * printf( "Composed fiber running.\n" ); * } * * // create instances of each type * Fiber derived = new DerivedFiber(); * Fiber composed = new Fiber( &fiberFunc ); * * // call both fibers once * derived.call(); * composed.call(); * printf( "Execution returned to calling context.\n" ); * composed.call(); * * // since each fiber has run to completion, each should have state TERM * assert( derived.state == Fiber.State.TERM ); * assert( composed.state == Fiber.State.TERM ); * ---------------------------------------------------------------------- * * Authors: Based on a design by Mikola Lysenko. */ class Fiber { static class Scheduler { alias void* Handle; enum Type {Read=1, Write=2, Accept=3, Connect=4, Transfer=5} void pause (uint ms) {} void ready (Fiber fiber) {} void open (Handle fd, char[] name) {} void close (Handle fd, char[] name) {} void await (Handle fd, Type t, uint timeout) {} void spawn (char[] name, void delegate() dg, size_t stack=8192) {} } struct Event // scheduler support { uint idx; // support for timer removal Fiber next; // linked list of elapsed fibers void* data; // data to exchange ulong clock; // request timeout duration Scheduler.Handle handle; // IO request handle Scheduler scheduler; // associated scheduler (may be null) } /+ final override int opCmp (Object o) { throw new Exception ("Invalid opCmp in Fiber"); auto other = cast(Fiber) cast(void*) o; if (other) { auto x = cast(long) event.clock - cast(long) other.event.clock; return (x < 0 ? -1 : x is 0 ? 0 : 1); } return 1; } +/ final static Scheduler scheduler () { return getThis.event.scheduler; } //////////////////////////////////////////////////////////////////////////// // Initialization //////////////////////////////////////////////////////////////////////////// /** * Initializes an empty fiber object * * (useful to reset it) */ this(size_t sz){ m_dg = null; m_fn = null; m_call = Call.NO; m_state = State.TERM; m_unhandled = null; allocStack( sz ); } /** * Initializes a fiber object which is associated with a static * D function. * * Params: * fn = The thread function. * sz = The stack size for this fiber. * * In: * fn must not be null. */ this( void function() fn, size_t sz = PAGESIZE) in { assert( fn ); } body { m_fn = fn; m_call = Call.FN; m_state = State.HOLD; allocStack( sz ); initStack(); } /** * Initializes a fiber object which is associated with a dynamic * D function. * * Params: * dg = The thread function. * sz = The stack size for this fiber. * * In: * dg must not be null. */ this( void delegate() dg, size_t sz = PAGESIZE, Scheduler s = null ) in { assert( dg ); } body { event.scheduler = s; m_dg = dg; m_call = Call.DG; m_state = State.HOLD; allocStack(sz); initStack(); } /** * Cleans up any remaining resources used by this object. */ ~this() { // NOTE: A live reference to this object will exist on its associated // stack from the first time its call() method has been called // until its execution completes with State.TERM. Thus, the only // times this dtor should be called are either if the fiber has // terminated (and therefore has no active stack) or if the user // explicitly deletes this object. The latter case is an error // but is not easily tested for, since State.HOLD may imply that // the fiber was just created but has never been run. There is // not a compelling case to create a State.INIT just to offer a // means of ensuring the user isn't violating this object's // contract, so for now this requirement will be enforced by // documentation only. freeStack(); } //////////////////////////////////////////////////////////////////////////// // General Actions //////////////////////////////////////////////////////////////////////////// /** * Transfers execution to this fiber object. The calling context will be * suspended until the fiber calls Fiber.yield() or until it terminates * via an unhandled exception. * * Params: * rethrow = Rethrow any unhandled exception which may have caused this * fiber to terminate. * * In: * This fiber must be in state HOLD. * * Throws: * Any exception not handled by the joined thread. * * Returns: * Any exception not handled by this fiber if rethrow = false, null * otherwise. */ final Object call( bool rethrow = true ) in { assert( m_state == State.HOLD ); } body { Fiber cur = getThis(); static if( is( ucontext_t ) ) m_ucur = cur ? &cur.m_utxt : &Fiber.sm_utxt; setThis( this ); this.switchIn(); setThis( cur ); static if( is( ucontext_t ) ) m_ucur = null; // NOTE: If the fiber has terminated then the stack pointers must be // reset. This ensures that the stack for this fiber is not // scanned if the fiber has terminated. This is necessary to // prevent any references lingering on the stack from delaying // the collection of otherwise dead objects. The most notable // being the current object, which is referenced at the top of // fiber_entryPoint. if( m_state == State.TERM ) { m_ctxt.tstack = m_ctxt.bstack; } if( m_unhandled ) { Object obj = m_unhandled; m_unhandled = null; if( rethrow ) throw obj; return obj; } return null; } /** * Resets this fiber so that it may be re-used with the same function. * This routine may only be * called for fibers that have terminated, as doing otherwise could result * in scope-dependent functionality that is not executed. Stack-based * classes, for example, may not be cleaned up properly if a fiber is reset * before it has terminated. * * In: * This fiber must be in state TERM, and have a valid function/delegate. */ final void reset() in { assert( m_call != Call.NO ); assert( m_state == State.TERM ); assert( m_ctxt.tstack == m_ctxt.bstack ); } body { m_state = State.HOLD; initStack(); m_unhandled = null; } /** * Reinitializes a fiber object which is associated with a static * D function. * * Params: * fn = The thread function. * * In: * This fiber must be in state TERM. * fn must not be null. */ final void reset( void function() fn ) in { assert( fn ); assert( m_state == State.TERM ); assert( m_ctxt.tstack == m_ctxt.bstack ); } body { m_fn = fn; m_call = Call.FN; m_state = State.HOLD; initStack(); m_unhandled = null; } /** * Reinitializes a fiber object which is associated with a dynamic * D function. * * Params: * dg = The thread function. * * In: * This fiber must be in state TERM. * dg must not be null. */ final void reset( void delegate() dg ) in { assert( dg ); assert( m_state == State.TERM ); assert( m_ctxt.tstack == m_ctxt.bstack ); } body { m_dg = dg; m_call = Call.DG; m_state = State.HOLD; initStack(); m_unhandled = null; } /** * Clears the fiber from all references to a previous call (unhandled exceptions, delegate) * * In: * This fiber must be in state TERM. */ final void clear() in { assert( m_state == State.TERM ); assert( m_ctxt.tstack == m_ctxt.bstack ); } body { if (m_state != State.TERM){ char[20] buf; throw new Exception("Fiber@"~ptrToStr(cast(size_t)cast(void*)this,buf)~" in unexpected state "~ptrToStr(m_state,buf),__FILE__,__LINE__); } if (m_ctxt.tstack != m_ctxt.bstack){ char[20] buf; throw new Exception("Fiber@"~ptrToStr(cast(size_t)cast(void*)this,buf)~" bstack="~ptrToStr(cast(size_t)cast(void*)m_ctxt.bstack,buf)~" != tstack="~ptrToStr(cast(size_t)cast(void*)m_ctxt.tstack,buf),__FILE__,__LINE__); } m_dg = null; m_fn = null; m_call = Call.NO; m_state = State.TERM; m_unhandled = null; } //////////////////////////////////////////////////////////////////////////// // General Properties //////////////////////////////////////////////////////////////////////////// /** * A fiber may occupy one of three states: HOLD, EXEC, and TERM. The HOLD * state applies to any fiber that is suspended and ready to be called. * The EXEC state will be set for any fiber that is currently executing. * And the TERM state is set when a fiber terminates. Once a fiber * terminates, it must be reset before it may be called again. */ enum State { HOLD, /// EXEC, /// TERM /// } /** * Gets the current state of this fiber. * * Returns: * The state of this fiber as an enumerated value. */ final State state() { return m_state; } size_t stackSize(){ return m_size; } //////////////////////////////////////////////////////////////////////////// // Actions on Calling Fiber //////////////////////////////////////////////////////////////////////////// /** * Forces a context switch to occur away from the calling fiber. */ final void cede () { assert( m_state == State.EXEC ); static if( is( ucontext_t ) ) m_ucur = &m_utxt; m_state = State.HOLD; switchOut(); m_state = State.EXEC; } /** * Forces a context switch to occur away from the calling fiber. */ static void yield() { Fiber cur = getThis; assert( cur, "Fiber.yield() called with no active fiber" ); if (cur.event.scheduler) cur.event.scheduler.pause (0); else cur.cede; } /** * Forces a context switch to occur away from the calling fiber and then * throws obj in the calling fiber. * * Params: * obj = The object to throw. * * In: * obj must not be null. */ static void yieldAndThrow( Object obj ) in { assert( obj ); } body { Fiber cur = getThis(); assert( cur, "Fiber.yield(obj) called with no active fiber" ); cur.m_unhandled = obj; if (cur.event.scheduler) cur.event.scheduler.pause (0); else cur.cede; } //////////////////////////////////////////////////////////////////////////// // Fiber Accessors //////////////////////////////////////////////////////////////////////////// /** * Provides a reference to the calling fiber or null if no fiber is * currently active. * * Returns: * The fiber object representing the calling fiber or null if no fiber * is currently active. The result of deleting this object is undefined. */ static Fiber getThis() { version( Win32 ) { return cast(Fiber) TlsGetValue( sm_this ); } else version( Posix ) { return cast(Fiber) pthread_getspecific( sm_this ); } } //////////////////////////////////////////////////////////////////////////// // Static Initialization //////////////////////////////////////////////////////////////////////////// static this() { version( Win32 ) { sm_this = TlsAlloc(); assert( sm_this != TLS_OUT_OF_INDEXES ); } else version( Posix ) { int status; status = pthread_key_create( &sm_this, null ); assert( status == 0 ); static if( is( ucontext_t ) ) { status = getcontext( &sm_utxt ); assert( status == 0 ); } } } private: // // Initializes a fiber object which has no associated executable function. // this() { m_call = Call.NO; } // // Fiber entry point. Invokes the function or delegate passed on // construction (if any). // final void run() { switch( m_call ) { case Call.FN: m_fn(); break; case Call.DG: m_dg(); break; default: break; } } private: // // The type of routine passed on fiber construction. // enum Call { NO, FN, DG } // // Standard fiber data // Call m_call; union { void function() m_fn; void delegate() m_dg; } bool m_isRunning; Object m_unhandled; State m_state; char[] m_name; public: Event event; private: //////////////////////////////////////////////////////////////////////////// // Stack Management //////////////////////////////////////////////////////////////////////////// // // Allocate a new stack for this fiber. // final void allocStack( size_t sz ) in { assert( !m_pmem && !m_ctxt ); } body { // adjust alloc size to a multiple of PAGESIZE sz += PAGESIZE - 1; sz -= sz % PAGESIZE; // NOTE: This instance of Thread.Context is dynamic so Fiber objects // can be collected by the GC so long as no user level references // to the object exist. If m_ctxt were not dynamic then its // presence in the global context list would be enough to keep // this object alive indefinitely. An alternative to allocating // room for this struct explicitly would be to mash it into the // base of the stack being allocated below. However, doing so // requires too much special logic to be worthwhile. m_ctxt = new Thread.Context; static if( is( typeof( VirtualAlloc ) ) ) { // reserve memory for stack m_pmem = VirtualAlloc( null, sz + PAGESIZE, MEM_RESERVE, PAGE_NOACCESS ); if( !m_pmem ) { throw new FiberException( "Unable to reserve memory for stack" ); } version( StackGrowsDown ) { void* stack = m_pmem + PAGESIZE; void* guard = m_pmem; void* pbase = stack + sz; } else { void* stack = m_pmem; void* guard = m_pmem + sz; void* pbase = stack; } // allocate reserved stack segment stack = VirtualAlloc( stack, sz, MEM_COMMIT, PAGE_READWRITE ); if( !stack ) { throw new FiberException( "Unable to allocate memory for stack" ); } // allocate reserved guard page guard = VirtualAlloc( guard, PAGESIZE, MEM_COMMIT, PAGE_READWRITE | PAGE_GUARD ); if( !guard ) { throw new FiberException( "Unable to create guard page for stack" ); } m_ctxt.bstack = pbase; m_ctxt.tstack = pbase; m_size = sz; } else { static if( is( typeof( mmap ) ) ) { m_pmem = mmap( null, sz, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0 ); if( m_pmem == MAP_FAILED ) m_pmem = null; } else static if( is( typeof( valloc ) ) ) { m_pmem = valloc( sz ); } else static if( is( typeof( malloc ) ) ) { m_pmem = malloc( sz ); } else { m_pmem = null; } if( !m_pmem ) { throw new FiberException( "Unable to allocate memory for stack" ); } version( StackGrowsDown ) { m_ctxt.bstack = m_pmem + sz; m_ctxt.tstack = m_pmem + sz; } else { m_ctxt.bstack = m_pmem; m_ctxt.tstack = m_pmem; } m_size = sz; } Thread.add( m_ctxt ); } // // Free this fiber's stack. // final void freeStack() in { assert( m_pmem && m_ctxt ); } body { // NOTE: Since this routine is only ever expected to be called from // the dtor, pointers to freed data are not set to null. // NOTE: m_ctxt is guaranteed to be alive because it is held in the // global context list. Thread.remove( m_ctxt ); static if( is( typeof( VirtualAlloc ) ) ) { VirtualFree( m_pmem, 0, MEM_RELEASE ); } else static if( is( typeof( mmap ) ) ) { munmap( m_pmem, m_size ); } else static if( is( typeof( valloc ) ) ) { free( m_pmem ); } else static if( is( typeof( malloc ) ) ) { free( m_pmem ); } delete m_ctxt; } // // Initialize the allocated stack. // final void initStack() in { assert( m_ctxt.tstack && m_ctxt.tstack == m_ctxt.bstack ); assert( cast(size_t) m_ctxt.bstack % (void*).sizeof == 0 ); } body { void* pstack = m_ctxt.tstack; scope( exit ) m_ctxt.tstack = pstack; void push( size_t val ) { version( StackGrowsDown ) { pstack -= size_t.sizeof; *(cast(size_t*) pstack) = val; } else { pstack += size_t.sizeof; *(cast(size_t*) pstack) = val; } } // NOTE: On OS X the stack must be 16-byte aligned according to the // IA-32 call spec. version( darwin ) { pstack = cast(void*)(cast(uint)(pstack) - (cast(uint)(pstack) & 0x0F)); } version( AsmX86_Win32 ) { push( cast(size_t) &fiber_entryPoint ); // EIP push( 0xFFFFFFFF ); // EBP push( 0x00000000 ); // EAX push( 0xFFFFFFFF ); // FS:[0] version( StackGrowsDown ) { push( cast(size_t) m_ctxt.bstack ); // FS:[4] push( cast(size_t) m_ctxt.bstack - m_size ); // FS:[8] } else { push( cast(size_t) m_ctxt.bstack ); // FS:[4] push( cast(size_t) m_ctxt.bstack + m_size ); // FS:[8] } push( 0x00000000 ); // EBX push( 0x00000000 ); // ESI push( 0x00000000 ); // EDI } else version( AsmX86_Posix ) { push( 0x00000000 ); // strange pre EIP push( cast(size_t) &fiber_entryPoint ); // EIP push( (cast(size_t)pstack)+8 ); // EBP push( 0x00000000 ); // EAX push( getEBX() ); // EBX used for PIC code push( 0x00000000 ); // ECX just to have it aligned... push( 0x00000000 ); // ESI push( 0x00000000 ); // EDI } else version( AsmX86_64_Posix ) { push( 0x00000000 ); // strange pre EIP push( cast(size_t) &fiber_entryPoint ); // RIP push( (cast(size_t)pstack)+8 ); // RBP push( 0x00000000_00000000 ); // RBX push( 0x00000000_00000000 ); // R12 push( 0x00000000_00000000 ); // R13 push( 0x00000000_00000000 ); // R14 push( 0x00000000_00000000 ); // R15 push( 0x00001f80_0000037f ); // MXCSR (32 bits), unused (16 bits) , x87 control (16 bits) } else version( AsmPPC_Posix ) { version( StackGrowsDown ) { pstack -= int.sizeof * 5; } else { pstack += int.sizeof * 5; } push( cast(size_t) &fiber_entryPoint ); // link register push( 0x00000000 ); // control register push( 0x00000000 ); // old stack pointer // GPR values version( StackGrowsDown ) { pstack -= int.sizeof * 20; } else { pstack += int.sizeof * 20; } assert( cast(uint) pstack & 0x0f == 0 ); } else static if( is( ucontext_t ) ) { getcontext( &m_utxt ); // patch from #1707 - thanks to jerdfelt //m_utxt.uc_stack.ss_sp = m_ctxt.bstack; m_utxt.uc_stack.ss_sp = m_pmem; m_utxt.uc_stack.ss_size = m_size; makecontext( &m_utxt, &fiber_entryPoint, 0 ); // NOTE: If ucontext is being used then the top of the stack will // be a pointer to the ucontext_t struct for that fiber. push( cast(size_t) &m_utxt ); } } public Thread.Context* m_ctxt; public size_t m_size; void* m_pmem; static if( is( ucontext_t ) ) { // NOTE: The static ucontext instance is used to represent the context // of the main application thread. static ucontext_t sm_utxt = void; ucontext_t m_utxt = void; ucontext_t* m_ucur = null; } private: //////////////////////////////////////////////////////////////////////////// // Storage of Active Fiber //////////////////////////////////////////////////////////////////////////// // // Sets a thread-local reference to the current fiber object. // static void setThis( Fiber f ) { version( Win32 ) { TlsSetValue( sm_this, cast(void*) f ); } else version( Posix ) { pthread_setspecific( sm_this, cast(void*) f ); } } static Thread.TLSKey sm_this; private: //////////////////////////////////////////////////////////////////////////// // Context Switching //////////////////////////////////////////////////////////////////////////// // // Switches into the stack held by this fiber. // final void switchIn() { Thread tobj = Thread.getThis(); void** oldp = &tobj.m_curr.tstack; void* newp = m_ctxt.tstack; // NOTE: The order of operations here is very important. The current // stack top must be stored before m_lock is set, and pushContext // must not be called until after m_lock is set. This process // is intended to prevent a race condition with the suspend // mechanism used for garbage collection. If it is not followed, // a badly timed collection could cause the GC to scan from the // bottom of one stack to the top of another, or to miss scanning // a stack that still contains valid data. The old stack pointer // oldp will be set again before the context switch to guarantee // that it points to exactly the correct stack location so the // successive pop operations will succeed. *oldp = getStackTop(); volatile tobj.m_lock = true; tobj.pushContext( m_ctxt ); fiber_switchContext( oldp, newp ); // NOTE: As above, these operations must be performed in a strict order // to prevent Bad Things from happening. tobj.popContext(); volatile tobj.m_lock = false; tobj.m_curr.tstack = tobj.m_curr.bstack; } // // Switches out of the current stack and into the enclosing stack. // final void switchOut() { Thread tobj = Thread.getThis(); void** oldp = &m_ctxt.tstack; void* newp = tobj.m_curr.within.tstack; // NOTE: The order of operations here is very important. The current // stack top must be stored before m_lock is set, and pushContext // must not be called until after m_lock is set. This process // is intended to prevent a race condition with the suspend // mechanism used for garbage collection. If it is not followed, // a badly timed collection could cause the GC to scan from the // bottom of one stack to the top of another, or to miss scanning // a stack that still contains valid data. The old stack pointer // oldp will be set again before the context switch to guarantee // that it points to exactly the correct stack location so the // successive pop operations will succeed. *oldp = getStackTop(); volatile tobj.m_lock = true; fiber_switchContext( oldp, newp ); // NOTE: As above, these operations must be performed in a strict order // to prevent Bad Things from happening. tobj=Thread.getThis(); volatile tobj.m_lock = false; tobj.m_curr.tstack = tobj.m_curr.bstack; } } extern(C){ void thread_yield(){ Thread.yield(); } void thread_sleep(double period){ Thread.sleep(period); } } |