Does OBJC_ASSOCIATION_ASSIGN mean atomic or non-atomic?

Does the OBJC_ASSOCIATION_ASSIGNfourth argument objc_setAssociatedObjectmean atomic or non-atomic?

This listing is defined as

enum {
    OBJC_ASSOCIATION_ASSIGN = 0,
    OBJC_ASSOCIATION_RETAIN_NONATOMIC = 1,
    OBJC_ASSOCIATION_COPY_NONATOMIC = 3,
    OBJC_ASSOCIATION_RETAIN = 01401,
    OBJC_ASSOCIATION_COPY = 01403
};

What for @property(assign, atomic) id idAssignAtomic;?

What for @property(assign, nonatomic) id idAssignNonatomic;?

+4
source share
1 answer

Currently (with iOS 8 and OS X 10.10) internally, all associations are atomically generated. This can be seen if you look at the source objc-references.mm:

// class AssociationsManager manages a lock / hash table singleton pair.
// Allocating an instance acquires the lock, and calling its assocations() method
// lazily allocates it.

class AssociationsManager {
    static spinlock_t _lock;
    static AssociationsHashMap *_map;               // associative references:  object pointer -> PtrPtrHashMap.
public:
    AssociationsManager()   { spinlock_lock(&_lock); }
    ~AssociationsManager()  { spinlock_unlock(&_lock); }

    AssociationsHashMap &associations() {
        if (_map == NULL)
            _map = new AssociationsHashMap();
        return *_map;
    }
};

In particular, note that _lockboth _mapare static and are defined as such:

spinlock_t AssociationsManager::_lock = SPINLOCK_INITIALIZER;
AssociationsHashMap *AssociationsManager::_map = NULL;

And then when both get the association and release it, we see the following:

AssociationsManager manager;
AssociationsHashMap &associations(manager.associations());

, , .

, , , , , .

+1

Source: https://habr.com/ru/post/1529617/


All Articles