I know that Io wants to bundle in file operations, networking, etc into the interface along with async. However, every time I see a demonstration of Io, it always looks something like this:
var threadpool = std.ThreadPool.create(alloc);
const io = threadpool.io();
Since io is an instance of the Io interface, I’m assuming it would have file/networking/etc operations implemented. I’m also assuming that the zig std library will have implementations of those functions for most targets that the threadpool.io() call will link to when making the Io vtable.
What I’m wondering is how that would work on a freestanding target. If I had some storage device connected to my chip, how would I tell ThreadPool to use my file management functions instead of the default ones by the zig std library (which I’m assuming would just @panic() on freestanding targets)? All the talk I’ve seen about Io has been about async stuff, so I haven’t seen anyone mention this yet apologies if this was already asked in another place
It doesn’t magically use std.fs the implementation explicitly calls them, just call your fs in your Io implementation.
Yes I’m aware of how the fs functions would work normally in the new system
What I’m confused about is how I would set my fs functions on a freestanding target. I could override the values in the vtable at runtime, like:
io.openFile = myOpenFile;
but then the std implementations of the functions would also be compiled in to be set as the default in the threadpool.io() function when I do not want the default implementations in the binary
You shouldn’t modify the vtable after it’s created, instead provide a complete one with your functions, preferably a myTargetThreadPoolIo(tp: *ThreadPool) Io.
That way, as long as you never call pool.io, the original functions will never be reference, guaranteeing they aren’t compiled.
At least currently, reusing the existing ThreadPool is ill-advised as many of the functions and thread local state is private, meaning you will lose functionality, specifically for the case of fs functions, you won’t be able to check if the task is cancelled.
1 Like