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 { fn discoverIpAddressFromNewThread() void {
var sta = main.heap.StackAllocator.init(main.globalAllocator, 1 << 23); main.initThreadLocals();
defer sta.deinit(); defer main.deinitThreadLocals();
main.stackAllocator = sta.allocator();
discoverIpAddress(); discoverIpAddress();
} }

View File

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

View File

@ -42,11 +42,22 @@ const Vec3d = vec.Vec3d;
pub threadlocal var stackAllocator: heap.NeverFailingAllocator = undefined; pub threadlocal var stackAllocator: heap.NeverFailingAllocator = undefined;
pub threadlocal var seed: u64 = undefined; pub threadlocal var seed: u64 = undefined;
threadlocal var stackAllocatorBase: heap.StackAllocator = undefined;
var global_gpa = std.heap.GeneralPurposeAllocator(.{.thread_safe = true}){}; var global_gpa = std.heap.GeneralPurposeAllocator(.{.thread_safe = true}){};
var handled_gpa = heap.ErrorHandlingAllocator.init(global_gpa.allocator()); var handled_gpa = heap.ErrorHandlingAllocator.init(global_gpa.allocator());
pub const globalAllocator: heap.NeverFailingAllocator = handled_gpa.allocator(); pub const globalAllocator: heap.NeverFailingAllocator = handled_gpa.allocator();
pub var threadPool: *utils.ThreadPool = undefined; 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 { fn cacheStringImpl(comptime len: usize, comptime str: [len]u8) []const u8 {
return str[0..len]; 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() pub fn main() void { // MARK: main()
seed = @bitCast(std.time.milliTimestamp());
defer if(global_gpa.deinit() == .leak) { defer if(global_gpa.deinit() == .leak) {
std.log.err("Memory leak", .{}); std.log.err("Memory leak", .{});
}; };
var sta = heap.StackAllocator.init(globalAllocator, 1 << 23); initThreadLocals();
defer sta.deinit(); defer deinitThreadLocals();
stackAllocator = sta.allocator();
initLogging(); initLogging();
defer deinitLogging(); defer deinitLogging();

View File

@ -559,9 +559,8 @@ pub const ConnectionManager = struct { // MARK: ConnectionManager
pub fn run(self: *ConnectionManager) void { pub fn run(self: *ConnectionManager) void {
self.threadId = std.Thread.getCurrentId(); self.threadId = std.Thread.getCurrentId();
var sta = main.heap.StackAllocator.init(main.globalAllocator, 1 << 23); main.initThreadLocals();
defer sta.deinit(); defer main.deinitThreadLocals();
main.stackAllocator = sta.allocator();
var lastTime: i64 = @truncate(std.time.nanoTimestamp()); var lastTime: i64 = @truncate(std.time.nanoTimestamp());
while(self.running.load(.monotonic)) { 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 { pub fn start(name: []const u8, port: ?u16) void {
var sta = main.heap.StackAllocator.init(main.globalAllocator, 1 << 23); main.initThreadLocals();
defer sta.deinit(); defer main.deinitThreadLocals();
main.stackAllocator = sta.allocator();
std.debug.assert(!running.load(.monotonic)); // There can only be one server. std.debug.assert(!running.load(.monotonic)); // There can only be one server.
init(name, port); init(name, port);
defer deinit(); defer deinit();

View File

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