11
11
12
12
import Foundation
13
13
14
- public typealias Error = NSError
15
- extension Error : Printable , Equatable {
16
- public func error( ) -> String {
17
- return description
18
- }
19
- convenience public init ( error : String ) {
20
- self . init ( domain: " goswift " , code: - 1 , userInfo: [ NSLocalizedDescriptionKey: error] )
21
- }
22
- override public var description : String {
23
- return localizedDescription
24
- }
25
- }
26
- public func == ( lhs: Error , rhs: Error ) -> Bool {
27
- return lhs. description == rhs. description
28
- }
29
- private let pt_entry : @objc_block ( UnsafeMutablePointer < Void > ) -> UnsafeMutablePointer < Void > = { ( ctx) in
14
+ private let pt_entry : @convention ( c) ( UnsafeMutablePointer < Void > ) -> UnsafeMutablePointer < Void > = { ( ctx) in
30
15
let np = UnsafeMutablePointer < ( ) -> ( ) > ( ctx)
31
16
np. memory ( )
32
17
np. destroy ( )
33
18
np. dealloc ( 1 )
34
19
return nil
35
20
}
36
- private var pt_entry_imp = imp_implementationWithBlock ( unsafeBitCast ( pt_entry, AnyObject . self) )
37
- private let pt_entry_fp = CFunctionPointer < ( UnsafeMutablePointer < Void > ) -> UnsafeMutablePointer < Void > > ( pt_entry_imp)
38
21
public func dispatch_thread( block : ( ) -> ( ) ) {
39
22
let p = UnsafeMutablePointer < ( ) -> ( ) > . alloc ( 1 )
40
23
p. initialize ( block)
41
24
var t = pthread_t ( )
42
- pthread_create ( & t, nil , pt_entry_fp , p)
25
+ pthread_create ( & t, nil , pt_entry , p)
43
26
pthread_detach ( t)
44
27
}
45
28
public protocol Locker {
@@ -60,7 +43,7 @@ public class Mutex : Locker {
60
43
public func unlock( ) {
61
44
pthread_mutex_unlock ( & mutex)
62
45
}
63
- func lock( closure: ( ) -> ( ) ) {
46
+ func lock( closure : ( ) -> ( ) ) {
64
47
lock ( )
65
48
closure ( )
66
49
unlock ( )
@@ -196,7 +179,7 @@ public class Chan<T> : ChanAny {
196
179
cond. locker. unlock ( )
197
180
return ( nil , true , false )
198
181
}
199
- var msg = msgs. removeAtIndex ( 0 )
182
+ let msg = msgs. removeAtIndex ( 0 )
200
183
cond. broadcast ( )
201
184
cond. locker. unlock ( )
202
185
return ( msg, true , true )
@@ -220,7 +203,7 @@ public class Chan<T> : ChanAny {
220
203
if msgs. count > 0 {
221
204
flag = true
222
205
mutex!. unlock ( )
223
- var msg = msgs. removeAtIndex ( 0 )
206
+ let msg = msgs. removeAtIndex ( 0 )
224
207
cond. broadcast ( )
225
208
cond. locker. unlock ( )
226
209
return ( msg, true , true )
@@ -237,7 +220,7 @@ public class Chan<T> : ChanAny {
237
220
return ( nil , false , true )
238
221
}
239
222
if msgs. count > 0 {
240
- var msg = msgs. removeAtIndex ( 0 )
223
+ let msg = msgs. removeAtIndex ( 0 )
241
224
cond. broadcast ( )
242
225
cond. locker. unlock ( )
243
226
return ( msg, true , true )
@@ -257,12 +240,12 @@ public func <-<T>(l: Chan<T>, r: T?){
257
240
}
258
241
public prefix func <? < T> ( r: Chan < T > ) -> ( T ? , Bool ) {
259
242
var flag = false
260
- let ( v, ok, ready ) = r. receive ( true , mutex: nil , flag: & flag)
243
+ let ( v, ok, _ ) = r. receive ( true , mutex: nil , flag: & flag)
261
244
return ( v as? T , ok)
262
245
}
263
246
public prefix func <- < T> ( r: Chan < T > ) -> T ? {
264
247
var flag = false
265
- let ( v, ok , ready ) = r. receive ( true , mutex: nil , flag: & flag)
248
+ let ( v, _ , _ ) = r. receive ( true , mutex: nil , flag: & flag)
266
249
return v as? T
267
250
}
268
251
public func close< T> ( chan : Chan < T > ) {
@@ -281,36 +264,31 @@ private struct GoPanicError {
281
264
}
282
265
private class GoRoutineStack {
283
266
var error = GoPanicError ( )
284
- var defers : [ ( ) -> ( ) ] = [ ]
285
267
var ( jump, jumped) = ( UnsafeMutablePointer < Int32 > ( ) , false )
286
- var ( select, cases : [ ( msg : Any? , ok : Bool) - > ( ) ] , chans : [ ChanAny] , defalt : ( ( ) -> ( ) ) ? ) = ( false , [ ] , [ ] , nil )
268
+ var select = false
269
+ var cases : [ ( msg : Any ? , ok : Bool ) -> ( ) ] = [ ]
270
+ var chans : [ ChanAny ] = [ ]
271
+ var defalt : ( ( ) -> ( ) ) ?
287
272
init ( ) {
288
273
jump = UnsafeMutablePointer < Int32 > ( malloc ( 4 * 50 ) )
289
274
}
290
275
deinit {
291
276
free ( jump)
292
277
}
293
- func unwind( ) {
294
- for var i = defers. count - 1 ; i >= 0 ; i-- {
295
- defers [ i] ( )
296
- }
297
- defers = [ ]
298
- }
299
278
}
300
279
private class GoRoutine {
301
280
var stack = [ GoRoutineStack] ( )
302
281
func $( closure: ( ) -> ( ) ) {
303
282
let s = GoRoutineStack ( )
304
283
if stack. count > 0 {
305
- var ls = stack. last!
284
+ let ls = stack. last!
306
285
s. error = ls. error
307
286
ls. error. what = nil
308
287
}
309
288
stack. append ( s)
310
289
if setjmp ( s. jump) == 0 {
311
290
closure ( )
312
291
}
313
- s. unwind ( )
314
292
stack. removeLast ( )
315
293
if s. error. what != nil {
316
294
if stack. count > 0 {
@@ -320,12 +298,6 @@ private class GoRoutine {
320
298
}
321
299
}
322
300
}
323
- func defer( file : StaticString = __FILE__, line : UWord = __LINE__, closure: ( ) -> ( ) ) {
324
- if stack. count == 0 {
325
- fatalError ( " missing ${} context " , file: file, line: line)
326
- }
327
- stack. last!. defers. append ( { self . ${ closure ( ) } } )
328
- }
329
301
func panic( what : AnyObject , file : StaticString = __FILE__, line : UWord = __LINE__) {
330
302
if stack. count == 0 {
331
303
fatalError ( " \( what) " , file: file, line: line)
@@ -361,14 +333,14 @@ private class GoRoutine {
361
333
}
362
334
func select( file : StaticString = __FILE__, line : UWord = __LINE__, closure: ( ) -> ( ) ) {
363
335
${
364
- var s = self . stack. last!
336
+ let s = self . stack. last!
365
337
s. select = true
366
338
closure ( )
367
- var idxs = self . randomInts ( s. chans. count)
339
+ let idxs = self . randomInts ( s. chans. count)
368
340
if s. defalt != nil {
369
341
var ( flag, handled) = ( false , false )
370
342
for i in idxs {
371
- var ( msg, ok, ready) = s. chans [ i] . receive ( false , mutex: nil , flag: & flag)
343
+ let ( msg, ok, ready) = s. chans [ i] . receive ( false , mutex: nil , flag: & flag)
372
344
if ready {
373
345
s. cases [ i] ( msg: msg, ok: ok)
374
346
handled = true
@@ -386,21 +358,21 @@ private class GoRoutine {
386
358
NSThread . sleepForTimeInterval ( 0.05 )
387
359
}
388
360
} else {
389
- var wg = WaitGroup ( )
361
+ let wg = WaitGroup ( )
390
362
wg. add ( idxs. count)
391
- var signal : ( except : Int ) -> ( ) = { ( except) in
363
+ let signal : ( except : Int ) -> ( ) = { ( except) in
392
364
for i in idxs {
393
365
if i != except {
394
366
s. chans [ i] . signal ( )
395
367
}
396
368
}
397
369
}
398
370
var flag = false
399
- var mutex = Mutex ( )
371
+ let mutex = Mutex ( )
400
372
for i in idxs {
401
- var ( c, f, ci) = ( s. chans [ i] , s. cases [ i] , i)
373
+ let ( c, f, ci) = ( s. chans [ i] , s. cases [ i] , i)
402
374
dispatch_thread {
403
- var ( msg, ok, ready) = c. receive ( true , mutex: mutex, flag: & flag)
375
+ let ( msg, ok, ready) = c. receive ( true , mutex: mutex, flag: & flag)
404
376
if ready {
405
377
signal ( except: ci)
406
378
f ( msg: msg, ok: ok)
@@ -498,21 +470,18 @@ public func $(closure: ()->()){
498
470
public func go( closure: ( ) -> ( ) ) {
499
471
goapp. go ( closure)
500
472
}
501
- public func defer( file : StaticString = __FILE__, line : UWord = __LINE__, closure: ( ) -> ( ) ) {
502
- goapp. routine ( ) . defer ( file: file, line: line, closure: closure)
503
- }
504
473
public func panic( what : AnyObject , file : StaticString = __FILE__, line : UWord = __LINE__) {
505
474
goapp. routine ( ) . panic ( what, file: file, line: line)
506
475
}
507
476
public func recover( file : StaticString = __FILE__, line : UWord = __LINE__) -> AnyObject ? {
508
- return goapp. routine ( ) . recover ( file: file , line: line)
477
+ return goapp. routine ( ) . recover ( file, line: line)
509
478
}
510
479
public func select( file : StaticString = __FILE__, line : UWord = __LINE__, closure: ( ) -> ( ) ) {
511
- goapp. routine ( ) . select ( file: file , line: line, closure: closure)
480
+ goapp. routine ( ) . select ( file, line: line, closure: closure)
512
481
}
513
482
public func _case< T> ( l : Chan < T > , file : StaticString = __FILE__, line : UWord = __LINE__, closure: ( msg : T ? , ok : Bool ) -> ( ) ) {
514
483
goapp. routine ( ) . case_ ( l, file: file, line: line, closure: { ( msg, ok) in closure ( msg: msg as? T , ok: ok) } )
515
484
}
516
485
public func _default( file : StaticString = __FILE__, line : UWord = __LINE__, closure: ( ) -> ( ) ) {
517
- goapp. routine ( ) . default_ ( file: file , line: line, closure: closure)
486
+ goapp. routine ( ) . default_ ( file, line: line, closure: closure)
518
487
}
0 commit comments