Created
July 18, 2016 19:47
-
-
Save robinkunde/56ef4b2316ef0480434c543d22c51993 to your computer and use it in GitHub Desktop.
generic_dispatch_lock.swift
This file contains 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
@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