-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathCompatibilityOverrideConcurrency.cpp
386 lines (311 loc) · 12.2 KB
/
CompatibilityOverrideConcurrency.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
//===--- CompatibilityOverrideConcurrency.cpp - Compatibility override tests ---===//
//
// 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
//
//===----------------------------------------------------------------------===//
#if defined(__APPLE__) && defined(__MACH__)
#define SWIFT_TARGET_LIBRARY_NAME swift_Concurrency
#include "../../stdlib/public/CompatibilityOverride/CompatibilityOverride.h"
#include "swift/Runtime/Concurrency.h"
#include "gtest/gtest.h"
#if __has_include("pthread.h")
#define RUN_ASYNC_MAIN_DRAIN_QUEUE_TEST 1
#include <pthread.h>
#endif // HAVE_PTHREAD_H
#include <stdio.h>
using namespace swift;
static bool EnableOverride;
static bool Ran;
namespace {
template <typename T>
T getEmptyValue() {
return T();
}
template <>
SerialExecutorRef getEmptyValue() {
return SerialExecutorRef::generic();
}
} // namespace
#define OVERRIDE(name, ret, attrs, ccAttrs, namespace, typedArgs, namedArgs) \
static ccAttrs ret name##Override(COMPATIBILITY_UNPAREN_WITH_COMMA( \
typedArgs) Original_##name originalImpl) { \
if (!EnableOverride) \
return originalImpl COMPATIBILITY_PAREN(namedArgs); \
Ran = true; \
return getEmptyValue<ret>(); \
}
#define OVERRIDE_TASK_NORETURN(name, attrs, ccAttrs, namespace, typedArgs, \
namedArgs) \
static ccAttrs void name##Override(COMPATIBILITY_UNPAREN_WITH_COMMA( \
typedArgs) Original_##name originalImpl) { \
if (!EnableOverride) \
originalImpl COMPATIBILITY_PAREN(namedArgs); \
Ran = true; \
}
#include "../../stdlib/public/CompatibilityOverride/CompatibilityOverrideConcurrency.def"
struct OverrideSection {
uintptr_t version;
#define OVERRIDE(name, ret, attrs, ccAttrs, namespace, typedArgs, namedArgs) \
Override_##name name;
#include "../../stdlib/public/CompatibilityOverride/CompatibilityOverrideConcurrency.def"
};
OverrideSection ConcurrencyOverrides
__attribute__((section("__DATA," COMPATIBILITY_OVERRIDE_SECTION_NAME_swift_Concurrency))) = {
0,
#define OVERRIDE(name, ret, attrs, ccAttrs, namespace, typedArgs, namedArgs) \
name##Override,
#include "../../stdlib/public/CompatibilityOverride/CompatibilityOverrideConcurrency.def"
};
SWIFT_CC(swift)
static void
swift_task_enqueueGlobal_override(Job *job,
swift_task_enqueueGlobal_original original) {
Ran = true;
}
SWIFT_CC(swift)
static void
swift_task_checkIsolated_override(SerialExecutorRef executor,
swift_task_checkIsolated_original original) {
Ran = true;
}
SWIFT_CC(swift)
static bool
swift_task_isIsolatingCurrentContext_override(SerialExecutorRef executor,
swift_task_isIsolatingCurrentContext_original original) {
Ran = true;
return true;
}
SWIFT_CC(swift)
static void swift_task_enqueueGlobalWithDelay_override(
unsigned long long delay, Job *job,
swift_task_enqueueGlobalWithDelay_original original) {
Ran = true;
}
SWIFT_CC(swift)
static void swift_task_enqueueMainExecutor_override(
Job *job, swift_task_enqueueMainExecutor_original original) {
Ran = true;
}
SWIFT_CC(swift)
static void swift_task_startOnMainActor_override(AsyncTask* task) {
Ran = true;
}
SWIFT_CC(swift)
static void swift_task_startSynchronously_override(AsyncTask* task) {
Ran = true;
}
#ifdef RUN_ASYNC_MAIN_DRAIN_QUEUE_TEST
[[noreturn]] SWIFT_CC(swift)
static void swift_task_asyncMainDrainQueue_override_fn(
swift_task_asyncMainDrainQueue_original original,
swift_task_asyncMainDrainQueue_override compatOverride) {
Ran = true;
pthread_exit(nullptr); // noreturn function
}
#endif
class CompatibilityOverrideConcurrencyTest : public ::testing::Test {
protected:
virtual void SetUp() {
// This check is a bit pointless, as long as you trust the section
// attribute to work correctly, but it ensures that the override
// section actually gets linked into the test executable. If it's not
// used then it disappears and the real tests begin to fail.
ASSERT_EQ(ConcurrencyOverrides.version, static_cast<uintptr_t>(0));
EnableOverride = true;
Ran = false;
// Executor hooks need to be set manually.
swift_task_enqueueGlobal_hook = swift_task_enqueueGlobal_override;
swift_task_enqueueGlobalWithDelay_hook =
swift_task_enqueueGlobalWithDelay_override;
swift_task_enqueueMainExecutor_hook =
swift_task_enqueueMainExecutor_override;
swift_task_checkIsolated_hook =
swift_task_checkIsolated_override;
swift_task_isIsolatingCurrentContext_hook =
swift_task_isIsolatingCurrentContext_override;
#ifdef RUN_ASYNC_MAIN_DRAIN_QUEUE_TEST
swift_task_asyncMainDrainQueue_hook =
swift_task_asyncMainDrainQueue_override_fn;
#endif
}
virtual void TearDown() {
EnableOverride = false;
ASSERT_TRUE(Ran);
}
};
static Job fakeJob{{JobKind::DefaultActorInline},
static_cast<JobInvokeFunction *>(nullptr),
nullptr};
TEST_F(CompatibilityOverrideConcurrencyTest, test_swift_task_enqueue) {
swift_task_enqueue(&fakeJob, SerialExecutorRef::generic());
}
TEST_F(CompatibilityOverrideConcurrencyTest, test_swift_job_run) {
swift_job_run(&fakeJob, SerialExecutorRef::generic());
}
TEST_F(CompatibilityOverrideConcurrencyTest,
test_swift_job_run_on_task_executor) {
swift_job_run_on_task_executor(&fakeJob, TaskExecutorRef::undefined());
}
TEST_F(CompatibilityOverrideConcurrencyTest,
test_swift_job_run_on_serial_and_task_executor) {
swift_job_run_on_serial_and_task_executor(
&fakeJob, SerialExecutorRef::generic(), TaskExecutorRef::undefined());
}
TEST_F(CompatibilityOverrideConcurrencyTest, test_swift_task_getCurrentExecutor) {
swift_task_getCurrentExecutor();
}
TEST_F(CompatibilityOverrideConcurrencyTest, test_swift_task_switch) {
swift_task_switch(nullptr, nullptr, SerialExecutorRef::generic());
}
TEST_F(CompatibilityOverrideConcurrencyTest, test_swift_task_enqueueGlobal) {
swift_task_enqueueGlobal(&fakeJob);
}
TEST_F(CompatibilityOverrideConcurrencyTest,
test_swift_task_enqueueGlobalWithDelay) {
swift_task_enqueueGlobalWithDelay(0, &fakeJob);
}
TEST_F(CompatibilityOverrideConcurrencyTest,
test_swift_task_checkIsolated) {
swift_task_checkIsolated(SerialExecutorRef::generic());
}
TEST_F(CompatibilityOverrideConcurrencyTest,
test_swift_task_isIsolatingCurrentContext) {
swift_task_isIsolatingCurrentContext(SerialExecutorRef::generic());
}
TEST_F(CompatibilityOverrideConcurrencyTest,
test_swift_task_enqueueMainExecutor) {
swift_task_enqueueMainExecutor(&fakeJob);
}
TEST_F(CompatibilityOverrideConcurrencyTest, test_swift_task_create_common) {
swift_task_create_common(0, nullptr, nullptr, nullptr, nullptr, 0);
}
TEST_F(CompatibilityOverrideConcurrencyTest, test_swift_task_future_wait) {
swift_task_future_wait(nullptr, nullptr, nullptr, nullptr, nullptr);
}
TEST_F(CompatibilityOverrideConcurrencyTest,
test_swift_task_future_wait_throwing) {
swift_task_future_wait_throwing(nullptr, nullptr, nullptr, nullptr, nullptr);
}
TEST_F(CompatibilityOverrideConcurrencyTest, test_swift_continuation_resume) {
swift_continuation_resume(nullptr);
}
TEST_F(CompatibilityOverrideConcurrencyTest,
test_swift_continuation_throwingResume) {
swift_continuation_throwingResume(nullptr);
}
TEST_F(CompatibilityOverrideConcurrencyTest,
test_swift_continuation_throwingResumeWithError) {
swift_continuation_throwingResumeWithError(nullptr, nullptr);
}
TEST_F(CompatibilityOverrideConcurrencyTest, test_swift_asyncLet_wait) {
swift_asyncLet_wait(nullptr, nullptr, nullptr, nullptr, nullptr);
}
TEST_F(CompatibilityOverrideConcurrencyTest, test_swift_asyncLet_wait_throwing) {
swift_asyncLet_wait(nullptr, nullptr, nullptr, nullptr, nullptr);
}
TEST_F(CompatibilityOverrideConcurrencyTest, test_swift_asyncLet_end) {
swift_asyncLet_end(nullptr);
}
TEST_F(CompatibilityOverrideConcurrencyTest, test_swift_taskGroup_initialize) {
swift_taskGroup_initialize(nullptr, nullptr);
}
TEST_F(CompatibilityOverrideConcurrencyTest, test_swift_taskGroup_initializeWithFlags) {
swift_taskGroup_initializeWithFlags(0, nullptr, nullptr);
}
TEST_F(CompatibilityOverrideConcurrencyTest, test_swift_taskGroup_attachChild) {
swift_taskGroup_attachChild(nullptr, nullptr);
}
TEST_F(CompatibilityOverrideConcurrencyTest, test_swift_taskGroup_destroy) {
swift_taskGroup_destroy(nullptr);
}
TEST_F(CompatibilityOverrideConcurrencyTest,
test_swift_taskGroup_wait_next_throwing) {
swift_taskGroup_wait_next_throwing(nullptr, nullptr, nullptr, nullptr,
nullptr);
}
TEST_F(CompatibilityOverrideConcurrencyTest, test_swift_taskGroup_isEmpty) {
swift_taskGroup_isEmpty(nullptr);
}
TEST_F(CompatibilityOverrideConcurrencyTest, test_taskGroup_isCancelled) {
swift_taskGroup_isCancelled(nullptr);
}
TEST_F(CompatibilityOverrideConcurrencyTest, test_swift_taskGroup_cancelAll) {
swift_taskGroup_cancelAll(nullptr);
}
TEST_F(CompatibilityOverrideConcurrencyTest, test_swift_taskGroup_waitAll) {
swift_taskGroup_waitAll(nullptr, nullptr, nullptr, nullptr, nullptr, nullptr);
}
TEST_F(CompatibilityOverrideConcurrencyTest, test_swift_taskGroup_addPending) {
swift_taskGroup_addPending(nullptr, true);
}
TEST_F(CompatibilityOverrideConcurrencyTest, test_swift_task_localValuePush) {
swift_task_localValuePush(nullptr, nullptr, nullptr);
}
TEST_F(CompatibilityOverrideConcurrencyTest, test_swift_task_localValueGet) {
swift_task_localValueGet(nullptr);
}
TEST_F(CompatibilityOverrideConcurrencyTest, test_swift_task_localValuePop) {
swift_task_localValuePop();
}
TEST_F(CompatibilityOverrideConcurrencyTest, test_swift_task_localsCopyTo) {
swift_task_localsCopyTo(nullptr);
}
TEST_F(CompatibilityOverrideConcurrencyTest, task_hasTaskGroupStatusRecord) {
swift_task_hasTaskGroupStatusRecord();
}
TEST_F(CompatibilityOverrideConcurrencyTest, task_getPreferredTaskExecutor) {
swift_task_getPreferredTaskExecutor();
}
TEST_F(CompatibilityOverrideConcurrencyTest, task_pushTaskExecutorPreference) {
swift_task_pushTaskExecutorPreference(TaskExecutorRef::undefined());
}
TEST_F(CompatibilityOverrideConcurrencyTest, task_popTaskExecutorPreference) {
swift_task_popTaskExecutorPreference(nullptr);
}
TEST_F(CompatibilityOverrideConcurrencyTest, test_swift_task_cancel) {
swift_task_cancel(nullptr);
}
TEST_F(CompatibilityOverrideConcurrencyTest, test_swift_task_cancel_group_child_tasks) {
swift_task_cancel_group_child_tasks(nullptr);
}
TEST_F(CompatibilityOverrideConcurrencyTest, test_swift_task_escalate) {
swift_task_escalate(nullptr, {});
}
TEST_F(CompatibilityOverrideConcurrencyTest, test_swift_startOnMainActorImpl) {
swift_task_startOnMainActor(nullptr);
}
TEST_F(CompatibilityOverrideConcurrencyTest, test_swift_startSynchronously) {
swift_task_startSynchronously(nullptr);
}
TEST_F(CompatibilityOverrideConcurrencyTest,
test_swift_task_isCurrentExecutorWithFlags) {
swift_task_isCurrentExecutorWithFlags(
swift_task_getMainExecutor(), swift_task_is_current_executor_flag::None);
}
#if RUN_ASYNC_MAIN_DRAIN_QUEUE_TEST
TEST_F(CompatibilityOverrideConcurrencyTest, test_swift_task_asyncMainDrainQueue) {
auto runner = [](void *) -> void * {
swift_task_asyncMainDrainQueue();
return nullptr;
};
int ret = 0;
pthread_t thread;
pthread_attr_t attrs;
ret = pthread_attr_init(&attrs);
ASSERT_EQ(ret, 0);
ret = pthread_create(&thread, &attrs, runner, nullptr);
ASSERT_EQ(ret, 0);
void * result = nullptr;
ret = pthread_join(thread, &result);
ASSERT_EQ(ret, 0);
pthread_attr_destroy(&attrs);
ASSERT_EQ(ret, 0);
}
#endif
#endif