libuv: thread self, no deinit

This commit is contained in:
Mitchell Hashimoto
2022-04-21 16:28:06 -07:00
parent a5b3b52b1b
commit 4b0cddc948

View File

@ -9,6 +9,11 @@ const errors = @import("error.zig");
thread: c.uv_thread_t, thread: c.uv_thread_t,
/// Get the current thread
pub fn self() Thread {
return .{ .thread = c.uv_thread_self() };
}
/// Initialize a new thread. /// Initialize a new thread.
pub fn init( pub fn init(
comptime callback: fn () void, comptime callback: fn () void,
@ -52,18 +57,13 @@ pub fn initData(
return res; return res;
} }
pub fn deinit(self: *Thread) void { pub fn join(t: *Thread) !void {
self.* = undefined; try errors.convertError(c.uv_thread_join(&t.thread));
}
pub fn join(self: *Thread) !void {
try errors.convertError(c.uv_thread_join(&self.thread));
} }
test "Thread: no data argument" { test "Thread: no data argument" {
count = 0; count = 0;
var thread = try init(incr); var thread = try init(incr);
defer thread.deinit();
try thread.join(); try thread.join();
try testing.expectEqual(@as(u8, 1), count); try testing.expectEqual(@as(u8, 1), count);
} }
@ -72,7 +72,6 @@ test "Thread: with data argument" {
count = 0; count = 0;
var data: u8 = 2; var data: u8 = 2;
var thread = try initData(&data, incrBy); var thread = try initData(&data, incrBy);
defer thread.deinit();
try thread.join(); try thread.join();
try testing.expectEqual(@as(u8, 2), count); try testing.expectEqual(@as(u8, 2), count);
} }