3 * Copyright (c) 1996-2002 Douglas E. Wegscheid. All rights reserved.
5 * Copyright (c) 2002-2010 Jarkko Hietaniemi.
8 * Copyright (C) 2011, 2012, 2013 Andrew Main (Zefram) <zefram@fysh.org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the same terms as Perl itself.
17 #define PERL_NO_GET_CONTEXT
25 #if defined(__CYGWIN__) && defined(HAS_W32API_WINDOWS_H)
26 # include <w32api/windows.h>
27 # define CYGWIN_WITH_W32API
32 # include <sys/time.h>
36 # include <sys/select.h>
39 #if defined(TIME_HIRES_CLOCK_GETTIME_SYSCALL) || defined(TIME_HIRES_CLOCK_GETRES_SYSCALL)
46 #ifndef GCC_DIAG_IGNORE
47 # define GCC_DIAG_IGNORE(x)
48 # define GCC_DIAG_RESTORE
50 #ifndef GCC_DIAG_IGNORE_STMT
51 # define GCC_DIAG_IGNORE_STMT(x) GCC_DIAG_IGNORE(x) NOOP
52 # define GCC_DIAG_RESTORE_STMT GCC_DIAG_RESTORE NOOP
55 #if PERL_VERSION_GE(5,7,3) && !PERL_VERSION_GE(5,10,1)
57 # define SAVEOP() SAVEVPTR(PL_op)
60 #define IV_1E6 1000000
61 #define IV_1E7 10000000
62 #define IV_1E9 1000000000
64 #define NV_1E6 1000000.0
65 #define NV_1E7 10000000.0
66 #define NV_1E9 1000000000.0
68 #ifndef PerlProc_pause
69 # define PerlProc_pause() Pause()
75 # undef Pause /* In case perl.h did it already. */
76 # define Pause() sleep(~0) /* Zzz for a long time. */
79 /* Though the cpp define ITIMER_VIRTUAL is available the functionality
80 * is not supported in Cygwin as of August 2004, ditto for Win32.
81 * Neither are ITIMER_PROF or ITIMER_REALPROF implemented. --jhi
83 #if defined(__CYGWIN__) || defined(WIN32)
84 # undef ITIMER_VIRTUAL
86 # undef ITIMER_REALPROF
89 #ifndef TIME_HIRES_CLOCKID_T
90 typedef int clockid_t;
93 #if defined(TIME_HIRES_CLOCK_GETTIME) && defined(_STRUCT_ITIMERSPEC)
95 /* HP-UX has CLOCK_XXX values but as enums, not as defines.
96 * The only way to detect these would be to test compile for each. */
98 /* However, it seems that at least in HP-UX 11.31 ia64 there *are*
99 * defines for these, so let's try detecting them. */
100 # ifndef CLOCK_REALTIME
101 # define CLOCK_REALTIME CLOCK_REALTIME
102 # define CLOCK_VIRTUAL CLOCK_VIRTUAL
103 # define CLOCK_PROFILE CLOCK_PROFILE
105 # endif /* # ifdef __hpux */
107 #endif /* #if defined(TIME_HIRES_CLOCK_GETTIME) && defined(_STRUCT_ITIMERSPEC) */
109 #if defined(WIN32) || defined(CYGWIN_WITH_W32API)
111 # ifndef HAS_GETTIMEOFDAY
112 # define HAS_GETTIMEOFDAY
115 /* shows up in winsock.h?
123 unsigned __int64 ft_i64;
127 # define MY_CXT_KEY "Time::HiRes_" XS_VERSION
130 unsigned long run_count;
131 unsigned __int64 base_ticks;
132 unsigned __int64 tick_frequency;
133 FT_t base_systime_as_filetime;
134 unsigned __int64 reset_time;
137 /* Visual C++ 2013 and older don't have the timespec structure.
138 * Neither do mingw.org compilers with MinGW runtimes older than 3.22. */
139 # if((defined(_MSC_VER) && _MSC_VER < 1900) || \
140 (defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR) && \
141 defined(__MINGW32_MAJOR_VERSION) && (__MINGW32_MAJOR_VERSION < 3 || \
142 (__MINGW32_MAJOR_VERSION == 3 && __MINGW32_MINOR_VERSION < 22))))
151 /* Number of 100 nanosecond units from 1/1/1601 to 1/1/1970 */
153 # define Const64(x) x##LL
155 # define Const64(x) x##i64
157 # define EPOCH_BIAS Const64(116444736000000000)
161 # define IV_1E6LL 1000000LL /* Needed because of Const64() ##-appends LL (or i64). */
162 # define IV_1E7LL 10000000LL
163 # define IV_1E9LL 1000000000LL
165 # define IV_1E6i64 1000000i64
166 # define IV_1E7i64 10000000i64
167 # define IV_1E9i64 1000000000i64
171 /* NOTE: This does not compute the timezone info (doing so can be expensive,
172 * and appears to be unsupported even by glibc) */
174 /* dMY_CXT needs a Perl context and we don't want to call PERL_GET_CONTEXT
175 for performance reasons */
178 # define gettimeofday(tp, not_used) _gettimeofday(aTHX_ tp, not_used)
180 # undef GetSystemTimePreciseAsFileTime
181 # define GetSystemTimePreciseAsFileTime(out) _GetSystemTimePreciseAsFileTime(aTHX_ out)
183 # undef clock_gettime
184 # define clock_gettime(clock_id, tp) _clock_gettime(aTHX_ clock_id, tp)
187 # define clock_getres(clock_id, tp) _clock_getres(clock_id, tp)
189 # ifndef CLOCK_REALTIME
190 # define CLOCK_REALTIME 1
191 # define CLOCK_MONOTONIC 2
194 /* If the performance counter delta drifts more than 0.5 seconds from the
195 * system time then we recalibrate to the system time. This means we may
196 * move *backwards* in time! */
197 # define MAX_PERF_COUNTER_SKEW Const64(5000000) /* 0.5 seconds */
199 /* Reset reading from the performance counter every five minutes.
200 * Many PC clocks just seem to be so bad. */
201 # define MAX_PERF_COUNTER_TICKS Const64(300000000) /* 300 seconds */
204 * Windows 8 introduced GetSystemTimePreciseAsFileTime(), but currently we have
205 * to support older systems, so for now we provide our own implementation.
206 * In the future we will switch to the real deal.
209 _GetSystemTimePreciseAsFileTime(pTHX_ FILETIME *out)
214 if (MY_CXT.run_count++ == 0 ||
215 MY_CXT.base_systime_as_filetime.ft_i64 > MY_CXT.reset_time) {
217 QueryPerformanceFrequency((LARGE_INTEGER*)&MY_CXT.tick_frequency);
218 QueryPerformanceCounter((LARGE_INTEGER*)&MY_CXT.base_ticks);
219 GetSystemTimeAsFileTime(&MY_CXT.base_systime_as_filetime.ft_val);
220 ft.ft_i64 = MY_CXT.base_systime_as_filetime.ft_i64;
221 MY_CXT.reset_time = ft.ft_i64 + MAX_PERF_COUNTER_TICKS;
225 unsigned __int64 ticks;
226 QueryPerformanceCounter((LARGE_INTEGER*)&ticks);
227 ticks -= MY_CXT.base_ticks;
228 ft.ft_i64 = MY_CXT.base_systime_as_filetime.ft_i64
229 + Const64(IV_1E7) * (ticks / MY_CXT.tick_frequency)
230 +(Const64(IV_1E7) * (ticks % MY_CXT.tick_frequency)) / MY_CXT.tick_frequency;
231 diff = ft.ft_i64 - MY_CXT.base_systime_as_filetime.ft_i64;
232 if (diff < -MAX_PERF_COUNTER_SKEW || diff > MAX_PERF_COUNTER_SKEW) {
233 MY_CXT.base_ticks += ticks;
234 GetSystemTimeAsFileTime(&MY_CXT.base_systime_as_filetime.ft_val);
235 ft.ft_i64 = MY_CXT.base_systime_as_filetime.ft_i64;
245 _gettimeofday(pTHX_ struct timeval *tp, void *not_used)
249 PERL_UNUSED_ARG(not_used);
251 GetSystemTimePreciseAsFileTime(&ft.ft_val);
253 /* seconds since epoch */
254 tp->tv_sec = (long)((ft.ft_i64 - EPOCH_BIAS) / Const64(IV_1E7));
256 /* microseconds remaining */
257 tp->tv_usec = (long)((ft.ft_i64 / Const64(10)) % Const64(IV_1E6));
263 _clock_gettime(pTHX_ clockid_t clock_id, struct timespec *tp)
268 case CLOCK_REALTIME: {
271 GetSystemTimePreciseAsFileTime(&ft.ft_val);
272 tp->tv_sec = (time_t)((ft.ft_i64 - EPOCH_BIAS) / IV_1E7);
273 tp->tv_nsec = (long)((ft.ft_i64 % IV_1E7) * 100);
276 case CLOCK_MONOTONIC: {
277 unsigned __int64 freq, ticks;
279 QueryPerformanceFrequency((LARGE_INTEGER*)&freq);
280 QueryPerformanceCounter((LARGE_INTEGER*)&ticks);
282 tp->tv_sec = (time_t)(ticks / freq);
283 tp->tv_nsec = (long)((IV_1E9 * (ticks % freq)) / freq);
295 _clock_getres(clockid_t clock_id, struct timespec *tp)
297 unsigned __int64 freq, qpc_res_ns;
299 QueryPerformanceFrequency((LARGE_INTEGER*)&freq);
300 qpc_res_ns = IV_1E9 > freq ? IV_1E9 / freq : 1;
305 /* the resolution can't be smaller than 100ns because our implementation
306 * of CLOCK_REALTIME is using FILETIME internally */
307 tp->tv_nsec = (long)(qpc_res_ns > 100 ? qpc_res_ns : 100);
310 case CLOCK_MONOTONIC:
312 tp->tv_nsec = (long)qpc_res_ns;
323 #endif /* #if defined(WIN32) || defined(CYGWIN_WITH_W32API) */
325 /* Do not use H A S _ N A N O S L E E P
326 * so that Perl Configure doesn't scan for it (and pull in -lrt and
327 * the like which are not usually good ideas for the default Perl).
328 * (We are part of the core perl now.)
329 * The TIME_HIRES_NANOSLEEP is set by Makefile.PL. */
330 #if !defined(HAS_USLEEP) && defined(TIME_HIRES_NANOSLEEP)
332 # define usleep hrt_usleep /* could conflict with ncurses for static build */
335 hrt_usleep(unsigned long usec) /* This is used to emulate usleep. */
338 res.tv_sec = usec / IV_1E6;
339 res.tv_nsec = ( usec - res.tv_sec * IV_1E6 ) * 1000;
340 nanosleep(&res, NULL);
343 #endif /* #if !defined(HAS_USLEEP) && defined(TIME_HIRES_NANOSLEEP) */
345 #if !defined(HAS_USLEEP) && defined(HAS_SELECT)
346 # ifndef SELECT_IS_BROKEN
348 # define usleep hrt_usleep /* could conflict with ncurses for static build */
351 hrt_usleep(unsigned long usec)
356 select(0, (Select_fd_set_t)NULL, (Select_fd_set_t)NULL,
357 (Select_fd_set_t)NULL, &tv);
360 #endif /* #if !defined(HAS_USLEEP) && defined(HAS_SELECT) */
362 #if !defined(HAS_USLEEP) && defined(WIN32)
364 # define usleep hrt_usleep /* could conflict with ncurses for static build */
367 hrt_usleep(unsigned long usec)
373 #endif /* #if !defined(HAS_USLEEP) && defined(WIN32) */
375 #if !defined(HAS_USLEEP) && defined(HAS_POLL)
377 # define usleep hrt_usleep /* could conflict with ncurses for static build */
380 hrt_usleep(unsigned long usec)
382 int msec = usec / 1000;
386 #endif /* #if !defined(HAS_USLEEP) && defined(HAS_POLL) */
388 #if defined(HAS_SETITIMER) && defined(ITIMER_REAL)
391 hrt_ualarm_itimero(struct itimerval *oitv, int usec, int uinterval)
393 struct itimerval itv;
394 itv.it_value.tv_sec = usec / IV_1E6;
395 itv.it_value.tv_usec = usec % IV_1E6;
396 itv.it_interval.tv_sec = uinterval / IV_1E6;
397 itv.it_interval.tv_usec = uinterval % IV_1E6;
398 return setitimer(ITIMER_REAL, &itv, oitv);
401 #endif /* #if !defined(HAS_UALARM) && defined(HAS_SETITIMER) */
403 #if !defined(HAS_UALARM) && defined(HAS_SETITIMER)
405 # define ualarm hrt_ualarm_itimer /* could conflict with ncurses for static build */
408 #if !defined(HAS_UALARM) && defined(VMS)
410 # define ualarm vms_ualarm
412 # include <lib$routines.h>
414 # include <starlet.h>
415 # include <descrip.h>
420 # define VMSERR(s) (!((s)&1))
423 us_to_VMS(useconds_t mseconds, unsigned long v[])
432 iss = lib$addx(qq,qq,qq);
433 if (VMSERR(iss)) lib$signal(iss);
434 iss = lib$subx(v,qq,v);
435 if (VMSERR(iss)) lib$signal(iss);
436 iss = lib$addx(qq,qq,qq);
437 if (VMSERR(iss)) lib$signal(iss);
438 iss = lib$subx(v,qq,v);
439 if (VMSERR(iss)) lib$signal(iss);
440 iss = lib$subx(v,qq,v);
441 if (VMSERR(iss)) lib$signal(iss);
445 VMS_to_us(unsigned long v[])
448 unsigned long div=10,quot, rem;
450 iss = lib$ediv(&div,v,",&rem);
451 if (VMSERR(iss)) lib$signal(iss);
456 typedef unsigned short word;
457 typedef struct _ualarm {
460 unsigned long delay[2];
461 unsigned long interval[2];
462 unsigned long remain[2];
467 static Alarm *a0, alarm_base;
471 # define UAL_ACTIVE 4
472 static void ualarm_AST(Alarm *a);
475 vms_ualarm(int mseconds, int interval)
484 static struct item_list3 itmlst[2];
485 static int first = 1;
491 itmlst[0].code = JPI$_ASTEN;
492 itmlst[0].length = sizeof(asten);
493 itmlst[0].retlenaddr = NULL;
495 itmlst[1].length = 0;
496 itmlst[1].bufaddr = NULL;
497 itmlst[1].retlenaddr = NULL;
499 iss = lib$get_ef(&alarm_ef);
500 if (VMSERR(iss)) lib$signal(iss);
503 a0->function = UAL_NULL;
505 itmlst[0].bufaddr = &asten;
507 iss = sys$getjpiw(0,0,0,itmlst,0,0,0);
508 if (VMSERR(iss)) lib$signal(iss);
509 if (!(asten&0x08)) return -1;
513 a->function = UAL_SET;
515 a->function = UAL_CLEAR;
518 us_to_VMS(mseconds, a->delay);
520 us_to_VMS(interval, a->interval);
525 iss = sys$clref(alarm_ef);
526 if (VMSERR(iss)) lib$signal(iss);
528 iss = sys$dclast(ualarm_AST,a,0);
529 if (VMSERR(iss)) lib$signal(iss);
531 iss = sys$waitfr(alarm_ef);
532 if (VMSERR(iss)) lib$signal(iss);
534 if (a->function == UAL_ACTIVE)
535 return VMS_to_us(a->remain);
546 unsigned long now[2];
548 iss = sys$gettim(now);
549 if (VMSERR(iss)) lib$signal(iss);
551 if (a->function == UAL_SET || a->function == UAL_CLEAR) {
552 if (a0->function == UAL_ACTIVE) {
553 iss = sys$cantim(a0,PSL$C_USER);
554 if (VMSERR(iss)) lib$signal(iss);
556 iss = lib$subx(a0->remain, now, a->remain);
557 if (VMSERR(iss)) lib$signal(iss);
559 if (a->remain[1] & 0x80000000)
560 a->remain[0] = a->remain[1] = 0;
563 if (a->function == UAL_SET) {
564 a->function = a0->function;
565 a0->function = UAL_ACTIVE;
566 a0->repeat = a->repeat;
568 a0->interval[0] = a->interval[0];
569 a0->interval[1] = a->interval[1];
571 a0->delay[0] = a->delay[0];
572 a0->delay[1] = a->delay[1];
574 iss = lib$subx(now, a0->delay, a0->remain);
575 if (VMSERR(iss)) lib$signal(iss);
577 iss = sys$setimr(0,a0->delay,ualarm_AST,a0);
578 if (VMSERR(iss)) lib$signal(iss);
580 a->function = a0->function;
581 a0->function = UAL_NULL;
583 iss = sys$setef(alarm_ef);
584 if (VMSERR(iss)) lib$signal(iss);
585 } else if (a->function == UAL_ACTIVE) {
587 iss = lib$subx(now, a->interval, a->remain);
588 if (VMSERR(iss)) lib$signal(iss);
590 iss = sys$setimr(0,a->interval,ualarm_AST,a);
591 if (VMSERR(iss)) lib$signal(iss);
593 a->function = UAL_NULL;
596 if (VMSERR(iss)) lib$signal(iss);
597 lib$signal(SS$_ASTFLT);
599 lib$signal(SS$_BADPARAM);
603 #endif /* #if !defined(HAS_UALARM) && defined(VMS) */
605 #ifdef HAS_GETTIMEOFDAY
608 myU2time(pTHX_ UV *ret)
612 status = gettimeofday (&Tp, NULL);
626 status = gettimeofday (&Tp, NULL);
627 return status == 0 ? Tp.tv_sec + (Tp.tv_usec / NV_1E6) : -1.0;
630 #endif /* #ifdef HAS_GETTIMEOFDAY */
633 hrstatns(UV *atime_nsec, UV *mtime_nsec, UV *ctime_nsec)
636 #if TIME_HIRES_STAT == 1
637 *atime_nsec = PL_statcache.st_atimespec.tv_nsec;
638 *mtime_nsec = PL_statcache.st_mtimespec.tv_nsec;
639 *ctime_nsec = PL_statcache.st_ctimespec.tv_nsec;
640 #elif TIME_HIRES_STAT == 2
641 *atime_nsec = PL_statcache.st_atimensec;
642 *mtime_nsec = PL_statcache.st_mtimensec;
643 *ctime_nsec = PL_statcache.st_ctimensec;
644 #elif TIME_HIRES_STAT == 3
645 *atime_nsec = PL_statcache.st_atime_n;
646 *mtime_nsec = PL_statcache.st_mtime_n;
647 *ctime_nsec = PL_statcache.st_ctime_n;
648 #elif TIME_HIRES_STAT == 4
649 *atime_nsec = PL_statcache.st_atim.tv_nsec;
650 *mtime_nsec = PL_statcache.st_mtim.tv_nsec;
651 *ctime_nsec = PL_statcache.st_ctim.tv_nsec;
652 #elif TIME_HIRES_STAT == 5
653 *atime_nsec = PL_statcache.st_uatime * 1000;
654 *mtime_nsec = PL_statcache.st_umtime * 1000;
655 *ctime_nsec = PL_statcache.st_uctime * 1000;
656 #else /* !TIME_HIRES_STAT */
660 #endif /* !TIME_HIRES_STAT */
663 /* Until Apple implements clock_gettime()
664 * (ditto clock_getres() and clock_nanosleep())
665 * we will emulate them using the Mach kernel interfaces. */
666 #if defined(PERL_DARWIN) && \
667 (defined(TIME_HIRES_CLOCK_GETTIME_EMULATION) || \
668 defined(TIME_HIRES_CLOCK_GETRES_EMULATION) || \
669 defined(TIME_HIRES_CLOCK_NANOSLEEP_EMULATION))
671 # ifndef CLOCK_REALTIME
672 # define CLOCK_REALTIME 0x01
673 # define CLOCK_MONOTONIC 0x02
676 # ifndef TIMER_ABSTIME
677 # define TIMER_ABSTIME 0x01
681 # define PERL_DARWIN_MUTEX
684 # ifdef PERL_DARWIN_MUTEX
685 STATIC perl_mutex darwin_time_mutex;
688 # include <mach/mach_time.h>
690 static uint64_t absolute_time_init;
691 static mach_timebase_info_data_t timebase_info;
692 static struct timespec timespec_init;
694 static int darwin_time_init() {
697 # ifdef PERL_DARWIN_MUTEX
698 MUTEX_LOCK(&darwin_time_mutex);
700 if (absolute_time_init == 0) {
701 /* mach_absolute_time() cannot fail */
702 absolute_time_init = mach_absolute_time();
703 success = mach_timebase_info(&timebase_info) == KERN_SUCCESS;
705 success = gettimeofday(&tv, NULL) == 0;
707 timespec_init.tv_sec = tv.tv_sec;
708 timespec_init.tv_nsec = tv.tv_usec * 1000;
712 # ifdef PERL_DARWIN_MUTEX
713 MUTEX_UNLOCK(&darwin_time_mutex);
718 # ifdef TIME_HIRES_CLOCK_GETTIME_EMULATION
719 static int th_clock_gettime(clockid_t clock_id, struct timespec *ts) {
720 if (darwin_time_init() && timebase_info.denom) {
725 ((mach_absolute_time() - absolute_time_init) *
726 (uint64_t)timebase_info.numer) / (uint64_t)timebase_info.denom;
727 ts->tv_sec = timespec_init.tv_sec + nanos / IV_1E9;
728 ts->tv_nsec = timespec_init.tv_nsec + nanos % IV_1E9;
732 case CLOCK_MONOTONIC:
735 (mach_absolute_time() *
736 (uint64_t)timebase_info.numer) / (uint64_t)timebase_info.denom;
737 ts->tv_sec = nanos / IV_1E9;
738 ts->tv_nsec = nanos - ts->tv_sec * IV_1E9;
747 SETERRNO(EINVAL, LIB_INVARG);
751 # define clock_gettime(clock_id, ts) th_clock_gettime((clock_id), (ts))
753 # endif /* TIME_HIRES_CLOCK_GETTIME_EMULATION */
755 # ifdef TIME_HIRES_CLOCK_GETRES_EMULATION
756 static int th_clock_getres(clockid_t clock_id, struct timespec *ts) {
757 if (darwin_time_init() && timebase_info.denom) {
760 case CLOCK_MONOTONIC:
762 /* In newer kernels both the numer and denom are one,
763 * resulting in conversion factor of one, which is of
764 * course unrealistic. */
765 ts->tv_nsec = timebase_info.numer / timebase_info.denom;
772 SETERRNO(EINVAL, LIB_INVARG);
776 # define clock_getres(clock_id, ts) th_clock_getres((clock_id), (ts))
777 # endif /* TIME_HIRES_CLOCK_GETRES_EMULATION */
779 # ifdef TIME_HIRES_CLOCK_NANOSLEEP_EMULATION
780 static int th_clock_nanosleep(clockid_t clock_id, int flags,
781 const struct timespec *rqtp,
782 struct timespec *rmtp) {
783 if (darwin_time_init()) {
786 case CLOCK_MONOTONIC:
788 uint64_t nanos = rqtp->tv_sec * IV_1E9 + rqtp->tv_nsec;
790 if ((flags & TIMER_ABSTIME)) {
792 timespec_init.tv_sec * IV_1E9 + timespec_init.tv_nsec;
793 nanos = nanos > back ? nanos - back : 0;
796 mach_wait_until(mach_absolute_time() + nanos) == KERN_SUCCESS;
798 /* In the relative sleep, the rmtp should be filled in with
799 * the 'unused' part of the rqtp in case the sleep gets
800 * interrupted by a signal. But it is unknown how signals
801 * interact with mach_wait_until(). In the absolute sleep,
802 * the rmtp should stay untouched. */
814 SETERRNO(EINVAL, LIB_INVARG);
818 # define clock_nanosleep(clock_id, flags, rqtp, rmtp) \
819 th_clock_nanosleep((clock_id), (flags), (rqtp), (rmtp))
821 # endif /* TIME_HIRES_CLOCK_NANOSLEEP_EMULATION */
823 #endif /* PERL_DARWIN */
825 /* The macOS headers warn about using certain interfaces in
826 * OS-release-ignorant manner, for example:
828 * warning: 'futimens' is only available on macOS 10.13 or newer
829 * [-Wunguarded-availability-new]
831 * (ditto for utimensat)
833 * There is clang __builtin_available() *runtime* check for this.
834 * The gotchas are that neither __builtin_available() nor __has_builtin()
835 * are always available.
837 #ifndef __has_builtin
838 # define __has_builtin(x) 0 /* non-clang */
841 # if defined(PERL_DARWIN) && __has_builtin(__builtin_available)
842 # define FUTIMENS_AVAILABLE __builtin_available(macOS 10.13, *)
844 # define FUTIMENS_AVAILABLE 1
847 # define FUTIMENS_AVAILABLE 0
850 # if defined(PERL_DARWIN) && __has_builtin(__builtin_available)
851 # define UTIMENSAT_AVAILABLE __builtin_available(macOS 10.13, *)
853 # define UTIMENSAT_AVAILABLE 1
856 # define UTIMENSAT_AVAILABLE 0
859 #include "const-c.inc"
861 #if (defined(TIME_HIRES_NANOSLEEP)) || \
862 (defined(TIME_HIRES_CLOCK_NANOSLEEP) && defined(TIMER_ABSTIME))
865 nanosleep_init(NV nsec,
866 struct timespec *sleepfor,
867 struct timespec *unslept) {
868 sleepfor->tv_sec = (Time_t)(nsec / NV_1E9);
869 sleepfor->tv_nsec = (long)(nsec - ((NV)sleepfor->tv_sec) * NV_1E9);
871 unslept->tv_nsec = 0;
875 nsec_without_unslept(struct timespec *sleepfor,
876 const struct timespec *unslept) {
877 if (sleepfor->tv_sec >= unslept->tv_sec) {
878 sleepfor->tv_sec -= unslept->tv_sec;
879 if (sleepfor->tv_nsec >= unslept->tv_nsec) {
880 sleepfor->tv_nsec -= unslept->tv_nsec;
881 } else if (sleepfor->tv_sec > 0) {
883 sleepfor->tv_nsec += IV_1E9;
884 sleepfor->tv_nsec -= unslept->tv_nsec;
886 sleepfor->tv_sec = 0;
887 sleepfor->tv_nsec = 0;
890 sleepfor->tv_sec = 0;
891 sleepfor->tv_nsec = 0;
893 return ((NV)sleepfor->tv_sec) * NV_1E9 + ((NV)sleepfor->tv_nsec);
898 /* In case Perl and/or Devel::PPPort are too old, minimally emulate
899 * IS_SAFE_PATHNAME() (which looks for zero bytes in the pathname). */
900 #ifndef IS_SAFE_PATHNAME
901 # if PERL_VERSION_GE(5,12,0) /* Perl_ck_warner is 5.10.0 -> */
902 # ifdef WARN_SYSCALLS
903 # define WARNEMUCAT WARN_SYSCALLS /* 5.22.0 -> */
905 # define WARNEMUCAT WARN_MISC
907 # define WARNEMU(opname) Perl_ck_warner(aTHX_ packWARN(WARNEMUCAT), "Invalid \\0 character in pathname for %s",opname)
909 # define WARNEMU(opname) Perl_warn(aTHX_ "Invalid \\0 character in pathname for %s",opname)
911 # define IS_SAFE_PATHNAME(pv, len, opname) (((len)>1)&&memchr((pv), 0, (len)-1)?(SETERRNO(ENOENT, LIB_INVARG),WARNEMU(opname),FALSE):(TRUE))
914 MODULE = Time::HiRes PACKAGE = Time::HiRes
923 #ifdef HAS_GETTIMEOFDAY
925 (void) hv_store(PL_modglobal, "Time::NVtime", 12,
926 newSViv(PTR2IV(myNVtime)), 0);
927 (void) hv_store(PL_modglobal, "Time::U2time", 12,
928 newSViv(PTR2IV(myU2time)), 0);
931 #if defined(PERL_DARWIN)
932 # if defined(USE_ITHREADS) && defined(PERL_DARWIN_MUTEX)
933 MUTEX_INIT(&darwin_time_mutex);
938 #if defined(USE_ITHREADS) && defined(MY_CXT_KEY)
947 INCLUDE: const-xs.inc
949 #if defined(HAS_USLEEP) && defined(HAS_GETTIMEOFDAY)
955 struct timeval Ta, Tb;
957 gettimeofday(&Ta, NULL);
959 if (useconds >= NV_1E6) {
960 IV seconds = (IV) (useconds / NV_1E6);
961 /* If usleep() has been implemented using setitimer()
962 * then this contortion is unnecessary-- but usleep()
963 * may be implemented in some other way, so let's contort. */
966 useconds -= NV_1E6 * seconds;
968 } else if (useconds < 0.0)
969 croak("Time::HiRes::usleep(%" NVgf
970 "): negative time not invented yet", useconds);
972 usleep((U32)useconds);
976 gettimeofday(&Tb, NULL);
978 printf("[%ld %ld] [%ld %ld]\n", Tb.tv_sec, Tb.tv_usec, Ta.tv_sec, Ta.tv_usec);
980 RETVAL = NV_1E6*(Tb.tv_sec-Ta.tv_sec)+(NV)((IV)Tb.tv_usec-(IV)Ta.tv_usec);
985 # if defined(TIME_HIRES_NANOSLEEP)
991 struct timespec sleepfor, unslept;
994 croak("Time::HiRes::nanosleep(%" NVgf
995 "): negative time not invented yet", nsec);
996 nanosleep_init(nsec, &sleepfor, &unslept);
997 if (nanosleep(&sleepfor, &unslept) == 0) {
1000 RETVAL = nsec_without_unslept(&sleepfor, &unslept);
1005 # else /* #if defined(TIME_HIRES_NANOSLEEP) */
1011 PERL_UNUSED_ARG(nsec);
1012 croak("Time::HiRes::nanosleep(): unimplemented in this platform");
1017 # endif /* #if defined(TIME_HIRES_NANOSLEEP) */
1022 struct timeval Ta, Tb;
1024 gettimeofday(&Ta, NULL);
1026 NV seconds = SvNV(ST(0));
1027 if (seconds >= 0.0) {
1028 UV useconds = (UV)(1E6 * (seconds - (UV)seconds));
1030 sleep((U32)seconds);
1031 if ((IV)useconds < 0) {
1032 # if defined(__sparc64__) && defined(__GNUC__)
1033 /* Sparc64 gcc 2.95.3 (e.g. on NetBSD) has a bug
1034 * where (0.5 - (UV)(0.5)) will under certain
1035 * circumstances (if the double is cast to UV more
1036 * than once?) evaluate to -0.5, instead of 0.5. */
1037 useconds = -(IV)useconds;
1038 # endif /* #if defined(__sparc64__) && defined(__GNUC__) */
1039 if ((IV)useconds < 0)
1040 croak("Time::HiRes::sleep(%" NVgf
1041 "): internal error: useconds < 0 (unsigned %" UVuf
1042 " signed %" IVdf ")",
1043 seconds, useconds, (IV)useconds);
1047 croak("Time::HiRes::sleep(%" NVgf
1048 "): negative time not invented yet", seconds);
1052 gettimeofday(&Tb, NULL);
1054 printf("[%ld %ld] [%ld %ld]\n", Tb.tv_sec, Tb.tv_usec, Ta.tv_sec, Ta.tv_usec);
1056 RETVAL = (NV)(Tb.tv_sec-Ta.tv_sec)+0.000001*(NV)(Tb.tv_usec-Ta.tv_usec);
1061 #else /* #if defined(HAS_USLEEP) && defined(HAS_GETTIMEOFDAY) */
1067 PERL_UNUSED_ARG(useconds);
1068 croak("Time::HiRes::usleep(): unimplemented in this platform");
1073 #endif /* #if defined(HAS_USLEEP) && defined(HAS_GETTIMEOFDAY) */
1078 ualarm(useconds,uinterval=0)
1082 if (useconds < 0 || uinterval < 0)
1083 croak("Time::HiRes::ualarm(%d, %d): negative time not invented yet", useconds, uinterval);
1084 # if defined(HAS_SETITIMER) && defined(ITIMER_REAL)
1086 struct itimerval itv;
1087 if (hrt_ualarm_itimero(&itv, useconds, uinterval)) {
1088 /* To conform to ualarm's interface, we're actually ignoring
1092 RETVAL = itv.it_value.tv_sec * IV_1E6 + itv.it_value.tv_usec;
1096 if (useconds >= IV_1E6 || uinterval >= IV_1E6)
1097 croak("Time::HiRes::ualarm(%d, %d): useconds or uinterval"
1098 " equal to or more than %" IVdf,
1099 useconds, uinterval, IV_1E6);
1101 RETVAL = ualarm(useconds, uinterval);
1108 alarm(seconds,interval=0)
1112 if (seconds < 0.0 || interval < 0.0)
1113 croak("Time::HiRes::alarm(%" NVgf ", %" NVgf
1114 "): negative time not invented yet", seconds, interval);
1117 IV iseconds = (IV)seconds;
1118 IV iinterval = (IV)interval;
1119 NV fseconds = seconds - iseconds;
1120 NV finterval = interval - iinterval;
1121 IV useconds, uinterval;
1122 if (fseconds >= 1.0 || finterval >= 1.0)
1123 croak("Time::HiRes::alarm(%" NVgf ", %" NVgf
1124 "): seconds or interval too large to split correctly",
1127 useconds = IV_1E6 * fseconds;
1128 uinterval = IV_1E6 * finterval;
1129 # if defined(HAS_SETITIMER) && defined(ITIMER_REAL)
1131 struct itimerval nitv, oitv;
1132 nitv.it_value.tv_sec = iseconds;
1133 nitv.it_value.tv_usec = useconds;
1134 nitv.it_interval.tv_sec = iinterval;
1135 nitv.it_interval.tv_usec = uinterval;
1136 if (setitimer(ITIMER_REAL, &nitv, &oitv)) {
1137 /* To conform to alarm's interface, we're actually ignoring
1141 RETVAL = oitv.it_value.tv_sec + ((NV)oitv.it_value.tv_usec) / NV_1E6;
1145 if (iseconds || iinterval)
1146 croak("Time::HiRes::alarm(%" NVgf ", %" NVgf
1147 "): seconds or interval equal to or more than 1.0 ",
1150 RETVAL = (NV)ualarm( useconds, uinterval ) / NV_1E6;
1157 #else /* #ifdef HAS_UALARM */
1160 ualarm(useconds,interval=0)
1164 PERL_UNUSED_ARG(useconds);
1165 PERL_UNUSED_ARG(interval);
1166 croak("Time::HiRes::ualarm(): unimplemented in this platform");
1172 alarm(seconds,interval=0)
1176 PERL_UNUSED_ARG(seconds);
1177 PERL_UNUSED_ARG(interval);
1178 croak("Time::HiRes::alarm(): unimplemented in this platform");
1183 #endif /* #ifdef HAS_UALARM */
1185 #ifdef HAS_GETTIMEOFDAY
1193 status = gettimeofday (&Tp, NULL);
1195 if (GIMME == G_ARRAY) {
1197 PUSHs(sv_2mortal(newSViv(Tp.tv_sec)));
1198 PUSHs(sv_2mortal(newSViv(Tp.tv_usec)));
1201 PUSHs(sv_2mortal(newSVnv(Tp.tv_sec + (Tp.tv_usec / NV_1E6))));
1211 status = gettimeofday (&Tp, NULL);
1213 RETVAL = Tp.tv_sec + (Tp.tv_usec / NV_1E6);
1220 #endif /* #ifdef HAS_GETTIMEOFDAY */
1222 #if defined(HAS_GETITIMER) && defined(HAS_SETITIMER)
1224 # define TV2NV(tv) ((NV)((tv).tv_sec) + 0.000001 * (NV)((tv).tv_usec))
1227 setitimer(which, seconds, interval = 0)
1232 struct itimerval newit;
1233 struct itimerval oldit;
1235 if (seconds < 0.0 || interval < 0.0)
1236 croak("Time::HiRes::setitimer(%" IVdf ", %" NVgf ", %" NVgf
1237 "): negative time not invented yet",
1238 (IV)which, seconds, interval);
1239 newit.it_value.tv_sec = (IV)seconds;
1240 newit.it_value.tv_usec =
1241 (IV)((seconds - (NV)newit.it_value.tv_sec) * NV_1E6);
1242 newit.it_interval.tv_sec = (IV)interval;
1243 newit.it_interval.tv_usec =
1244 (IV)((interval - (NV)newit.it_interval.tv_sec) * NV_1E6);
1245 /* on some platforms the 1st arg to setitimer is an enum, which
1246 * causes -Wc++-compat to complain about passing an int instead
1248 GCC_DIAG_IGNORE_STMT(-Wc++-compat);
1249 if (setitimer(which, &newit, &oldit) == 0) {
1251 PUSHs(sv_2mortal(newSVnv(TV2NV(oldit.it_value))));
1252 if (GIMME == G_ARRAY) {
1254 PUSHs(sv_2mortal(newSVnv(TV2NV(oldit.it_interval))));
1257 GCC_DIAG_RESTORE_STMT;
1263 struct itimerval nowit;
1265 /* on some platforms the 1st arg to getitimer is an enum, which
1266 * causes -Wc++-compat to complain about passing an int instead
1268 GCC_DIAG_IGNORE_STMT(-Wc++-compat);
1269 if (getitimer(which, &nowit) == 0) {
1271 PUSHs(sv_2mortal(newSVnv(TV2NV(nowit.it_value))));
1272 if (GIMME == G_ARRAY) {
1274 PUSHs(sv_2mortal(newSVnv(TV2NV(nowit.it_interval))));
1277 GCC_DIAG_RESTORE_STMT;
1279 #endif /* #if defined(HAS_GETITIMER) && defined(HAS_SETITIMER) */
1281 #if defined(TIME_HIRES_UTIME)
1284 utime(accessed, modified, ...)
1291 struct timespec utbuf[2];
1292 struct timespec *utbufp = utbuf;
1301 if ( accessed == &PL_sv_undef && modified == &PL_sv_undef )
1304 if (SvNV(accessed) < 0.0 || SvNV(modified) < 0.0)
1305 croak("Time::HiRes::utime(%" NVgf ", %" NVgf
1306 "): negative time not invented yet",
1307 SvNV(accessed), SvNV(modified));
1308 Zero(&utbuf, sizeof utbuf, char);
1310 utbuf[0].tv_sec = (Time_t)SvNV(accessed); /* time accessed */
1311 utbuf[0].tv_nsec = (long)(
1312 (SvNV(accessed) - (NV)utbuf[0].tv_sec)
1313 * NV_1E9 + (NV)0.5);
1315 utbuf[1].tv_sec = (Time_t)SvNV(modified); /* time modified */
1316 utbuf[1].tv_nsec = (long)(
1317 (SvNV(modified) - (NV)utbuf[1].tv_sec)
1318 * NV_1E9 + (NV)0.5);
1322 file = POPs; items--;
1324 if (SvROK(file) && GvIO(SvRV(file)) && IoIFP(sv_2io(SvRV(file)))) {
1325 int fd = PerlIO_fileno(IoIFP(sv_2io(file)));
1327 SETERRNO(EBADF,RMS_IFI);
1329 # ifdef HAS_FUTIMENS
1330 if (FUTIMENS_AVAILABLE) {
1331 if (futimens(fd, utbufp) == 0) {
1335 croak("futimens unimplemented in this platform");
1337 # else /* HAS_FUTIMENS */
1338 croak("futimens unimplemented in this platform");
1339 # endif /* HAS_FUTIMENS */
1343 # ifdef HAS_UTIMENSAT
1344 if (UTIMENSAT_AVAILABLE) {
1346 char * name = SvPV(file, len);
1347 if (IS_SAFE_PATHNAME(name, len, "utime") &&
1348 utimensat(AT_FDCWD, name, utbufp, 0) == 0) {
1353 croak("utimensat unimplemented in this platform");
1355 # else /* HAS_UTIMENSAT */
1356 croak("utimensat unimplemented in this platform");
1357 # endif /* HAS_UTIMENSAT */
1365 #else /* #if defined(TIME_HIRES_UTIME) */
1368 utime(accessed, modified, ...)
1370 croak("Time::HiRes::utime(): unimplemented in this platform");
1375 #endif /* #if defined(TIME_HIRES_UTIME) */
1377 #if defined(TIME_HIRES_CLOCK_GETTIME)
1380 clock_gettime(clock_id = CLOCK_REALTIME)
1386 # ifdef TIME_HIRES_CLOCK_GETTIME_SYSCALL
1387 status = syscall(SYS_clock_gettime, clock_id, &ts);
1389 status = clock_gettime(clock_id, &ts);
1391 RETVAL = status == 0 ? ts.tv_sec + (NV) ts.tv_nsec / NV_1E9 : -1;
1396 #else /* if defined(TIME_HIRES_CLOCK_GETTIME) */
1399 clock_gettime(clock_id = 0)
1402 PERL_UNUSED_ARG(clock_id);
1403 croak("Time::HiRes::clock_gettime(): unimplemented in this platform");
1408 #endif /* #if defined(TIME_HIRES_CLOCK_GETTIME) */
1410 #if defined(TIME_HIRES_CLOCK_GETRES)
1413 clock_getres(clock_id = CLOCK_REALTIME)
1419 # ifdef TIME_HIRES_CLOCK_GETRES_SYSCALL
1420 status = syscall(SYS_clock_getres, clock_id, &ts);
1422 status = clock_getres(clock_id, &ts);
1424 RETVAL = status == 0 ? ts.tv_sec + (NV) ts.tv_nsec / NV_1E9 : -1;
1429 #else /* if defined(TIME_HIRES_CLOCK_GETRES) */
1432 clock_getres(clock_id = 0)
1435 PERL_UNUSED_ARG(clock_id);
1436 croak("Time::HiRes::clock_getres(): unimplemented in this platform");
1441 #endif /* #if defined(TIME_HIRES_CLOCK_GETRES) */
1443 #if defined(TIME_HIRES_CLOCK_NANOSLEEP) && defined(TIMER_ABSTIME)
1446 clock_nanosleep(clock_id, nsec, flags = 0)
1451 struct timespec sleepfor, unslept;
1454 croak("Time::HiRes::clock_nanosleep(..., %" NVgf
1455 "): negative time not invented yet", nsec);
1456 nanosleep_init(nsec, &sleepfor, &unslept);
1457 if (clock_nanosleep(clock_id, flags, &sleepfor, &unslept) == 0) {
1460 RETVAL = nsec_without_unslept(&sleepfor, &unslept);
1465 #else /* if defined(TIME_HIRES_CLOCK_NANOSLEEP) && defined(TIMER_ABSTIME) */
1468 clock_nanosleep(clock_id, nsec, flags = 0)
1473 PERL_UNUSED_ARG(clock_id);
1474 PERL_UNUSED_ARG(nsec);
1475 PERL_UNUSED_ARG(flags);
1476 croak("Time::HiRes::clock_nanosleep(): unimplemented in this platform");
1481 #endif /* #if defined(TIME_HIRES_CLOCK_NANOSLEEP) && defined(TIMER_ABSTIME) */
1483 #if defined(TIME_HIRES_CLOCK) && defined(CLOCKS_PER_SEC)
1491 RETVAL = clocks == (clock_t) -1 ? (clock_t) -1 : (NV)clocks / (NV)CLOCKS_PER_SEC;
1496 #else /* if defined(TIME_HIRES_CLOCK) && defined(CLOCKS_PER_SEC) */
1501 croak("Time::HiRes::clock(): unimplemented in this platform");
1506 #endif /* #if defined(TIME_HIRES_CLOCK) && defined(CLOCKS_PER_SEC) */
1515 Time::HiRes::lstat = 1
1517 XPUSHs(sv_2mortal(newSVsv(items == 1 ? ST(0) : DEFSV)));
1520 PL_laststatval = -1;
1522 Zero(&fakeop, 1, OP);
1523 fakeop.op_type = ix ? OP_LSTAT : OP_STAT;
1524 fakeop.op_ppaddr = PL_ppaddr[fakeop.op_type];
1525 fakeop.op_flags = GIMME_V == G_ARRAY ? OPf_WANT_LIST :
1526 GIMME_V == G_SCALAR ? OPf_WANT_SCALAR : OPf_WANT_VOID;
1528 (void)fakeop.op_ppaddr(aTHX);
1531 nret = SP+1 - &ST(0);
1533 UV atime = SvUV(ST( 8));
1534 UV mtime = SvUV(ST( 9));
1535 UV ctime = SvUV(ST(10));
1539 hrstatns(&atime_nsec, &mtime_nsec, &ctime_nsec);
1541 ST( 8) = sv_2mortal(newSVnv(atime + (NV) atime_nsec / NV_1E9));
1543 ST( 9) = sv_2mortal(newSVnv(mtime + (NV) mtime_nsec / NV_1E9));
1545 ST(10) = sv_2mortal(newSVnv(ctime + (NV) ctime_nsec / NV_1E9));