-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Switch ThreadId to an opaque value type. #7330
Conversation
Actually it does need to be an abstract type. Please read #6825 (comment) And it's not difficult to make them hashable. Just define a pure hash() method and supply a posix impl. I don't think it would be hard to revive/replicate #6825 and just make it work. Can we go in that direction instead of concretizing it. Happy to help if there is trouble. |
Oh I just correlated who you are ... is there some reason you wanted to go down this route rather than just make the object hashable? |
The idea of a full virtual hierachy for something that is an int64 (or smaller) on all platforms seems wasteful. Also, it has harder than just adding a hash virtual, you will need custom Hasher and Eq operators too. Making this a value makes it that much simpler to work with. |
Also, this is not a pthread_id it is a https://linux.die.net/man/2/gettid or a http://www.manpagez.com/man/3/pthread_threadid_np/ both of which are == comparable if I am reading it correctly... |
Current impl in posix is in source/common/common/posix/thread_impl.cc and is:
So the linux version calls syscall(SYS_gettid)) . Doc for gettid() I found is https://linux.die.net/man/2/gettid : which is
So that's where I got to pid_t from. I don't know of a platform where you couldn't render the pid_t as a uint64_t but the doc seems specific that you ought not to assume that, if you want your code to be portable. There were some efforts a while back to get Envoy working on Windows, though they seem to be dormant at the moment. My feeling is that it's not maximally efficient to have to have a virtual class around a uint64_t but given Envoy's thread-per-core model (https://blog.envoyproxy.io/envoy-threading-model-a8d44b922310), there may be hundreds but not thousands of threads, so this is not going to amount to any measurable cost in memory consumption, and it might make porting life easier for someone. RE Hash and Eq functors; I don't think you need an Eq functor if we add operator== to the class; it just works....realizing now who I'm talking to; is supplying a Hash but not an Eq to absl::flat_hash_map -- is that something that currently works but is not really supported? Obviously it's not hard to work around if we need to specify both. |
If you want a table of the thing you will need to do:
you definitely don't want the default Eq because it will do pointer comparison for that case |
I am a bit confused, #6825 (comment) specifically is talking about https://linux.die.net/man/3/pthread_self which is different from what the implementation you quoted use. My point is the implementation doesn't use the thing you linked in that comment and uses something that is == comparable (I think) |
Good point; I was thinking about keeping a table of the objects instead of pointers to them, but that's hard since ThreadId is abstract :) OK we need the Eq functor too. |
I am not wed to this change, but the extra indirections felt easy to remove and the result is an Id that you could keep by value in tables and have everything be much cleaner for. |
I think you're right; I was confused about pthread_self being relevant here; it looks like in posix pid_t is defined as a signed integer type, and in Windows it's a DWORD. One minor annoyance is that if we change the API we'll need to change the windows impl in source/common/common/win32/thread_impl.cc and I don't know who's set up to test that, and it's not in CI. But I guess we could just break it and let people who work on Windows sort it out later until they can get into CI. @sesmith177 are you still looking at Windows stuff? Last couple of times I pinged you you didn't respond. With regards to this PR you'll have to sort out DCO: https://github.com/envoyproxy/envoy/blob/master/CONTRIBUTING.md#dco-sign-your-work as well as fixing the format errors from CI and the compile error in compile_time_options. |
I will sort out those details tomorrow! Cheers |
@jmarantz yeah, I left Pivotal a couple months ago and so am no longer working on the port of Envoy to Windows. Since a DWORD is an unsigned 32-bit integer, it may be OK to just store it an int64, but it may just be safer to typedef it in include/envoy/thread/thread.h or something |
The static_cast<int64_t> for the DWORD is pretty easy and guaranteed to be safe. The sentinel I am using is out of range for a DWORD and thus guaranteed to be safe. I am less clear on if INT_MIN is safe sentinel for posix systems, but my guess is yes ;) |
I raised an issue on the slack (envoy-dev) about what to do about the windows code maintenance:
|
ThreadId does not need to be an abstract class and that just interferes with using them as keys in hashtables. Signed-off-by: Matt Kulukundis <[email protected]>
Signed-off-by: Matt Kulukundis <[email protected]>
fixed the formatting and DCO stuff |
Signed-off-by: Matt Kulukundis <[email protected]>
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.
generally looks fine; just wanted to see a hash-table definition come out of this effort as well :)
Also I will + in an Envoy Windows developer (Matt Horan) once he gives me his github ID, which I could not figure out.
Signed-off-by: Matt Kulukundis <[email protected]>
Signed-off-by: Matt Kulukundis <[email protected]>
Signed-off-by: Matt Kulukundis <[email protected]>
Hi there, note that on master I'm blocked on issue 7357 to have Win32 compile, so if someone has recommendations for a clean fix to the use of absl against an older stdc++ style str implementation, that should jumpstart my ability to inspect this fix. |
@wrowe @mhoran does it make sense to proceed with merging this PR with the somewhat speculative win32 implementation, and leave it in your hands to clean it up as we get win32 into CI? @fowles your clang-tidy errors appear to be in windows-related files where clang can't find windows.h etc. @lizan how do we work around this? The other CI failures may be known flakes and we can trigger a retest. I still need to do a final pass over the code. Thanks for pushing through this Matt. /retest |
🔨 rebuilding |
/azp run |
@envoyproxy/senior-maintainers over to you for a final pass. |
I don't think clang-tidy can be fixed for this PR as the PR includes windows-specific files, for which clang-tidy cannot find includes. |
Actually he can't fix the errors but he could fix the warnings:
|
Ah, that's what I get for commenting without reading the full thread. |
Signed-off-by: Matt Kulukundis <[email protected]>
fixed clang-tidy warnings and pulled from head |
Ah, lizan is out this week. Well as it's going to require Matt's force-merge anyway, @mattklein123 can you take a look? |
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.
Thanks, looks great with some tiny nits.
/wait
} | ||
|
||
private: | ||
int64_t id_; |
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.
nit: const
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.
I think we don't want this. As it stands, this is value type that can be copied and assigned normally. If we make this const we lost assignment
Signed-off-by: Matt Kulukundis <[email protected]>
Pulled from latest mainline, merged, made most of the changes requested |
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.
Thank you!
Sorry can you fix format? /wait |
Signed-off-by: Matt Kulukundis <[email protected]>
format fixed |
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.
Thank you!
Description: ThreadId does not need to be an abstract class and that just interferes
with using them as keys in hashtables.
For an explanation of how to fill out the fields, please see the relevant section
in PULL_REQUESTS.md
Risk Level: Low
Testing: Unit
Docs Changes: N/A
Release Notes: N/A