Cloudef / zig-aio

io_uring like asynchronous API and coroutine powered IO tasks for zig
https://cloudef.github.io/zig-aio/
MIT License
175 stars 6 forks source link

Proposal: Implementing an async mutex #25

Closed YiraSan closed 2 weeks ago

YiraSan commented 2 weeks ago

I've been using zig-aio for the past weeks, and I would say that the only thing missing right now is an async mutex, instead of blocking the coroutine, it would be nice to have an asynchronous mutex, and a rwlock deriving from it.

I've multiple idea on how to implement it, but since I did not read enough the source code of zig-aio I can't really directly pull request. I first thought of a basic aio.Mutex, that would use inside a std.Thread.Mutex, but only doing tryLock() then it would either do a aio.Timeout(1ns or so), or directly complete a pending coroutine then check again tryLock until the lock is finally acquirable.

What do you think of that idea ? Is it a better way to implement an asynchronous mutex ?

Cloudef commented 2 weeks ago

Did you look at https://github.com/Cloudef/zig-aio/blob/master/src/coro/Sync.zig ?

YiraSan commented 2 weeks ago

Right, I totally missed that one ! There's still a need for a RwLock implementation, this being pretty simple to do, I will probably PR this week for it.