@@ -279,8 +279,16 @@ class TaskGroupImpl: public TaskGroupTaskStatusRecord {
279
279
280
280
private:
281
281
282
+ #if !SWIFT_STDLIB_SINGLE_THREADED_RUNTIME
282
283
// TODO: move to lockless via the status atomic (make readyQueue an mpsc_queue_t<ReadyQueueItem>)
283
- 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 {}
291
+ #endif
284
292
285
293
// / Used for queue management, counting number of waiting and ready tasks
286
294
std::atomic <uint64_t > status;
@@ -557,7 +565,7 @@ void TaskGroupImpl::offer(AsyncTask *completedTask, AsyncContext *context) {
557
565
assert (completedTask->groupChildFragment ()->getGroup () == asAbstract (this ));
558
566
SWIFT_TASK_DEBUG_LOG (" offer task %p to group %p" , completedTask, this );
559
567
560
- mutex. lock (); // TODO: remove fragment lock, and use status for synchronization
568
+ lock (); // TODO: remove fragment lock, and use status for synchronization
561
569
562
570
// Immediately increment ready count and acquire the status
563
571
// Examples:
@@ -595,7 +603,7 @@ void TaskGroupImpl::offer(AsyncTask *completedTask, AsyncContext *context) {
595
603
// Run the task.
596
604
auto result = PollResult::get (completedTask, hadErrorResult);
597
605
598
- mutex. unlock (); // TODO: remove fragment lock, and use status for synchronization
606
+ unlock (); // TODO: remove fragment lock, and use status for synchronization
599
607
600
608
auto waitingContext =
601
609
static_cast <TaskFutureWaitAsyncContext *>(
@@ -635,7 +643,7 @@ void TaskGroupImpl::offer(AsyncTask *completedTask, AsyncContext *context) {
635
643
assert (completedTask == readyItem.getTask ());
636
644
assert (readyItem.getTask ()->isFuture ());
637
645
readyQueue.enqueue (readyItem);
638
- mutex. unlock (); // TODO: remove fragment lock, and use status for synchronization
646
+ unlock (); // TODO: remove fragment lock, and use status for synchronization
639
647
return ;
640
648
}
641
649
@@ -720,7 +728,7 @@ static void swift_taskGroup_wait_next_throwingImpl(
720
728
}
721
729
722
730
PollResult TaskGroupImpl::poll (AsyncTask *waitingTask) {
723
- mutex. lock (); // TODO: remove group lock, and use status for synchronization
731
+ lock (); // TODO: remove group lock, and use status for synchronization
724
732
SWIFT_TASK_DEBUG_LOG (" poll group = %p" , this );
725
733
auto assumed = statusMarkWaitingAssumeAcquire ();
726
734
@@ -738,7 +746,7 @@ PollResult TaskGroupImpl::poll(AsyncTask *waitingTask) {
738
746
statusRemoveWaiting ();
739
747
result.status = PollStatus::Empty;
740
748
result.successType = this ->successType ;
741
- mutex. unlock (); // TODO: remove group lock, and use status for synchronization
749
+ unlock (); // TODO: remove group lock, and use status for synchronization
742
750
return result;
743
751
}
744
752
@@ -785,7 +793,7 @@ PollResult TaskGroupImpl::poll(AsyncTask *waitingTask) {
785
793
result.successType = futureFragment->getResultType ();
786
794
assert (result.retainedTask && " polled a task, it must be not null" );
787
795
_swift_tsan_acquire (static_cast <Job *>(result.retainedTask ));
788
- mutex. unlock (); // TODO: remove fragment lock, and use status for synchronization
796
+ unlock (); // TODO: remove fragment lock, and use status for synchronization
789
797
return result;
790
798
791
799
case ReadyStatus::Error:
@@ -796,15 +804,15 @@ PollResult TaskGroupImpl::poll(AsyncTask *waitingTask) {
796
804
result.successType = nullptr ;
797
805
assert (result.retainedTask && " polled a task, it must be not null" );
798
806
_swift_tsan_acquire (static_cast <Job *>(result.retainedTask ));
799
- mutex. unlock (); // TODO: remove fragment lock, and use status for synchronization
807
+ unlock (); // TODO: remove fragment lock, and use status for synchronization
800
808
return result;
801
809
802
810
case ReadyStatus::Empty:
803
811
result.status = PollStatus::Empty;
804
812
result.storage = nullptr ;
805
813
result.retainedTask = nullptr ;
806
814
result.successType = this ->successType ;
807
- mutex. unlock (); // TODO: remove fragment lock, and use status for synchronization
815
+ unlock (); // TODO: remove fragment lock, and use status for synchronization
808
816
return result;
809
817
}
810
818
assert (false && " must return result when status compare-and-swap was successful" );
@@ -824,7 +832,7 @@ PollResult TaskGroupImpl::poll(AsyncTask *waitingTask) {
824
832
waitHead, waitingTask,
825
833
/* success*/ std::memory_order_release,
826
834
/* failure*/ std::memory_order_acquire)) {
827
- mutex. unlock (); // TODO: remove fragment lock, and use status for synchronization
835
+ unlock (); // TODO: remove fragment lock, and use status for synchronization
828
836
// no ready tasks, so we must wait.
829
837
result.status = PollStatus::MustWait;
830
838
_swift_task_clearCurrent ();
0 commit comments