Skip to content

Commit 5f57e70

Browse files
authored
Omnibus to implement CFRunLoop with kevent. (#3004)
* [runloop] Set up some symbols for generic code. CFRunLoop has a recurring abstraction for platform-specific code, but it is a little leaky. Plug these leaks: ensure `MACH_PORT_NULL` in the generic, non-platform context is rewritten as `CFPORT_NULL`, and ensure that `kern_return_t` and `KERN_SUCCESS` are defined properly. * [runloop] Remind porter these stubs are required. New platforms must define these types and functions; it is incorrect to not have #else cases here. * [runloop] Implement runloop abstraction for BSD. * [runloop] Ensure timing arithmetic is done in ns. Fixes SR-14288. * [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 9bc1a18 commit 5f57e70

File tree

3 files changed

+333
-23
lines changed

3 files changed

+333
-23
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)