Initialize the seed in every thread, also added a helper function for threadlocal (de-)initialization.

fixes #1248
This commit is contained in:
IntegratedQuantum 2025-03-27 20:09:09 +01:00
parent dc902b3909
commit a82ed65dfb
6 changed files with 23 additions and 20 deletions

View File

@ -35,9 +35,8 @@ fn discoverIpAddress() void {
}
fn discoverIpAddressFromNewThread() void {
var sta = main.heap.StackAllocator.init(main.globalAllocator, 1 << 23);
defer sta.deinit();
main.stackAllocator = sta.allocator();
main.initThreadLocals();
defer main.deinitThreadLocals();
discoverIpAddress();
}

View File

@ -39,9 +39,8 @@ fn discoverIpAddress() void {
}
fn discoverIpAddressFromNewThread() void {
var sta = main.heap.StackAllocator.init(main.globalAllocator, 1 << 23);
defer sta.deinit();
main.stackAllocator = sta.allocator();
main.initThreadLocals();
defer main.deinitThreadLocals();
discoverIpAddress();
}

View File

@ -42,11 +42,22 @@ const Vec3d = vec.Vec3d;
pub threadlocal var stackAllocator: heap.NeverFailingAllocator = undefined;
pub threadlocal var seed: u64 = undefined;
threadlocal var stackAllocatorBase: heap.StackAllocator = undefined;
var global_gpa = std.heap.GeneralPurposeAllocator(.{.thread_safe = true}){};
var handled_gpa = heap.ErrorHandlingAllocator.init(global_gpa.allocator());
pub const globalAllocator: heap.NeverFailingAllocator = handled_gpa.allocator();
pub var threadPool: *utils.ThreadPool = undefined;
pub fn initThreadLocals() void {
seed = @bitCast(@as(i64, @truncate(std.time.nanoTimestamp())));
stackAllocatorBase = heap.StackAllocator.init(globalAllocator, 1 << 23);
stackAllocator = stackAllocatorBase.allocator();
}
pub fn deinitThreadLocals() void {
stackAllocatorBase.deinit();
}
fn cacheStringImpl(comptime len: usize, comptime str: [len]u8) []const u8 {
return str[0..len];
}
@ -522,13 +533,11 @@ pub fn convertJsonToZon(jsonPath: []const u8) void { // TODO: Remove after #480
}
pub fn main() void { // MARK: main()
seed = @bitCast(std.time.milliTimestamp());
defer if(global_gpa.deinit() == .leak) {
std.log.err("Memory leak", .{});
};
var sta = heap.StackAllocator.init(globalAllocator, 1 << 23);
defer sta.deinit();
stackAllocator = sta.allocator();
initThreadLocals();
defer deinitThreadLocals();
initLogging();
defer deinitLogging();

View File

@ -559,9 +559,8 @@ pub const ConnectionManager = struct { // MARK: ConnectionManager
pub fn run(self: *ConnectionManager) void {
self.threadId = std.Thread.getCurrentId();
var sta = main.heap.StackAllocator.init(main.globalAllocator, 1 << 23);
defer sta.deinit();
main.stackAllocator = sta.allocator();
main.initThreadLocals();
defer main.deinitThreadLocals();
var lastTime: i64 = @truncate(std.time.nanoTimestamp());
while(self.running.load(.monotonic)) {

View File

@ -432,9 +432,8 @@ fn update() void { // MARK: update()
}
pub fn start(name: []const u8, port: ?u16) void {
var sta = main.heap.StackAllocator.init(main.globalAllocator, 1 << 23);
defer sta.deinit();
main.stackAllocator = sta.allocator();
main.initThreadLocals();
defer main.deinitThreadLocals();
std.debug.assert(!running.load(.monotonic)); // There can only be one server.
init(name, port);
defer deinit();

View File

@ -716,10 +716,8 @@ pub const ThreadPool = struct { // MARK: ThreadPool
}
fn run(self: *ThreadPool, id: usize) void {
// In case any of the tasks wants to allocate memory:
var sta = main.heap.StackAllocator.init(main.globalAllocator, 1 << 23);
defer sta.deinit();
main.stackAllocator = sta.allocator();
main.initThreadLocals();
defer main.deinitThreadLocals();
var lastUpdate = std.time.milliTimestamp();
while(true) {