Implement type safety for TLS#26942
Implement type safety for TLS#26942ctiller merged 2 commits intogrpc:masterfrom tamird:type-safe-tls
Conversation
|
Oops, had some incorrect formatting. Craig, could I get another try run? |
|
Some significant changes here to reduce the tls.h API surface. One more kokoro run please, Craig. |
|
```
template <typename T, typename Ignored>
struct Wrapper;
template <typename T>
struct Wrapper<T, absl::enable_if_t<sizeof(T) <= sizeof(intptr_t)>> {
using Type = T;
};
#define GPR_THREAD_LOCAL(type) thread_local typename Wrapper<type>::Type
```
|
|
maybe better: |
|
Nice! Done, and made it apply uniformly to all toolchains (not just stdcpp). |
|
Collapsed all the implementations into the same header to avoid having to include tls.h from each one. Otherwise clang-tidy is unhappy: |
|
Damn it, compilation failures on Apple systems: Removed initialization of pthread_key_t. Another run, please? |
|
More compilation failures, I apparently forgot to include headers needed for pthread when I combined the headers. Should be fixed now. |
|
Fixed formatting. |
|
This is good stuff... Will give it one more thorough review after breakfast. One thing to note/discuss @veblush @nicolasnoble @markdroth -- #26912 passed the portability suites without using the GPR TLS stuff (I'd forgotten about the facility when I drafted the code) -- which warrants the question, do we know that we still need this facility, or would it suffice to just use the C++11 mechanism for supported compilers? When I built this mechanism we were still very much in C land where the ecosystem here is substantially more fractured. |
|
Assuming that we can use the C++ interface inside of core, that seems like a better option than this GPR API. The only reason I can think of why we might need the GPR API is if it's used by wrapped languages, so I'd suggest checking that. |
|
Yeah I guess I remembered the iOS thing... was hoping that was old enough to be gone now (is a 2016 phone OS old enough for us to drop support for?) |
|
Per the description in #24572:
So it isn't clear that this is really about phones. Anyway, removed all this stuff in #26960, let's see what the bots say there. |
|
Our minimum supported iOS version is 9 so theoretically we should support iOS 9.0 on both 32 bits and 64 bits anyway. We bumped this version (#24282) so I don't think we can bump this again soon without strong reason. This affects all iOS applications relying on gRPC so it should be done carefully. So we should keep iOS 9 32 bits emulation support. Also Google3 still depends on |
|
Makes sense to me. I can send another PR after this one to retire GPR_GCC_TLS and GPR_MSVC_TLS if that sounds reasonable to folks. |
|
Another question: should this move to |
|
If this is now a C++ API, I would be in favor of moving the C++ API to src/core/lib/gprpp. If we still need the C99 API for wrapped languages, that part can be in src/core/lib/gpr, but it should just be a wrapper around the C++ API. |
|
Wrapped languages do not use this API as far as I can tell. See these searches (all results are from this repo):
Shall I go ahead and move it? |
This is mostly free when compiler support is available, but requires careful templating when implemented using pthread. Significantly slimmed the tls.h interface; it now only defines the "TLS keyword" for each supported compiler, delegating enforcement of correct usage (i.e. must be static) to the compiler itself. Implemented implicit conversion for the pthread wrapper so it can be used (mostly) the same as native support. Notable exception to this is that static_cast<void*> is needed when printing a pointer stored in TLS as %p.
|
CI passed but formatting was off. Fixed now. |
markdroth
left a comment
There was a problem hiding this comment.
LGTM.
I'll let Craig work with you to get this merged.
* Implement type safety for TLS This is mostly free when compiler support is available, but requires careful templating when implemented using pthread. Significantly slimmed the tls.h interface; it now only defines the "TLS keyword" for each supported compiler, delegating enforcement of correct usage (i.e. must be static) to the compiler itself. Implemented implicit conversion for the pthread wrapper so it can be used (mostly) the same as native support. Notable exception to this is that static_cast<void*> is needed when printing a pointer stored in TLS as %p. * Use GPR_THREAD_LOCAL macros consistently
* Implement type safety for TLS This is mostly free when compiler support is available, but requires careful templating when implemented using pthread. Significantly slimmed the tls.h interface; it now only defines the "TLS keyword" for each supported compiler, delegating enforcement of correct usage (i.e. must be static) to the compiler itself. Implemented implicit conversion for the pthread wrapper so it can be used (mostly) the same as native support. Notable exception to this is that static_cast<void*> is needed when printing a pointer stored in TLS as %p. * Use GPR_THREAD_LOCAL macros consistently
I came across these type conversions while investigating https://bugs.fuchsia.dev/p/fuchsia/issues/detail?id=68325. It turns out that changing TLS from pthread to stdcpp (#26936) did resolve the UB on Fuchsia.
@ctiller @brunowonka