Skip to content

CoreFoundation: introduce _CFMutex, _CFRecursiveMutex #1811

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions CoreFoundation/Base.subproj/CFInternal.h
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,84 @@ static void os_unfair_lock_unlock(os_unfair_lock_t lock) { pthread_mutex_unlock(
#define os_unfair_lock_unlock __CFUnlock
#endif // __has_include(<os/lock.h>)

#if _POSIX_THREADS
typedef pthread_mutex_t _CFMutex;
#define _CF_MUTEX_STATIC_INITIALIZER PTHREAD_MUTEX_INITIALIZER
static int _CFMutexCreate(_CFMutex *lock) {
return pthread_mutex_init(lock, NULL);
}
static int _CFMutexDestroy(_CFMutex *lock) {
return pthread_mutex_destroy(lock);
}
static int _CFMutexLock(_CFMutex *lock) {
return pthread_mutex_lock(lock);
}
static int _CFMutexUnlock(_CFMutex *lock) {
return pthread_mutex_unlock(lock);
}

typedef pthread_mutex_t _CFRecursiveMutex;
static int _CFRecursiveMutexCreate(_CFRecursiveMutex *mutex) {
pthread_mutexattr_t attr;
pthread_mutexattr_init(&attr);
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);

int result = pthread_mutex_init(mutex, &attr);

pthread_mutexattr_destroy(&attr);

return result;
}
static int _CFRecursiveMutexDestroy(_CFRecursiveMutex *mutex) {
return pthread_mutex_destroy(mutex);
}
static int _CFRecursiveMutexLock(_CFRecursiveMutex *mutex) {
return pthread_mutex_lock(mutex);
}
static int _CFRecursiveMutexUnlock(_CFRecursiveMutex *mutex) {
return pthread_mutex_unlock(mutex);
}
#elif defined(_WIN32)
typedef SRWLOCK _CFMutex;
#define _CF_MUTEX_STATIC_INITIALIZER SRWLOCK_INIT
static int _CFMutexCreate(_CFMutex *lock) {
InitializeSRWLock(lock);
return 0;
}
static int _CFMutexDestroy(_CFMutex *lock) {
(void)lock;
return 0;
}
static int _CFMutexLock(_CFMutex *lock) {
AcquireSRWLockExclusive(lock);
return 0;
}
static int _CFMutexUnlock(_CFMutex *lock) {
ReleaseSRWLockExclusive(lock);
return 0;
}

typedef CRITICAL_SECTION _CFRecursiveMutex;
static int _CFRecursiveMutexCreate(_CFRecursiveMutex *mutex) {
InitializeCriticalSection(mutex);
return 0;
}
static int _CFRecursiveMutexDestroy(_CFRecursiveMutex *mutex) {
DeleteCriticalSection(mutex);
return 0;
}
static int _CFRecursiveMutexLock(_CFRecursiveMutex *mutex) {
EnterCriticalSection(mutex);
return 0;
}
static int _CFRecursiveMutexUnlock(_CFRecursiveMutex *mutex) {
LeaveCriticalSection(mutex);
return 0;
}
#else
#error "do not know how to define mutex and recursive mutex for this OS"
#endif

#if !__HAS_DISPATCH__

typedef volatile long dispatch_once_t;
Expand Down
6 changes: 3 additions & 3 deletions CoreFoundation/Locale.subproj/CFDateFormatter.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ CFArrayRef CFDateFormatterCreateDateFormatsFromTemplates(CFAllocatorRef allocato

static Boolean useTemplatePatternGenerator(CFLocaleRef locale, void(^work)(UDateTimePatternGenerator *ptg)) {
static UDateTimePatternGenerator *ptg;
static pthread_mutex_t ptgLock = PTHREAD_MUTEX_INITIALIZER;
static _CFMutex ptgLock = _CF_MUTEX_STATIC_INITIALIZER;
static const char *ptgLocaleName;
CFStringRef ln = locale ? CFLocaleGetIdentifier(locale) : CFSTR("");
char buffer[BUFFER_SIZE];
Expand All @@ -73,7 +73,7 @@ static Boolean useTemplatePatternGenerator(CFLocaleRef locale, void(^work)(UDate
free((void *)ptgLocaleName);
ptgLocaleName = NULL;
};
pthread_mutex_lock(&ptgLock);
_CFMutexLock(&ptgLock);
if (ptgLocaleName && strcmp(ptgLocaleName, localeName) != 0) {
flushCache();
}
Expand All @@ -88,7 +88,7 @@ static Boolean useTemplatePatternGenerator(CFLocaleRef locale, void(^work)(UDate
if (result && work) {
work(ptg);
}
pthread_mutex_unlock(&ptgLock);
_CFMutexUnlock(&ptgLock);
return result;
}

Expand Down
Loading