Dispatch Barrier on Main Queue

584 views Asked by At

So I am reading Facebook's awesome AsyncDisplayKit source code. Specifically, I am reading the implementation behind ASDealloc2MainObject. One thing caught my eye.

In _AS-objc-internal.h, line 423 to 424, there is the programmer dispatching some task onto the main queue.

        dispatch_barrier_async_f(dispatch_get_main_queue(), self,           \
            _objc_deallocOnMainThreadHelper);                               \

As is the case of other dispatch barrier functions, the barrier logic from dispatch_barrier_async_f()only makes sense if it's dealing with a custom concurrent queue. For global concurrent queues and the main queue, it'll act just like dispatch_async_f() with the barrier having no effect.

So why using barrier here?

2

There are 2 answers

3
Catfish_Man On

That looks like a mistake to me. At best, they're trying to signal intent and remind the programmer "hey, this thing is serial", but that seems pretty dubious.

0
Di Wu On

So I have found a possible explanation: dispatch_async_f() calls dispatch_barrier_async_f() under the hood if the queue passed in has the width of concurrency set to 1.

The implementation of dispatch_async_f() looks like this:

DISPATCH_NOINLINE
    void
    dispatch_async_f(dispatch_queue_t dq, void *ctxt, dispatch_function_t func)
    {
            dispatch_continuation_t dc;

            // No fastpath/slowpath hint because we simply don't know
            if (dq->dq_width == 1) {
                    return dispatch_barrier_async_f(dq, ctxt, func);
            }

            dc = fastpath(_dispatch_continuation_alloc_cacheonly());
            if (!dc) {
                    return _dispatch_async_f_slow(dq, ctxt, func);
            }

            dc->do_vtable = (void *)DISPATCH_OBJ_ASYNC_BIT;
            dc->dc_func = func;
            dc->dc_ctxt = ctxt;

            // No fastpath/slowpath hint because we simply don't know
            if (dq->do_targetq) {
                    return _dispatch_async_f2(dq, dc);
            }

            _dispatch_queue_push(dq, dc);
    }