threading tests

This commit is contained in:
2025-07-21 08:37:32 -04:00
parent 466d614d75
commit cb27084693
2 changed files with 34 additions and 6 deletions

View File

@@ -5,18 +5,46 @@ const Self = @This();
export const MODULE = core.module(Self);
_unload: std.Thread.ResetEvent = .{},
looper: ?std.Thread = null,
pub fn startup(_: std.mem.Allocator) !Self {
std.log.debug("!! startup {s} !!", .{@typeName(@This())});
return .{};
}
pub fn unload(_: *Self, _: std.mem.Allocator) ![]u8 {
std.log.debug("!! unload {s} !!", .{@typeName(@This())});
return &.{};
pub fn reload(self: *Self, alloc: std.mem.Allocator, _: []u8) !void {
std.log.debug("!! reload {s} !!", .{@typeName(@This())});
self._unload.reset();
self.looper = try std.Thread.spawn(
.{ .allocator = alloc },
_loop,
.{self},
);
errdefer {
self._unload.set();
self.looper.?.join();
}
}
pub fn reload(_: *Self, _: std.mem.Allocator, _: []u8) !void {
std.log.debug("!! reload {s} !!", .{@typeName(@This())});
fn _loop(self: *Self) void {
std.log.debug("START", .{});
for (0..10) |i| {
std.log.debug("LOOP {d}", .{i});
self._unload.timedWait(std.time.ns_per_s) catch continue;
std.log.debug("HALT", .{});
break;
}
}
pub fn unload(self: *Self, _: std.mem.Allocator) ![]u8 {
std.log.debug("!! unload {s} !!", .{@typeName(@This())});
if (self.looper) |looper| {
self._unload.set();
looper.join();
self.looper = null;
}
return &.{};
}
pub fn shutdown(_: *Self, _: std.mem.Allocator) void {

View File

@@ -192,7 +192,7 @@ pub fn main() !void {
const eventbuf = try alloc.alloc(u8, 5 * (@sizeOf(linux.inotify_event) + linux.NAME_MAX));
defer alloc.free(eventbuf);
for (0..300) |_| {
for (0..60 * 10) |_| {
const n = linux.poll(&fds, fds.len, std.time.ms_per_s);
if (n > 0) {