|
| 1 | +//===--- LoopSemaphore.swift -----------------------------------------------===// |
| 2 | +//Copyright (c) 2016 Daniel Leping (dileping) |
| 3 | +// |
| 4 | +//Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +//you may not use this file except in compliance with the License. |
| 6 | +//You may obtain a copy of the License at |
| 7 | +// |
| 8 | +//http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +//Unless required by applicable law or agreed to in writing, software |
| 11 | +//distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +//WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +//See the License for the specific language governing permissions and |
| 14 | +//limitations under the License. |
| 15 | +//===----------------------------------------------------------------------===// |
| 16 | + |
| 17 | +import Foundation |
| 18 | +#if os(Linux) |
| 19 | +import CoreFoundation |
| 20 | +#endif |
| 21 | + |
| 22 | +#if !os(Linux) |
| 23 | + import Dispatch |
| 24 | + |
| 25 | + public class DispatchLoopSemaphore : SemaphoreType { |
| 26 | + let sema:dispatch_semaphore_t |
| 27 | + |
| 28 | + public required convenience init() { |
| 29 | + self.init(value: 0) |
| 30 | + } |
| 31 | + |
| 32 | + public required init(value: Int) { |
| 33 | + self.sema = dispatch_semaphore_create(value) |
| 34 | + } |
| 35 | + |
| 36 | + public func wait() -> Bool { |
| 37 | + return dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER) == 0 |
| 38 | + } |
| 39 | + |
| 40 | + public func wait(until:NSDate?) -> Bool { |
| 41 | + let timeout = until?.timeIntervalSinceNow |
| 42 | + return wait(timeout) |
| 43 | + } |
| 44 | + |
| 45 | + public func wait(timeout: Double?) -> Bool { |
| 46 | + guard let timeout = timeout else { |
| 47 | + return wait() |
| 48 | + } |
| 49 | + let time = dispatch_time(DISPATCH_TIME_NOW, Int64(timeout * NSTimeInterval(NSEC_PER_SEC))) |
| 50 | + let result = dispatch_semaphore_wait(sema, time) |
| 51 | + return result == 0 |
| 52 | + } |
| 53 | + |
| 54 | + public func signal() -> Int { |
| 55 | + return dispatch_semaphore_signal(sema) |
| 56 | + } |
| 57 | + } |
| 58 | +#endif |
| 59 | + |
| 60 | +extension Optional { |
| 61 | + func getOrElse(@autoclosure f:()->Wrapped) -> Wrapped { |
| 62 | + switch self { |
| 63 | + case .Some(let value): |
| 64 | + return value |
| 65 | + case .None: |
| 66 | + return f() |
| 67 | + } |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +public class CFRunLoopSemaphore : SemaphoreType { |
| 72 | + var source: RunLoopSource? |
| 73 | + var signaled: Bool |
| 74 | + |
| 75 | + private(set) public var value: Int |
| 76 | + |
| 77 | + /// Creates a new semaphore with the given initial value |
| 78 | + /// See NSCondition and https://developer.apple.com/library/prerelease/mac/documentation/Cocoa/Conceptual/Multithreading/ThreadSafety/ThreadSafety.html#//apple_ref/doc/uid/10000057i-CH8-SW13 |
| 79 | + public required init(value: Int) { |
| 80 | + self.value = value |
| 81 | + self.signaled = false |
| 82 | + self.source = RunLoopSource( { [unowned self] in |
| 83 | + self.signaled = true |
| 84 | + self.value += 1 |
| 85 | + // On linux timeout not working in run loop |
| 86 | + #if os(Linux) |
| 87 | + CFRunLoopStop(CFRunLoopGetCurrent()) |
| 88 | + #endif |
| 89 | + }, priority: 2) |
| 90 | + } |
| 91 | + |
| 92 | + /// Creates a new semaphore with initial value 0 |
| 93 | + /// This kind of semaphores is useful to protect a critical section |
| 94 | + public convenience required init() { |
| 95 | + self.init(value: 0) |
| 96 | + } |
| 97 | + |
| 98 | + public func wait() -> Bool { |
| 99 | + return wait(nil) |
| 100 | + } |
| 101 | + |
| 102 | + /// returns true on success (false if timeout expired) |
| 103 | + /// if nil is passed - waits forever |
| 104 | + public func wait(until:NSDate?) -> Bool { |
| 105 | + let until = until.getOrElse(NSDate.distantFuture()) |
| 106 | + |
| 107 | + defer { |
| 108 | + self.signaled = false |
| 109 | + } |
| 110 | + |
| 111 | + var timedout:Bool = false |
| 112 | + |
| 113 | + while value <= 0 { |
| 114 | + while !self.signaled && !timedout { |
| 115 | + RunLoop.runUntilOnce(RunLoop.defaultMode, until: until) |
| 116 | + timedout = until.timeIntervalSinceNow <= 0 |
| 117 | + } |
| 118 | + if timedout { |
| 119 | + break |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + if signaled { |
| 124 | + value -= 1 |
| 125 | + } |
| 126 | + |
| 127 | + return signaled |
| 128 | + } |
| 129 | + |
| 130 | + /// Performs the signal operation on this semaphore |
| 131 | + public func signal() -> Int { |
| 132 | + source?.signal() |
| 133 | + return value |
| 134 | + } |
| 135 | + |
| 136 | + public func willUse() { |
| 137 | + let loop:RunLoop = RunLoop.currentRunLoop() |
| 138 | + loop.addSource(source!, mode: RunLoop.defaultMode) |
| 139 | + } |
| 140 | + |
| 141 | + public func didUse() { |
| 142 | + let loop:RunLoop = RunLoop.currentRunLoop() |
| 143 | + loop.removeSource(source!, mode: RunLoop.defaultMode) |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +#if os(Linux) |
| 148 | + public typealias LoopSemaphore = CFRunLoopSemaphore |
| 149 | +#else |
| 150 | + public typealias LoopSemaphore = DispatchLoopSemaphore |
| 151 | +#endif |
0 commit comments