| ... | @@ -24,16 +24,14 @@ pub const Condition = @import("Thread/Condition.zig"); | ... | @@ -24,16 +24,14 @@ pub const Condition = @import("Thread/Condition.zig"); |
| 24 | pub const spinLoopHint = @compileError("deprecated: use std.atomic.spinLoopHint"); | 24 | pub const spinLoopHint = @compileError("deprecated: use std.atomic.spinLoopHint"); |
| 25 | | 25 | |
| 26 | test "std.Thread" { | 26 | test "std.Thread" { |
| 27 | if (!builtin.single_threaded) { | 27 | // Doesn't use testing.refAllDecls() since that would pull in the compileError spinLoopHint. |
| 28 | // Doesn't use testing.refAllDecls() since that would pull in the compileError spinLoopHint. | 28 | _ = AutoResetEvent; |
| 29 | _ = AutoResetEvent; | 29 | _ = Futex; |
| 30 | _ = Futex; | 30 | _ = ResetEvent; |
| 31 | _ = ResetEvent; | 31 | _ = StaticResetEvent; |
| 32 | _ = StaticResetEvent; | 32 | _ = Mutex; |
| 33 | _ = Mutex; | 33 | _ = Semaphore; |
| 34 | _ = Semaphore; | 34 | _ = Condition; |
| 35 | _ = Condition; | | |
| 36 | } | | |
| 37 | } | 35 | } |
| 38 | | 36 | |
| 39 | pub const use_pthreads = target.os.tag != .windows and std.builtin.link_libc; | 37 | pub const use_pthreads = target.os.tag != .windows and std.builtin.link_libc; |
| ... | @@ -114,17 +112,13 @@ pub const SpawnError = error { | ... | @@ -114,17 +112,13 @@ pub const SpawnError = error { |
| 114 | /// `config` can be used as hints to the platform for now to spawn and execute the `function`. | 112 | /// `config` can be used as hints to the platform for now to spawn and execute the `function`. |
| 115 | /// The caller must eventually either call `join()` to wait for the thread to finish and free its resources | 113 | /// The caller must eventually either call `join()` to wait for the thread to finish and free its resources |
| 116 | /// or call `detach()` to excuse the caller from calling `join()` and have the thread clean up its resources on completion`. | 114 | /// or call `detach()` to excuse the caller from calling `join()` and have the thread clean up its resources on completion`. |
| 117 | pub fn spawn( | 115 | pub fn spawn(config: SpawnConfig, comptime function: anytype, args: anytype) SpawnError!Thread { |
| 118 | config: SpawnConfig, | | |
| 119 | comptime function: anytype, | | |
| 120 | args: std.meta.ArgsTuple(function), | | |
| 121 | ) SpawnError!Thread { | | |
| 122 | if (std.builtin.single_threaded) { | 116 | if (std.builtin.single_threaded) { |
| 123 | @compileError("cannot spawn thread when building in single-threaded mode"); | 117 | @compileError("cannot spawn thread when building in single-threaded mode"); |
| 124 | } | 118 | } |
| 125 | | 119 | |
| 126 | const impl = try Thread.spawn(config, function, args); | 120 | const impl = try Impl.spawn(config, function, args); |
| 127 | return .{ .impl = impl }; | 121 | return Thread{ .impl = impl }; |
| 128 | } | 122 | } |
| 129 | | 123 | |
| 130 | /// Represents a kernel thread handle. | 124 | /// Represents a kernel thread handle. |
| ... | @@ -438,7 +432,7 @@ const LinuxThreadImpl = struct { | ... | @@ -438,7 +432,7 @@ const LinuxThreadImpl = struct { |
| 438 | | 432 | |
| 439 | fn getCurrentId() Id { | 433 | fn getCurrentId() Id { |
| 440 | return tls_thread_id orelse { | 434 | return tls_thread_id orelse { |
| 441 | const tid = linux.gettid(); | 435 | const tid = @bitCast(u32, linux.gettid()); |
| 442 | tls_thread_id = tid; | 436 | tls_thread_id = tid; |
| 443 | return tid; | 437 | return tid; |
| 444 | }; | 438 | }; |
| ... | @@ -550,7 +544,7 @@ const LinuxThreadImpl = struct { | ... | @@ -550,7 +544,7 @@ const LinuxThreadImpl = struct { |
| 550 | const instance = @ptrCast(*Instance, @alignCast(@alignOf(Instance), &mapped[instance_offset])); | 544 | const instance = @ptrCast(*Instance, @alignCast(@alignOf(Instance), &mapped[instance_offset])); |
| 551 | instance.* = .{ | 545 | instance.* = .{ |
| 552 | .fn_args = args, | 546 | .fn_args = args, |
| 553 | .thread = .{ .mapped = .mapped }, | 547 | .thread = .{ .mapped = mapped }, |
| 554 | }; | 548 | }; |
| 555 | | 549 | |
| 556 | const flags: u32 = os.CLONE_VM | os.CLONE_FS | os.CLONE_FILES | | 550 | const flags: u32 = os.CLONE_VM | os.CLONE_FS | os.CLONE_FILES | |
| ... | @@ -591,7 +585,7 @@ const LinuxThreadImpl = struct { | ... | @@ -591,7 +585,7 @@ const LinuxThreadImpl = struct { |
| 591 | } | 585 | } |
| 592 | | 586 | |
| 593 | fn join(self: Impl) void { | 587 | fn join(self: Impl) void { |
| 594 | defer self.thread.free(); | 588 | defer os.munmap(self.thread.mapped); |
| 595 | | 589 | |
| 596 | var spin: u8 = 10; | 590 | var spin: u8 = 10; |
| 597 | while (true) { | 591 | while (true) { |