-
Notifications
You must be signed in to change notification settings - Fork 284
/
concurrency.d
1371 lines (1130 loc) · 40.6 KB
/
concurrency.d
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
/**
Functions and structures for dealing with threads and concurrent access.
This module is modeled after std.concurrency, but provides a fiber-aware alternative
to it. All blocking operations will yield the calling fiber instead of blocking it.
Copyright: © 2013-2014 RejectedSoftware e.K.
License: Subject to the terms of the MIT license, as written in the included LICENSE.txt file.
Authors: Sönke Ludwig
*/
module vibe.core.concurrency;
import core.time;
import std.traits;
import std.typecons;
import std.typetuple;
import std.variant;
import std.string;
import vibe.core.task;
import vibe.utils.memory;
import vibe.internal.newconcurrency;
static if (newStdConcurrency) public import std.concurrency;
else public import std.concurrency : MessageMismatch, OwnerTerminated, LinkTerminated, PriorityMessageException, MailboxFull, OnCrowding;
private extern (C) pure nothrow void _d_monitorenter(Object h);
private extern (C) pure nothrow void _d_monitorexit(Object h);
/**
Locks the given shared object and returns a ScopedLock for accessing any unshared members.
Using this function will ensure that there are no data races. For this reason, the class
type T is required to contain no unshared or unisolated aliasing.
See_Also: core.concurrency.isWeaklyIsolated
*/
ScopedLock!T lock(T : const(Object))(shared(T) object)
pure nothrow @safe {
return ScopedLock!T(object);
}
/// ditto
void lock(T : const(Object))(shared(T) object, scope void delegate(scope T) accessor)
nothrow {
auto l = lock(object);
accessor(l.unsafeGet());
}
///
unittest {
import vibe.core.concurrency;
static class Item {
private double m_value;
this(double value) pure { m_value = value; }
@property double value() const pure { return m_value; }
}
static class Manager {
private {
string m_name;
Isolated!(Item) m_ownedItem;
Isolated!(shared(Item)[]) m_items;
}
pure this(string name)
{
m_name = name;
auto itm = makeIsolated!Item(3.5);
m_ownedItem = itm.move;
}
void addItem(shared(Item) item) pure { m_items ~= item; }
double getTotalValue()
const pure {
double sum = 0;
// lock() is required to access shared objects
foreach (itm; m_items.unsafeGet) {
auto l = itm.lock();
sum += l.value;
}
// owned objects can be accessed without locking
sum += m_ownedItem.value;
return sum;
}
}
void test()
{
import std.stdio;
auto man = cast(shared)new Manager("My manager");
{
auto l = man.lock();
l.addItem(new shared(Item)(1.5));
l.addItem(new shared(Item)(0.5));
}
writefln("Total value: %s", man.lock().getTotalValue());
}
}
/**
Proxy structure that keeps the monitor of the given object locked until it
goes out of scope.
Any unshared members of the object are safely accessible during this time. The usual
way to use it is by calling lock.
See_Also: lock
*/
struct ScopedLock(T)
{
static assert(is(T == class), "ScopedLock is only usable with classes.");
// static assert(isWeaklyIsolated!(FieldTypeTuple!T), T.stringof~" contains non-immutable, non-shared references. Accessing it in a multi-threaded environment is not safe.");
private Rebindable!T m_ref;
@disable this(this);
this(shared(T) obj)
pure nothrow @trusted
{
assert(obj !is null, "Attempting to lock null object.");
m_ref = cast(T)obj;
_d_monitorenter(getObject());
assert(getObject().__monitor !is null);
}
~this()
pure nothrow @trusted
{
assert(m_ref !is null);
assert(getObject().__monitor !is null);
_d_monitorexit(getObject());
}
/**
Returns an unshared reference to the locked object.
Note that using this function breaks type safety. Be sure to not escape the reference beyond
the life time of the lock.
*/
@property inout(T) unsafeGet() inout nothrow { return m_ref; }
inout(T) opDot() inout nothrow { return m_ref; }
//pragma(msg, "In ScopedLock!("~T.stringof~")");
//pragma(msg, isolatedRefMethods!T());
// mixin(isolatedAggregateMethodsString!T());
private Object getObject()
pure nothrow {
static if( is(Rebindable!T == struct) ) return cast(Unqual!T)m_ref.get();
else return cast(Unqual!T)m_ref;
}
}
/**
Creates a new isolated object.
Isolated objects contain no mutable aliasing outside of their own reference tree. They can thus
be safely converted to immutable and they can be safely passed between threads.
The function returns an instance of Isolated that will allow proxied access to the members of
the object, as well as providing means to convert the object to immutable or to an ordinary
mutable object.
*/
pure Isolated!T makeIsolated(T, ARGS...)(ARGS args)
{
static if (is(T == class)) return Isolated!T(new T(args));
else static if (is(T == struct)) return T(args);
else static if (isPointer!T && is(PointerTarget!T == struct)) {
alias TB = PointerTarget!T;
return Isolated!T(new TB(args));
} else static assert(false, "makeIsolated works only for class and (pointer to) struct types.");
}
///
unittest {
import vibe.core.concurrency;
import vibe.core.core;
static class Item {
double value;
string name;
}
static void modifyItem(Isolated!Item itm)
{
itm.value = 1.3;
// TODO: send back to initiating thread
}
void test()
{
immutable(Item)[] items;
// create immutable item procedurally
auto itm = makeIsolated!Item();
itm.value = 2.4;
itm.name = "Test";
items ~= itm.freeze();
// send isolated item to other thread
auto itm2 = makeIsolated!Item();
runWorkerTask(&modifyItem, itm2.move());
// ...
}
}
unittest {
static class C { this(int x) pure {} }
static struct S { this(int x) pure {} }
alias CI = typeof(makeIsolated!C(0));
alias SI = typeof(makeIsolated!S(0));
alias SPI = typeof(makeIsolated!(S*)(0));
static assert(isStronglyIsolated!CI);
static assert(is(CI == IsolatedRef!C));
static assert(isStronglyIsolated!SI);
static assert(is(SI == S));
static assert(isStronglyIsolated!SPI);
static assert(is(SPI == IsolatedRef!S));
}
/**
Creates a new isolated array.
*/
pure Isolated!(T[]) makeIsolatedArray(T)(size_t size)
{
Isolated!(T[]) ret;
ret.length = size;
return ret.move();
}
///
unittest {
import vibe.core.concurrency;
import vibe.core.core;
static void compute(Tid tid, Isolated!(double[]) array, size_t start_index)
{
foreach( i; 0 .. array.length )
array[i] = (start_index + i) * 0.5;
send(tid, array.move());
}
void test()
{
import std.stdio;
// compute contents of an array using multiple threads
auto arr = makeIsolatedArray!double(256);
// partition the array (no copying takes place)
size_t[] indices = [64, 128, 192, 256];
Isolated!(double[])[] subarrays = arr.splice(indices);
// start processing in threads
Tid[] tids;
foreach (i, idx; indices)
tids ~= runWorkerTaskH(&compute, thisTid, subarrays[i].move(), idx);
// collect results
auto resultarrays = new Isolated!(double[])[tids.length];
foreach( i, tid; tids )
resultarrays[i] = receiveOnly!(Isolated!(double[])).move();
// BUG: the arrays must be sorted here, but since there is no way to tell
// from where something was received, this is difficult here.
// merge results (no copying takes place again)
foreach( i; 1 .. resultarrays.length )
resultarrays[0].merge(resultarrays[i]);
// convert the final result to immutable
auto result = resultarrays[0].freeze();
writefln("Result: %s", result);
}
}
/**
Unsafe facility to assume that an existing reference is unique.
*/
Isolated!T assumeIsolated(T)(T object)
{
return Isolated!T(object);
}
/**
Encapsulates the given type in a way that guarantees memory isolation.
See_Also: makeIsolated, makeIsolatedArray
*/
template Isolated(T)
{
static if( isWeaklyIsolated!T ){
alias Isolated = T;
} else static if( is(T == class) ){
alias Isolated = IsolatedRef!T;
} else static if( isPointer!T ){
alias Isolated = IsolatedRef!(PointerTarget!T);
} else static if( isDynamicArray!T ){
alias Isolated = IsolatedArray!(typeof(T.init[0]));
} else static if( isAssociativeArray!T ){
alias Isolated = IsolatedAssociativeArray!(KeyType!T, ValueType!T);
} else static assert(false, T.stringof~": Unsupported type for Isolated!T - must be class, pointer, array or associative array.");
}
// unit tests fails with DMD 2.064 due to some cyclic import regression
version (none) unittest
{
static class CE {}
static struct SE {}
static assert(is(Isolated!CE == IsolatedRef!CE));
static assert(is(Isolated!(SE*) == IsolatedRef!SE));
static assert(is(Isolated!(SE[]) == IsolatedArray!SE));
version(EnablePhobosFails){
// AAs don't work because they are impure
static assert(is(Isolated!(SE[string]) == IsolatedAssociativeArray!(string, SE)));
}
}
/// private
private struct IsolatedRef(T)
{
pure:
static assert(isWeaklyIsolated!(FieldTypeTuple!T), T.stringof ~ " contains non-immutable/non-shared references. Isolation cannot be guaranteed.");
enum __isWeakIsolatedType = true;
static if( isStronglyIsolated!(FieldTypeTuple!T) )
enum __isIsolatedType = true;
alias BaseType = T;
static if( is(T == class) ){
alias Tref = T;
alias Tiref = immutable(T);
} else {
alias Tref = T*;
alias Tiref = immutable(T)*;
}
private Tref m_ref;
//mixin isolatedAggregateMethods!T;
//pragma(msg, isolatedAggregateMethodsString!T());
mixin(isolatedAggregateMethodsString!T());
@disable this(this);
private this(Tref obj)
{
m_ref = obj;
}
this(ref IsolatedRef src)
{
m_ref = src.m_ref;
src.m_ref = null;
}
void opAssign(ref IsolatedRef src)
{
m_ref = src.m_ref;
src.m_ref = null;
}
/**
Returns the raw reference.
Note that using this function breaks type safety. Be sure to not escape the reference.
*/
inout(Tref) unsafeGet() inout { return m_ref; }
/**
Move the contained reference to a new IsolatedRef.
Since IsolatedRef is not copyable, using this function may be necessary when
passing a reference to a function or when returning it. The reference in
this instance will be set to null after the call returns.
*/
IsolatedRef move() { auto r = m_ref; m_ref = null; return IsolatedRef(r); }
/// ditto
void move(ref IsolatedRef target) { target.m_ref = m_ref; m_ref = null; }
/**
Convert the isolated reference to a normal mutable reference.
The reference in this instance will be set to null after the call returns.
*/
Tref extract()
{
auto ret = m_ref;
m_ref = null;
return ret;
}
/**
Converts the isolated reference to immutable.
The reference in this instance will be set to null after the call has returned.
Note that this method is only available for strongly isolated references,
which means references that do not contain shared aliasing.
*/
Tiref freeze()()
{
static assert(isStronglyIsolated!(FieldTypeTuple!T), "freeze() can only be called on strongly isolated values, but "~T.stringof~" contains shared references.");
auto ret = m_ref;
m_ref = null;
return cast(immutable)ret;
}
/**
Performs an up- or down-cast of the reference and moves it to a new IsolatedRef instance.
The reference in this instance will be set to null after the call has returned.
*/
U opCast(U)()
if (isInstanceOf!(IsolatedRef, U) && (is(U.BaseType : BaseType) || is(BaseType : U.BaseType)))
{
auto r = U(cast(U.BaseType)m_ref);
m_ref = null;
return r;
}
/**
Determines if the contained reference is non-null.
This method allows Isolated references to be used in boolean expressions without having to
extract the reference.
*/
U opCast(U)() const if(is(U == bool)) { return m_ref !is null; }
}
/// private
private struct IsolatedArray(T)
{
static assert(isWeaklyIsolated!T, T.stringof ~ " contains non-immutable references. Isolation cannot be guaranteed.");
enum __isWeakIsolatedType = true;
static if( isStronglyIsolated!T )
enum __isIsolatedType = true;
alias BaseType = T[];
private T[] m_array;
mixin isolatedArrayMethods!T;
@disable this(this);
/**
Returns the raw reference.
Note that using this function breaks type safety. Be sure to not escape the reference.
*/
inout(T[]) unsafeGet() inout { return m_array; }
IsolatedArray!T move() pure { auto r = m_array; m_array = null; return IsolatedArray(r); }
void move(ref IsolatedArray target) pure { target.m_array = m_array; m_array = null; }
T[] extract()
pure {
auto arr = m_array;
m_array = null;
return arr;
}
immutable(T)[] freeze()() pure
{
static assert(isStronglyIsolated!T, "Freeze can only be called on strongly isolated values, but "~T.stringof~" contains shared references.");
auto arr = m_array;
m_array = null;
return cast(immutable)arr;
}
/**
Splits the array into individual slices at the given incides.
The indices must be in ascending order. Any items that are larger than
the last given index will remain in this IsolatedArray.
*/
IsolatedArray!T[] splice(in size_t[] indices...) pure
in {
//import std.algorithm : isSorted;
assert(indices.length > 0, "At least one splice index must be given.");
//assert(isSorted(indices), "Indices must be in ascending order.");
assert(indices[$-1] <= m_array.length, "Splice index out of bounds.");
}
body {
auto ret = new IsolatedArray!T[indices.length];
size_t lidx = 0;
foreach( i, sidx; indices ){
ret[i].m_array = m_array[lidx .. sidx];
lidx = sidx;
}
m_array = m_array[lidx .. $];
return ret;
}
void merge(ref IsolatedArray!T array) pure
in {
assert(array.m_array.ptr == m_array.ptr+m_array.length || array.m_array.ptr+array.length == m_array.ptr,
"Argument to merge() must be a neighbouring array partition.");
}
body {
if( array.m_array.ptr == m_array.ptr + m_array.length ){
m_array = m_array.ptr[0 .. m_array.length + array.length];
} else {
m_array = array.m_array.ptr[0 .. m_array.length + array.length];
}
array.m_array.length = 0;
}
}
/// private
private struct IsolatedAssociativeArray(K, V)
{
pure:
static assert(isWeaklyIsolated!K, "Key type has aliasing. Memory isolation cannot be guaranteed.");
static assert(isWeaklyIsolated!V, "Value type has aliasing. Memory isolation cannot be guaranteed.");
enum __isWeakIsolatedType = true;
static if( isStronglyIsolated!K && isStronglyIsolated!V )
enum __isIsolatedType = true;
alias BaseType = V[K];
private {
V[K] m_aa;
}
mixin isolatedAssociativeArrayMethods!(K, V);
/**
Returns the raw reference.
Note that using this function breaks type safety. Be sure to not escape the reference.
*/
inout(V[K]) unsafeGet() inout { return m_aa; }
IsolatedAssociativeArray move() { auto r = m_aa; m_aa = null; return IsolatedAssociativeArray(r); }
void move(ref IsolatedAssociativeArray target) { target.m_aa = m_aa; m_aa = null; }
V[K] extract()
{
auto arr = m_aa;
m_aa = null;
return arr;
}
static if( is(typeof(IsolatedAssociativeArray.__isIsolatedType)) ){
immutable(V)[K] freeze()
{
auto arr = m_aa;
m_aa = null;
return cast(immutable(V)[K])(arr);
}
immutable(V[K]) freeze2()
{
auto arr = m_aa;
m_aa = null;
return cast(immutable(V[K]))(arr);
}
}
}
/** Encapsulates a reference in a way that disallows escaping it or any contained references.
*/
template ScopedRef(T)
{
static if( isAggregateType!T ) alias ScopedRef = ScopedRefAggregate!T;
else static if( isAssociativeArray!T ) alias ScopedRef = ScopedRefAssociativeArray!T;
else static if( isArray!T ) alias ScopedRef = ScopedRefArray!T;
else static if( isBasicType!T ) alias ScopedRef = ScopedRefBasic!T;
else static assert(false, "Unsupported type for ScopedRef: "~T.stringof);
}
/// private
private struct ScopedRefBasic(T)
{
private T* m_ref;
@disable this(this);
this(ref T tref) pure { m_ref = &tref; }
//void opAssign(T value) { *m_ref = value; }
ref T unsafeGet() pure { return *m_ref; }
alias unsafeGet this;
}
/// private
private struct ScopedRefAggregate(T)
{
private T* m_ref;
@disable this(this);
this(ref T tref) pure { m_ref = &tref; }
//void opAssign(T value) { *m_ref = value; }
ref T unsafeGet() pure { return *m_ref; }
static if( is(T == shared) ){
auto lock() pure { return .lock(unsafeGet()); }
} else {
mixin(isolatedAggregateMethodsString!T());
//mixin isolatedAggregateMethods!T;
}
}
/// private
private struct ScopedRefArray(T)
{
alias V = typeof(T.init[0]) ;
private T* m_ref;
private @property ref T m_array() pure { return *m_ref; }
private @property ref const(T) m_array() const pure { return *m_ref; }
mixin isolatedArrayMethods!(V, !is(T == const) && !is(T == immutable));
@disable this(this);
this(ref T tref) pure { m_ref = &tref; }
//void opAssign(T value) { *m_ref = value; }
ref T unsafeGet() pure { return *m_ref; }
}
/// private
private struct ScopedRefAssociativeArray(K, V)
{
alias K = KeyType!T;
alias V = ValueType!T;
private T* m_ref;
private @property ref T m_array() pure { return *m_ref; }
private @property ref const(T) m_array() const pure { return *m_ref; }
mixin isolatedAssociativeArrayMethods!(K, V);
@disable this(this);
this(ref T tref) pure { m_ref = &tref; }
//void opAssign(T value) { *m_ref = value; }
ref T unsafeGet() pure { return *m_ref; }
}
/******************************************************************************/
/* COMMON MIXINS FOR NON-REF-ESCAPING WRAPPER STRUCTS */
/******************************************************************************/
/// private
/*private mixin template(T) isolatedAggregateMethods
{
mixin(isolatedAggregateMethodsString!T());
}*/
/// private
private string isolatedAggregateMethodsString(T)()
{
import vibe.internal.meta.traits;
string ret = generateModuleImports!T();
//pragma(msg, "Type '"~T.stringof~"'");
foreach( mname; __traits(allMembers, T) ){
static if (isPublicMember!(T, mname)) {
static if (isRWPlainField!(T, mname)) {
alias mtype = typeof(__traits(getMember, T, mname)) ;
auto mtypename = fullyQualifiedName!mtype;
//pragma(msg, " field " ~ mname ~ " : " ~ mtype.stringof);
ret ~= "@property ScopedRef!(const("~mtypename~")) "~mname~"() const pure { return ScopedRef!(const("~mtypename~"))(m_ref."~mname~"); }\n";
ret ~= "@property ScopedRef!("~mtypename~") "~mname~"() pure { return ScopedRef!("~mtypename~")(m_ref."~mname~"); }\n";
static if( !is(mtype == const) && !is(mtype == immutable) ){
static if( isWeaklyIsolated!mtype ){
ret ~= "@property void "~mname~"("~mtypename~" value) pure { m_ref."~mname~" = value; }\n";
} else {
ret ~= "@property void "~mname~"(AT)(AT value) pure { static assert(isWeaklyIsolated!AT); m_ref."~mname~" = value.unsafeGet(); }\n";
}
}
} else {
foreach( method; __traits(getOverloads, T, mname) ){
alias ftype = FunctionTypeOf!method;
// only pure functions are allowed (or they could escape references to global variables)
// don't allow non-isolated references to be escaped
if( functionAttributes!ftype & FunctionAttribute.pure_ &&
isWeaklyIsolated!(ReturnType!ftype) )
{
static if( __traits(isStaticFunction, method) ){
//pragma(msg, " static method " ~ mname ~ " : " ~ ftype.stringof);
ret ~= "static "~fullyQualifiedName!(ReturnType!ftype)~" "~mname~"(";
foreach( i, P; ParameterTypeTuple!ftype ){
if( i > 0 ) ret ~= ", ";
ret ~= fullyQualifiedName!P ~ " p"~i.stringof;
}
ret ~= "){ return "~fullyQualifiedName!T~"."~mname~"(";
foreach( i, P; ParameterTypeTuple!ftype ){
if( i > 0 ) ret ~= ", ";
ret ~= "p"~i.stringof;
}
ret ~= "); }\n";
} else if (mname != "__ctor") {
//pragma(msg, " normal method " ~ mname ~ " : " ~ ftype.stringof);
if( is(ftype == const) ) ret ~= "const ";
if( is(ftype == shared) ) ret ~= "shared ";
if( is(ftype == immutable) ) ret ~= "immutable ";
if( functionAttributes!ftype & FunctionAttribute.pure_ ) ret ~= "pure ";
if( functionAttributes!ftype & FunctionAttribute.property ) ret ~= "@property ";
ret ~= fullyQualifiedName!(ReturnType!ftype)~" "~mname~"(";
foreach( i, P; ParameterTypeTuple!ftype ){
if( i > 0 ) ret ~= ", ";
ret ~= fullyQualifiedName!P ~ " p"~i.stringof;
}
ret ~= "){ return m_ref."~mname~"(";
foreach( i, P; ParameterTypeTuple!ftype ){
if( i > 0 ) ret ~= ", ";
ret ~= "p"~i.stringof;
}
ret ~= "); }\n";
}
}
}
}
} //else pragma(msg, " non-public field " ~ mname);
}
return ret;
}
/// private
private mixin template isolatedArrayMethods(T, bool mutableRef = true)
{
@property size_t length() const pure { return m_array.length; }
@property bool empty() const pure { return m_array.length == 0; }
static if( mutableRef ){
@property void length(size_t value) pure { m_array.length = value; }
void opCatAssign(T item) pure
{
static if( isCopyable!T ) m_array ~= item;
else {
m_array.length++;
m_array[$-1] = item;
}
}
void opCatAssign(IsolatedArray!T array) pure
{
static if( isCopyable!T ) m_array ~= array.m_array;
else {
size_t start = m_array.length;
m_array.length += array.length;
foreach( i, ref itm; array.m_array )
m_array[start+i] = itm;
}
}
}
ScopedRef!(const(T)) opIndex(size_t idx) const pure { return ScopedRef!(const(T))(m_array[idx]); }
ScopedRef!T opIndex(size_t idx) pure { return ScopedRef!T(m_array[idx]); }
static if( !is(T == const) && !is(T == immutable) )
void opIndexAssign(T value, size_t idx) pure { m_array[idx] = value; }
int opApply(int delegate(ref size_t, ref ScopedRef!T) del)
pure {
foreach( idx, ref v; m_array ){
auto noref = ScopedRef!T(v);
if( auto ret = (cast(int delegate(ref size_t, ref ScopedRef!T) pure)del)(idx, noref) )
return ret;
}
return 0;
}
int opApply(int delegate(ref size_t, ref ScopedRef!(const(T))) del)
const pure {
foreach( idx, ref v; m_array ){
auto noref = ScopedRef!(const(T))(v);
if( auto ret = (cast(int delegate(ref size_t, ref ScopedRef!(const(T))) pure)del)(idx, noref) )
return ret;
}
return 0;
}
int opApply(int delegate(ref ScopedRef!T) del)
pure {
foreach( v; m_array ){
auto noref = ScopedRef!T(v);
if( auto ret = (cast(int delegate(ref ScopedRef!T) pure)del)(noref) )
return ret;
}
return 0;
}
int opApply(int delegate(ref ScopedRef!(const(T))) del)
const pure {
foreach( v; m_array ){
auto noref = ScopedRef!(const(T))(v);
if( auto ret = (cast(int delegate(ref ScopedRef!(const(T))) pure)del)(noref) )
return ret;
}
return 0;
}
}
/// private
private mixin template isolatedAssociativeArrayMethods(K, V, bool mutableRef = true)
{
@property size_t length() const pure { return m_aa.length; }
@property bool empty() const pure { return m_aa.length == 0; }
static if( !is(V == const) && !is(V == immutable) )
void opIndexAssign(V value, K key) pure { m_aa[key] = value; }
inout(V) opIndex(K key) inout pure { return m_aa[key]; }
int opApply(int delegate(ref ScopedRef!K, ref ScopedRef!V) del)
pure {
foreach( ref k, ref v; m_aa )
if( auto ret = (cast(int delegate(ref ScopedRef!K, ref ScopedRef!V) pure)del)(k, v) )
return ret;
return 0;
}
int opApply(int delegate(ref ScopedRef!V) del)
pure {
foreach( ref v; m_aa )
if( auto ret = (cast(int delegate(ref ScopedRef!V) pure)del)(v) )
return ret;
return 0;
}
int opApply(int delegate(ref ScopedRef!(const(K)), ref ScopedRef!(const(V))) del)
const pure {
foreach( ref k, ref v; m_aa )
if( auto ret = (cast(int delegate(ref ScopedRef!(const(K)), ref ScopedRef!(const(V))) pure)del)(k, v) )
return ret;
return 0;
}
int opApply(int delegate(ref ScopedRef!(const(V))) del)
const pure {
foreach( v; m_aa )
if( auto ret = (cast(int delegate(ref ScopedRef!(const(V))) pure)del)(v) )
return ret;
return 0;
}
}
/******************************************************************************/
/* UTILITY FUNCTIONALITY */
/******************************************************************************/
// private
private @property string generateModuleImports(T)()
{
bool[string] visited;
//pragma(msg, "generateModuleImports "~T.stringof);
return generateModuleImportsImpl!T(visited);
}
private @property string generateModuleImportsImpl(T, TYPES...)(ref bool[string] visited)
{
string ret;
//pragma(msg, T);
//pragma(msg, TYPES);
static if( !haveTypeAlready!(T, TYPES) ){
void addModule(string mod){
if( mod !in visited ){
ret ~= "static import "~mod~";\n";
visited[mod] = true;
}
}
static if( isAggregateType!T && !is(typeof(T.__isWeakIsolatedType)) ){ // hack to avoid a recursive template instantiation when Isolated!T is passed to moduleName
addModule(moduleName!T);
foreach( member; __traits(allMembers, T) ){
//static if( isPublicMember!(T, member) ){
static if( !is(typeof(__traits(getMember, T, member))) ){
// ignore sub types
} else static if( !is(FunctionTypeOf!(__traits(getMember, T, member)) == function) ){
alias mtype = typeof(__traits(getMember, T, member)) ;
ret ~= generateModuleImportsImpl!(mtype, T, TYPES)(visited);
} else static if( is(T == class) || is(T == interface) ){
foreach( overload; MemberFunctionsTuple!(T, member) ){
ret ~= generateModuleImportsImpl!(ReturnType!overload, T, TYPES)(visited);
foreach( P; ParameterTypeTuple!overload )
ret ~= generateModuleImportsImpl!(P, T, TYPES)(visited);
}
} // TODO: handle structs!
//}
}
}
else static if( isPointer!T ) ret ~= generateModuleImportsImpl!(PointerTarget!T, T, TYPES)(visited);
else static if( isArray!T ) ret ~= generateModuleImportsImpl!(typeof(T.init[0]), T, TYPES)(visited);
else static if( isAssociativeArray!T ) ret ~= generateModuleImportsImpl!(KeyType!T, T, TYPES)(visited) ~ generateModuleImportsImpl!(ValueType!T, T, TYPES)(visited);
}
return ret;
}
template haveTypeAlready(T, TYPES...)
{
static if( TYPES.length == 0 ) enum haveTypeAlready = false;
else static if( is(T == TYPES[0]) ) enum haveTypeAlready = true;
else alias haveTypeAlready = haveTypeAlready!(T, TYPES[1 ..$]);
}
/******************************************************************************/
/* Additional traits useful for handling isolated data */
/******************************************************************************/
/**
Determines if the given list of types has any non-immutable aliasing outside of their object tree.
The types in particular may only contain plain data, pointers or arrays to immutable data, or references
encapsulated in stdx.typecons.Isolated.
*/
template isStronglyIsolated(T...)
{
static if (T.length == 0) enum bool isStronglyIsolated = true;
else static if (T.length > 1) enum bool isStronglyIsolated = isStronglyIsolated!(T[0 .. $/2]) && isStronglyIsolated!(T[$/2 .. $]);
else {
static if (is(T[0] == immutable)) enum bool isStronglyIsolated = true;
else static if(isInstanceOf!(Rebindable, T[0])) enum bool isStronglyIsolated = isStronglyIsolated!(typeof(T[0].get()));
else static if (is(typeof(T[0].__isIsolatedType))) enum bool isStronglyIsolated = true;
else static if (is(T[0] == class)) enum bool isStronglyIsolated = false;
else static if (is(T[0] == interface)) enum bool isStronglyIsolated = false; // can't know if the implementation is isolated
else static if (is(T[0] == delegate)) enum bool isStronglyIsolated = false; // can't know to what a delegate points
else static if (isDynamicArray!(T[0])) enum bool isStronglyIsolated = is(typeof(T[0].init[0]) == immutable);
else static if (isAssociativeArray!(T[0])) enum bool isStronglyIsolated = false; // TODO: be less strict here
else static if (isSomeFunction!(T[0])) enum bool isStronglyIsolated = true; // functions are immutable
else static if (isPointer!(T[0])) enum bool isStronglyIsolated = is(typeof(*T[0].init) == immutable);
else static if (isAggregateType!(T[0])) enum bool isStronglyIsolated = isStronglyIsolated!(FieldTypeTuple!(T[0]));
else enum bool isStronglyIsolated = true;
}
}
/**
Determines if the given list of types has any non-immutable and unshared aliasing outside of their object tree.
The types in particular may only contain plain data, pointers or arrays to immutable or shared data, or references
encapsulated in stdx.typecons.Isolated. Values that do not have unshared and unisolated aliasing are safe to be passed
between threads.
*/
template isWeaklyIsolated(T...)
{
static if (T.length == 0) enum bool isWeaklyIsolated = true;
else static if (T.length > 1) enum bool isWeaklyIsolated = isWeaklyIsolated!(T[0 .. $/2]) && isWeaklyIsolated!(T[$/2 .. $]);
else {
static if(is(T[0] == immutable)) enum bool isWeaklyIsolated = true;
else static if (is(T[0] == shared)) enum bool isWeaklyIsolated = true;
else static if (isInstanceOf!(Rebindable, T[0])) enum bool isWeaklyIsolated = isWeaklyIsolated!(typeof(T[0].get()));
else static if (is(T[0] : Throwable)) enum bool isWeaklyIsolated = true; // WARNING: this is unsafe, but needed for send/receive!
else static if (is(typeof(T[0].__isIsolatedType))) enum bool isWeaklyIsolated = true;
else static if (is(typeof(T[0].__isWeakIsolatedType))) enum bool isWeaklyIsolated = true;
else static if (is(T[0] == class)) enum bool isWeaklyIsolated = false;
else static if (is(T[0] == interface)) enum bool isWeaklyIsolated = false; // can't know if the implementation is isolated
else static if (is(T[0] == delegate)) enum bool isWeaklyIsolated = T[0].stringof.endsWith(" shared"); // can't know to what a delegate points - FIXME: use something better than a string comparison
else static if (isDynamicArray!(T[0])) enum bool isWeaklyIsolated = is(typeof(T[0].init[0]) == immutable);
else static if (isAssociativeArray!(T[0])) enum bool isWeaklyIsolated = false; // TODO: be less strict here
else static if (isSomeFunction!(T[0])) enum bool isWeaklyIsolated = true; // functions are immutable