-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Add thread ID support to std.os.Thread (fixes #1316) #1330
Conversation
Hmm, looks like it works on Mac (which is what I wrote and tested this on) and fails on Linux and Windows. So I will have to dig into this. |
@mdsteele I sent you a pull request. |
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.
Almost there...
std/os/index.zig
Outdated
@@ -2516,26 +2516,56 @@ pub const Thread = struct { | |||
data: Data, | |||
|
|||
pub const use_pthreads = is_posix and builtin.link_libc; | |||
|
|||
/// An opaque type representing a kernel thread 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.
Remove/update this comment because it does not match the code now.
std/os/test.zig
Outdated
} | ||
|
||
test "std.os.Thread.currentId" { | ||
var threadCurrentId: ?os.Thread.Id = null; |
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.
os.Thread.currentId()
should always return os.Thread.Id
, so no need to make it optional.
fn testThreadIdFn(threadId: *?os.Thread.Id) void {
to
fn testThreadIdFn(threadId: *os.Thread.Id) void {
std/os/test.zig
Outdated
const thread = try os.spawnThread(&threadCurrentId, testThreadIdFn); | ||
const threadId = thread.id(); | ||
thread.wait(); | ||
assert(threadCurrentId == threadId); |
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.
If you fix the above, this should work.
It doesn't work, because of issue ziglang#1332.
I fixed it on linux in a local branch. Just needed to use For windows, I think this is the problem:
So the result of |
Whoops, duh. I think I saw that the handle variable for Linux was called
Whoops again. I should probably be reading the documentation of these functions I'm calling, huh? Speaking of said documentation, I see it also says this:
Sounds promising. I'll look into it. |
I believe I have the tests passing on all platforms: Here's what I did:
I thought about DuplicateHandle but I don't think it accomplishes anything for us. It still wouldn't let us compare the original handle with the new handle, since the duplicate one would be a different address. Plus you'd introduce the ability of I don't think this is where the API will stay. It's pretty messy and not clear where the cross platform abstraction ends and begins, however, let's focus on your allocator use case and make sure that is solved appropriately, and then we can revisit the std lib API before 1.0.0. If I understand correctly, what you're now going to do is use this as a way to detect which thread is calling into the allocator, using |
This provides a cross-platform way to both (1) get a unique (opaque) ID for the current thread (using
std.os.Thread.currentId()
), and (2) given astd.os.Thread
object, get the unique ID that that thread would get if it calledstd.os.Thread.currentId()
. Note that this second thing is something the proposedgetThreadId
solution in #1316 usingthreadlocal
can't do.What do you think?