Skip to content

Commit 574039f

Browse files
committed
CoreFoundation: introduce _CFMutex, _CFRecursiveMutex
Introduce the `_CFMutex` and `_CFRecursiveMutex` types to allow us to differentiate between recursive and non-recursive mutexes and be able to replace them on different platforms. This codepath does not use `CFLock_t` as these are expected to be full mutexes rather than unfair locks.
1 parent a7f12d0 commit 574039f

File tree

7 files changed

+170
-109
lines changed

7 files changed

+170
-109
lines changed

CoreFoundation/Base.subproj/CFInternal.h

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,84 @@ static void os_unfair_lock_unlock(os_unfair_lock_t lock) { pthread_mutex_unlock(
592592
#define os_unfair_lock_unlock __CFUnlock
593593
#endif // __has_include(<os/lock.h>)
594594

595+
#if _POSIX_THREADS
596+
typedef pthread_mutex_t _CFMutex;
597+
#define _CF_MUTEX_STATIC_INITIALIZER PTHREAD_MUTEX_INITIALIZER
598+
static int _CFMutexCreate(_CFMutex *lock) {
599+
return pthread_mutex_init(lock, NULL);
600+
}
601+
static int _CFMutexDestroy(_CFMutex *lock) {
602+
return pthread_mutex_destroy(lock);
603+
}
604+
static int _CFMutexLock(_CFMutex *lock) {
605+
return pthread_mutex_lock(lock);
606+
}
607+
static int _CFMutexUnlock(_CFMutex *lock) {
608+
return pthread_mutex_unlock(lock);
609+
}
610+
611+
typedef pthread_mutex_t _CFRecursiveMutex;
612+
static int _CFRecursiveMutexCreate(_CFRecursiveMutex *mutex) {
613+
pthread_mutexattr_t attr;
614+
pthread_mutexattr_init(&attr);
615+
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
616+
617+
int result = pthread_mutex_init(mutex, &attr);
618+
619+
pthread_mutexattr_destroy(&attr);
620+
621+
return result;
622+
}
623+
static int _CFRecursiveMutexDestroy(_CFRecursiveMutex *mutex) {
624+
return pthread_mutex_destroy(mutex);
625+
}
626+
static int _CFRecursiveMutexLock(_CFRecursiveMutex *mutex) {
627+
return pthread_mutex_lock(mutex);
628+
}
629+
static int _CFRecursiveMutexUnlock(_CFRecursiveMutex *mutex) {
630+
return pthread_mutex_unlock(mutex);
631+
}
632+
#elif defined(_WIN32)
633+
typedef SRWLOCK _CFMutex;
634+
#define _CF_MUTEX_STATIC_INITIALIZER SRWLOCK_INIT
635+
static int _CFMutexCreate(_CFMutex *lock) {
636+
InitializeSRWLock(lock);
637+
return 0;
638+
}
639+
static int _CFMutexDestroy(_CFMutex *lock) {
640+
(void)lock;
641+
return 0;
642+
}
643+
static int _CFMutexLock(_CFMutex *lock) {
644+
AcquireSRWLockExclusive(lock);
645+
return 0;
646+
}
647+
static int _CFMutexUnlock(_CFMutex *lock) {
648+
ReleaseSRWLockExclusive(lock);
649+
return 0;
650+
}
651+
652+
typedef CRITICAL_SECTION _CFRecursiveMutex;
653+
static int _CFRecursiveMutexCreate(_CFRecursiveMutex *mutex) {
654+
InitializeCriticalSection(mutex);
655+
return 0;
656+
}
657+
static int _CFRecursiveMutexDestroy(_CFRecursiveMutex *mutex) {
658+
DeleteCriticalSection(mutex);
659+
return 0;
660+
}
661+
static int _CFRecursiveMutexLock(_CFRecursiveMutex *mutex) {
662+
EnterCriticalSection(mutex);
663+
return 0;
664+
}
665+
static int _CFRecursiveMutexUnlock(_CFRecursiveMutex *mutex) {
666+
LeaveCriticalSection(mutex);
667+
return 0;
668+
}
669+
#else
670+
#error "do not know how to define mutex and recursive mutex for this OS"
671+
#endif
672+
595673
#if !__HAS_DISPATCH__
596674

597675
typedef volatile long dispatch_once_t;

CoreFoundation/Locale.subproj/CFDateFormatter.c

Lines changed: 3 additions & 3 deletions
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 _CFMutex ptgLock = _CF_MUTEX_STATIC_INITIALIZER;
6262
static const char *ptgLocaleName;
6363
CFStringRef ln = locale ? CFLocaleGetIdentifier(locale) : CFSTR("");
6464
char buffer[BUFFER_SIZE];
@@ -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+
_CFMutexLock(&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+
_CFMutexUnlock(&ptgLock);
9292
return result;
9393
}
9494

0 commit comments

Comments
 (0)