SideTable

作者: 初灬终 | 来源:发表于2019-10-15 11:14 被阅读0次
    image.png
    struct weak_table_t {
        weak_entry_t *weak_entries;
        size_t    num_entries;
        uintptr_t mask;
        uintptr_t max_hash_displacement;
    };
    
    // Template parameters.
    enum HaveOld { DontHaveOld = false, DoHaveOld = true };
    enum HaveNew { DontHaveNew = false, DoHaveNew = true };
    
    struct SideTable {
        spinlock_t slock;  //锁
        RefcountMap refcnts; //引用记数表
        weak_table_t weak_table; //弱引用hash表
    
        //构造方法
        SideTable() {
            memset(&weak_table, 0, sizeof(weak_table));
        }
        //析构方法
        ~SideTable() {
            _objc_fatal("Do not delete SideTable.");
        }
        //加锁
        void lock() { slock.lock(); }
        //解锁
        void unlock() { slock.unlock(); }
        //重置锁
        void forceReset() { slock.forceReset(); }
    
        // Address-ordered lock discipline for a pair of side tables.
        
        //将两个SideTable加锁
        template<HaveOld, HaveNew>
        static void lockTwo(SideTable *lock1, SideTable *lock2);
    
        //将两个SideTable解锁
        template<HaveOld, HaveNew>
        static void unlockTwo(SideTable *lock1, SideTable *lock2);
    };
    
    
    
    template<>
    void SideTable::lockTwo<DoHaveOld, DoHaveNew>
        (SideTable *lock1, SideTable *lock2)
    {
        spinlock_t::lockTwo(&lock1->slock, &lock2->slock);
    }
    
    template<>
    void SideTable::lockTwo<DoHaveOld, DontHaveNew>
        (SideTable *lock1, SideTable *)
    {
        lock1->lock();
    }
    
    template<>
    void SideTable::lockTwo<DontHaveOld, DoHaveNew>
        (SideTable *, SideTable *lock2)
    {
        lock2->lock();
    }
    
    template<>
    void SideTable::unlockTwo<DoHaveOld, DoHaveNew>
        (SideTable *lock1, SideTable *lock2)
    {
        spinlock_t::unlockTwo(&lock1->slock, &lock2->slock);
    }
    
    template<>
    void SideTable::unlockTwo<DoHaveOld, DontHaveNew>
        (SideTable *lock1, SideTable *)
    {
        lock1->unlock();
    }
    
    template<>
    void SideTable::unlockTwo<DontHaveOld, DoHaveNew>
        (SideTable *, SideTable *lock2)
    {
        lock2->unlock();
    }
    
    

    相关文章

      网友评论

          本文标题:SideTable

          本文链接:https://www.haomeiwen.com/subject/mcdnpctx.html