Skip to content

Commit ebf01c9

Browse files
authored
Merge pull request swiftlang#32593 from compnerd/dangling
SwiftPrivateThreadExtras: correct misuse of API
2 parents bbbe67e + 364981f commit ebf01c9

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

stdlib/private/SwiftPrivateThreadExtras/SwiftPrivateThreadExtras.swift

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -117,17 +117,18 @@ public func _stdlib_thread_join<Result>(
117117
) -> (CInt, Result?) {
118118
#if os(Windows)
119119
let result = WaitForSingleObject(thread, INFINITE)
120-
if result == WAIT_OBJECT_0 {
121-
var threadResult: DWORD = 0
122-
GetExitCodeThread(thread, &threadResult)
123-
CloseHandle(thread)
124-
125-
return (CInt(result),
126-
UnsafeMutablePointer<DWORD>(&threadResult)
127-
.withMemoryRebound(to: Result.self, capacity: 1){ $0.pointee })
128-
} else {
129-
return (CInt(result), nil)
120+
guard result == WAIT_OBJECT_0 else { return (CInt(result), nil) }
121+
122+
var dwResult: DWORD = 0
123+
GetExitCodeThread(thread, &dwResult)
124+
CloseHandle(thread)
125+
126+
let value: Result = withUnsafePointer(to: &dwResult) {
127+
$0.withMemoryRebound(to: Result.self, capacity: 1) {
128+
$0.pointee
129+
}
130130
}
131+
return (CInt(result), value)
131132
#else
132133
var threadResultRawPtr: UnsafeMutableRawPointer?
133134
let result = pthread_join(thread, &threadResultRawPtr)

0 commit comments

Comments
 (0)