Skip to content

[overlay-syncup] sync overlay changes to Linux #116

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
Jul 28, 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
35 changes: 16 additions & 19 deletions src/swift/Block.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,14 @@ public class DispatchWorkItem {
internal var _block: _DispatchBlock
internal var _group: DispatchGroup?

public init(group: DispatchGroup? = nil, qos: DispatchQoS = .unspecified, flags: DispatchWorkItemFlags = [], block: @convention(block) () -> ()) {
// Temporary for swift-corelibs-foundation
@available(*, deprecated, renamed: "DispatchWorkItem(qos:flags:block:)")
public convenience init(group: DispatchGroup, qos: DispatchQoS = .unspecified, flags: DispatchWorkItemFlags = [], block: @convention(block) () -> ()) {
self.init(qos: qos, flags: flags, block: block)

}

public init(qos: DispatchQoS = .unspecified, flags: DispatchWorkItemFlags = [], block: @convention(block) () -> ()) {
_block = dispatch_block_create_with_qos_class(dispatch_block_flags_t(flags.rawValue),
qos.qosClass.rawValue.rawValue, Int32(qos.relativePriority), block)
}
Expand All @@ -51,10 +58,6 @@ public class DispatchWorkItem {
}

public func perform() {
if let g = _group {
g.enter()
defer { g.leave() }
}
_block()
}

Expand All @@ -63,14 +66,19 @@ public class DispatchWorkItem {
}

public func wait(timeout: DispatchTime) -> DispatchTimeoutResult {
return dispatch_block_wait(_block, timeout.rawValue) == 0 ? .Success : .TimedOut
return dispatch_block_wait(_block, timeout.rawValue) == 0 ? .success : .timedOut
}

public func wait(wallTimeout: DispatchWallTime) -> DispatchTimeoutResult {
return dispatch_block_wait(_block, wallTimeout.rawValue) == 0 ? .Success : .TimedOut
return dispatch_block_wait(_block, wallTimeout.rawValue) == 0 ? .success : .timedOut
}

public func notify(qos: DispatchQoS = .unspecified, flags: DispatchWorkItemFlags = [], queue: DispatchQueue, execute: @convention(block) () -> Void) {
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)
dispatch_block_notify(_block, queue.__wrapped, item._block)
Expand All @@ -92,17 +100,6 @@ public class DispatchWorkItem {
}
}

@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 DispatchTimeoutResult.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
Expand Down
18 changes: 10 additions & 8 deletions src/swift/Data.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,17 +77,20 @@ public struct DispatchData : RandomAccessCollection {
body: @noescape (UnsafePointer<ContentType>) throws -> Result) rethrows -> Result
{
var ptr: UnsafeRawPointer? = nil
var size = 0;
var size = 0
let data = CDispatch.dispatch_data_create_map(__wrapped.__wrapped, &ptr, &size)
let contentPtr = ptr!.bindMemory(
to: ContentType.self, capacity: size / strideof(ContentType.self))
defer { _fixLifetime(data) }
return try body(UnsafePointer<ContentType>(ptr!))
return try body(contentPtr)
}

public func enumerateBytes(
block: @noescape (buffer: UnsafeBufferPointer<UInt8>, byteIndex: Int, stop: inout Bool) -> Void)
{
_swift_dispatch_data_apply(__wrapped.__wrapped) { (data: dispatch_data_t, offset: Int, ptr: UnsafeRawPointer, size: Int) in
let bp = UnsafeBufferPointer(start: UnsafePointer<UInt8>(ptr), count: size)
let bytePtr = ptr.bindMemory(to: UInt8.self, capacity: size)
let bp = UnsafeBufferPointer(start: bytePtr, count: size)
var stop = false
block(buffer: bp, byteIndex: offset, stop: &stop)
return !stop
Expand Down Expand Up @@ -188,8 +191,7 @@ public struct DispatchData : RandomAccessCollection {
let map = CDispatch.dispatch_data_create_map(subdata, &ptr, &size)
defer { _fixLifetime(map) }

let pptr = UnsafePointer<UInt8>(ptr!)
return pptr[index - offset]
return ptr!.load(fromByteOffset: index - offset, as: UInt8.self)
}

public subscript(bounds: Range<Int>) -> RandomAccessSlice<DispatchData> {
Expand Down Expand Up @@ -242,7 +244,7 @@ public struct DispatchDataIterator : IteratorProtocol, Sequence {
var ptr: UnsafeRawPointer?
self._count = 0
self._data = __DispatchData(data: CDispatch.dispatch_data_create_map(_data.__wrapped.__wrapped, &ptr, &self._count))
self._ptr = UnsafePointer(ptr)
self._ptr = ptr
self._position = _data.startIndex

// The only time we expect a 'nil' pointer is when the data is empty.
Expand All @@ -253,13 +255,13 @@ public struct DispatchDataIterator : IteratorProtocol, Sequence {
/// element exists.
public mutating func next() -> DispatchData._Element? {
if _position == _count { return nil }
let element = _ptr[_position];
let element = _ptr.load(fromByteOffset: _position, as: UInt8.self)
_position = _position + 1
return element
}

internal let _data: __DispatchData
internal var _ptr: UnsafePointer<UInt8>!
internal var _ptr: UnsafeRawPointer!
internal var _count: Int
internal var _position: DispatchData.Index
}
Expand Down
48 changes: 10 additions & 38 deletions src/swift/Dispatch.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,6 @@ public struct DispatchQoS : Equatable {
@available(OSX 10.10, iOS 8.0, *)
public static let `default` = DispatchQoS(qosClass: .default, relativePriority: 0)

@available(OSX, introduced: 10.10, deprecated: 10.10, renamed: "DispatchQoS.default")
@available(iOS, introduced: 8.0, deprecated: 8.0, renamed: "DispatchQoS.default")
@available(*, deprecated, renamed: "DispatchQoS.default")
public static let defaultQoS = DispatchQoS.default

@available(OSX 10.10, iOS 8.0, *)
public static let userInitiated = DispatchQoS(qosClass: .userInitiated, relativePriority: 0)

Expand All @@ -82,11 +77,6 @@ public struct DispatchQoS : Equatable {
@available(OSX 10.10, iOS 8.0, *)
case `default`

@available(OSX, introduced: 10.10, deprecated: 10.10, renamed: "QoSClass.default")
@available(iOS, introduced: 8.0, deprecated: 8.0, renamed: "QoSClass.default")
@available(*, deprecated, renamed: "QoSClass.default")
static let defaultQoS = QoSClass.default

@available(OSX 10.10, iOS 8.0, *)
case userInitiated

Expand All @@ -95,9 +85,11 @@ public struct DispatchQoS : Equatable {

case unspecified

// _OSQoSClass is internal on Linux, so this initialiser has to
// remain as an internal init.
@available(OSX 10.10, iOS 8.0, *)
internal init?(qosClass: _OSQoSClass) {
switch qosClass {
internal init?(rawValue: _OSQoSClass) {
switch rawValue {
case .QOS_CLASS_BACKGROUND: self = .background
case .QOS_CLASS_UTILITY: self = .utility
case .QOS_CLASS_DEFAULT: self = .default
Expand Down Expand Up @@ -135,8 +127,8 @@ public func ==(a: DispatchQoS, b: DispatchQoS) -> Bool {

public enum DispatchTimeoutResult {
static let KERN_OPERATION_TIMED_OUT:Int = 49
case Success
case TimedOut
case success
case timedOut
}

/// dispatch_group
Expand All @@ -161,21 +153,11 @@ public extension DispatchGroup {
}

public func wait(timeout: DispatchTime) -> DispatchTimeoutResult {
return dispatch_group_wait(self.__wrapped, timeout.rawValue) == 0 ? .Success : .TimedOut
return dispatch_group_wait(self.__wrapped, timeout.rawValue) == 0 ? .success : .timedOut
}

public func wait(wallTimeout timeout: DispatchWallTime) -> DispatchTimeoutResult {
return dispatch_group_wait(self.__wrapped, timeout.rawValue) == 0 ? .Success : .TimedOut
}
}

public extension DispatchGroup {
@available(*, deprecated, renamed: "DispatchGroup.wait(self:wallTimeout:)")
public func wait(walltime timeout: DispatchWallTime) -> Int {
switch wait(wallTimeout: timeout) {
case .Success: return 0
case .TimedOut: return DispatchTimeoutResult.KERN_OPERATION_TIMED_OUT
}
return dispatch_group_wait(self.__wrapped, timeout.rawValue) == 0 ? .success : .timedOut
}
}

Expand All @@ -192,20 +174,10 @@ public extension DispatchSemaphore {
}

public func wait(timeout: DispatchTime) -> DispatchTimeoutResult {
return dispatch_semaphore_wait(self.__wrapped, timeout.rawValue) == 0 ? .Success : .TimedOut
return dispatch_semaphore_wait(self.__wrapped, timeout.rawValue) == 0 ? .success : .timedOut
}

public func wait(wallTimeout: DispatchWallTime) -> DispatchTimeoutResult {
return dispatch_semaphore_wait(self.__wrapped, wallTimeout.rawValue) == 0 ? .Success : .TimedOut
}
}

public extension DispatchSemaphore {
@available(*, deprecated, renamed: "DispatchSemaphore.wait(self:wallTimeout:)")
public func wait(walltime timeout: DispatchWalltime) -> Int {
switch wait(wallTimeout: timeout) {
case .Success: return 0
case .TimedOut: return DispatchTimeoutResult.KERN_OPERATION_TIMED_OUT
}
return dispatch_semaphore_wait(self.__wrapped, wallTimeout.rawValue) == 0 ? .success : .timedOut
}
}
36 changes: 2 additions & 34 deletions src/swift/IO.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ public extension DispatchIO {
}
}

public class func write(fromFileDescriptor: Int32, data: DispatchData, runningHandlerOn queue: DispatchQueue, handler: (data: DispatchData?, error: Int32) -> Void) {
dispatch_write(fromFileDescriptor, data.__wrapped.__wrapped, queue.__wrapped) { (data: dispatch_data_t?, error: Int32) in
public class func write(toFileDescriptor: Int32, data: DispatchData, runningHandlerOn queue: DispatchQueue, handler: (data: DispatchData?, error: Int32) -> Void) {
dispatch_write(toFileDescriptor, data.__wrapped.__wrapped, queue.__wrapped) { (data: dispatch_data_t?, error: Int32) in
handler(data: data.flatMap { DispatchData(data: $0) }, error: error)
}
}
Expand Down Expand Up @@ -95,35 +95,3 @@ public extension DispatchIO {
dispatch_io_close(self.__wrapped, flags.rawValue)
}
}

extension DispatchIO {
@available(*, deprecated, renamed: "DispatchIO.read(fromFileDescriptor:maxLength:runningHandlerOn:handler:)")
public class func read(fd: Int32, length: Int, queue: DispatchQueue, handler: (DispatchData, Int32) -> Void) {
DispatchIO.read(fromFileDescriptor: fd, maxLength: length, runningHandlerOn: queue, handler: handler)
}

@available(*, deprecated, renamed: "DispatchIO.write(fromFileDescriptor:data:runningHandlerOn:handler:)")
public class func write(fd: Int32, data: DispatchData, queue: DispatchQueue, handler: (DispatchData?, Int32) -> Void) {
DispatchIO.write(fromFileDescriptor: fd, data: data, runningHandlerOn: queue, handler: handler)
}

@available(*, deprecated, renamed: "DispatchIO.barrier(self:execute:)")
public func withBarrier(barrier work: () -> ()) {
barrier(execute: work)
}

@available(*, deprecated, renamed: "DispatchIO.setLimit(self:highWater:)")
public func setHighWater(highWater: Int) {
setLimit(highWater: highWater)
}

@available(*, deprecated, renamed: "DispatchIO.setLimit(self:lowWater:)")
public func setLowWater(lowWater: Int) {
setLimit(lowWater: lowWater)
}

@available(*, deprecated, renamed: "DispatchIO.setInterval(self:interval:flags:)")
public func setInterval(interval: UInt64, flags: IntervalFlags) {
setInterval(interval: .nanoseconds(Int(interval)), flags: flags)
}
}
Loading