iOS開發之 runtime(37) :load 方法調用 同步

本系列博客是本人的源碼閱讀筆記,如果有 iOS 開發者在看 runtime 的,歡迎大家多多交流。

前言

上一篇文章筆者講解了 load 方法的準備工作,主要是將擁有 load 方法的類或者分類找出來,並將其放入到靜態變量數組 loadable_classes 以及 loadable_categories 中,並在文章中給出猜測 load 方法調用的順序。本文將給大家介紹 load 方法的調用過程。

分析

call load 方法代碼如下:

void call_load_methods(void)
{
    static bool loading = NO;
    bool more_categories;

    loadMethodLock.assertLocked();

    // Re-entrant calls do nothing; the outermost call will finish the job.
    if (loading) return;
    loading = YES;

    void *pool = objc_autoreleasePoolPush();

    do {
        // 1. Repeatedly call class +loads until there aren't any more
        while (loadable_classes_used > 0) {
            call_class_loads();
        }

        // 2. Call category +loads ONCE
        more_categories = call_category_loads();

        // 3. Run more +loads if there are classes OR more untried categories
    } while (loadable_classes_used > 0  ||  more_categories);

    objc_autoreleasePoolPop(pool);

    loading = NO;
}

同步

iOS開發之 runtime(37) :load 方法調用

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章