Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions stdlib/private/SwiftPrivateThreadExtras/ThreadBarriers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -139,26 +139,35 @@ public func _stdlib_thread_barrier_wait(
#elseif os(WASI)
// WASI environment has only a single thread
#else
if pthread_mutex_lock(barrier.pointee.mutex!) != 0 {
let mutex = barrier.pointee.mutex!
if pthread_mutex_lock(mutex) != 0 {
return -1
}
var shouldUnlockMutex = true
defer {
if shouldUnlockMutex {
_ = pthread_mutex_unlock(mutex)
}
}
#endif
barrier.pointee.numThreadsWaiting += 1
if barrier.pointee.numThreadsWaiting < barrier.pointee.count {
// Put the thread to sleep.
#if os(Windows)
if !SleepConditionVariableSRW(barrier.pointee.cond!, barrier.pointee.mutex!,
INFINITE, 0) {
ReleaseSRWLockExclusive(barrier.pointee.mutex!)
return -1
}
ReleaseSRWLockExclusive(barrier.pointee.mutex!)
#elseif os(WASI)
// WASI environment has a only single thread
#else
if pthread_cond_wait(barrier.pointee.cond!, barrier.pointee.mutex!) != 0 {
if pthread_cond_wait(barrier.pointee.cond!, mutex) != 0 {
return -1
}
if pthread_mutex_unlock(barrier.pointee.mutex!) != 0 {
shouldUnlockMutex = false
if pthread_mutex_unlock(mutex) != 0 {
return -1
}
#endif
Expand All @@ -174,10 +183,11 @@ public func _stdlib_thread_barrier_wait(
#elseif os(WASI)
// WASI environment has a only single thread
#else
if pthread_cond_broadcast(barrier.pointee.cond!) != 0 {
if pthread_cond_broadcast(barrier.pointee.cond!, mutex) != 0 {
return -1
}
if pthread_mutex_unlock(barrier.pointee.mutex!) != 0 {
shouldUnlockMutex = false
if pthread_mutex_unlock(mutex) != 0 {
return -1
}
#endif
Expand Down