Skip to content

Commit a9c0598

Browse files
authored
Merge pull request #1805 from compnerd/windows-3
Windows Port Continuation
2 parents f3bced8 + 4c6b877 commit a9c0598

File tree

8 files changed

+60
-44
lines changed

8 files changed

+60
-44
lines changed

CoreFoundation/Base.subproj/CFFileUtilities.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1024,7 +1024,6 @@ CF_PRIVATE void _CFIterateDirectory(CFStringRef directoryPath, Boolean appendSla
10241024
if (!CFStringGetFileSystemRepresentation(directoryPath, directoryPathBuf, CFMaxPathSize)) return;
10251025

10261026
#if DEPLOYMENT_TARGET_WINDOWS
1027-
#error this path does not support calculateFullResultPath but it must do so someday
10281027
CFIndex cpathLen = strlen(directoryPathBuf);
10291028
// Make sure there is room for the additional space we need in the win32 api
10301029
if (cpathLen + 2 < CFMaxPathSize) {

CoreFoundation/Base.subproj/CFPlatform.c

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include <shlobj.h>
2828
#include <WinIoCtl.h>
2929
#include <direct.h>
30+
#include <process.h>
3031
#define SECURITY_WIN32
3132
#include <Security.h>
3233

@@ -558,15 +559,14 @@ CF_EXPORT void _NS_pthread_setname_np(const char *name) {
558559
}
559560
}
560561

561-
static pthread_t __initialPthread = { NULL, 0 };
562+
static _CFThreadRef __initialPthread = INVALID_HANDLE_VALUE;
562563

563564
CF_EXPORT int _NS_pthread_main_np() {
564-
pthread_t me = pthread_self();
565-
if (NULL == __initialPthread.p) {
566-
__initialPthread.p = me.p;
567-
__initialPthread.x = me.x;
568-
}
569-
return (pthread_equal(__initialPthread, me));
565+
if (__initialPthread == INVALID_HANDLE_VALUE)
566+
DuplicateHandle(GetCurrentProcess(), GetCurrentThread(),
567+
GetCurrentProcess(), &__initialPthread, 0, FALSE,
568+
DUPLICATE_SAME_ACCESS);
569+
return CompareObjectHandles(__initialPthread, GetCurrentThread());
570570
}
571571

572572
#endif
@@ -1242,7 +1242,7 @@ CF_INLINE void _CF_put_thread_semaphore(_CF_sema_t s) {
12421242
typedef struct _CF_dispatch_once_waiter_s {
12431243
volatile struct _CF_dispatch_once_waiter_s *volatile dow_next;
12441244
_CF_sema_t dow_sema;
1245-
pthread_t dow_thread;
1245+
_CFThreadRef dow_thread;
12461246
} *_CF_dispatch_once_waiter_t;
12471247

12481248
#if defined(__x86_64__) || defined(__i386__)
@@ -1369,12 +1369,18 @@ void _CFThreadSpecificSet(_CFThreadSpecificKey key, CFTypeRef _Nullable value) {
13691369
}
13701370

13711371
_CFThreadRef _CFThreadCreate(const _CFThreadAttributes attrs, void *_Nullable (* _Nonnull startfn)(void *_Nullable), void *_CF_RESTRICT _Nullable context) {
1372-
pthread_t thread;
1372+
#if DEPLOYMENT_TARGET_WINDOWS
1373+
return (_CFThreadRef)_beginthreadex(NULL, 0,
1374+
(_beginthreadex_proc_type)startfn,
1375+
context, 0, NULL);
1376+
#else
1377+
_CFThreadRef thread;
13731378
pthread_create(&thread, &attrs, startfn, context);
13741379
return thread;
1380+
#endif
13751381
}
13761382

1377-
CF_CROSS_PLATFORM_EXPORT int _CFThreadSetName(pthread_t thread, const char *_Nonnull name) {
1383+
CF_CROSS_PLATFORM_EXPORT int _CFThreadSetName(_CFThreadRef thread, const char *_Nonnull name) {
13781384
#if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_EMBEDDED_MINI
13791385
if (pthread_equal(pthread_self(), thread)) {
13801386
return pthread_setname_np(name);

CoreFoundation/Base.subproj/CFRuntime.c

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1071,9 +1071,9 @@ CF_PRIVATE Boolean __CFProcessIsRestricted() {
10711071
}
10721072

10731073
#if DEPLOYMENT_TARGET_WINDOWS
1074-
#define kNilPthreadT { nil, nil }
1074+
#define kNilPthreadT INVALID_HANDLE_VALUE
10751075
#else
1076-
#define kNilPthreadT (pthread_t)0
1076+
#define kNilPthreadT (_CFThreadRef)0
10771077
#endif
10781078

10791079

@@ -1090,12 +1090,12 @@ static Boolean __CFInitializing = 0;
10901090
Boolean __CFInitialized = 0;
10911091

10921092
// move the next 2 lines down into the #if below, and make it static, after Foundation gets off this symbol on other platforms.
1093-
CF_EXPORT pthread_t _CFMainPThread;
1094-
pthread_t _CFMainPThread = kNilPthreadT;
1093+
CF_EXPORT _CFThreadRef _CFMainPThread;
1094+
_CFThreadRef _CFMainPThread = kNilPthreadT;
10951095
#if DEPLOYMENT_TARGET_WINDOWS || DEPLOYMENT_TARGET_LINUX
10961096

1097-
CF_EXPORT pthread_t _CF_pthread_main_thread_np(void);
1098-
pthread_t _CF_pthread_main_thread_np(void) {
1097+
CF_EXPORT _CFThreadRef _CF_pthread_main_thread_np(void);
1098+
_CFThreadRef _CF_pthread_main_thread_np(void) {
10991099
return _CFMainPThread;
11001100
}
11011101
#define pthread_main_thread_np() _CF_pthread_main_thread_np()
@@ -1117,9 +1117,14 @@ void __CFInitialize(void) {
11171117

11181118
#if DEPLOYMENT_TARGET_WINDOWS
11191119
if (!pthread_main_np()) HALT; // CoreFoundation must be initialized on the main thread
1120-
#endif
1120+
1121+
DuplicateHandle(GetCurrentProcess(), GetCurrentThread(),
1122+
GetCurrentProcess(), &_CFMainPThread, 0, FALSE,
1123+
DUPLICATE_SAME_ACCESS);
1124+
#else
11211125
// move this next line up into the #if above after Foundation gets off this symbol. Also: <rdar://problem/39622745> Stop using _CFMainPThread
11221126
_CFMainPThread = pthread_self();
1127+
#endif
11231128

11241129
#if DEPLOYMENT_TARGET_WINDOWS
11251130
// Must not call any CF functions

CoreFoundation/Base.subproj/ForSwiftFoundationOnly.h

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -345,22 +345,28 @@ CF_EXPORT char *_Nullable *_Nonnull _CFEnviron(void);
345345

346346
CF_EXPORT void CFLog1(CFLogLevel lev, CFStringRef message);
347347

348+
#if DEPLOYMENT_TARGET_WINDOWS
349+
typedef HANDLE _CFThreadRef;
350+
typedef DWORD _CFThreadAttributes;
351+
typedef DWORD _CFThreadSpecificKey;
352+
#elif _POSIX_THREADS
353+
typedef pthread_t _CFThreadRef;
354+
typedef pthread_attr_t _CFThreadAttributes;
355+
typedef pthread_key_t _CFThreadSpecificKey;
356+
#endif
357+
348358
CF_CROSS_PLATFORM_EXPORT Boolean _CFIsMainThread(void);
349-
CF_EXPORT pthread_t _CFMainPThread;
359+
CF_EXPORT _CFThreadRef _CFMainPThread;
350360

351361
CF_EXPORT CFHashCode __CFHashDouble(double d);
352362

353-
typedef pthread_key_t _CFThreadSpecificKey;
354363
CF_EXPORT CFTypeRef _Nullable _CFThreadSpecificGet(_CFThreadSpecificKey key);
355364
CF_EXPORT void _CFThreadSpecificSet(_CFThreadSpecificKey key, CFTypeRef _Nullable value);
356365
CF_EXPORT _CFThreadSpecificKey _CFThreadSpecificKeyCreate(void);
357366

358-
typedef pthread_attr_t _CFThreadAttributes;
359-
typedef pthread_t _CFThreadRef;
360-
361367
CF_EXPORT _CFThreadRef _CFThreadCreate(const _CFThreadAttributes attrs, void *_Nullable (* _Nonnull startfn)(void *_Nullable), void *_CF_RESTRICT _Nullable context);
362368

363-
CF_CROSS_PLATFORM_EXPORT int _CFThreadSetName(pthread_t thread, const char *_Nonnull name);
369+
CF_CROSS_PLATFORM_EXPORT int _CFThreadSetName(_CFThreadRef thread, const char *_Nonnull name);
364370
CF_CROSS_PLATFORM_EXPORT int _CFThreadGetName(char *_Nonnull buf, int length);
365371

366372
CF_EXPORT Boolean _CFCharacterSetIsLongCharacterMember(CFCharacterSetRef theSet, UTF32Char theChar);

CoreFoundation/PlugIn.subproj/CFBundle_Executable.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ static CFURLRef _CFBundleCopyExecutableURLRaw(CFURLRef urlPath, CFStringRef exeN
119119
#elif DEPLOYMENT_TARGET_WINDOWS
120120
if (!executableURL) {
121121
executableURL = CFURLCreateWithFileSystemPathRelativeToBase(kCFAllocatorSystemDefault, exeName, kCFURLWindowsPathStyle, false, urlPath);
122-
if (executableURL && !_urlExists(executableURL)) {
122+
if (executableURL && !_binaryLoadable(executableURL)) {
123123
CFRelease(executableURL);
124124
executableURL = NULL;
125125
}

CoreFoundation/RunLoop.subproj/CFRunLoop.c

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ typedef int dispatch_runloop_handle_t;
9090

9191
extern void _dispatch_main_queue_callback_4CF(void *);
9292

93-
extern pthread_t pthread_main_thread_np(void);
93+
extern _CFThreadRef pthread_main_thread_np(void);
9494
typedef struct voucher_s *voucher_t;
9595

9696
extern voucher_t _Nullable voucher_copy(void);
@@ -125,7 +125,7 @@ extern void _dispatch_main_queue_callback_4CF(void *_Null_unspecified msg);
125125
#endif
126126

127127
#if DEPLOYMENT_TARGET_WINDOWS || DEPLOYMENT_TARGET_LINUX
128-
CF_EXPORT pthread_t _CF_pthread_main_thread_np(void);
128+
CF_EXPORT _CFThreadRef _CF_pthread_main_thread_np(void);
129129
#define pthread_main_thread_np() _CF_pthread_main_thread_np()
130130
#endif
131131

@@ -154,21 +154,21 @@ static void _runLoopTimerWithBlockContext(CFRunLoopTimerRef timer, void *opaqueB
154154

155155
#if DEPLOYMENT_TARGET_WINDOWS
156156

157-
static pthread_t const kNilPthreadT = { nil, nil };
157+
static _CFThreadRef const kNilPthreadT = { nil, nil };
158158
#define pthreadPointer(a) a.p
159159
typedef int kern_return_t;
160160
#define KERN_SUCCESS 0
161161

162162
#elif DEPLOYMENT_TARGET_LINUX
163163

164-
static pthread_t const kNilPthreadT = (pthread_t)0;
164+
static _CFThreadRef const kNilPthreadT = (_CFThreadRef)0;
165165
#define pthreadPointer(a) ((void*)a)
166166
typedef int kern_return_t;
167167
#define KERN_SUCCESS 0
168168

169169
#else
170170

171-
static pthread_t const kNilPthreadT = (pthread_t)0;
171+
static _CFThreadRef const kNilPthreadT = (_CFThreadRef)0;
172172
#define pthreadPointer(a) a
173173
#define lockCount(a) a
174174
#endif
@@ -785,7 +785,7 @@ struct __CFRunLoop {
785785
__CFPort _wakeUpPort; // used for CFRunLoopWakeUp
786786
Boolean _unused;
787787
volatile _per_run_data *_perRunData; // reset for runs of the run loop
788-
pthread_t _pthread;
788+
_CFThreadRef _pthread;
789789
uint32_t _winthread;
790790
CFMutableSetRef _commonModes;
791791
CFMutableSetRef _commonModeItems;
@@ -1373,7 +1373,7 @@ static void __CFRunLoopDeallocateTimers(const void *value, void *context) {
13731373
}
13741374
}
13751375

1376-
CF_EXPORT CFRunLoopRef _CFRunLoopGet0b(pthread_t t);
1376+
CF_EXPORT CFRunLoopRef _CFRunLoopGet0b(_CFThreadRef t);
13771377

13781378
static void __CFRunLoopDeallocate(CFTypeRef cf) {
13791379
CFRunLoopRef rl = (CFRunLoopRef)cf;
@@ -1459,7 +1459,7 @@ CFTypeID CFRunLoopGetTypeID(void) {
14591459
return _kCFRuntimeIDCFRunLoop;
14601460
}
14611461

1462-
static CFRunLoopRef __CFRunLoopCreate(pthread_t t) {
1462+
static CFRunLoopRef __CFRunLoopCreate(_CFThreadRef t) {
14631463
CFRunLoopRef loop = NULL;
14641464
CFRunLoopModeRef rlm;
14651465
uint32_t size = sizeof(struct __CFRunLoop) - sizeof(CFRuntimeBase);
@@ -1496,7 +1496,7 @@ static CFRunLoopRef __CFRunLoopCreate(pthread_t t) {
14961496
static CFMutableDictionaryRef __CFRunLoops = NULL;
14971497
static CFLock_t loopsLock = CFLockInit;
14981498

1499-
CF_PRIVATE CFRunLoopRef _CFRunLoopCacheLookup(pthread_t t, const Boolean createCache) {
1499+
CF_PRIVATE CFRunLoopRef _CFRunLoopCacheLookup(_CFThreadRef t, const Boolean createCache) {
15001500
CFRunLoopRef loop = NULL;
15011501
if (pthread_equal(t, kNilPthreadT)) {
15021502
t = pthread_main_thread_np();
@@ -1540,7 +1540,7 @@ CF_EXPORT Boolean _CFRunLoopIsCurrent(const CFRunLoopRef rl) {
15401540

15411541
// should only be called by Foundation
15421542
// t==0 is a synonym for "main thread" that always works
1543-
CF_EXPORT CFRunLoopRef _CFRunLoopGet0(pthread_t t) {
1543+
CF_EXPORT CFRunLoopRef _CFRunLoopGet0(_CFThreadRef t) {
15441544
if (pthread_equal(t, kNilPthreadT)) {
15451545
t = pthread_main_thread_np();
15461546
}
@@ -1575,7 +1575,7 @@ CF_EXPORT CFRunLoopRef _CFRunLoopGet0(pthread_t t) {
15751575
}
15761576

15771577
// should only be called by Foundation
1578-
CFRunLoopRef _CFRunLoopGet0b(pthread_t t) {
1578+
CFRunLoopRef _CFRunLoopGet0b(_CFThreadRef t) {
15791579
if (pthread_equal(t, kNilPthreadT)) {
15801580
t = pthread_main_thread_np();
15811581
}
@@ -1629,7 +1629,7 @@ CF_PRIVATE void __CFFinalizeRunLoop(uintptr_t data) {
16291629
}
16301630
}
16311631

1632-
pthread_t _CFRunLoopGet1(CFRunLoopRef rl) {
1632+
_CFThreadRef _CFRunLoopGet1(CFRunLoopRef rl) {
16331633
return rl->_pthread;
16341634
}
16351635

CoreFoundation/RunLoop.subproj/CFSocket.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1026,17 +1026,17 @@ static void timeradd(struct timeval *a, struct timeval *b, struct timeval *res)
10261026

10271027
#include <sys/syslog.h>
10281028

1029-
static pthread_t __cfSocketTid()
1029+
static _CFThreadRef __cfSocketTid()
10301030
{
10311031
#if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED
10321032
uint64_t tid = 0;
10331033
if (0 != pthread_threadid_np(NULL, &tid))
10341034
tid = pthread_mach_thread_np(pthread_self());
1035-
return (pthread_t) tid;
1035+
return (_CFThreadRef) tid;
10361036
#elif DEPLOYMENT_TARGET_WINDOWS
1037-
return (pthread_t) GetCurrentThreadId();
1037+
return (_CFThreadRef) GetCurrentThreadId();
10381038
#else
1039-
return (pthread_t) pthread_self();
1039+
return (_CFThreadRef) pthread_self();
10401040
#endif
10411041
}
10421042

@@ -2615,7 +2615,7 @@ static CFSocketRef _CFSocketCreateWithNative(CFAllocatorRef allocator, CFSocketN
26152615
if (INVALID_SOCKET != sock) CFDictionaryAddValue(__CFAllSockets, (void *)(uintptr_t)sock, memory);
26162616
if (NULL == __CFSocketManagerThread) {
26172617
#if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_EMBEDDED_MINI || DEPLOYMENT_TARGET_LINUX || DEPLOYMENT_TARGET_FREEBSD
2618-
pthread_t tid = 0;
2618+
_CFThreadRef tid = 0;
26192619
pthread_attr_t attr;
26202620
pthread_attr_init(&attr);
26212621
pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
@@ -2625,7 +2625,7 @@ static CFSocketRef _CFSocketCreateWithNative(CFAllocatorRef allocator, CFSocketN
26252625
#endif
26262626
pthread_create(&tid, &attr, __CFSocketManager, 0);
26272627
pthread_attr_destroy(&attr);
2628-
//warning CF: we dont actually know that a pthread_t is the same size as void *
2628+
_Static_assert(sizeof(_CFThreadRef) == sizeof(void *), "_CFThreadRef is not pointer sized");
26292629
__CFSocketManagerThread = (void *)tid;
26302630
#elif DEPLOYMENT_TARGET_WINDOWS
26312631
unsigned tid;

CoreFoundation/Stream.subproj/CFStream.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1745,7 +1745,7 @@ static CFRunLoopRef _legacyStreamRunLoop()
17451745
#if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED
17461746
pthread_attr_set_qos_class_np(&attr, qos_class_main(), 0);
17471747
#endif
1748-
pthread_t workThread;
1748+
_CFThreadRef workThread;
17491749
(void) pthread_create(&workThread, &attr, _legacyStreamRunLoop_workThread, &sem);
17501750
pthread_attr_destroy(&attr);
17511751
#endif

0 commit comments

Comments
 (0)