Skip to content

Windows Port Continuation #1810

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 15 commits into from
Dec 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ ExternalProject_Add(CoreFoundation
-DCF_PATH_TO_LIBDISPATCH_SOURCE=${FOUNDATION_PATH_TO_LIBDISPATCH_SOURCE}
-DCF_PATH_TO_LIBDISPATCH_BUILD=${FOUNDATION_PATH_TO_LIBDISPATCH_BUILD}
-DICU_INCLUDE_DIR=${ICU_INCLUDE_DIR}
-DLIBXML2_LIBRARY=${LIBXML2_LIBRARY}
-DLIBXML2_INCLUDE_DIR=${LIBXML2_INCLUDE_DIR}
-DCURL_LIBRARY=${CURL_LIBRARY}
-DCURL_INCLUDE_DIR=${CURL_INCLUDE_DIR}
INSTALL_COMMAND
${CMAKE_COMMAND} -E env --unset=DESTDIR ${CMAKE_COMMAND} --build . --target install)
ExternalProject_Get_Property(CoreFoundation install_dir)
Expand Down
2 changes: 1 addition & 1 deletion CoreFoundation/Base.subproj/CFFileUtilities.c
Original file line number Diff line number Diff line change
Expand Up @@ -1059,7 +1059,7 @@ CF_PRIVATE void _CFIterateDirectory(CFStringRef directoryPath, Boolean appendSla
}

Boolean isDirectory = file.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
Boolean result = fileHandler(fileName, isDirectory ? DT_DIR : DT_REG);
Boolean result = fileHandler(fileName, NULL, isDirectory ? DT_DIR : DT_REG);
CFRelease(fileName);
if (!result) break;
} while (FindNextFileW(handle, &file));
Expand Down
10 changes: 10 additions & 0 deletions CoreFoundation/Base.subproj/CFInternal.h
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,11 @@ typedef pthread_mutex_t os_unfair_lock;
typedef pthread_mutex_t * os_unfair_lock_t;
static void os_unfair_lock_lock(os_unfair_lock_t lock) { pthread_mutex_lock(lock); }
static void os_unfair_lock_unlock(os_unfair_lock_t lock) { pthread_mutex_unlock(lock); }
#elif defined(_WIN32)
#define OS_UNFAIR_LOCK_INIT CFLockInit
#define os_unfair_lock CFLock_t
#define os_unfair_lock_lock __CFLock
#define os_unfair_lock_unlock __CFUnlock
#endif // __has_include(<os/lock.h>)

#if !__HAS_DISPATCH__
Expand Down Expand Up @@ -873,13 +878,18 @@ CF_EXPORT int _NS_pthread_setspecific(_CFThreadSpecificKey key, const void *val)
CF_EXPORT void* _NS_pthread_getspecific(_CFThreadSpecificKey key);
CF_EXPORT int _NS_pthread_key_init_np(int key, void (*destructor)(void *));
CF_EXPORT void _NS_pthread_setname_np(const char *name);
CF_EXPORT bool _NS_pthread_equal(_CFThreadRef t1, _CFThreadRef t2);

// map use of pthread_set/getspecific to internal API
#define pthread_setspecific _NS_pthread_setspecific
#define pthread_getspecific _NS_pthread_getspecific
#define pthread_key_init_np _NS_pthread_key_init_np
#define pthread_main_np _NS_pthread_main_np
#define pthread_setname_np _NS_pthread_setname_np
#define pthread_equal _NS_pthread_equal

#define pthread_self() GetCurrentThread()

#endif

#if DEPLOYMENT_TARGET_LINUX
Expand Down
78 changes: 49 additions & 29 deletions CoreFoundation/Base.subproj/CFPlatform.c
Original file line number Diff line number Diff line change
Expand Up @@ -529,34 +529,8 @@ CF_EXPORT CFMutableStringRef _CFCreateApplicationRepositoryPath(CFAllocatorRef a

#if DEPLOYMENT_TARGET_WINDOWS

// This code from here:
// http://msdn.microsoft.com/en-us/library/xcb2z8hs.aspx

const DWORD MS_VC_EXCEPTION=0x406D1388;
#pragma pack(push,8)
typedef struct tagTHREADNAME_INFO
{
DWORD dwType; // Must be 0x1000.
LPCSTR szName; // Pointer to name (in user addr space).
DWORD dwThreadID; // Thread ID (-1=caller thread).
DWORD dwFlags; // Reserved for future use, must be zero.
} THREADNAME_INFO;
#pragma pack(pop)

CF_EXPORT void _NS_pthread_setname_np(const char *name) {
THREADNAME_INFO info;
info.dwType = 0x1000;
info.szName = name;
info.dwThreadID = GetCurrentThreadId();
info.dwFlags = 0;

__try
{
RaiseException( MS_VC_EXCEPTION, 0, sizeof(info)/sizeof(ULONG_PTR), (ULONG_PTR*)&info );
}
__except(EXCEPTION_EXECUTE_HANDLER)
{
}
_CFThreadSetName(GetCurrentThread(), name);
}

static _CFThreadRef __initialPthread = INVALID_HANDLE_VALUE;
Expand All @@ -569,6 +543,10 @@ CF_EXPORT int _NS_pthread_main_np() {
return CompareObjectHandles(__initialPthread, GetCurrentThread());
}

CF_EXPORT bool _NS_pthread_equal(_CFThreadRef t1, _CFThreadRef t2) {
return CompareObjectHandles(t1, t2) == TRUE;
}

#endif

#pragma mark -
Expand Down Expand Up @@ -618,7 +596,7 @@ CF_PRIVATE void __CFFinalizeWindowsThreadData() {
__CFTSDFinalize(TlsGetValue(__CFTSDIndexKey));
}

#endif
#else

static _CFThreadSpecificKey __CFTSDIndexKey;

Expand All @@ -629,6 +607,8 @@ CF_PRIVATE void __CFTSDInitialize() {
});
}

#endif

static void __CFTSDSetSpecific(void *arg) {
#if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_EMBEDDED_MINI
pthread_setspecific(__CFTSDIndexKey, arg);
Expand Down Expand Up @@ -677,14 +657,19 @@ static void __CFTSDFinalize(void *arg) {
table->destructors[i]((void *)(old));
}
}


#if _POSIX_THREADS
if (table->destructorCount == PTHREAD_DESTRUCTOR_ITERATIONS - 1) { // On PTHREAD_DESTRUCTOR_ITERATIONS-1 call, destroy our data
free(table);

// Now if the destructor is called again we will take the shortcut at the beginning of this function.
__CFTSDSetSpecific(CF_TSD_BAD_PTR);
return;
}
#else
free(table);
__CFTSDSetSpecific(CF_TSD_BAD_PTR);
#endif
}

#if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_EMBEDDED_MINI
Expand All @@ -703,7 +688,9 @@ static __CFTSDTable *__CFTSDGetTable(const Boolean create) {
// This memory is freed in the finalize function
table = (__CFTSDTable *)calloc(1, sizeof(__CFTSDTable));
// Windows and Linux have created the table already, we need to initialize it here for other platforms. On Windows, the cleanup function is called by DllMain when a thread exits. On Linux the destructor is set at init time.
#if !DEPLOYMENT_TARGET_WINDOWS
__CFTSDInitialize();
#endif
__CFTSDSetSpecific(table);
}

Expand Down Expand Up @@ -1380,12 +1367,45 @@ _CFThreadRef _CFThreadCreate(const _CFThreadAttributes attrs, void *_Nullable (*
#endif
}

#if DEPLOYMENT_TARGET_WINDOWS

// This code from here:
// http://msdn.microsoft.com/en-us/library/xcb2z8hs.aspx

const DWORD MS_VC_EXCEPTION=0x406D1388;
#pragma pack(push,8)
typedef struct tagTHREADNAME_INFO
{
DWORD dwType; // Must be 0x1000.
LPCSTR szName; // Pointer to name (in user addr space).
DWORD dwThreadID; // Thread ID (-1=caller thread).
DWORD dwFlags; // Reserved for future use, must be zero.
} THREADNAME_INFO;
#pragma pack(pop)

#endif

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);
}
return EINVAL;
#elif DEPLOYMENT_TARGET_WINDOWS
THREADNAME_INFO info;

info.dwType = 0x1000;
info.szName = name;
info.dwThreadID = GetThreadId(thread);
info.dwFlags = 0;

__try {
RaiseException(MS_VC_EXCEPTION, 0, sizeof(info) / sizeof(ULONG_PTR),
(ULONG_PTR*)&info);
} __except(EXCEPTION_EXECUTE_HANDLER) {
}

return 0;
#elif DEPLOYMENT_TARGET_LINUX
return pthread_setname_np(thread, name);
#endif
Expand Down
8 changes: 0 additions & 8 deletions CoreFoundation/Base.subproj/CFPriv.h
Original file line number Diff line number Diff line change
Expand Up @@ -546,14 +546,6 @@ CF_EXPORT void _CFLocaleResetCurrent(void);
CF_EXPORT CFMutableStringRef _CFCreateApplicationRepositoryPath(CFAllocatorRef alloc, int nFolder);
#endif

#if TARGET_OS_WIN32
#include <CoreFoundation/CFMessagePort.h>

#define CF_MESSAGE_PORT_CLONE_MESSAGE_ID -1209
CF_EXPORT CFMessagePortRef CFMessagePortCreateUber(CFAllocatorRef allocator, CFStringRef name, CFMessagePortCallBack callout, CFMessagePortContext *context, Boolean *shouldFreeInfo, Boolean isRemote);
CF_EXPORT void CFMessagePortSetCloneCallout(CFMessagePortRef ms, CFMessagePortCallBack cloneCallout);
#endif

#if (TARGET_OS_MAC && !(TARGET_OS_EMBEDDED || TARGET_OS_IPHONE || TARGET_OS_LINUX)) || (TARGET_OS_EMBEDDED || TARGET_OS_IPHONE)
#include <CoreFoundation/CFMessagePort.h>

Expand Down
8 changes: 4 additions & 4 deletions CoreFoundation/Base.subproj/ForFoundationOnly.h
Original file line number Diff line number Diff line change
Expand Up @@ -605,10 +605,10 @@ CF_EXPORT CFStringRef CFCopySystemVersionString(void);
CF_EXPORT CFDictionaryRef _CFCopySystemVersionDictionary(void);

CF_CROSS_PLATFORM_EXPORT Boolean _CFCalendarInitWithIdentifier(CFCalendarRef calendar, CFStringRef identifier);
CF_EXPORT Boolean _CFCalendarComposeAbsoluteTimeV(CFCalendarRef calendar, /* out */ CFAbsoluteTime *atp, const char *componentDesc, int32_t *vector, int32_t count);
CF_EXPORT Boolean _CFCalendarDecomposeAbsoluteTimeV(CFCalendarRef calendar, CFAbsoluteTime at, const char *componentDesc, int32_t *_Nonnull * _Nonnull vector, int32_t count);
CF_EXPORT Boolean _CFCalendarAddComponentsV(CFCalendarRef calendar, /* inout */ CFAbsoluteTime *atp, CFOptionFlags options, const char *componentDesc, int32_t *vector, int32_t count);
CF_EXPORT Boolean _CFCalendarGetComponentDifferenceV(CFCalendarRef calendar, CFAbsoluteTime startingAT, CFAbsoluteTime resultAT, CFOptionFlags options, const char *componentDesc, int32_t *_Nonnull * _Nonnull vector, int32_t count);
CF_CROSS_PLATFORM_EXPORT Boolean _CFCalendarComposeAbsoluteTimeV(CFCalendarRef calendar, /* out */ CFAbsoluteTime *atp, const char *componentDesc, int32_t *vector, int32_t count);
CF_CROSS_PLATFORM_EXPORT Boolean _CFCalendarDecomposeAbsoluteTimeV(CFCalendarRef calendar, CFAbsoluteTime at, const char *componentDesc, int32_t *_Nonnull * _Nonnull vector, int32_t count);
CF_CROSS_PLATFORM_EXPORT Boolean _CFCalendarAddComponentsV(CFCalendarRef calendar, /* inout */ CFAbsoluteTime *atp, CFOptionFlags options, const char *componentDesc, int32_t *vector, int32_t count);
CF_CROSS_PLATFORM_EXPORT Boolean _CFCalendarGetComponentDifferenceV(CFCalendarRef calendar, CFAbsoluteTime startingAT, CFAbsoluteTime resultAT, CFOptionFlags options, const char *componentDesc, int32_t *_Nonnull * _Nonnull vector, int32_t count);

CF_CROSS_PLATFORM_EXPORT Boolean _CFLocaleInit(CFLocaleRef locale, CFStringRef identifier);

Expand Down
12 changes: 9 additions & 3 deletions CoreFoundation/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -400,9 +400,15 @@ else()
-fno-common)
endif()
if(CF_DEPLOYMENT_SWIFT)
target_compile_options(CoreFoundation
PRIVATE
-fcf-runtime-abi=swift)
if("${CMAKE_C_SIMULATE_ID}" STREQUAL "MSVC")
target_compile_options(CoreFoundation
PRIVATE
/clang:-fcf-runtime-abi=swift)
else()
target_compile_options(CoreFoundation
PRIVATE
-fcf-runtime-abi=swift)
endif()
endif()

target_compile_options(CoreFoundation
Expand Down
2 changes: 1 addition & 1 deletion CoreFoundation/Parsing.subproj/CFPropertyList.c
Original file line number Diff line number Diff line change
Expand Up @@ -707,7 +707,7 @@ CFDataRef CFPropertyListCreateXMLData(CFAllocatorRef allocator, CFPropertyListRe
return _CFPropertyListCreateXMLData(allocator, propertyList, true);
}

CF_EXPORT CFDataRef _CFPropertyListCreateXMLDataWithExtras(CFAllocatorRef allocator, CFPropertyListRef propertyList) {
CFDataRef _CFPropertyListCreateXMLDataWithExtras(CFAllocatorRef allocator, CFPropertyListRef propertyList) {
return _CFPropertyListCreateXMLData(allocator, propertyList, false);
}

Expand Down
10 changes: 7 additions & 3 deletions CoreFoundation/RunLoop.subproj/CFRunLoop.c
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,8 @@ static void _runLoopTimerWithBlockContext(CFRunLoopTimerRef timer, void *opaqueB

#if DEPLOYMENT_TARGET_WINDOWS

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

Expand Down Expand Up @@ -428,7 +428,7 @@ static __CFPortSet __CFPortSetAllocate(void) {
result->used = 0;
result->size = 4;
result->handles = (HANDLE *)CFAllocatorAllocate(kCFAllocatorSystemDefault, result->size * sizeof(HANDLE), 0);
CF_SPINLOCK_INIT_FOR_STRUCTS(result->lock);
CF_LOCK_INIT_FOR_STRUCTS(result->lock);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that result->lock is of type CFLock_t on L415.

return result;
}

Expand Down Expand Up @@ -1568,7 +1568,11 @@ CF_EXPORT CFRunLoopRef _CFRunLoopGet0(_CFThreadRef t) {
if (pthread_equal(t, pthread_self())) {
_CFSetTSD(__CFTSDKeyRunLoop, (void *)loop, NULL);
if (0 == _CFGetTSD(__CFTSDKeyRunLoopCntr)) {
#if _POSIX_THREADS
_CFSetTSD(__CFTSDKeyRunLoopCntr, (void *)(PTHREAD_DESTRUCTOR_ITERATIONS-1), (void (*)(void *))__CFFinalizeRunLoop);
#else
_CFSetTSD(__CFTSDKeyRunLoopCntr, 0, &__CFFinalizeRunLoop);
#endif
}
}
return loop;
Expand Down
1 change: 1 addition & 0 deletions CoreFoundation/RunLoop.subproj/CFSocket.c
Original file line number Diff line number Diff line change
Expand Up @@ -990,6 +990,7 @@ typedef int32_t fd_mask;
typedef int socklen_t;

#define gettimeofday _NS_gettimeofday
struct timezone;
CF_PRIVATE int _NS_gettimeofday(struct timeval *tv, struct timezone *tz);

// although this is only used for debug info, we define it for compatibility
Expand Down