123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327 |
- #include "objc-private.h"
- #include "objc-sync.h"
- typedef struct alignas(CacheLineSize) SyncData {
- struct SyncData* nextData;
- DisguisedPtr<objc_object> object;
- int32_t threadCount;
- recursive_mutex_t mutex;
- } SyncData;
- typedef struct {
- SyncData *data;
- unsigned int lockCount;
- } SyncCacheItem;
- typedef struct SyncCache {
- unsigned int allocated;
- unsigned int used;
- SyncCacheItem list[0];
- } SyncCache;
- struct SyncList {
- SyncData *data;
- spinlock_t lock;
- constexpr SyncList() : data(nil), lock(fork_unsafe_lock) { }
- };
- #define LOCK_FOR_OBJ(obj) sDataLists[obj].lock
- #define LIST_FOR_OBJ(obj) sDataLists[obj].data
- static StripedMap<SyncList> sDataLists;
- enum usage { ACQUIRE, RELEASE, CHECK };
- static SyncCache *fetch_cache(bool create)
- {
- _objc_pthread_data *data;
-
- data = _objc_fetch_pthread_data(create);
- if (!data) return NULL;
- if (!data->syncCache) {
- if (!create) {
- return NULL;
- } else {
- int count = 4;
- data->syncCache = (SyncCache *)
- calloc(1, sizeof(SyncCache) + count*sizeof(SyncCacheItem));
- data->syncCache->allocated = count;
- }
- }
-
- if (data->syncCache->allocated == data->syncCache->used) {
- data->syncCache->allocated *= 2;
- data->syncCache = (SyncCache *)
- realloc(data->syncCache, sizeof(SyncCache)
- + data->syncCache->allocated * sizeof(SyncCacheItem));
- }
- return data->syncCache;
- }
- void _destroySyncCache(struct SyncCache *cache)
- {
- if (cache) free(cache);
- }
- static SyncData* id2data(id object, enum usage why)
- {
- spinlock_t *lockp = &LOCK_FOR_OBJ(object);
- SyncData **listp = &LIST_FOR_OBJ(object);
- SyncData* result = NULL;
- #if SUPPORT_DIRECT_THREAD_KEYS
-
- bool fastCacheOccupied = NO;
- SyncData *data = (SyncData *)tls_get_direct(SYNC_DATA_DIRECT_KEY);
- if (data) {
- fastCacheOccupied = YES;
- if (data->object == object) {
-
- uintptr_t lockCount;
- result = data;
- lockCount = (uintptr_t)tls_get_direct(SYNC_COUNT_DIRECT_KEY);
- if (result->threadCount <= 0 || lockCount <= 0) {
- _objc_fatal("id2data fastcache is buggy");
- }
- switch(why) {
- case ACQUIRE: {
- lockCount++;
- tls_set_direct(SYNC_COUNT_DIRECT_KEY, (void*)lockCount);
- break;
- }
- case RELEASE:
- lockCount--;
- tls_set_direct(SYNC_COUNT_DIRECT_KEY, (void*)lockCount);
- if (lockCount == 0) {
-
- tls_set_direct(SYNC_DATA_DIRECT_KEY, NULL);
-
- OSAtomicDecrement32Barrier(&result->threadCount);
- }
- break;
- case CHECK:
-
- break;
- }
- return result;
- }
- }
- #endif
-
- SyncCache *cache = fetch_cache(NO);
- if (cache) {
- unsigned int i;
- for (i = 0; i < cache->used; i++) {
- SyncCacheItem *item = &cache->list[i];
- if (item->data->object != object) continue;
-
- result = item->data;
- if (result->threadCount <= 0 || item->lockCount <= 0) {
- _objc_fatal("id2data cache is buggy");
- }
-
- switch(why) {
- case ACQUIRE:
- item->lockCount++;
- break;
- case RELEASE:
- item->lockCount--;
- if (item->lockCount == 0) {
-
- cache->list[i] = cache->list[--cache->used];
-
- OSAtomicDecrement32Barrier(&result->threadCount);
- }
- break;
- case CHECK:
-
- break;
- }
- return result;
- }
- }
-
-
-
-
-
-
-
- lockp->lock();
- {
- SyncData* p;
- SyncData* firstUnused = NULL;
- for (p = *listp; p != NULL; p = p->nextData) {
- if ( p->object == object ) {
- result = p;
-
- OSAtomicIncrement32Barrier(&result->threadCount);
- goto done;
- }
- if ( (firstUnused == NULL) && (p->threadCount == 0) )
- firstUnused = p;
- }
-
-
- if ( (why == RELEASE) || (why == CHECK) )
- goto done;
-
-
- if ( firstUnused != NULL ) {
- result = firstUnused;
- result->object = (objc_object *)object;
- result->threadCount = 1;
- goto done;
- }
- }
-
-
-
-
- posix_memalign((void **)&result, alignof(SyncData), sizeof(SyncData));
- result->object = (objc_object *)object;
- result->threadCount = 1;
- new (&result->mutex) recursive_mutex_t(fork_unsafe_lock);
- result->nextData = *listp;
- *listp = result;
-
- done:
- lockp->unlock();
- if (result) {
-
-
-
- if (why == RELEASE) {
-
-
- return nil;
- }
- if (why != ACQUIRE) _objc_fatal("id2data is buggy");
- if (result->object != object) _objc_fatal("id2data is buggy");
- #if SUPPORT_DIRECT_THREAD_KEYS
- if (!fastCacheOccupied) {
-
- tls_set_direct(SYNC_DATA_DIRECT_KEY, result);
- tls_set_direct(SYNC_COUNT_DIRECT_KEY, (void*)1);
- } else
- #endif
- {
-
- if (!cache) cache = fetch_cache(YES);
- cache->list[cache->used].data = result;
- cache->list[cache->used].lockCount = 1;
- cache->used++;
- }
- }
- return result;
- }
- BREAKPOINT_FUNCTION(
- void objc_sync_nil(void)
- );
- int objc_sync_enter(id obj)
- {
- int result = OBJC_SYNC_SUCCESS;
- if (obj) {
- SyncData* data = id2data(obj, ACQUIRE);
- assert(data);
- data->mutex.lock();
- } else {
-
- if (DebugNilSync) {
- _objc_inform("NIL SYNC DEBUG: @synchronized(nil); set a breakpoint on objc_sync_nil to debug");
- }
- objc_sync_nil();
- }
- return result;
- }
- int objc_sync_exit(id obj)
- {
- int result = OBJC_SYNC_SUCCESS;
-
- if (obj) {
- SyncData* data = id2data(obj, RELEASE);
- if (!data) {
- result = OBJC_SYNC_NOT_OWNING_THREAD_ERROR;
- } else {
- bool okay = data->mutex.tryUnlock();
- if (!okay) {
- result = OBJC_SYNC_NOT_OWNING_THREAD_ERROR;
- }
- }
- } else {
-
- }
-
- return result;
- }
|