-
Notifications
You must be signed in to change notification settings - Fork 1
/
builder.d
389 lines (308 loc) · 12.1 KB
/
builder.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
module sidero.eventloop.coroutine.builder;
import sidero.eventloop.coroutine.internal.state;
import sidero.eventloop.coroutine.condition;
import sidero.eventloop.coroutine.generic;
import sidero.eventloop.coroutine.future;
import sidero.eventloop.coroutine.instanceable;
import sidero.eventloop.handles;
import sidero.base.allocators;
import sidero.base.errors;
import sidero.base.internal.atomic;
///
struct CoroutineBuilder(State, Stages, ResultType = void, Args...) {
///
static assert(is(State == struct), "State of a coroutine can only be represented by a struct");
static assert(!__traits(isNested, State), "State of a coroutine must not be a nested struct. Annotate with static.");
///
static assert(() {
size_t lastValue;
foreach(Name; __traits(allMembers, Stages)) {
if(__traits(getMember, Stages, Name) > lastValue + 1)
return false;
lastValue = __traits(getMember, Stages, Name);
}
return true;
}(), "Stages must be an enum of 0 .. length in values and may have duplicates sequentially");
///
alias CoroutineResultType = CoroutineResult!(ResultType);
///
alias FunctionPrototype = CoroutineResultType function(scope ref State) @safe nothrow @nogc;
private {
FunctionPrototype[__traits(allMembers, Stages).length] functions;
}
export @safe nothrow @nogc:
///
void opIndexAssign(FunctionPrototype func, Stages stage) scope {
functions[stage] = func;
}
///
Result!(InstanceableCoroutine!(ResultType, Args)) build(RCAllocator allocator = RCAllocator.init) scope @trusted {
foreach(func; functions) {
if(func is null)
return typeof(return)(NullPointerException("All functions in vtable must be filled"));
}
if(allocator.isNull)
allocator = globalAllocator();
CoroutinePair!ResultType pair;
pair.descriptor = allocator.make!(CoroutineDescriptor!ResultType)();
pair.descriptor.base.allocator = allocator;
pair.descriptor.base.toDeallocate = (cast(void*)pair.descriptor)[0 .. CoroutineDescriptor!ResultType.sizeof];
pair.descriptor.base.deinit = (scope void[] memory) @trusted {
CoroutineDescriptor!ResultType* descriptor = cast(CoroutineDescriptor!ResultType*)memory.ptr;
(*descriptor).destroy;
};
pair.descriptor.base.userFunctions = allocator.makeArray!(void*)(this.functions.length);
pair.descriptor.base.functions = allocator.makeArray!(CoroutineAllocatorMemoryDescriptor.FunctionPrototype)(this.functions.length);
static struct CoroutineState2 {
CoroutineState!ResultType parent;
alias parent this;
State userState;
}
pair.descriptor.base.createInstanceState = (RCAllocator allocator, void* args) @trusted {
CoroutineState2* ret = allocator.make!CoroutineState2;
ret.base.allocator = allocator;
ret.base.toDeallocate = (cast(void*)ret)[0 .. CoroutineState2.sizeof];
ret.base.deinit = (scope void[] memory) @trusted {
CoroutineState2* state = cast(CoroutineState2*)memory.ptr;
(*state).destroy;
};
static if(Args.length > 0) {
ArgsStorage!Args argsStorage = *cast(ArgsStorage!Args*)args;
ret.userState = State(argsStorage.values);
}
return &ret.parent.base;
};
pair.descriptor.base.setErrorResult = (scope CoroutineAllocatorMemoryState* state, ErrorInfo errorInfo) @trusted nothrow @nogc {
CoroutineState2* actualState = cast(CoroutineState2*)state;
actualState.base.conditionToContinue = CoroutineCondition.init;
actualState.result = Result!ResultType(errorInfo.info, errorInfo.moduleName, errorInfo.line);
actualState.base.nextFunctionTag = -2;
actualState.base.setAsComplete();
};
static foreach(offset, Stage; __traits(allMembers, Stages)) {
pair.descriptor.base.userFunctions[offset] = this.functions[__traits(getMember, Stages, Stage)];
pair.descriptor.base.functions[offset] = (scope CoroutineAllocatorMemoryDescriptor* descriptor,
scope CoroutineAllocatorMemoryState* state) @trusted {
CoroutineDescriptor!ResultType* actualDescriptor = cast(CoroutineDescriptor!ResultType*)descriptor;
CoroutineState2* actualState = cast(CoroutineState2*)state;
actualState.base.conditionToContinue = CoroutineCondition.init;
FunctionPrototype actualFunction = cast(FunctionPrototype)actualDescriptor.base.userFunctions[offset];
CoroutineResultType result = actualFunction(actualState.userState);
final switch(result.tag) {
case CoroutineResultType.Tag.Value:
static if(!is(ResultType == void)) {
actualState.result = Result!ResultType(result.resultValue);
}
actualState.base.nextFunctionTag = -1;
actualState.base.setAsComplete();
break;
case CoroutineResultType.Tag.Error:
actualState.result = Result!ResultType(result.error);
actualState.base.nextFunctionTag = -2;
actualState.base.setAsComplete();
break;
case CoroutineResultType.Tag.Stage:
actualState.base.conditionToContinue = result.condition;
actualState.base.nextFunctionTag = cast(ptrdiff_t)result.stage;
break;
}
};
}
InstanceableCoroutine!(ResultType, Args) ret;
ret.pair = pair;
ret.pair.rc(true);
return typeof(return)(ret);
}
static if(is(ResultType == void)) {
///
static CoroutineResultType complete() {
return CoroutineResultType.init;
}
} else {
///
static CoroutineResultType complete(ResultType value) {
auto ret = CoroutineResultType(CoroutineResultType.Tag.Value, Stages.init, ErrorInfo.init, CoroutineCondition.init, value);
return ret;
}
}
///
static CoroutineResultType complete(ErrorMessage error, string mod = __MODULE__, int line = __LINE__) {
return CoroutineResultType(CoroutineResultType.Tag.Error, Stages.init, ErrorInfo(error, mod, line));
}
///
static CoroutineResultType complete(ErrorInfo error) {
return CoroutineResultType(CoroutineResultType.Tag.Error, Stages.init, error);
}
///
static CoroutineResultType nextStage(Stages next) {
return CoroutineResultType(CoroutineResultType.Tag.Stage, next);
}
}
///
struct CoroutineResult(ResultType = void) {
private {
Tag tag;
size_t stage;
ErrorInfo error;
CoroutineCondition condition;
static if(!is(ResultType == void)) {
ResultType resultValue;
}
enum Tag {
Value,
Stage,
Error,
}
}
export @safe nothrow @nogc:
this(scope ref CoroutineResult other) scope @trusted {
this.tupleof = other.tupleof;
}
~this() scope {
}
///
CoroutineResult after(ResultType)(Future!ResultType waitingOn) scope return @trusted {
return this.after(waitingOn.asGeneric());
}
///
CoroutineResult after(ResultType, Args...)(InstanceableCoroutine!(ResultType, Args) waitingOn) scope return @trusted {
return this.after(waitingOn.asGeneric());
}
///
CoroutineResult after(GenericCoroutine waitingOn) scope return @trusted {
assert(tag == Tag.Stage);
this.condition.coroutine = waitingOn;
return this;
}
}
///
unittest {
InstanceableCoroutine!(int, int) createCo() {
static struct State {
int value;
@safe nothrow @nogc:
this(int value) {
this.value = value;
}
}
enum Stages {
Multiply,
Add,
}
alias Builder = CoroutineBuilder!(State, Stages, int, int);
Builder builder;
builder[Stages.Multiply] = (scope ref state) {
state.value *= 3;
// workaround for: https://issues.dlang.org/show_bug.cgi?id=23835
return Builder.nextStage(Stages.Add);
};
builder[Stages.Add] = (scope ref state) {
state.value += 2;
// workaround for: https://issues.dlang.org/show_bug.cgi?id=23835
return Builder.complete(state.value);
};
auto got = builder.build();
assert(got);
return got.get;
}
auto co = createCo();
assert(!co.isNull);
auto co2 = co.makeInstance(RCAllocator.init, 4);
assert(!co2.isNull);
while(!co2.isComplete) {
assert(co2.condition.waitingOn == CoroutineCondition.WaitingOn.Nothing);
ErrorResult er = co2.unsafeResume();
assert(er);
}
auto result = co2.result();
assert(result);
assert(result == 14);
}
///
unittest {
static InstanceableCoroutine!(int, int) createCo1() {
static struct State {
int value;
@safe nothrow @nogc:
this(int value) {
this.value = value;
}
}
enum Stages {
Multiply,
Add,
}
alias Builder = CoroutineBuilder!(State, Stages, int, int);
Builder builder;
builder[Stages.Multiply] = (scope ref state) {
state.value *= 3;
// workaround for: https://issues.dlang.org/show_bug.cgi?id=23835
return Builder.nextStage(Stages.Add);
};
builder[Stages.Add] = (scope ref state) {
state.value += 2;
// workaround for: https://issues.dlang.org/show_bug.cgi?id=23835
return Builder.complete(state.value);
};
auto got = builder.build();
assert(got);
return got.get;
}
static InstanceableCoroutine!(int, int) createCo2() {
import sidero.base.synchronization.system.lock;
static struct State {
SystemLock lock;
int value;
Future!int worker;
@safe nothrow @nogc:
this(int value) {
this.value = value;
}
}
enum Stages {
INeedCoroutine,
Done,
}
CoroutineBuilder!(State, Stages, int, int) builder;
builder[Stages.INeedCoroutine] = (scope ref state) @trusted {
state.lock.unlock;
auto worker = createCo1();
state.worker = worker.makeInstance(RCAllocator.init, state.value);
assert(!state.worker.isNull);
// workaround for: https://issues.dlang.org/show_bug.cgi?id=23835
return typeof(builder).nextStage(Stages.Done).after(state.worker);
};
builder[Stages.Done] = (scope ref state) @trusted {
// workaround for: https://issues.dlang.org/show_bug.cgi?id=23835
return typeof(builder).complete(state.worker.result.assumeOkay + 8);
};
auto got = builder.build();
assert(got);
return got.get;
}
auto co = createCo2();
assert(!co.isNull);
auto coI = co.makeInstance(RCAllocator.init, 4);
assert(!coI.isNull);
while(!coI.isComplete) {
final switch(coI.condition.waitingOn) {
case CoroutineCondition.WaitingOn.Nothing:
break;
case CoroutineCondition.WaitingOn.ExternalTrigger:
assert(0);
case CoroutineCondition.WaitingOn.Coroutine:
auto coI2 = coI.condition.coroutine;
while(!coI2.isComplete) {
assert(coI2.condition.waitingOn == CoroutineCondition.WaitingOn.Nothing);
ErrorResult er = coI2.unsafeResume();
assert(er);
}
break;
}
ErrorResult er = coI.unsafeResume();
assert(er);
}
auto result = coI.result();
assert(result);
assert(result == 22);
}