Skip to content

Commit 26ac266

Browse files
committed
[runloop] Avoid pthread_main_np on OpenBSD.
During runloop testing, log messages suggest that the runloop thinks the main thread has exited and certain runloop operations don't actually proceed. The flag for denoting when the main thread has exited is set in the thread-specific data destructor `__CFTSDFinalize`. This function detects whether the thread in question is the main thread via `pthread_main_np`, and has been observed in debugging this problem to return 1 on apparently non-main threads on OpenBSD. This obviously will cause the main thread exited flag to be erroneously set when non-main threads complete their work and dispose of thread-specific data. Instead, use the existing `_CFMainPThread` symbol set in `__CFInitialize` to determine the main thread. I am not sure whether the platform `pthread_main_np` is at fault here, but since this workaround makes the tests pass, let's use it.
1 parent d059d1c commit 26ac266

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

CoreFoundation/Base.subproj/CFPlatform.c

+5-1
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,11 @@ const char *_CFProcessPath(void) {
192192

193193
#if TARGET_OS_MAC || TARGET_OS_WIN32 || TARGET_OS_BSD
194194
CF_CROSS_PLATFORM_EXPORT Boolean _CFIsMainThread(void) {
195+
#if !defined(__OpenBSD__)
195196
return pthread_main_np() == 1;
197+
#else
198+
return pthread_equal(pthread_self(), _CFMainPThread) != 0;
199+
#endif
196200
}
197201
#endif
198202

@@ -699,7 +703,7 @@ static void *__CFTSDGetSpecific() {
699703
_Atomic(bool) __CFMainThreadHasExited = false;
700704

701705
static void __CFTSDFinalize(void *arg) {
702-
if (pthread_main_np() == 1) {
706+
if (_CFIsMainThread()) {
703707
// Important: we need to be sure that the only time we set this flag to true is when we actually can guarentee we ARE the main thread.
704708
__CFMainThreadHasExited = true;
705709
}

0 commit comments

Comments
 (0)