| ... | @@ -25,6 +25,7 @@ pub const spinLoopHint = @compileError("deprecated: use std.atomic.spinLoopHint" | ... | @@ -25,6 +25,7 @@ pub const spinLoopHint = @compileError("deprecated: use std.atomic.spinLoopHint" |
| 25 | | 25 | |
| 26 | pub const use_pthreads = target.os.tag != .windows and std.builtin.link_libc; | 26 | pub const use_pthreads = target.os.tag != .windows and std.builtin.link_libc; |
| 27 | | 27 | |
| | 28 | const Thread = @This(); |
| 28 | const Impl = if (target.os.tag == .windows) | 29 | const Impl = if (target.os.tag == .windows) |
| 29 | WindowsThreadImpl | 30 | WindowsThreadImpl |
| 30 | else if (use_pthreads) | 31 | else if (use_pthreads) |
| ... | @@ -36,7 +37,6 @@ else | ... | @@ -36,7 +37,6 @@ else |
| 36 | | 37 | |
| 37 | impl: Impl, | 38 | impl: Impl, |
| 38 | | 39 | |
| 39 | | | |
| 40 | /// Represents a kernel thread handle. | 40 | /// Represents a kernel thread handle. |
| 41 | /// May be an integer or a pointer depending on the platform. | 41 | /// May be an integer or a pointer depending on the platform. |
| 42 | /// On Linux and POSIX, this is the same as Id. | 42 | /// On Linux and POSIX, this is the same as Id. |
| ... | @@ -120,6 +120,29 @@ pub fn spawn( | ... | @@ -120,6 +120,29 @@ pub fn spawn( |
| 120 | return .{ .impl = impl }; | 120 | return .{ .impl = impl }; |
| 121 | } | 121 | } |
| 122 | | 122 | |
| | 123 | /// Retrns the handle of this thread |
| | 124 | /// On Linux and POSIX, this is the same as Id. |
| | 125 | pub fn getHandle(self: Thread) Handle { |
| | 126 | return self.impl.getHandle(); |
| | 127 | } |
| | 128 | |
| | 129 | /// Release the obligation of the caller to call `join()` and have the thread clean up its own resources on completion. |
| | 130 | pub fn detach(self: Thread) void { |
| | 131 | return self.impl.detach(); |
| | 132 | } |
| | 133 | |
| | 134 | /// Waits for the thread to complete, then deallocates any resources created on `spawn()`. |
| | 135 | pub fn join(self: Thread) void { |
| | 136 | return self.impl.join(); |
| | 137 | } |
| | 138 | |
| | 139 | /// State to synchronize detachment of spawner thread to spawned thread |
| | 140 | const Completion = Atomic(enum { |
| | 141 | running, |
| | 142 | detached, |
| | 143 | completed, |
| | 144 | }); |
| | 145 | |
| 123 | /// Used by the Thread implementations to call the spawned function with the arguments. | 146 | /// Used by the Thread implementations to call the spawned function with the arguments. |
| 124 | fn callFn(comptime f: anytype, args: anytype) switch (Impl) { | 147 | fn callFn(comptime f: anytype, args: anytype) switch (Impl) { |
| 125 | WindowsThreadImpl => windows.DWORD, | 148 | WindowsThreadImpl => windows.DWORD, |
| ... | @@ -172,29 +195,6 @@ fn callFn(comptime f: anytype, args: anytype) switch (Impl) { | ... | @@ -172,29 +195,6 @@ fn callFn(comptime f: anytype, args: anytype) switch (Impl) { |
| 172 | } | 195 | } |
| 173 | } | 196 | } |
| 174 | | 197 | |
| 175 | /// Retrns the handle of this thread | | |
| 176 | /// On Linux and POSIX, this is the same as Id. | | |
| 177 | pub fn getHandle(self: Thread) Handle { | | |
| 178 | return self.impl.getHandle(); | | |
| 179 | } | | |
| 180 | | | |
| 181 | /// Release the obligation of the caller to call `join()` and have the thread clean up its own resources on completion. | | |
| 182 | pub fn detach(self: Thread) void { | | |
| 183 | return self.impl.detach(); | | |
| 184 | } | | |
| 185 | | | |
| 186 | /// Waits for the thread to complete, then deallocates any resources created on `spawn()`. | | |
| 187 | pub fn join(self: Thread) void { | | |
| 188 | return self.impl.join(); | | |
| 189 | } | | |
| 190 | | | |
| 191 | /// State to synchronize detachment of spawner thread to spawned thread | | |
| 192 | const Completion = Atomic(enum { | | |
| 193 | running, | | |
| 194 | detached, | | |
| 195 | completed, | | |
| 196 | }); | | |
| 197 | | | |
| 198 | const WindowsThreadImpl = struct { | 198 | const WindowsThreadImpl = struct { |
| 199 | const windows = os.windows; | 199 | const windows = os.windows; |
| 200 | | 200 | |