Skip to content

Instantly share code, notes, and snippets.

@joshfriend
Last active July 19, 2016 19:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joshfriend/db1452593e98963f668ceeac6e67de50 to your computer and use it in GitHub Desktop.
Save joshfriend/db1452593e98963f668ceeac6e67de50 to your computer and use it in GitHub Desktop.
Semaphore wrapper for swift2
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