Implement file system worker thread in event loop
continuous-integration/drone/push Build is passing Details

This commit is contained in:
kayomn 2022-10-01 17:46:21 +01:00
parent 9443d02de0
commit 3c82b8a6a5
3 changed files with 211 additions and 56 deletions

View File

@ -17,16 +17,22 @@ pub fn main() anyerror!void {
fn run(event_loop: *sys.EventLoop, graphics: *sys.GraphicsContext) anyerror!void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
{
const file_access =
try event_loop.open(.readonly, try sys.FileSystem.data.pathJoin(&.{"data", "ona.lua"}));
const file_access = try event_loop.open(.readonly,
try sys.FileSystem.data.joinedPath(&.{"data", "ona.lua"}));
defer _ = event_loop.close(file_access);
const file_size = try file_access.size(event_loop);
var buffer = try gpa.allocator().alloc(u8, file_size);
const allocator = gpa.allocator();
const buffer = try allocator.alloc(u8, file_size);
if ((try event_loop.readFile(file_access, buffer)) != file_size) return error.ScriptLoadError;
defer allocator.free(buffer);
if ((try event_loop.readFile(file_access, buffer)) != file_size)
return error.ScriptLoadFailure;
event_loop.log(buffer);
}

View File

@ -34,7 +34,7 @@ pub fn Spliterator(comptime Element: type) type {
while (self.hasNext()) {
var cursor = @as(usize, 0);
var window = self.source[cursor .. (self.source - cursor)];
var window = self.source[cursor .. (self.source.len - cursor)];
defer self.source = window;

View File

@ -11,29 +11,56 @@ const std = @import("std");
///
///
pub const EventLoop = opaque {
const Implementation = struct {
current_message: ?*Message,
///
///
///
const FileSystemMessage = struct {
next: ?*FileSystemMessage = null,
frame: anyframe,
request: union(enum) {
close: struct {
file_access: *FileAccess,
},
open: struct {
mode: OpenMode,
path: *const FileSystem.Path,
result: OpenError!*FileAccess = error.NotFound,
},
read_file: struct {
file_access: *FileAccess,
buffer: []const u8,
result: FileError!usize = error.Inaccessible,
},
seek_file: struct {
file_access: *FileAccess,
origin: SeekOrigin,
offset: usize,
result: FileError!void = error.Inaccessible,
},
tell_file: struct {
file_access: *FileAccess,
result: FileError!usize = error.Inaccessible,
},
},
};
///
///
///
const Message = struct {
next: ?*Message,
frame: anyframe,
const Implementation = struct {
user_prefix: []const u8,
file_system_semaphore: *ext.SDL_Semaphore,
file_system_thread: *ext.SDL_Thread,
file_system_messages: ?*FileSystemMessage = null,
request: union(enum) {
open: struct {
mode: OpenMode,
path: *const FileSystem.Path,
},
},
response: union(enum) {
none,
file_access: *FileAccess,
file_error: FileError,
} = .none,
fn cast(event_loop: *EventLoop) *Implementation {
return @ptrCast(*Implementation, @alignCast(@alignOf(Implementation), event_loop));
}
};
///
@ -41,6 +68,7 @@ pub const EventLoop = opaque {
///
pub const OpenError = error {
NotFound,
BadFileSystem,
};
///
@ -55,34 +83,42 @@ pub const EventLoop = opaque {
///
///
///
pub fn close(event_loop: *EventLoop, file_access: *FileAccess) void {
var message = Message{
.frame = @frame(),
.request = .{
.close = .{
.file_access = file_access,
},
},
pub const SeekOrigin = enum {
head,
tail,
current,
};
suspend event_loop.enqueueMessage(&message);
///
///
///
pub fn close(event_loop: *EventLoop, file_access: *FileAccess) void {
var message = FileSystemMessage{
.frame = @frame(),
.request = .{.close = .{.file_access = file_access}},
};
suspend event_loop.enqueueFileSystemMessage(&message);
}
///
///
///
fn enqueueMessage(event_loop: *EventLoop, message: *Message) void {
const implementation =
@ptrCast(*Implementation, @alignCast(@alignOf(Implementation), event_loop));
pub fn enqueueFileSystemMessage(event_loop: *EventLoop, message: *FileSystemMessage) void {
const implementation = Implementation.cast(event_loop);
if (implementation.current_message) |current_message| {
current_message.next = message;
if (implementation.file_system_messages) |messages| {
messages.next = message;
} else {
implementation.current_message = message;
}
implementation.file_system_messages = message;
}
// TODO: Post message to FS thread to perform task.
}
///
///
///
pub fn log(event_loop: *EventLoop, message: []const u8) void {
// TODO: Implement.
_ = event_loop;
@ -95,7 +131,7 @@ pub const EventLoop = opaque {
pub fn open(event_loop: *EventLoop, mode: OpenMode,
path: FileSystem.Path) OpenError!*FileAccess {
var message = Message{
var message = FileSystemMessage{
.frame = @frame(),
.request = .{
@ -106,13 +142,80 @@ pub const EventLoop = opaque {
},
};
suspend event_loop.enqueueMessage(&message);
suspend event_loop.enqueueFileSystemMessage(&message);
switch (message.response) {
.file_access => |file_access| return file_access,
.open_error => |open_error| return open_error,
else => unreachable,
return message.request.open.result;
}
///
///
///
fn processFileSystemMessages(data: ?*anyopaque) callconv(.C) c_int {
const implementation = Implementation.cast(@ptrCast(*EventLoop, data orelse unreachable));
while (implementation.file_system_messages) |messages| {
switch (messages.request) {
.open => |*open_request| {
switch (open_request.path.file_system) {
.data => {
// TODO: Implement
open_request.result = error.NotFound;
},
.user => {
var path_buffer = std.mem.zeroes([4096]u8);
var path = stack.Fixed(u8){.buffer = path_buffer[0 .. ]};
path.pushAll(implementation.user_prefix) catch {
open_request.result = error.BadFileSystem;
continue;
};
if (!open_request.path.write(path.writer())) {
open_request.result = error.NotFound;
continue;
}
if (ext.SDL_RWFromFile(&path_buffer, switch (open_request.mode) {
.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;
}
return 0;
}
///
@ -121,7 +224,7 @@ pub const EventLoop = opaque {
pub fn readFile(event_loop: *EventLoop, file_access: *FileAccess,
buffer: []const u8) FileError!usize {
var message = Message{
var message = FileSystemMessage{
.frame = @frame(),
.request = .{
@ -132,13 +235,46 @@ pub const EventLoop = opaque {
},
};
suspend event_loop.enqueueMessage(&message);
suspend event_loop.enqueueFileSystemMessage(&message);
switch (message.response) {
.size => |size| return size,
.file_error => |file_error| return file_error,
else => unreachable,
return message.request.read_file.result;
}
///
///
///
pub fn seekFile(event_loop: *EventLoop, file_access: *FileAccess,
origin: SeekOrigin, offset: usize) FileError!void {
var message = FileSystemMessage{
.frame = @frame(),
.request = .{
.seek_file = .{
.file_access = file_access,
.origin = origin,
.offset = offset,
},
},
};
suspend event_loop.enqueueFileSystemMessage(&message);
return message.request.seek_file.result;
}
///
///
///
pub fn tellFile(event_loop: *EventLoop, file_access: *FileAccess) FileError!usize {
var message = FileSystemMessage{
.frame = @frame(),
.request = .{.tell_file = .{.file_access = file_access}},
};
suspend event_loop.enqueueFileSystemMessage(&message);
return message.request.tell_file.result;
}
};
@ -230,18 +366,18 @@ pub const FileSystem = enum {
///
/// Creates and returns a [Path] value in the file system to the location specified by the
/// joining of the `component_groups` path values.
/// joining of the `sequences` path values.
///
pub fn pathJoin(file_system: FileSystem, component_groups: []const []const u8) PathError!Path {
pub fn joinedPath(file_system: FileSystem, sequences: []const []const u8) PathError!Path {
var path = Path{
.file_system = file_system,
.buffer = std.mem.zeroes([Path.max]u8),
.length = 0,
};
for (component_groups) |component_group| if (component_group.len != 0) {
for (sequences) |sequence| if (sequence.len != 0) {
var components = mem.Spliterator(u8){
.source = component_group,
.source = sequence,
.delimiter = "/",
};
@ -272,7 +408,11 @@ pub const GraphicsContext = opaque {
///
///
pub const Event = struct {
keys_up: Keys = std.mem.zeroes(Keys),
keys_down: Keys = std.mem.zeroes(Keys),
keys_held: Keys = std.mem.zeroes(Keys),
const Keys = [256]bool;
};
const Implementation = struct {
@ -283,7 +423,6 @@ pub const GraphicsContext = opaque {
///
///
pub fn poll(graphics_context: *GraphicsContext) ?*const Event {
// TODO: Implement.
_ = graphics_context;
return null;
@ -355,9 +494,19 @@ pub fn runGraphics(comptime Errors: anytype, run: GraphicsRunner(Errors)) Errors
defer ext.SDL_DestroyRenderer(renderer);
var event_loop = EventLoop.Implementation{
.current_message = null,
.file_system_semaphore = ext.SDL_CreateSemaphore(0) orelse return error.InitFailure,
.file_system_thread = ext.SDL_CreateThread(EventLoop.processFileSystemMessages,
"File System Worker", &event_loop) orelse return error.InitFailure,
.user_prefix = pref_path,
};
defer {
ext.SDL_DestroyThread(event_loop.file_system_thread);
ext.SDL_DestroySemaphore(event_loop.file_system_semaphore);
}
var graphics_context = GraphicsContext.Implementation{
.event = .{