Last active
July 19, 2016 19:58
-
-
Save joshfriend/db1452593e98963f668ceeac6e67de50 to your computer and use it in GitHub Desktop.
Semaphore wrapper for swift2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
struct Semaphore { | |
typealias Signal = () -> Int | |
private let semaphore: dispatch_semaphore_t | |
init(count: Int = 0) { | |
self.semaphore = dispatch_semaphore_create(count) | |
} | |
func signal() -> Int { | |
return dispatch_semaphore_signal(self.semaphore) | |
} | |
func wait(until timeout: UInt64) -> Int { | |
return dispatch_semaphore_wait(self.semaphore, timeout: timeout) | |
} | |
func waitForever() -> Int { | |
return self.wait(until: DISPATCH_TIME_FOREVER) | |
} | |
func wait(until timeout: UInt64, forBlock block: (Signal) -> Void) { | |
let done: Signal = { | |
return self.signal() | |
} | |
block(done) | |
return self.wait(until: timeout) | |
} | |
func waitForever(forBlock block: (Signal) -> Void) { | |
return self.wait(until: DISPATCH_TIME_FOREVER, forBlock: block) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment