mirror of
https://github.com/ghostty-org/ghostty.git
synced 2025-07-14 15:56:13 +03:00
libuv: Sem
This commit is contained in:
35
src/libuv/Sem.zig
Normal file
35
src/libuv/Sem.zig
Normal 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);
|
||||||
|
}
|
@ -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;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user