Skip to content

Commit f8cb6a4

Browse files
committed
Base, Locale, PlugIn, RunLoop: spell pthread as CF
Use `CFLock_t` instead of `pthread_mutex_t`. This has a small semantic change on Linux where we now use pthreads rather than a cmpxchg to implement locking. This makes the behaviour of POSIX-y systems and Darwin more uniform. Additionally, we now generically target pthreads on any system that supports POSIX threading. Windows continues to behave differently (though should replace the cmpxchg in favour of SWRLOCK). This leaves the open question of why do we not use `pthread_spinlock_t` to implement the libkern `OSSpinLock*` family of functions. If we do so, we should be able to fold away the differences on POSIX and Darwin systems.
1 parent 50b7086 commit f8cb6a4

File tree

6 files changed

+29
-36
lines changed

6 files changed

+29
-36
lines changed

CoreFoundation/Base.subproj/CFInternal.h

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -522,53 +522,46 @@ typedef pthread_mutex_t CFLock_t;
522522

523523
#define __CFLockTry(LP) ({ pthread_mutex_trylock(LP) == 0; })
524524

525-
#elif DEPLOYMENT_TARGET_WINDOWS
525+
#elif _POSIX_THREADS
526526

527-
typedef int32_t CFLock_t;
528-
#define CFLockInit 0
527+
typedef pthread_mutex_t CFLock_t;
528+
#if DEPLOYMENT_TARGET_FREEBSD
529+
#define CFLockInit ((pthread_mutex_t)PTHREAD_MUTEX_INITIALIZER)
530+
#else
531+
#define CFLockInit ((pthread_mutex_t)PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP)
532+
#endif
529533
#define CF_LOCK_INIT_FOR_STRUCTS(X) (X = CFLockInit)
530534

531-
CF_INLINE void __CFLock(volatile CFLock_t *lock) {
532-
while (InterlockedCompareExchange((LONG volatile *)lock, ~0, 0) != 0) {
533-
Sleep(0);
534-
}
535-
}
536-
537-
CF_INLINE void __CFUnlock(volatile CFLock_t *lock) {
538-
MemoryBarrier();
539-
*lock = 0;
540-
}
535+
#define __CFLock(LP) ({ (void)pthread_mutex_lock(LP); })
536+
#define __CFUnlock(LP) ({ (void)pthread_mutex_unlock(LP); })
537+
#define __CFLockTry(LP) ({ pthread_mutex_trylock(LP) == 0; })
541538

542-
CF_INLINE Boolean __CFLockTry(volatile CFLock_t *lock) {
543-
return (InterlockedCompareExchange((LONG volatile *)lock, ~0, 0) == 0);
544-
}
539+
typedef int32_t OSSpinLock;
540+
#define OS_SPINLOCK_INIT 0
541+
#define OSSpinLockLock(lock) ({ while (__sync_val_compare_and_swap(lock, 0, ~0) != 0) sleep(0); })
542+
#define OSSpinLockUnlock(lock) ({ __sync_synchronize(); *lock = 0; })
545543

546-
#elif DEPLOYMENT_TARGET_LINUX || DEPLOYMENT_TARGET_FREEBSD
544+
#elif DEPLOYMENT_TARGET_WINDOWS
547545

548546
typedef int32_t CFLock_t;
549547
#define CFLockInit 0
550548
#define CF_LOCK_INIT_FOR_STRUCTS(X) (X = CFLockInit)
551549

552550
CF_INLINE void __CFLock(volatile CFLock_t *lock) {
553-
while (__sync_val_compare_and_swap(lock, 0, ~0) != 0) {
554-
sleep(0);
551+
while (InterlockedCompareExchange((LONG volatile *)lock, ~0, 0) != 0) {
552+
Sleep(0);
555553
}
556554
}
557555

558556
CF_INLINE void __CFUnlock(volatile CFLock_t *lock) {
559-
__sync_synchronize();
557+
MemoryBarrier();
560558
*lock = 0;
561559
}
562560

563561
CF_INLINE Boolean __CFLockTry(volatile CFLock_t *lock) {
564-
return (__sync_val_compare_and_swap(lock, 0, ~0) == 0);
562+
return (InterlockedCompareExchange((LONG volatile *)lock, ~0, 0) == 0);
565563
}
566564

567-
typedef CFLock_t OSSpinLock;
568-
#define OS_SPINLOCK_INIT CFLockInit
569-
#define OSSpinLockLock(lock) __CFLock(lock)
570-
#define OSSpinLockUnlock(lock) __CFUnlock(lock)
571-
572565
#else
573566

574567
#warning CF locks not defined for this platform -- CF is not thread-safe

CoreFoundation/Locale.subproj/CFDateFormatter.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ CFArrayRef CFDateFormatterCreateDateFormatsFromTemplates(CFAllocatorRef allocato
5858

5959
static Boolean useTemplatePatternGenerator(CFLocaleRef locale, void(^work)(UDateTimePatternGenerator *ptg)) {
6060
static UDateTimePatternGenerator *ptg;
61-
static pthread_mutex_t ptgLock = PTHREAD_MUTEX_INITIALIZER;
61+
static CFLock_t ptgLock = PTHREAD_MUTEX_INITIALIZER;
6262
static const char *ptgLocaleName;
6363
CFStringRef ln = locale ? CFLocaleGetIdentifier(locale) : CFSTR("");
6464
char buffer[BUFFER_SIZE];

CoreFoundation/PlugIn.subproj/CFBundle.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ CONST_STRING_DECL(_kCFBundleCFMLoadAsBundleKey, "CFBundleCFMLoadAsBundle")
119119
// Keys used by NSBundle for loaded Info plists.
120120
CONST_STRING_DECL(_kCFBundlePrincipalClassKey, "NSPrincipalClass")
121121

122-
static pthread_mutex_t CFBundleGlobalDataLock = PTHREAD_MUTEX_INITIALIZER;
122+
static CFLock_t CFBundleGlobalDataLock = PTHREAD_MUTEX_INITIALIZER;
123123

124124
static CFMutableDictionaryRef _bundlesByIdentifier = NULL;
125125
static CFMutableDictionaryRef _bundlesByURL = NULL;

CoreFoundation/PlugIn.subproj/CFBundle_Internal.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ struct __CFBundle {
109109

110110
_CFPlugInData _plugInData;
111111

112-
pthread_mutex_t _bundleLoadingLock;
112+
CFLock_t _bundleLoadingLock;
113113

114114
CFStringRef _executablePath; // Calculated and cached here
115115
CFStringRef _developmentRegion; // Calculated and cached here

CoreFoundation/PlugIn.subproj/CFBundle_Main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ static Boolean _initedMainBundle = false;
2424
static CFBundleRef _mainBundle = NULL;
2525
static char __CFBundleMainID__[1026] = {0};
2626
char *__CFBundleMainID = __CFBundleMainID__;
27-
static pthread_mutex_t _mainBundleLock = PTHREAD_MUTEX_INITIALIZER;
27+
static CFLock_t _mainBundleLock = PTHREAD_MUTEX_INITIALIZER;
2828

2929
#pragma mark -
3030

CoreFoundation/RunLoop.subproj/CFRunLoop.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -668,7 +668,7 @@ typedef struct __CFRunLoopMode *CFRunLoopModeRef;
668668

669669
struct __CFRunLoopMode {
670670
CFRuntimeBase _base;
671-
pthread_mutex_t _lock; /* must have the run loop locked before locking this */
671+
CFLock_t _lock; /* must have the run loop locked before locking this */
672672
CFStringRef _name;
673673
Boolean _stopped;
674674
char _padding[3];
@@ -781,7 +781,7 @@ typedef struct _per_run_data {
781781

782782
struct __CFRunLoop {
783783
CFRuntimeBase _base;
784-
pthread_mutex_t _lock; /* locked for accessing mode list */
784+
CFLock_t _lock; /* locked for accessing mode list */
785785
__CFPort _wakeUpPort; // used for CFRunLoopWakeUp
786786
Boolean _unused;
787787
volatile _per_run_data *_perRunData; // reset for runs of the run loop
@@ -892,7 +892,7 @@ CF_PRIVATE void __CFRunLoopDump() { // __private_extern__ to keep the compiler f
892892
CFRelease(desc);
893893
}
894894

895-
CF_INLINE void __CFRunLoopLockInit(pthread_mutex_t *lock) {
895+
CF_INLINE void __CFRunLoopLockInit(CFLock_t *lock) {
896896
pthread_mutexattr_t mattr;
897897
pthread_mutexattr_init(&mattr);
898898
pthread_mutexattr_settype(&mattr, PTHREAD_MUTEX_RECURSIVE);
@@ -1099,7 +1099,7 @@ CF_INLINE void __CFUnsetValid(void *cf) {
10991099

11001100
struct __CFRunLoopSource {
11011101
CFRuntimeBase _base;
1102-
pthread_mutex_t _lock;
1102+
CFLock_t _lock;
11031103
CFIndex _order; /* immutable */
11041104
CFMutableBagRef _runLoops;
11051105
union {
@@ -1135,7 +1135,7 @@ CF_INLINE void __CFRunLoopSourceUnlock(CFRunLoopSourceRef rls) {
11351135

11361136
struct __CFRunLoopObserver {
11371137
CFRuntimeBase _base;
1138-
pthread_mutex_t _lock;
1138+
CFLock_t _lock;
11391139
CFRunLoopRef _runLoop;
11401140
CFIndex _rlCount;
11411141
CFOptionFlags _activities; /* immutable */
@@ -1204,7 +1204,7 @@ static void __CFRunLoopObserverCancel(CFRunLoopObserverRef rlo, CFRunLoopRef rl,
12041204
struct __CFRunLoopTimer {
12051205
CFRuntimeBase _base;
12061206
uint16_t _bits;
1207-
pthread_mutex_t _lock;
1207+
CFLock_t _lock;
12081208
CFRunLoopRef _runLoop;
12091209
CFMutableSetRef _rlModes;
12101210
CFAbsoluteTime _nextFireDate;

0 commit comments

Comments
 (0)