@@ -281,7 +281,13 @@ class TaskGroupImpl: public TaskGroupTaskStatusRecord {
281
281
282
282
#if !SWIFT_STDLIB_SINGLE_THREADED_RUNTIME
283
283
// TODO: move to lockless via the status atomic (make readyQueue an mpsc_queue_t<ReadyQueueItem>)
284
- mutable std::mutex mutex;
284
+ mutable std::mutex mutex_;
285
+
286
+ void lock () const { mutex_.lock (); }
287
+ void unlock () const { mutex_.unlock (); }
288
+ #else
289
+ void lock () const {}
290
+ void unlock () const {}
285
291
#endif
286
292
287
293
// / Used for queue management, counting number of waiting and ready tasks
@@ -561,9 +567,7 @@ void TaskGroupImpl::offer(AsyncTask *completedTask, AsyncContext *context) {
561
567
assert (completedTask->groupChildFragment ()->getGroup () == asAbstract (this ));
562
568
SWIFT_TASK_DEBUG_LOG (" offer task %p to group %p" , completedTask, this );
563
569
564
- #if !SWIFT_STDLIB_SINGLE_THREADED_RUNTIME
565
- mutex.lock (); // TODO: remove fragment lock, and use status for synchronization
566
- #endif
570
+ lock (); // TODO: remove fragment lock, and use status for synchronization
567
571
568
572
// Immediately increment ready count and acquire the status
569
573
// Examples:
@@ -601,9 +605,7 @@ void TaskGroupImpl::offer(AsyncTask *completedTask, AsyncContext *context) {
601
605
// Run the task.
602
606
auto result = PollResult::get (completedTask, hadErrorResult);
603
607
604
- #if !SWIFT_STDLIB_SINGLE_THREADED_RUNTIME
605
- mutex.unlock (); // TODO: remove fragment lock, and use status for synchronization
606
- #endif
608
+ unlock (); // TODO: remove fragment lock, and use status for synchronization
607
609
608
610
auto waitingContext =
609
611
static_cast <TaskFutureWaitAsyncContext *>(
@@ -643,9 +645,7 @@ void TaskGroupImpl::offer(AsyncTask *completedTask, AsyncContext *context) {
643
645
assert (completedTask == readyItem.getTask ());
644
646
assert (readyItem.getTask ()->isFuture ());
645
647
readyQueue.enqueue (readyItem);
646
- #if !SWIFT_STDLIB_SINGLE_THREADED_RUNTIME
647
- mutex.unlock (); // TODO: remove fragment lock, and use status for synchronization
648
- #endif
648
+ unlock (); // TODO: remove fragment lock, and use status for synchronization
649
649
return ;
650
650
}
651
651
@@ -730,9 +730,7 @@ static void swift_taskGroup_wait_next_throwingImpl(
730
730
}
731
731
732
732
PollResult TaskGroupImpl::poll (AsyncTask *waitingTask) {
733
- #if !SWIFT_STDLIB_SINGLE_THREADED_RUNTIME
734
- mutex.lock (); // TODO: remove group lock, and use status for synchronization
735
- #endif
733
+ lock (); // TODO: remove group lock, and use status for synchronization
736
734
SWIFT_TASK_DEBUG_LOG (" poll group = %p" , this );
737
735
auto assumed = statusMarkWaitingAssumeAcquire ();
738
736
@@ -750,9 +748,7 @@ PollResult TaskGroupImpl::poll(AsyncTask *waitingTask) {
750
748
statusRemoveWaiting ();
751
749
result.status = PollStatus::Empty;
752
750
result.successType = this ->successType ;
753
- #if !SWIFT_STDLIB_SINGLE_THREADED_RUNTIME
754
- mutex.unlock (); // TODO: remove group lock, and use status for synchronization
755
- #endif
751
+ unlock (); // TODO: remove group lock, and use status for synchronization
756
752
return result;
757
753
}
758
754
@@ -799,9 +795,7 @@ PollResult TaskGroupImpl::poll(AsyncTask *waitingTask) {
799
795
result.successType = futureFragment->getResultType ();
800
796
assert (result.retainedTask && " polled a task, it must be not null" );
801
797
_swift_tsan_acquire (static_cast <Job *>(result.retainedTask ));
802
- #if !SWIFT_STDLIB_SINGLE_THREADED_RUNTIME
803
- mutex.unlock (); // TODO: remove fragment lock, and use status for synchronization
804
- #endif
798
+ unlock (); // TODO: remove fragment lock, and use status for synchronization
805
799
return result;
806
800
807
801
case ReadyStatus::Error:
@@ -812,19 +806,15 @@ PollResult TaskGroupImpl::poll(AsyncTask *waitingTask) {
812
806
result.successType = nullptr ;
813
807
assert (result.retainedTask && " polled a task, it must be not null" );
814
808
_swift_tsan_acquire (static_cast <Job *>(result.retainedTask ));
815
- #if !SWIFT_STDLIB_SINGLE_THREADED_RUNTIME
816
- mutex.unlock (); // TODO: remove fragment lock, and use status for synchronization
817
- #endif
809
+ unlock (); // TODO: remove fragment lock, and use status for synchronization
818
810
return result;
819
811
820
812
case ReadyStatus::Empty:
821
813
result.status = PollStatus::Empty;
822
814
result.storage = nullptr ;
823
815
result.retainedTask = nullptr ;
824
816
result.successType = this ->successType ;
825
- #if !SWIFT_STDLIB_SINGLE_THREADED_RUNTIME
826
- mutex.unlock (); // TODO: remove fragment lock, and use status for synchronization
827
- #endif
817
+ unlock (); // TODO: remove fragment lock, and use status for synchronization
828
818
return result;
829
819
}
830
820
assert (false && " must return result when status compare-and-swap was successful" );
@@ -844,9 +834,7 @@ PollResult TaskGroupImpl::poll(AsyncTask *waitingTask) {
844
834
waitHead, waitingTask,
845
835
/* success*/ std::memory_order_release,
846
836
/* failure*/ std::memory_order_acquire)) {
847
- #if !SWIFT_STDLIB_SINGLE_THREADED_RUNTIME
848
- mutex.unlock (); // TODO: remove fragment lock, and use status for synchronization
849
- #endif
837
+ unlock (); // TODO: remove fragment lock, and use status for synchronization
850
838
// no ready tasks, so we must wait.
851
839
result.status = PollStatus::MustWait;
852
840
_swift_task_clearCurrent ();
0 commit comments