Skip to content

Commit 0b1dbef

Browse files
committed
POSIX: guard the inclusion of pthread.h
Not all deployment targets use POSIX threads (pthreads). Use the POSIX mandated macro `_POSIX_THREADS` to detect whether POSIX threads are in use. Conditionally include `unistd.h` which is the Unix standard header which provides the definition for `_POSIX_THREADS` if the platform uses POSIX threads. Unfortunately, clang does not define `__unix__` for macOS (Bob Wilson does not recall why this was the case, and we have yet to test if we can get away with defining `__unix__` for the platform without breaking existing software).
1 parent 835259e commit 0b1dbef

File tree

8 files changed

+36
-9
lines changed

8 files changed

+36
-9
lines changed

CoreFoundation/AppServices.subproj/CFUserNotification.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,18 @@
1717
#include "CFRuntime_Internal.h"
1818
#include <CoreFoundation/CFMachPort.h>
1919
#include <stdlib.h>
20-
#include <unistd.h>
2120
#include <stdio.h>
2221
#include <mach/mach.h>
2322
#include <mach/error.h>
2423
#include <limits.h>
2524
#include <errno.h>
25+
26+
#if __has_include(<unistd.h>)
27+
#include <unistd.h>
28+
#endif
29+
#if _POSIX_THREADS
2630
#include <pthread.h>
31+
#endif
2732

2833
#define CFUserNotificationLog(alertHeader, alertMessage) CFLog(3, CFSTR("%@: %@"), alertHeader, alertMessage);
2934

CoreFoundation/Base.subproj/CFBase.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@
1111
#include <CoreFoundation/CFBase.h>
1212
#include "CFInternal.h"
1313
#include "CFRuntime_Internal.h"
14-
#if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_EMBEDDED_MINI || DEPLOYMENT_TARGET_LINUX || DEPLOYMENT_TARGET_FREEBSD
14+
#if __has_include(<unistd.h>)
15+
#include <unistd.h>
16+
#endif
17+
#if _POSIX_THREADS
1518
#include <pthread.h>
1619
#endif
1720
#if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_EMBEDDED_MINI

CoreFoundation/Base.subproj/CFInternal.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,17 @@ CF_EXTERN_C_BEGIN
101101
#include <xlocale.h>
102102
#endif // !TARGET_OS_CYGWIN && !defined(__linux__)
103103

104-
#include <unistd.h>
105104
#include <sys/time.h>
106105
#include <signal.h>
107106
#include <stdio.h>
108107
#endif // TARGET_OS_MAC || TARGET_OS_LINUX || TARGET_OS_BSD
109108

109+
#if __has_include(<unistd.h>)
110+
#include <unistd.h>
111+
#endif
112+
#if _POSIX_THREADS
110113
#include <pthread.h>
114+
#endif
111115

112116
#if !DEPLOYMENT_RUNTIME_SWIFT && __has_include(<os/log.h>)
113117
#include <os/log.h>

CoreFoundation/Base.subproj/CFPriv.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -567,7 +567,10 @@ CF_EXPORT CFMessagePortRef _CFMessagePortCreateLocalEx(CFAllocatorRef allocator,
567567

568568
#endif
569569

570-
#if TARGET_OS_MAC || TARGET_OS_EMBEDDED || TARGET_OS_IPHONE
570+
#if __has_include(<unistd.h>)
571+
#include <unistd.h>
572+
#endif
573+
#if _POSIX_THREADS
571574
#include <pthread.h>
572575
#elif !TARGET_OS_LINUX
573576
// Avoid including the pthread header

CoreFoundation/Base.subproj/CFUtilities.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
#include <unistd.h>
4141
#include <sys/uio.h>
4242
#include <mach/mach.h>
43-
#include <pthread.h>
4443
#include <mach-o/loader.h>
4544
#include <mach-o/dyld.h>
4645
#include <crt_externs.h>
@@ -61,12 +60,15 @@
6160

6261
#if DEPLOYMENT_TARGET_LINUX || DEPLOYMENT_TARGET_FREEBSD
6362
#include <string.h>
64-
#include <pthread.h>
6563
#include <sys/mman.h>
66-
#include <unistd.h>
6764
#endif
6865

69-
66+
#if __has_include(<unistd.h>)
67+
#include <unistd.h>
68+
#endif
69+
#if _POSIX_THREADS
70+
#include <pthread.h>
71+
#endif
7072

7173

7274

CoreFoundation/Base.subproj/CoreFoundation_Prefix.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,6 @@ CF_EXPORT int _NS_access(const char *name, int amode);
318318
#define FD_SETSIZE 1024
319319
#include <winsock2.h>
320320
#include <windows.h>
321-
#include <pthread.h>
322321

323322
#undef BOOL
324323

CoreFoundation/Base.subproj/ForSwiftFoundationOnly.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,12 @@
2626
#include <CoreFoundation/CFURLSessionInterface.h>
2727
#include <CoreFoundation/ForFoundationOnly.h>
2828
#include <fts.h>
29+
#if __has_include(<unistd.h>)
30+
#include <unistd.h>
31+
#endif
32+
#if _POSIX_THREADS
2933
#include <pthread.h>
34+
#endif
3035
#include <dirent.h>
3136

3237
#include <CoreFoundation/CFCalendar_Internal.h>

CoreFoundation/RunLoop.subproj/CFRunLoop.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,13 @@
1919
#include <math.h>
2020
#include <stdio.h>
2121
#include <limits.h>
22+
23+
#if __has_include(<unistd.h>)
24+
#include <unistd.h>
25+
#endif
26+
#if _POSIX_THREADS
2227
#include <pthread.h>
28+
#endif
2329
#if __HAS_DISPATCH__
2430
#include <dispatch/dispatch.h>
2531
#endif

0 commit comments

Comments
 (0)