Skip to content

Commit 8060f93

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 50b7086 commit 8060f93

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
@@ -587,6 +587,84 @@ static void os_unfair_lock_lock(os_unfair_lock_t lock) { pthread_mutex_lock(lock
587587
static void os_unfair_lock_unlock(os_unfair_lock_t lock) { pthread_mutex_unlock(lock); }
588588
#endif // __has_include(<os/lock.h>)
589589

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

592670
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)