mirror of
https://github.com/janet-lang/janet
synced 2024-12-29 18:00:26 +00:00
Added 'source' argument to os/clock to select the clock source
This commit is contained in:
parent
61132d6c40
commit
aaf3d08bcd
@ -1278,14 +1278,32 @@ JANET_CORE_FN(os_time,
|
||||
}
|
||||
|
||||
JANET_CORE_FN(os_clock,
|
||||
"(os/clock)",
|
||||
"Return the number of whole + fractional seconds since some fixed point in time. The clock "
|
||||
"is guaranteed to be non-decreasing in real time.") {
|
||||
"(os/clock &opt source)",
|
||||
"Return the number of whole + fractional seconds of the requested clock source.\n\n"
|
||||
"The `source` argument is the identifier of the particular clock source to use, when not "
|
||||
"specified the default is `:realtime`:\n"
|
||||
"- :realtime: Return the real (i.e., wall-clock) time. This clock is affected by discontinuous "
|
||||
" jumps in the system time\n"
|
||||
"- :monotonic: Return the number of whole + fractional seconds since some fixed point in "
|
||||
" time. The clock is guaranteed to be non-decreasing in real time.\n"
|
||||
"- :cputime: Return the CPU time consumed by this process (i.e. all threads in the process)\n") {
|
||||
janet_sandbox_assert(JANET_SANDBOX_HRTIME);
|
||||
janet_fixarity(argc, 0);
|
||||
(void) argv;
|
||||
janet_arity(argc, 0, 1);
|
||||
enum JanetTimeSource source = JANET_TIME_REALTIME;
|
||||
if (argc == 1) {
|
||||
JanetKeyword sourcestr = janet_getkeyword(argv, 0);
|
||||
if (janet_cstrcmp(sourcestr, "realtime") == 0) {
|
||||
source = JANET_TIME_REALTIME;
|
||||
} else if (janet_cstrcmp(sourcestr, "monotonic") == 0) {
|
||||
source = JANET_TIME_MONOTONIC;
|
||||
} else if (janet_cstrcmp(sourcestr, "cputime") == 0) {
|
||||
source = JANET_TIME_CPUTIME;
|
||||
} else {
|
||||
janet_panicf("expected :real-time, :monotonic, or :cputime, got %v", argv[0]);
|
||||
}
|
||||
}
|
||||
struct timespec tv;
|
||||
if (janet_gettime(&tv)) janet_panic("could not get time");
|
||||
if (janet_gettime(&tv, source)) janet_panic("could not get time");
|
||||
double dtime = tv.tv_sec + (tv.tv_nsec / 1E9);
|
||||
return janet_wrap_number(dtime);
|
||||
}
|
||||
|
@ -901,8 +901,20 @@ int janet_gettime(struct timespec *spec) {
|
||||
return 0;
|
||||
}
|
||||
#else
|
||||
int janet_gettime(struct timespec *spec) {
|
||||
return clock_gettime(CLOCK_MONOTONIC, spec);
|
||||
int janet_gettime(struct timespec *spec, enum JanetTimeSource source) {
|
||||
clockid_t cid = JANET_TIME_REALTIME;
|
||||
switch (source) {
|
||||
case JANET_TIME_REALTIME:
|
||||
cid = CLOCK_REALTIME;
|
||||
break;
|
||||
case JANET_TIME_MONOTONIC:
|
||||
cid = CLOCK_MONOTONIC;
|
||||
break;
|
||||
case JANET_TIME_CPUTIME:
|
||||
cid = CLOCK_PROCESS_CPUTIME_ID;
|
||||
break;
|
||||
}
|
||||
return clock_gettime(cid, spec);
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
@ -126,7 +126,12 @@ void janet_core_cfuns_ext(JanetTable *env, const char *regprefix, const JanetReg
|
||||
|
||||
/* Clock gettime */
|
||||
#ifdef JANET_GETTIME
|
||||
int janet_gettime(struct timespec *spec);
|
||||
enum JanetTimeSource {
|
||||
JANET_TIME_REALTIME,
|
||||
JANET_TIME_MONOTONIC,
|
||||
JANET_TIME_CPUTIME
|
||||
};
|
||||
int janet_gettime(struct timespec *spec, enum JanetTimeSource source);
|
||||
#endif
|
||||
|
||||
/* strdup */
|
||||
|
Loading…
Reference in New Issue
Block a user