Skip to content

Commit 65c983d

Browse files
author
Itai Ferber
committed
Update Thread to match API exposed by Darwin Foundation
1 parent d353232 commit 65c983d

File tree

1 file changed

+38
-15
lines changed

1 file changed

+38
-15
lines changed

Foundation/NSThread.swift

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -56,28 +56,33 @@ private func NSThreadStart(_ context: UnsafeMutableRawPointer?) -> UnsafeMutable
5656
return nil
5757
}
5858

59-
open class Thread: NSObject {
59+
open class Thread : NSObject {
6060

6161
static internal var _currentThread = NSThreadSpecific<Thread>()
62-
public static var current: Thread {
62+
open class var current: Thread {
6363
return Thread._currentThread.get() {
6464
return Thread(thread: pthread_self())
6565
}
6666
}
67+
68+
open class var isMainThread: Bool { NSUnimplemented() }
69+
70+
// !!! NSThread's mainThread property is incorrectly exported as "main", which conflicts with its "main" method.
71+
open class var mainThread: Thread { NSUnimplemented() }
6772

6873
/// Alternative API for detached thread creation
6974
/// - Experiment: This is a draft API currently under consideration for official import into Foundation as a suitable alternative to creation via selector
7075
/// - Note: Since this API is under consideration it may be either removed or revised in the near future
71-
open class func detachNewThread(_ main: @escaping (Void) -> Void) {
72-
let t = Thread(main)
76+
open class func detachNewThread(_ block: @escaping () -> Swift.Void) {
77+
let t = Thread(block: block)
7378
t.start()
7479
}
7580

7681
open class func isMultiThreaded() -> Bool {
7782
return true
7883
}
7984

80-
open class func sleepUntilDate(_ date: Date) {
85+
open class func sleep(until date: Date) {
8186
let start_ut = CFGetSystemUptime()
8287
let start_at = CFAbsoluteTimeGetCurrent()
8388
let end_at = date.timeIntervalSinceReferenceDate
@@ -100,7 +105,7 @@ open class Thread: NSObject {
100105
}
101106
}
102107

103-
open class func sleepForTimeInterval(_ interval: TimeInterval) {
108+
open class func sleep(forTimeInterval interval: TimeInterval) {
104109
var ti = interval
105110
let start_ut = CFGetSystemUptime()
106111
let end_ut = start_ut + ti
@@ -135,21 +140,25 @@ open class Thread: NSObject {
135140
internal var _status = _NSThreadStatus.initialized
136141
internal var _cancelled = false
137142
/// - Note: this differs from the Darwin implementation in that the keys must be Strings
138-
open var threadDictionary = [String:AnyObject]()
143+
open var threadDictionary = [String : AnyObject]()
139144

140145
internal init(thread: pthread_t) {
141146
// Note: even on Darwin this is a non-optional pthread_t; this is only used for valid threads, which are never null pointers.
142147
_thread = thread
143148
}
144-
145-
public init(_ main: @escaping (Void) -> Void) {
146-
_main = main
149+
150+
public override init() {
147151
let _ = withUnsafeMutablePointer(to: &_attr) { attr in
148152
pthread_attr_init(attr)
149153
pthread_attr_setscope(attr, Int32(PTHREAD_SCOPE_SYSTEM))
150154
pthread_attr_setdetachstate(attr, Int32(PTHREAD_CREATE_DETACHED))
151155
}
152156
}
157+
158+
public convenience init(block: @escaping () -> Swift.Void) {
159+
self.init()
160+
_main = block
161+
}
153162

154163
open func start() {
155164
precondition(_status == .initialized, "attempting to start a thread that has already been started")
@@ -166,6 +175,10 @@ open class Thread: NSObject {
166175
open func main() {
167176
_main()
168177
}
178+
179+
open var name: String? {
180+
NSUnimplemented()
181+
}
169182

170183
open var stackSize: Int {
171184
get {
@@ -189,27 +202,37 @@ open class Thread: NSObject {
189202
}
190203
}
191204

192-
open var executing: Bool {
205+
open var isExecuting: Bool {
193206
return _status == .executing
194207
}
195208

196-
open var finished: Bool {
209+
open var isFinished: Bool {
197210
return _status == .finished
198211
}
199212

200-
open var cancelled: Bool {
213+
open var isCancelled: Bool {
201214
return _cancelled
202215
}
203216

217+
open var isMainThread: Bool {
218+
NSUnimplemented()
219+
}
220+
204221
open func cancel() {
205222
_cancelled = true
206223
}
207224

208-
open class func callStackReturnAddresses() -> [NSNumber] {
225+
open class var callStackReturnAddresses: [NSNumber] {
209226
NSUnimplemented()
210227
}
211228

212-
open class func callStackSymbols() -> [String] {
229+
open class var callStackSymbols: [String] {
213230
NSUnimplemented()
214231
}
215232
}
233+
234+
extension NSNotification.Name {
235+
public static let NSWillBecomeMultiThreaded = NSNotification.Name(rawValue: "") // NSUnimplemented
236+
public static let NSDidBecomeSingleThreaded = NSNotification.Name(rawValue: "") // NSUnimplemented
237+
public static let NSThreadWillExit = NSNotification.Name(rawValue: "") // NSUnimplemented
238+
}

0 commit comments

Comments
 (0)