Skip to content

Commit 536fc6f

Browse files
committed
Locale, PlugIn, RunLoop: use __CFLock, __CFUnlock
__CFLock, __CFUnlock are wrappers over pthread where applicable. This now works towards switching towards Windows threading on Windows.
1 parent f8cb6a4 commit 536fc6f

File tree

5 files changed

+64
-64
lines changed

5 files changed

+64
-64
lines changed

CoreFoundation/Locale.subproj/CFDateFormatter.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ static Boolean useTemplatePatternGenerator(CFLocaleRef locale, void(^work)(UDate
7373
free((void *)ptgLocaleName);
7474
ptgLocaleName = NULL;
7575
};
76-
pthread_mutex_lock(&ptgLock);
76+
__CFLock(&ptgLock);
7777
if (ptgLocaleName && strcmp(ptgLocaleName, localeName) != 0) {
7878
flushCache();
7979
}
@@ -88,7 +88,7 @@ static Boolean useTemplatePatternGenerator(CFLocaleRef locale, void(^work)(UDate
8888
if (result && work) {
8989
work(ptg);
9090
}
91-
pthread_mutex_unlock(&ptgLock);
91+
__CFUnlock(&ptgLock);
9292
return result;
9393
}
9494

CoreFoundation/PlugIn.subproj/CFBundle.c

Lines changed: 44 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ static void _CFBundleAddToTables(CFBundleRef bundle) {
207207

208208
CFStringRef bundleID = CFBundleGetIdentifier(bundle);
209209

210-
pthread_mutex_lock(&CFBundleGlobalDataLock);
210+
__CFLock(&CFBundleGlobalDataLock);
211211

212212
// Add to the _allBundles list
213213
if (!_allBundles) {
@@ -266,7 +266,7 @@ static void _CFBundleAddToTables(CFBundleRef bundle) {
266266
CFRelease(bundlesWithThisID);
267267
}
268268
}
269-
pthread_mutex_unlock(&CFBundleGlobalDataLock);
269+
__CFUnlock(&CFBundleGlobalDataLock);
270270
}
271271

272272
static void _CFBundleRemoveFromTables(CFBundleRef bundle, CFURLRef bundleURL, CFStringRef bundleID) {
@@ -278,7 +278,7 @@ static void _CFBundleRemoveFromTables(CFBundleRef bundle, CFURLRef bundleURL, CF
278278
// Unique bundles aren't in the tables anyway
279279
if (bundle->_isUnique) return;
280280

281-
pthread_mutex_lock(&CFBundleGlobalDataLock);
281+
__CFLock(&CFBundleGlobalDataLock);
282282
// Remove from the table of all bundles
283283
if (_allBundles) {
284284
CFIndex i = CFArrayGetFirstIndexOfValue(_allBundles, CFRangeMake(0, CFArrayGetCount(_allBundles)), bundle);
@@ -300,14 +300,14 @@ static void _CFBundleRemoveFromTables(CFBundleRef bundle, CFURLRef bundleURL, CF
300300
if (0 == CFArrayGetCount(bundlesWithThisID)) CFDictionaryRemoveValue(_bundlesByIdentifier, bundleID);
301301
}
302302
}
303-
pthread_mutex_unlock(&CFBundleGlobalDataLock);
303+
__CFUnlock(&CFBundleGlobalDataLock);
304304
}
305305
#endif
306306
}
307307

308308
static CFBundleRef _CFBundleGetFromTables(CFStringRef bundleID) {
309309
CFBundleRef result = NULL, bundle;
310-
pthread_mutex_lock(&CFBundleGlobalDataLock);
310+
__CFLock(&CFBundleGlobalDataLock);
311311
if (_bundlesByIdentifier && bundleID) {
312312
// Note that this array is maintained in descending order by version number
313313
CFArrayRef bundlesWithThisID = (CFArrayRef)CFDictionaryGetValue(_bundlesByIdentifier, bundleID);
@@ -324,7 +324,7 @@ static CFBundleRef _CFBundleGetFromTables(CFStringRef bundleID) {
324324
}
325325
}
326326
}
327-
pthread_mutex_unlock(&CFBundleGlobalDataLock);
327+
__CFUnlock(&CFBundleGlobalDataLock);
328328
return result;
329329
}
330330

@@ -342,14 +342,14 @@ static CFBundleRef _CFBundleCopyFromTablesForURL(CFURLRef url) {
342342
Since the main bundle is not part of the bundle tables, we can support this scenario by having the _bundlesByURL data structure hold the bundle for URL "/S/L/F/Foo.framework/Foo" and _mainBundle (in CFBundle_Main.c) hold the bundle for URL "/S/L/F/Foo.framework/food".
343343
*/
344344
CFBundleRef result = NULL;
345-
pthread_mutex_lock(&CFBundleGlobalDataLock);
345+
__CFLock(&CFBundleGlobalDataLock);
346346
if (_bundlesByURL) result = (CFBundleRef)CFDictionaryGetValue(_bundlesByURL, url);
347347
if (result && !result->_url) {
348348
result = NULL;
349349
CFDictionaryRemoveValue(_bundlesByURL, url);
350350
}
351351
if (result) CFRetain(result);
352-
pthread_mutex_unlock(&CFBundleGlobalDataLock);
352+
__CFUnlock(&CFBundleGlobalDataLock);
353353
return result;
354354
}
355355

@@ -478,15 +478,15 @@ static void _CFBundleArrayApplyFlushBundleCaches(const void *value, void *unused
478478
#endif
479479

480480
CF_PRIVATE void _CFBundleFlushAllBundleCaches(void) {
481-
pthread_mutex_lock(&CFBundleGlobalDataLock);
481+
__CFLock(&CFBundleGlobalDataLock);
482482
#if __OBJC__ || __OBJC2__
483483
for (id value in (id)_allBundles) {
484484
_CFBundleFlushBundleCachesAlreadyLocked((CFBundleRef)value, true);
485485
}
486486
#else
487487
CFArrayApplyFunction(_allBundles, CFRangeMake(0, CFArrayGetCount(_allBundles)), &_CFBundleArrayApplyFlushBundleCaches, NULL);
488488
#endif
489-
pthread_mutex_unlock(&CFBundleGlobalDataLock);
489+
__CFUnlock(&CFBundleGlobalDataLock);
490490
}
491491

492492
CFBundleRef CFBundleGetBundleWithIdentifier(CFStringRef bundleID) {
@@ -1145,7 +1145,7 @@ Boolean _CFBundleLoadExecutableAndReturnError(CFBundleRef bundle, Boolean forceG
11451145
CFURLRef executableURL = CFBundleCopyExecutableURL(bundle);
11461146

11471147

1148-
pthread_mutex_lock(&(bundle->_bundleLoadingLock));
1148+
__CFLock(&(bundle->_bundleLoadingLock));
11491149
if (!executableURL) bundle->_binaryType = __CFBundleNoBinary;
11501150
// make sure we know whether bundle is already loaded or not
11511151
#if defined(BINARY_SUPPORT_DLFCN)
@@ -1163,30 +1163,30 @@ Boolean _CFBundleLoadExecutableAndReturnError(CFBundleRef bundle, Boolean forceG
11631163
if (executableURL) CFRelease(executableURL);
11641164

11651165
if (bundle->_isLoaded) {
1166-
pthread_mutex_unlock(&(bundle->_bundleLoadingLock));
1166+
__CFUnlock(&(bundle->_bundleLoadingLock));
11671167
// Remove from the scheduled unload set if we are there.
1168-
pthread_mutex_lock(&CFBundleGlobalDataLock);
1168+
__CFLock(&CFBundleGlobalDataLock);
11691169
if (_bundlesToUnload) CFSetRemoveValue(_bundlesToUnload, bundle);
1170-
pthread_mutex_unlock(&CFBundleGlobalDataLock);
1170+
__CFUnlock(&CFBundleGlobalDataLock);
11711171
return true;
11721172
}
11731173

11741174
// Unload bundles scheduled for unloading
11751175
if (!_scheduledBundlesAreUnloading) {
1176-
pthread_mutex_unlock(&(bundle->_bundleLoadingLock));
1176+
__CFUnlock(&(bundle->_bundleLoadingLock));
11771177
_CFBundleUnloadScheduledBundles();
1178-
pthread_mutex_lock(&(bundle->_bundleLoadingLock));
1178+
__CFLock(&(bundle->_bundleLoadingLock));
11791179
}
11801180

11811181
if (bundle->_isLoaded) {
1182-
pthread_mutex_unlock(&(bundle->_bundleLoadingLock));
1182+
__CFUnlock(&(bundle->_bundleLoadingLock));
11831183
// Remove from the scheduled unload set if we are there.
1184-
pthread_mutex_lock(&CFBundleGlobalDataLock);
1184+
__CFLock(&CFBundleGlobalDataLock);
11851185
if (_bundlesToUnload) CFSetRemoveValue(_bundlesToUnload, bundle);
1186-
pthread_mutex_unlock(&CFBundleGlobalDataLock);
1186+
__CFUnlock(&CFBundleGlobalDataLock);
11871187
return true;
11881188
}
1189-
pthread_mutex_unlock(&(bundle->_bundleLoadingLock));
1189+
__CFUnlock(&(bundle->_bundleLoadingLock));
11901190

11911191
switch (bundle->_binaryType) {
11921192
#if defined(BINARY_SUPPORT_DLFCN)
@@ -1264,7 +1264,7 @@ Boolean CFBundlePreflightExecutable(CFBundleRef bundle, CFErrorRef *error) {
12641264
#endif
12651265
CFURLRef executableURL = CFBundleCopyExecutableURL(bundle);
12661266

1267-
pthread_mutex_lock(&(bundle->_bundleLoadingLock));
1267+
__CFLock(&(bundle->_bundleLoadingLock));
12681268
if (!executableURL) bundle->_binaryType = __CFBundleNoBinary;
12691269
// make sure we know whether bundle is already loaded or not
12701270
#if defined(BINARY_SUPPORT_DLFCN)
@@ -1282,10 +1282,10 @@ Boolean CFBundlePreflightExecutable(CFBundleRef bundle, CFErrorRef *error) {
12821282
if (executableURL) CFRelease(executableURL);
12831283

12841284
if (bundle->_isLoaded) {
1285-
pthread_mutex_unlock(&(bundle->_bundleLoadingLock));
1285+
__CFUnlock(&(bundle->_bundleLoadingLock));
12861286
return true;
12871287
}
1288-
pthread_mutex_unlock(&(bundle->_bundleLoadingLock));
1288+
__CFUnlock(&(bundle->_bundleLoadingLock));
12891289

12901290
switch (bundle->_binaryType) {
12911291
#if defined(BINARY_SUPPORT_DLFCN)
@@ -1349,19 +1349,19 @@ void CFBundleUnloadExecutable(CFBundleRef bundle) {
13491349
if (!bundle->_isLoaded) return;
13501350

13511351
// Remove from the scheduled unload set if we are there.
1352-
if (!_scheduledBundlesAreUnloading) pthread_mutex_lock(&CFBundleGlobalDataLock);
1352+
if (!_scheduledBundlesAreUnloading) __CFLock(&CFBundleGlobalDataLock);
13531353
if (_bundlesToUnload) CFSetRemoveValue(_bundlesToUnload, bundle);
1354-
if (!_scheduledBundlesAreUnloading) pthread_mutex_unlock(&CFBundleGlobalDataLock);
1354+
if (!_scheduledBundlesAreUnloading) __CFUnlock(&CFBundleGlobalDataLock);
13551355

13561356
// Give the plugIn code a chance to realize this...
13571357
_CFPlugInWillUnload(bundle);
13581358

1359-
pthread_mutex_lock(&(bundle->_bundleLoadingLock));
1359+
__CFLock(&(bundle->_bundleLoadingLock));
13601360
if (!bundle->_isLoaded) {
1361-
pthread_mutex_unlock(&(bundle->_bundleLoadingLock));
1361+
__CFUnlock(&(bundle->_bundleLoadingLock));
13621362
return;
13631363
}
1364-
pthread_mutex_unlock(&(bundle->_bundleLoadingLock));
1364+
__CFUnlock(&(bundle->_bundleLoadingLock));
13651365

13661366
switch (bundle->_binaryType) {
13671367
#if defined(BINARY_SUPPORT_DYLD)
@@ -1392,25 +1392,25 @@ void CFBundleUnloadExecutable(CFBundleRef bundle) {
13921392
}
13931393

13941394
CF_PRIVATE void _CFBundleScheduleForUnloading(CFBundleRef bundle) {
1395-
pthread_mutex_lock(&CFBundleGlobalDataLock);
1395+
__CFLock(&CFBundleGlobalDataLock);
13961396
if (!_bundlesToUnload) {
13971397
CFSetCallBacks nonRetainingCallbacks = kCFTypeSetCallBacks;
13981398
nonRetainingCallbacks.retain = NULL;
13991399
nonRetainingCallbacks.release = NULL;
14001400
_bundlesToUnload = CFSetCreateMutable(kCFAllocatorSystemDefault, 0, &nonRetainingCallbacks);
14011401
}
14021402
CFSetAddValue(_bundlesToUnload, bundle);
1403-
pthread_mutex_unlock(&CFBundleGlobalDataLock);
1403+
__CFUnlock(&CFBundleGlobalDataLock);
14041404
}
14051405

14061406
CF_PRIVATE void _CFBundleUnscheduleForUnloading(CFBundleRef bundle) {
1407-
pthread_mutex_lock(&CFBundleGlobalDataLock);
1407+
__CFLock(&CFBundleGlobalDataLock);
14081408
if (_bundlesToUnload) CFSetRemoveValue(_bundlesToUnload, bundle);
1409-
pthread_mutex_unlock(&CFBundleGlobalDataLock);
1409+
__CFUnlock(&CFBundleGlobalDataLock);
14101410
}
14111411

14121412
static void _CFBundleUnloadScheduledBundles(void) {
1413-
pthread_mutex_lock(&CFBundleGlobalDataLock);
1413+
__CFLock(&CFBundleGlobalDataLock);
14141414
if (_bundlesToUnload) {
14151415
CFIndex i, c = CFSetGetCount(_bundlesToUnload);
14161416
if (c > 0) {
@@ -1425,7 +1425,7 @@ static void _CFBundleUnloadScheduledBundles(void) {
14251425
CFAllocatorDeallocate(kCFAllocatorSystemDefault, unloadThese);
14261426
}
14271427
}
1428-
pthread_mutex_unlock(&CFBundleGlobalDataLock);
1428+
__CFUnlock(&CFBundleGlobalDataLock);
14291429
}
14301430

14311431
#pragma mark -
@@ -1566,7 +1566,7 @@ static void _CFBundleEnsureBundleExistsForImagePath(CFStringRef imagePath, Boole
15661566
// NB doFinalProcessing must be false here, see below
15671567
bundle = _CFBundleCreate(kCFAllocatorSystemDefault, curURL, false, false, true);
15681568
if (bundle) {
1569-
pthread_mutex_lock(&(bundle->_bundleLoadingLock));
1569+
__CFLock(&(bundle->_bundleLoadingLock));
15701570
if (!bundle->_isLoaded) {
15711571
// make sure that these bundles listed as loaded, and mark them frameworks (we probably can't see anything else here, and we cannot unload them)
15721572
#if defined(BINARY_SUPPORT_DLFCN)
@@ -1583,7 +1583,7 @@ static void _CFBundleEnsureBundleExistsForImagePath(CFStringRef imagePath, Boole
15831583
#endif /* LOG_BUNDLE_LOAD */
15841584
bundle->_isLoaded = true;
15851585
}
1586-
pthread_mutex_unlock(&(bundle->_bundleLoadingLock));
1586+
__CFUnlock(&(bundle->_bundleLoadingLock));
15871587
// Perform delayed final processing steps.
15881588
// This must be done after _isLoaded has been set, for security reasons (3624341).
15891589
_CFBundleInitPlugIn(bundle);
@@ -1635,14 +1635,14 @@ CFArrayRef CFBundleGetAllBundles(void) {
16351635
// This API is fundamentally broken from a thread safety point of view. To mitigate the issues, we keep around the last list we handed out. If the list of allBundles changed, we leak the last one and return a new copy. If no bundle loading is done this list would be static.
16361636
// Fortunately this method is rarely used.
16371637
CFArrayRef result = NULL;
1638-
pthread_mutex_lock(&CFBundleGlobalDataLock);
1638+
__CFLock(&CFBundleGlobalDataLock);
16391639
static CFArrayRef _lastBundleList = NULL;
16401640
if (!_lastBundleList) {
16411641
// This is the first time we've been asked for a list of all bundles
16421642
// Unlock the global lock. CopyAllBundles will use it.
1643-
pthread_mutex_unlock(&CFBundleGlobalDataLock);
1643+
__CFUnlock(&CFBundleGlobalDataLock);
16441644
result = _CFBundleCopyAllBundles();
1645-
pthread_mutex_lock(&CFBundleGlobalDataLock);
1645+
__CFLock(&CFBundleGlobalDataLock);
16461646
if (_lastBundleList) {
16471647
// Another thread beat us here
16481648
CFRelease(result);
@@ -1651,25 +1651,25 @@ CFArrayRef CFBundleGetAllBundles(void) {
16511651
}
16521652
} else if (!CFEqual(_lastBundleList, _allBundles)) {
16531653
// Check if the list of bundles has changed
1654-
pthread_mutex_unlock(&CFBundleGlobalDataLock);
1654+
__CFUnlock(&CFBundleGlobalDataLock);
16551655
result = _CFBundleCopyAllBundles();
1656-
pthread_mutex_lock(&CFBundleGlobalDataLock);
1656+
__CFLock(&CFBundleGlobalDataLock);
16571657
// note: intentionally leak the last value in _lastBundleList, due to API contract of 'get'
16581658
_lastBundleList = result;
16591659
}
16601660
result = _lastBundleList;
1661-
pthread_mutex_unlock(&CFBundleGlobalDataLock);
1661+
__CFUnlock(&CFBundleGlobalDataLock);
16621662
return result;
16631663
}
16641664

16651665
CF_EXPORT CFArrayRef _CFBundleCopyAllBundles(void) {
16661666
// To answer this properly, we have to have created the static bundles!
16671667
_CFBundleEnsureAllBundlesUpToDate();
16681668
CFBundleRef main = CFBundleGetMainBundle();
1669-
pthread_mutex_lock(&CFBundleGlobalDataLock);
1669+
__CFLock(&CFBundleGlobalDataLock);
16701670
// _allBundles does not include the main bundle, so insert it here.
16711671
CFMutableArrayRef bundles = CFArrayCreateMutableCopy(kCFAllocatorSystemDefault, CFArrayGetCount(_allBundles) + 1, _allBundles);
1672-
pthread_mutex_unlock(&CFBundleGlobalDataLock);
1672+
__CFUnlock(&CFBundleGlobalDataLock);
16731673
CFArrayInsertValueAtIndex(bundles, 0, main);
16741674
return bundles;
16751675
}

CoreFoundation/PlugIn.subproj/CFBundle_Main.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,9 @@ static CFBundleRef _CFBundleGetMainBundleAlreadyLocked(void) {
124124
// Perform delayed final processing steps.
125125
// This must be done after _isLoaded has been set, for security reasons (3624341).
126126
// It is safe to unlock and re-lock here because we don't really do anything under the lock after we are done. It is just re-locked to satisfy the 'already locked' contract.
127-
pthread_mutex_unlock(&_mainBundleLock);
127+
__CFUnlock(&_mainBundleLock);
128128
_CFBundleInitPlugIn(_mainBundle);
129-
pthread_mutex_lock(&_mainBundleLock);
129+
__CFLock(&_mainBundleLock);
130130
}
131131
}
132132
if (bundleURL) CFRelease(bundleURL);
@@ -164,8 +164,8 @@ CF_EXPORT CFURLRef _CFBundleCopyMainBundleExecutableURL(Boolean *looksLikeBundle
164164

165165
CF_EXPORT CFBundleRef CFBundleGetMainBundle(void) {
166166
CFBundleRef mainBundle;
167-
pthread_mutex_lock(&_mainBundleLock);
167+
__CFLock(&_mainBundleLock);
168168
mainBundle = _CFBundleGetMainBundleAlreadyLocked();
169-
pthread_mutex_unlock(&_mainBundleLock);
169+
__CFUnlock(&_mainBundleLock);
170170
return mainBundle;
171171
}

CoreFoundation/PlugIn.subproj/CFBundle_Tables.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ CF_EXPORT CFBundleRef CFBundleGetBundleWithIdentifier(CFStringRef bundleID) {
176176
}
177177
}
178178
}
179-
pthread_mutex_lock(&CFBundleGlobalDataLock);
179+
__CFLock(&CFBundleGlobalDataLock);
180180
result = _CFBundlePrimitiveGetBundleWithIdentifierAlreadyLocked(bundleID);
181181
#if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_EMBEDDED_MINI
182182
if (!result) {
@@ -208,15 +208,15 @@ CF_EXPORT CFBundleRef CFBundleGetBundleWithIdentifier(CFStringRef bundleID) {
208208
_CFBundleEnsureBundlesUpToDateWithHintAlreadyLocked(bundleID);
209209
result = _CFBundlePrimitiveGetBundleWithIdentifierAlreadyLocked(bundleID);
210210
}
211-
pthread_mutex_unlock(&CFBundleGlobalDataLock);
211+
__CFUnlock(&CFBundleGlobalDataLock);
212212
}
213213

214214
if (!result) {
215-
pthread_mutex_lock(&CFBundleGlobalDataLock);
215+
__CFLock(&CFBundleGlobalDataLock);
216216
// Make sure all bundles have been created and try again.
217217
_CFBundleEnsureAllBundlesUpToDateAlreadyLocked();
218218
result = _CFBundlePrimitiveGetBundleWithIdentifierAlreadyLocked(bundleID);
219-
pthread_mutex_unlock(&CFBundleGlobalDataLock);
219+
__CFUnlock(&CFBundleGlobalDataLock);
220220
}
221221

222222
return result;

0 commit comments

Comments
 (0)