libuv: Sem

This commit is contained in:
Mitchell Hashimoto
2022-04-21 15:57:45 -07:00
parent 21ee510471
commit b1f9f68e87
2 changed files with 37 additions and 0 deletions

35
src/libuv/Sem.zig Normal file
View File

@ -0,0 +1,35 @@
//! Semaphores implemented via libuv.
const Sem = @This();
const std = @import("std");
const Allocator = std.mem.Allocator;
const testing = std.testing;
const c = @import("c.zig");
const errors = @import("error.zig");
sem: *c.uv_sem_t,
pub fn init(alloc: Allocator, value: u32) !Sem {
const sem = try alloc.create(c.uv_sem_t);
try errors.convertError(c.uv_sem_init(sem, value));
return Sem{ .sem = sem };
}
pub fn deinit(self: *Sem, alloc: Allocator) void {
c.uv_sem_destroy(self.sem);
alloc.destroy(self.sem);
self.* = undefined;
}
pub fn post(self: Sem) void {
c.uv_sem_post(self.sem);
}
pub fn wait(self: Sem) void {
c.uv_sem_wait(self.sem);
}
test {
var sem = try init(testing.allocator, 0);
defer sem.deinit(testing.allocator);
}

View File

@ -1,7 +1,9 @@
const Loop = @import("Loop.zig"); const Loop = @import("Loop.zig");
const Sem = @import("Sem.zig");
const Error = @import("error.zig").Error; const Error = @import("error.zig").Error;
test { test {
_ = Loop; _ = Loop;
_ = Sem;
_ = Error; _ = Error;
} }