brunoczim / fslock

File locking for Rust.
MIT License
40 stars 11 forks source link

Any plan to support async? #11

Open soulmachine opened 2 years ago

soulmachine commented 2 years ago

This is not a bug report but a feature request.

This library will expand its usage cases if it supports async. Nowadays a lot of users in Rust world are using tokio, I hope this libary can provide async APIs, they can co-exist with sync APIs.

soulmachine commented 2 years ago

Currently this library only provides blocking APIs, which makes it difficult to use in async environments.

For example, if I use lock.lock().unwrap(), it will block the current thread, while cooperative runtimes like tokio are not aware that it is a blocking API, tokio will not suspend this task and this thread might get occupied forever.

So I have to call lock.try_lock() periodically in a loop, to alleviate lock competing I make it sleep a random time between 10 to 100 milliseconds, see utils.rs#L489-L503

If this library provides an async version of lock(), I could just do the job by one line lock.lock_async().await;, that's it. When tokio sees await, it knows that it's a good timing to suspend current task and run other tasks, so the thread is not wasted.

brunoczim commented 2 years ago

I think this could be possible, but I am not sure about what API I'd use with non-Linux platforms