|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2020 - 2021 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +import Swift |
| 14 | +@_implementationOnly import _SwiftConcurrencyShims |
| 15 | + |
| 16 | +@available(SwiftStdlib 5.1, *) |
| 17 | +@_silgen_name("swift_job_run") |
| 18 | +@usableFromInline |
| 19 | +internal func _swiftJobRun(_ job: UnownedJob, |
| 20 | + _ executor: UnownedSerialExecutor) -> () |
| 21 | + |
| 22 | +// ==== ----------------------------------------------------------------------- |
| 23 | +// MARK: UnownedJob |
| 24 | + |
| 25 | +/// A unit of scheduleable work. |
| 26 | +/// |
| 27 | +/// Unless you're implementing a scheduler, |
| 28 | +/// you don't generally interact with jobs directly. |
| 29 | +@available(SwiftStdlib 5.1, *) |
| 30 | +@frozen |
| 31 | +public struct UnownedJob: Sendable { |
| 32 | + internal var context: Builtin.Job |
| 33 | + |
| 34 | + @usableFromInline |
| 35 | + internal init(context: Builtin.Job) { |
| 36 | + self.context = context |
| 37 | + } |
| 38 | + |
| 39 | + @available(SwiftStdlib 5.9, *) |
| 40 | + public init(_ job: __owned Job) { |
| 41 | + self.context = job.context |
| 42 | + } |
| 43 | + |
| 44 | + /// The priority of this job. |
| 45 | + @available(SwiftStdlib 5.9, *) |
| 46 | + public var priority: JobPriority { |
| 47 | + let raw = _swift_concurrency_jobPriority(self) |
| 48 | + return JobPriority(rawValue: raw) |
| 49 | + } |
| 50 | + |
| 51 | + @_alwaysEmitIntoClient |
| 52 | + @inlinable |
| 53 | + @available(*, deprecated, renamed: "Job.runSynchronously(on:)") |
| 54 | + public func _runSynchronously(on executor: UnownedSerialExecutor) { |
| 55 | + _swiftJobRun(self, executor) |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | + |
| 60 | +/// A unit of scheduleable work. |
| 61 | +/// |
| 62 | +/// Unless you're implementing a scheduler, |
| 63 | +/// you don't generally interact with jobs directly. |
| 64 | +@available(SwiftStdlib 5.9, *) |
| 65 | +@frozen |
| 66 | +@_moveOnly |
| 67 | +public struct Job: Sendable { |
| 68 | + internal var context: Builtin.Job |
| 69 | + |
| 70 | + @usableFromInline |
| 71 | + internal init(context: __owned Builtin.Job) { |
| 72 | + self.context = context |
| 73 | + } |
| 74 | + |
| 75 | + public init(_ job: UnownedJob) { |
| 76 | + self.context = job.context |
| 77 | + } |
| 78 | + |
| 79 | + public var priority: JobPriority { |
| 80 | + // let raw = _swift_concurrency_jobPriority(UnownedJob(context: self.context)) // TODO: do we also surface this or the base priority? |
| 81 | + let raw = _taskCurrentPriority(context as! Builtin.NativeObject) |
| 82 | + return JobPriority(rawValue: raw) |
| 83 | + } |
| 84 | + |
| 85 | + // TODO: move only types cannot conform to protocols, so we can't conform to CustomStringConvertible; |
| 86 | + // we can still offer a description to be called explicitly though. |
| 87 | + public var description: String { |
| 88 | + let id = _getJobTaskId(UnownedJob(context: self.context)) |
| 89 | + /// Tasks are always assigned an unique ID, however some jobs may not have it set, |
| 90 | + /// and it appearing as 0 for _different_ jobs may lead to misunderstanding it as |
| 91 | + /// being "the same 0 id job", we specifically print 0 (id not set) as nil. |
| 92 | + if (id > 0) { |
| 93 | + return "\(Self.self)(id: \(id))" |
| 94 | + } else { |
| 95 | + return "\(Self.self)(id: nil)" |
| 96 | + } |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +@available(SwiftStdlib 5.9, *) |
| 101 | +extension Job { |
| 102 | + |
| 103 | + /// Run the job synchronously. |
| 104 | + /// |
| 105 | + /// This operation consumes the job, preventing it accidental use after it has ben run. |
| 106 | + /// |
| 107 | + /// Converting a `Job` to an `UnownedJob` and invoking `runSynchronously` on it multiple times is undefined behavior, |
| 108 | + /// as a job can only ever be run once, and must not be accessed after it has been run. |
| 109 | + @_alwaysEmitIntoClient |
| 110 | + @inlinable |
| 111 | + __consuming public func runSynchronously(on executor: some SerialExecutor) { |
| 112 | + _swiftJobRun(UnownedJob(self), UnownedSerialExecutor(ordinary: executor)) |
| 113 | + } |
| 114 | + |
| 115 | + /// Run the job synchronously. |
| 116 | + /// |
| 117 | + /// This operation consumes the job, preventing it accidental use after it has ben run. |
| 118 | + /// |
| 119 | + /// Converting a `Job` to an `UnownedJob` and invoking `runSynchronously` on it multiple times is undefined behavior, |
| 120 | + /// as a job can only ever be run once, and must not be accessed after it has been run. |
| 121 | + @_alwaysEmitIntoClient |
| 122 | + @inlinable |
| 123 | + __consuming public func runSynchronously(on executor: UnownedSerialExecutor) { |
| 124 | + _swiftJobRun(UnownedJob(self), executor) |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +// ==== ----------------------------------------------------------------------- |
| 129 | +// MARK: JobPriority |
| 130 | + |
| 131 | +/// The priority of this job. |
| 132 | +/// |
| 133 | +/// The executor determines how priority information affects the way tasks are scheduled. |
| 134 | +/// The behavior varies depending on the executor currently being used. |
| 135 | +/// Typically, executors attempt to run tasks with a higher priority |
| 136 | +/// before tasks with a lower priority. |
| 137 | +/// However, the semantics of how priority is treated are left up to each |
| 138 | +/// platform and `Executor` implementation. |
| 139 | +/// |
| 140 | +/// A Job's priority is roughly equivalent to a `TaskPriority`, |
| 141 | +/// however, since not all jobs are tasks, represented as separate type. |
| 142 | +/// |
| 143 | +/// Conversions between the two priorities are available as initializers on the respective types. |
| 144 | +@available(SwiftStdlib 5.9, *) |
| 145 | +public struct JobPriority: Sendable { |
| 146 | + public typealias RawValue = UInt8 |
| 147 | + |
| 148 | + /// The raw priority value. |
| 149 | + public var rawValue: RawValue |
| 150 | +} |
| 151 | + |
| 152 | +@available(SwiftStdlib 5.9, *) |
| 153 | +extension TaskPriority { |
| 154 | + /// Convert this ``UnownedJob/Priority`` to a ``TaskPriority``. |
| 155 | + /// |
| 156 | + /// Most values are directly interchangeable, but this initializer reserves the right to fail for certain values. |
| 157 | + @available(SwiftStdlib 5.9, *) |
| 158 | + public init?(_ p: JobPriority) { |
| 159 | + guard p.rawValue != 0 else { |
| 160 | + /// 0 is "undefined" |
| 161 | + return nil |
| 162 | + } |
| 163 | + self = TaskPriority(rawValue: p.rawValue) |
| 164 | + } |
| 165 | +} |
| 166 | + |
| 167 | +@available(SwiftStdlib 5.9, *) |
| 168 | +extension JobPriority: Equatable { |
| 169 | + public static func == (lhs: JobPriority, rhs: JobPriority) -> Bool { |
| 170 | + lhs.rawValue == rhs.rawValue |
| 171 | + } |
| 172 | + |
| 173 | + public static func != (lhs: JobPriority, rhs: JobPriority) -> Bool { |
| 174 | + lhs.rawValue != rhs.rawValue |
| 175 | + } |
| 176 | +} |
| 177 | + |
| 178 | +@available(SwiftStdlib 5.9, *) |
| 179 | +extension JobPriority: Comparable { |
| 180 | + public static func < (lhs: JobPriority, rhs: JobPriority) -> Bool { |
| 181 | + lhs.rawValue < rhs.rawValue |
| 182 | + } |
| 183 | + |
| 184 | + public static func <= (lhs: JobPriority, rhs: JobPriority) -> Bool { |
| 185 | + lhs.rawValue <= rhs.rawValue |
| 186 | + } |
| 187 | + |
| 188 | + public static func > (lhs: JobPriority, rhs: JobPriority) -> Bool { |
| 189 | + lhs.rawValue > rhs.rawValue |
| 190 | + } |
| 191 | + |
| 192 | + public static func >= (lhs: JobPriority, rhs: JobPriority) -> Bool { |
| 193 | + lhs.rawValue >= rhs.rawValue |
| 194 | + } |
| 195 | +} |
| 196 | + |
| 197 | +// ==== ----------------------------------------------------------------------- |
| 198 | +// MARK: Runtime functions |
| 199 | + |
| 200 | +@available(SwiftStdlib 5.9, *) |
| 201 | +@_silgen_name("swift_concurrency_jobPriority") |
| 202 | +internal func _swift_concurrency_jobPriority(_ job: UnownedJob) -> UInt8 |
0 commit comments