aboutsummaryrefslogtreecommitdiff
path: root/src/io/io_linux.zig
diff options
context:
space:
mode:
authorGravatar Jarred Sumner <jarred@jarredsumner.com> 2022-08-28 21:28:05 -0700
committerGravatar GitHub <noreply@github.com> 2022-08-28 21:28:05 -0700
commitc1734c6ec5ef709ee4126b3474c7bee0a377a1fa (patch)
tree097710a13a1d85228efadf6d57823bb3a4f1c011 /src/io/io_linux.zig
parentb2141a204fbc351a40467037138168aea23a6930 (diff)
downloadbun-c1734c6ec5ef709ee4126b3474c7bee0a377a1fa.tar.gz
bun-c1734c6ec5ef709ee4126b3474c7bee0a377a1fa.tar.zst
bun-c1734c6ec5ef709ee4126b3474c7bee0a377a1fa.zip
More reliable macOS event loop (#1166)
* More reliable macOS event loop * Reduce CPU usage of idling * Add another implementation * Add benchmark Co-authored-by: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com>
Diffstat (limited to 'src/io/io_linux.zig')
-rw-r--r--src/io/io_linux.zig28
1 files changed, 26 insertions, 2 deletions
diff --git a/src/io/io_linux.zig b/src/io/io_linux.zig
index dfad7e795..af5382053 100644
--- a/src/io/io_linux.zig
+++ b/src/io/io_linux.zig
@@ -498,7 +498,7 @@ pub fn wait(this: *@This(), ptr: anytype, comptime onReady: anytype) void {
_ = this.ring.enter(submitted, 1, linux.IORING_ENTER_GETEVENTS) catch 0;
}
-pub fn init(entries_: u12, flags: u32, event_fd: os.fd_t) !IO {
+pub fn init(entries_: u12, flags: u32, waker: Waker) !IO {
var ring: IO_Uring = undefined;
var entries = entries_;
@@ -541,7 +541,7 @@ pub fn init(entries_: u12, flags: u32, event_fd: os.fd_t) !IO {
break;
}
- return IO{ .ring = ring, .event_fd = event_fd };
+ return IO{ .ring = ring, .event_fd = waker.fd };
}
pub fn deinit(self: *IO) void {
@@ -984,6 +984,30 @@ pub const Completion = struct {
}
};
+pub const Waker = struct {
+ fd: os.fd_t,
+
+ pub fn init(_: std.mem.Allocator) !Waker {
+ return Waker{
+ .fd = try os.eventfd(0, 0),
+ };
+ }
+
+ pub fn wait(this: Waker) !u64 {
+ var bytes: usize = 0;
+ _ = std.os.read(this.fd, @ptrCast(*[8]u8, &bytes)) catch 0;
+ return @intCast(u64, bytes);
+ }
+
+ pub fn wake(this: Waker) !void {
+ var bytes: usize = 1;
+ _ = std.os.write(
+ this.fd,
+ @ptrCast(*[8]u8, &bytes),
+ ) catch 0;
+ }
+};
+
/// This union encodes the set of operations supported as well as their arguments.
const Operation = union(enum) {
accept: struct {