Support idling in file system message processor
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
This commit is contained in:
parent
52bc326187
commit
584f892e4d
168
src/sys.zig
168
src/sys.zig
|
@ -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,69 +161,74 @@ 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 (implementation.file_system_messages) |messages| {
|
while (true) {
|
||||||
switch (messages.request) {
|
while (implementation.file_system_messages) |messages| {
|
||||||
.open => |*open_request| {
|
switch (messages.request) {
|
||||||
switch (open_request.path.file_system) {
|
.exit => return 0,
|
||||||
.data => {
|
|
||||||
// TODO: Implement
|
|
||||||
open_request.result = error.NotFound;
|
|
||||||
},
|
|
||||||
|
|
||||||
.user => {
|
.open => |*open_request| {
|
||||||
var path_buffer = std.mem.zeroes([4096]u8);
|
switch (open_request.path.file_system) {
|
||||||
var path = stack.Fixed(u8){.buffer = path_buffer[0 .. ]};
|
.data => {
|
||||||
|
// TODO: Implement
|
||||||
path.pushAll(implementation.user_prefix) catch {
|
|
||||||
open_request.result = error.BadFileSystem;
|
|
||||||
|
|
||||||
continue;
|
|
||||||
};
|
|
||||||
|
|
||||||
if (!open_request.path.write(path.writer())) {
|
|
||||||
open_request.result = error.NotFound;
|
open_request.result = error.NotFound;
|
||||||
|
},
|
||||||
|
|
||||||
continue;
|
.user => {
|
||||||
}
|
var path_buffer = std.mem.zeroes([4096]u8);
|
||||||
|
var path = stack.Fixed(u8){.buffer = path_buffer[0 .. ]};
|
||||||
|
|
||||||
if (ext.SDL_RWFromFile(&path_buffer, switch (open_request.mode) {
|
path.pushAll(implementation.user_prefix) catch {
|
||||||
.readonly => "rb",
|
open_request.result = error.BadFileSystem;
|
||||||
.overwrite => "wb",
|
|
||||||
.append => "ab",
|
|
||||||
})) |rw_ops| {
|
|
||||||
open_request.result = @ptrCast(*FileAccess, rw_ops);
|
|
||||||
} else {
|
|
||||||
open_request.result = error.NotFound;
|
|
||||||
}
|
|
||||||
},
|
|
||||||
}
|
|
||||||
},
|
|
||||||
|
|
||||||
.close => |*close_request| {
|
continue;
|
||||||
// Don't care if this doesn't work.
|
};
|
||||||
_ = ext.SDL_RWclose(@ptrCast(*ext.SDL_RWops, @alignCast(
|
|
||||||
@alignOf(ext.SDL_RWops), close_request.file_access)));
|
|
||||||
},
|
|
||||||
|
|
||||||
.read_file => |read_request| {
|
if (!open_request.path.write(path.writer())) {
|
||||||
_ = read_request;
|
open_request.result = error.NotFound;
|
||||||
},
|
|
||||||
|
|
||||||
.seek_file => |seek_request| {
|
continue;
|
||||||
_ = seek_request;
|
}
|
||||||
},
|
|
||||||
|
|
||||||
.tell_file => |tell_request| {
|
if (ext.SDL_RWFromFile(&path_buffer, switch (open_request.mode) {
|
||||||
_ = tell_request;
|
.readonly => "rb",
|
||||||
},
|
.overwrite => "wb",
|
||||||
|
.append => "ab",
|
||||||
|
})) |rw_ops| {
|
||||||
|
open_request.result = @ptrCast(*FileAccess, rw_ops);
|
||||||
|
} else {
|
||||||
|
open_request.result = error.NotFound;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
.close => |*close_request| {
|
||||||
|
// Don't care if this doesn't work.
|
||||||
|
_ = ext.SDL_RWclose(@ptrCast(*ext.SDL_RWops, @alignCast(
|
||||||
|
@alignOf(ext.SDL_RWops), close_request.file_access)));
|
||||||
|
},
|
||||||
|
|
||||||
|
.read_file => |read_request| {
|
||||||
|
_ = read_request;
|
||||||
|
},
|
||||||
|
|
||||||
|
.seek_file => |seek_request| {
|
||||||
|
_ = seek_request;
|
||||||
|
},
|
||||||
|
|
||||||
|
.tell_file => |tell_request| {
|
||||||
|
_ = tell_request;
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
resume messages.frame;
|
||||||
|
|
||||||
|
implementation.file_system_messages = messages.next;
|
||||||
}
|
}
|
||||||
|
|
||||||
resume messages.frame;
|
// TODO: Error check this.
|
||||||
|
_ = ext.SDL_SemWait(implementation.file_system_semaphore);
|
||||||
implementation.file_system_messages = messages.next;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
///
|
///
|
||||||
|
@ -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));
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue