Skip to content

Commit ce40d72

Browse files
committed
stdlib: implement _stdlib_pthread_join in terms of Windows threading
Implement `_stdlib_pthread_join` in terms of windows threading. Additionally, rename it to `_stdlib_thread_join`.
1 parent 6bf7b9e commit ce40d72

File tree

4 files changed

+25
-7
lines changed

4 files changed

+25
-7
lines changed

stdlib/private/StdlibUnittest/RaceTest.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -626,14 +626,14 @@ public func runRaceTest<RT : RaceTestWithPerTrialData>(
626626

627627
// Join all testing threads.
628628
for tid in testTids {
629-
let (ret, _) = _stdlib_pthread_join(tid, Void.self)
629+
let (ret, _) = _stdlib_thread_join(tid, Void.self)
630630
expectEqual(0, ret)
631631
}
632632

633633
// Tell the alarm thread to stop if it hasn't already, then join it.
634634
do {
635635
alarmTimer.wake()
636-
let (ret, _) = _stdlib_pthread_join(alarmTid, Void.self)
636+
let (ret, _) = _stdlib_thread_join(alarmTid, Void.self)
637637
expectEqual(0, ret)
638638
}
639639

stdlib/private/SwiftPrivateThreadExtras/SwiftPrivateThreadExtras.swift

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import Darwin
2121
import Glibc
2222
#elseif os(Windows)
2323
import MSVCRT
24+
import WinSDK
2425
#endif
2526

2627
/// An abstract base class to encapsulate the context necessary to invoke
@@ -110,10 +111,26 @@ public func _stdlib_thread_create_block<Argument, Result>(
110111
}
111112

112113
/// Block-based wrapper for `pthread_join`.
113-
public func _stdlib_pthread_join<Result>(
114-
_ thread: pthread_t,
114+
public func _stdlib_thread_join<Result>(
115+
_ thread: ThreadHandle,
115116
_ resultType: Result.Type
116117
) -> (CInt, Result?) {
118+
#if os(Windows)
119+
// TODO(compnerd) modularize rpc.h for INFINITE (0xffffffff)
120+
let result = WaitForSingleObject(thread, 0xffffffff);
121+
// TODO(compnerd) modularize WinBase.h for WAIT_OBJECT_0 (0)
122+
if result == 0 {
123+
let threadResult: DWORD = 0
124+
GetExitCodeThread(thread, &threadResult)
125+
CloseHandle(thread)
126+
127+
return (result,
128+
UnsafeMutablePointer<DWORD>(&threadResult)
129+
.withMemoryRebound(to: Result.self, capacity: 1){ $0.pointee })
130+
} else {
131+
return (result, nil)
132+
}
133+
#else
117134
var threadResultRawPtr: UnsafeMutableRawPointer?
118135
let result = pthread_join(thread, &threadResultRawPtr)
119136
if result == 0 {
@@ -126,6 +143,7 @@ public func _stdlib_pthread_join<Result>(
126143
} else {
127144
return (result, nil)
128145
}
146+
#endif
129147
}
130148

131149
public class _stdlib_Barrier {

test/Interpreter/enforce_exclusive_access.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ ExclusiveAccessTestSuite.test("PerThreadEnforcement") {
125125
return ()
126126
}, ())
127127

128-
_ = _stdlib_pthread_join(otherThread!, Void.self)
128+
_ = _stdlib_thread_join(otherThread!, Void.self)
129129
}
130130
}
131131

validation-test/stdlib/StringSlicesConcurrentAppend.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,8 @@ StringTestSuite.test("SliceConcurrentAppend") {
101101
expectEqual(0, createRet1)
102102
expectEqual(0, createRet2)
103103

104-
let (joinRet1, _) = _stdlib_pthread_join(tid1!, Void.self)
105-
let (joinRet2, _) = _stdlib_pthread_join(tid2!, Void.self)
104+
let (joinRet1, _) = _stdlib_thread_join(tid1!, Void.self)
105+
let (joinRet2, _) = _stdlib_thread_join(tid2!, Void.self)
106106

107107
expectEqual(0, joinRet1)
108108
expectEqual(0, joinRet2)

0 commit comments

Comments
 (0)