Skip to content

Commit 882568c

Browse files
committed
Directly call gettid syscall instead of library function
1 parent 1f206ec commit 882568c

File tree

3 files changed

+11
-9
lines changed

3 files changed

+11
-9
lines changed

stdlib/public/SwiftShims/swift/shims/SynchronizationShims.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323

2424
#define SWIFT_FUTEX_WAITERS FUTEX_WAITERS
2525

26+
static inline __swift_uint32_t _swift_stdlib_gettid() {
27+
return syscall(SYS_gettid);
28+
}
29+
2630
static inline __swift_bool _swift_stdlib_wait(__swift_uint32_t *lock) {
2731
return syscall(SYS_futex, lock, FUTEX_LOCK_PI_PRIVATE,
2832
/* val */ 0, // this value is ignored by this futex op

stdlib/public/Synchronization/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ add_swift_target_library(swiftSynchronization ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES
9898
${SWIFT_SYNCHRONIZATION_DARWIN_DEPENDENCIES}
9999
SWIFT_MODULE_DEPENDS_WATCHOS
100100
${SWIFT_SYNCHRONIZATION_DARWIN_DEPENDENCIES}
101+
SWIFT_MODULE_DEPENDS_WINDOWS
102+
${SWIFT_SYNCHRONIZATION_WINDOWS_DEPENDENCIES}
101103

102104
SWIFT_COMPILE_FLAGS
103105
${SWIFT_SYNCHRNOIZATION_SWIFT_FLAGS}

stdlib/public/Synchronization/Mutex/LinuxImpl.swift

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,6 @@
1212

1313
import SynchronizationShims
1414

15-
@_alwaysEmitIntoClient
16-
@_extern(c, "gettid")
17-
func _gettid() -> UInt32
18-
1915
extension Atomic where Value == UInt32 {
2016
borrowing func wait() {
2117
_swift_stdlib_wait(.init(rawAddress))
@@ -33,8 +29,8 @@ extension Atomic where Value == UInt32 {
3329
internal struct _MutexHandle: ~Copyable {
3430
// There are only 3 different values that storage can hold at a single time.
3531
// 0: unlocked
36-
// _gettid: locked, current thread's id (uncontended)
37-
// (_gettid | SWIFT_FUTEX_WAITERS): locked, current thread's id (contended)
32+
// TID: locked, current thread's id (uncontended)
33+
// (TID | SWIFT_FUTEX_WAITERS): locked, current thread's id (contended)
3834
@usableFromInline
3935
let storage: Atomic<UInt32>
4036

@@ -49,7 +45,7 @@ internal struct _MutexHandle: ~Copyable {
4945
@usableFromInline
5046
borrowing func lock() {
5147
// TODO: Is it worth caching this value in TLS?
52-
let selfId = _gettid()
48+
let selfId = _swift_stdlib_gettid()
5349

5450
// Note: We could probably merge this cas into a do/while style loop, but we
5551
// really want to perform the strong variant before attempting to do weak
@@ -97,7 +93,7 @@ internal struct _MutexHandle: ~Copyable {
9793
borrowing func tryLock() -> Bool {
9894
storage.compareExchange(
9995
expected: 0,
100-
desired: _gettid(),
96+
desired: _swift_stdlib_gettid(),
10197
successOrdering: .acquiring,
10298
failureOrdering: .relaxed
10399
).exchanged
@@ -107,7 +103,7 @@ internal struct _MutexHandle: ~Copyable {
107103
@usableFromInline
108104
borrowing func unlock() {
109105
// TODO: Is it worth caching this value in TLS?
110-
let selfId = _gettid()
106+
let selfId = _swift_stdlib_gettid()
111107

112108
// Attempt to release the lock. We can only atomically release the lock in
113109
// user-space when there are no other waiters. If there are waiters, the

0 commit comments

Comments
 (0)