mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
Adds an explicit @escaping throughout the standard library, validation test suite, and tests. This will be necessary as soon as noescape is the default for closure parameters.
101 lines
3.5 KiB
Swift
101 lines
3.5 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This source file is part of the Swift.org open source project
|
|
//
|
|
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
|
|
// Licensed under Apache License v2.0 with Runtime Library Exception
|
|
//
|
|
// See http://swift.org/LICENSE.txt for license information
|
|
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
import SwiftShims
|
|
|
|
public struct DispatchWorkItemFlags : OptionSet, RawRepresentable {
|
|
public let rawValue: UInt
|
|
public init(rawValue: UInt) { self.rawValue = rawValue }
|
|
|
|
public static let barrier = DispatchWorkItemFlags(rawValue: 0x1)
|
|
|
|
@available(OSX 10.10, iOS 8.0, *)
|
|
public static let detached = DispatchWorkItemFlags(rawValue: 0x2)
|
|
|
|
@available(OSX 10.10, iOS 8.0, *)
|
|
public static let assignCurrentContext = DispatchWorkItemFlags(rawValue: 0x4)
|
|
|
|
@available(OSX 10.10, iOS 8.0, *)
|
|
public static let noQoS = DispatchWorkItemFlags(rawValue: 0x8)
|
|
|
|
@available(OSX 10.10, iOS 8.0, *)
|
|
public static let inheritQoS = DispatchWorkItemFlags(rawValue: 0x10)
|
|
|
|
@available(OSX 10.10, iOS 8.0, *)
|
|
public static let enforceQoS = DispatchWorkItemFlags(rawValue: 0x20)
|
|
}
|
|
|
|
@available(OSX 10.10, iOS 8.0, *)
|
|
public class DispatchWorkItem {
|
|
internal var _block: _DispatchBlock
|
|
|
|
public init(qos: DispatchQoS = .unspecified, flags: DispatchWorkItemFlags = [], block: @escaping @convention(block) () -> ()) {
|
|
_block = _swift_dispatch_block_create_with_qos_class(flags.rawValue,
|
|
qos.qosClass.rawValue.rawValue, Int32(qos.relativePriority), block)
|
|
}
|
|
|
|
// Used by DispatchQueue.synchronously<T> to provide a @noescape path through
|
|
// dispatch_block_t, as we know the lifetime of the block in question.
|
|
internal init(flags: DispatchWorkItemFlags = [], noescapeBlock: @noescape () -> ()) {
|
|
_block = _swift_dispatch_block_create_noescape(flags.rawValue, noescapeBlock)
|
|
}
|
|
|
|
public func perform() {
|
|
_block()
|
|
}
|
|
|
|
public func wait() {
|
|
_ = _swift_dispatch_block_wait(_block, DispatchTime.distantFuture.rawValue)
|
|
}
|
|
|
|
public func wait(timeout: DispatchTime) -> DispatchTimeoutResult {
|
|
return _swift_dispatch_block_wait(_block, timeout.rawValue) == 0 ? .success : .timedOut
|
|
}
|
|
|
|
public func wait(wallTimeout: DispatchWallTime) -> DispatchTimeoutResult {
|
|
return _swift_dispatch_block_wait(_block, wallTimeout.rawValue) == 0 ? .success : .timedOut
|
|
}
|
|
|
|
public func notify(
|
|
qos: DispatchQoS = .unspecified,
|
|
flags: DispatchWorkItemFlags = [],
|
|
queue: DispatchQueue,
|
|
execute: @escaping @convention(block) () -> Void)
|
|
{
|
|
if qos != .unspecified || !flags.isEmpty {
|
|
let item = DispatchWorkItem(qos: qos, flags: flags, block: execute)
|
|
_swift_dispatch_block_notify(_block, queue, item._block)
|
|
} else {
|
|
_swift_dispatch_block_notify(_block, queue, execute)
|
|
}
|
|
}
|
|
|
|
public func notify(queue: DispatchQueue, execute: DispatchWorkItem) {
|
|
_swift_dispatch_block_notify(_block, queue, execute._block)
|
|
}
|
|
|
|
public func cancel() {
|
|
_swift_dispatch_block_cancel(_block)
|
|
}
|
|
|
|
public var isCancelled: Bool {
|
|
return _swift_dispatch_block_testcancel(_block) != 0
|
|
}
|
|
}
|
|
|
|
/// The dispatch_block_t typealias is different from usual closures in that it
|
|
/// uses @convention(block). This is to avoid unnecessary bridging between
|
|
/// C blocks and Swift closures, which interferes with dispatch APIs that depend
|
|
/// on the referential identity of a block. Particularly, dispatch_block_create.
|
|
internal typealias _DispatchBlock = @convention(block) () -> Void
|
|
|