// HACKER NEWS — CYBERSECURITY
Zig’s io.threaded is neat
std.Io.Threaded
is one of the implementations of Zig’s new Io interface that enables concurrency. This is a boring
“just use threads” impl. I personally find it neat though — it does this weird thing that
I wanted to do for ages, that to my knowledge no
one else is doing properly, and implements it better than I thought to be possible.
Io.Threaded uses blocking syscalls and fully supports cancelation.
I think this definition is correct, but doesn’t provide useful intuition directly. Concurrency is
the same thing as state transducers? Yes, obviously, but not really illuminating as to how you’d
program the thing.
For intuition, I like these two litmus tests. First, parallelism is deterministic or
“declarative”:
You describe how to split the problem into independent partitions, and implement a function to
process one partition at a time . It’s platform’s job to verify the partitioning to be correct
(non-racy), process all partitions, and yield control back once that is done.
Second, concurrency invariably involves cancelation. Whenever you have two asynchronous
computations happening at the same time, there comes a moment when one computation becomes aware
that the second computation is no longer necessary, and must be canceled, actively. In general, it
is not possible to just wait until the other computation completes: often, the reason why you want
to cancel it in the first place is precisely because you’ve learned that it can’t complete (e.g.,
it is waiting for a message it will never receive).
Well, there are more, the chief being that, while you totally can spawn many threads, this often
requires system-wide configuration change, which is a non-starter for most application. But absence
of cancelation really makes you hit a wall sooner or later. The problem are syscalls. It’s easy
enough, in any loopy code, to do something like
But, the thread is instead blocked inside the syscall in the kernel, programming language APIs
generally doesn’t give any way to unblock it:
Wouldn’t it be cool if we could just use standard OS threads, blocking APIs, avoid new shinies like
io_uring, but still get to cancel any work reliably? That’s exactly what Zig’s std.Io.Threaded
provides.
The way this works on POSIX is a bit cursed. Turns out, the kernel actually provides a roundabout
way to cancel a blocking syscall — signals. When a thread is blocked in the kernel, and a signal
is delivered to the thread, the thread is woken up and the syscall returns EINTR. It is customary
to just
loop re-try the syscall
in such cases, but one doesn’t have to.