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

make Unix.sleepf available under all OCaml versions #930

Merged
merged 5 commits into from
Dec 16, 2019
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ Changelog

## NEXT_RELEASE

... cool features to come.
- Unix.sleepf is provided across all OCaml versions;
previously it was only for OCaml >= 4.03.0
#930
(Francois Berenger, review by Cedric Cellier)

## v2.10.0 (minor release)

Expand Down
8 changes: 4 additions & 4 deletions src/batUnix.mliv
Original file line number Diff line number Diff line change
Expand Up @@ -1144,10 +1144,10 @@ val alarm : int -> int
val sleep : int -> unit
(** Stop execution for the given number of seconds. *)

##V>=4.3##val sleepf : float -> unit
##V>=4.3##(** Stop execution for the given number of seconds. Like [sleep],
##V>=4.3## but fractions of seconds are supported.
##V>=4.3## @since 2.5.0 and OCaml 4.03 *)
val sleepf : float -> unit
(** Stop execution for the given number of seconds. Like [sleep],
but fractions of seconds are supported.
@since 2.5.0 *)

val times : unit -> process_times
(** Return the execution times of the process. *)
Expand Down
31 changes: 31 additions & 0 deletions src/batUnix.mlv
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,37 @@ include Unix
##V<4.2##let send_substring = send
##V<4.2##let sendto_substring = sendto

##V<4.3##let sleepf (timeout: float): unit =
##V<4.3## let elapsed = ref 0.0 in
##V<4.3## while !elapsed < timeout do
##V<4.3## let start = gettimeofday () in
##V<4.3## begin
##V<4.3## try ignore(select [] [] [] (timeout -. !elapsed))
##V<4.3## with Unix_error(EINTR, _, _) -> ()
##V<4.3## end;
##V<4.3## let stop = gettimeofday () in
##V<4.3## let dt = stop -. start in
##V<4.3## elapsed := !elapsed +. dt
##V<4.3## done;
##V<4.3## ()

(* chronometer is useful to test sleepf *)
(*$inject
let chronometer f =
let start = gettimeofday () in
let res = f () in
let stop = gettimeofday () in
let dt = stop -. start in
(dt, res) ;;
*)

(* do not underestimate the imprecission of sleepf
and so don't be too harsh when testing it *)
(*$T sleepf
let dt, _ = chronometer (fun () -> sleepf 0.002) in \
0.001 <= dt && dt <= 0.003
*)

let run_and_read cmd =
(* This code is before the open of BatInnerIO
to avoid using batteries' wrapped IOs *)
Expand Down