Skip to content

stdlib: generalise TLS to support Windows #11949

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 1 commit into from
Sep 18, 2017
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
24 changes: 12 additions & 12 deletions stdlib/public/SwiftShims/LibcShims.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,28 +114,28 @@ double _swift_stdlib_squareRoot(double _self) {
// TLS - thread local storage

#if defined(__ANDROID__)
typedef int __swift_pthread_key_t;
typedef int __swift_thread_key_t;
#elif defined(__linux__)
typedef unsigned int __swift_pthread_key_t;
typedef unsigned int __swift_thread_key_t;
#elif defined(__FreeBSD__)
typedef int __swift_pthread_key_t;
typedef int __swift_thread_key_t;
#elif defined(_WIN32)
typedef unsigned long __swift_thread_key_t;
#else
typedef unsigned long __swift_pthread_key_t;
typedef unsigned long __swift_thread_key_t;
#endif

SWIFT_RUNTIME_STDLIB_INTERFACE
int _swift_stdlib_pthread_key_create(
__swift_pthread_key_t * _Nonnull key, void
(* _Nullable destructor)(void * _Nullable )
);
int
_swift_stdlib_thread_key_create(__swift_thread_key_t * _Nonnull key,
void (* _Nullable destructor)(void * _Nullable));

SWIFT_RUNTIME_STDLIB_INTERFACE
void * _Nullable _swift_stdlib_pthread_getspecific(__swift_pthread_key_t key);
void * _Nullable _swift_stdlib_thread_getspecific(__swift_thread_key_t key);

SWIFT_RUNTIME_STDLIB_INTERFACE
int _swift_stdlib_pthread_setspecific(
__swift_pthread_key_t key, const void * _Nullable value
);
int _swift_stdlib_thread_setspecific(__swift_thread_key_t key,
const void * _Nullable value);

// TODO: Remove horrible workaround when importer does Float80 <-> long double.
#if (defined __i386__ || defined __x86_64__) && !defined _MSC_VER
Expand Down
14 changes: 7 additions & 7 deletions stdlib/public/core/ThreadLocalStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ internal struct _ThreadLocalStorage {
static internal func getPointer()
-> UnsafeMutablePointer<_ThreadLocalStorage>
{
let tlsRawPtr = _swift_stdlib_pthread_getspecific(_tlsKey)
let tlsRawPtr = _swift_stdlib_thread_getspecific(_tlsKey)
if _fastPath(tlsRawPtr != nil) {
return tlsRawPtr._unsafelyUnwrappedUnchecked.assumingMemoryBound(
to: _ThreadLocalStorage.self)
Expand Down Expand Up @@ -116,10 +116,10 @@ internal func _destroyTLS(_ ptr: UnsafeMutableRawPointer?) {
}

// Lazily created global key for use with pthread TLS
internal let _tlsKey: __swift_pthread_key_t = {
let sentinelValue = __swift_pthread_key_t.max
var key: __swift_pthread_key_t = sentinelValue
let success = _swift_stdlib_pthread_key_create(&key, _destroyTLS)
internal let _tlsKey: __swift_thread_key_t = {
let sentinelValue = __swift_thread_key_t.max
var key: __swift_thread_key_t = sentinelValue
let success = _swift_stdlib_thread_key_create(&key, _destroyTLS)
Copy link
Member Author

Choose a reason for hiding this comment

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

Is there a way to change the calling convention for _destroyTLS? On Windows (x86), this should be a __stdcall function.

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think the Swift compiler can manipulate Windows calling conventions yet. It should be straightforward to rewrite _destroyTLS in C, or write a C __stdcall function that calls back into Swift.

Copy link
Member Author

Choose a reason for hiding this comment

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

We could introduce a @_callingconv attribute. But, it sounds like it would be easier to move it to swift-shims and rewrite it in C.

Copy link
Member

Choose a reason for hiding this comment

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

A shim that calls back into the stdlib for deinitialization would be my preferred approach. We have non-trivial deinitialization logic that ideally would be kept in Swift. We likewise probably want allocate/deallocate parity, and Swift knows the sizes of the TLS buffer already.

_sanityCheck(success == 0, "somehow failed to create TLS key")
_sanityCheck(key != sentinelValue, "Didn't make a new key")
return key
Expand All @@ -129,7 +129,7 @@ internal let _tlsKey: __swift_pthread_key_t = {
internal func _initializeThreadLocalStorage()
-> UnsafeMutablePointer<_ThreadLocalStorage>
{
_sanityCheck(_swift_stdlib_pthread_getspecific(_tlsKey) == nil,
_sanityCheck(_swift_stdlib_thread_getspecific(_tlsKey) == nil,
"already initialized")

// Create and initialize one.
Expand All @@ -146,7 +146,7 @@ internal func _initializeThreadLocalStorage()
tlsPtr.initialize(
to: _ThreadLocalStorage(_uBreakIterator: newUBreakIterator)
)
let success = _swift_stdlib_pthread_setspecific(_tlsKey, tlsPtr)
let success = _swift_stdlib_thread_setspecific(_tlsKey, tlsPtr)
_sanityCheck(success == 0, "setspecific failed")
return tlsPtr
}
Expand Down
53 changes: 39 additions & 14 deletions stdlib/public/stubs/LibcShims.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@
#include <cmath>
#if defined(_WIN32)
#include <io.h>
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#else
#include <unistd.h>
#endif
#include <pthread.h>
#endif

#include <stdlib.h>
#include <stdio.h>
Expand Down Expand Up @@ -60,7 +62,7 @@ __swift_size_t swift::_swift_stdlib_strlen(const char *s) {

SWIFT_RUNTIME_STDLIB_INTERFACE
__swift_size_t swift::_swift_stdlib_strlen_unsigned(const unsigned char *s) {
return strlen((char *)s);
return strlen(reinterpret_cast<const char *>(s));
}

SWIFT_RUNTIME_STDLIB_INTERFACE
Expand Down Expand Up @@ -98,34 +100,57 @@ int swift::_swift_stdlib_close(int fd) {
#endif
}

// Guard compilation on the typedef for __swift_pthread_key_t in LibcShims.h
#if defined(_WIN32)
static_assert(std::is_same<__swift_thread_key_t, DWORD>::value,
"__swift_thread_key_t is not a DWORD");

SWIFT_RUNTIME_STDLIB_INTERFACE
int
swift::_swift_stdlib_thread_key_create(__swift_thread_key_t * _Nonnull key,
void (* _Nullable destructor)(void *)) {
// TODO(compnerd) account for x86 CC violation (__stdcall)
*key = FlsAlloc(reinterpret_cast<PFLS_CALLBACK_FUNCTION>(destructor));
return *key != FLS_OUT_OF_INDEXES;
}

SWIFT_RUNTIME_STDLIB_INTERFACE
void * _Nullable
swift::_swift_stdlib_thread_getspecific(__swift_thread_key_t key) {
return FlsGetValue(key);
}

SWIFT_RUNTIME_STDLIB_INTERFACE
int swift::_swift_stdlib_thread_setspecific(__swift_thread_key_t key,
const void * _Nullable value) {
return FlsSetValue(key, const_cast<void *>(value)) == TRUE;
}
#else
// Guard compilation on the typedef for __swift_thread_key_t in LibcShims.h
// being identical to the platform's pthread_key_t
static_assert(std::is_same<__swift_pthread_key_t, pthread_key_t>::value,
static_assert(std::is_same<__swift_thread_key_t, pthread_key_t>::value,
"This platform's pthread_key_t differs. If you hit this assert, "
"fix __swift_pthread_key_t's typedef in LibcShims.h by adding an "
"#if guard and definition for your platform");

SWIFT_RUNTIME_STDLIB_INTERFACE
int swift::_swift_stdlib_pthread_key_create(
__swift_pthread_key_t * _Nonnull key,
void (* _Nullable destructor)(void *)
) {
int
swift::_swift_stdlib_thread_key_create(__swift_thread_key_t * _Nonnull key,
void (* _Nullable destructor)(void *)) {
return pthread_key_create(key, destructor);
}

SWIFT_RUNTIME_STDLIB_INTERFACE
void * _Nullable swift::_swift_stdlib_pthread_getspecific(
__swift_pthread_key_t key
) {
void * _Nullable
swift::_swift_stdlib_thread_getspecific(__swift_thread_key_t key) {
return pthread_getspecific(key);
}

SWIFT_RUNTIME_STDLIB_INTERFACE
int swift::_swift_stdlib_pthread_setspecific(
__swift_pthread_key_t key, const void * _Nullable value
) {
int swift::_swift_stdlib_thread_setspecific(__swift_thread_key_t key,
const void * _Nullable value) {
return pthread_setspecific(key, value);
}
#endif

#if defined(__APPLE__)
#include <malloc/malloc.h>
Expand Down