// Begin synchronizing on 'obj'. // Allocates recursive mutex associated with 'obj' if needed. // Returns OBJC_SYNC_SUCCESS once lock is acquired. int objc_sync_enter(id obj) { int result = OBJC_SYNC_SUCCESS; // 1. obj有值 if (obj) { // 2. 生成SyncData类型的data,这是重点,注意参数ACQUIRE SyncData* data = id2data(obj, ACQUIRE); ASSERT(data); // 3. 加互斥锁 data->mutex.lock(); } else { // 4. // @synchronized(nil) does nothing if (DebugNilSync) { _objc_inform("NIL SYNC DEBUG: @synchronized(nil); set a breakpoint on objc_sync_nil to debug"); } objc_sync_nil(); }
// End synchronizing on 'obj'. // Returns OBJC_SYNC_SUCCESS or OBJC_SYNC_NOT_OWNING_THREAD_ERROR int objc_sync_exit(id obj) { int result = OBJC_SYNC_SUCCESS; // 1. 判断是否有obj if (obj) { // 获取data,注意传的值的参数是RELEASE 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 { // @synchronized(nil) does nothing } return result; }
static SyncCache *fetch_cache(bool create) { _objc_pthread_data *data; // 这个函数有解释,而且一目了然,没有从线程缓存池中拿到数据,并且参数create==NO,然后null,如果参数是YES,则创建一个。所以这里没有找到的话返回的是null,这个函数的主要作用就是获取cache 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; } }
// Make sure there's at least one open slot in the list. if (data->syncCache->allocated == data->syncCache->used) { data->syncCache->allocated *= 2; data->syncCache = (SyncCache *) realloc(data->syncCache, sizeof(SyncCache) + data->syncCache->allocated * sizeof(SyncCacheItem)); }
/*********************************************************************** * _objc_fetch_pthread_data * Fetch objc's pthread data for this thread. * If the data doesn't exist yet and create is NO, return NULL. * If the data doesn't exist yet and create is YES, allocate and return it. **********************************************************************/ _objc_pthread_data *_objc_fetch_pthread_data(bool create) { _objc_pthread_data *data;
data = (_objc_pthread_data *)tls_get(_objc_pthread_key); if (!data && create) { data = (_objc_pthread_data *) calloc(1, sizeof(_objc_pthread_data)); tls_set(_objc_pthread_key, data); }
done: // 自旋锁解锁, 这个锁只在第三步有使用。因为这是一个耗时操作 lockp->unlock(); if (result) { // Only new ACQUIRE should get here. // All RELEASE and CHECK and recursive ACQUIRE are // handled by the per-thread caches above. if (why == RELEASE) { // 啥都没有呢,就释放,返回nil // Probably some thread is incorrectly exiting // while the object is held by another thread. return nil; } // 错误判断 if (why != ACQUIRE) _objc_fatal("id2data is buggy"); if (result->object != object) _objc_fatal("id2data is buggy");
#if SUPPORT_DIRECT_THREAD_KEYS // 走了第一步就会变成YES。 if (!fastCacheOccupied) { // Save in fast thread cache // 把SyncData、lockCount=1锁的个数存到tls表中,快速查找的表 tls_set_direct(SYNC_DATA_DIRECT_KEY, result); tls_set_direct(SYNC_COUNT_DIRECT_KEY, (void*)1); } else #endif { // Save in thread cache // 存到线程池的表中 if (!cache) cache = fetch_cache(YES); cache->list[cache->used].data = result; cache->list[cache->used].lockCount = 1; cache->used++; } } // 返回结果。 return result; }