vibe.core.concurrency 12/321(3%) line coverage

      
10
20
30
40
50
60
70
80
90
100
110
120
130
140
150
160
170
180
190
200
210
220
230
240
250
260
270
280
290
300
310
320
330
340
350
360
370
381
390
400
410
420
430
440
450
460
470
480
490
500
510
520
530
540
550
560
570
580
590
600
610
620
630
640
650
660
670
680
690
700
710
720
730
740
750
760
770
780
790
800
810
820
830
840
850
860
870
880
890
900
910
920
930
940
950
960
970
980
990
1000
1010
1020
1030
1040
1050
1060
1070
1080
1090
1100
1110
1120
1130
1140
1150
1160
1170
1180
1190
1200
1210
1220
1230
1240
1250
1261
1270
1280
1291
1301
1311
1321
1330
1340
1350
1360
1370
1381
1391
1401
1410
1420
1430
1440
1450
1460
1470
1480
1490
1500
1512
1520
1530
1540
1550
1560
1570
1580
1594
1600
1610
1620
1630
1640
1650
1660
1670
1680
1690
1700
1710
1720
1730
1740
1750
1760
1770
1780
1790
1800
1810
1820
1830
1840
1850
1860
1870
1880
1890
1900
1910
1920
1930
1940
1950
1960
1970
1980
1990
2000
2010
2020
2030
2040
2050
2060
2070
2080
2090
2100
2110
2120
2130
2140
2150
2160
2170
2180
2190
2200
2210
2220
2230
2240
2250
2260
2270
2280
2290
2300
2310
2320
2330
2340
2350
2360
2370
2380
2390
2400
2410
2420
2430
2440
2450
2460
2470
2480
2490
2500
2510
2520
2530
2540
2550
2560
2570
2580
2590
2600
2610
2620
2630
2640
2650
2660
2670
2680
2690
2700
2710
2720
2730
2740
2750
2760
2770
2780
2790
2800
2810
2820
2830
2840
2850
2860
2870
2880
2890
2900
2910
2920
2930
2940
2950
2960
2970
2980
2990
3000
3010
3020
3030
3040
3050
3060
3070
3080
3090
3100
3110
3120
3130
3140
3150
3160
3170
3180
3190
3200
3210
3220
3230
3240
3250
3260
3270
3280
3290
3300
3310
3320
3330
3340
3350
3360
3370
3380
3390
3400
3410
3420
3430
3440
3450
3460
3470
3480
3490
3500
3510
3520
3530
3540
3550
3560
3570
3580
3590
3600
3610
3620
3630
3640
3650
3660
3670
3680
3690
3700
3710
3720
3730
3740
3750
3760
3770
3780
3790
3800
3810
3820
3830
3840
3850
3860
3870
3880
3890
3900
3910
3920
3930
3940
3950
3960
3970
3980
3990
4000
4010
4020
4030
4040
4050
4060
4070
4080
4090
4100
4110
4120
4130
4140
4150
4160
4170
4180
4190
4200
4210
4220
4230
4240
4250
4260
4270
4280
4290
4300
4310
4320
4330
4340
4350
4360
4370
4380
4390
4400
4410
4420
4430
4440
4450
4460
4470
4480
4490
4500
4510
4520
4530
4540
4550
4560
4570
4580
4590
4600
4610
4620
4630
4640
4650
4660
4670
4680
4690
4700
4710
4720
4730
4740
4750
4760
4770
4780
4790
4800
4810
4820
4830
4840
4850
4860
4870
4880
4890
4900
4910
4920
4930
4940
4950
4960
4970
4980
4990
5000
5010
5020
5030
5040
5050
5060
5070
5080
5090
5100
5110
5120
5130
5140
5150
5160
5170
5180
5190
5200
5210
5220
5230
5240
5250
5260
5270
5280
5290
5300
5310
5320
5330
5340
5350
5360
5370
5380
5390
5400
5410
5420
5430
5440
5450
5460
5470
5480
5490
5500
5510
5520
5530
5540
5550
5560
5570
5580
5590
5600
5610
5620
5630
5640
5650
5660
5670
5680
5690
5700
5710
5720
5730
5740
5750
5760
5770
5780
5790
5800
5810
5820
5830
5840
5850
5860
5870
5880
5890
5900
5910
5920
5930
5940
5950
5960
5970
5980
5990
6000
6010
6020
6030
6040
6050
6060
6070
6080
6090
6100
6110
6120
6130
6140
6150
6160
6170
6180
6190
6200
6210
6220
6230
6240
6250
6260
6270
6280
6290
6300
6310
6320
6330
6340
6350
6360
6370
6380
6390
6400
6410
6420
6430
6440
6450
6460
6470
6480
6490
6500
6510
6520
6530
6540
6550
6560
6570
6580
6590
6600
6610
6620
6630
6640
6650
6660
6670
6680
6690
6700
6710
6720
6730
6740
6750
6760
6770
6780
6790
6800
6810
6820
6830
6840
6850
6860
6870
6880
6890
6900
6910
6920
6930
6940
6950
6960
6970
6980
6990
7000
7010
7020
7030
7040
7050
7060
7070
7080
7090
7100
7110
7120
7130
7140
7150
7160
7170
7180
7190
7200
7210
7220
7230
7240
7250
7260
7270
7280
7290
7300
7310
7320
7330
7340
7350
7360
7370
7380
7390
7400
7410
7420
7430
7440
7450
7460
7470
7480
7490
7500
7510
7520
7530
7540
7550
7560
7570
7580
7590
7600
7610
7620
7630
7640
7650
7660
7670
7680
7690
7700
7710
7720
7730
7740
7750
7760
7770
7780
7790
7800
7810
7820
7830
7840
7850
7860
7870
7880
7890
7900
7910
7920
7930
7940
7950
7960
7970
7980
7990
8000
8010
8020
8030
8040
8050
8060
8070
8080
8090
8100
8110
8120
8130
8140
8150
8160
8170
8180
8190
8200
8210
8220
8230
8240
8250
8260
8270
8280
8290
8300
8310
8320
8330
8340
8350
8360
8370
8380
8390
8400
8410
8420
8430
8440
8450
8460
8470
8480
8490
8500
8510
8520
8530
8540
8550
8560
8570
8580
8590
8600
8610
8620
8630
8640
8650
8660
8670
8680
8690
8700
8710
8720
8730
8740
8750
8760
8770
8780
8790
8800
8810
8820
8830
8840
8850
8860
8870
8880
8890
8900
8910
8920
8930
8940
8950
8960
8970
8980
8990
9000
9010
9020
9030
9040
9050
9060
9070
9080
9090
9100
9110
9120
9130
9140
9150
9160
9170
9180
9190
9200
9210
9220
9230
9240
9250
9260
9270
9280
9290
9300
9310
9320
9330
9340
9350
9360
9370
9380
9390
9400
9410
9420
9430
9440
9450
9460
9470
9480
9490
9500
9510
9520
9530
9540
9550
9560
9570
9580
9590
9600
9610
9620
9630
9640
9650
9660
9670
9680
9690
9700
9710
9720
9730
9740
9750
9760
9770
9780
9790
9800
9810
9820
9830
9840
9850
9860
9870
9880
9890
9900
9910
9920
9930
9940
9950
9960
9970
9980
9990
10000
10010
10020
10030
10040
10050
10060
10070
10080
10090
10100
10110
10120
10130
10140
10150
10160
10170
10180
10190
10200
10210
10220
10230
10240
10250
10260
10270
10280
10290
10300
10310
10320
10330
10340
10350
10360
10370
10380
10390
10400
10410
10420
10430
10440
10450
10460
10470
10480
10490
10500
10510
10520
10530
10540
10550
10560
10570
10580
10590
10600
10610
10620
10630
10640
10650
10660
10670
10680
10690
10700
10710
10720
10730
10740
10750
10760
10770
10780
10790
10800
10810
10820
10830
10840
10850
10860
10870
10880
10890
10900
10910
10920
10930
10940
10950
10960
10970
10980
10990
11000
11010
11020
11030
11040
11050
11060
11070
11080
11090
11100
11110
11120
11130
11140
11150
11160
11170
11180
11190
11200
11210
11220
11230
11240
11250
11260
11270
11280
11290
11300
11310
11320
11330
11340
11350
11360
11370
11380
11390
11400
11410
11420
11430
11440
11450
11460
11470
11480
11490
11500
11510
11520
11530
11540
11550
11560
11570
11580
11590
11600
11610
11620
11630
11640
11650
11660
11670
11680
11690
11700
11710
11720
11730
11740
11750
11760
11770
11780
11790
11800
11810
11820
11830
11840
11850
11860
11870
11880
11890
11900
11910
11920
11930
11940
11950
11960
11970
11980
11990
12000
12010
12020
12030
12040
12050
12060
12070
12080
12090
12100
12110
12120
12130
12140
12150
12160
12170
12180
12190
12200
12210
12220
12230
12240
12250
12260
12270
12280
12290
12300
12310
12320
12330
12340
12350
12360
12370
12380
12390
12400
12410
12420
12430
12440
12450
12460
12470
12480
12490
12500
12510
12520
12530
12540
12550
12560
12570
12580
12590
12600
12610
12620
12630
12640
12650
12660
12670
12680
12690
12700
12710
12720
12733
12740
12750
12760
12770
12780
12790
12800
12810
12820
12830
12840
12850
12860
12870
12880
12890
12900
12910
12920
12930
12940
12950
12960
12970
12980
12990
13000
13010
13020
13030
13040
13050
13060
13070
13080
13090
13100
13110
13120
13130
13140
13150
13160
13170
13180
13190
13200
13210
13220
13230
13240
13250
13260
13270
13280
13290
13300
13310
13320
13330
13340
13350
13360
13370
13380
13390
13400
13410
13420
13430
13440
13450
13460
13470
13480
13490
13500
13510
13520
13530
13540
13550
13560
13570
13580
13590
13600
13610
13620
13630
13640
13650
13660
13670
13680
13690
13700
13710
13720
13730
13740
13750
13760
13770
13780
13790
13800
13810
13820
13830
13840
13850
13860
13870
13880
13890
13900
13910
13920
13930
13940
13950
13960
13970
13980
13990
14000
14010
14020
14030
14040
14050
14060
14070
14080
14090
14100
14110
14120
14130
14140
14150
14160
14170
14180
14190
14200
14210
14220
14230
14240
14250
14260
14270
14280
14290
14300
14310
14320
14330
14340
14350
14360
14370
14380
14390
14400
14410
14420
14430
14440
14450
14460
14470
14480
14490
14500
14510
14520
14530
14540
14550
14560
14570
14580
14590
14600
14610
14620
14630
14640
14650
14660
14670
14680
14690
14700
14710
14720
/** 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-2016 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.internal.allocator; import vibe.internal.meta.traits : StripHeadConst; public import std.concurrency; 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(Task tid, Isolated!(double[]) array, size_t start_index) { foreach( i; 0 .. array.length ) array[i] = (start_index + i) * 0.5; sendCompat(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 Task[] tids; foreach (i, idx; indices) tids ~= runWorkerTaskH(&compute, Task.getThis(), subarrays[i].move(), idx); // collect results auto resultarrays = new Isolated!(double[])[tids.length]; foreach( i, tid; tids ) resultarrays[i] = receiveOnlyCompat!(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 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 `vibe.core.concurrency.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 `vibe.core.concurrency.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] == Tid)) enum bool isWeaklyIsolated = true; // Tid/MessageBox is not properly annotated with shared 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 else static if (isPointer!(T[0])) enum bool isWeaklyIsolated = is(typeof(*T[0].init) == immutable) || is(typeof(*T[0].init) == shared); else static if (isAggregateType!(T[0])) enum bool isWeaklyIsolated = isWeaklyIsolated!(FieldTypeTuple!(T[0])); else enum bool isWeaklyIsolated = true; } } unittest { static class A { int x; string y; } static struct B { string a; // strongly isolated Isolated!A b; // strongly isolated version(EnablePhobosFails) Isolated!(Isolated!A[]) c; // strongly isolated version(EnablePhobosFails) Isolated!(Isolated!A[string]) c; // AA implementation does not like this version(EnablePhobosFails) Isolated!(int[string]) d; // strongly isolated } static struct C { string a; // strongly isolated shared(A) b; // weakly isolated Isolated!A c; // strongly isolated shared(A*) d; // weakly isolated shared(A[]) e; // weakly isolated shared(A[string]) f; // weakly isolated } static struct D { A a; } // not isolated static struct E { void delegate() a; } // not isolated static struct F { void function() a; } // strongly isolated (functions are immutable) static struct G { void test(); } // strongly isolated static struct H { A[] a; } // not isolated static interface I {} static assert(!isStronglyIsolated!A); static assert(isStronglyIsolated!(FieldTypeTuple!A)); static assert(isStronglyIsolated!B); static assert(!isStronglyIsolated!C); static assert(!isStronglyIsolated!D); static assert(!isStronglyIsolated!E); static assert(isStronglyIsolated!F); static assert(isStronglyIsolated!G); static assert(!isStronglyIsolated!H); static assert(!isStronglyIsolated!I); static assert(!isWeaklyIsolated!A); static assert(isWeaklyIsolated!(FieldTypeTuple!A)); static assert(isWeaklyIsolated!B); static assert(isWeaklyIsolated!C); static assert(!isWeaklyIsolated!D); static assert(!isWeaklyIsolated!E); static assert(isWeaklyIsolated!F); static assert(isWeaklyIsolated!G); static assert(!isWeaklyIsolated!H); static assert(!isWeaklyIsolated!I); } unittest { static assert(isWeaklyIsolated!Tid); } template isCopyable(T) { static if( __traits(compiles, {foreach( t; [T.init]){}}) ) enum isCopyable = true; else enum isCopyable = false; } /******************************************************************************/ /* Future (promise) suppport */ /******************************************************************************/ /** Represents a values that will be computed asynchronously. This type uses $(D alias this) to enable transparent access to the result value. */ struct Future(T) { import vibe.internal.freelistref : FreeListRef; private { FreeListRef!(shared(T)) m_result; Task m_task; } /// Checks if the values was fully computed. @property bool ready() const { return !m_task.running; } /** Returns the computed value. This function waits for the computation to finish, if necessary, and then returns the final value. In case of an uncaught exception happening during the computation, the exception will be thrown instead. */ ref T getResult() { if (!ready) m_task.join(); assert(ready, "Task still running after join()!?"); return *cast(T*)&m_result.get(); // casting away shared is safe, because this is a unique reference } alias getResult this; private void init() { m_result = FreeListRef!(shared(T))(); } } /** Starts an asynchronous computation and returns a future for the result value. If the supplied callable and arguments are all weakly isolated, $(D vibe.core.core.runWorkerTask) will be used to perform the computation. Otherwise, $(D vibe.core.core.runTask) will be used. Params: callable: A callable value, can be either a function, a delegate, or a user defined type that defines an $(D opCall). args: Arguments to pass to the callable. Returns: Returns a $(D Future) object that can be used to access the result. See_also: $(D isWeaklyIsolated) */ Future!(StripHeadConst!(ReturnType!CALLABLE)) async(CALLABLE, ARGS...)(CALLABLE callable, ARGS args) if (is(typeof(callable(args)) == ReturnType!CALLABLE)) { import vibe.internal.freelistref : FreeListRef; import vibe.core.core; import std.functional : toDelegate; alias RET = StripHeadConst!(ReturnType!CALLABLE); Future!RET ret; ret.init(); static void compute(FreeListRef!(shared(RET)) dst, CALLABLE callable, ARGS args) { dst = cast(shared(RET))callable(args); } static if (isWeaklyIsolated!CALLABLE && isWeaklyIsolated!ARGS) { ret.m_task = runWorkerTaskH(&compute, ret.m_result, callable, args); } else { ret.m_task = runTask(toDelegate(&compute), ret.m_result, callable, args); } return ret; } /// unittest { import vibe.core.core; import vibe.core.log; void test() { auto val = async({ logInfo("Starting to compute value in worker task."); sleep(500.msecs); // simulate some lengthy computation logInfo("Finished computing value in worker task."); return 32; }); logInfo("Starting computation in main task"); sleep(200.msecs); // simulate some lengthy computation logInfo("Finished computation in main task. Waiting for async value."); logInfo("Result: %s", val.getResult()); } } /// unittest { int sum(int a, int b) { return a + b; } static int sum2(int a, int b) { return a + b; } void test() { // Using a delegate will use runTask internally assert(async(&sum, 2, 3).getResult() == 5); // Using a static function will use runTaskWorker internally, // if all arguments are weakly isolated assert(async(&sum2, 2, 3).getResult() == 5); } } unittest { import vibe.core.core : sleep; auto f = async({ immutable byte b = 1; return b; }); sleep(10.msecs); // let it finish first assert(f.getResult() == 1); // currently not possible because Task.join only works within a single thread. /*f = async({ immutable byte b = 2; sleep(10.msecs); // let the caller wait a little return b; }); assert(f.getResult() == 1);*/ } /******************************************************************************/ /******************************************************************************/ /* std.concurrency compatible interface for message passing */ /******************************************************************************/ /******************************************************************************/ enum ConcurrencyPrimitive { task, // Task run in the caller's thread (`runTask`) workerTask, // Task run in the worker thread pool (`runWorkerTask`) thread // Separate thread } /** Sets the concurrency primitive to use for `śtd.concurrency.spawn()`. By default, `spawn()` will start a thread for each call, mimicking the default behavior of `std.concurrency`. */ void setConcurrencyPrimitive(ConcurrencyPrimitive primitive) { import core.atomic : atomicStore; atomicStore(st_concurrencyPrimitive, primitive); } private shared ConcurrencyPrimitive st_concurrencyPrimitive = ConcurrencyPrimitive.thread; void send(ARGS...)(Task task, ARGS args) { std.concurrency.send(task.tidInfo.ident, args); } void send(ARGS...)(Tid tid, ARGS args) { std.concurrency.send(tid, args); } void prioritySend(ARGS...)(Task task, ARGS args) { std.concurrency.prioritySend(task.tidInfo.ident, args); } void prioritySend(ARGS...)(Tid tid, ARGS args) { std.concurrency.prioritySend(tid, args); } package class VibedScheduler : Scheduler { import core.sync.mutex; import vibe.core.core; import vibe.core.sync; override void start(void delegate() op) { op(); } override void spawn(void delegate() op) { import core.thread : Thread; final switch (st_concurrencyPrimitive) with (ConcurrencyPrimitive) { case task: runTask(op); break; case workerTask: static void wrapper(shared(void delegate()) op) { (cast(void delegate())op)(); } runWorkerTask(&wrapper, cast(shared)op); break; case thread: auto t = new Thread(op); t.start(); break; } } override void yield() {} override @property ref ThreadInfo thisInfo() { return Task.getThis().tidInfo; } override TaskCondition newCondition(Mutex m) { scope (failure) assert(false); version (VibeLibasyncDriver) { import vibe.core.drivers.libasync; if (LibasyncDriver.isControlThread) return null; } setupDriver(); return new TaskCondition(m); } } // Compatibility implementation of `send` using vibe.d's own std.concurrency implementation void sendCompat(ARGS...)(Task tid, ARGS args) { assert (tid != Task(), "Invalid task handle"); static assert(args.length > 0, "Need to send at least one value."); foreach(A; ARGS){ static assert(isWeaklyIsolated!A, "Only objects with no unshared or unisolated aliasing may be sent, not "~A.stringof~"."); } tid.messageQueue.send(Variant(IsolatedValueProxyTuple!ARGS(args))); } // Compatibility implementation of `prioritySend` using vibe.d's own std.concurrency implementation void prioritySendCompat(ARGS...)(Task tid, ARGS args) { assert (tid != Task(), "Invalid task handle"); static assert(args.length > 0, "Need to send at least one value."); foreach(A; ARGS){ static assert(isWeaklyIsolated!A, "Only objects with no unshared or unisolated aliasing may be sent, not "~A.stringof~"."); } tid.messageQueue.prioritySend(Variant(IsolatedValueProxyTuple!ARGS(args))); } // TODO: handle special exception types // Compatibility implementation of `receive` using vibe.d's own std.concurrency implementation void receiveCompat(OPS...)(OPS ops) { auto tid = Task.getThis(); assert(tid != Task.init, "Cannot receive task messages outside of a task."); tid.messageQueue.receive(opsFilter(ops), opsHandler(ops)); } // Compatibility implementation of `receiveOnly` using vibe.d's own std.concurrency implementation auto receiveOnlyCompat(ARGS...)() { import std.algorithm : move; ARGS ret; receiveCompat( (ARGS val) { move(val, ret); }, (LinkTerminated e) { throw e; }, (OwnerTerminated e) { throw e; }, (Variant val) { throw new MessageMismatch(format("Unexpected message type %s, expected %s.", val.type, ARGS.stringof)); } ); static if(ARGS.length == 1) return ret[0]; else return tuple(ret); } // Compatibility implementation of `receiveTimeout` using vibe.d's own std.concurrency implementation bool receiveTimeoutCompat(OPS...)(Duration timeout, OPS ops) { auto tid = Task.getThis(); assert(tid != Task.init, "Cannot receive task messages outside of a task."); return tid.messageQueue.receiveTimeout!OPS(timeout, opsFilter(ops), opsHandler(ops)); } // Compatibility implementation of `setMailboxSize` using vibe.d's own std.concurrency implementation void setMaxMailboxSizeCompat(Task tid, size_t messages, OnCrowding on_crowding) { final switch(on_crowding){ case OnCrowding.block: setMaxMailboxSizeCompat(tid, messages, null); break; case OnCrowding.throwException: setMaxMailboxSizeCompat(tid, messages, &onCrowdingThrow); break; case OnCrowding.ignore: setMaxMailboxSizeCompat(tid, messages, &onCrowdingDrop); break; } } // Compatibility implementation of `setMailboxSize` using vibe.d's own std.concurrency implementation void setMaxMailboxSizeCompat(Task tid, size_t messages, bool function(Task) on_crowding) { tid.messageQueue.setMaxSize(messages, on_crowding); } unittest { static class CLS {} static assert(is(typeof(sendCompat(Task.init, makeIsolated!CLS())))); static assert(is(typeof(sendCompat(Task.init, 1)))); static assert(is(typeof(sendCompat(Task.init, 1, "str", makeIsolated!CLS())))); static assert(!is(typeof(sendCompat(Task.init, new CLS)))); static assert(is(typeof(receiveCompat((Isolated!CLS){})))); static assert(is(typeof(receiveCompat((int){})))); static assert(is(typeof(receiveCompat!(void delegate(int, string, Isolated!CLS))((int, string, Isolated!CLS){})))); static assert(!is(typeof(receiveCompat((CLS){})))); } private bool onCrowdingThrow(Task tid){ import std.concurrency : Tid; throw new MailboxFull(Tid()); } private bool onCrowdingDrop(Task tid){ return false; } private struct IsolatedValueProxyTuple(T...) { staticMap!(IsolatedValueProxy, T) fields; this(ref T values) { foreach (i, Ti; T) { static if (isInstanceOf!(IsolatedSendProxy, IsolatedValueProxy!Ti)) { fields[i] = IsolatedValueProxy!Ti(values[i].unsafeGet()); } else fields[i] = values[i]; } } } private template IsolatedValueProxy(T) { static if (isInstanceOf!(IsolatedRef, T) || isInstanceOf!(IsolatedArray, T) || isInstanceOf!(IsolatedAssociativeArray, T)) { alias IsolatedValueProxy = IsolatedSendProxy!(T.BaseType); } else { alias IsolatedValueProxy = T; } } /+unittest { static class Test {} void test() { Task.getThis().send(new immutable Test, makeIsolated!Test()); } }+/ private struct IsolatedSendProxy(T) { alias BaseType = T; T value; } private bool callBool(F, T...)(F fnc, T args) { static string caller(string prefix) { import std.conv; string ret = prefix ~ "fnc("; foreach (i, Ti; T) { static if (i > 0) ret ~= ", "; static if (isInstanceOf!(IsolatedSendProxy, Ti)) ret ~= "assumeIsolated(args["~to!string(i)~"].value)"; else ret ~= "args["~to!string(i)~"]"; } ret ~= ");"; return ret; } static assert(is(ReturnType!F == bool) || is(ReturnType!F == void), "Message handlers must return either bool or void."); static if (is(ReturnType!F == bool)) mixin(caller("return ")); else { mixin(caller("")); return true; } } private bool delegate(Variant) @safe opsFilter(OPS...)(OPS ops) { return (Variant msg) @trusted { // Variant if (msg.convertsTo!Throwable) return true; foreach (i, OP; OPS) if (matchesHandler!OP(msg)) return true; return false; }; } private void delegate(Variant) @safe opsHandler(OPS...)(OPS ops) { return (Variant msg) @trusted { // Variant foreach (i, OP; OPS) { alias PTypes = ParameterTypeTuple!OP; if (matchesHandler!OP(msg)) { static if (PTypes.length == 1 && is(PTypes[0] == Variant)) { if (callBool(ops[i], msg)) return; // WARNING: proxied isolated values will go through verbatim! } else { auto msgt = msg.get!(IsolatedValueProxyTuple!PTypes); if (callBool(ops[i], msgt.fields)) return; } } } if (msg.convertsTo!Throwable) throw msg.get!Throwable(); }; } private bool matchesHandler(F)(Variant msg) { alias PARAMS = ParameterTypeTuple!F; if (PARAMS.length == 1 && is(PARAMS[0] == Variant)) return true; else return msg.convertsTo!(IsolatedValueProxyTuple!PARAMS); }