1- //===--- Semaphore .swift ------- -----------------------------------------------===//
1+ //===--- LoopSemaphore .swift -----------------------------------------------===//
22//Copyright (c) 2016 Daniel Leping (dileping)
33//
44//Licensed under the Apache License, Version 2.0 (the "License");
1616
1717import Foundation
1818
19- import Dispatch
19+ #if !os(Linux)
20+ import Dispatch
2021
21- public class DispatchLoopSemaphore : SemaphoreType {
22- let sema : dispatch_semaphore_t
22+ public class DispatchLoopSemaphore : SemaphoreType {
23+ let sema : dispatch_semaphore_t
2324
24- public required convenience init ( ) {
25- self . init ( value: 0 )
26- }
25+ public required convenience init ( ) {
26+ self . init ( value: 0 )
27+ }
2728
28- public required init ( value: Int ) {
29- self . sema = dispatch_semaphore_create ( value)
30- }
29+ public required init ( value: Int ) {
30+ self . sema = dispatch_semaphore_create ( value)
31+ }
3132
32- public func wait( ) -> Bool {
33- return dispatch_semaphore_wait ( sema, DISPATCH_TIME_FOREVER) == 0
34- }
33+ public func wait( ) -> Bool {
34+ return dispatch_semaphore_wait ( sema, DISPATCH_TIME_FOREVER) == 0
35+ }
3536
36- public func wait( until: NSDate ? ) -> Bool {
37- let timeout = until? . timeIntervalSinceNow
38- return wait ( timeout)
39- }
37+ public func wait( until: NSDate ? ) -> Bool {
38+ let timeout = until? . timeIntervalSinceNow
39+ return wait ( timeout)
40+ }
4041
41- public func wait( timeout: Double ? ) -> Bool {
42- guard let timeout = timeout else {
43- return wait ( )
42+ public func wait( timeout: Double ? ) -> Bool {
43+ guard let timeout = timeout else {
44+ return wait ( )
45+ }
46+ let time = dispatch_time ( DISPATCH_TIME_NOW, Int64 ( timeout * NSTimeInterval( NSEC_PER_SEC) ) )
47+ let result = dispatch_semaphore_wait ( sema, time)
48+ return result == 0
4449 }
45- let time = dispatch_time ( DISPATCH_TIME_NOW, Int64 ( timeout * NSTimeInterval( NSEC_PER_SEC) ) )
46- let result = dispatch_semaphore_wait ( sema, time)
47- return result == 0
48- }
4950
50- public func signal( ) -> Int {
51- return dispatch_semaphore_signal ( sema)
51+ public func signal( ) -> Int {
52+ return dispatch_semaphore_signal ( sema)
53+ }
5254 }
53- }
55+ #endif
5456
5557extension Optional {
5658 func getOrElse( @autoclosure f: ( ) -> Wrapped ) -> Wrapped {
@@ -105,7 +107,7 @@ public class CFRunLoopSemaphore : SemaphoreType {
105107 while value <= 0 {
106108 while !self . signaled && !timedout {
107109 RunLoop . runUntilOnce ( RunLoop . defaultMode, until: until)
108- timedout = ! until. isGreaterThan ( NSDate ( ) )
110+ timedout = until. timeIntervalSinceNow <= 0
109111 }
110112 if timedout {
111113 break
@@ -126,4 +128,4 @@ public class CFRunLoopSemaphore : SemaphoreType {
126128 }
127129}
128130
129- public typealias LoopSemaphore = CFRunLoopSemaphore
131+ public typealias LoopSemaphore = CFRunLoopSemaphore
0 commit comments