Skip to content

Commit e08fd33

Browse files
committed
[Runtime] Tidy up locking/unlocking conditionals.
Rather than conditionalising all the calls to mutex.lock() and mutex.unlock(), centralise the logic. rdar://84393438
1 parent 2d33506 commit e08fd33

File tree

1 file changed

+16
-28
lines changed

1 file changed

+16
-28
lines changed

stdlib/public/Concurrency/TaskGroup.cpp

Lines changed: 16 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,13 @@ class TaskGroupImpl: public TaskGroupTaskStatusRecord {
281281

282282
#if !SWIFT_STDLIB_SINGLE_THREADED_RUNTIME
283283
// 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 {}
285291
#endif
286292

287293
/// Used for queue management, counting number of waiting and ready tasks
@@ -561,9 +567,7 @@ void TaskGroupImpl::offer(AsyncTask *completedTask, AsyncContext *context) {
561567
assert(completedTask->groupChildFragment()->getGroup() == asAbstract(this));
562568
SWIFT_TASK_DEBUG_LOG("offer task %p to group %p", completedTask, this);
563569

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
567571

568572
// Immediately increment ready count and acquire the status
569573
// Examples:
@@ -601,9 +605,7 @@ void TaskGroupImpl::offer(AsyncTask *completedTask, AsyncContext *context) {
601605
// Run the task.
602606
auto result = PollResult::get(completedTask, hadErrorResult);
603607

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
607609

608610
auto waitingContext =
609611
static_cast<TaskFutureWaitAsyncContext *>(
@@ -643,9 +645,7 @@ void TaskGroupImpl::offer(AsyncTask *completedTask, AsyncContext *context) {
643645
assert(completedTask == readyItem.getTask());
644646
assert(readyItem.getTask()->isFuture());
645647
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
649649
return;
650650
}
651651

@@ -730,9 +730,7 @@ static void swift_taskGroup_wait_next_throwingImpl(
730730
}
731731

732732
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
736734
SWIFT_TASK_DEBUG_LOG("poll group = %p", this);
737735
auto assumed = statusMarkWaitingAssumeAcquire();
738736

@@ -750,9 +748,7 @@ PollResult TaskGroupImpl::poll(AsyncTask *waitingTask) {
750748
statusRemoveWaiting();
751749
result.status = PollStatus::Empty;
752750
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
756752
return result;
757753
}
758754

@@ -799,9 +795,7 @@ PollResult TaskGroupImpl::poll(AsyncTask *waitingTask) {
799795
result.successType = futureFragment->getResultType();
800796
assert(result.retainedTask && "polled a task, it must be not null");
801797
_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
805799
return result;
806800

807801
case ReadyStatus::Error:
@@ -812,19 +806,15 @@ PollResult TaskGroupImpl::poll(AsyncTask *waitingTask) {
812806
result.successType = nullptr;
813807
assert(result.retainedTask && "polled a task, it must be not null");
814808
_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
818810
return result;
819811

820812
case ReadyStatus::Empty:
821813
result.status = PollStatus::Empty;
822814
result.storage = nullptr;
823815
result.retainedTask = nullptr;
824816
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
828818
return result;
829819
}
830820
assert(false && "must return result when status compare-and-swap was successful");
@@ -844,9 +834,7 @@ PollResult TaskGroupImpl::poll(AsyncTask *waitingTask) {
844834
waitHead, waitingTask,
845835
/*success*/ std::memory_order_release,
846836
/*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
850838
// no ready tasks, so we must wait.
851839
result.status = PollStatus::MustWait;
852840
_swift_task_clearCurrent();

0 commit comments

Comments
 (0)