mirror of
https://github.com/PixelGuys/Cubyz.git
synced 2025-09-11 05:19:18 -04:00
More random fixes.
Note to self: Zig is cursed https://github.com/ziglang/zig/issues/16311
This commit is contained in:
parent
649eaf30a1
commit
f5220cfe9f
@ -162,7 +162,9 @@ pub fn deinit() void {
|
|||||||
handleError(c.Pa_StopStream(stream));
|
handleError(c.Pa_StopStream(stream));
|
||||||
handleError(c.Pa_CloseStream(stream));
|
handleError(c.Pa_CloseStream(stream));
|
||||||
handleError(c.Pa_Terminate());
|
handleError(c.Pa_Terminate());
|
||||||
|
main.threadPool.closeAllTasksOfType(&MusicLoadTask.vtable);
|
||||||
musicCache.clear();
|
musicCache.clear();
|
||||||
|
activeTasks.deinit(main.globalAllocator);
|
||||||
activeMusicId.len = 0;
|
activeMusicId.len = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -102,7 +102,7 @@ fn setButtonPosFromValue(self: *DiscreteSlider) !void {
|
|||||||
fn updateLabel(self: *DiscreteSlider, newValue: []const u8, width: f32) !void {
|
fn updateLabel(self: *DiscreteSlider, newValue: []const u8, width: f32) !void {
|
||||||
main.globalAllocator.free(self.currentText);
|
main.globalAllocator.free(self.currentText);
|
||||||
self.currentText = try main.globalAllocator.alloc(u8, newValue.len + self.text.len);
|
self.currentText = try main.globalAllocator.alloc(u8, newValue.len + self.text.len);
|
||||||
@memcpy(self.currentText[self.text.len..], self.text);
|
@memcpy(self.currentText[0..self.text.len], self.text);
|
||||||
@memcpy(self.currentText[self.text.len..], newValue);
|
@memcpy(self.currentText[self.text.len..], newValue);
|
||||||
const label = try Label.init(undefined, width - 3*border, self.currentText, .center);
|
const label = try Label.init(undefined, width - 3*border, self.currentText, .center);
|
||||||
self.label.deinit();
|
self.label.deinit();
|
||||||
|
@ -525,17 +525,19 @@ pub const ThreadPool = struct {
|
|||||||
const refreshTime: u32 = 100; // The time after which all priorities get refreshed in milliseconds.
|
const refreshTime: u32 = 100; // The time after which all priorities get refreshed in milliseconds.
|
||||||
|
|
||||||
threads: []std.Thread,
|
threads: []std.Thread,
|
||||||
|
currentTasks: []std.atomic.Atomic(?*const VTable),
|
||||||
loadList: *BlockingMaxHeap(Task),
|
loadList: *BlockingMaxHeap(Task),
|
||||||
allocator: Allocator,
|
allocator: Allocator,
|
||||||
|
|
||||||
pub fn init(allocator: Allocator, threadCount: usize) !ThreadPool {
|
pub fn init(allocator: Allocator, threadCount: usize) !ThreadPool {
|
||||||
var self = ThreadPool {
|
var self = ThreadPool {
|
||||||
.threads = try allocator.alloc(std.Thread, threadCount),
|
.threads = try allocator.alloc(std.Thread, threadCount),
|
||||||
|
.currentTasks = try allocator.alloc(std.atomic.Atomic(?*const VTable), threadCount),
|
||||||
.loadList = try BlockingMaxHeap(Task).init(allocator),
|
.loadList = try BlockingMaxHeap(Task).init(allocator),
|
||||||
.allocator = allocator,
|
.allocator = allocator,
|
||||||
};
|
};
|
||||||
for(self.threads, 0..) |*thread, i| {
|
for(self.threads, 0..) |*thread, i| {
|
||||||
thread.* = try std.Thread.spawn(.{}, run, .{self});
|
thread.* = try std.Thread.spawn(.{}, run, .{self, i});
|
||||||
var buf: [64]u8 = undefined;
|
var buf: [64]u8 = undefined;
|
||||||
try thread.setName(try std.fmt.bufPrint(&buf, "Worker Thread {}", .{i+1}));
|
try thread.setName(try std.fmt.bufPrint(&buf, "Worker Thread {}", .{i+1}));
|
||||||
}
|
}
|
||||||
@ -554,10 +556,32 @@ pub const ThreadPool = struct {
|
|||||||
for(self.threads) |thread| {
|
for(self.threads) |thread| {
|
||||||
thread.join();
|
thread.join();
|
||||||
}
|
}
|
||||||
|
self.allocator.free(self.currentTasks);
|
||||||
self.allocator.free(self.threads);
|
self.allocator.free(self.threads);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn run(self: ThreadPool) !void {
|
pub fn closeAllTasksOfType(self: ThreadPool, vtable: *const VTable) void {
|
||||||
|
self.loadList.mutex.lock();
|
||||||
|
defer self.loadList.mutex.unlock();
|
||||||
|
var i: u32 = 0;
|
||||||
|
while(i < self.loadList.size) {
|
||||||
|
const task = &self.loadList.array[i];
|
||||||
|
if(task.vtable == vtable) {
|
||||||
|
task.vtable.clean(task.self);
|
||||||
|
self.loadList.removeIndex(i);
|
||||||
|
} else {
|
||||||
|
i += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Wait for active tasks:
|
||||||
|
for(self.currentTasks) |*task| {
|
||||||
|
while(task.load(.Monotonic) == vtable) {
|
||||||
|
std.time.sleep(1e6);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn run(self: ThreadPool, id: usize) !void {
|
||||||
// In case any of the tasks wants to allocate memory:
|
// In case any of the tasks wants to allocate memory:
|
||||||
var gpa = std.heap.GeneralPurposeAllocator(.{.thread_safe=false}){};
|
var gpa = std.heap.GeneralPurposeAllocator(.{.thread_safe=false}){};
|
||||||
main.threadAllocator = gpa.allocator();
|
main.threadAllocator = gpa.allocator();
|
||||||
@ -569,7 +593,9 @@ pub const ThreadPool = struct {
|
|||||||
while(true) {
|
while(true) {
|
||||||
{
|
{
|
||||||
var task = self.loadList.extractMax() catch break;
|
var task = self.loadList.extractMax() catch break;
|
||||||
|
self.currentTasks[id].store(task.vtable, .Monotonic);
|
||||||
try task.vtable.run(task.self);
|
try task.vtable.run(task.self);
|
||||||
|
self.currentTasks[id].store(null, .Monotonic);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(std.time.milliTimestamp() -% lastUpdate > refreshTime) {
|
if(std.time.milliTimestamp() -% lastUpdate > refreshTime) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user