-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathActor.cpp
499 lines (428 loc) · 16.4 KB
/
Actor.cpp
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
//===--- Actor.cpp - Unit tests for the actor API -------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#include "swift/ABI/Actor.h"
#include "swift/Runtime/Concurrency.h"
#include "swift/Runtime/Metadata.h"
#include "swift/Basic/STLExtras.h"
#include "gtest/gtest.h"
#include <optional>
#include <tuple>
#include <vector>
using namespace swift;
/// The current location.
static unsigned progressIndex = 0;
#define EXPECT_PROGRESS(NUMBER) \
EXPECT_EQ((unsigned) (NUMBER), progressIndex++)
enum {
FinishedIndex = 100000
};
static void finishTest() {
progressIndex = FinishedIndex;
}
static std::vector<Job*> globalQueue;
SWIFT_CC(swift)
static void enqueueGlobal(Job *job,
swift_task_enqueueGlobal_original original) {
assert(job);
// Check that the job isn't already on the queue.
for (auto oldJob: globalQueue) {
EXPECT_NE(job, oldJob);
}
// The queue will actually be executed starting from the back.
// Add the job after (i.e. before in execution order) all jobs
// with lower priority.
for (auto i = globalQueue.begin(), e = globalQueue.end(); i != e; ++i) {
if (descendingPriorityOrder((*i)->getPriority(), job->getPriority()) <= 0) {
globalQueue.insert(i, job);
return;
}
}
// If the job's priority is higher than everything in the existing
// queue, set it as the new front of the queue.
globalQueue.push_back(job);
}
static void run(llvm::function_ref<void()> fn) {
swift_task_enqueueGlobal_hook = &enqueueGlobal;
progressIndex = 0;
// Run the setup function.
fn();
// The setup function needs to add something to the queue.
EXPECT_FALSE(globalQueue.empty());
// Insertion does a priority sort, so we can just process in-order.
// But that order starts from the back.
while (!globalQueue.empty()) {
auto job = globalQueue.back();
globalQueue.pop_back();
swift_job_run(job, SerialExecutorRef::generic());
}
EXPECT_EQ(FinishedIndex, progressIndex);
swift_task_enqueueGlobal_hook = nullptr;
}
namespace {
/// A simple actor.
class TestActor : public DefaultActor {
public:
TestActor();
~TestActor() {
swift_defaultActor_destroy(this);
}
bool HasBeenDestructed = false;
};
static SWIFT_CC(swift)
void destroyTestActor(SWIFT_CONTEXT HeapObject *_object) {
delete static_cast<TestActor*>(_object);
}
static FullMetadata<ClassMetadata> TestActorMetadata = {
{ { nullptr }, { &destroyTestActor }, { &VALUE_WITNESS_SYM(Bo) } },
{ { nullptr }, ClassFlags::UsesSwiftRefcounting, 0, 0, 0, 0, 0, 0 }
};
TestActor::TestActor() : DefaultActor(&TestActorMetadata) {
swift_defaultActor_initialize(this);
}
static TestActor *createActor() {
static ClassDescriptor *descriptor;
if (!descriptor) {
// Install a fake descriptor pointer into the actor metadata so that
// swift_getTypeName will tolerate it. Otherwise we crash when trying to
// signpost actor creation.
descriptor =
reinterpret_cast<ClassDescriptor *>(calloc(1, sizeof(*descriptor)));
TestActorMetadata.setDescription(descriptor);
}
return new TestActor();
}
/// A very silly template that stores the latest instance of a particular
/// lambda in global storage and then returns a function pointer that
/// matches an async task continuation function signature.
template <class Fn, class Context>
class TaskContinuationFromLambda {
static std::optional<Fn> lambdaStorage;
SWIFT_CC(swiftasync)
static void invoke(SWIFT_ASYNC_CONTEXT AsyncContext *context, SWIFT_CONTEXT HeapObject *) {
return (*lambdaStorage)(static_cast<Context*>(context));
}
public:
static TaskContinuationFunction *get(Fn &&fn) {
lambdaStorage.emplace(std::move(fn));
return (TaskContinuationFunction*) &invoke;
}
};
template <class Fn, class Context>
std::optional<Fn> TaskContinuationFromLambda<Fn, Context>::lambdaStorage;
} // end anonymous namespace
template <class Context, class Fn>
static std::pair<AsyncTask*, Context*>
createTaskWithContext(JobPriority priority, Fn &&fn) {
auto invoke =
TaskContinuationFromLambda<Fn, Context>::get(std::move(fn));
TaskCreateFlags createFlags;
createFlags.setRequestedPriority(priority);
auto pair = swift_task_create_common(createFlags.getOpaqueValue(),
nullptr,
nullptr,
invoke,
nullptr,
sizeof(Context));
return std::make_pair(pair.Task,
static_cast<Context*>(pair.InitialContext));
}
template <class Fn>
static AsyncTask *createTask(JobPriority priority, Fn &&fn) {
return createTaskWithContext<AsyncContext, Fn>(priority, std::move(fn))
.first;
}
template <class Fn>
static AsyncTask *createAndEnqueueTask(JobPriority priority,
TestActor *actor,
Fn &&fn) {
auto task = createTaskWithContext<AsyncContext, Fn>(priority, std::move(fn))
.first;
SerialExecutorRef executor = SerialExecutorRef::generic();
if (actor) {
executor = SerialExecutorRef::forDefaultActor(actor);
}
swift_task_enqueueTaskOnExecutor(task, executor);
return task;
}
template <class Context, class Fn>
static void parkTask(AsyncTask *task, Context *context, Fn &&fn) {
auto invoke =
TaskContinuationFromLambda<Fn, Context>::get(std::move(fn));
auto currentTask = swift_task_suspend();
EXPECT_EQ(task, currentTask);
task->ResumeTask = invoke;
task->ResumeContext = context;
}
template <class Context, class Fn>
static TaskContinuationFunction *prepareContinuation(Fn &&fn) {
return TaskContinuationFromLambda<Fn, Context>::get(std::move(fn));
}
namespace {
template <class... ValueTypes>
class TupleContext : public AsyncContext {
public:
using TupleType = std::tuple<ValueTypes...>;
TupleType values;
template <unsigned N> auto get() { return std::get<N>(values); }
};
/// This extremely silly template repeatedly rotates an argument list
/// until the last argument is first, then returns a pair of that and
/// a tuple of the remaining arguments.
template <unsigned N> struct Decomposer {
template <class FirstTy, class... OtherTys>
static auto decompose(FirstTy &&first, OtherTys &&...others) {
return Decomposer<N-1>::decompose(std::forward<OtherTys>(others)...,
std::forward<FirstTy>(first));
}
};
template <> struct Decomposer<0> {
template <class FirstTy, class... OtherTys>
static auto decompose(FirstTy &&first, OtherTys &&...others) {
return std::make_pair(std::move(first),
std::make_tuple(std::forward<OtherTys>(others)...));
}
};
/// This moderately silly template forwards a template argument pack.
template <class T> struct TupleContextTypeFor;
template <class... EltTys> struct TupleContextTypeFor<std::tuple<EltTys...>> {
using type = TupleContext<EltTys...>;
};
} // end anonymous namespace
template <class... ArgTypes>
static AsyncTask *createTaskStoring(JobPriority priority,
ArgTypes... args) {
auto fnAndTuple = Decomposer<sizeof...(args) - 1>::decompose(args...);
using TupleType = decltype(fnAndTuple.second);
using ContextType = typename TupleContextTypeFor<TupleType>::type;
auto taskAndContext =
createTaskWithContext<ContextType>(priority, std::move(fnAndTuple.first));
auto ptr = &taskAndContext.second->values;
new(ptr) TupleType(std::move(fnAndTuple.second));
return taskAndContext.first;
}
TEST(ActorTest, validateTestHarness) {
run([] {
auto task0 = createTask(JobPriority::Background,
[](AsyncContext *context) SWIFT_CC(swiftasync) {
EXPECT_PROGRESS(5);
EXPECT_PROGRESS(6);
finishTest();
return context->ResumeParent(context);
});
auto task1 = createTask(JobPriority::Default,
[](AsyncContext *context) SWIFT_CC(swiftasync) {
EXPECT_PROGRESS(1);
EXPECT_PROGRESS(2);
return context->ResumeParent(context);
});
auto task2 = createTask(JobPriority::Default,
[](AsyncContext *context) SWIFT_CC(swiftasync) {
EXPECT_PROGRESS(3);
EXPECT_PROGRESS(4);
return context->ResumeParent(context);
});
SerialExecutorRef executor = SerialExecutorRef::generic();
swift_task_enqueueTaskOnExecutor(task0, executor);
swift_task_enqueueTaskOnExecutor(task1, executor);
swift_task_enqueueTaskOnExecutor(task2, executor);
EXPECT_PROGRESS(0);
});
}
TEST(ActorTest, actorSwitch) {
run([] {
using Context = TupleContext<AsyncTask*, TestActor*>;
auto actor = createActor();
auto task0 = createTaskStoring(JobPriority::Default,
(AsyncTask*) nullptr, actor,
[](Context *context) SWIFT_CC(swiftasync) {
EXPECT_PROGRESS(1);
EXPECT_TRUE(swift_task_getCurrentExecutor().isGeneric());
EXPECT_EQ(nullptr, context->get<0>());
std::get<0>(context->values) = swift_task_getCurrent();
auto continuation = prepareContinuation<Context>(
[](Context *context) SWIFT_CC(swiftasync) {
EXPECT_PROGRESS(2);
auto executor = swift_task_getCurrentExecutor();
EXPECT_FALSE(executor.isGeneric());
EXPECT_EQ(SerialExecutorRef::forDefaultActor(context->get<1>()),
executor);
EXPECT_EQ(swift_task_getCurrent(), context->get<0>());
auto continuation = prepareContinuation<Context>(
[](Context *context) SWIFT_CC(swiftasync) {
EXPECT_PROGRESS(3);
EXPECT_TRUE(swift_task_getCurrentExecutor().isGeneric());
EXPECT_EQ(swift_task_getCurrent(), context->get<0>());
finishTest();
return context->ResumeParent(context);
});
return swift_task_switch(context, continuation,
SerialExecutorRef::generic());
});
return swift_task_switch(context, continuation,
SerialExecutorRef::forDefaultActor(context->get<1>()));
});
swift_task_enqueueTaskOnExecutor(task0, SerialExecutorRef::generic());
EXPECT_PROGRESS(0);
});
}
TEST(ActorTest, actorContention) {
run([] {
using Context = TupleContext<AsyncTask*, TestActor*>;
auto actor = createActor();
// This test only really works because actors are FIFO.
auto task0 = createTaskStoring(JobPriority::Default,
(AsyncTask*) nullptr, actor,
[](Context *context) SWIFT_CC(swiftasync) {
EXPECT_PROGRESS(1);
EXPECT_TRUE(swift_task_getCurrentExecutor().isGeneric());
EXPECT_EQ(nullptr, context->get<0>());
auto task = swift_task_getCurrent();
EXPECT_FALSE(task == nullptr);
std::get<0>(context->values) = task;
parkTask(task, context,
[](Context *context) SWIFT_CC(swiftasync) {
EXPECT_PROGRESS(2);
auto executor = swift_task_getCurrentExecutor();
EXPECT_FALSE(executor.isGeneric());
EXPECT_EQ(SerialExecutorRef::forDefaultActor(context->get<1>()),
executor);
auto task = swift_task_getCurrent();
EXPECT_EQ(task, context->get<0>());
parkTask(task, context,
[](Context *context) SWIFT_CC(swiftasync) {
EXPECT_PROGRESS(4);
EXPECT_TRUE(swift_task_getCurrentExecutor().isGeneric());
EXPECT_EQ(swift_task_getCurrent(), context->get<0>());
return context->ResumeParent(context);
});
swift_task_enqueueTaskOnExecutor(task, SerialExecutorRef::generic());
});
swift_task_enqueueTaskOnExecutor(task, SerialExecutorRef::forDefaultActor(context->get<1>()));
});
swift_task_enqueueTaskOnExecutor(task0, SerialExecutorRef::generic());
auto task1 = createTaskStoring(JobPriority::Background,
(AsyncTask*) nullptr, actor,
[](Context *context) SWIFT_CC(swiftasync) {
EXPECT_PROGRESS(3);
auto executor = swift_task_getCurrentExecutor();
EXPECT_FALSE(executor.isGeneric());
EXPECT_EQ(SerialExecutorRef::forDefaultActor(context->get<1>()),
executor);
EXPECT_EQ(nullptr, context->get<0>());
auto task = swift_task_getCurrent();
std::get<0>(context->values) = task;
parkTask(task, context,
[](Context *context) SWIFT_CC(swiftasync) {
EXPECT_PROGRESS(5);
EXPECT_TRUE(swift_task_getCurrentExecutor().isGeneric());
EXPECT_EQ(swift_task_getCurrent(), context->get<0>());
finishTest();
return context->ResumeParent(context);
});
swift_task_enqueueTaskOnExecutor(task, SerialExecutorRef::generic());
});
swift_task_enqueueTaskOnExecutor(task1, SerialExecutorRef::forDefaultActor(actor));
EXPECT_PROGRESS(0);
});
}
TEST(ActorTest, actorPriority) {
run([] {
auto actor = createActor();
createAndEnqueueTask(JobPriority::Background, actor,
[=](AsyncContext *context) {
EXPECT_PROGRESS(4);
return context->ResumeParent(context);
});
createAndEnqueueTask(JobPriority::Utility, actor,
[=](AsyncContext *context) {
EXPECT_PROGRESS(1);
return context->ResumeParent(context);
});
createAndEnqueueTask(JobPriority::Background, actor,
[=](AsyncContext *context) {
EXPECT_PROGRESS(5);
finishTest();
return context->ResumeParent(context);
});
createAndEnqueueTask(JobPriority::Utility, actor,
[=](AsyncContext *context) {
EXPECT_PROGRESS(2);
return context->ResumeParent(context);
});
createAndEnqueueTask(JobPriority::Default, actor,
[=](AsyncContext *context) {
EXPECT_PROGRESS(0);
return context->ResumeParent(context);
});
createAndEnqueueTask(JobPriority::Utility, actor,
[=](AsyncContext *context) {
EXPECT_PROGRESS(3);
return context->ResumeParent(context);
});
});
}
TEST(ActorTest, actorPriority2) {
run([] {
auto actor = createActor();
createAndEnqueueTask(JobPriority::Background, actor,
[=](AsyncContext *context) {
EXPECT_PROGRESS(7);
return context->ResumeParent(context);
});
createAndEnqueueTask(JobPriority::Utility, actor,
[=](AsyncContext *context) {
EXPECT_PROGRESS(1);
createAndEnqueueTask(JobPriority::Utility, actor,
[=](AsyncContext *context) {
EXPECT_PROGRESS(5);
return context->ResumeParent(context);
});
createAndEnqueueTask(JobPriority::Default, actor,
[](AsyncContext *context) {
EXPECT_PROGRESS(2);
return context->ResumeParent(context);
});
return context->ResumeParent(context);
});
createAndEnqueueTask(JobPriority::Background, actor,
[=](AsyncContext *context) {
EXPECT_PROGRESS(8);
return context->ResumeParent(context);
});
createAndEnqueueTask(JobPriority::Utility, actor,
[=](AsyncContext *context) {
EXPECT_PROGRESS(3);
createAndEnqueueTask(JobPriority::Background, actor,
[=](AsyncContext *context) {
EXPECT_PROGRESS(9);
finishTest();
return context->ResumeParent(context);
});
createAndEnqueueTask(JobPriority::Utility, actor,
[=](AsyncContext *context) {
EXPECT_PROGRESS(6);
return context->ResumeParent(context);
});
return context->ResumeParent(context);
});
createAndEnqueueTask(JobPriority::Default, actor,
[=](AsyncContext *context) {
EXPECT_PROGRESS(0);
return context->ResumeParent(context);
});
createAndEnqueueTask(JobPriority::Utility, actor,
[=](AsyncContext *context) {
EXPECT_PROGRESS(4);
return context->ResumeParent(context);
});
});
}