Support idling in file system message processor
continuous-integration/drone/push Build is passing Details

This commit is contained in:
kayomn 2022-10-01 21:12:12 +01:00
parent 52bc326187
commit 584f892e4d
1 changed files with 108 additions and 60 deletions

View File

@ -19,6 +19,8 @@ pub const EventLoop = opaque {
frame: anyframe, frame: anyframe,
request: union(enum) { request: union(enum) {
exit,
close: struct { close: struct {
file_access: *FileAccess, file_access: *FileAccess,
}, },
@ -55,6 +57,7 @@ pub const EventLoop = opaque {
const Implementation = struct { const Implementation = struct {
user_prefix: []const u8, user_prefix: []const u8,
file_system_semaphore: *ext.SDL_sem, file_system_semaphore: *ext.SDL_sem,
file_system_mutex: *ext.SDL_mutex,
file_system_thread: *ext.SDL_Thread, file_system_thread: *ext.SDL_Thread,
file_system_messages: ?*FileSystemMessage = null, file_system_messages: ?*FileSystemMessage = null,
@ -107,13 +110,18 @@ pub const EventLoop = opaque {
pub fn enqueueFileSystemMessage(event_loop: *EventLoop, message: *FileSystemMessage) void { pub fn enqueueFileSystemMessage(event_loop: *EventLoop, message: *FileSystemMessage) void {
const implementation = Implementation.cast(event_loop); const implementation = Implementation.cast(event_loop);
// TODO: Error check this.
_ = ext.SDL_LockMutex(implementation.file_system_mutex);
if (implementation.file_system_messages) |messages| { if (implementation.file_system_messages) |messages| {
messages.next = message; messages.next = message;
} else { } else {
implementation.file_system_messages = message; implementation.file_system_messages = message;
} }
// TODO: Post message to FS thread to perform task. // TODO: Error check these.
_ = ext.SDL_UnlockMutex(implementation.file_system_mutex);
_ = ext.SDL_SemPost(implementation.file_system_semaphore);
} }
/// ///
@ -153,8 +161,11 @@ pub const EventLoop = opaque {
fn processFileSystemMessages(data: ?*anyopaque) callconv(.C) c_int { fn processFileSystemMessages(data: ?*anyopaque) callconv(.C) c_int {
const implementation = Implementation.cast(@ptrCast(*EventLoop, data orelse unreachable)); const implementation = Implementation.cast(@ptrCast(*EventLoop, data orelse unreachable));
while (true) {
while (implementation.file_system_messages) |messages| { while (implementation.file_system_messages) |messages| {
switch (messages.request) { switch (messages.request) {
.exit => return 0,
.open => |*open_request| { .open => |*open_request| {
switch (open_request.path.file_system) { switch (open_request.path.file_system) {
.data => { .data => {
@ -215,7 +226,9 @@ pub const EventLoop = opaque {
implementation.file_system_messages = messages.next; implementation.file_system_messages = messages.next;
} }
return 0; // TODO: Error check this.
_ = ext.SDL_SemWait(implementation.file_system_semaphore);
}
} }
/// ///
@ -279,7 +292,7 @@ pub const EventLoop = opaque {
}; };
/// ///
/// /// File-system agnostic abstraction for manipulating a file.
/// ///
pub const FileAccess = opaque { pub const FileAccess = opaque {
/// ///
@ -449,7 +462,7 @@ pub fn GraphicsRunner(comptime Errors: type) type {
/// ///
pub fn runGraphics(comptime Errors: anytype, run: GraphicsRunner(Errors)) Errors!void { pub fn runGraphics(comptime Errors: anytype, run: GraphicsRunner(Errors)) Errors!void {
if (ext.SDL_Init(ext.SDL_INIT_EVERYTHING) != 0) { if (ext.SDL_Init(ext.SDL_INIT_EVERYTHING) != 0) {
ext.SDL_LogCritical(ext.SDL_LOG_CATEGORY_APPLICATION, "Failed to initialize SDL2 runtime"); ext.SDL_LogCritical(ext.SDL_LOG_CATEGORY_APPLICATION, "Failed to initialize runtime");
return error.InitFailure; return error.InitFailure;
} }
@ -473,7 +486,7 @@ pub fn runGraphics(comptime Errors: anytype, run: GraphicsRunner(Errors)) Errors
var flags = @as(u32, 0); var flags = @as(u32, 0);
break: create_window ext.SDL_CreateWindow("Ona", pos, pos, 640, 480, flags) orelse { break: create_window ext.SDL_CreateWindow("Ona", pos, pos, 640, 480, flags) orelse {
ext.SDL_LogCritical(ext.SDL_LOG_CATEGORY_APPLICATION, "Failed to load SDL2 window"); ext.SDL_LogCritical(ext.SDL_LOG_CATEGORY_APPLICATION, "Failed to create window");
return error.InitFailure; return error.InitFailure;
}; };
@ -485,7 +498,7 @@ pub fn runGraphics(comptime Errors: anytype, run: GraphicsRunner(Errors)) Errors
var flags = @as(u32, 0); var flags = @as(u32, 0);
break: create_renderer ext.SDL_CreateRenderer(window, -1, flags) orelse { break: create_renderer ext.SDL_CreateRenderer(window, -1, flags) orelse {
ext.SDL_LogCritical(ext.SDL_LOG_CATEGORY_APPLICATION, "Failed to load SDL2 renderer"); ext.SDL_LogCritical(ext.SDL_LOG_CATEGORY_APPLICATION, "Failed to create renderer");
return error.InitFailure; return error.InitFailure;
}; };
@ -494,16 +507,36 @@ pub fn runGraphics(comptime Errors: anytype, run: GraphicsRunner(Errors)) Errors
defer ext.SDL_DestroyRenderer(renderer); defer ext.SDL_DestroyRenderer(renderer);
var event_loop = EventLoop.Implementation{ var event_loop = EventLoop.Implementation{
.file_system_semaphore = ext.SDL_CreateSemaphore(0) orelse return error.InitFailure, .file_system_semaphore = ext.SDL_CreateSemaphore(0) orelse {
ext.SDL_LogCritical(ext.SDL_LOG_CATEGORY_APPLICATION,
"Failed to create file-system work scheduler");
return error.InitFailure;
},
.file_system_mutex = ext.SDL_CreateMutex() orelse {
ext.SDL_LogCritical(ext.SDL_LOG_CATEGORY_APPLICATION,
"Failed to create file-system work lock");
return error.InitFailure;
},
.file_system_thread = unreachable, .file_system_thread = unreachable,
.user_prefix = pref_path, .user_prefix = pref_path,
}; };
event_loop.file_system_thread = ext.SDL_CreateThread(EventLoop.processFileSystemMessages, event_loop.file_system_thread = ext.SDL_CreateThread(
"File System Worker", &event_loop) orelse return error.InitFailure; EventLoop.processFileSystemMessages, "File System Worker", &event_loop) orelse {
ext.SDL_LogCritical(ext.SDL_LOG_CATEGORY_APPLICATION,
"Failed to create file-system work processor");
return error.InitFailure;
};
defer { defer {
ext.SDL_DestroyThread(event_loop.file_system_thread); ext.SDL_DestroyThread(event_loop.file_system_thread);
ext.SDL_DestroySemaphore(event_loop.file_system_mutex);
ext.SDL_DestroySemaphore(event_loop.file_system_semaphore); ext.SDL_DestroySemaphore(event_loop.file_system_semaphore);
} }
@ -513,5 +546,20 @@ pub fn runGraphics(comptime Errors: anytype, run: GraphicsRunner(Errors)) Errors
}, },
}; };
var message = EventLoop.FileSystemMessage{
.frame = @frame(),
.request = .exit,
};
@ptrCast(*EventLoop, event_loop).enqueueFileSystemMessage(&message);
var status = @as(c_int, 0);
ext.SDL_WaitThread(event_loop.file_system_thread, &status);
if (status != 0) {
// TODO: Error check this.
}
return run(@ptrCast(*EventLoop, &event_loop), @ptrCast(*GraphicsContext, &graphics_context)); return run(@ptrCast(*EventLoop, &event_loop), @ptrCast(*GraphicsContext, &graphics_context));
} }