1 /**
2 * The fiber module provides OS-indepedent lightweight threads aka fibers.
3 *
4 * Copyright: Copyright Sean Kelly 2005 - 2012.
5 * License: Distributed under the
6 * $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost Software License 1.0).
7 * (See accompanying file LICENSE)
8 * Authors: Sean Kelly, Walter Bright, Alex Rønne Petersen, Martin Nowak
9 * Source: $(DRUNTIMESRC core/thread/fiber.d)
10 */
11
12 /* NOTE: This file has been patched from the original DMD distribution to
13 * work with the GDC compiler.
14 */
15 module core.thread.fiber;
16
17 import core.thread.osthread;
18 import core.thread.threadgroup;
19 import core.thread.types;
20 import core.thread.context;
21
22 ///////////////////////////////////////////////////////////////////////////////
23 // Fiber Platform Detection
24 ///////////////////////////////////////////////////////////////////////////////
25
version(GNU)26 version (GNU)
27 {
28 import gcc.builtins;
29 import gcc.config;
30 version (GNU_StackGrowsDown)
31 version = StackGrowsDown;
32 }
33 else
34 {
35 // this should be true for most architectures
36 version = StackGrowsDown;
37 }
38
version(Windows)39 version (Windows)
40 {
41 import core.stdc.stdlib : malloc, free;
42 import core.sys.windows.winbase;
43 import core.sys.windows.winnt;
44 }
45
46 private
47 {
version(D_InlineAsm_X86)48 version (D_InlineAsm_X86)
49 {
50 version (Windows)
51 version = AsmX86_Windows;
52 else version (Posix)
53 version = AsmX86_Posix;
54
55 version = AlignFiberStackTo16Byte;
56 }
version(D_InlineAsm_X86_64)57 else version (D_InlineAsm_X86_64)
58 {
59 version (Windows)
60 {
61 version = AsmX86_64_Windows;
62 version = AlignFiberStackTo16Byte;
63 }
64 else version (Posix)
65 {
66 version = AsmX86_64_Posix;
67 version = AlignFiberStackTo16Byte;
68 }
69 }
version(X86)70 else version (X86)
71 {
72 version = AlignFiberStackTo16Byte;
73
74 version (CET)
75 {
76 // fiber_switchContext does not support shadow stack from
77 // Intel CET. So use ucontext implementation.
78 }
79 else
80 {
81 version = AsmExternal;
82
83 version (MinGW)
84 version = GNU_AsmX86_Windows;
85 else version (OSX)
86 version = AsmX86_Posix;
87 else version (Posix)
88 version = AsmX86_Posix;
89 }
90 }
version(X86_64)91 else version (X86_64)
92 {
93 version = AlignFiberStackTo16Byte;
94
95 version (CET)
96 {
97 // fiber_switchContext does not support shadow stack from
98 // Intel CET. So use ucontext implementation.
99 }
100 else version (D_X32)
101 {
102 // let X32 be handled by ucontext swapcontext
103 }
104 else
105 {
106 version = AsmExternal;
107
108 version (MinGW)
109 version = GNU_AsmX86_64_Windows;
110 else version (OSX)
111 version = AsmX86_64_Posix;
112 else version (Posix)
113 version = AsmX86_64_Posix;
114 }
115 }
version(PPC)116 else version (PPC)
117 {
118 version (OSX)
119 {
120 version = AsmPPC_Darwin;
121 version = AsmExternal;
122 version = AlignFiberStackTo16Byte;
123 }
124 else version (Posix)
125 {
126 version = AsmPPC_Posix;
127 version = AsmExternal;
128 }
129 }
version(PPC64)130 else version (PPC64)
131 {
132 version (OSX)
133 {
134 version = AsmPPC_Darwin;
135 version = AsmExternal;
136 version = AlignFiberStackTo16Byte;
137 }
138 else version (Posix)
139 {
140 version = AlignFiberStackTo16Byte;
141 }
142 }
version(MIPS_O32)143 else version (MIPS_O32)
144 {
145 version (Posix)
146 {
147 version = AsmMIPS_O32_Posix;
148 version = AsmExternal;
149 }
150 }
version(AArch64)151 else version (AArch64)
152 {
153 version (Posix)
154 {
155 version = AsmAArch64_Posix;
156 version = AsmExternal;
157 version = AlignFiberStackTo16Byte;
158 }
159 }
version(ARM)160 else version (ARM)
161 {
162 version (Posix)
163 {
164 version = AsmARM_Posix;
165 version = AsmExternal;
166 }
167 }
version(SPARC)168 else version (SPARC)
169 {
170 // NOTE: The SPARC ABI specifies only doubleword alignment.
171 version = AlignFiberStackTo16Byte;
172 }
version(SPARC64)173 else version (SPARC64)
174 {
175 version = AlignFiberStackTo16Byte;
176 }
177
version(Posix)178 version (Posix)
179 {
180 version (AsmX86_Windows) {} else
181 version (AsmX86_Posix) {} else
182 version (AsmX86_64_Windows) {} else
183 version (AsmX86_64_Posix) {} else
184 version (AsmExternal) {} else
185 {
186 // NOTE: The ucontext implementation requires architecture specific
187 // data definitions to operate so testing for it must be done
188 // by checking for the existence of ucontext_t rather than by
189 // a version identifier. Please note that this is considered
190 // an obsolescent feature according to the POSIX spec, so a
191 // custom solution is still preferred.
192 import core.sys.posix.ucontext;
193 }
194 }
195 }
196
197 ///////////////////////////////////////////////////////////////////////////////
198 // Fiber Entry Point and Context Switch
199 ///////////////////////////////////////////////////////////////////////////////
200
201 private
202 {
203 import core.atomic : atomicStore, cas, MemoryOrder;
204 import core.exception : onOutOfMemoryError;
205 import core.stdc.stdlib : abort;
206
fiber_entryPoint()207 extern (C) void fiber_entryPoint() nothrow
208 {
209 Fiber obj = Fiber.getThis();
210 assert( obj );
211
212 assert( Thread.getThis().m_curr is obj.m_ctxt );
213 atomicStore!(MemoryOrder.raw)(*cast(shared)&Thread.getThis().m_lock, false);
214 obj.m_ctxt.tstack = obj.m_ctxt.bstack;
215 obj.m_state = Fiber.State.EXEC;
216
217 try
218 {
219 obj.run();
220 }
221 catch ( Throwable t )
222 {
223 obj.m_unhandled = t;
224 }
225
226 static if ( __traits( compiles, ucontext_t ) )
227 obj.m_ucur = &obj.m_utxt;
228
229 obj.m_state = Fiber.State.TERM;
230 obj.switchOut();
231 }
232
233 // Look above the definition of 'class Fiber' for some information about the implementation of this routine
version(AsmExternal)234 version (AsmExternal)
235 {
236 extern (C) void fiber_switchContext( void** oldp, void* newp ) nothrow @nogc;
237 version (AArch64)
238 extern (C) void fiber_trampoline() nothrow;
239 }
240 else
fiber_switchContext(void ** oldp,void * newp)241 extern (C) void fiber_switchContext( void** oldp, void* newp ) nothrow @nogc
242 {
243 // NOTE: The data pushed and popped in this routine must match the
244 // default stack created by Fiber.initStack or the initial
245 // switch into a new context will fail.
246
247 version (AsmX86_Windows)
248 {
249 asm pure nothrow @nogc
250 {
251 naked;
252
253 // save current stack state
254 push EBP;
255 mov EBP, ESP;
256 push EDI;
257 push ESI;
258 push EBX;
259 push dword ptr FS:[0];
260 push dword ptr FS:[4];
261 push dword ptr FS:[8];
262 push EAX;
263
264 // store oldp again with more accurate address
265 mov EAX, dword ptr 8[EBP];
266 mov [EAX], ESP;
267 // load newp to begin context switch
268 mov ESP, dword ptr 12[EBP];
269
270 // load saved state from new stack
271 pop EAX;
272 pop dword ptr FS:[8];
273 pop dword ptr FS:[4];
274 pop dword ptr FS:[0];
275 pop EBX;
276 pop ESI;
277 pop EDI;
278 pop EBP;
279
280 // 'return' to complete switch
281 pop ECX;
282 jmp ECX;
283 }
284 }
285 else version (AsmX86_64_Windows)
286 {
287 asm pure nothrow @nogc
288 {
289 naked;
290
291 // save current stack state
292 // NOTE: When changing the layout of registers on the stack,
293 // make sure that the XMM registers are still aligned.
294 // On function entry, the stack is guaranteed to not
295 // be aligned to 16 bytes because of the return address
296 // on the stack.
297 push RBP;
298 mov RBP, RSP;
299 push R12;
300 push R13;
301 push R14;
302 push R15;
303 push RDI;
304 push RSI;
305 // 7 registers = 56 bytes; stack is now aligned to 16 bytes
306 sub RSP, 160;
307 movdqa [RSP + 144], XMM6;
308 movdqa [RSP + 128], XMM7;
309 movdqa [RSP + 112], XMM8;
310 movdqa [RSP + 96], XMM9;
311 movdqa [RSP + 80], XMM10;
312 movdqa [RSP + 64], XMM11;
313 movdqa [RSP + 48], XMM12;
314 movdqa [RSP + 32], XMM13;
315 movdqa [RSP + 16], XMM14;
316 movdqa [RSP], XMM15;
317 push RBX;
318 xor RAX,RAX;
319 push qword ptr GS:[RAX];
320 push qword ptr GS:8[RAX];
321 push qword ptr GS:16[RAX];
322
323 // store oldp
324 mov [RCX], RSP;
325 // load newp to begin context switch
326 mov RSP, RDX;
327
328 // load saved state from new stack
329 pop qword ptr GS:16[RAX];
330 pop qword ptr GS:8[RAX];
331 pop qword ptr GS:[RAX];
332 pop RBX;
333 movdqa XMM15, [RSP];
334 movdqa XMM14, [RSP + 16];
335 movdqa XMM13, [RSP + 32];
336 movdqa XMM12, [RSP + 48];
337 movdqa XMM11, [RSP + 64];
338 movdqa XMM10, [RSP + 80];
339 movdqa XMM9, [RSP + 96];
340 movdqa XMM8, [RSP + 112];
341 movdqa XMM7, [RSP + 128];
342 movdqa XMM6, [RSP + 144];
343 add RSP, 160;
344 pop RSI;
345 pop RDI;
346 pop R15;
347 pop R14;
348 pop R13;
349 pop R12;
350 pop RBP;
351
352 // 'return' to complete switch
353 pop RCX;
354 jmp RCX;
355 }
356 }
357 else version (AsmX86_Posix)
358 {
359 asm pure nothrow @nogc
360 {
361 naked;
362
363 // save current stack state
364 push EBP;
365 mov EBP, ESP;
366 push EDI;
367 push ESI;
368 push EBX;
369 push EAX;
370
371 // store oldp again with more accurate address
372 mov EAX, dword ptr 8[EBP];
373 mov [EAX], ESP;
374 // load newp to begin context switch
375 mov ESP, dword ptr 12[EBP];
376
377 // load saved state from new stack
378 pop EAX;
379 pop EBX;
380 pop ESI;
381 pop EDI;
382 pop EBP;
383
384 // 'return' to complete switch
385 pop ECX;
386 jmp ECX;
387 }
388 }
389 else version (AsmX86_64_Posix)
390 {
391 asm pure nothrow @nogc
392 {
393 naked;
394
395 // save current stack state
396 push RBP;
397 mov RBP, RSP;
398 push RBX;
399 push R12;
400 push R13;
401 push R14;
402 push R15;
403
404 // store oldp
405 mov [RDI], RSP;
406 // load newp to begin context switch
407 mov RSP, RSI;
408
409 // load saved state from new stack
410 pop R15;
411 pop R14;
412 pop R13;
413 pop R12;
414 pop RBX;
415 pop RBP;
416
417 // 'return' to complete switch
418 pop RCX;
419 jmp RCX;
420 }
421 }
422 else static if ( __traits( compiles, ucontext_t ) )
423 {
424 Fiber cfib = Fiber.getThis();
425 void* ucur = cfib.m_ucur;
426
427 *oldp = &ucur;
428 swapcontext( **(cast(ucontext_t***) oldp),
429 *(cast(ucontext_t**) newp) );
430 }
431 else
432 static assert(0, "Not implemented");
433 }
434 }
435
436
437 ///////////////////////////////////////////////////////////////////////////////
438 // Fiber
439 ///////////////////////////////////////////////////////////////////////////////
440 /*
441 * Documentation of Fiber internals:
442 *
443 * The main routines to implement when porting Fibers to new architectures are
444 * fiber_switchContext and initStack. Some version constants have to be defined
445 * for the new platform as well, search for "Fiber Platform Detection and Memory Allocation".
446 *
447 * Fibers are based on a concept called 'Context'. A Context describes the execution
448 * state of a Fiber or main thread which is fully described by the stack, some
449 * registers and a return address at which the Fiber/Thread should continue executing.
450 * Please note that not only each Fiber has a Context, but each thread also has got a
451 * Context which describes the threads stack and state. If you call Fiber fib; fib.call
452 * the first time in a thread you switch from Threads Context into the Fibers Context.
453 * If you call fib.yield in that Fiber you switch out of the Fibers context and back
454 * into the Thread Context. (However, this is not always the case. You can call a Fiber
455 * from within another Fiber, then you switch Contexts between the Fibers and the Thread
456 * Context is not involved)
457 *
458 * In all current implementations the registers and the return address are actually
459 * saved on a Contexts stack.
460 *
461 * The fiber_switchContext routine has got two parameters:
462 * void** a: This is the _location_ where we have to store the current stack pointer,
463 * the stack pointer of the currently executing Context (Fiber or Thread).
464 * void* b: This is the pointer to the stack of the Context which we want to switch into.
465 * Note that we get the same pointer here as the one we stored into the void** a
466 * in a previous call to fiber_switchContext.
467 *
468 * In the simplest case, a fiber_switchContext rountine looks like this:
469 * fiber_switchContext:
470 * push {return Address}
471 * push {registers}
472 * copy {stack pointer} into {location pointed to by a}
473 * //We have now switch to the stack of a different Context!
474 * copy {b} into {stack pointer}
475 * pop {registers}
476 * pop {return Address}
477 * jump to {return Address}
478 *
479 * The GC uses the value returned in parameter a to scan the Fibers stack. It scans from
480 * the stack base to that value. As the GC dislikes false pointers we can actually optimize
481 * this a little: By storing registers which can not contain references to memory managed
482 * by the GC outside of the region marked by the stack base pointer and the stack pointer
483 * saved in fiber_switchContext we can prevent the GC from scanning them.
484 * Such registers are usually floating point registers and the return address. In order to
485 * implement this, we return a modified stack pointer from fiber_switchContext. However,
486 * we have to remember that when we restore the registers from the stack!
487 *
488 * --------------------------- <= Stack Base
489 * | Frame | <= Many other stack frames
490 * | Frame |
491 * |-------------------------| <= The last stack frame. This one is created by fiber_switchContext
492 * | registers with pointers |
493 * | | <= Stack pointer. GC stops scanning here
494 * | return address |
495 * |floating point registers |
496 * --------------------------- <= Real Stack End
497 *
498 * fiber_switchContext:
499 * push {registers with pointers}
500 * copy {stack pointer} into {location pointed to by a}
501 * push {return Address}
502 * push {Floating point registers}
503 * //We have now switch to the stack of a different Context!
504 * copy {b} into {stack pointer}
505 * //We now have to adjust the stack pointer to point to 'Real Stack End' so we can pop
506 * //the FP registers
507 * //+ or - depends on if your stack grows downwards or upwards
508 * {stack pointer} = {stack pointer} +- ({FPRegisters}.sizeof + {return address}.sizeof}
509 * pop {Floating point registers}
510 * pop {return Address}
511 * pop {registers with pointers}
512 * jump to {return Address}
513 *
514 * So the question now is which registers need to be saved? This depends on the specific
515 * architecture ABI of course, but here are some general guidelines:
516 * - If a register is callee-save (if the callee modifies the register it must saved and
517 * restored by the callee) it needs to be saved/restored in switchContext
518 * - If a register is caller-save it needn't be saved/restored. (Calling fiber_switchContext
519 * is a function call and the compiler therefore already must save these registers before
520 * calling fiber_switchContext)
521 * - Argument registers used for passing parameters to functions needn't be saved/restored
522 * - The return register needn't be saved/restored (fiber_switchContext hasn't got a return type)
523 * - All scratch registers needn't be saved/restored
524 * - The link register usually needn't be saved/restored (but sometimes it must be cleared -
525 * see below for details)
526 * - The frame pointer register - if it exists - is usually callee-save
527 * - All current implementations do not save control registers
528 *
529 * What happens on the first switch into a Fiber? We never saved a state for this fiber before,
530 * but the initial state is prepared in the initStack routine. (This routine will also be called
531 * when a Fiber is being resetted). initStack must produce exactly the same stack layout as the
532 * part of fiber_switchContext which saves the registers. Pay special attention to set the stack
533 * pointer correctly if you use the GC optimization mentioned before. the return Address saved in
534 * initStack must be the address of fiber_entrypoint.
535 *
536 * There's now a small but important difference between the first context switch into a fiber and
537 * further context switches. On the first switch, Fiber.call is used and the returnAddress in
538 * fiber_switchContext will point to fiber_entrypoint. The important thing here is that this jump
539 * is a _function call_, we call fiber_entrypoint by jumping before it's function prologue. On later
540 * calls, the user used yield() in a function, and therefore the return address points into a user
541 * function, after the yield call. So here the jump in fiber_switchContext is a _function return_,
542 * not a function call!
543 *
544 * The most important result of this is that on entering a function, i.e. fiber_entrypoint, we
545 * would have to provide a return address / set the link register once fiber_entrypoint
546 * returns. Now fiber_entrypoint does never return and therefore the actual value of the return
547 * address / link register is never read/used and therefore doesn't matter. When fiber_switchContext
548 * performs a _function return_ the value in the link register doesn't matter either.
549 * However, the link register will still be saved to the stack in fiber_entrypoint and some
550 * exception handling / stack unwinding code might read it from this stack location and crash.
551 * The exact solution depends on your architecture, but see the ARM implementation for a way
552 * to deal with this issue.
553 *
554 * The ARM implementation is meant to be used as a kind of documented example implementation.
555 * Look there for a concrete example.
556 *
557 * FIXME: fiber_entrypoint might benefit from a @noreturn attribute, but D doesn't have one.
558 */
559
560 /**
561 * This class provides a cooperative concurrency mechanism integrated with the
562 * threading and garbage collection functionality. Calling a fiber may be
563 * considered a blocking operation that returns when the fiber yields (via
564 * Fiber.yield()). Execution occurs within the context of the calling thread
565 * so synchronization is not necessary to guarantee memory visibility so long
566 * as the same thread calls the fiber each time. Please note that there is no
567 * requirement that a fiber be bound to one specific thread. Rather, fibers
568 * may be freely passed between threads so long as they are not currently
569 * executing. Like threads, a new fiber thread may be created using either
570 * derivation or composition, as in the following example.
571 *
572 * Warning:
573 * Status registers are not saved by the current implementations. This means
574 * floating point exception status bits (overflow, divide by 0), rounding mode
575 * and similar stuff is set per-thread, not per Fiber!
576 *
577 * Warning:
578 * On ARM FPU registers are not saved if druntime was compiled as ARM_SoftFloat.
579 * If such a build is used on a ARM_SoftFP system which actually has got a FPU
580 * and other libraries are using the FPU registers (other code is compiled
581 * as ARM_SoftFP) this can cause problems. Druntime must be compiled as
582 * ARM_SoftFP in this case.
583 *
584 * Authors: Based on a design by Mikola Lysenko.
585 */
586 class Fiber
587 {
588 ///////////////////////////////////////////////////////////////////////////
589 // Initialization
590 ///////////////////////////////////////////////////////////////////////////
591
592 version (Windows)
593 // exception handling walks the stack, invoking DbgHelp.dll which
594 // needs up to 16k of stack space depending on the version of DbgHelp.dll,
595 // the existence of debug symbols and other conditions. Avoid causing
596 // stack overflows by defaulting to a larger stack size
597 enum defaultStackPages = 8;
598 else
599 enum defaultStackPages = 4;
600
601 /**
602 * Initializes a fiber object which is associated with a static
603 * D function.
604 *
605 * Params:
606 * fn = The fiber function.
607 * sz = The stack size for this fiber.
608 * guardPageSize = size of the guard page to trap fiber's stack
609 * overflows. Beware that using this will increase
610 * the number of mmaped regions on platforms using mmap
611 * so an OS-imposed limit may be hit.
612 *
613 * In:
614 * fn must not be null.
615 */
function()616 this( void function() fn, size_t sz = PAGESIZE * defaultStackPages,
617 size_t guardPageSize = PAGESIZE ) nothrow
618 in
619 {
620 assert( fn );
621 }
622 do
623 {
624 allocStack( sz, guardPageSize );
625 reset( fn );
626 }
627
628
629 /**
630 * Initializes a fiber object which is associated with a dynamic
631 * D function.
632 *
633 * Params:
634 * dg = The fiber function.
635 * sz = The stack size for this fiber.
636 * guardPageSize = size of the guard page to trap fiber's stack
637 * overflows. Beware that using this will increase
638 * the number of mmaped regions on platforms using mmap
639 * so an OS-imposed limit may be hit.
640 *
641 * In:
642 * dg must not be null.
643 */
delegate()644 this( void delegate() dg, size_t sz = PAGESIZE * defaultStackPages,
645 size_t guardPageSize = PAGESIZE ) nothrow
646 in
647 {
648 assert( dg );
649 }
650 do
651 {
652 allocStack( sz, guardPageSize );
653 reset( dg );
654 }
655
656
657 /**
658 * Cleans up any remaining resources used by this object.
659 */
~this()660 ~this() nothrow @nogc
661 {
662 // NOTE: A live reference to this object will exist on its associated
663 // stack from the first time its call() method has been called
664 // until its execution completes with State.TERM. Thus, the only
665 // times this dtor should be called are either if the fiber has
666 // terminated (and therefore has no active stack) or if the user
667 // explicitly deletes this object. The latter case is an error
668 // but is not easily tested for, since State.HOLD may imply that
669 // the fiber was just created but has never been run. There is
670 // not a compelling case to create a State.INIT just to offer a
671 // means of ensuring the user isn't violating this object's
672 // contract, so for now this requirement will be enforced by
673 // documentation only.
674 freeStack();
675 }
676
677
678 ///////////////////////////////////////////////////////////////////////////
679 // General Actions
680 ///////////////////////////////////////////////////////////////////////////
681
682
683 /**
684 * Transfers execution to this fiber object. The calling context will be
685 * suspended until the fiber calls Fiber.yield() or until it terminates
686 * via an unhandled exception.
687 *
688 * Params:
689 * rethrow = Rethrow any unhandled exception which may have caused this
690 * fiber to terminate.
691 *
692 * In:
693 * This fiber must be in state HOLD.
694 *
695 * Throws:
696 * Any exception not handled by the joined thread.
697 *
698 * Returns:
699 * Any exception not handled by this fiber if rethrow = false, null
700 * otherwise.
701 */
702 // Not marked with any attributes, even though `nothrow @nogc` works
703 // because it calls arbitrary user code. Most of the implementation
704 // is already `@nogc nothrow`, but in order for `Fiber.call` to
705 // propagate the attributes of the user's function, the Fiber
706 // class needs to be templated.
707 final Throwable call( Rethrow rethrow = Rethrow.yes )
708 {
709 return rethrow ? call!(Rethrow.yes)() : call!(Rethrow.no);
710 }
711
712 /// ditto
call(Rethrow rethrow)713 final Throwable call( Rethrow rethrow )()
714 {
715 callImpl();
716 if ( m_unhandled )
717 {
718 Throwable t = m_unhandled;
719 m_unhandled = null;
720 static if ( rethrow )
721 throw t;
722 else
723 return t;
724 }
725 return null;
726 }
727
callImpl()728 private void callImpl() nothrow @nogc
729 in
730 {
731 assert( m_state == State.HOLD );
732 }
733 do
734 {
735 Fiber cur = getThis();
736
737 static if ( __traits( compiles, ucontext_t ) )
738 m_ucur = cur ? &cur.m_utxt : &Fiber.sm_utxt;
739
740 setThis( this );
741 this.switchIn();
742 setThis( cur );
743
744 static if ( __traits( compiles, ucontext_t ) )
745 m_ucur = null;
746
747 // NOTE: If the fiber has terminated then the stack pointers must be
748 // reset. This ensures that the stack for this fiber is not
749 // scanned if the fiber has terminated. This is necessary to
750 // prevent any references lingering on the stack from delaying
751 // the collection of otherwise dead objects. The most notable
752 // being the current object, which is referenced at the top of
753 // fiber_entryPoint.
754 if ( m_state == State.TERM )
755 {
756 m_ctxt.tstack = m_ctxt.bstack;
757 }
758 }
759
760 /// Flag to control rethrow behavior of $(D $(LREF call))
761 enum Rethrow : bool { no, yes }
762
763 /**
764 * Resets this fiber so that it may be re-used, optionally with a
765 * new function/delegate. This routine should only be called for
766 * fibers that have terminated, as doing otherwise could result in
767 * scope-dependent functionality that is not executed.
768 * Stack-based classes, for example, may not be cleaned up
769 * properly if a fiber is reset before it has terminated.
770 *
771 * In:
772 * This fiber must be in state TERM or HOLD.
773 */
reset()774 final void reset() nothrow @nogc
775 in
776 {
777 assert( m_state == State.TERM || m_state == State.HOLD );
778 }
779 do
780 {
781 m_ctxt.tstack = m_ctxt.bstack;
782 m_state = State.HOLD;
783 initStack();
784 m_unhandled = null;
785 }
786
787 /// ditto
reset(void function ()fn)788 final void reset( void function() fn ) nothrow @nogc
789 {
790 reset();
791 m_call = fn;
792 }
793
794 /// ditto
reset(void delegate ()dg)795 final void reset( void delegate() dg ) nothrow @nogc
796 {
797 reset();
798 m_call = dg;
799 }
800
801 ///////////////////////////////////////////////////////////////////////////
802 // General Properties
803 ///////////////////////////////////////////////////////////////////////////
804
805
806 /// A fiber may occupy one of three states: HOLD, EXEC, and TERM.
807 enum State
808 {
809 /** The HOLD state applies to any fiber that is suspended and ready to
810 be called. */
811 HOLD,
812 /** The EXEC state will be set for any fiber that is currently
813 executing. */
814 EXEC,
815 /** The TERM state is set when a fiber terminates. Once a fiber
816 terminates, it must be reset before it may be called again. */
817 TERM
818 }
819
820
821 /**
822 * Gets the current state of this fiber.
823 *
824 * Returns:
825 * The state of this fiber as an enumerated value.
826 */
state()827 final @property State state() const @safe pure nothrow @nogc
828 {
829 return m_state;
830 }
831
832
833 ///////////////////////////////////////////////////////////////////////////
834 // Actions on Calling Fiber
835 ///////////////////////////////////////////////////////////////////////////
836
837
838 /**
839 * Forces a context switch to occur away from the calling fiber.
840 */
yield()841 static void yield() nothrow @nogc
842 {
843 Fiber cur = getThis();
844 assert( cur, "Fiber.yield() called with no active fiber" );
845 assert( cur.m_state == State.EXEC );
846
847 static if ( __traits( compiles, ucontext_t ) )
848 cur.m_ucur = &cur.m_utxt;
849
850 cur.m_state = State.HOLD;
851 cur.switchOut();
852 cur.m_state = State.EXEC;
853 }
854
855
856 /**
857 * Forces a context switch to occur away from the calling fiber and then
858 * throws obj in the calling fiber.
859 *
860 * Params:
861 * t = The object to throw.
862 *
863 * In:
864 * t must not be null.
865 */
yieldAndThrow(Throwable t)866 static void yieldAndThrow( Throwable t ) nothrow @nogc
867 in
868 {
869 assert( t );
870 }
871 do
872 {
873 Fiber cur = getThis();
874 assert( cur, "Fiber.yield() called with no active fiber" );
875 assert( cur.m_state == State.EXEC );
876
877 static if ( __traits( compiles, ucontext_t ) )
878 cur.m_ucur = &cur.m_utxt;
879
880 cur.m_unhandled = t;
881 cur.m_state = State.HOLD;
882 cur.switchOut();
883 cur.m_state = State.EXEC;
884 }
885
886
887 ///////////////////////////////////////////////////////////////////////////
888 // Fiber Accessors
889 ///////////////////////////////////////////////////////////////////////////
890
891
892 /**
893 * Provides a reference to the calling fiber or null if no fiber is
894 * currently active.
895 *
896 * Returns:
897 * The fiber object representing the calling fiber or null if no fiber
898 * is currently active within this thread. The result of deleting this object is undefined.
899 */
getThis()900 static Fiber getThis() @safe nothrow @nogc
901 {
902 version (GNU) pragma(inline, false);
903 return sm_this;
904 }
905
906
907 ///////////////////////////////////////////////////////////////////////////
908 // Static Initialization
909 ///////////////////////////////////////////////////////////////////////////
910
911
version(Posix)912 version (Posix)
913 {
914 static this()
915 {
916 static if ( __traits( compiles, ucontext_t ) )
917 {
918 int status = getcontext( &sm_utxt );
919 assert( status == 0 );
920 }
921 }
922 }
923
924 private:
925
926 //
927 // Fiber entry point. Invokes the function or delegate passed on
928 // construction (if any).
929 //
run()930 final void run()
931 {
932 m_call();
933 }
934
935 //
936 // Standard fiber data
937 //
938 Callable m_call;
939 bool m_isRunning;
940 Throwable m_unhandled;
941 State m_state;
942
943
944 private:
945 ///////////////////////////////////////////////////////////////////////////
946 // Stack Management
947 ///////////////////////////////////////////////////////////////////////////
948
949
950 //
951 // Allocate a new stack for this fiber.
952 //
allocStack(size_t sz,size_t guardPageSize)953 final void allocStack( size_t sz, size_t guardPageSize ) nothrow
954 in
955 {
956 assert( !m_pmem && !m_ctxt );
957 }
958 do
959 {
960 // adjust alloc size to a multiple of PAGESIZE
961 sz += PAGESIZE - 1;
962 sz -= sz % PAGESIZE;
963
964 // NOTE: This instance of Thread.Context is dynamic so Fiber objects
965 // can be collected by the GC so long as no user level references
966 // to the object exist. If m_ctxt were not dynamic then its
967 // presence in the global context list would be enough to keep
968 // this object alive indefinitely. An alternative to allocating
969 // room for this struct explicitly would be to mash it into the
970 // base of the stack being allocated below. However, doing so
971 // requires too much special logic to be worthwhile.
972 m_ctxt = new StackContext;
973
version(Windows)974 version (Windows)
975 {
976 // reserve memory for stack
977 m_pmem = VirtualAlloc( null,
978 sz + guardPageSize,
979 MEM_RESERVE,
980 PAGE_NOACCESS );
981 if ( !m_pmem )
982 onOutOfMemoryError();
983
984 version (StackGrowsDown)
985 {
986 void* stack = m_pmem + guardPageSize;
987 void* guard = m_pmem;
988 void* pbase = stack + sz;
989 }
990 else
991 {
992 void* stack = m_pmem;
993 void* guard = m_pmem + sz;
994 void* pbase = stack;
995 }
996
997 // allocate reserved stack segment
998 stack = VirtualAlloc( stack,
999 sz,
1000 MEM_COMMIT,
1001 PAGE_READWRITE );
1002 if ( !stack )
1003 onOutOfMemoryError();
1004
1005 if (guardPageSize)
1006 {
1007 // allocate reserved guard page
1008 guard = VirtualAlloc( guard,
1009 guardPageSize,
1010 MEM_COMMIT,
1011 PAGE_READWRITE | PAGE_GUARD );
1012 if ( !guard )
1013 onOutOfMemoryError();
1014 }
1015
1016 m_ctxt.bstack = pbase;
1017 m_ctxt.tstack = pbase;
1018 m_size = sz;
1019 }
1020 else
1021 {
1022 version (Posix) import core.sys.posix.sys.mman; // mmap, MAP_ANON
1023
1024 static if ( __traits( compiles, ucontext_t ) )
1025 {
1026 // Stack size must be at least the minimum allowable by the OS.
1027 if (sz < MINSIGSTKSZ)
1028 sz = MINSIGSTKSZ;
1029 }
1030
1031 static if ( __traits( compiles, mmap ) )
1032 {
1033 // Allocate more for the memory guard
1034 sz += guardPageSize;
1035
1036 m_pmem = mmap( null,
1037 sz,
1038 PROT_READ | PROT_WRITE,
1039 MAP_PRIVATE | MAP_ANON,
1040 -1,
1041 0 );
1042 if ( m_pmem == MAP_FAILED )
1043 m_pmem = null;
1044 }
1045 else static if ( __traits( compiles, valloc ) )
1046 {
1047 m_pmem = valloc( sz );
1048 }
1049 else static if ( __traits( compiles, malloc ) )
1050 {
1051 m_pmem = malloc( sz );
1052 }
1053 else
1054 {
1055 m_pmem = null;
1056 }
1057
1058 if ( !m_pmem )
1059 onOutOfMemoryError();
1060
version(StackGrowsDown)1061 version (StackGrowsDown)
1062 {
1063 m_ctxt.bstack = m_pmem + sz;
1064 m_ctxt.tstack = m_pmem + sz;
1065 void* guard = m_pmem;
1066 }
1067 else
1068 {
1069 m_ctxt.bstack = m_pmem;
1070 m_ctxt.tstack = m_pmem;
1071 void* guard = m_pmem + sz - guardPageSize;
1072 }
1073 m_size = sz;
1074
1075 static if ( __traits( compiles, mmap ) )
1076 {
1077 if (guardPageSize)
1078 {
1079 // protect end of stack
1080 if ( mprotect(guard, guardPageSize, PROT_NONE) == -1 )
1081 abort();
1082 }
1083 }
1084 else
1085 {
1086 // Supported only for mmap allocated memory - results are
1087 // undefined if applied to memory not obtained by mmap
1088 }
1089 }
1090
1091 Thread.add( m_ctxt );
1092 }
1093
1094
1095 //
1096 // Free this fiber's stack.
1097 //
freeStack()1098 final void freeStack() nothrow @nogc
1099 in
1100 {
1101 assert( m_pmem && m_ctxt );
1102 }
1103 do
1104 {
1105 // NOTE: m_ctxt is guaranteed to be alive because it is held in the
1106 // global context list.
1107 Thread.slock.lock_nothrow();
1108 scope(exit) Thread.slock.unlock_nothrow();
1109 Thread.remove( m_ctxt );
1110
version(Windows)1111 version (Windows)
1112 {
1113 VirtualFree( m_pmem, 0, MEM_RELEASE );
1114 }
1115 else
1116 {
1117 import core.sys.posix.sys.mman; // munmap
1118
1119 static if ( __traits( compiles, mmap ) )
1120 {
1121 munmap( m_pmem, m_size );
1122 }
1123 else static if ( __traits( compiles, valloc ) )
1124 {
1125 free( m_pmem );
1126 }
1127 else static if ( __traits( compiles, malloc ) )
1128 {
1129 free( m_pmem );
1130 }
1131 }
1132 m_pmem = null;
1133 m_ctxt = null;
1134 }
1135
1136
1137 //
1138 // Initialize the allocated stack.
1139 // Look above the definition of 'class Fiber' for some information about the implementation of this routine
1140 //
initStack()1141 final void initStack() nothrow @nogc
1142 in
1143 {
1144 assert( m_ctxt.tstack && m_ctxt.tstack == m_ctxt.bstack );
1145 assert( cast(size_t) m_ctxt.bstack % (void*).sizeof == 0 );
1146 }
1147 do
1148 {
1149 void* pstack = m_ctxt.tstack;
1150 scope( exit ) m_ctxt.tstack = pstack;
1151
push(size_t val)1152 void push( size_t val ) nothrow
1153 {
1154 version (StackGrowsDown)
1155 {
1156 pstack -= size_t.sizeof;
1157 *(cast(size_t*) pstack) = val;
1158 }
1159 else
1160 {
1161 pstack += size_t.sizeof;
1162 *(cast(size_t*) pstack) = val;
1163 }
1164 }
1165
1166 // NOTE: On OS X the stack must be 16-byte aligned according
1167 // to the IA-32 call spec. For x86_64 the stack also needs to
1168 // be aligned to 16-byte according to SysV AMD64 ABI.
version(AlignFiberStackTo16Byte)1169 version (AlignFiberStackTo16Byte)
1170 {
1171 version (StackGrowsDown)
1172 {
1173 pstack = cast(void*)(cast(size_t)(pstack) - (cast(size_t)(pstack) & 0x0F));
1174 }
1175 else
1176 {
1177 pstack = cast(void*)(cast(size_t)(pstack) + (cast(size_t)(pstack) & 0x0F));
1178 }
1179 }
1180
version(AsmX86_Windows)1181 version (AsmX86_Windows)
1182 {
1183 version (StackGrowsDown) {} else static assert( false );
1184
1185 // On Windows Server 2008 and 2008 R2, an exploit mitigation
1186 // technique known as SEHOP is activated by default. To avoid
1187 // hijacking of the exception handler chain, the presence of a
1188 // Windows-internal handler (ntdll.dll!FinalExceptionHandler) at
1189 // its end is tested by RaiseException. If it is not present, all
1190 // handlers are disregarded, and the program is thus aborted
1191 // (see http://blogs.technet.com/b/srd/archive/2009/02/02/
1192 // preventing-the-exploitation-of-seh-overwrites-with-sehop.aspx).
1193 // For new threads, this handler is installed by Windows immediately
1194 // after creation. To make exception handling work in fibers, we
1195 // have to insert it for our new stacks manually as well.
1196 //
1197 // To do this, we first determine the handler by traversing the SEH
1198 // chain of the current thread until its end, and then construct a
1199 // registration block for the last handler on the newly created
1200 // thread. We then continue to push all the initial register values
1201 // for the first context switch as for the other implementations.
1202 //
1203 // Note that this handler is never actually invoked, as we install
1204 // our own one on top of it in the fiber entry point function.
1205 // Thus, it should not have any effects on OSes not implementing
1206 // exception chain verification.
1207
1208 alias fp_t = void function(); // Actual signature not relevant.
1209 static struct EXCEPTION_REGISTRATION
1210 {
1211 EXCEPTION_REGISTRATION* next; // sehChainEnd if last one.
1212 fp_t handler;
1213 }
1214 enum sehChainEnd = cast(EXCEPTION_REGISTRATION*) 0xFFFFFFFF;
1215
1216 __gshared static fp_t finalHandler = null;
1217 if ( finalHandler is null )
1218 {
1219 static EXCEPTION_REGISTRATION* fs0() nothrow
1220 {
1221 asm pure nothrow @nogc
1222 {
1223 naked;
1224 mov EAX, FS:[0];
1225 ret;
1226 }
1227 }
1228 auto reg = fs0();
1229 while ( reg.next != sehChainEnd ) reg = reg.next;
1230
1231 // Benign races are okay here, just to avoid re-lookup on every
1232 // fiber creation.
1233 finalHandler = reg.handler;
1234 }
1235
1236 // When linking with /safeseh (supported by LDC, but not DMD)
1237 // the exception chain must not extend to the very top
1238 // of the stack, otherwise the exception chain is also considered
1239 // invalid. Reserving additional 4 bytes at the top of the stack will
1240 // keep the EXCEPTION_REGISTRATION below that limit
1241 size_t reserve = EXCEPTION_REGISTRATION.sizeof + 4;
1242 pstack -= reserve;
1243 *(cast(EXCEPTION_REGISTRATION*)pstack) =
1244 EXCEPTION_REGISTRATION( sehChainEnd, finalHandler );
1245 auto pChainEnd = pstack;
1246
1247 push( cast(size_t) &fiber_entryPoint ); // EIP
1248 push( cast(size_t) m_ctxt.bstack - reserve ); // EBP
1249 push( 0x00000000 ); // EDI
1250 push( 0x00000000 ); // ESI
1251 push( 0x00000000 ); // EBX
1252 push( cast(size_t) pChainEnd ); // FS:[0]
1253 push( cast(size_t) m_ctxt.bstack ); // FS:[4]
1254 push( cast(size_t) m_ctxt.bstack - m_size ); // FS:[8]
1255 push( 0x00000000 ); // EAX
1256 }
version(AsmX86_64_Windows)1257 else version (AsmX86_64_Windows)
1258 {
1259 // Using this trampoline instead of the raw fiber_entryPoint
1260 // ensures that during context switches, source and destination
1261 // stacks have the same alignment. Otherwise, the stack would need
1262 // to be shifted by 8 bytes for the first call, as fiber_entryPoint
1263 // is an actual function expecting a stack which is not aligned
1264 // to 16 bytes.
1265 static void trampoline()
1266 {
1267 asm pure nothrow @nogc
1268 {
1269 naked;
1270 sub RSP, 32; // Shadow space (Win64 calling convention)
1271 call fiber_entryPoint;
1272 xor RCX, RCX; // This should never be reached, as
1273 jmp RCX; // fiber_entryPoint must never return.
1274 }
1275 }
1276
1277 push( cast(size_t) &trampoline ); // RIP
1278 push( 0x00000000_00000000 ); // RBP
1279 push( 0x00000000_00000000 ); // R12
1280 push( 0x00000000_00000000 ); // R13
1281 push( 0x00000000_00000000 ); // R14
1282 push( 0x00000000_00000000 ); // R15
1283 push( 0x00000000_00000000 ); // RDI
1284 push( 0x00000000_00000000 ); // RSI
1285 push( 0x00000000_00000000 ); // XMM6 (high)
1286 push( 0x00000000_00000000 ); // XMM6 (low)
1287 push( 0x00000000_00000000 ); // XMM7 (high)
1288 push( 0x00000000_00000000 ); // XMM7 (low)
1289 push( 0x00000000_00000000 ); // XMM8 (high)
1290 push( 0x00000000_00000000 ); // XMM8 (low)
1291 push( 0x00000000_00000000 ); // XMM9 (high)
1292 push( 0x00000000_00000000 ); // XMM9 (low)
1293 push( 0x00000000_00000000 ); // XMM10 (high)
1294 push( 0x00000000_00000000 ); // XMM10 (low)
1295 push( 0x00000000_00000000 ); // XMM11 (high)
1296 push( 0x00000000_00000000 ); // XMM11 (low)
1297 push( 0x00000000_00000000 ); // XMM12 (high)
1298 push( 0x00000000_00000000 ); // XMM12 (low)
1299 push( 0x00000000_00000000 ); // XMM13 (high)
1300 push( 0x00000000_00000000 ); // XMM13 (low)
1301 push( 0x00000000_00000000 ); // XMM14 (high)
1302 push( 0x00000000_00000000 ); // XMM14 (low)
1303 push( 0x00000000_00000000 ); // XMM15 (high)
1304 push( 0x00000000_00000000 ); // XMM15 (low)
1305 push( 0x00000000_00000000 ); // RBX
1306 push( 0xFFFFFFFF_FFFFFFFF ); // GS:[0]
1307 version (StackGrowsDown)
1308 {
1309 push( cast(size_t) m_ctxt.bstack ); // GS:[8]
1310 push( cast(size_t) m_ctxt.bstack - m_size ); // GS:[16]
1311 }
1312 else
1313 {
1314 push( cast(size_t) m_ctxt.bstack ); // GS:[8]
1315 push( cast(size_t) m_ctxt.bstack + m_size ); // GS:[16]
1316 }
1317 }
version(AsmX86_Posix)1318 else version (AsmX86_Posix)
1319 {
1320 push( 0x00000000 ); // Return address of fiber_entryPoint call
1321 push( cast(size_t) &fiber_entryPoint ); // EIP
1322 push( cast(size_t) m_ctxt.bstack ); // EBP
1323 push( 0x00000000 ); // EDI
1324 push( 0x00000000 ); // ESI
1325 push( 0x00000000 ); // EBX
1326 push( 0x00000000 ); // EAX
1327 }
version(AsmX86_64_Posix)1328 else version (AsmX86_64_Posix)
1329 {
1330 push( 0x00000000_00000000 ); // Return address of fiber_entryPoint call
1331 push( cast(size_t) &fiber_entryPoint ); // RIP
1332 push( cast(size_t) m_ctxt.bstack ); // RBP
1333 push( 0x00000000_00000000 ); // RBX
1334 push( 0x00000000_00000000 ); // R12
1335 push( 0x00000000_00000000 ); // R13
1336 push( 0x00000000_00000000 ); // R14
1337 push( 0x00000000_00000000 ); // R15
1338 }
version(AsmPPC_Posix)1339 else version (AsmPPC_Posix)
1340 {
1341 version (StackGrowsDown)
1342 {
1343 pstack -= int.sizeof * 5;
1344 }
1345 else
1346 {
1347 pstack += int.sizeof * 5;
1348 }
1349
1350 push( cast(size_t) &fiber_entryPoint ); // link register
1351 push( 0x00000000 ); // control register
1352 push( 0x00000000 ); // old stack pointer
1353
1354 // GPR values
1355 version (StackGrowsDown)
1356 {
1357 pstack -= int.sizeof * 20;
1358 }
1359 else
1360 {
1361 pstack += int.sizeof * 20;
1362 }
1363
1364 assert( (cast(size_t) pstack & 0x0f) == 0 );
1365 }
version(AsmPPC_Darwin)1366 else version (AsmPPC_Darwin)
1367 {
1368 version (StackGrowsDown) {}
1369 else static assert(false, "PowerPC Darwin only supports decrementing stacks");
1370
1371 uint wsize = size_t.sizeof;
1372
1373 // linkage + regs + FPRs + VRs
1374 uint space = 8 * wsize + 20 * wsize + 18 * 8 + 12 * 16;
1375 (cast(ubyte*)pstack - space)[0 .. space] = 0;
1376
1377 pstack -= wsize * 6;
1378 *cast(size_t*)pstack = cast(size_t) &fiber_entryPoint; // LR
1379 pstack -= wsize * 22;
1380
1381 // On Darwin PPC64 pthread self is in R13 (which is reserved).
1382 // At present, it is not safe to migrate fibers between threads, but if that
1383 // changes, then updating the value of R13 will also need to be handled.
1384 version (PPC64)
1385 *cast(size_t*)(pstack + wsize) = cast(size_t) Thread.getThis().m_addr;
1386 assert( (cast(size_t) pstack & 0x0f) == 0 );
1387 }
version(AsmMIPS_O32_Posix)1388 else version (AsmMIPS_O32_Posix)
1389 {
1390 version (StackGrowsDown) {}
1391 else static assert(0);
1392
1393 /* We keep the FP registers and the return address below
1394 * the stack pointer, so they don't get scanned by the
1395 * GC. The last frame before swapping the stack pointer is
1396 * organized like the following.
1397 *
1398 * |-----------|<= frame pointer
1399 * | $gp |
1400 * | $s0-8 |
1401 * |-----------|<= stack pointer
1402 * | $ra |
1403 * | align(8) |
1404 * | $f20-30 |
1405 * |-----------|
1406 *
1407 */
1408 enum SZ_GP = 10 * size_t.sizeof; // $gp + $s0-8
1409 enum SZ_RA = size_t.sizeof; // $ra
1410 version (MIPS_HardFloat)
1411 {
1412 enum SZ_FP = 6 * 8; // $f20-30
1413 enum ALIGN = -(SZ_FP + SZ_RA) & (8 - 1);
1414 }
1415 else
1416 {
1417 enum SZ_FP = 0;
1418 enum ALIGN = 0;
1419 }
1420
1421 enum BELOW = SZ_FP + ALIGN + SZ_RA;
1422 enum ABOVE = SZ_GP;
1423 enum SZ = BELOW + ABOVE;
1424
1425 (cast(ubyte*)pstack - SZ)[0 .. SZ] = 0;
1426 pstack -= ABOVE;
1427 *cast(size_t*)(pstack - SZ_RA) = cast(size_t)&fiber_entryPoint;
1428 }
version(AsmAArch64_Posix)1429 else version (AsmAArch64_Posix)
1430 {
1431 // Like others, FP registers and return address (lr) are kept
1432 // below the saved stack top (tstack) to hide from GC scanning.
1433 // fiber_switchContext expects newp sp to look like this:
1434 // 19: x19
1435 // ...
1436 // 9: x29 (fp) <-- newp tstack
1437 // 8: x30 (lr) [&fiber_entryPoint]
1438 // 7: d8
1439 // ...
1440 // 0: d15
1441
1442 version (StackGrowsDown) {}
1443 else
1444 static assert(false, "Only full descending stacks supported on AArch64");
1445
1446 // Only need to set return address (lr). Everything else is fine
1447 // zero initialized.
1448 pstack -= size_t.sizeof * 11; // skip past x19-x29
1449 push(cast(size_t) &fiber_trampoline); // see threadasm.S for docs
1450 pstack += size_t.sizeof; // adjust sp (newp) above lr
1451 }
version(AsmARM_Posix)1452 else version (AsmARM_Posix)
1453 {
1454 /* We keep the FP registers and the return address below
1455 * the stack pointer, so they don't get scanned by the
1456 * GC. The last frame before swapping the stack pointer is
1457 * organized like the following.
1458 *
1459 * | |-----------|<= 'frame starts here'
1460 * | | fp | (the actual frame pointer, r11 isn't
1461 * | | r10-r4 | updated and still points to the previous frame)
1462 * | |-----------|<= stack pointer
1463 * | | lr |
1464 * | | 4byte pad |
1465 * | | d15-d8 |(if FP supported)
1466 * | |-----------|
1467 * Y
1468 * stack grows down: The pointer value here is smaller than some lines above
1469 */
1470 // frame pointer can be zero, r10-r4 also zero initialized
1471 version (StackGrowsDown)
1472 pstack -= int.sizeof * 8;
1473 else
1474 static assert(false, "Only full descending stacks supported on ARM");
1475
1476 // link register
1477 push( cast(size_t) &fiber_entryPoint );
1478 /*
1479 * We do not push padding and d15-d8 as those are zero initialized anyway
1480 * Position the stack pointer above the lr register
1481 */
1482 pstack += int.sizeof * 1;
1483 }
version(GNU_AsmX86_Windows)1484 else version (GNU_AsmX86_Windows)
1485 {
1486 version (StackGrowsDown) {} else static assert( false );
1487
1488 // Currently, MinGW doesn't utilize SEH exceptions.
1489 // See DMD AsmX86_Windows If this code ever becomes fails and SEH is used.
1490
1491 push( 0x00000000 ); // Return address of fiber_entryPoint call
1492 push( cast(size_t) &fiber_entryPoint ); // EIP
1493 push( 0x00000000 ); // EBP
1494 push( 0x00000000 ); // EDI
1495 push( 0x00000000 ); // ESI
1496 push( 0x00000000 ); // EBX
1497 push( 0xFFFFFFFF ); // FS:[0] - Current SEH frame
1498 push( cast(size_t) m_ctxt.bstack ); // FS:[4] - Top of stack
1499 push( cast(size_t) m_ctxt.bstack - m_size ); // FS:[8] - Bottom of stack
1500 push( 0x00000000 ); // EAX
1501 }
version(GNU_AsmX86_64_Windows)1502 else version (GNU_AsmX86_64_Windows)
1503 {
1504 push( 0x00000000_00000000 ); // Return address of fiber_entryPoint call
1505 push( cast(size_t) &fiber_entryPoint ); // RIP
1506 push( 0x00000000_00000000 ); // RBP
1507 push( 0x00000000_00000000 ); // RBX
1508 push( 0x00000000_00000000 ); // R12
1509 push( 0x00000000_00000000 ); // R13
1510 push( 0x00000000_00000000 ); // R14
1511 push( 0x00000000_00000000 ); // R15
1512 push( 0xFFFFFFFF_FFFFFFFF ); // GS:[0] - Current SEH frame
1513 version (StackGrowsDown)
1514 {
1515 push( cast(size_t) m_ctxt.bstack ); // GS:[8] - Top of stack
1516 push( cast(size_t) m_ctxt.bstack - m_size ); // GS:[16] - Bottom of stack
1517 }
1518 else
1519 {
1520 push( cast(size_t) m_ctxt.bstack ); // GS:[8] - Top of stack
1521 push( cast(size_t) m_ctxt.bstack + m_size ); // GS:[16] - Bottom of stack
1522 }
1523 }
1524 else static if ( __traits( compiles, ucontext_t ) )
1525 {
1526 getcontext( &m_utxt );
1527 m_utxt.uc_stack.ss_sp = m_pmem;
1528 m_utxt.uc_stack.ss_size = m_size;
1529 makecontext( &m_utxt, &fiber_entryPoint, 0 );
1530 // NOTE: If ucontext is being used then the top of the stack will
1531 // be a pointer to the ucontext_t struct for that fiber.
1532 push( cast(size_t) &m_utxt );
1533 }
1534 else
1535 static assert(0, "Not implemented");
1536 }
1537
1538
1539 StackContext* m_ctxt;
1540 size_t m_size;
1541 void* m_pmem;
1542
1543 static if ( __traits( compiles, ucontext_t ) )
1544 {
1545 // NOTE: The static ucontext instance is used to represent the context
1546 // of the executing thread.
1547 static ucontext_t sm_utxt = void;
1548 ucontext_t m_utxt = void;
1549 ucontext_t* m_ucur = null;
1550 }
1551 else static if (GNU_Enable_CET)
1552 {
1553 // When libphobos was built with --enable-cet, these fields need to
1554 // always be present in the Fiber class layout.
1555 import core.sys.posix.ucontext;
1556 static ucontext_t sm_utxt = void;
1557 ucontext_t m_utxt = void;
1558 ucontext_t* m_ucur = null;
1559 }
1560
1561
1562 private:
1563 ///////////////////////////////////////////////////////////////////////////
1564 // Storage of Active Fiber
1565 ///////////////////////////////////////////////////////////////////////////
1566
1567
1568 //
1569 // Sets a thread-local reference to the current fiber object.
1570 //
setThis(Fiber f)1571 static void setThis( Fiber f ) nothrow @nogc
1572 {
1573 sm_this = f;
1574 }
1575
1576 static Fiber sm_this;
1577
1578
1579 private:
1580 ///////////////////////////////////////////////////////////////////////////
1581 // Context Switching
1582 ///////////////////////////////////////////////////////////////////////////
1583
1584
1585 //
1586 // Switches into the stack held by this fiber.
1587 //
switchIn()1588 final void switchIn() nothrow @nogc
1589 {
1590 Thread tobj = Thread.getThis();
1591 void** oldp = &tobj.m_curr.tstack;
1592 void* newp = m_ctxt.tstack;
1593
1594 // NOTE: The order of operations here is very important. The current
1595 // stack top must be stored before m_lock is set, and pushContext
1596 // must not be called until after m_lock is set. This process
1597 // is intended to prevent a race condition with the suspend
1598 // mechanism used for garbage collection. If it is not followed,
1599 // a badly timed collection could cause the GC to scan from the
1600 // bottom of one stack to the top of another, or to miss scanning
1601 // a stack that still contains valid data. The old stack pointer
1602 // oldp will be set again before the context switch to guarantee
1603 // that it points to exactly the correct stack location so the
1604 // successive pop operations will succeed.
1605 *oldp = getStackTop();
1606 atomicStore!(MemoryOrder.raw)(*cast(shared)&tobj.m_lock, true);
1607 tobj.pushContext( m_ctxt );
1608
1609 fiber_switchContext( oldp, newp );
1610
1611 // NOTE: As above, these operations must be performed in a strict order
1612 // to prevent Bad Things from happening.
1613 tobj.popContext();
1614 atomicStore!(MemoryOrder.raw)(*cast(shared)&tobj.m_lock, false);
1615 tobj.m_curr.tstack = tobj.m_curr.bstack;
1616 }
1617
1618
1619 //
1620 // Switches out of the current stack and into the enclosing stack.
1621 //
switchOut()1622 final void switchOut() nothrow @nogc
1623 {
1624 Thread tobj = Thread.getThis();
1625 void** oldp = &m_ctxt.tstack;
1626 void* newp = tobj.m_curr.within.tstack;
1627
1628 // NOTE: The order of operations here is very important. The current
1629 // stack top must be stored before m_lock is set, and pushContext
1630 // must not be called until after m_lock is set. This process
1631 // is intended to prevent a race condition with the suspend
1632 // mechanism used for garbage collection. If it is not followed,
1633 // a badly timed collection could cause the GC to scan from the
1634 // bottom of one stack to the top of another, or to miss scanning
1635 // a stack that still contains valid data. The old stack pointer
1636 // oldp will be set again before the context switch to guarantee
1637 // that it points to exactly the correct stack location so the
1638 // successive pop operations will succeed.
1639 *oldp = getStackTop();
1640 atomicStore!(MemoryOrder.raw)(*cast(shared)&tobj.m_lock, true);
1641
1642 fiber_switchContext( oldp, newp );
1643
1644 // NOTE: As above, these operations must be performed in a strict order
1645 // to prevent Bad Things from happening.
1646 // NOTE: If use of this fiber is multiplexed across threads, the thread
1647 // executing here may be different from the one above, so get the
1648 // current thread handle before unlocking, etc.
1649 tobj = Thread.getThis();
1650 atomicStore!(MemoryOrder.raw)(*cast(shared)&tobj.m_lock, false);
1651 tobj.m_curr.tstack = tobj.m_curr.bstack;
1652 }
1653 }
1654
1655 ///
1656 unittest {
1657 int counter;
1658
1659 class DerivedFiber : Fiber
1660 {
this()1661 this()
1662 {
1663 super( &run );
1664 }
1665
1666 private :
run()1667 void run()
1668 {
1669 counter += 2;
1670 }
1671 }
1672
fiberFunc()1673 void fiberFunc()
1674 {
1675 counter += 4;
1676 Fiber.yield();
1677 counter += 8;
1678 }
1679
1680 // create instances of each type
1681 Fiber derived = new DerivedFiber();
1682 Fiber composed = new Fiber( &fiberFunc );
1683
1684 assert( counter == 0 );
1685
1686 derived.call();
1687 assert( counter == 2, "Derived fiber increment." );
1688
1689 composed.call();
1690 assert( counter == 6, "First composed fiber increment." );
1691
1692 counter += 16;
1693 assert( counter == 22, "Calling context increment." );
1694
1695 composed.call();
1696 assert( counter == 30, "Second composed fiber increment." );
1697
1698 // since each fiber has run to completion, each should have state TERM
1699 assert( derived.state == Fiber.State.TERM );
1700 assert( composed.state == Fiber.State.TERM );
1701 }
1702
version(unittest)1703 version (unittest)
1704 {
1705 class TestFiber : Fiber
1706 {
1707 this()
1708 {
1709 super(&run);
1710 }
1711
1712 void run()
1713 {
1714 foreach (i; 0 .. 1000)
1715 {
1716 sum += i;
1717 Fiber.yield();
1718 }
1719 }
1720
1721 enum expSum = 1000 * 999 / 2;
1722 size_t sum;
1723 }
1724
1725 void runTen()
1726 {
1727 TestFiber[10] fibs;
1728 foreach (ref fib; fibs)
1729 fib = new TestFiber();
1730
1731 bool cont;
1732 do {
1733 cont = false;
1734 foreach (fib; fibs) {
1735 if (fib.state == Fiber.State.HOLD)
1736 {
1737 fib.call();
1738 cont |= fib.state != Fiber.State.TERM;
1739 }
1740 }
1741 } while (cont);
1742
1743 foreach (fib; fibs)
1744 {
1745 assert(fib.sum == TestFiber.expSum);
1746 }
1747 }
1748 }
1749
1750
1751 // Single thread running separate fibers
1752 unittest
1753 {
1754 runTen();
1755 }
1756
1757
1758 // Multiple threads running separate fibers
1759 unittest
1760 {
1761 auto group = new ThreadGroup();
1762 foreach (_; 0 .. 4)
1763 {
1764 group.create(&runTen);
1765 }
1766 group.joinAll();
1767 }
1768
1769
1770 // Multiple threads running shared fibers
1771 version (PPC) version = UnsafeFiberMigration;
1772 version (PPC64) version = UnsafeFiberMigration;
version(OSX)1773 version (OSX)
1774 {
1775 version (X86) version = UnsafeFiberMigration;
1776 version (X86_64) version = UnsafeFiberMigration;
1777 }
1778
version(UnsafeFiberMigration)1779 version (UnsafeFiberMigration)
1780 {
1781 // XBUG: core.thread fibers are supposed to be safe to migrate across
1782 // threads, however, there is a problem: GCC always assumes that the
1783 // address of thread-local variables don't change while on a given stack.
1784 // In consequence, migrating fibers between threads currently is an unsafe
1785 // thing to do, and will break on some targets (possibly PR26461).
1786 }
1787 else
1788 {
1789 version = FiberMigrationUnittest;
1790 }
1791
version(FiberMigrationUnittest)1792 version (FiberMigrationUnittest)
1793 unittest
1794 {
1795 shared bool[10] locks;
1796 TestFiber[10] fibs;
1797
1798 void runShared()
1799 {
1800 bool cont;
1801 do {
1802 cont = false;
1803 foreach (idx; 0 .. 10)
1804 {
1805 if (cas(&locks[idx], false, true))
1806 {
1807 if (fibs[idx].state == Fiber.State.HOLD)
1808 {
1809 fibs[idx].call();
1810 cont |= fibs[idx].state != Fiber.State.TERM;
1811 }
1812 locks[idx] = false;
1813 }
1814 else
1815 {
1816 cont = true;
1817 }
1818 }
1819 } while (cont);
1820 }
1821
1822 foreach (ref fib; fibs)
1823 {
1824 fib = new TestFiber();
1825 }
1826
1827 auto group = new ThreadGroup();
1828 foreach (_; 0 .. 4)
1829 {
1830 group.create(&runShared);
1831 }
1832 group.joinAll();
1833
1834 foreach (fib; fibs)
1835 {
1836 assert(fib.sum == TestFiber.expSum);
1837 }
1838 }
1839
1840
1841 // Test exception handling inside fibers.
1842 unittest
1843 {
1844 enum MSG = "Test message.";
1845 string caughtMsg;
1846 (new Fiber({
1847 try
1848 {
1849 throw new Exception(MSG);
1850 }
1851 catch (Exception e)
1852 {
1853 caughtMsg = e.msg;
1854 }
1855 })).call();
1856 assert(caughtMsg == MSG);
1857 }
1858
1859
1860 unittest
1861 {
1862 int x = 0;
1863
1864 (new Fiber({
1865 x++;
1866 })).call();
1867 assert( x == 1 );
1868 }
1869
1870 nothrow unittest
1871 {
1872 new Fiber({}).call!(Fiber.Rethrow.no)();
1873 }
1874
1875 unittest
1876 {
1877 new Fiber({}).call(Fiber.Rethrow.yes);
1878 new Fiber({}).call(Fiber.Rethrow.no);
1879 }
1880
1881 unittest
1882 {
1883 enum MSG = "Test message.";
1884
1885 try
1886 {
1887 (new Fiber({
1888 throw new Exception( MSG );
1889 })).call();
1890 assert( false, "Expected rethrown exception." );
1891 }
catch(Throwable t)1892 catch ( Throwable t )
1893 {
1894 assert( t.msg == MSG );
1895 }
1896 }
1897
1898 // Test exception chaining when switching contexts in finally blocks.
1899 unittest
1900 {
throwAndYield(string msg)1901 static void throwAndYield(string msg) {
1902 try {
1903 throw new Exception(msg);
1904 } finally {
1905 Fiber.yield();
1906 }
1907 }
1908
fiber(string name)1909 static void fiber(string name) {
1910 try {
1911 try {
1912 throwAndYield(name ~ ".1");
1913 } finally {
1914 throwAndYield(name ~ ".2");
1915 }
1916 } catch (Exception e) {
1917 assert(e.msg == name ~ ".1");
1918 assert(e.next);
1919 assert(e.next.msg == name ~ ".2");
1920 assert(!e.next.next);
1921 }
1922 }
1923
1924 auto first = new Fiber(() => fiber("first"));
1925 auto second = new Fiber(() => fiber("second"));
1926 first.call();
1927 second.call();
1928 first.call();
1929 second.call();
1930 first.call();
1931 second.call();
1932 assert(first.state == Fiber.State.TERM);
1933 assert(second.state == Fiber.State.TERM);
1934 }
1935
1936 // Test Fiber resetting
1937 unittest
1938 {
1939 static string method;
1940
foo()1941 static void foo()
1942 {
1943 method = "foo";
1944 }
1945
bar()1946 void bar()
1947 {
1948 method = "bar";
1949 }
1950
expect(Fiber fib,string s)1951 static void expect(Fiber fib, string s)
1952 {
1953 assert(fib.state == Fiber.State.HOLD);
1954 fib.call();
1955 assert(fib.state == Fiber.State.TERM);
1956 assert(method == s); method = null;
1957 }
1958 auto fib = new Fiber(&foo);
1959 expect(fib, "foo");
1960
1961 fib.reset();
1962 expect(fib, "foo");
1963
1964 fib.reset(&foo);
1965 expect(fib, "foo");
1966
1967 fib.reset(&bar);
1968 expect(fib, "bar");
1969
1970 fib.reset(function void(){method = "function";});
1971 expect(fib, "function");
1972
1973 fib.reset(delegate void(){method = "delegate";});
1974 expect(fib, "delegate");
1975 }
1976
1977 // Test unsafe reset in hold state
1978 unittest
1979 {
1980 auto fib = new Fiber(function {ubyte[2048] buf = void; Fiber.yield();}, 4096);
1981 foreach (_; 0 .. 10)
1982 {
1983 fib.call();
1984 assert(fib.state == Fiber.State.HOLD);
1985 fib.reset();
1986 }
1987 }
1988
1989 // stress testing GC stack scanning
1990 unittest
1991 {
1992 import core.memory;
1993 import core.time : dur;
1994
unreferencedThreadObject()1995 static void unreferencedThreadObject()
1996 {
1997 static void sleep() { Thread.sleep(dur!"msecs"(100)); }
1998 auto thread = new Thread(&sleep).start();
1999 }
2000 unreferencedThreadObject();
2001 GC.collect();
2002
2003 static class Foo
2004 {
this(int value)2005 this(int value)
2006 {
2007 _value = value;
2008 }
2009
bar()2010 int bar()
2011 {
2012 return _value;
2013 }
2014
2015 int _value;
2016 }
2017
collect()2018 static void collect()
2019 {
2020 auto foo = new Foo(2);
2021 assert(foo.bar() == 2);
2022 GC.collect();
2023 Fiber.yield();
2024 GC.collect();
2025 assert(foo.bar() == 2);
2026 }
2027
2028 auto fiber = new Fiber(&collect);
2029
2030 fiber.call();
2031 GC.collect();
2032 fiber.call();
2033
2034 // thread reference
2035 auto foo = new Foo(2);
2036
collect2()2037 void collect2()
2038 {
2039 assert(foo.bar() == 2);
2040 GC.collect();
2041 Fiber.yield();
2042 GC.collect();
2043 assert(foo.bar() == 2);
2044 }
2045
2046 fiber = new Fiber(&collect2);
2047
2048 fiber.call();
2049 GC.collect();
2050 fiber.call();
2051
recurse(size_t cnt)2052 static void recurse(size_t cnt)
2053 {
2054 --cnt;
2055 Fiber.yield();
2056 if (cnt)
2057 {
2058 auto fib = new Fiber(() { recurse(cnt); });
2059 fib.call();
2060 GC.collect();
2061 fib.call();
2062 }
2063 }
2064 fiber = new Fiber(() { recurse(20); });
2065 fiber.call();
2066 }
2067
2068
version(AsmX86_64_Windows)2069 version (AsmX86_64_Windows)
2070 {
2071 // Test Windows x64 calling convention
2072 unittest
2073 {
2074 void testNonvolatileRegister(alias REG)()
2075 {
2076 auto zeroRegister = new Fiber(() {
2077 mixin("asm pure nothrow @nogc { naked; xor "~REG~", "~REG~"; ret; }");
2078 });
2079 long after;
2080
2081 mixin("asm pure nothrow @nogc { mov "~REG~", 0xFFFFFFFFFFFFFFFF; }");
2082 zeroRegister.call();
2083 mixin("asm pure nothrow @nogc { mov after, "~REG~"; }");
2084
2085 assert(after == -1);
2086 }
2087
2088 void testNonvolatileRegisterSSE(alias REG)()
2089 {
2090 auto zeroRegister = new Fiber(() {
2091 mixin("asm pure nothrow @nogc { naked; xorpd "~REG~", "~REG~"; ret; }");
2092 });
2093 long[2] before = [0xFFFFFFFF_FFFFFFFF, 0xFFFFFFFF_FFFFFFFF], after;
2094
2095 mixin("asm pure nothrow @nogc { movdqu "~REG~", before; }");
2096 zeroRegister.call();
2097 mixin("asm pure nothrow @nogc { movdqu after, "~REG~"; }");
2098
2099 assert(before == after);
2100 }
2101
2102 testNonvolatileRegister!("R12")();
2103 testNonvolatileRegister!("R13")();
2104 testNonvolatileRegister!("R14")();
2105 testNonvolatileRegister!("R15")();
2106 testNonvolatileRegister!("RDI")();
2107 testNonvolatileRegister!("RSI")();
2108 testNonvolatileRegister!("RBX")();
2109
2110 testNonvolatileRegisterSSE!("XMM6")();
2111 testNonvolatileRegisterSSE!("XMM7")();
2112 testNonvolatileRegisterSSE!("XMM8")();
2113 testNonvolatileRegisterSSE!("XMM9")();
2114 testNonvolatileRegisterSSE!("XMM10")();
2115 testNonvolatileRegisterSSE!("XMM11")();
2116 testNonvolatileRegisterSSE!("XMM12")();
2117 testNonvolatileRegisterSSE!("XMM13")();
2118 testNonvolatileRegisterSSE!("XMM14")();
2119 testNonvolatileRegisterSSE!("XMM15")();
2120 }
2121 }
2122
2123
version(D_InlineAsm_X86_64)2124 version (D_InlineAsm_X86_64)
2125 {
2126 unittest
2127 {
2128 void testStackAlignment()
2129 {
2130 void* pRSP;
2131 asm pure nothrow @nogc
2132 {
2133 mov pRSP, RSP;
2134 }
2135 assert((cast(size_t)pRSP & 0xF) == 0);
2136 }
2137
2138 auto fib = new Fiber(&testStackAlignment);
2139 fib.call();
2140 }
2141 }
2142