All you need to think about is function try_lock(). As soon as you have implemented it, with async-listener it's trivial to also add: 1. fn lock() 2. fn lock_timeout() 3. async fn lock()
-
-
Show this thread
-
Have a lock_ops variable of type Event inside the mutex. Every time it is unlocked, call lock_ops.notify_one(). To implement lock() function, follow this procedure: 1. Attempt locking with try_lock(). 2. Start listening for events. 3. Attempt locking again. 4. Wait for an event.
Show this thread -
fn lock(&self) -> MutexGuard<'_, T> { loop { if let Some(g) = self.try_lock() { return g; } let listener = self.lock_ops.listen(); if let Some(g) = self.try_lock() { return g; } listener.wait(); } }
Show this thread -
That's it! If you want an asynchronous version, just change this line: listener.wait(); To this: listener.await; Event also has methods wait_timeout() and wait_deadline() that only wait for a limited amount of time. See full mutex implementation here: https://github.com/stjepang/event-listener/blob/036865ed5cc529d51be4045adf32c29b16b966e7/examples/mutex.rs …
Show this thread -
This also works with other data structures. As an example, if you have a concurrent queue, you can trivially add blocking or async methods to turn it into a channel. Think about how you would implement send() or recv() if all you have is try_send() or try_recv()!
Show this thread
End of conversation
New conversation -
-
-
These three crates seem tiny and tightly related. Why separate them?
-
They are tiny and kind of related but not very much. If they are kept in the same crate, that crate will become yet another catch-all kitchen-sink crate with a bunch of tiny related tools. I was never fond of such crates and they would always end up a big maintenance burden.
- 3 more replies
New conversation -
-
-
Thanks. Twitter will use this to make your timeline better. UndoUndo
-
-
-
Somebody is having a streak there
Thanks. Twitter will use this to make your timeline better. UndoUndo
-
-
-
I open twitter and
@stjepang has published a new crate.Thanks. Twitter will use this to make your timeline better. UndoUndo
-
Loading seems to be taking a while.
Twitter may be over capacity or experiencing a momentary hiccup. Try again or visit Twitter Status for more information.
