diff options
author | Lorenzo Torres <torres@sideros.org> | 2025-03-22 17:29:57 +0100 |
---|---|---|
committer | Lorenzo Torres <torres@sideros.org> | 2025-03-22 17:29:57 +0100 |
commit | 0c8cd856474f9646a12339c21475d449343832f7 (patch) | |
tree | 00334ad274e21cab26cdf73fc914d54b1ef9e407 /src/ecs/entities.zig | |
parent | 9c703cf8268ce8987aa4cf5c41e1905ecdf8ecff (diff) |
fixed memory bug with thread pools
Diffstat (limited to 'src/ecs/entities.zig')
-rw-r--r-- | src/ecs/entities.zig | 25 |
1 files changed, 17 insertions, 8 deletions
diff --git a/src/ecs/entities.zig b/src/ecs/entities.zig index d8b0ea4..d153c6c 100644 --- a/src/ecs/entities.zig +++ b/src/ecs/entities.zig @@ -3,24 +3,22 @@ const Allocator = std.mem.Allocator; const components = @import("components.zig"); const sparse = @import("sparse.zig"); -const System = *const fn (Pool) void; +const System = *const fn (*Pool) void; const SystemGroup = std.ArrayList(System); -//FIXME: for some reason this thing has very weird issues with -//hash maps pub const Pool = struct { // Components position: sparse.SparseSet(components.Position), speed: sparse.SparseSet(components.Speed), system_groups: std.ArrayList(SystemGroup), - thread_pool: std.Thread.Pool, + thread_pool: *std.Thread.Pool, wait_group: std.Thread.WaitGroup, mutex: std.Thread.Mutex, last_entity: usize, free_ids: std.ArrayList(usize), - component_flags: std.AutoHashMap(u32, usize), + component_flags: std.AutoHashMap(usize, usize), pub fn init(allocator: Allocator) !@This() { var pool = @This(){ @@ -28,12 +26,12 @@ pub const Pool = struct { .speed = sparse.SparseSet(components.Speed).init(allocator), .system_groups = std.ArrayList(SystemGroup).init(allocator), - .thread_pool = undefined, + .thread_pool = try allocator.create(std.Thread.Pool), .wait_group = .{}, .mutex = .{}, .last_entity = 0, .free_ids = std.ArrayList(usize).init(allocator), - .component_flags = std.AutoHashMap(u32, usize).init(allocator), + .component_flags = std.AutoHashMap(usize, usize).init(allocator), }; try pool.thread_pool.init(.{ @@ -44,6 +42,17 @@ pub const Pool = struct { return pool; } + pub fn deinit(self: *@This(), allocator: Allocator) void { + self.position.deinit(); + self.speed.deinit(); + + self.system_groups.deinit(); + self.thread_pool.deinit(); + allocator.destroy(self.thread_pool); + self.free_ids.deinit(); + self.component_flags.deinit(); + } + pub fn tick(self: *@This()) void { for (self.system_groups) |group| { self.thread_pool.spawnWg(&self.wait_group, struct { @@ -60,7 +69,7 @@ pub const Pool = struct { pub fn createEntity(self: *@This()) !usize { const id = self.free_ids.pop() orelse self.last_entity; self.last_entity += 1; - try self.component_flags.put(2, 0x2); + try self.component_flags.put(id, 0x0); return id; } |