Skip to content

Commit 4aaaa36

Browse files
authored
Add #ifdef conditions to CFPlatform.c for WASI (swiftlang#3028)
WASI doesn't support multi-threading and process info or environment variables. Here we `#ifdef` relevant code out, or replace with stubs that allow Foundation to be compiled for WASI.
1 parent f4cd0d1 commit 4aaaa36

File tree

1 file changed

+46
-11
lines changed

1 file changed

+46
-11
lines changed

CoreFoundation/Base.subproj/CFPlatform.c

+46-11
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,11 @@ int _CFArgc(void) { return *_NSGetArgc(); }
5959
#endif
6060

6161

62+
#if !TARGET_OS_WASI
6263
CF_PRIVATE Boolean _CFGetCurrentDirectory(char *path, int maxlen) {
6364
return getcwd(path, maxlen) != NULL;
6465
}
66+
#endif
6567

6668
#if TARGET_OS_WIN32
6769
// Returns the path to the CF DLL, which we can then use to find resources like char sets
@@ -96,8 +98,9 @@ CF_PRIVATE const wchar_t *_CFDLLPath(void) {
9698
}
9799
return cachedPath;
98100
}
99-
#endif
101+
#endif // TARGET_OS_WIN32
100102

103+
#if !TARGET_OS_WASI
101104
static const char *__CFProcessPath = NULL;
102105
static const char *__CFprogname = NULL;
103106

@@ -242,6 +245,7 @@ const char *_CFProcessPath(void) {
242245
return __CFProcessPath;
243246
#endif
244247
}
248+
#endif // TARGET_OS_WASI
245249

246250
#if TARGET_OS_MAC || TARGET_OS_WIN32 || TARGET_OS_BSD
247251
CF_CROSS_PLATFORM_EXPORT Boolean _CFIsMainThread(void) {
@@ -255,13 +259,14 @@ CF_CROSS_PLATFORM_EXPORT Boolean _CFIsMainThread(void) {
255259
#include <syscall.h>
256260
#else
257261
#include <sys/syscall.h>
258-
#endif
262+
#endif // __has_include(<syscall.h>)
259263

260264
Boolean _CFIsMainThread(void) {
261265
return syscall(SYS_gettid) == getpid();
262266
}
263-
#endif
267+
#endif // TARGET_OS_LINUX
264268

269+
#if !TARGET_OS_WASI
265270
CF_PRIVATE CFStringRef _CFProcessNameString(void) {
266271
static CFStringRef __CFProcessNameString = NULL;
267272
if (!__CFProcessNameString) {
@@ -274,7 +279,7 @@ CF_PRIVATE CFStringRef _CFProcessNameString(void) {
274279
}
275280
return __CFProcessNameString;
276281
}
277-
282+
#endif // !TARGET_OS_WASI
278283

279284
#if TARGET_OS_MAC || TARGET_OS_LINUX || TARGET_OS_BSD
280285

@@ -369,7 +374,7 @@ static CFURLRef _CFCopyHomeDirURLForUser(const char *username, bool fallBackToHo
369374

370375
#endif
371376

372-
377+
#if !TARGET_OS_WASI
373378
#define CFMaxHostNameLength 256
374379
#define CFMaxHostNameSize (CFMaxHostNameLength+1)
375380

@@ -582,6 +587,7 @@ CF_EXPORT CFURLRef CFCopyHomeDirectoryURLForUser(CFStringRef uName) {
582587

583588
#undef CFMaxHostNameLength
584589
#undef CFMaxHostNameSize
590+
#endif // !TARGET_OS_WASI
585591

586592
#if TARGET_OS_WIN32
587593
CF_INLINE CFIndex strlen_UniChar(const UniChar* p) {
@@ -720,11 +726,17 @@ CF_PRIVATE void __CFTSDWindowsCleanup() {
720726

721727
static _CFThreadSpecificKey __CFTSDIndexKey;
722728

729+
#if TARGET_OS_WASI
730+
static void *__CFThreadSpecificData;
731+
#endif
732+
723733
CF_PRIVATE void __CFTSDInitialize() {
734+
#if !TARGET_OS_WASI
724735
static dispatch_once_t once;
725736
dispatch_once(&once, ^{
726737
(void)pthread_key_create(&__CFTSDIndexKey, __CFTSDFinalize);
727738
});
739+
#endif
728740
}
729741

730742
#endif
@@ -736,6 +748,8 @@ static void __CFTSDSetSpecific(void *arg) {
736748
pthread_setspecific(__CFTSDIndexKey, arg);
737749
#elif TARGET_OS_WIN32
738750
FlsSetValue(__CFTSDIndexKey, arg);
751+
#elif TARGET_OS_WASI
752+
__CFThreadSpecificData = arg;
739753
#endif
740754
}
741755

@@ -746,16 +760,22 @@ static void *__CFTSDGetSpecific() {
746760
return pthread_getspecific(__CFTSDIndexKey);
747761
#elif TARGET_OS_WIN32
748762
return FlsGetValue(__CFTSDIndexKey);
763+
#elif TARGET_OS_WASI
764+
return __CFThreadSpecificData;
749765
#endif
750766
}
751767

752768
_Atomic(bool) __CFMainThreadHasExited = false;
753769

754770
static void __CFTSDFinalize(void *arg) {
771+
#if TARGET_OS_WASI
772+
__CFMainThreadHasExited = true;
773+
#else
755774
if (pthread_main_np() == 1) {
756775
// 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.
757776
__CFMainThreadHasExited = true;
758777
}
778+
#endif
759779

760780
// Set our TSD so we're called again by pthreads. It will call the destructor PTHREAD_DESTRUCTOR_ITERATIONS times as long as a value is set in the thread specific data. We handle each case below.
761781
__CFTSDSetSpecific(arg);
@@ -779,7 +799,7 @@ static void __CFTSDFinalize(void *arg) {
779799
}
780800
}
781801

782-
#if _POSIX_THREADS
802+
#if _POSIX_THREADS && !TARGET_OS_WASI
783803
if (table->destructorCount == PTHREAD_DESTRUCTOR_ITERATIONS - 1) { // On PTHREAD_DESTRUCTOR_ITERATIONS-1 call, destroy our data
784804
free(table);
785805

@@ -1303,9 +1323,9 @@ CF_PRIVATE int _NS_gettimeofday(struct timeval *tv, struct timezone *tz) {
13031323
#endif // TARGET_OS_WIN32
13041324

13051325
#pragma mark -
1306-
#pragma mark Linux OSAtomic
1326+
#pragma mark Linux, BSD, and WASI OSAtomic
13071327

1308-
#if TARGET_OS_LINUX || TARGET_OS_BSD
1328+
#if TARGET_OS_LINUX || TARGET_OS_BSD || TARGET_OS_WASI
13091329

13101330
bool OSAtomicCompareAndSwapPtr(void *oldp, void *newp, void *volatile *dst)
13111331
{
@@ -1364,7 +1384,7 @@ void OSMemoryBarrier() {
13641384
__sync_synchronize();
13651385
}
13661386

1367-
#endif // TARGET_OS_LINUX
1387+
#endif // TARGET_OS_LINUX || TARGET_OS_BSD || TARGET_OS_WASI
13681388

13691389
#pragma mark -
13701390
#pragma mark Dispatch Replacements
@@ -1378,11 +1398,15 @@ typedef struct _CF_sema_s {
13781398
} * _CF_sema_t;
13791399

13801400
CF_INLINE void _CF_sem_signal(_CF_sema_t s) {
1401+
#if !TARGET_OS_WASI
13811402
sem_post(&s->sema);
1403+
#endif
13821404
}
13831405

13841406
CF_INLINE void _CF_sem_wait(_CF_sema_t s) {
1407+
#if !TARGET_OS_WASI
13851408
sem_wait(&s->sema);
1409+
#endif
13861410
}
13871411

13881412
static void _CF_sem_destroy(_CF_sema_t s) {
@@ -1391,6 +1415,7 @@ static void _CF_sem_destroy(_CF_sema_t s) {
13911415

13921416
CF_INLINE _CFThreadSpecificKey _CF_thread_sem_key() {
13931417
static _CFThreadSpecificKey key = 0;
1418+
#if !TARGET_OS_WASI
13941419
static OSSpinLock lock = OS_SPINLOCK_INIT;
13951420
if (key == 0) {
13961421
OSSpinLockLock(&lock);
@@ -1399,9 +1424,11 @@ CF_INLINE _CFThreadSpecificKey _CF_thread_sem_key() {
13991424
}
14001425
OSSpinLockUnlock(&lock);
14011426
}
1427+
#endif
14021428
return key;
14031429
}
14041430

1431+
#if !TARGET_OS_WASI
14051432
CF_INLINE _CF_sema_t _CF_get_thread_semaphore() {
14061433
_CFThreadSpecificKey key = _CF_thread_sem_key();
14071434
_CF_sema_t s = (_CF_sema_t)pthread_getspecific(key);
@@ -1416,6 +1443,7 @@ CF_INLINE _CF_sema_t _CF_get_thread_semaphore() {
14161443
CF_INLINE void _CF_put_thread_semaphore(_CF_sema_t s) {
14171444
pthread_setspecific(_CF_thread_sem_key(), s);
14181445
}
1446+
#endif
14191447

14201448
#define CF_DISPATCH_ONCE_DONE ((_CF_dispatch_once_waiter_t)~0l)
14211449

@@ -1435,6 +1463,12 @@ defined(__arm64__)
14351463
#endif
14361464

14371465
void _CF_dispatch_once(dispatch_once_t *predicate, void (^block)(void)) {
1466+
#if TARGET_OS_WASI
1467+
if (!*predicate) {
1468+
block();
1469+
*predicate = 1;
1470+
}
1471+
#else
14381472
_CF_dispatch_once_waiter_t volatile *vval = (_CF_dispatch_once_waiter_t*)predicate;
14391473
struct _CF_dispatch_once_waiter_s dow = { NULL };
14401474
_CF_dispatch_once_waiter_t tail = &dow, next, tmp;
@@ -1465,6 +1499,7 @@ void _CF_dispatch_once(dispatch_once_t *predicate, void (^block)(void)) {
14651499
}
14661500
_CF_put_thread_semaphore(dow.dow_sema);
14671501
}
1502+
#endif // TARGET_OS_WASI
14681503
}
14691504

14701505
#endif
@@ -1689,7 +1724,7 @@ CF_EXPORT char **_CFEnviron(void) {
16891724
#elif TARGET_OS_WIN32
16901725
return _environ;
16911726
#else
1692-
#if TARGET_OS_BSD
1727+
#if TARGET_OS_BSD || TARGET_OS_WASI
16931728
extern char **environ;
16941729
#endif
16951730
return environ;
@@ -2056,7 +2091,7 @@ CF_EXPORT int _CFPosixSpawn(pid_t *_CF_RESTRICT pid, const char *_CF_RESTRICT pa
20562091
return _CFPosixSpawnImpl(pid, path, file_actions, attrp, argv, envp);
20572092
}
20582093

2059-
#elif !TARGET_OS_WIN32
2094+
#elif !TARGET_OS_WIN32 && !TARGET_OS_WASI
20602095

20612096
#include <spawn.h>
20622097

0 commit comments

Comments
 (0)