Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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