Skip to content

Commit 98d0a05

Browse files
authored
Merge pull request swiftlang#116 from mwwa/overlay-syncup
[overlay-syncup] sync overlay changes to Linux
2 parents 4d58038 + 522a73f commit 98d0a05

File tree

9 files changed

+287
-383
lines changed

9 files changed

+287
-383
lines changed

src/swift/Block.swift

+16-19
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,14 @@ public class DispatchWorkItem {
3939
internal var _block: _DispatchBlock
4040
internal var _group: DispatchGroup?
4141

42-
public init(group: DispatchGroup? = nil, qos: DispatchQoS = .unspecified, flags: DispatchWorkItemFlags = [], block: @convention(block) () -> ()) {
42+
// Temporary for swift-corelibs-foundation
43+
@available(*, deprecated, renamed: "DispatchWorkItem(qos:flags:block:)")
44+
public convenience init(group: DispatchGroup, qos: DispatchQoS = .unspecified, flags: DispatchWorkItemFlags = [], block: @convention(block) () -> ()) {
45+
self.init(qos: qos, flags: flags, block: block)
46+
47+
}
48+
49+
public init(qos: DispatchQoS = .unspecified, flags: DispatchWorkItemFlags = [], block: @convention(block) () -> ()) {
4350
_block = dispatch_block_create_with_qos_class(dispatch_block_flags_t(flags.rawValue),
4451
qos.qosClass.rawValue.rawValue, Int32(qos.relativePriority), block)
4552
}
@@ -51,10 +58,6 @@ public class DispatchWorkItem {
5158
}
5259

5360
public func perform() {
54-
if let g = _group {
55-
g.enter()
56-
defer { g.leave() }
57-
}
5861
_block()
5962
}
6063

@@ -63,14 +66,19 @@ public class DispatchWorkItem {
6366
}
6467

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

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

73-
public func notify(qos: DispatchQoS = .unspecified, flags: DispatchWorkItemFlags = [], queue: DispatchQueue, execute: @convention(block) () -> Void) {
76+
public func notify(
77+
qos: DispatchQoS = .unspecified,
78+
flags: DispatchWorkItemFlags = [],
79+
queue: DispatchQueue,
80+
execute: @convention(block) () -> Void)
81+
{
7482
if qos != .unspecified || !flags.isEmpty {
7583
let item = DispatchWorkItem(qos: qos, flags: flags, block: execute)
7684
dispatch_block_notify(_block, queue.__wrapped, item._block)
@@ -92,17 +100,6 @@ public class DispatchWorkItem {
92100
}
93101
}
94102

95-
@available(OSX 10.10, iOS 8.0, *)
96-
public extension DispatchWorkItem {
97-
@available(*, deprecated, renamed: "DispatchWorkItem.wait(self:wallTimeout:)")
98-
public func wait(timeout: DispatchWallTime) -> Int {
99-
switch wait(wallTimeout: timeout) {
100-
case .Success: return 0
101-
case .TimedOut: return DispatchTimeoutResult.KERN_OPERATION_TIMED_OUT
102-
}
103-
}
104-
}
105-
106103
/// The dispatch_block_t typealias is different from usual closures in that it
107104
/// uses @convention(block). This is to avoid unnecessary bridging between
108105
/// C blocks and Swift closures, which interferes with dispatch APIs that depend

src/swift/Data.swift

+10-8
Original file line numberDiff line numberDiff line change
@@ -77,17 +77,20 @@ public struct DispatchData : RandomAccessCollection {
7777
body: @noescape (UnsafePointer<ContentType>) throws -> Result) rethrows -> Result
7878
{
7979
var ptr: UnsafeRawPointer? = nil
80-
var size = 0;
80+
var size = 0
8181
let data = CDispatch.dispatch_data_create_map(__wrapped.__wrapped, &ptr, &size)
82+
let contentPtr = ptr!.bindMemory(
83+
to: ContentType.self, capacity: size / strideof(ContentType.self))
8284
defer { _fixLifetime(data) }
83-
return try body(UnsafePointer<ContentType>(ptr!))
85+
return try body(contentPtr)
8486
}
8587

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

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

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

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

261263
internal let _data: __DispatchData
262-
internal var _ptr: UnsafePointer<UInt8>!
264+
internal var _ptr: UnsafeRawPointer!
263265
internal var _count: Int
264266
internal var _position: DispatchData.Index
265267
}

src/swift/Dispatch.swift

+10-38
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,6 @@ public struct DispatchQoS : Equatable {
5959
@available(OSX 10.10, iOS 8.0, *)
6060
public static let `default` = DispatchQoS(qosClass: .default, relativePriority: 0)
6161

62-
@available(OSX, introduced: 10.10, deprecated: 10.10, renamed: "DispatchQoS.default")
63-
@available(iOS, introduced: 8.0, deprecated: 8.0, renamed: "DispatchQoS.default")
64-
@available(*, deprecated, renamed: "DispatchQoS.default")
65-
public static let defaultQoS = DispatchQoS.default
66-
6762
@available(OSX 10.10, iOS 8.0, *)
6863
public static let userInitiated = DispatchQoS(qosClass: .userInitiated, relativePriority: 0)
6964

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

85-
@available(OSX, introduced: 10.10, deprecated: 10.10, renamed: "QoSClass.default")
86-
@available(iOS, introduced: 8.0, deprecated: 8.0, renamed: "QoSClass.default")
87-
@available(*, deprecated, renamed: "QoSClass.default")
88-
static let defaultQoS = QoSClass.default
89-
9080
@available(OSX 10.10, iOS 8.0, *)
9181
case userInitiated
9282

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

9686
case unspecified
9787

88+
// _OSQoSClass is internal on Linux, so this initialiser has to
89+
// remain as an internal init.
9890
@available(OSX 10.10, iOS 8.0, *)
99-
internal init?(qosClass: _OSQoSClass) {
100-
switch qosClass {
91+
internal init?(rawValue: _OSQoSClass) {
92+
switch rawValue {
10193
case .QOS_CLASS_BACKGROUND: self = .background
10294
case .QOS_CLASS_UTILITY: self = .utility
10395
case .QOS_CLASS_DEFAULT: self = .default
@@ -135,8 +127,8 @@ public func ==(a: DispatchQoS, b: DispatchQoS) -> Bool {
135127

136128
public enum DispatchTimeoutResult {
137129
static let KERN_OPERATION_TIMED_OUT:Int = 49
138-
case Success
139-
case TimedOut
130+
case success
131+
case timedOut
140132
}
141133

142134
/// dispatch_group
@@ -161,21 +153,11 @@ public extension DispatchGroup {
161153
}
162154

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

167159
public func wait(wallTimeout timeout: DispatchWallTime) -> DispatchTimeoutResult {
168-
return dispatch_group_wait(self.__wrapped, timeout.rawValue) == 0 ? .Success : .TimedOut
169-
}
170-
}
171-
172-
public extension DispatchGroup {
173-
@available(*, deprecated, renamed: "DispatchGroup.wait(self:wallTimeout:)")
174-
public func wait(walltime timeout: DispatchWallTime) -> Int {
175-
switch wait(wallTimeout: timeout) {
176-
case .Success: return 0
177-
case .TimedOut: return DispatchTimeoutResult.KERN_OPERATION_TIMED_OUT
178-
}
160+
return dispatch_group_wait(self.__wrapped, timeout.rawValue) == 0 ? .success : .timedOut
179161
}
180162
}
181163

@@ -192,20 +174,10 @@ public extension DispatchSemaphore {
192174
}
193175

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

198180
public func wait(wallTimeout: DispatchWallTime) -> DispatchTimeoutResult {
199-
return dispatch_semaphore_wait(self.__wrapped, wallTimeout.rawValue) == 0 ? .Success : .TimedOut
200-
}
201-
}
202-
203-
public extension DispatchSemaphore {
204-
@available(*, deprecated, renamed: "DispatchSemaphore.wait(self:wallTimeout:)")
205-
public func wait(walltime timeout: DispatchWalltime) -> Int {
206-
switch wait(wallTimeout: timeout) {
207-
case .Success: return 0
208-
case .TimedOut: return DispatchTimeoutResult.KERN_OPERATION_TIMED_OUT
209-
}
181+
return dispatch_semaphore_wait(self.__wrapped, wallTimeout.rawValue) == 0 ? .success : .timedOut
210182
}
211183
}

src/swift/IO.swift

+2-34
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ public extension DispatchIO {
4040
}
4141
}
4242

43-
public class func write(fromFileDescriptor: Int32, data: DispatchData, runningHandlerOn queue: DispatchQueue, handler: (data: DispatchData?, error: Int32) -> Void) {
44-
dispatch_write(fromFileDescriptor, data.__wrapped.__wrapped, queue.__wrapped) { (data: dispatch_data_t?, error: Int32) in
43+
public class func write(toFileDescriptor: Int32, data: DispatchData, runningHandlerOn queue: DispatchQueue, handler: (data: DispatchData?, error: Int32) -> Void) {
44+
dispatch_write(toFileDescriptor, data.__wrapped.__wrapped, queue.__wrapped) { (data: dispatch_data_t?, error: Int32) in
4545
handler(data: data.flatMap { DispatchData(data: $0) }, error: error)
4646
}
4747
}
@@ -95,35 +95,3 @@ public extension DispatchIO {
9595
dispatch_io_close(self.__wrapped, flags.rawValue)
9696
}
9797
}
98-
99-
extension DispatchIO {
100-
@available(*, deprecated, renamed: "DispatchIO.read(fromFileDescriptor:maxLength:runningHandlerOn:handler:)")
101-
public class func read(fd: Int32, length: Int, queue: DispatchQueue, handler: (DispatchData, Int32) -> Void) {
102-
DispatchIO.read(fromFileDescriptor: fd, maxLength: length, runningHandlerOn: queue, handler: handler)
103-
}
104-
105-
@available(*, deprecated, renamed: "DispatchIO.write(fromFileDescriptor:data:runningHandlerOn:handler:)")
106-
public class func write(fd: Int32, data: DispatchData, queue: DispatchQueue, handler: (DispatchData?, Int32) -> Void) {
107-
DispatchIO.write(fromFileDescriptor: fd, data: data, runningHandlerOn: queue, handler: handler)
108-
}
109-
110-
@available(*, deprecated, renamed: "DispatchIO.barrier(self:execute:)")
111-
public func withBarrier(barrier work: () -> ()) {
112-
barrier(execute: work)
113-
}
114-
115-
@available(*, deprecated, renamed: "DispatchIO.setLimit(self:highWater:)")
116-
public func setHighWater(highWater: Int) {
117-
setLimit(highWater: highWater)
118-
}
119-
120-
@available(*, deprecated, renamed: "DispatchIO.setLimit(self:lowWater:)")
121-
public func setLowWater(lowWater: Int) {
122-
setLimit(lowWater: lowWater)
123-
}
124-
125-
@available(*, deprecated, renamed: "DispatchIO.setInterval(self:interval:flags:)")
126-
public func setInterval(interval: UInt64, flags: IntervalFlags) {
127-
setInterval(interval: .nanoseconds(Int(interval)), flags: flags)
128-
}
129-
}

0 commit comments

Comments
 (0)