fixed memory bug with thread pools
This commit is contained in:
parent
9c703cf826
commit
0c8cd85647
3 changed files with 36 additions and 20 deletions
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -15,6 +15,12 @@ pub fn SparseSet(comptime T: type) type {
|
|||
};
|
||||
}
|
||||
|
||||
pub fn deinit(self: *@This()) void {
|
||||
self.sparse.deinit();
|
||||
self.dense.deinit();
|
||||
self.components.deinit();
|
||||
}
|
||||
|
||||
pub fn addEntity(self: *@This(), entity: usize, component: T) !void {
|
||||
if (entity >= self.sparse.items.len) {
|
||||
try self.sparse.resize(entity + 10);
|
||||
|
|
|
|||
25
src/main.zig
25
src/main.zig
|
|
@ -15,23 +15,24 @@ pub fn main() !void {
|
|||
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
||||
const allocator = gpa.allocator();
|
||||
{
|
||||
var global_runtime = wasm.GlobalRuntime.init(allocator);
|
||||
defer global_runtime.deinit();
|
||||
try global_runtime.addFunction("debug", wasm.debug);
|
||||
//var global_runtime = wasm.GlobalRuntime.init(allocator);
|
||||
//defer global_runtime.deinit();
|
||||
//try global_runtime.addFunction("debug", wasm.debug);
|
||||
|
||||
const file = try std.fs.cwd().openFile("assets/core.wasm", .{});
|
||||
const module = try Parser.parseWasm(allocator, file.reader());
|
||||
var runtime = try vm.Runtime.init(allocator, module, &global_runtime);
|
||||
defer runtime.deinit(allocator);
|
||||
//const file = try std.fs.cwd().openFile("assets/core.wasm", .{});
|
||||
//const module = try Parser.parseWasm(allocator, file.reader());
|
||||
//var runtime = try vm.Runtime.init(allocator, module, &global_runtime);
|
||||
//defer runtime.deinit(allocator);
|
||||
|
||||
var parameters = [_]usize{};
|
||||
try runtime.callExternal(allocator, "preinit", ¶meters);
|
||||
//var parameters = [_]usize{};
|
||||
//try runtime.callExternal(allocator, "preinit", ¶meters);
|
||||
const w = try window.Window.create(800, 600, "sideros");
|
||||
defer w.destroy();
|
||||
|
||||
//var pool = try entities.Pool.init(allocator);
|
||||
//_ = try pool.createEntity();
|
||||
//try pool.addComponent(entity, components.Speed{ .speed = 0.0 });
|
||||
var pool = try entities.Pool.init(allocator);
|
||||
defer pool.deinit(allocator);
|
||||
const entity = try pool.createEntity();
|
||||
try pool.addComponent(entity, components.Speed{ .speed = 0.0 });
|
||||
|
||||
// TODO(luccie-cmd): Renderer.create shouldn't return an error
|
||||
var r = try Renderer.create(allocator, w);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue