Skip to content

Instantly share code, notes, and snippets.

Created September 13, 2012 09:39
  • Star 13 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
I often use dispatch queues for locking, and this function just makes life SO MUCH EASIER. Accidental deadlocks in more complex code paths are a PITA otherwise. But Apple deprecated dispatch_get_current_queue with iOS6?
nline void pspdf_dispatch_sync_reentrant(dispatch_queue_t queue, dispatch_block_t block) {
dispatch_get_current_queue() == queue ? block() : dispatch_sync(queue, block);
Copy link

@ctp That's extremely disappointing. It's already hard enough to use synchronous dispatches in any safe way, and I've written a lot of code to make this pattern scale (and behave more predictably):

Copy link

ctp commented Sep 14, 2012

@jspahrsummers - Disappointing? It's part of the dispatch_sync() contract and makes sense if you think about what's happening with respect to the queue.

I'm not sure what to tell you relative to your SafeDispatch project - I'm afraid I don't have the time right now to go through and figure out what to recommend if this pattern appears there.

The man page for dispatch_sync() describes one approach to the problem if you must use a recursive locking pattern; perhaps that's something which might help out (I don't know).

Copy link

@ctp I agree that it's part of the contract, and does violate ordering, but, in my opinion, such issues are preferable to deadlocks. All of these problems are difficult to verify statically (with a compiler or as a human), so it's really a question of which failure you would rather have.

I could see an argument either way, honestly, which sounds to me like it should be up to the caller. GCD is already a fairly low-level API – I think allowing users to make decisions like this fits with its design.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment