Skip to content

Instantly share code, notes, and snippets.

@robinkunde
Created July 18, 2016 19:47
Show Gist options
  • Save robinkunde/56ef4b2316ef0480434c543d22c51993 to your computer and use it in GitHub Desktop.
Save robinkunde/56ef4b2316ef0480434c543d22c51993 to your computer and use it in GitHub Desktop.
generic_dispatch_lock.swift
@inline(__always) func with<T>(queue: dispatch_queue_t, @autoclosure(escaping) get block: () -> T) -> T {
assert(dispatch_queue_get_label(queue) != dispatch_queue_get_label(DISPATCH_CURRENT_QUEUE_LABEL), "Invoked dispatch_sync in a way that will deadlock")
var result: T!
dispatch_sync(queue) {
result = block()
}
return result
}
@inline(__always) func with<T>(queue: dispatch_queue_t, @autoclosure(escaping) get block: () -> T?) -> T? {
assert(dispatch_queue_get_label(queue) != dispatch_queue_get_label(DISPATCH_CURRENT_QUEUE_LABEL), "Invoked dispatch_sync in a way that will deadlock")
var result: T?
dispatch_sync(queue) {
result = block()
}
return result
}
@inline(__always) func with(queue: dispatch_queue_t, @autoclosure(escaping) set block: () -> Void) {
assert(dispatch_queue_get_label(queue) != dispatch_queue_get_label(DISPATCH_CURRENT_QUEUE_LABEL), "Invoked dispatch_sync in a way that will deadlock")
dispatch_sync(queue, block)
}
@inline(__always) func with(queue: dispatch_queue_t, @autoclosure(escaping) barrierSet block: () -> Void) {
assert(dispatch_queue_get_label(queue) != dispatch_queue_get_label(DISPATCH_CURRENT_QUEUE_LABEL), "Invoked dispatch_sync in a way that will deadlock")
dispatch_barrier_sync(queue, block)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment