Skip to content

Import Swift 3 overlay from swift repo #89

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 22, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,18 @@ DTRACE_SOURCES=provider.h
endif

if HAVE_SWIFT
libdispatch_la_SOURCES+=swift/swift_wrappers.c
EXTRA_libdispatch_la_SOURCES+=swift/Dispatch.swift
libdispatch_la_SOURCES+=swift/Dispatch.mm
EXTRA_libdispatch_la_SOURCES+= \
swift/Dispatch.swift \
swift/Block.swift \
swift/Data.swift \
swift/Dispatch.swift \
swift/IO.swift \
swift/Private.swift \
swift/Queue.swift \
swift/Source.swift \
swift/Time.swift

EXTRA_libdispatch_la_DEPENDENCIES+=$(abs_builddir)/Dispatch.o $(abs_builddir)/Dispatch.swiftmodule
libdispatch_la_LIBADD+=$(abs_builddir)/Dispatch.o

Expand Down
131 changes: 131 additions & 0 deletions src/swift/Block.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

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
internal var _group: DispatchGroup?

public init(group: DispatchGroup? = nil, qos: DispatchQoS = .unspecified, flags: DispatchWorkItemFlags = [], block: @convention(block) () -> ()) {
_block = _swift_dispatch_block_create_with_qos_class(__dispatch_block_flags_t(flags.rawValue),
qos.qosClass.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(__dispatch_block_flags_t(flags.rawValue), noescapeBlock)
}

public func perform() {
if let g = _group {
g.enter()
defer { g.leave() }
}
_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: @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
}
}

@available(OSX 10.10, iOS 8.0, *)
public extension DispatchWorkItem {
@available(*, deprecated, renamed: "DispatchWorkItem.wait(self:wallTimeout:)")
public func wait(timeout: DispatchWallTime) -> Int {
switch wait(wallTimeout: timeout) {
case .Success: return 0
case .TimedOut: return Int(KERN_OPERATION_TIMED_OUT)
}
}
}

/// 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

/// APINotes also removes the old dispatch_block_t typedef from the Dispatch module
/// completely. In doing so it causes the dispatch_block_* API to lose their
/// @convention(block) attributes. As such, all of the entry points are shimmed
//// through Dispatch.mm with _DispatchBlock types.
@_silgen_name("_swift_dispatch_block_create_with_qos_class")
internal func _swift_dispatch_block_create_with_qos_class(_ flags: __dispatch_block_flags_t, _ qos: qos_class_t, _ relativePriority: Int32, _ block: _DispatchBlock) -> _DispatchBlock

@_silgen_name("_swift_dispatch_block_create_noescape")
internal func _swift_dispatch_block_create_noescape(_ flags: __dispatch_block_flags_t, _ block: @noescape () -> ()) -> _DispatchBlock

@_silgen_name("_swift_dispatch_block_wait")
internal func _swift_dispatch_block_wait(_ block: _DispatchBlock, _ timeout: UInt64) -> Int

@_silgen_name("_swift_dispatch_block_notify")
internal func _swift_dispatch_block_notify(_ block: _DispatchBlock, _ queue: DispatchQueue, _ notifier: _DispatchBlock)

@_silgen_name("_swift_dispatch_block_cancel")
internal func _swift_dispatch_block_cancel(_ block: _DispatchBlock)

@_silgen_name("_swift_dispatch_block_testcancel")
internal func _swift_dispatch_block_testcancel(_ block: _DispatchBlock) -> Int

Loading