@@ -379,15 +379,6 @@ bool swift_bincompat_useLegacyNonCrashingExecutorChecks() {
379
379
return legacyMode;
380
380
}
381
381
382
- // Check override of executor checking mode.
383
- static void checkIsCurrentExecutorMode (void *context) {
384
- bool useLegacyMode =
385
- swift_bincompat_useLegacyNonCrashingExecutorChecks ();
386
- auto checkMode = static_cast <IsCurrentExecutorCheckMode *>(context);
387
- *checkMode = useLegacyMode ? Legacy_NoCheckIsolated_NonCrashing
388
- : Swift6_UseCheckIsolated_AllowCrash;
389
- }
390
-
391
382
// Implemented in Swift to avoid some annoying hard-coding about
392
383
// TaskExecutor's protocol witness table. We could inline this
393
384
// with effort, though.
@@ -402,9 +393,16 @@ extern "C" SWIFT_CC(swift) void _swift_task_enqueueOnExecutor(
402
393
Job *job, HeapObject *executor, const Metadata *executorType,
403
394
const SerialExecutorWitnessTable *wtable);
404
395
396
+ namespace {
397
+ using SwiftTaskIsCurrentExecutorOptions =
398
+ OptionSet<swift_task_is_current_executor_flag>;
399
+ }
400
+
405
401
SWIFT_CC (swift)
406
- static bool isCurrentExecutor (SerialExecutorRef expectedExecutor,
407
- IsCurrentExecutorCheckMode checkMode) {
402
+ static bool swift_task_isCurrentExecutorWithFlagsImpl (
403
+ SerialExecutorRef expectedExecutor,
404
+ swift_task_is_current_executor_flag flags) {
405
+ auto options = SwiftTaskIsCurrentExecutorOptions (flags);
408
406
auto current = ExecutorTrackingInfo::current ();
409
407
410
408
if (!current) {
@@ -423,14 +421,14 @@ static bool isCurrentExecutor(SerialExecutorRef expectedExecutor,
423
421
424
422
// Otherwise, as last resort, let the expected executor check using
425
423
// external means, as it may "know" this thread is managed by it etc.
426
- if (checkMode == Swift6_UseCheckIsolated_AllowCrash ) {
424
+ if (options. contains (swift_task_is_current_executor_flag::Assert) ) {
427
425
swift_task_checkIsolated (expectedExecutor); // will crash if not same context
428
426
429
427
// checkIsolated did not crash, so we are on the right executor, after all!
430
428
return true ;
431
429
}
432
430
433
- assert (checkMode == Legacy_NoCheckIsolated_NonCrashing );
431
+ assert (!options. contains (swift_task_is_current_executor_flag::Assert) );
434
432
return false ;
435
433
}
436
434
@@ -461,7 +459,7 @@ static bool isCurrentExecutor(SerialExecutorRef expectedExecutor,
461
459
// the crashing 'dispatch_assert_queue(main queue)' which will either crash
462
460
// or confirm we actually are on the main queue; or the custom expected
463
461
// executor has a chance to implement a similar queue check.
464
- if (checkMode == Legacy_NoCheckIsolated_NonCrashing ) {
462
+ if (!options. contains (swift_task_is_current_executor_flag::Assert) ) {
465
463
if ((expectedExecutor.isMainExecutor () && !currentExecutor.isMainExecutor ()) ||
466
464
(!expectedExecutor.isMainExecutor () && currentExecutor.isMainExecutor ())) {
467
465
return false ;
@@ -522,7 +520,7 @@ static bool isCurrentExecutor(SerialExecutorRef expectedExecutor,
522
520
// Note that this only works because the closure in assumeIsolated is
523
521
// synchronous, and will not cause suspensions, as that would require the
524
522
// presence of a Task.
525
- if (checkMode == Swift6_UseCheckIsolated_AllowCrash ) {
523
+ if (options. contains (swift_task_is_current_executor_flag::Assert) ) {
526
524
swift_task_checkIsolated (expectedExecutor); // will crash if not same context
527
525
528
526
// The checkIsolated call did not crash, so we are on the right executor.
@@ -531,10 +529,20 @@ static bool isCurrentExecutor(SerialExecutorRef expectedExecutor,
531
529
532
530
// In the end, since 'checkIsolated' could not be used, so we must assume
533
531
// that the executors are not the same context.
534
- assert (checkMode == Legacy_NoCheckIsolated_NonCrashing );
532
+ assert (!options. contains (swift_task_is_current_executor_flag::Assert) );
535
533
return false ;
536
534
}
537
535
536
+ // Check override of executor checking mode.
537
+ static void swift_task_setDefaultExecutorCheckingFlags (void *context) {
538
+ bool useLegacyMode = swift_bincompat_useLegacyNonCrashingExecutorChecks ();
539
+ auto checkMode = static_cast <swift_task_is_current_executor_flag *>(context);
540
+ if (!useLegacyMode) {
541
+ *checkMode = swift_task_is_current_executor_flag (
542
+ *checkMode | swift_task_is_current_executor_flag::Assert);
543
+ }
544
+ }
545
+
538
546
SWIFT_CC (swift)
539
547
static bool
540
548
swift_task_isCurrentExecutorImpl (SerialExecutorRef expectedExecutor) {
@@ -546,11 +554,14 @@ swift_task_isCurrentExecutorImpl(SerialExecutorRef expectedExecutor) {
546
554
// instead must call into 'checkIsolated' or crash directly.
547
555
//
548
556
// Whenever we confirm an executor equality, we can return true, in any mode.
549
- static IsCurrentExecutorCheckMode checkMode;
550
- static swift::once_t checkModeToken;
551
- swift::once (checkModeToken, checkIsCurrentExecutorMode, &checkMode);
557
+ static swift_task_is_current_executor_flag isCurrentExecutorFlag;
558
+ static swift::once_t isCurrentExecutorFlagToken;
559
+ swift::once (isCurrentExecutorFlagToken,
560
+ swift_task_setDefaultExecutorCheckingFlags,
561
+ &isCurrentExecutorFlag);
552
562
553
- return isCurrentExecutor (expectedExecutor, checkMode);
563
+ return swift_task_isCurrentExecutorWithFlags (expectedExecutor,
564
+ isCurrentExecutorFlag);
554
565
}
555
566
556
567
// / Logging level for unexpected executors:
@@ -2337,11 +2348,8 @@ static void swift_task_deinitOnExecutorImpl(void *object,
2337
2348
//
2338
2349
// Note that isCurrentExecutor() returns true for @MainActor
2339
2350
// when running on the main thread without any executor.
2340
- //
2341
- // We always use "legacy" checking mode here, because that's the desired
2342
- // behaviour for this use case. This does not change with SDK version or
2343
- // language mode.
2344
- if (isCurrentExecutor (newExecutor, Legacy_NoCheckIsolated_NonCrashing)) {
2351
+ if (swift_task_isCurrentExecutorWithFlags (
2352
+ newExecutor, swift_task_is_current_executor_flag::None)) {
2345
2353
return work (object); // 'return' forces tail call
2346
2354
}
2347
2355
0 commit comments