Skip to content

Commit 1f206ec

Browse files
committed
Attempt to release the lock and wake when we can't
1 parent 2a60c88 commit 1f206ec

File tree

2 files changed

+21
-14
lines changed

2 files changed

+21
-14
lines changed

stdlib/cmake/modules/AddSwiftStdlib.cmake

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1830,34 +1830,34 @@ endfunction()
18301830
# Sources that are built when this library is being built for iOS
18311831
#
18321832
# SWIFT_SOURCES_DEPENDS_TVOS
1833-
# Sources that are built when this library is being built for macOS
1833+
# Sources that are built when this library is being built for tvOS
18341834
#
18351835
# SWIFT_SOURCES_DEPENDS_WATCHOS
1836-
# Sources that are built when this library is being built for macOS
1836+
# Sources that are built when this library is being built for watchOS
18371837
#
18381838
# SWIFT_SOURCES_DEPENDS_FREESTANDING
1839-
# Sources that are built when this library is being built for macOS
1839+
# Sources that are built when this library is being built for freestanding
18401840
#
18411841
# SWIFT_SOURCES_DEPENDS_FREEBSD
1842-
# Sources that are built when this library is being built for macOS
1842+
# Sources that are built when this library is being built for FreeBSD
18431843
#
18441844
# SWIFT_SOURCES_DEPENDS_OPENBSD
1845-
# Sources that are built when this library is being built for macOS
1845+
# Sources that are built when this library is being built for OpenBSD
18461846
#
18471847
# SWIFT_SOURCES_DEPENDS_LINUX
1848-
# Sources that are built when this library is being built for macOS
1848+
# Sources that are built when this library is being built for Linux
18491849
#
18501850
# SWIFT_SOURCES_DEPENDS_CYGWIN
1851-
# Sources that are built when this library is being built for macOS
1851+
# Sources that are built when this library is being built for Cygwin
18521852
#
18531853
# SWIFT_SOURCES_DEPENDS_HAIKU
1854-
# Sources that are built when this library is being built for macOS
1854+
# Sources that are built when this library is being built for Haiku
18551855
#
18561856
# SWIFT_SOURCES_DEPENDS_WASI
1857-
# Sources that are built when this library is being built for macOS
1857+
# Sources that are built when this library is being built for WASI
18581858
#
18591859
# SWIFT_SOURCES_DEPENDS_WINDOWS
1860-
# Sources that are built when this library is being built for macOS
1860+
# Sources that are built when this library is being built for Windows
18611861
#
18621862
# source1 ...
18631863
# Sources to add into this library.

stdlib/public/Synchronization/Mutex/LinuxImpl.swift

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,18 @@ internal struct _MutexHandle: ~Copyable {
108108
borrowing func unlock() {
109109
// TODO: Is it worth caching this value in TLS?
110110
let selfId = _gettid()
111-
// Unconditionally update the storage to be unlocked. At this point, the
112-
// lock is acquirable.
113-
let oldValue = storage.exchange(0, ordering: .releasing)
114111

115-
if _fastPath(oldValue == selfId) {
112+
// Attempt to release the lock. We can only atomically release the lock in
113+
// user-space when there are no other waiters. If there are waiters, the
114+
// waiter bit is set and we need to inform the kernel that we're unlocking.
115+
let (exchanged, oldValue) = storage.compareExchange(
116+
expected: selfId,
117+
desired: 0,
118+
successOrdering: .releasing,
119+
failureOrdering: .relaxed
120+
)
121+
122+
if _fastPath(exchanged) {
116123
// No waiters, unlocked!
117124
return
118125
}

0 commit comments

Comments
 (0)