Skip to content

Commit

Permalink
Fully implement the ThreadManager.
Browse files Browse the repository at this point in the history
  • Loading branch information
Makosai committed Aug 23, 2024
1 parent e836e31 commit e6f80df
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 20 deletions.
59 changes: 46 additions & 13 deletions src/Transport/ThreadManager.zig
Original file line number Diff line number Diff line change
@@ -1,36 +1,69 @@
const std = @import("std");
const sustenet = @import("root").sustenet;

const RwLock = std.Thread.RwLock;
const ArrayList = std.ArrayList;
const Action = sustenet.utils.Extensions.Action;

pub const ThreadManager = @This();
const ThreadManager = @This();
var instance: ?ThreadManager = null;

mainPool: ArrayList(Action),
mainPoolCopied: ArrayList(Action),
executeAction: bool = false,
main_pool_lock: RwLock = .{},
main_pool: ArrayList(Action),
main_pool_copied: ArrayList(Action),
execute_action: bool = false,

/// Get the singleton instance.
pub fn getInstance(allocator: std.mem.Allocator) !ThreadManager {
if (instance == null) {
instance = ThreadManager{
.main_pool = ArrayList(Action).init(allocator),
.main_pool_copied = ArrayList(Action).init(allocator),
.execute_action = false,
};
}

return instance.?;
}

//#region Execution Functions
/// Sets an action to be executed on the main thread.
pub fn executeOnMainThread(
self: *ThreadManager,
/// The action to be executed on the main thread.
action: Action,
) void {
action.invoke();
self.main_pool_lock.lock();
defer self.main_pool_lock.unlock();

self.main_pool.append(action) catch unreachable;
self.execute_action = true;
}

/// Execute all code meant to run on the main thread. Should only be called from the main thread.
pub fn updateMain(self: ThreadManager) void {
if (self.executeAction) {
self.mainPoolCopied.clearAndFree();
// RWLock mainPool
pub fn updateMain(self: *ThreadManager) void {
if (self.execute_action) {
self.main_pool_lock.lock();
defer self.main_pool_lock.unlock();

self.main_pool_copied.clearAndFree();
{
self.mainPoolCopied.appendSlice(self.mainPool);
self.mainPool.clear();
self.executeAction = false;
const main_pool_slice = self.main_pool.toOwnedSlice() catch unreachable;
self.main_pool_copied.appendSlice(main_pool_slice) catch unreachable;
self.execute_action = false;
}

for (self.mainPoolCopied.items()) |action| {
for (self.main_pool_copied.items) |action| {
action.invoke();
}
}
}
//#endregion

//#region Memory Functions
pub fn deinit(self: *ThreadManager) void {
self.main_pool.deinit();
self.main_pool_copied.deinit();
instance = null;
}
//#endregion
4 changes: 3 additions & 1 deletion src/Utils/Constants.zig
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
const std = @import("std");

pub const VERSION = "0.1.0";

pub const DEBUGGING = false;

/// How many ticks are in a second.
pub const TICK_RATE: i32 = 30;
pub const MS_PER_TICK = 1000 / TICK_RATE;
pub const MS_PER_TICK = std.time.ms_per_s / TICK_RATE;

pub const DEFAULT_IP = "127.0.0.1";
pub const MASTER_PORT: i16 = 6256;
Expand Down
26 changes: 20 additions & 6 deletions src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ const ArrayList = std.ArrayList;
const transport = sustenet.transport;
const clients = sustenet.clients;

const ThreadManager = transport.ThreadManager;
const Constants = sustenet.utils.Constants;
const BaseServer = transport.BaseServer;

Expand Down Expand Up @@ -86,7 +85,6 @@ pub fn main() !void {
max_clients,
Constants.TERMINAL_DEFAULT,
});
return;
} else if (eql(u8, arg, "cluster") or eql(u8, arg, "cs")) {
return;
} else if (eql(u8, arg, "master") or eql(u8, arg, "ms")) {
Expand All @@ -96,22 +94,38 @@ pub fn main() !void {
return;
}
}

is_running = true;
defer is_running = false;

var logic_thread = try std.Thread.spawn(.{}, updateMain, .{allocator});
logic_thread.setName("Logic Thread") catch |err| {
print("Error setting thread name: {}\n", .{err});
};
logic_thread.detach();

var buffer: [1]u8 = undefined;
print("Press Enter to close Sustenet...", .{});
_ = try std.io.getStdIn().reader().read(buffer[0..1]);
}

fn updateMain() void {
fn updateMain(allocator: std.mem.Allocator) void {
var next = std.time.milliTimestamp();
var ThreadManager = try transport.ThreadManager.getInstance(allocator);

while (is_running) {
const now = std.time.milliTimestamp();
while (next < now) {
ThreadManager.updateMain();
// ThreadManager.updateMain();
next += Constants.MS_PER_TICK;

if (next > std.time.milliTimestamp()) {
std.time.sleep(next - std.time.milliTimestamp());
if (next > now) {
std.time.sleep(@as(u64, @intCast(next - now)) * std.time.ns_per_ms);
}
}
print("Tick\n", .{});
}
ThreadManager.deinit();
}

//#region Tests
Expand Down

0 comments on commit e6f80df

Please sign in to comment.