-
Notifications
You must be signed in to change notification settings - Fork 10.5k
stdlib/public fixes for Windows support #11110
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
Conversation
@milseman This touches TLS, so you should probably have a look |
libc++ at least has a complete abstraction over the threading model (it uses Win32 threading on Windows, and pthreads on Linux/Darwin). It would be nice if we could use that instead in the LibcShims.cpp if the TLS data is not exposed to others through the |
From what I can tell, the only usage of those methods is in |
Right, this key is for the standard library internal use only. I think that on Darwin, we will still want to call pthread for now (see https://bugs.swift.org/browse/SR-4915, CC @gparker42 and @atrick). Otherwise, I think @troughton's renaming the shims is a good idea to reflect that it's not literally pthread on all platforms anymore. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rename _swift_stdlib_pthread_[get|set]specific
to _swift_stdlib_tls_[get|set]specific
and this looks great to me!
I've done the renaming. I also modified my original change to use the Fls* rather than Tls* functions on Windows, which should do the same thing (as per the comment in LibcShims.cpp) and ensures the destructor is correctly called. |
The suggestion from @compnerd seems like an excellent one and should not be ignored. Using the C++ standard library as a portability layer, where applicable, is a technique we want to employ elsewhere, e.g. random number generation. |
@dabrahams I am not ignoring his suggestions. I agree with you in general, but there are specific (pun intended!) concerns surrounding project policy and especially surrounding Darwin's implementation of thread local storage. (Also, as this PR was originally written, the semantics were potentially different, but I'm completely unfamiliar with "fibers" on Windows. @troughton's latest commit makes this a moot point.) @compnerd, are you referring specifically to Another approach would be to copy the definitions (or even the file itself) into the Swift project. I don't think that should block this PR, but if anyone thinks that's worth pursuing, please file an SR. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
@swift-ci please smoke test |
@milseman yeah, I was referring to the |
|
||
#if defined(_WIN32) | ||
char demangled[1024]; | ||
DWORD length = UnDecorateSymbolName(syminfo.symbolName, demangled, 1024, 0); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using sizeof(demanglled) / sizeof(*demangled)
or defining a local array_size
as follows would be nicer:
template <typename T, size_t N>
size_t array_size(const T (&)[N]) { return N; }
stdlib/public/stubs/LibcShims.cpp
Outdated
@@ -15,10 +15,11 @@ | |||
#include <cmath> | |||
#if defined(_WIN32) | |||
#include <io.h> | |||
#include <windows.h> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably a good idea to add:
#define WIN32_LEAN_AND_MEAN
#define VC_EXTRA_LEAN
#define NOMINMAX
before the windows.h
include to avoid a ton of Windows.h
cruft. It particularly helps clang.
// on Windows, "If no fiber switching occurs, FLS acts exactly the same | ||
// as thread local storage," and FlsAlloc takes a destructor while | ||
// TlsAlloc doesn't. | ||
// https://msdn.microsoft.com/en-us/library/windows/desktop/ms682661.aspx |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This also makes the code more portable, as some Windows targets (e.g. Windows Store, Windows Phone) only support the FLS variant.
stdlib/public/stubs/LibcShims.cpp
Outdated
@@ -15,6 +15,9 @@ | |||
#include <cmath> | |||
#if defined(_WIN32) | |||
#include <io.h> | |||
#define WIN32_LEAN_AND_MEAN | |||
#define VC_EXTRA_LEAN |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mispelled VC_EXTRALEAN
I think? ref
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed - thanks for the catch.
@swift-ci please smoke test |
@compnerd, there are two constraints here. First, we want to manage the TLS allocation, initialization, and deinitialization on the Swift side for the stdlib. The second is that after ABI stability, we definitely want to be making pthread calls on Darwin for highly-Darwin-specific reasons. Currently, the TLS only caches an ICU pointer, but we want the ability to add pure-Swift constructs onto it in the future. If |
I've rebased this onto master; the only modification is removing what I'd changed in Is this pull request ready to be merged? I know there was some discussion about possibly moving to |
@swift-ci please smoke test |
@troughton sorry, if tests pass I will merge. |
This now requires rebasing |
I'm sorry, I thought this got merged. @compnerd, you've been generalizing this area as well. Does this PR overlap with your work? I'm going to assign to you to help steward this through. edit: Please feel free to assign to me if you're no longer working in this area. |
This pull request contains a few changes to allow the stdlib to compile against the Windows/MSVC toolchain.
Of particular note:
There was a compile issue with the use of __cxa_demangle in Errors.cpp as that's unavailable on Windows. The containing method is actually not used on Windows, since it's conditional on
SWIFT_SUPPORTS_BACKTRACE_REPORTING
; however, I thought it better to provide an implementation for when Windows eventually does support that.I'm using the Windows API for thread-local storage, but it's still implemented under the pthread_create method; the functionality should be the same, but a higher level of abstraction might be appropriate. The other caveat with this implementation is that
TlsAlloc
doesn't support a destructor; from what I can gather, it takes care of disposing its own memory, but if I'm incorrect in that or misunderstanding the usage this could cause a memory leak.