threading tests
This commit is contained in:
38
src/foo.zig
38
src/foo.zig
@@ -5,18 +5,46 @@ const Self = @This();
|
|||||||
|
|
||||||
export const MODULE = core.module(Self);
|
export const MODULE = core.module(Self);
|
||||||
|
|
||||||
|
_unload: std.Thread.ResetEvent = .{},
|
||||||
|
looper: ?std.Thread = null,
|
||||||
|
|
||||||
pub fn startup(_: std.mem.Allocator) !Self {
|
pub fn startup(_: std.mem.Allocator) !Self {
|
||||||
std.log.debug("!! startup {s} !!", .{@typeName(@This())});
|
std.log.debug("!! startup {s} !!", .{@typeName(@This())});
|
||||||
return .{};
|
return .{};
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn unload(_: *Self, _: std.mem.Allocator) ![]u8 {
|
pub fn reload(self: *Self, alloc: std.mem.Allocator, _: []u8) !void {
|
||||||
std.log.debug("!! unload {s} !!", .{@typeName(@This())});
|
std.log.debug("!! reload {s} !!", .{@typeName(@This())});
|
||||||
return &.{};
|
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 {
|
fn _loop(self: *Self) void {
|
||||||
std.log.debug("!! reload {s} !!", .{@typeName(@This())});
|
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 {
|
pub fn shutdown(_: *Self, _: std.mem.Allocator) void {
|
||||||
|
@@ -192,7 +192,7 @@ pub fn main() !void {
|
|||||||
const eventbuf = try alloc.alloc(u8, 5 * (@sizeOf(linux.inotify_event) + linux.NAME_MAX));
|
const eventbuf = try alloc.alloc(u8, 5 * (@sizeOf(linux.inotify_event) + linux.NAME_MAX));
|
||||||
defer alloc.free(eventbuf);
|
defer alloc.free(eventbuf);
|
||||||
|
|
||||||
for (0..300) |_| {
|
for (0..60 * 10) |_| {
|
||||||
const n = linux.poll(&fds, fds.len, std.time.ms_per_s);
|
const n = linux.poll(&fds, fds.len, std.time.ms_per_s);
|
||||||
|
|
||||||
if (n > 0) {
|
if (n > 0) {
|
||||||
|
Reference in New Issue
Block a user