Skip to content

Commit

Permalink
Windows TLS mocking support
Browse files Browse the repository at this point in the history
  • Loading branch information
milseman committed Oct 23, 2020
1 parent 78dd525 commit 52c8376
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions Sources/SystemInternals/Mocking.swift
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,26 @@ import Glibc
#endif

// TLS helper functions
#if !os(Windows)
#if os(Windows)
internal typealias TLSKey = DWORD
internal func makeTLSKey() -> TLSKey {
var raw: DWORD = FlsAlloc(nil)
if raw == FLS_OUT_OF_INDEXES {
fatalError("Unable to create key")
}
return raw
}
internal func setTLS(_ key: TLSKey, _ p: UnsafeMutableRawPointer?) {
guard 0 != FlsSetValue(key, p) else {
fatalError("Unable to set TLS")
}
}
internal func getTLS(_ key: TLSKey) -> UnsafeMutableRawPointer? {
FlsGetValue(key)
}

#else

internal typealias TLSKey = pthread_key_t
internal func makeTLSKey() -> TLSKey {
var raw = pthread_key_t()
Expand All @@ -109,9 +128,6 @@ internal func setTLS(_ key: TLSKey, _ p: UnsafeMutableRawPointer?) {
internal func getTLS(_ key: TLSKey) -> UnsafeMutableRawPointer? {
pthread_getspecific(key)
}
#else
// TODO: Windows version...
#error("Unsupported Platform")
#endif

private let driverKey: TLSKey = { makeTLSKey() }()
Expand Down

0 comments on commit 52c8376

Please sign in to comment.