mirror of
https://github.com/ghostty-org/ghostty.git
synced 2025-07-14 15:56:13 +03:00
libuv: add Cond, Mutex
This commit is contained in:
39
src/libuv/Cond.zig
Normal file
39
src/libuv/Cond.zig
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
//! Condition variables implemented via libuv.
|
||||||
|
const Cond = @This();
|
||||||
|
|
||||||
|
const std = @import("std");
|
||||||
|
const Allocator = std.mem.Allocator;
|
||||||
|
const testing = std.testing;
|
||||||
|
const c = @import("c.zig");
|
||||||
|
const errors = @import("error.zig");
|
||||||
|
|
||||||
|
cond: *c.uv_cond_t,
|
||||||
|
|
||||||
|
pub fn init(alloc: Allocator) !Cond {
|
||||||
|
const cond = try alloc.create(c.uv_cond_t);
|
||||||
|
try errors.convertError(c.uv_cond_init(cond));
|
||||||
|
return Cond{ .cond = cond };
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn deinit(self: *Cond, alloc: Allocator) void {
|
||||||
|
c.uv_cond_destroy(self.cond);
|
||||||
|
alloc.destroy(self.cond);
|
||||||
|
self.* = undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn signal(self: Cond) void {
|
||||||
|
c.uv_cond_signal(self.cond);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn broadcast(self: Cond) void {
|
||||||
|
c.uv_cond_broadcast(self.cond);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn wait(self: Cond) void {
|
||||||
|
c.uv_cond_wait(self.cond);
|
||||||
|
}
|
||||||
|
|
||||||
|
test {
|
||||||
|
var cond = try init(testing.allocator);
|
||||||
|
defer cond.deinit(testing.allocator);
|
||||||
|
}
|
35
src/libuv/Mutex.zig
Normal file
35
src/libuv/Mutex.zig
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
//! Mutexes implemented via libuv.
|
||||||
|
const Mutex = @This();
|
||||||
|
|
||||||
|
const std = @import("std");
|
||||||
|
const Allocator = std.mem.Allocator;
|
||||||
|
const testing = std.testing;
|
||||||
|
const c = @import("c.zig");
|
||||||
|
const errors = @import("error.zig");
|
||||||
|
|
||||||
|
mutex: *c.uv_mutex_t,
|
||||||
|
|
||||||
|
pub fn init(alloc: Allocator) !Mutex {
|
||||||
|
const mutex = try alloc.create(c.uv_mutex_t);
|
||||||
|
try errors.convertError(c.uv_mutex_init(mutex));
|
||||||
|
return Mutex{ .mutex = mutex };
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn deinit(self: *Mutex, alloc: Allocator) void {
|
||||||
|
c.uv_mutex_destroy(self.mutex);
|
||||||
|
alloc.destroy(self.mutex);
|
||||||
|
self.* = undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn lock(self: Mutex) void {
|
||||||
|
c.uv_mutex_lock(self.mutex);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn unlock(self: Mutex) void {
|
||||||
|
c.uv_mutex_unlock(self.mutex);
|
||||||
|
}
|
||||||
|
|
||||||
|
test {
|
||||||
|
var mutex = try init(testing.allocator);
|
||||||
|
defer mutex.deinit(testing.allocator);
|
||||||
|
}
|
@ -5,6 +5,8 @@ pub const Async = @import("Async.zig");
|
|||||||
pub const Pipe = @import("Pipe.zig");
|
pub const Pipe = @import("Pipe.zig");
|
||||||
pub const Timer = @import("Timer.zig");
|
pub const Timer = @import("Timer.zig");
|
||||||
pub const Tty = @import("Tty.zig");
|
pub const Tty = @import("Tty.zig");
|
||||||
|
pub const Cond = @import("Cond.zig");
|
||||||
|
pub const Mutex = @import("Mutex.zig");
|
||||||
pub const Sem = @import("Sem.zig");
|
pub const Sem = @import("Sem.zig");
|
||||||
pub const Thread = @import("Thread.zig");
|
pub const Thread = @import("Thread.zig");
|
||||||
pub const WriteReq = stream.WriteReq;
|
pub const WriteReq = stream.WriteReq;
|
||||||
@ -22,6 +24,8 @@ test {
|
|||||||
_ = Pipe;
|
_ = Pipe;
|
||||||
_ = Timer;
|
_ = Timer;
|
||||||
_ = Tty;
|
_ = Tty;
|
||||||
|
_ = Cond;
|
||||||
|
_ = Mutex;
|
||||||
_ = Sem;
|
_ = Sem;
|
||||||
_ = Thread;
|
_ = Thread;
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user