-
-
Notifications
You must be signed in to change notification settings - Fork 39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
process based mutex #56
Open
julien-leclercq
wants to merge
1
commit into
riot-ml:main
Choose a base branch
from
julien-leclercq:process-based-mutex
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
module type Base = sig | ||
type value | ||
end | ||
|
||
module MakeServer (B : Base) = struct | ||
open Global | ||
open Util | ||
|
||
type value = B.value | ||
type state = { value : value; queue : Pid.t Lf_queue.t } | ||
|
||
type Message.t += | ||
| Lock of Pid.t | ||
| Unlock of (Pid.t * B.value) | ||
| LockAck of (Pid.t * value) | ||
|
||
let rec loop_locked state locker_pid = | ||
match receive () with | ||
| Lock pid -> | ||
let () = Lf_queue.push state.queue pid in | ||
loop_locked state locker_pid | ||
| Unlock (pid, value) when locker_pid = pid -> | ||
let () = demonitor locker_pid in | ||
loop_unlocked { state with value } | ||
| Unlock (_, _) -> failwith "wrong pid tried to unlock mutex" | ||
| Process.Messages.Monitor (Process_down fell_pid) | ||
when locker_pid = fell_pid -> | ||
Logger.debug (fun f -> f "locker process crashed"); | ||
loop_unlocked state | ||
| _ -> failwith "unexpected message" | ||
|
||
and loop_unlocked state = | ||
match Lf_queue.pop state.queue with | ||
| Some pid -> | ||
let () = send pid (LockAck (self (), state.value)) in | ||
let () = monitor pid in | ||
loop_locked state pid | ||
| None -> ( | ||
match receive () with | ||
| Lock pid -> | ||
let () = send pid (LockAck (self (), state.value)) in | ||
let () = monitor pid in | ||
loop_locked state pid | ||
| _ -> failwith "unexpected message") | ||
|
||
let start_link value = | ||
let state = { queue = Lf_queue.create (); value } in | ||
(fun () -> loop_unlocked state) |> spawn_link |> Result.ok | ||
end | ||
|
||
module type Intf = sig | ||
type value | ||
type t | ||
|
||
val start_link : value -> (t, [> `Exn of exn ]) result | ||
val lock : t -> value | ||
val unlock : t -> value -> unit | ||
end | ||
|
||
module Make (B : Base) = struct | ||
open Global | ||
module Server = MakeServer (B) | ||
|
||
type value = B.value | ||
type t = Pid.t | ||
|
||
let start_link = Server.start_link | ||
|
||
(* PR-Note (from: @julien-leclercq): How to handle the case of a dead mutex process ?*) | ||
let lock mutex = | ||
let () = send mutex @@ Server.Lock (self ()) in | ||
let rec do_receive () = | ||
match receive () with | ||
| Server.(LockAck (sender, value)) when sender = mutex -> value | ||
| msg -> | ||
send (self ()) msg; | ||
do_receive () | ||
in | ||
do_receive () | ||
|
||
let unlock mutex new_value = send mutex Server.(Unlock (self (), new_value)) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
☝️ here is the part where we could be in trouble if the Mutex process is dead