Skip to content
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

Add Lwt_mvar.(take_available, is_empty) #459

Merged
merged 5 commits into from
Jul 27, 2017
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 22 additions & 10 deletions src/core/lwt_mvar.ml
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,28 @@ let put mvar v =
Lwt.on_cancel res (fun _ -> Lwt_sequence.remove node);
res

let take mvar =
let next_writer mvar =
match Lwt_sequence.take_opt_l mvar.writers with
| Some(v', w) ->
mvar.mvar_contents <- Some v';
Lwt.wakeup_later w ()
| None ->
mvar.mvar_contents <- None

let take_available mvar =
match mvar.mvar_contents with
| Some v ->
begin match Lwt_sequence.take_opt_l mvar.writers with
| Some(v', w) ->
mvar.mvar_contents <- Some v';
Lwt.wakeup_later w ()
| None ->
mvar.mvar_contents <- None
end;
Lwt.return v
next_writer mvar;
Some v
| None ->
Lwt.add_task_r mvar.readers
None

let take mvar =
match take_available mvar with
| Some v -> Lwt.return v
| None -> Lwt.add_task_r mvar.readers

let is_empty mvar =
match mvar.mvar_contents with
| Some _ -> false
| None -> true
8 changes: 8 additions & 0 deletions src/core/lwt_mvar.mli
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,11 @@ val take : 'a t -> 'a Lwt.t
mailbox variable. If no value is currently available, the
current thread will block, awaiting a value to be [put] by
another thread. *)

val take_available : 'a t -> 'a option
(** [take_available mvar] immediately takes the value from [mvar] without
blocking, returning [None] if the mailbox is empty. *)

val is_empty : 'a t -> bool
(** [is_empty mvar] indicates if [put mvar] can be called without
blocking. *)
28 changes: 28 additions & 0 deletions test/core/test_lwt_mvar.ml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,34 @@ let suite =
Lwt.return (Lwt.state y = Lwt.Return 0)
);

test "take_available (full)"
(fun () ->
let x = Lwt_mvar.create 0 in
let y = Lwt_mvar.take_available x in
Lwt.return (y = Some 0)
);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There should be another test to check that after take_available has been called, the mvar is empty. Something along the lines of:

test "take_available (twice)"
(fun () ->
  let m = Lwt_mvar.create 0 in
  let (_: int option) = Lwt_mvar.take_available m in
  let t = Let_mvar.take_available m in
  Lwt.return (t = None)
);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good suggestion, thanks! Added in 7799c15


test "take_available (empty)"
(fun () ->
let x = Lwt_mvar.create_empty () in
let y = Lwt_mvar.take_available x in
Lwt.return (y = None)
);

test "is_empty (full)"
(fun () ->
let x = Lwt_mvar.create 0 in
let y = Lwt_mvar.is_empty x in
Lwt.return (not y)
);

test "is_empty (empty)"
(fun () ->
let x = Lwt_mvar.create_empty () in
let y = Lwt_mvar.is_empty x in
Lwt.return y
);

test "blocking put"
(fun () ->
let x = Lwt_mvar.create 0 in
Expand Down