riot
riot copied to clipboard
process based mutex
Hello,
Here is a first draft for a process based Mutex
implementation.
The idea is that when you call lock
on a Mutex
- it will send a
Lock
message to theMutex
implementation - the current process will wait to receive a
LockAck
message wrapping the current Mutex value - if the mutex is currently locked it will enqueue the caller pid in its internal state
- when the Mutex process gets unlocked (or as soon as it receives a lock query if it's unlocked);
- it will elect the next locker pid (first in the queue)
- monitor the locker process
- go in a locked state until the locker process dies or the caller process sends an
Unlock
message
- when the locker process does not need the lock anymore, it sends an
Unlock
message containing the new value for the mutex.
I would like to add tests but what I have locally is a bit clumsy, so I thought I would PR this for comment already.
I would specially like to drag your attention on what should be done in case of a dead Mutex process, I am not sure how I could assert that the mutex is still alive before trying to lock on it.
Hi @julien-leclercq! Thanks for the PR 🙏🏼
I think I'd like us to explore an API that is not functor-based, so its a little easier to use.
For example, if we had Mutex.start_link value
function that returns a 'value Mutex.t
we should be able to just create new mutexes for any type:
let m_int : int Mutex.t = Mutex.start_link 2112 in
let m_band : band Mutex.t = Mutex.start_link { name = "rush" } in
Another thing is that I'd love to understand if we want to treat a mutex as a "box" that we can swap the contents of or a "box" that we can access the contents of. I think the box approach is very nice, but maybe we want an API that only gives us access to the resource within the mutex while we are holding the mutex.
let m_band = Mutex.start_link { name = "rush" } in
Mutex.lock m_band (fun band ->
(* do something with band *)
);
What do you think?
Hi! I volunteered to do #46 but I think I'm blocked on this as I need a Riot-friendly Mutex. I noticed it's been a little while since you've done any work on this, is it alright if I pick this up? @julien-leclercq
Edit: oop, meant to put this in the issue 😵💫