Skip to content

Commit

Permalink
Expose uv_clock_gettime
Browse files Browse the repository at this point in the history
  • Loading branch information
aantron committed Dec 15, 2023
1 parent fa3bc3e commit df7d218
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 3 deletions.
4 changes: 4 additions & 0 deletions src/c/luv_c_function_descriptions.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1852,6 +1852,10 @@ struct
let hrtime =
foreign "uv_hrtime"
(void @-> returning uint64_t)

let clock_gettime =
foreign "uv_clock_gettime"
(int @-> ptr Types.Time.Timespec.t @-> returning error_code)
end

module Random =
Expand Down
12 changes: 12 additions & 0 deletions src/c/luv_c_type_descriptions.ml
Original file line number Diff line number Diff line change
Expand Up @@ -976,6 +976,18 @@ struct
let usec = field t "tv_usec" int32_t
let () = seal t
end

module Timespec =
struct
let t : ([ `Timespec ] structure) typ =
typedef (structure "`Timespec64") "uv_timespec64_t"
let sec = field t "tv_sec" int64_t
let nsec = field t "tv_nsec" int32_t
let () = seal t

let monotonic = constant "UV_CLOCK_MONOTONIC" int
let real_time = constant "UV_CLOCK_REALTIME" int
end
end

module Env_item =
Expand Down
10 changes: 10 additions & 0 deletions src/c/shims.h
Original file line number Diff line number Diff line change
Expand Up @@ -714,4 +714,14 @@
static void uv_os_free_group(uv_passwd_t *group)
{
}

typedef struct {
int64_t tv_sec;
int32_t tv_nsec;
} uv_timespec64_t;

static int uv_clock_gettime(int clock_id, uv_timespec64_t *time)
{
return ENOSYS;
}
#endif
1 change: 1 addition & 0 deletions src/feature/detect_features.ml
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ let () =

let parallelism = "available_parallelism" in
needs 44 parallelism "See {!Luv.System_info.available_parallelism}.";
needs 45 "clock_gettime" "See {!Luv.Time.clock_gettime}.";
needs 45 "cpumask_size" "See {!Luv.System_info.cpumask_size}.";
needs 9 "disconnect" "See [`DISCONNECT] in {!Luv.Poll.Event.t}.";
needs 21 "eftype" "See [`EFTYPE] in {!Luv.Error.t}.";
Expand Down
24 changes: 23 additions & 1 deletion src/time.ml
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@



type t = {
type timeval = {
tv_sec : int64;
tv_usec : int32;
}

type t = timeval

let gettimeofday () =
let timeval = Ctypes.make C.Types.Time.Timeval.t in
C.Functions.Time.gettimeofday (Ctypes.addr timeval)
Expand All @@ -21,5 +23,25 @@ let gettimeofday () =
let hrtime =
C.Functions.Time.hrtime

type timespec = {
sec : int64;
nsec : int32;
}

let clock_gettime clock =
let timespec = Ctypes.make C.Types.Time.Timespec.t in
let clock =
match clock with
| `Monotonic -> C.Types.Time.Timespec.monotonic
| `Real_time -> C.Types.Time.Timespec.real_time
in
C.Functions.Time.clock_gettime clock (Ctypes.addr timespec)
|> Error.to_result_lazy begin fun () ->
{
sec = Ctypes.getf timespec C.Types.Time.Timespec.sec;
nsec = Ctypes.getf timespec C.Types.Time.Timespec.nsec;
}
end

let sleep =
C.Blocking.Time.sleep
27 changes: 25 additions & 2 deletions src/time.mli
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@



type t = {
type timeval = {
tv_sec : int64;
tv_usec : int32;
}
(** Binds {{:http://docs.libuv.org/en/v1.x/misc.html#c.uv_timeval64_t}
[uv_timeval64_t]}. *)

val gettimeofday : unit -> (t, Error.t) result
(**/**)
type t = timeval
(**/**)

val gettimeofday : unit -> (timeval, Error.t) result
(** Binds {{:http://docs.libuv.org/en/v1.x/misc.html#c.uv_gettimeofday}
[uv_gettimeofday]}. See
{{:http://man7.org/linux/man-pages/man3/gettimeofday.3p.html}
Expand All @@ -28,6 +32,25 @@ val hrtime : unit -> Unsigned.uint64
{{:http://man7.org/linux/man-pages/man3/clock_gettime.3p.html}
[clock_gettime(3p)]}. *)

type timespec = {
sec : int64;
nsec : int32;
}
(** Binds {{:http://docs.libuv.org/en/v1.x/misc.html#c.uv_timespec64_t}
[uv_timespec64_t]}. *)

val clock_gettime : [< `Monotonic | `Real_time ] -> (timespec, Error.t) result
(** Samples one of the high-resolution timers.
Binds {{:http://docs.libuv.org/en/v1.x/misc.html#c.uv_clock_gettime}
[uv_clock_gettime]}. See
{{:http://man7.org/linux/man-pages/man3/clock_gettime.3p.html}
[clock_gettime(3p)]}.
Requires Luv 0.5.13 and libuv 1.45.0.
{{!Luv.Require} Feature check}: [Luv.Require.(has clock_gettime)] *)

val sleep : int -> unit
(** Suspends the calling thread for at least the given number of milliseconds.
Expand Down

0 comments on commit df7d218

Please sign in to comment.