Skip to content

Commit

Permalink
provide new sleeper storage in here
Browse files Browse the repository at this point in the history
  • Loading branch information
hannesm committed May 18, 2024
1 parent d7c1088 commit 32c2880
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 10 deletions.
1 change: 0 additions & 1 deletion mirage-time.opam
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ depends: [
"lwt" {>= "4.0.0"}
"duration"
]
depopts: [ "mirage-runtime" ]
build: [
["dune" "subst"] {dev}
["dune" "build" "-p" name "-j" jobs]
Expand Down
1 change: 0 additions & 1 deletion solo5/dune
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@
(name mirage_time_solo5)
(public_name mirage-time.solo5)
(implements mirage-time)
(libraries mirage-runtime lwt)
(optional))
13 changes: 11 additions & 2 deletions solo5/mirage_time.ml
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
external time : unit -> int64 = "caml_get_monotonic_time"

type sleep = { time : int64; mutable canceled : bool; thread : unit Lwt.u }

let sleepers = ref []

let sleep_ns d =
let res, w = Lwt.task () in
let time = Int64.add (time ()) d in
let sleeper = Mirage_runtime.{ time; canceled = false; thread = w } in
Mirage_runtime.add_new_sleeper sleeper;
let sleeper = { time; canceled = false; thread = w } in
sleepers := sleeper :: !sleepers;
Lwt.on_cancel res (fun _ -> sleeper.canceled <- true);
res

let new_sleepers () =
let sl = !sleepers in
sleepers := [];
sl
19 changes: 13 additions & 6 deletions src/mirage_time.mli
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,20 @@
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*)

(** {1 Time-related devices}
(** {1 Time device}
This module define time-related devices for MirageOS and
sleep operations.
{e Release %%VERSION%% } *)
This module defines a target-agnostic MirageOS sleep operation. *)

val sleep_ns: int64 -> unit Lwt.t
(** [sleep_ns n] Block the current thread for [n] nanoseconds, treating the [n]
(** [sleep_ns n] blocks the current task for [n] nanoseconds, treating [n] as
unsigned. *)

(** {1 Values used by the schedulers} *)

type sleep = { time : int64; mutable canceled : bool; thread : unit Lwt.u }
(** The type for a sleeping task. *)

val new_sleepers : unit -> sleep list
(** [new_sleepers ()] is used by the scheduler to find at their convenience
the tasks that need to be enqueued into their task set. This also empties
the list of sleepers. *)
4 changes: 4 additions & 0 deletions unix/mirage_time.ml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,7 @@
*)

let sleep_ns ns = Lwt_unix.sleep (Duration.to_f ns)

type sleep = { time : int64; mutable canceled : bool; thread : unit Lwt.u }

let new_sleepers () = []

0 comments on commit 32c2880

Please sign in to comment.