Skip to content

Commit c50e12d

Browse files
committed
Support for 64-bit timespec seconds on 32-bit platforms
Modern 32-bit platforms (such as embedded Linux builds) support 64-bit timestamps in time_t and timespec.
1 parent f5339fb commit c50e12d

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

Sources/NIOConcurrencyHelpers/lock.swift

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -252,12 +252,16 @@ public final class ConditionLock<T: Equatable> {
252252
var curTime = timeval()
253253
gettimeofday(&curTime, nil)
254254

255+
#if canImport(wasi_pthread)
256+
typealias _Seconds = Int64
257+
#elseif canImport(Glibc)
258+
typealias _Seconds = time_t
259+
#else
260+
typealias _Seconds = Int
261+
#endif
262+
255263
let allNSecs: Int64 = timeoutNS + Int64(curTime.tv_usec) * 1000
256-
#if canImport(wasi_pthread)
257-
let tvSec = curTime.tv_sec + (allNSecs / nsecPerSec)
258-
#else
259-
let tvSec = curTime.tv_sec + Int((allNSecs / nsecPerSec))
260-
#endif
264+
let tvSec = curTime.tv_sec + _Seconds((allNSecs / nsecPerSec))
261265

262266
var timeoutAbs = timespec(
263267
tv_sec: tvSec,

Sources/NIOPosix/SelectorGeneric.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,18 @@ extension Optional {
4343
extension timespec {
4444
@inlinable
4545
init(timeAmount amount: TimeAmount) {
46+
#if canImport(wasi_pthread)
47+
typealias _Seconds = Int64
48+
#elseif canImport(Glibc)
49+
typealias _Seconds = time_t
50+
#else
51+
typealias _Seconds = Int
52+
#endif
53+
4654
let nsecPerSec: Int64 = 1_000_000_000
4755
let ns = amount.nanoseconds
4856
let sec = ns / nsecPerSec
49-
self = timespec(tv_sec: Int(sec), tv_nsec: Int(ns - sec * nsecPerSec))
57+
self = timespec(tv_sec: _Seconds(sec), tv_nsec: Int(ns - sec * nsecPerSec))
5058
}
5159
}
5260
#endif

0 commit comments

Comments
 (0)