From ef0787ec42fe421621309b36915f3eb3391933ee Mon Sep 17 00:00:00 2001 From: Rylee Alanza Lyman <46907231+ryleelyman@users.noreply.github.com> Date: Sat, 23 Sep 2023 15:38:59 -0400 Subject: [PATCH] fix: pthread differs on linux vs macos (#65) --- src/clock.zig | 6 +----- src/main.zig | 6 +----- src/metros.zig | 6 +----- src/pthread.zig | 17 ++++++++++++++++- 4 files changed, 19 insertions(+), 16 deletions(-) diff --git a/src/clock.zig b/src/clock.zig index 9bb941c..3a47a42 100644 --- a/src/clock.zig +++ b/src/clock.zig @@ -37,11 +37,7 @@ const Fabric = struct { allocator.destroy(self); } fn loop(self: *Fabric) void { - const priority: pthread.sched_param = .{ - .sched_priority = 90, - .__opaque = undefined, - }; - _ = pthread.pthread_setschedparam(pthread.pthread_self(), pthread.SCHED_FIFO, &priority); + pthread.set_priority(90); while (!self.quit) { self.do_tick(); self.time += self.tick; diff --git a/src/main.zig b/src/main.zig index f9d04fa..84d4358 100644 --- a/src/main.zig +++ b/src/main.zig @@ -49,11 +49,7 @@ pub fn main() !void { defer if (option) |_| create.deinit(); while (go_again) { try print_version(); - const priority: pthread.sched_param = .{ - .sched_priority = 99, - .__opaque = undefined, - }; - _ = pthread.pthread_setschedparam(pthread.pthread_self(), pthread.SCHED_FIFO, &priority); + pthread.set_priority(99); const path = try std.fs.path.join(allocator, &.{ location, "..", "share", "seamstress", "lua" }); defer allocator.free(path); diff --git a/src/metros.zig b/src/metros.zig index d2c0bd7..cc0fec8 100644 --- a/src/metros.zig +++ b/src/metros.zig @@ -132,11 +132,7 @@ fn loop(self: *Metro, pid: *Thread) void { self.status_lock.lock(); self.status = Status.Running; self.status_lock.unlock(); - const priority: pthread.sched_param = .{ - .sched_priority = 90, - .__opaque = undefined, - }; - _ = pthread.pthread_setschedparam(pthread.pthread_self(), pthread.SCHED_FIFO, &priority); + pthread.set_priority(90); while (!pid.quit) { self.wait(); self.stage_lock.lock(); diff --git a/src/pthread.zig b/src/pthread.zig index 1964485..76932ad 100644 --- a/src/pthread.zig +++ b/src/pthread.zig @@ -1 +1,16 @@ -pub usingnamespace @cImport(@cInclude("pthread.h")); +const c = @cImport(@cInclude("pthread.h")); +const builtin = @import("builtin"); + +pub fn set_priority(priority: u8) void { + const priority_struct: c.sched_param = switch (comptime builtin.os.tag) { + .linux => .{ + .sched_priority = priority, + }, + .macos => .{ + .sched_priority = priority, + .__opaque = undefined, + }, + else => return, + }; + _ = c.pthread_setschedparam(c.pthread_self(), c.SCHED_FIFO, &priority_struct); +}