Libdispatch. Как сделать приложение отзывчивым

image









. iOS- . .









GCD.







Libdispatch — Apple, . GCD Mac OS X 10.6. libdispatch, GCD, Apache 10 2009. Unix, FreeBSD Linux. . libdispatch .







. , . : pthreads, background selectors, NSThread. — libdispatch . NSThread pthreads. .







GCD



« iOS»



, Obj-C ( iOS 10). — , , AVFoundation WebKit. , , Apple. , trustedTimestamp CLLocation.







, , PhysicsKit. , timeline — . , iOS ( , ). . , : lib + . , libobjc libxpc. , github .







, libdispatch. , . . , : debugDescription



, description



, hash



superclass



. Apple.









libdispatch. . (, libdispatch), . , . cmake xcodeconfig, .







:









— , . , , , El Capitan, .







.



, libdispatch. , — queue_internal.h.







DISPATCH_STRUCT_HEADER



— . OS_OBJECT_HEADER



( OS_OBJECT_HEADER



— vtable ), , . (target queue) — .







DISPATCH_QUEUE_HEADER



DISPATCH_QUEUE_CACHELINE_PADDING



. , , . DISPATCH_QUEUE_HEADER



, «» ( ), , .







continuation. DISPATCH_CONTINUATION_HEADER



. , , , , , .







, libdispatch libpqw POSIX Thread API.







libpqw



, GCD pthread — libpwq, Apple. . 2011 , 0.9 2014 .







<pthread.h>



, . , : , , . , pthread_workqueue_additem_np



, , .







, . . , ( — , ), . . — , , , .







, libdispatch libpwq, pthreads. , ( Mac OS X Snow Leopard 2009 ).







dispatch_async



- libdispatch.







DispatchQueue.main.async {
    // some asynchronous code...
}
      
      





. swift' . , CDispatch — GCD Swift .







public class DispatchQueue : DispatchObject {
    ...
}

public extension DispatchQueue {

    ...

    public class var main: DispatchQueue {
        return DispatchQueue(queue: _swift_dispatch_get_main_queue())
    }

    ...

    @available(OSX 10.10, iOS 8.0, *)
    public func async(execute workItem: DispatchWorkItem) {
        CDispatch.dispatch_async(self.__wrapped, workItem._block)
    }

    ...
}
      
      





, . GCD dispatch_async.







(pthread_create) (libpwq) :









. dispatch_async



:







void
dispatch_async(dispatch_queue_t dq, dispatch_block_t work)
{
    dispatch_continuation_t dc = _dispatch_continuation_alloc();
    uintptr_t dc_flags = DISPATCH_OBJ_CONSUME_BIT;

    _dispatch_continuation_init(dc, dq, work, 0, 0, dc_flags);
    _dispatch_continuation_async(dq, dc);
}
      
      





? -, — continuation. , _t



_s



. , - (, typedef struct dispatch_queue_s *dispatch_queue_t;



). -, , . , , 0.







, (_dispatch_continuation_async



_dispatch_continuation_async2



). (noinline



) (inline



) , , . — continuation . _dispatch_continuation_push



. , .







, _dispatch_async_f2



, QoS continuation — . _dispatch_continuation_push



, dx_push



. , _dispatch_queue_push_inline



. .







, — , . . , . .







_dispatch_queue_push_inline



. (, , libdispatch) :









— . _dispatch_async_f_redirect



continuation . — .







dx_wakeup



_dispatch_queue_class_wakeup



. , . , , , dx_push



.







_dispatch_queue_class_wakeup_with_override



, _dispatch_queue_class_wakeup_with_override_slow



. slow



— .







, , . . pthread_create



pthread_workqueue_additem_np



. , libpwq, , .







, , — .







Swift



, swift-, libdispatch. , Swift. libdispatch c swift' . , , — API GCD.







, libdispatch Swift — Wrapper.swift



. .







, . libdispatch, dispatch_group_t



dispatch_queue_t



, - __wrapped



. - libdispatch __wrapped



.







:







public class DispatchQueue : DispatchObject {

    //     libdispatch
    internal let __wrapped:dispatch_queue_t

    ...

    final internal override func wrapped() -> dispatch_object_t {
        return unsafeBitCast(__wrapped, to: dispatch_object_t.self)
    }

    ...

    public func sync(execute workItem: ()->()) {
        //     
        dispatch_sync(self.__wrapped, workItem)
    }

    ...
}
      
      





, , . , , . , Private.swift



libdispatch. . swift



' (, libdispatch).







@available(*, unavailable, renamed:"DispatchQueue.async(self:group:qos:flags:execute:)")
public func dispatch_group_async(_ group: DispatchGroup, _ queue: DispatchQueue, _ block: @escaping () -> Void)
{
    fatalError()
}
      
      





— ,



, libdispatch. . libdispatch POSIX Thread API — API .







libdispatch (libpwq), .







— ? . - , .







. - pthread iOS, .












All Articles