Skip to content
This repository has been archived by the owner on Oct 18, 2020. It is now read-only.

Commit

Permalink
xen: also sleep in nanoseconds
Browse files Browse the repository at this point in the history
  • Loading branch information
hannesm committed Jul 27, 2016
1 parent 9bc921f commit 5b84d50
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 13 deletions.
2 changes: 1 addition & 1 deletion xen/lib/main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ let run t =
end else begin
let timeout =
match Time.select_next () with
|None -> Time.Monotonic.(time () + of_seconds 86400.0) (* one day = 24 * 60 * 60 s *)
|None -> Time.Monotonic.(time () + of_nanoseconds 86_400_000_000_000L) (* one day = 24 * 60 * 60 s *)
|Some tm -> tm
in
MProf.Trace.(note_hiatus Wait_for_work);
Expand Down
9 changes: 4 additions & 5 deletions xen/lib/time.ml
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ module Monotonic = struct

external time : unit -> int64 = "caml_get_monotonic_time"

let of_seconds x = Int64.of_float (x *. 1_000_000_000.)
let to_seconds x = Int64.to_float x /. 1_000_000_000.
let of_nanoseconds x = x

let ( + ) = ( Int64.add )
let ( - ) = ( Int64.sub )
Expand Down Expand Up @@ -67,17 +66,17 @@ let sleep_queue = ref SleepQueue.empty
*)
let new_sleeps = ref []

let sleep d =
let sleep_ns d =
let (res, w) = MProf.Trace.named_task "sleep" in
let t = if d <= 0. then 0L else Monotonic.(time () + of_seconds d) in
let t = Monotonic.(time () + of_nanoseconds d) in
let sleeper = { time = t; canceled = false; thread = w } in
new_sleeps := sleeper :: !new_sleeps;
Lwt.on_cancel res (fun _ -> sleeper.canceled <- true);
res

exception Timeout

let timeout d = sleep d >>= fun () -> Lwt.fail Timeout
let timeout d = sleep_ns d >>= fun () -> Lwt.fail Timeout

let with_timeout d f = Lwt.pick [timeout d; Lwt.apply f ()]

Expand Down
12 changes: 5 additions & 7 deletions xen/lib/time.mli
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,9 @@ module Monotonic : sig
val ( - ) : 'a t -> [`Interval] t -> 'a t
val interval : [`Time] t -> [`Time] t -> [`Interval] t

(** Conversions. Note: these floats are still seconds since boot. *)
(** Conversions. Note: still seconds since boot. *)
val of_nanoseconds : int64 -> _ t

val of_seconds : float -> _ t
val to_seconds : _ t -> float
end

val restart_threads: (unit -> [`Time] Monotonic.t) -> unit
Expand All @@ -48,14 +47,13 @@ val select_next : unit -> [`Time] Monotonic.t option
when one sleeping thread will wake up, or [None] if there is no
sleeping threads. *)

val sleep : float -> unit Lwt.t
(** [sleep d] is a threads which remain suspended for [d] seconds and
then terminates. *)
val sleep_ns : int64 -> unit Lwt.t
(** [sleep_ns n] Block the current thread for n nanoseconds. *)

exception Timeout
(** Exception raised by timeout operations *)

val with_timeout : float -> (unit -> 'a Lwt.t) -> 'a Lwt.t
val with_timeout : int64 -> (unit -> 'a Lwt.t) -> 'a Lwt.t
(** [with_timeout d f] is a short-hand for:
{[
Expand Down

0 comments on commit 5b84d50

Please sign in to comment.