This repository has been archived by the owner on Oct 12, 2022. It is now read-only.
/
thread.di
1062 lines (874 loc) · 29.8 KB
/
thread.di
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* The thread module provides support for thread creation and management.
*
* Copyright: Copyright Sean Kelly 2005 - 2009.
* License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost License 1.0)
* Authors: Sean Kelly, Walter Bright
* Source: $(DRUNTIMESRC core/_thread.d)
*/
/* Copyright Sean Kelly 2005 - 2009.
* Distributed under the Boost Software License, Version 1.0.
* (See accompanying file LICENSE or copy at
* http://www.boost.org/LICENSE_1_0.txt)
* Source: $(LINK http://www.dsource.org/projects/druntime/browser/trunk/src/core/thread.d)
*/
module core.thread;
public import core.time; // for Duration
// this should be true for most architectures
version = StackGrowsDown;
/**
* Returns the process ID of the calling process, which is guaranteed to be
* unique on the system. This call is always successful.
*
* Example:
* ---
* writefln("Current process id: %s", getpid());
* ---
*/
version(Posix)
{
import core.sys.posix.unistd;
alias core.sys.posix.unistd.getpid getpid;
}
else version (Windows)
{
import core.sys.windows.windows;
alias core.sys.windows.windows.GetCurrentProcessId getpid;
}
///////////////////////////////////////////////////////////////////////////////
// Thread and Fiber Exceptions
///////////////////////////////////////////////////////////////////////////////
/**
* Base class for thread exceptions.
*/
class ThreadException : Exception
{
this(string msg, string file = __FILE__, size_t line = __LINE__, Throwable next = null);
this(string msg, Throwable next, string file = __FILE__, size_t line = __LINE__);
}
/**
* Base class for fiber exceptions.
*/
class FiberException : Exception
{
this(string msg, string file = __FILE__, size_t line = __LINE__, Throwable next = null);
this(string msg, Throwable next, string file = __FILE__, size_t line = __LINE__);
}
///////////////////////////////////////////////////////////////////////////////
// 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 );
/**
* 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 );
~this();
///////////////////////////////////////////////////////////////////////////
// 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();
/**
* 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 Throwable join( bool rethrow = true );
///////////////////////////////////////////////////////////////////////////
// General Properties
///////////////////////////////////////////////////////////////////////////
/**
* Gets the user-readable label for this thread.
*
* Returns:
* The name of this thread.
*/
final @property string name();
/**
* Sets the user-readable label for this thread.
*
* Params:
* val = The new name of this thread.
*/
final @property void name( string val );
/**
* 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 @property bool 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 @property void isDaemon( bool val );
/**
* Tests whether this thread is running.
*
* Returns:
* true if the thread is running, false if not.
*/
final @property bool 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.
*/
__gshared 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.
*/
__gshared const int PRIORITY_MAX;
/**
* Gets the scheduling priority for the associated thread.
*
* Returns:
* The scheduling priority of this thread.
*/
final @property int priority();
/**
* Sets the scheduling priority for the associated thread.
*
* Params:
* val = The new scheduling priority of this thread.
*/
final @property void priority( int val );
///////////////////////////////////////////////////////////////////////////
// Actions on Calling Thread
///////////////////////////////////////////////////////////////////////////
/**
* Suspends the calling thread for at least the supplied period. This may
* result in multiple OS calls if period is greater than the maximum sleep
* duration supported by the operating system.
*
* Params:
* val = The minimum duration the calling thread should be suspended.
*
* In:
* period must be non-negative.
*
* Example:
* ------------------------------------------------------------------------
*
* Thread.sleep( dur!("msecs")( 50 ) ); // sleep for 50 milliseconds
* Thread.sleep( dur!("seconds")( 5 ) ); // sleep for 5 seconds
*
* ------------------------------------------------------------------------
*/
static void sleep( Duration val );
/**
* $(RED Deprecated. It will be removed in December 2012. Please use the
* version which takes a $(D Duration) instead.)
*
* Suspends the calling thread for at least the supplied period. This may
* result in multiple OS calls if period is greater than the maximum sleep
* duration supported by the operating system.
*
* Params:
* period = The minimum duration the calling thread should be suspended,
* in 100 nanosecond intervals.
*
* In:
* period must be non-negative.
*
* Example:
* ------------------------------------------------------------------------
*
* Thread.sleep( 500_000 ); // sleep for 50 milliseconds
* Thread.sleep( 50_000_000 ); // sleep for 5 seconds
*
* ------------------------------------------------------------------------
*/
deprecated static void sleep( long period );
/**
* Forces a context switch to occur away from the calling thread.
*/
static void 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. If the current thread is not
* attached to the runtime, a null reference is returned.
*/
static Thread getThis();
/**
* 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();
/**
* 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( scope int delegate( ref Thread ) dg );
///////////////////////////////////////////////////////////////////////////
// Static Initalizer
///////////////////////////////////////////////////////////////////////////
// This initializer is used to set thread constants. All functional
// initialization occurs within thread_init().
shared static this();
///////////////////////////////////////////////////////////////////////////
// Stuff That Should Go Away
///////////////////////////////////////////////////////////////////////////
private:
//
// Standard types
//
version( Windows )
{
alias uint TLSKey;
alias uint ThreadAddr;
}
else version( Posix )
{
import core.sys.posix.pthread;
alias pthread_key_t TLSKey;
alias pthread_t ThreadAddr;
}
// These must be kept in sync with core/thread.d
version (D_LP64)
{
version (Windows) enum ThreadSize = 312;
else version (OSX) enum ThreadSize = 320;
else version (Posix) enum ThreadSize = 184;
else static assert(0, "Platform not supported.");
}
else
{
static assert((void*).sizeof == 4); // 32-bit
version (Windows) enum ThreadSize = 128;
else version (OSX) enum ThreadSize = 128;
else version (Posix) enum ThreadSize = 92;
else static assert(0, "Platform not supported.");
}
void data[ThreadSize - __traits(classInstanceSize, Object)] = void;
}
///////////////////////////////////////////////////////////////////////////////
// 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();
/**
*
*/
extern (C) bool thread_isMainThread();
/**
* Registers the calling thread for use with the D Runtime. If this routine
* is called for a thread which is already registered, no action is performed.
*/
extern (C) Thread thread_attachThis();
version( Windows )
{
// NOTE: These calls are not safe on Posix systems that use signals to
// perform garbage collection. The suspendHandler uses getThis()
// to get the thread handle so getThis() must be a simple call.
// Mutexes can't safely be acquired inside signal handlers, and
// even if they could, the mutex needed (Thread.slock) is held by
// thread_suspendAll(). So in short, these routines will remain
// Windows-specific. If they are truly needed elsewhere, the
// suspendHandler will need a way to call a version of getThis()
// that only does the TLS lookup without the fancy fallback stuff.
/// ditto
extern (C) Thread thread_attachByAddr( Thread.ThreadAddr addr );
/// ditto
extern (C) Thread thread_attachByAddrB( Thread.ThreadAddr addr, void* bstack );
}
/**
* Deregisters the calling thread from use with the runtime. If this routine
* is called for a thread which is not registered, no action is performed.
*/
extern (C) void thread_detachThis();
/// ditto
extern (C) void thread_detachByAddr( Thread.ThreadAddr addr );
/**
* Search the list of all threads for a thread with the given thread identifier.
*
* Params:
* addr = The thread identifier to search for.
* Returns:
* The thread object associated with the thread identifier, null if not found.
*/
static Thread thread_findByAddr( Thread.ThreadAddr addr );
/**
* Sets the current thread to a specific reference. Only to be used
* when dealing with externally-created threads (in e.g. C code).
* The primary use of this function is when Thread.getThis() must
* return a sensible value in, for example, TLS destructors. In
* other words, don't touch this unless you know what you're doing.
*
* Params:
* t = A reference to the current thread. May be null.
*/
extern (C) void thread_setThis(Thread t);
/**
* 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();
// Performs intermediate shutdown of the thread module.
shared static ~this();
/**
* 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();
/**
* 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();
/**
* Indicates the kind of scan being performed by $(D thread_scanAllType).
*/
enum ScanType
{
stack, /// The stack and/or registers are being scanned.
tls, /// TLS data is being scanned.
}
alias void delegate(void*, void*) ScanAllThreadsFn; /// The scanning function.
alias void delegate(ScanType, void*, void*) ScanAllThreadsTypeFn; /// ditto
/**
* 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.
*
* In:
* This routine must be preceded by a call to thread_suspendAll.
*/
extern (C) void thread_scanAllType( scope ScanAllThreadsTypeFn scan );
/**
* 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.
*
* In:
* This routine must be preceded by a call to thread_suspendAll.
*/
extern (C) void thread_scanAll( scope ScanAllThreadsFn scan );
/*
* Signals that the code following this call is a critical region. Any code in
* this region must finish running before the calling thread can be suspended
* by a call to thread_suspendAll. If the world is stopped while the calling
* thread is in a critical region, it will be continually suspended and resumed
* until it is outside a critical region.
*
* This function is, in particular, meant to help maintain garbage collector
* invariants when a lock is not used.
*
* A critical region is exited with thread_exitCriticalRegion.
*
* $(RED Warning):
* Using critical regions is extremely error-prone. For instance, using a lock
* inside a critical region will most likely result in an application deadlocking
* because the stop-the-world routine will attempt to suspend and resume the thread
* forever, to no avail.
*
* The term and concept of a 'critical region' comes from
* $(LINK2 https://github.com/mono/mono/blob/521f4a198e442573c400835ef19bbb36b60b0ebb/mono/metadata/sgen-gc.h#L925 Mono's SGen garbage collector).
*
* In:
* The calling thread must be attached to the runtime.
*/
extern (C) void thread_enterCriticalRegion();
/*
* Signals that the calling thread is no longer in a critical region. Following
* a call to this function, the thread can once again be suspended.
*
* In:
* The calling thread must be attached to the runtime.
*/
extern (C) void thread_exitCriticalRegion();
/*
* Returns true if the current thread is in a critical region; otherwise, false.
*
* In:
* The calling thread must be attached to the runtime.
*/
extern (C) bool thread_inCriticalRegion();
/**
* Indicates whether an address has been marked by the GC.
*/
enum IsMarked : int
{
no, /// Address is not marked.
yes, /// Address is marked.
unknown, /// Address is not managed by the GC.
}
alias IsMarked delegate( void* addr ) IsMarkedDg;
/**
* This routine allows the runtime to process any special per-thread handling
* for the GC. This is needed for taking into account any memory that is
* referenced by non-scanned pointers but is about to be freed. That currently
* means the array append cache.
*
* Params:
* isMarked = The function used to check if $(D addr) is marked.
*
* In:
* This routine must be called just prior to resuming all threads.
*/
extern(C) void thread_processGCMarks( scope IsMarkedDg isMarked );
/**
* Returns the stack top of the currently active stack within the calling
* thread.
*
* In:
* The calling thread must be attached to the runtime.
*
* Returns:
* The address of the stack top.
*/
extern (C) void* thread_stackTop();
/**
* Returns the stack bottom of the currently active stack within the calling
* thread.
*
* In:
* The calling thread must be attached to the runtime.
*
* Returns:
* The address of the stack bottom.
*/
extern (C) void* thread_stackBottom();
///////////////////////////////////////////////////////////////////////////////
// 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 );
/**
* 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 );
/**
* 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 );
/**
* 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 );
/**
* Operates on all threads currently tracked by this object.
*/
final int opApply( scope int delegate( ref Thread ) dg );
/**
* 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 );
private:
// These must be kept in sync with core/thread.d
version (D_LP64)
{
enum ThreadGroupSize = 24;
}
else
{
static assert((void*).sizeof == 4); // 32-bit
enum ThreadGroupSize = 12;
}
void data[ThreadGroupSize - __traits(classInstanceSize, Object)] = void;
}
///////////////////////////////////////////////////////////////////////////////
// Fiber Platform Detection and Memory Allocation
///////////////////////////////////////////////////////////////////////////////
private extern __gshared const size_t PAGESIZE;
shared static this();
///////////////////////////////////////////////////////////////////////////////
// Fiber
///////////////////////////////////////////////////////////////////////////////
/**
* 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
{
///////////////////////////////////////////////////////////////////////////
// Initialization
///////////////////////////////////////////////////////////////////////////
/**
* Initializes a fiber object which is associated with a static
* D function.
*
* Params:
* fn = The fiber function.
* sz = The stack size for this fiber.
*
* In:
* fn must not be null.
*/
this( void function() fn, size_t sz = PAGESIZE*4 );
/**
* Initializes a fiber object which is associated with a dynamic
* D function.
*
* Params:
* dg = The fiber function.
* sz = The stack size for this fiber.
*
* In:
* dg must not be null.
*/
this( void delegate() dg, size_t sz = PAGESIZE*4 );
~this();
///////////////////////////////////////////////////////////////////////////
// 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 );
/**
* Resets this fiber so that it may be re-used. 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.
*
* Params:
* fn = The fiber function.
* dg = The fiber function.
*
* In:
* This fiber must be in state TERM.
*/
final void reset();
/// ditto
final void reset( void function() fn );
/// ditto
final void reset( void delegate() dg );
///////////////////////////////////////////////////////////////////////////
// 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 @property State state() const;
///////////////////////////////////////////////////////////////////////////
// Actions on Calling Fiber
///////////////////////////////////////////////////////////////////////////
/**
* Forces a context switch to occur away from the calling fiber.
*/
static void yield();
/**
* Forces a context switch to occur away from the calling fiber and then
* throws obj in the calling fiber.
*
* Params:
* t = The object to throw.
*
* In:
* t must not be null.
*/
static void yieldAndThrow( Throwable t );