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

Unbox Unix.gettimeofday and Unix.time #9561

Merged
merged 1 commit into from
Jun 2, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions Changes
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ Working version
- #9075: define to_rev_seq in Set and Map modules.
(Sébastien Briais, review by Gabriel Scherer and Nicolás Ojeda Bär)

- #9561: Unbox Unix.gettimeofday and Unix.time
(Stephen Dolan, review by David Allsopp)

- #9571: Make at_exit and Printexc.register_printer thread-safe.
(Guillaume Munch-Maccagnoni, review by Gabriel Scherer and Xavier Leroy)

Expand Down
17 changes: 6 additions & 11 deletions otherlibs/unix/gettimeofday.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,17 @@
#include <caml/alloc.h>
#include <caml/fail.h>
#include "unixsupport.h"

#ifdef HAS_GETTIMEOFDAY

#include <sys/types.h>
#include <sys/time.h>

CAMLprim value unix_gettimeofday(value unit)
double unix_gettimeofday_unboxed(value unit)
{
struct timeval tp;
if (gettimeofday(&tp, NULL) == -1) uerror("gettimeofday", Nothing);
return caml_copy_double((double) tp.tv_sec + (double) tp.tv_usec / 1e6);
gettimeofday(&tp, NULL);
return ((double) tp.tv_sec + (double) tp.tv_usec / 1e6);
}

#else

CAMLprim value unix_gettimeofday(value unit)
{ caml_invalid_argument("gettimeofday not implemented"); }

#endif
{
return caml_copy_double(unix_gettimeofday_unboxed(unit));
}
7 changes: 6 additions & 1 deletion otherlibs/unix/time.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@
#include <caml/alloc.h>
#include "unixsupport.h"

double unix_time_unboxed(value unit)
{
return ((double) time((time_t *) NULL));
}

CAMLprim value unix_time(value unit)
{
return caml_copy_double((double) time((time_t *) NULL));
return caml_copy_double(unix_time_unboxed(unit));
}
6 changes: 4 additions & 2 deletions otherlibs/unix/unix.ml
Original file line number Diff line number Diff line change
Expand Up @@ -497,8 +497,10 @@ type tm =
tm_yday : int;
tm_isdst : bool }

external time : unit -> float = "unix_time"
external gettimeofday : unit -> float = "unix_gettimeofday"
external time : unit -> (float [@unboxed]) =
"unix_time" "unix_time_unboxed" [@@noalloc]
external gettimeofday : unit -> (float [@unboxed]) =
"unix_gettimeofday" "unix_gettimeofday_unboxed" [@@noalloc]
dra27 marked this conversation as resolved.
Show resolved Hide resolved
external gmtime : float -> tm = "unix_gmtime"
external localtime : float -> tm = "unix_localtime"
external mktime : tm -> float * tm = "unix_mktime"
Expand Down
9 changes: 7 additions & 2 deletions otherlibs/win32unix/gettimeofday.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
/* Unix epoch as a Windows timestamp in hundreds of ns */
#define epoch_ft 116444736000000000.0;

CAMLprim value unix_gettimeofday(value unit)
double unix_gettimeofday_unboxed(value unit)
{
FILETIME ft;
double tm;
Expand All @@ -36,5 +36,10 @@ CAMLprim value unix_gettimeofday(value unit)
#else
tm = *(uint64_t *)&ft - epoch_ft; /* shift to Epoch-relative time */
#endif
return caml_copy_double(tm * 1e-7); /* tm is in 100ns */
return (tm * 1e-7); /* tm is in 100ns */
}

CAMLprim value unix_gettimeofday(value unit)
{
return caml_copy_double(unix_gettimeofday_unboxed(unit));
}
6 changes: 4 additions & 2 deletions otherlibs/win32unix/unix.ml
Original file line number Diff line number Diff line change
Expand Up @@ -566,8 +566,10 @@ type tm =
tm_yday : int;
tm_isdst : bool }

external time : unit -> float = "unix_time"
external gettimeofday : unit -> float = "unix_gettimeofday"
external time : unit -> (float [@unboxed]) =
"unix_time" "unix_time_unboxed" [@@noalloc]
external gettimeofday : unit -> (float [@unboxed]) =
"unix_gettimeofday" "unix_gettimeofday_unboxed" [@@noalloc]
external gmtime : float -> tm = "unix_gmtime"
external localtime : float -> tm = "unix_localtime"
external mktime : tm -> float * tm = "unix_mktime"
Expand Down