Skip to content

Use _Thread_local for thread-local storage #358

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
Jun 7, 2018
Merged

Use _Thread_local for thread-local storage #358

merged 1 commit into from
Jun 7, 2018

Conversation

adierking
Copy link
Contributor

When building for Windows, shims/tsd.h declares __dispatch_tsd with
__declspec(thread). init.c needs to match this or else it will not compile with
newer versions of Clang.

@adierking
Copy link
Contributor Author

cc @compnerd

src/init.c Outdated
#if defined(_WIN32)
__declspec(thread) struct dispatch_tsd __dispatch_tsd;
Copy link
Contributor

Choose a reason for hiding this comment

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

would using _Thread_local work instead? I would assume it maps to the right thing on both platforms, and libdispatch otherwise already requires C11

Copy link
Contributor Author

Choose a reason for hiding this comment

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

MSVC actually doesn't support C11 threads, so using __declspec(thread) is the canonical method of declaring thread-local variables in Windows code. I agree that using _Thread_local everywhere would be best if we don't care about building with MSVC (I don't).

Copy link
Contributor

Choose a reason for hiding this comment

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

by MSVC you mean the MSFT C compiler as opposed to clang, correct? Not MSFT Visual Studio?

libdispatch needs clang (which is I'm told supported on Windows nowadays?) for blocks support anyway (and more). So yeah I don't think we care about MSVC in this instance, if _Thread_local does the job, we should use it, __thread really is a gcc-ism and C11 has the right thing.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

by MSVC you mean the MSFT C compiler as opposed to clang, correct?

Right.

I updated this to use _Thread_local now. Thanks for the guidance.

init.c currently does not compile for Windows with newer versions of Clang
because it declares `__dispatch_tsd` with `__thread` but shims/tsd.h declares it
with `__declspec(thread)`. Use the standard `_Thread_local` instead.
@adierking adierking changed the title Fix the __dispatch_tsd declaration for Windows Use _Thread_local for thread-local storage Jun 5, 2018
@compnerd
Copy link
Member

compnerd commented Jun 6, 2018

I think that using _Thread_local is fine. We require C11, we require clang/clang-cl. It's better to use the modern, standards approach. LGTM

@MadCoder
Copy link
Contributor

MadCoder commented Jun 6, 2018

@swift-ci please test

1 similar comment
@compnerd
Copy link
Member

compnerd commented Jun 7, 2018

@swift-ci please test

@MadCoder MadCoder merged commit 4ac77b7 into swiftlang:master Jun 7, 2018
@adierking adierking deleted the tsd branch December 2, 2018 00:33
ktopley-apple pushed a commit that referenced this pull request Dec 6, 2018
Use _Thread_local for thread-local storage

Signed-off-by: Kim Topley <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants