Skip to content

Windows Port Continuation #1805

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Dec 15, 2018
Merged
1 change: 0 additions & 1 deletion CoreFoundation/Base.subproj/CFFileUtilities.c
Original file line number Diff line number Diff line change
Expand Up @@ -1024,7 +1024,6 @@ CF_PRIVATE void _CFIterateDirectory(CFStringRef directoryPath, Boolean appendSla
if (!CFStringGetFileSystemRepresentation(directoryPath, directoryPathBuf, CFMaxPathSize)) return;

#if DEPLOYMENT_TARGET_WINDOWS
#error this path does not support calculateFullResultPath but it must do so someday
CFIndex cpathLen = strlen(directoryPathBuf);
// Make sure there is room for the additional space we need in the win32 api
if (cpathLen + 2 < CFMaxPathSize) {
Expand Down
26 changes: 16 additions & 10 deletions CoreFoundation/Base.subproj/CFPlatform.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <shlobj.h>
#include <WinIoCtl.h>
#include <direct.h>
#include <process.h>
#define SECURITY_WIN32
#include <Security.h>

Expand Down Expand Up @@ -558,15 +559,14 @@ CF_EXPORT void _NS_pthread_setname_np(const char *name) {
}
}

static pthread_t __initialPthread = { NULL, 0 };
static _CFThreadRef __initialPthread = INVALID_HANDLE_VALUE;

CF_EXPORT int _NS_pthread_main_np() {
pthread_t me = pthread_self();
if (NULL == __initialPthread.p) {
__initialPthread.p = me.p;
__initialPthread.x = me.x;
}
return (pthread_equal(__initialPthread, me));
if (__initialPthread == INVALID_HANDLE_VALUE)
DuplicateHandle(GetCurrentProcess(), GetCurrentThread(),
GetCurrentProcess(), &__initialPthread, 0, FALSE,
DUPLICATE_SAME_ACCESS);
return CompareObjectHandles(__initialPthread, GetCurrentThread());
}

#endif
Expand Down Expand Up @@ -1242,7 +1242,7 @@ CF_INLINE void _CF_put_thread_semaphore(_CF_sema_t s) {
typedef struct _CF_dispatch_once_waiter_s {
volatile struct _CF_dispatch_once_waiter_s *volatile dow_next;
_CF_sema_t dow_sema;
pthread_t dow_thread;
_CFThreadRef dow_thread;
} *_CF_dispatch_once_waiter_t;

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

_CFThreadRef _CFThreadCreate(const _CFThreadAttributes attrs, void *_Nullable (* _Nonnull startfn)(void *_Nullable), void *_CF_RESTRICT _Nullable context) {
pthread_t thread;
#if DEPLOYMENT_TARGET_WINDOWS
return (_CFThreadRef)_beginthreadex(NULL, 0,
(_beginthreadex_proc_type)startfn,
context, 0, NULL);
#else
_CFThreadRef thread;
pthread_create(&thread, &attrs, startfn, context);
return thread;
#endif
}

CF_CROSS_PLATFORM_EXPORT int _CFThreadSetName(pthread_t thread, const char *_Nonnull name) {
CF_CROSS_PLATFORM_EXPORT int _CFThreadSetName(_CFThreadRef thread, const char *_Nonnull name) {
#if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_EMBEDDED_MINI
if (pthread_equal(pthread_self(), thread)) {
return pthread_setname_np(name);
Expand Down
19 changes: 12 additions & 7 deletions CoreFoundation/Base.subproj/CFRuntime.c
Original file line number Diff line number Diff line change
Expand Up @@ -1071,9 +1071,9 @@ CF_PRIVATE Boolean __CFProcessIsRestricted() {
}

#if DEPLOYMENT_TARGET_WINDOWS
#define kNilPthreadT { nil, nil }
#define kNilPthreadT INVALID_HANDLE_VALUE
#else
#define kNilPthreadT (pthread_t)0
#define kNilPthreadT (_CFThreadRef)0
#endif


Expand All @@ -1090,12 +1090,12 @@ static Boolean __CFInitializing = 0;
Boolean __CFInitialized = 0;

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

CF_EXPORT pthread_t _CF_pthread_main_thread_np(void);
pthread_t _CF_pthread_main_thread_np(void) {
CF_EXPORT _CFThreadRef _CF_pthread_main_thread_np(void);
_CFThreadRef _CF_pthread_main_thread_np(void) {
return _CFMainPThread;
}
#define pthread_main_thread_np() _CF_pthread_main_thread_np()
Expand All @@ -1117,9 +1117,14 @@ void __CFInitialize(void) {

#if DEPLOYMENT_TARGET_WINDOWS
if (!pthread_main_np()) HALT; // CoreFoundation must be initialized on the main thread
#endif

DuplicateHandle(GetCurrentProcess(), GetCurrentThread(),
GetCurrentProcess(), &_CFMainPThread, 0, FALSE,
DUPLICATE_SAME_ACCESS);
#else
// move this next line up into the #if above after Foundation gets off this symbol. Also: <rdar://problem/39622745> Stop using _CFMainPThread
_CFMainPThread = pthread_self();
#endif

#if DEPLOYMENT_TARGET_WINDOWS
// Must not call any CF functions
Expand Down
18 changes: 12 additions & 6 deletions CoreFoundation/Base.subproj/ForSwiftFoundationOnly.h
Original file line number Diff line number Diff line change
Expand Up @@ -345,22 +345,28 @@ CF_EXPORT char *_Nullable *_Nonnull _CFEnviron(void);

CF_EXPORT void CFLog1(CFLogLevel lev, CFStringRef message);

#if DEPLOYMENT_TARGET_WINDOWS
typedef HANDLE _CFThreadRef;
typedef DWORD _CFThreadAttributes;
typedef DWORD _CFThreadSpecificKey;
#elif _POSIX_THREADS
typedef pthread_t _CFThreadRef;
typedef pthread_attr_t _CFThreadAttributes;
typedef pthread_key_t _CFThreadSpecificKey;
#endif

CF_CROSS_PLATFORM_EXPORT Boolean _CFIsMainThread(void);
CF_EXPORT pthread_t _CFMainPThread;
CF_EXPORT _CFThreadRef _CFMainPThread;

CF_EXPORT CFHashCode __CFHashDouble(double d);

typedef pthread_key_t _CFThreadSpecificKey;
CF_EXPORT CFTypeRef _Nullable _CFThreadSpecificGet(_CFThreadSpecificKey key);
CF_EXPORT void _CFThreadSpecificSet(_CFThreadSpecificKey key, CFTypeRef _Nullable value);
CF_EXPORT _CFThreadSpecificKey _CFThreadSpecificKeyCreate(void);

typedef pthread_attr_t _CFThreadAttributes;
typedef pthread_t _CFThreadRef;

CF_EXPORT _CFThreadRef _CFThreadCreate(const _CFThreadAttributes attrs, void *_Nullable (* _Nonnull startfn)(void *_Nullable), void *_CF_RESTRICT _Nullable context);

CF_CROSS_PLATFORM_EXPORT int _CFThreadSetName(pthread_t thread, const char *_Nonnull name);
CF_CROSS_PLATFORM_EXPORT int _CFThreadSetName(_CFThreadRef thread, const char *_Nonnull name);
CF_CROSS_PLATFORM_EXPORT int _CFThreadGetName(char *_Nonnull buf, int length);

CF_EXPORT Boolean _CFCharacterSetIsLongCharacterMember(CFCharacterSetRef theSet, UTF32Char theChar);
Expand Down
2 changes: 1 addition & 1 deletion CoreFoundation/PlugIn.subproj/CFBundle_Executable.c
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ static CFURLRef _CFBundleCopyExecutableURLRaw(CFURLRef urlPath, CFStringRef exeN
#elif DEPLOYMENT_TARGET_WINDOWS
if (!executableURL) {
executableURL = CFURLCreateWithFileSystemPathRelativeToBase(kCFAllocatorSystemDefault, exeName, kCFURLWindowsPathStyle, false, urlPath);
if (executableURL && !_urlExists(executableURL)) {
if (executableURL && !_binaryLoadable(executableURL)) {
CFRelease(executableURL);
executableURL = NULL;
}
Expand Down
24 changes: 12 additions & 12 deletions CoreFoundation/RunLoop.subproj/CFRunLoop.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ typedef int dispatch_runloop_handle_t;

extern void _dispatch_main_queue_callback_4CF(void *);

extern pthread_t pthread_main_thread_np(void);
extern _CFThreadRef pthread_main_thread_np(void);
typedef struct voucher_s *voucher_t;

extern voucher_t _Nullable voucher_copy(void);
Expand Down Expand Up @@ -125,7 +125,7 @@ extern void _dispatch_main_queue_callback_4CF(void *_Null_unspecified msg);
#endif

#if DEPLOYMENT_TARGET_WINDOWS || DEPLOYMENT_TARGET_LINUX
CF_EXPORT pthread_t _CF_pthread_main_thread_np(void);
CF_EXPORT _CFThreadRef _CF_pthread_main_thread_np(void);
#define pthread_main_thread_np() _CF_pthread_main_thread_np()
#endif

Expand Down Expand Up @@ -154,21 +154,21 @@ static void _runLoopTimerWithBlockContext(CFRunLoopTimerRef timer, void *opaqueB

#if DEPLOYMENT_TARGET_WINDOWS

static pthread_t const kNilPthreadT = { nil, nil };
static _CFThreadRef const kNilPthreadT = { nil, nil };
#define pthreadPointer(a) a.p
typedef int kern_return_t;
#define KERN_SUCCESS 0

#elif DEPLOYMENT_TARGET_LINUX

static pthread_t const kNilPthreadT = (pthread_t)0;
static _CFThreadRef const kNilPthreadT = (_CFThreadRef)0;
#define pthreadPointer(a) ((void*)a)
typedef int kern_return_t;
#define KERN_SUCCESS 0

#else

static pthread_t const kNilPthreadT = (pthread_t)0;
static _CFThreadRef const kNilPthreadT = (_CFThreadRef)0;
#define pthreadPointer(a) a
#define lockCount(a) a
#endif
Expand Down Expand Up @@ -785,7 +785,7 @@ struct __CFRunLoop {
__CFPort _wakeUpPort; // used for CFRunLoopWakeUp
Boolean _unused;
volatile _per_run_data *_perRunData; // reset for runs of the run loop
pthread_t _pthread;
_CFThreadRef _pthread;
uint32_t _winthread;
CFMutableSetRef _commonModes;
CFMutableSetRef _commonModeItems;
Expand Down Expand Up @@ -1373,7 +1373,7 @@ static void __CFRunLoopDeallocateTimers(const void *value, void *context) {
}
}

CF_EXPORT CFRunLoopRef _CFRunLoopGet0b(pthread_t t);
CF_EXPORT CFRunLoopRef _CFRunLoopGet0b(_CFThreadRef t);

static void __CFRunLoopDeallocate(CFTypeRef cf) {
CFRunLoopRef rl = (CFRunLoopRef)cf;
Expand Down Expand Up @@ -1459,7 +1459,7 @@ CFTypeID CFRunLoopGetTypeID(void) {
return _kCFRuntimeIDCFRunLoop;
}

static CFRunLoopRef __CFRunLoopCreate(pthread_t t) {
static CFRunLoopRef __CFRunLoopCreate(_CFThreadRef t) {
CFRunLoopRef loop = NULL;
CFRunLoopModeRef rlm;
uint32_t size = sizeof(struct __CFRunLoop) - sizeof(CFRuntimeBase);
Expand Down Expand Up @@ -1496,7 +1496,7 @@ static CFRunLoopRef __CFRunLoopCreate(pthread_t t) {
static CFMutableDictionaryRef __CFRunLoops = NULL;
static CFLock_t loopsLock = CFLockInit;

CF_PRIVATE CFRunLoopRef _CFRunLoopCacheLookup(pthread_t t, const Boolean createCache) {
CF_PRIVATE CFRunLoopRef _CFRunLoopCacheLookup(_CFThreadRef t, const Boolean createCache) {
CFRunLoopRef loop = NULL;
if (pthread_equal(t, kNilPthreadT)) {
t = pthread_main_thread_np();
Expand Down Expand Up @@ -1540,7 +1540,7 @@ CF_EXPORT Boolean _CFRunLoopIsCurrent(const CFRunLoopRef rl) {

// should only be called by Foundation
// t==0 is a synonym for "main thread" that always works
CF_EXPORT CFRunLoopRef _CFRunLoopGet0(pthread_t t) {
CF_EXPORT CFRunLoopRef _CFRunLoopGet0(_CFThreadRef t) {
if (pthread_equal(t, kNilPthreadT)) {
t = pthread_main_thread_np();
}
Expand Down Expand Up @@ -1575,7 +1575,7 @@ CF_EXPORT CFRunLoopRef _CFRunLoopGet0(pthread_t t) {
}

// should only be called by Foundation
CFRunLoopRef _CFRunLoopGet0b(pthread_t t) {
CFRunLoopRef _CFRunLoopGet0b(_CFThreadRef t) {
if (pthread_equal(t, kNilPthreadT)) {
t = pthread_main_thread_np();
}
Expand Down Expand Up @@ -1629,7 +1629,7 @@ CF_PRIVATE void __CFFinalizeRunLoop(uintptr_t data) {
}
}

pthread_t _CFRunLoopGet1(CFRunLoopRef rl) {
_CFThreadRef _CFRunLoopGet1(CFRunLoopRef rl) {
return rl->_pthread;
}

Expand Down
12 changes: 6 additions & 6 deletions CoreFoundation/RunLoop.subproj/CFSocket.c
Original file line number Diff line number Diff line change
Expand Up @@ -1026,17 +1026,17 @@ static void timeradd(struct timeval *a, struct timeval *b, struct timeval *res)

#include <sys/syslog.h>

static pthread_t __cfSocketTid()
static _CFThreadRef __cfSocketTid()
{
#if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED
uint64_t tid = 0;
if (0 != pthread_threadid_np(NULL, &tid))
tid = pthread_mach_thread_np(pthread_self());
return (pthread_t) tid;
return (_CFThreadRef) tid;
#elif DEPLOYMENT_TARGET_WINDOWS
return (pthread_t) GetCurrentThreadId();
return (_CFThreadRef) GetCurrentThreadId();
#else
return (pthread_t) pthread_self();
return (_CFThreadRef) pthread_self();
#endif
}

Expand Down Expand Up @@ -2615,7 +2615,7 @@ static CFSocketRef _CFSocketCreateWithNative(CFAllocatorRef allocator, CFSocketN
if (INVALID_SOCKET != sock) CFDictionaryAddValue(__CFAllSockets, (void *)(uintptr_t)sock, memory);
if (NULL == __CFSocketManagerThread) {
#if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_EMBEDDED_MINI || DEPLOYMENT_TARGET_LINUX || DEPLOYMENT_TARGET_FREEBSD
pthread_t tid = 0;
_CFThreadRef tid = 0;
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
Expand All @@ -2625,7 +2625,7 @@ static CFSocketRef _CFSocketCreateWithNative(CFAllocatorRef allocator, CFSocketN
#endif
pthread_create(&tid, &attr, __CFSocketManager, 0);
pthread_attr_destroy(&attr);
//warning CF: we dont actually know that a pthread_t is the same size as void *
_Static_assert(sizeof(_CFThreadRef) == sizeof(void *), "_CFThreadRef is not pointer sized");
__CFSocketManagerThread = (void *)tid;
#elif DEPLOYMENT_TARGET_WINDOWS
unsigned tid;
Expand Down
2 changes: 1 addition & 1 deletion CoreFoundation/Stream.subproj/CFStream.c
Original file line number Diff line number Diff line change
Expand Up @@ -1745,7 +1745,7 @@ static CFRunLoopRef _legacyStreamRunLoop()
#if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED
pthread_attr_set_qos_class_np(&attr, qos_class_main(), 0);
#endif
pthread_t workThread;
_CFThreadRef workThread;
(void) pthread_create(&workThread, &attr, _legacyStreamRunLoop_workThread, &sem);
pthread_attr_destroy(&attr);
#endif
Expand Down