Skip to content

Commit 13bc94f

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 05ceecf commit 13bc94f

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

Diff for: CoreFoundation/Base.subproj/CFPlatform.c

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

250250
#if TARGET_OS_MAC || TARGET_OS_WIN32 || TARGET_OS_BSD
251251
CF_CROSS_PLATFORM_EXPORT Boolean _CFIsMainThread(void) {
252+
#if defined(__OpenBSD__)
253+
return pthread_equal(pthread_self(), _CFMainPThread) != 0;
254+
#else
252255
return pthread_main_np() == 1;
256+
#endif
253257
}
254258
#endif
255259

@@ -774,7 +778,7 @@ static void __CFTSDFinalize(void *arg) {
774778
#if TARGET_OS_WASI
775779
__CFMainThreadHasExited = true;
776780
#else
777-
if (pthread_main_np() == 1) {
781+
if (_CFIsMainThread()) {
778782
// 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.
779783
__CFMainThreadHasExited = true;
780784
}

0 commit comments

Comments
 (0)