-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
397 additions
and
178 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
const std = @import("std"); | ||
|
||
pub const EventSource = struct { | ||
fd: std.posix.fd_t, | ||
|
||
pub inline fn init() !@This() { | ||
return .{ | ||
.fd = try std.posix.eventfd(0, std.os.linux.EFD.CLOEXEC), | ||
}; | ||
} | ||
|
||
pub inline fn deinit(self: *@This()) void { | ||
std.posix.close(self.fd); | ||
self.* = undefined; | ||
} | ||
|
||
pub inline fn notify(self: *@This()) void { | ||
_ = std.posix.write(self.fd, &std.mem.toBytes(@as(u64, 1))) catch unreachable; | ||
} | ||
|
||
pub inline fn wait(self: *@This()) void { | ||
var v: u64 = undefined; | ||
_ = std.posix.read(self.fd, std.mem.asBytes(&v)) catch unreachable; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
const std = @import("std"); | ||
|
||
pub fn Pool(T: type, SZ: type) type { | ||
return struct { | ||
pub const Node = union(enum) { free: ?SZ, used: T }; | ||
nodes: []Node, | ||
free: ?SZ = null, | ||
num_free: SZ = 0, | ||
num_used: SZ = 0, | ||
|
||
pub const Error = error{ | ||
OutOfMemory, | ||
}; | ||
|
||
pub fn init(allocator: std.mem.Allocator, n: SZ) Error!@This() { | ||
return .{ .nodes = try allocator.alloc(Node, n) }; | ||
} | ||
|
||
pub fn deinit(self: *@This(), allocator: std.mem.Allocator) void { | ||
allocator.free(self.nodes); | ||
self.* = undefined; | ||
} | ||
|
||
pub fn empty(self: *@This()) bool { | ||
return self.num_used == self.num_free; | ||
} | ||
|
||
pub fn next(self: *@This()) ?SZ { | ||
if (self.free) |fslot| return fslot; | ||
if (self.num_used >= self.nodes.len) return null; | ||
return self.num_used; | ||
} | ||
|
||
pub fn add(self: *@This(), item: T) Error!SZ { | ||
if (self.free) |fslot| { | ||
self.free = self.nodes[fslot].free; | ||
self.nodes[fslot] = .{ .used = item }; | ||
self.num_free -= 1; | ||
return fslot; | ||
} | ||
if (self.num_used >= self.nodes.len) return error.OutOfMemory; | ||
self.nodes[self.num_used] = .{ .used = item }; | ||
defer self.num_used += 1; | ||
return self.num_used; | ||
} | ||
|
||
pub fn remove(self: *@This(), slot: SZ) void { | ||
if (self.free) |fslot| { | ||
self.nodes[slot] = .{ .free = fslot }; | ||
} else { | ||
self.nodes[slot] = .{ .free = null }; | ||
} | ||
self.free = slot; | ||
self.num_free += 1; | ||
} | ||
|
||
pub fn get(self: *@This(), slot: SZ) *T { | ||
return &self.nodes[slot].used; | ||
} | ||
|
||
pub const Iterator = struct { | ||
items: []Node, | ||
index: SZ = 0, | ||
|
||
pub fn next(self: *@This()) *T { | ||
while (self.index < self.items.len) { | ||
defer self.index += 1; | ||
if (self.items[self.index] == .used) { | ||
return &self.items[self.index].used; | ||
} | ||
} | ||
return null; | ||
} | ||
}; | ||
|
||
pub fn iterator(self: *@This()) Iterator { | ||
return .{ .items = self.nodes[0..self.num_used] }; | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
const std = @import("std"); | ||
|
||
pub const RENAME_NOREPLACE = 1 << 0; | ||
|
||
pub fn convertOpenFlags(flags: std.fs.File.OpenFlags) std.posix.O { | ||
var os_flags: std.posix.O = .{ | ||
.ACCMODE = switch (flags.mode) { | ||
.read_only => .RDONLY, | ||
.write_only => .WRONLY, | ||
.read_write => .RDWR, | ||
}, | ||
}; | ||
if (@hasField(std.posix.O, "CLOEXEC")) os_flags.CLOEXEC = true; | ||
if (@hasField(std.posix.O, "LARGEFILE")) os_flags.LARGEFILE = true; | ||
if (@hasField(std.posix.O, "NOCTTY")) os_flags.NOCTTY = !flags.allow_ctty; | ||
|
||
// Use the O locking flags if the os supports them to acquire the lock | ||
// atomically. | ||
const has_flock_open_flags = @hasField(std.posix.O, "EXLOCK"); | ||
if (has_flock_open_flags) { | ||
// Note that the NONBLOCK flag is removed after the openat() call | ||
// is successful. | ||
switch (flags.lock) { | ||
.none => {}, | ||
.shared => { | ||
os_flags.SHLOCK = true; | ||
os_flags.NONBLOCK = flags.lock_nonblocking; | ||
}, | ||
.exclusive => { | ||
os_flags.EXLOCK = true; | ||
os_flags.NONBLOCK = flags.lock_nonblocking; | ||
}, | ||
} | ||
} | ||
return os_flags; | ||
} | ||
|
||
pub fn statusToTerm(status: u32) std.process.Child.Term { | ||
return if (std.posix.W.IFEXITED(status)) | ||
.{ .Exited = std.posix.W.EXITSTATUS(status) } | ||
else if (std.posix.W.IFSIGNALED(status)) | ||
.{ .Signal = std.posix.W.TERMSIG(status) } | ||
else if (std.posix.W.IFSTOPPED(status)) | ||
.{ .Stopped = std.posix.W.STOPSIG(status) } | ||
else | ||
.{ .Unknown = status }; | ||
} |
Oops, something went wrong.