File: Clock.swift

package info (click to toggle)
swiftlang 6.2.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,856,264 kB
  • sloc: cpp: 9,995,718; ansic: 2,234,019; asm: 1,092,167; python: 313,940; objc: 82,726; f90: 80,126; lisp: 38,373; pascal: 25,580; sh: 20,378; ml: 5,058; perl: 4,751; makefile: 4,725; awk: 3,535; javascript: 3,018; xml: 918; fortran: 664; cs: 573; ruby: 396
file content (121 lines) | stat: -rw-r--r-- 3,065 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#if SYSTEM_PACKAGE_DARWIN
import Darwin
#elseif canImport(Glibc)
import CSystem
import Glibc
#elseif canImport(Musl)
import CSystem
import Musl
#elseif canImport(Android)
import CSystem
import Android
#elseif os(Windows)
import CSystem
import ucrt
#else
#error("Unsupported Platform")
#endif

import SystemPackage

#if !os(Windows)

@frozen
public struct Clock: RawRepresentable {

  @_alwaysEmitIntoClient
  public var rawValue: CInterop.ClockId

  @_alwaysEmitIntoClient
  public init(rawValue: CInterop.ClockId) { self.rawValue = rawValue }
}

extension Clock {
  #if os(Linux) || os(Android)
  @_alwaysEmitIntoClient
  public static var boottime: Clock { Clock(rawValue: CLOCK_BOOTTIME) }
  #endif

  #if SYSTEM_PACKAGE_DARWIN
  @_alwaysEmitIntoClient
  public static var rawMonotonic: Clock { Clock(rawValue: _CLOCK_MONOTONIC_RAW) }
  #endif

  #if SYSTEM_PACKAGE_DARWIN || os(Linux) || os(Android) || os(OpenBSD) || os(FreeBSD) || os(WASI)
  @_alwaysEmitIntoClient
  public static var monotonic: Clock { Clock(rawValue: _CLOCK_MONOTONIC) }
  #endif

  #if os(OpenBSD) || os(FreeBSD) || os(WASI)
  @_alwaysEmitIntoClient
  public static var uptime: Clock { Clock(rawValue: _CLOCK_UPTIME) }
  #endif

  #if SYSTEM_PACKAGE_DARWIN
  @_alwaysEmitIntoClient
  public static var rawUptime: Clock { Clock(rawValue: _CLOCK_UPTIME_RAW) }
  #endif

  @_alwaysEmitIntoClient
  public func currentTime() throws -> Clock.TimeSpec {
    try _currentTime().get()
  }

  @usableFromInline
  internal func _currentTime() -> Result<Clock.TimeSpec, Errno> {
    var timeSpec = CInterop.TimeSpec()
    return nothingOrErrno(retryOnInterrupt: false) {
      system_clock_gettime(self.rawValue, &timeSpec)
    }
    .map { Clock.TimeSpec(rawValue: timeSpec) }
  }

  @_alwaysEmitIntoClient
  public func resolution() throws -> Clock.TimeSpec {
    try _resolution().get()
  }

  @usableFromInline
  internal func _resolution() -> Result<Clock.TimeSpec, Errno> {
    var timeSpec = CInterop.TimeSpec()
    return nothingOrErrno(retryOnInterrupt: false) {
      system_clock_getres(self.rawValue, &timeSpec)
    }
    .map { Clock.TimeSpec(rawValue: timeSpec) }
  }
}

extension Clock {
  @frozen
  public struct TimeSpec: RawRepresentable {
    @_alwaysEmitIntoClient
    public var rawValue: CInterop.TimeSpec

    @_alwaysEmitIntoClient
    public var seconds: Int { rawValue.tv_sec }

    @_alwaysEmitIntoClient
    public var nanoseconds: Int { rawValue.tv_nsec }

    @_alwaysEmitIntoClient
    public init(rawValue: CInterop.TimeSpec) {
      self.rawValue = rawValue
    }

    @_alwaysEmitIntoClient
    public init(seconds: Int, nanoseconds: Int) {
      self.init(rawValue: CInterop.TimeSpec(tv_sec: seconds, tv_nsec: nanoseconds))
    }

    @_alwaysEmitIntoClient
    public static var now: TimeSpec {
      return TimeSpec(rawValue: CInterop.TimeSpec(tv_sec: 0, tv_nsec: Int(_UTIME_NOW)))
    }

    @_alwaysEmitIntoClient
    public static var omit: TimeSpec {
      return TimeSpec(rawValue: CInterop.TimeSpec(tv_sec: 0, tv_nsec: Int(_UTIME_OMIT)))
    }
  }
}
#endif