@@ -59,9 +59,11 @@ int _CFArgc(void) { return *_NSGetArgc(); }
59
59
#endif
60
60
61
61
62
+ #if !TARGET_OS_WASI
62
63
CF_PRIVATE Boolean _CFGetCurrentDirectory (char * path , int maxlen ) {
63
64
return getcwd (path , maxlen ) != NULL ;
64
65
}
66
+ #endif
65
67
66
68
#if TARGET_OS_WIN32
67
69
// 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) {
96
98
}
97
99
return cachedPath ;
98
100
}
99
- #endif
101
+ #endif // TARGET_OS_WIN32
100
102
103
+ #if !TARGET_OS_WASI
101
104
static const char * __CFProcessPath = NULL ;
102
105
static const char * __CFprogname = NULL ;
103
106
@@ -242,6 +245,7 @@ const char *_CFProcessPath(void) {
242
245
return __CFProcessPath ;
243
246
#endif
244
247
}
248
+ #endif // TARGET_OS_WASI
245
249
246
250
#if TARGET_OS_MAC || TARGET_OS_WIN32 || TARGET_OS_BSD
247
251
CF_CROSS_PLATFORM_EXPORT Boolean _CFIsMainThread (void ) {
@@ -255,13 +259,14 @@ CF_CROSS_PLATFORM_EXPORT Boolean _CFIsMainThread(void) {
255
259
#include <syscall.h>
256
260
#else
257
261
#include <sys/syscall.h>
258
- #endif
262
+ #endif // __has_include(<syscall.h>)
259
263
260
264
Boolean _CFIsMainThread (void ) {
261
265
return syscall (SYS_gettid ) == getpid ();
262
266
}
263
- #endif
267
+ #endif // TARGET_OS_LINUX
264
268
269
+ #if !TARGET_OS_WASI
265
270
CF_PRIVATE CFStringRef _CFProcessNameString (void ) {
266
271
static CFStringRef __CFProcessNameString = NULL ;
267
272
if (!__CFProcessNameString ) {
@@ -274,7 +279,7 @@ CF_PRIVATE CFStringRef _CFProcessNameString(void) {
274
279
}
275
280
return __CFProcessNameString ;
276
281
}
277
-
282
+ #endif // !TARGET_OS_WASI
278
283
279
284
#if TARGET_OS_MAC || TARGET_OS_LINUX || TARGET_OS_BSD
280
285
@@ -369,7 +374,7 @@ static CFURLRef _CFCopyHomeDirURLForUser(const char *username, bool fallBackToHo
369
374
370
375
#endif
371
376
372
-
377
+ #if ! TARGET_OS_WASI
373
378
#define CFMaxHostNameLength 256
374
379
#define CFMaxHostNameSize (CFMaxHostNameLength+1)
375
380
@@ -582,6 +587,7 @@ CF_EXPORT CFURLRef CFCopyHomeDirectoryURLForUser(CFStringRef uName) {
582
587
583
588
#undef CFMaxHostNameLength
584
589
#undef CFMaxHostNameSize
590
+ #endif // !TARGET_OS_WASI
585
591
586
592
#if TARGET_OS_WIN32
587
593
CF_INLINE CFIndex strlen_UniChar (const UniChar * p ) {
@@ -720,11 +726,17 @@ CF_PRIVATE void __CFTSDWindowsCleanup() {
720
726
721
727
static _CFThreadSpecificKey __CFTSDIndexKey ;
722
728
729
+ #if TARGET_OS_WASI
730
+ static void * __CFThreadSpecificData ;
731
+ #endif
732
+
723
733
CF_PRIVATE void __CFTSDInitialize () {
734
+ #if !TARGET_OS_WASI
724
735
static dispatch_once_t once ;
725
736
dispatch_once (& once , ^{
726
737
(void )pthread_key_create (& __CFTSDIndexKey , __CFTSDFinalize );
727
738
});
739
+ #endif
728
740
}
729
741
730
742
#endif
@@ -736,6 +748,8 @@ static void __CFTSDSetSpecific(void *arg) {
736
748
pthread_setspecific (__CFTSDIndexKey , arg );
737
749
#elif TARGET_OS_WIN32
738
750
FlsSetValue (__CFTSDIndexKey , arg );
751
+ #elif TARGET_OS_WASI
752
+ __CFThreadSpecificData = arg ;
739
753
#endif
740
754
}
741
755
@@ -746,16 +760,22 @@ static void *__CFTSDGetSpecific() {
746
760
return pthread_getspecific (__CFTSDIndexKey );
747
761
#elif TARGET_OS_WIN32
748
762
return FlsGetValue (__CFTSDIndexKey );
763
+ #elif TARGET_OS_WASI
764
+ return __CFThreadSpecificData ;
749
765
#endif
750
766
}
751
767
752
768
_Atomic(bool ) __CFMainThreadHasExited = false;
753
769
754
770
static void __CFTSDFinalize (void * arg ) {
771
+ #if TARGET_OS_WASI
772
+ __CFMainThreadHasExited = true;
773
+ #else
755
774
if (pthread_main_np () == 1 ) {
756
775
// 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.
757
776
__CFMainThreadHasExited = true;
758
777
}
778
+ #endif
759
779
760
780
// 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.
761
781
__CFTSDSetSpecific (arg );
@@ -779,7 +799,7 @@ static void __CFTSDFinalize(void *arg) {
779
799
}
780
800
}
781
801
782
- #if _POSIX_THREADS
802
+ #if _POSIX_THREADS && ! TARGET_OS_WASI
783
803
if (table -> destructorCount == PTHREAD_DESTRUCTOR_ITERATIONS - 1 ) { // On PTHREAD_DESTRUCTOR_ITERATIONS-1 call, destroy our data
784
804
free (table );
785
805
@@ -1303,9 +1323,9 @@ CF_PRIVATE int _NS_gettimeofday(struct timeval *tv, struct timezone *tz) {
1303
1323
#endif // TARGET_OS_WIN32
1304
1324
1305
1325
#pragma mark -
1306
- #pragma mark Linux OSAtomic
1326
+ #pragma mark Linux, BSD, and WASI OSAtomic
1307
1327
1308
- #if TARGET_OS_LINUX || TARGET_OS_BSD
1328
+ #if TARGET_OS_LINUX || TARGET_OS_BSD || TARGET_OS_WASI
1309
1329
1310
1330
bool OSAtomicCompareAndSwapPtr (void * oldp , void * newp , void * volatile * dst )
1311
1331
{
@@ -1364,7 +1384,7 @@ void OSMemoryBarrier() {
1364
1384
__sync_synchronize ();
1365
1385
}
1366
1386
1367
- #endif // TARGET_OS_LINUX
1387
+ #endif // TARGET_OS_LINUX || TARGET_OS_BSD || TARGET_OS_WASI
1368
1388
1369
1389
#pragma mark -
1370
1390
#pragma mark Dispatch Replacements
@@ -1378,11 +1398,15 @@ typedef struct _CF_sema_s {
1378
1398
} * _CF_sema_t ;
1379
1399
1380
1400
CF_INLINE void _CF_sem_signal (_CF_sema_t s ) {
1401
+ #if !TARGET_OS_WASI
1381
1402
sem_post (& s -> sema );
1403
+ #endif
1382
1404
}
1383
1405
1384
1406
CF_INLINE void _CF_sem_wait (_CF_sema_t s ) {
1407
+ #if !TARGET_OS_WASI
1385
1408
sem_wait (& s -> sema );
1409
+ #endif
1386
1410
}
1387
1411
1388
1412
static void _CF_sem_destroy (_CF_sema_t s ) {
@@ -1391,6 +1415,7 @@ static void _CF_sem_destroy(_CF_sema_t s) {
1391
1415
1392
1416
CF_INLINE _CFThreadSpecificKey _CF_thread_sem_key () {
1393
1417
static _CFThreadSpecificKey key = 0 ;
1418
+ #if !TARGET_OS_WASI
1394
1419
static OSSpinLock lock = OS_SPINLOCK_INIT ;
1395
1420
if (key == 0 ) {
1396
1421
OSSpinLockLock (& lock );
@@ -1399,9 +1424,11 @@ CF_INLINE _CFThreadSpecificKey _CF_thread_sem_key() {
1399
1424
}
1400
1425
OSSpinLockUnlock (& lock );
1401
1426
}
1427
+ #endif
1402
1428
return key ;
1403
1429
}
1404
1430
1431
+ #if !TARGET_OS_WASI
1405
1432
CF_INLINE _CF_sema_t _CF_get_thread_semaphore () {
1406
1433
_CFThreadSpecificKey key = _CF_thread_sem_key ();
1407
1434
_CF_sema_t s = (_CF_sema_t )pthread_getspecific (key );
@@ -1416,6 +1443,7 @@ CF_INLINE _CF_sema_t _CF_get_thread_semaphore() {
1416
1443
CF_INLINE void _CF_put_thread_semaphore (_CF_sema_t s ) {
1417
1444
pthread_setspecific (_CF_thread_sem_key (), s );
1418
1445
}
1446
+ #endif
1419
1447
1420
1448
#define CF_DISPATCH_ONCE_DONE ((_CF_dispatch_once_waiter_t)~0l)
1421
1449
@@ -1435,6 +1463,12 @@ defined(__arm64__)
1435
1463
#endif
1436
1464
1437
1465
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
1438
1472
_CF_dispatch_once_waiter_t volatile * vval = (_CF_dispatch_once_waiter_t * )predicate ;
1439
1473
struct _CF_dispatch_once_waiter_s dow = { NULL };
1440
1474
_CF_dispatch_once_waiter_t tail = & dow , next , tmp ;
@@ -1465,6 +1499,7 @@ void _CF_dispatch_once(dispatch_once_t *predicate, void (^block)(void)) {
1465
1499
}
1466
1500
_CF_put_thread_semaphore (dow .dow_sema );
1467
1501
}
1502
+ #endif // TARGET_OS_WASI
1468
1503
}
1469
1504
1470
1505
#endif
@@ -1689,7 +1724,7 @@ CF_EXPORT char **_CFEnviron(void) {
1689
1724
#elif TARGET_OS_WIN32
1690
1725
return _environ ;
1691
1726
#else
1692
- #if TARGET_OS_BSD
1727
+ #if TARGET_OS_BSD || TARGET_OS_WASI
1693
1728
extern char * * environ ;
1694
1729
#endif
1695
1730
return environ ;
@@ -2056,7 +2091,7 @@ CF_EXPORT int _CFPosixSpawn(pid_t *_CF_RESTRICT pid, const char *_CF_RESTRICT pa
2056
2091
return _CFPosixSpawnImpl (pid , path , file_actions , attrp , argv , envp );
2057
2092
}
2058
2093
2059
- #elif !TARGET_OS_WIN32
2094
+ #elif !TARGET_OS_WIN32 && ! TARGET_OS_WASI
2060
2095
2061
2096
#include <spawn.h>
2062
2097
0 commit comments