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
22 #if defined(__CYGWIN__) && defined(HAS_W32API_WINDOWS_H)
23 # include <w32api/windows.h>
24 # define CYGWIN_WITH_W32API
29 # include <sys/time.h>
33 # include <sys/select.h>
36 #if defined(TIME_HIRES_CLOCK_GETTIME_SYSCALL) || defined(TIME_HIRES_CLOCK_GETRES_SYSCALL)
43 #define PERL_VERSION_DECIMAL(r,v,s) (r*1000000 + v*1000 + s)
44 #define PERL_DECIMAL_VERSION \
45 PERL_VERSION_DECIMAL(PERL_REVISION,PERL_VERSION,PERL_SUBVERSION)
46 #define PERL_VERSION_GE(r,v,s) \
47 (PERL_DECIMAL_VERSION >= PERL_VERSION_DECIMAL(r,v,s))
49 /* At least ppport.h 3.13 gets this wrong: one really cannot
50 * have NVgf as anything else than "g" under Perl 5.6.x. */
51 #if PERL_REVISION == 5 && PERL_VERSION == 6
56 #if PERL_VERSION_GE(5,7,3) && !PERL_VERSION_GE(5,10,1)
58 # define SAVEOP() SAVEVPTR(PL_op)
61 #define IV_1E6 1000000
62 #define IV_1E7 10000000
63 #define IV_1E9 1000000000
65 #define NV_1E6 1000000.0
66 #define NV_1E7 10000000.0
67 #define NV_1E9 1000000000.0
69 #ifndef PerlProc_pause
70 # define PerlProc_pause() Pause()
76 # undef Pause /* In case perl.h did it already. */
77 # define Pause() sleep(~0) /* Zzz for a long time. */
80 /* Though the cpp define ITIMER_VIRTUAL is available the functionality
81 * is not supported in Cygwin as of August 2004, ditto for Win32.
82 * Neither are ITIMER_PROF or ITIMER_REALPROF implemented. --jhi
84 #if defined(__CYGWIN__) || defined(WIN32)
85 # undef ITIMER_VIRTUAL
87 # undef ITIMER_REALPROF
90 #if defined(TIME_HIRES_CLOCK_GETTIME) && defined(_STRUCT_ITIMERSPEC)
92 /* HP-UX has CLOCK_XXX values but as enums, not as defines.
93 * The only way to detect these would be to test compile for each. */
95 /* However, it seems that at least in HP-UX 11.31 ia64 there *are*
96 * defines for these, so let's try detecting them. */
97 # ifndef CLOCK_REALTIME
98 # define CLOCK_REALTIME CLOCK_REALTIME
99 # define CLOCK_VIRTUAL CLOCK_VIRTUAL
100 # define CLOCK_PROFILE CLOCK_PROFILE
102 # endif /* # ifdef __hpux */
104 #endif /* #if defined(TIME_HIRES_CLOCK_GETTIME) && defined(_STRUCT_ITIMERSPEC) */
106 #if defined(WIN32) || defined(CYGWIN_WITH_W32API)
108 #ifndef HAS_GETTIMEOFDAY
109 # define HAS_GETTIMEOFDAY
112 /* shows up in winsock.h?
120 unsigned __int64 ft_i64;
124 #define MY_CXT_KEY "Time::HiRes_" XS_VERSION
127 unsigned long run_count;
128 unsigned __int64 base_ticks;
129 unsigned __int64 tick_frequency;
130 FT_t base_systime_as_filetime;
131 unsigned __int64 reset_time;
136 /* Number of 100 nanosecond units from 1/1/1601 to 1/1/1970 */
138 # define Const64(x) x##LL
140 # define Const64(x) x##i64
142 #define EPOCH_BIAS Const64(116444736000000000)
146 # define IV_1E6LL 1000000LL /* Needed because of Const64() ##-appends LL (or i64). */
147 # define IV_1E7LL 10000000LL
148 # define IV_1E9LL 1000000000LL
150 # define IV_1E6i64 1000000i64
151 # define IV_1E7i64 10000000i64
152 # define IV_1E9i64 1000000000i64
156 /* NOTE: This does not compute the timezone info (doing so can be expensive,
157 * and appears to be unsupported even by glibc) */
159 /* dMY_CXT needs a Perl context and we don't want to call PERL_GET_CONTEXT
160 for performance reasons */
163 #define gettimeofday(tp, not_used) _gettimeofday(aTHX_ tp, not_used)
165 /* If the performance counter delta drifts more than 0.5 seconds from the
166 * system time then we recalibrate to the system time. This means we may
167 * move *backwards* in time! */
168 #define MAX_PERF_COUNTER_SKEW Const64(5000000) /* 0.5 seconds */
170 /* Reset reading from the performance counter every five minutes.
171 * Many PC clocks just seem to be so bad. */
172 #define MAX_PERF_COUNTER_TICKS Const64(300000000) /* 300 seconds */
175 _gettimeofday(pTHX_ struct timeval *tp, void *not_used)
179 unsigned __int64 ticks;
182 if (MY_CXT.run_count++ == 0 ||
183 MY_CXT.base_systime_as_filetime.ft_i64 > MY_CXT.reset_time) {
184 QueryPerformanceFrequency((LARGE_INTEGER*)&MY_CXT.tick_frequency);
185 QueryPerformanceCounter((LARGE_INTEGER*)&MY_CXT.base_ticks);
186 GetSystemTimeAsFileTime(&MY_CXT.base_systime_as_filetime.ft_val);
187 ft.ft_i64 = MY_CXT.base_systime_as_filetime.ft_i64;
188 MY_CXT.reset_time = ft.ft_i64 + MAX_PERF_COUNTER_TICKS;
192 QueryPerformanceCounter((LARGE_INTEGER*)&ticks);
193 ticks -= MY_CXT.base_ticks;
194 ft.ft_i64 = MY_CXT.base_systime_as_filetime.ft_i64
195 + Const64(IV_1E7) * (ticks / MY_CXT.tick_frequency)
196 +(Const64(IV_1E7) * (ticks % MY_CXT.tick_frequency)) / MY_CXT.tick_frequency;
197 diff = ft.ft_i64 - MY_CXT.base_systime_as_filetime.ft_i64;
198 if (diff < -MAX_PERF_COUNTER_SKEW || diff > MAX_PERF_COUNTER_SKEW) {
199 MY_CXT.base_ticks += ticks;
200 GetSystemTimeAsFileTime(&MY_CXT.base_systime_as_filetime.ft_val);
201 ft.ft_i64 = MY_CXT.base_systime_as_filetime.ft_i64;
205 /* seconds since epoch */
206 tp->tv_sec = (long)((ft.ft_i64 - EPOCH_BIAS) / Const64(IV_1E7));
208 /* microseconds remaining */
209 tp->tv_usec = (long)((ft.ft_i64 / Const64(10)) % Const64(IV_1E6));
215 #if defined(WIN32) && !defined(ATLEASTFIVEOHOHFIVE)
217 sleep(unsigned int t)
224 #if !defined(HAS_GETTIMEOFDAY) && defined(VMS)
225 #define HAS_GETTIMEOFDAY
228 #include <time.h> /* gettimeofday */
229 #include <stdlib.h> /* qdiv */
230 #include <starlet.h> /* sys$gettim */
233 #include <lib$routines.h> /* lib$ediv() */
237 VMS binary time is expressed in 100 nano-seconds since
238 system base time which is 17-NOV-1858 00:00:00.00
241 #define DIV_100NS_TO_SECS 10000000L
242 #define DIV_100NS_TO_USECS 10L
245 gettimeofday is supposed to return times since the epoch
246 so need to determine this in terms of VMS base time
248 static $DESCRIPTOR(dscepoch,"01-JAN-1970 00:00:00.00");
251 static long base_adjust[2]={0L,0L};
253 static __int64 base_adjust=0;
258 If we don't have gettimeofday, then likely we are on a VMS machine that
259 operates on local time rather than UTC...so we have to zone-adjust.
260 This code gleefully swiped from VMS.C
263 /* method used to handle UTC conversions:
264 * 1 == CRTL gmtime(); 2 == SYS$TIMEZONE_DIFFERENTIAL; 3 == no correction
266 static int gmtime_emulation_type;
267 /* number of secs to add to UTC POSIX-style time to get local time */
268 static long int utc_offset_secs;
269 static struct dsc$descriptor_s fildevdsc =
270 { 12, DSC$K_DTYPE_T, DSC$K_CLASS_S, "LNM$FILE_DEV" };
271 static struct dsc$descriptor_s *fildev[] = { &fildevdsc, NULL };
273 static time_t toutc_dst(time_t loc) {
276 if ((rsltmp = localtime(&loc)) == NULL) return -1;
277 loc -= utc_offset_secs;
278 if (rsltmp->tm_isdst) loc -= 3600;
282 static time_t toloc_dst(time_t utc) {
285 utc += utc_offset_secs;
286 if ((rsltmp = localtime(&utc)) == NULL) return -1;
287 if (rsltmp->tm_isdst) utc += 3600;
291 #define _toutc(secs) ((secs) == (time_t) -1 ? (time_t) -1 : \
292 ((gmtime_emulation_type || timezone_setup()), \
293 (gmtime_emulation_type == 1 ? toutc_dst(secs) : \
294 ((secs) - utc_offset_secs))))
296 #define _toloc(secs) ((secs) == (time_t) -1 ? (time_t) -1 : \
297 ((gmtime_emulation_type || timezone_setup()), \
298 (gmtime_emulation_type == 1 ? toloc_dst(secs) : \
299 ((secs) + utc_offset_secs))))
306 if (gmtime_emulation_type == 0) {
308 time_t base = 15 * 86400; /* 15jan71; to avoid month/year ends between */
309 /* results of calls to gmtime() and localtime() */
312 gmtime_emulation_type++;
313 if ((tm_p = gmtime(&base)) == NULL) { /* CRTL gmtime() is a fake */
314 char off[LNM$C_NAMLENGTH+1];;
316 gmtime_emulation_type++;
317 if (!Perl_vmstrnenv("SYS$TIMEZONE_DIFFERENTIAL",off,0,fildev,0)) {
318 gmtime_emulation_type++;
320 Perl_warn(aTHX_ "no UTC offset information; assuming local time is UTC");
322 else { utc_offset_secs = atol(off); }
324 else { /* We've got a working gmtime() */
325 struct tm gmt, local;
328 tm_p = localtime(&base);
330 utc_offset_secs = (local.tm_mday - gmt.tm_mday) * 86400;
331 utc_offset_secs += (local.tm_hour - gmt.tm_hour) * 3600;
332 utc_offset_secs += (local.tm_min - gmt.tm_min) * 60;
333 utc_offset_secs += (local.tm_sec - gmt.tm_sec);
341 gettimeofday (struct timeval *tp, void *tpz)
347 long div_100ns_to_secs;
348 long div_100ns_to_usecs;
356 In case of error, tv_usec = 0 and tv_sec = VMS condition code.
357 The return from function is also set to -1.
358 This is not exactly as per the manual page.
364 if (base_adjust[0]==0 && base_adjust[1]==0) {
366 if (base_adjust==0) { /* Need to determine epoch adjustment */
368 ret=sys$bintim(&dscepoch,&base_adjust);
369 if (1 != (ret &&1)) {
375 ret=sys$gettim(&quad); /* Get VMS system time */
376 if ((1 && ret) == 1) {
378 quad[0] -= base_adjust[0]; /* convert to epoch offset */
379 quad[1] -= base_adjust[1]; /* convert 2nd half of quadword */
380 div_100ns_to_secs = DIV_100NS_TO_SECS;
381 div_100ns_to_usecs = DIV_100NS_TO_USECS;
382 lib$ediv(&div_100ns_to_secs,&quad,&quo,&rem);
385 lib$ediv(&div_100ns_to_usecs,&quad1,&quo1,&rem1);
386 tp->tv_sec = quo; /* Whole seconds */
387 tp->tv_usec = quo1; /* Micro-seconds */
389 quad -= base_adjust; /* convert to epoch offset */
390 ans1=qdiv(quad,DIV_100NS_TO_SECS);
391 ans2=qdiv(ans1.rem,DIV_100NS_TO_USECS);
392 tp->tv_sec = ans1.quot; /* Whole seconds */
393 tp->tv_usec = ans2.quot; /* Micro-seconds */
401 if (VMSISH_TIME) tp->tv_sec = _toloc(tp->tv_sec);
403 if (!VMSISH_TIME) tp->tv_sec = _toutc(tp->tv_sec);
411 /* Do not use H A S _ N A N O S L E E P
412 * so that Perl Configure doesn't scan for it (and pull in -lrt and
413 * the like which are not usually good ideas for the default Perl).
414 * (We are part of the core perl now.)
415 * The TIME_HIRES_NANOSLEEP is set by Makefile.PL. */
416 #if !defined(HAS_USLEEP) && defined(TIME_HIRES_NANOSLEEP)
418 #define usleep hrt_usleep /* could conflict with ncurses for static build */
421 hrt_usleep(unsigned long usec) /* This is used to emulate usleep. */
424 res.tv_sec = usec / IV_1E6;
425 res.tv_nsec = ( usec - res.tv_sec * IV_1E6 ) * 1000;
426 nanosleep(&res, NULL);
429 #endif /* #if !defined(HAS_USLEEP) && defined(TIME_HIRES_NANOSLEEP) */
431 #if !defined(HAS_USLEEP) && defined(HAS_SELECT)
432 #ifndef SELECT_IS_BROKEN
434 #define usleep hrt_usleep /* could conflict with ncurses for static build */
437 hrt_usleep(unsigned long usec)
442 select(0, (Select_fd_set_t)NULL, (Select_fd_set_t)NULL,
443 (Select_fd_set_t)NULL, &tv);
446 #endif /* #if !defined(HAS_USLEEP) && defined(HAS_SELECT) */
448 #if !defined(HAS_USLEEP) && defined(WIN32)
450 #define usleep hrt_usleep /* could conflict with ncurses for static build */
453 hrt_usleep(unsigned long usec)
459 #endif /* #if !defined(HAS_USLEEP) && defined(WIN32) */
461 #if !defined(HAS_USLEEP) && defined(HAS_POLL)
463 #define usleep hrt_usleep /* could conflict with ncurses for static build */
466 hrt_usleep(unsigned long usec)
468 int msec = usec / 1000;
472 #endif /* #if !defined(HAS_USLEEP) && defined(HAS_POLL) */
474 #if defined(HAS_SETITIMER) && defined(ITIMER_REAL)
477 hrt_ualarm_itimero(struct itimerval *oitv, int usec, int uinterval)
479 struct itimerval itv;
480 itv.it_value.tv_sec = usec / IV_1E6;
481 itv.it_value.tv_usec = usec % IV_1E6;
482 itv.it_interval.tv_sec = uinterval / IV_1E6;
483 itv.it_interval.tv_usec = uinterval % IV_1E6;
484 return setitimer(ITIMER_REAL, &itv, oitv);
488 hrt_ualarm_itimer(int usec, int uinterval)
490 return hrt_ualarm_itimero(NULL, usec, uinterval);
495 hrt_ualarm(int usec, int interval) /* for binary compat before 1.91 */
497 return hrt_ualarm_itimer(usec, interval);
499 #endif /* #ifdef HAS_UALARM */
500 #endif /* #if !defined(HAS_UALARM) && defined(HAS_SETITIMER) */
502 #if !defined(HAS_UALARM) && defined(HAS_SETITIMER)
504 #define ualarm hrt_ualarm_itimer /* could conflict with ncurses for static build */
507 #if !defined(HAS_UALARM) && defined(VMS)
509 #define ualarm vms_ualarm
511 #include <lib$routines.h>
519 #define VMSERR(s) (!((s)&1))
522 us_to_VMS(useconds_t mseconds, unsigned long v[])
531 iss = lib$addx(qq,qq,qq);
532 if (VMSERR(iss)) lib$signal(iss);
533 iss = lib$subx(v,qq,v);
534 if (VMSERR(iss)) lib$signal(iss);
535 iss = lib$addx(qq,qq,qq);
536 if (VMSERR(iss)) lib$signal(iss);
537 iss = lib$subx(v,qq,v);
538 if (VMSERR(iss)) lib$signal(iss);
539 iss = lib$subx(v,qq,v);
540 if (VMSERR(iss)) lib$signal(iss);
544 VMS_to_us(unsigned long v[])
547 unsigned long div=10,quot, rem;
549 iss = lib$ediv(&div,v,",&rem);
550 if (VMSERR(iss)) lib$signal(iss);
555 typedef unsigned short word;
556 typedef struct _ualarm {
559 unsigned long delay[2];
560 unsigned long interval[2];
561 unsigned long remain[2];
566 static Alarm *a0, alarm_base;
571 static void ualarm_AST(Alarm *a);
574 vms_ualarm(int mseconds, int interval)
583 static struct item_list3 itmlst[2];
584 static int first = 1;
590 itmlst[0].code = JPI$_ASTEN;
591 itmlst[0].length = sizeof(asten);
592 itmlst[0].retlenaddr = NULL;
594 itmlst[1].length = 0;
595 itmlst[1].bufaddr = NULL;
596 itmlst[1].retlenaddr = NULL;
598 iss = lib$get_ef(&alarm_ef);
599 if (VMSERR(iss)) lib$signal(iss);
602 a0->function = UAL_NULL;
604 itmlst[0].bufaddr = &asten;
606 iss = sys$getjpiw(0,0,0,itmlst,0,0,0);
607 if (VMSERR(iss)) lib$signal(iss);
608 if (!(asten&0x08)) return -1;
612 a->function = UAL_SET;
614 a->function = UAL_CLEAR;
617 us_to_VMS(mseconds, a->delay);
619 us_to_VMS(interval, a->interval);
624 iss = sys$clref(alarm_ef);
625 if (VMSERR(iss)) lib$signal(iss);
627 iss = sys$dclast(ualarm_AST,a,0);
628 if (VMSERR(iss)) lib$signal(iss);
630 iss = sys$waitfr(alarm_ef);
631 if (VMSERR(iss)) lib$signal(iss);
633 if (a->function == UAL_ACTIVE)
634 return VMS_to_us(a->remain);
645 unsigned long now[2];
647 iss = sys$gettim(now);
648 if (VMSERR(iss)) lib$signal(iss);
650 if (a->function == UAL_SET || a->function == UAL_CLEAR) {
651 if (a0->function == UAL_ACTIVE) {
652 iss = sys$cantim(a0,PSL$C_USER);
653 if (VMSERR(iss)) lib$signal(iss);
655 iss = lib$subx(a0->remain, now, a->remain);
656 if (VMSERR(iss)) lib$signal(iss);
658 if (a->remain[1] & 0x80000000)
659 a->remain[0] = a->remain[1] = 0;
662 if (a->function == UAL_SET) {
663 a->function = a0->function;
664 a0->function = UAL_ACTIVE;
665 a0->repeat = a->repeat;
667 a0->interval[0] = a->interval[0];
668 a0->interval[1] = a->interval[1];
670 a0->delay[0] = a->delay[0];
671 a0->delay[1] = a->delay[1];
673 iss = lib$subx(now, a0->delay, a0->remain);
674 if (VMSERR(iss)) lib$signal(iss);
676 iss = sys$setimr(0,a0->delay,ualarm_AST,a0);
677 if (VMSERR(iss)) lib$signal(iss);
679 a->function = a0->function;
680 a0->function = UAL_NULL;
682 iss = sys$setef(alarm_ef);
683 if (VMSERR(iss)) lib$signal(iss);
684 } else if (a->function == UAL_ACTIVE) {
686 iss = lib$subx(now, a->interval, a->remain);
687 if (VMSERR(iss)) lib$signal(iss);
689 iss = sys$setimr(0,a->interval,ualarm_AST,a);
690 if (VMSERR(iss)) lib$signal(iss);
692 a->function = UAL_NULL;
695 if (VMSERR(iss)) lib$signal(iss);
696 lib$signal(SS$_ASTFLT);
698 lib$signal(SS$_BADPARAM);
702 #endif /* #if !defined(HAS_UALARM) && defined(VMS) */
704 #ifdef HAS_GETTIMEOFDAY
707 myU2time(pTHX_ UV *ret)
711 status = gettimeofday (&Tp, NULL);
725 status = gettimeofday (&Tp, NULL);
726 return status == 0 ? Tp.tv_sec + (Tp.tv_usec / NV_1E6) : -1.0;
729 #endif /* #ifdef HAS_GETTIMEOFDAY */
732 hrstatns(UV *atime_nsec, UV *mtime_nsec, UV *ctime_nsec)
735 #if TIME_HIRES_STAT == 1
736 *atime_nsec = PL_statcache.st_atimespec.tv_nsec;
737 *mtime_nsec = PL_statcache.st_mtimespec.tv_nsec;
738 *ctime_nsec = PL_statcache.st_ctimespec.tv_nsec;
739 #elif TIME_HIRES_STAT == 2
740 *atime_nsec = PL_statcache.st_atimensec;
741 *mtime_nsec = PL_statcache.st_mtimensec;
742 *ctime_nsec = PL_statcache.st_ctimensec;
743 #elif TIME_HIRES_STAT == 3
744 *atime_nsec = PL_statcache.st_atime_n;
745 *mtime_nsec = PL_statcache.st_mtime_n;
746 *ctime_nsec = PL_statcache.st_ctime_n;
747 #elif TIME_HIRES_STAT == 4
748 *atime_nsec = PL_statcache.st_atim.tv_nsec;
749 *mtime_nsec = PL_statcache.st_mtim.tv_nsec;
750 *ctime_nsec = PL_statcache.st_ctim.tv_nsec;
751 #elif TIME_HIRES_STAT == 5
752 *atime_nsec = PL_statcache.st_uatime * 1000;
753 *mtime_nsec = PL_statcache.st_umtime * 1000;
754 *ctime_nsec = PL_statcache.st_uctime * 1000;
755 #else /* !TIME_HIRES_STAT */
759 #endif /* !TIME_HIRES_STAT */
762 /* Until Apple implements clock_gettime() (ditto clock_getres())
763 * we will emulate it using Mach interfaces. */
764 #if defined(PERL_DARWIN) && !defined(CLOCK_REALTIME)
766 # include <mach/mach_time.h>
768 # define CLOCK_REALTIME 0x01
769 # define CLOCK_MONOTONIC 0x02
771 # define TIMER_ABSTIME 0x01
774 STATIC perl_mutex darwin_time_mutex;
777 static uint64_t absolute_time_init;
778 static mach_timebase_info_data_t timebase_info;
779 static struct timespec timespec_init;
781 static int darwin_time_init() {
783 PERL_MUTEX_LOCK(&darwin_time_mutex);
787 if (absolute_time_init == 0) {
788 /* mach_absolute_time() cannot fail */
789 absolute_time_init = mach_absolute_time();
790 success = mach_timebase_info(&timebase_info) == KERN_SUCCESS;
792 success = gettimeofday(&tv, NULL) == 0;
794 timespec_init.tv_sec = tv.tv_sec;
795 timespec_init.tv_nsec = tv.tv_usec * 1000;
800 PERL_MUTEX_UNLOCK(&darwin_time_mutex);
805 static int clock_gettime(int clock_id, struct timespec *ts) {
806 if (darwin_time_init() && timebase_info.denom) {
811 ((mach_absolute_time() - absolute_time_init) *
812 (uint64_t)timebase_info.numer) / (uint64_t)timebase_info.denom;
813 ts->tv_sec = timespec_init.tv_sec + nanos / IV_1E9;
814 ts->tv_nsec = timespec_init.tv_nsec + nanos % IV_1E9;
818 case CLOCK_MONOTONIC:
821 (mach_absolute_time() *
822 (uint64_t)timebase_info.numer) / (uint64_t)timebase_info.denom;
823 ts->tv_sec = nanos / IV_1E9;
824 ts->tv_nsec = nanos - ts->tv_sec * IV_1E9;
833 SETERRNO(EINVAL, LIB_INVARG);
837 static int clock_getres(int clock_id, struct timespec *ts) {
838 if (darwin_time_init() && timebase_info.denom) {
841 case CLOCK_MONOTONIC:
843 /* In newer kernels both the numer and denom are one,
844 * resulting in conversion factor of one, which is of
845 * course unrealistic. */
846 ts->tv_nsec = timebase_info.numer / timebase_info.denom;
853 SETERRNO(EINVAL, LIB_INVARG);
857 static int clock_nanosleep(int clock_id, int flags,
858 const struct timespec *rqtp,
859 struct timespec *rmtp) {
860 if (darwin_time_init()) {
863 case CLOCK_MONOTONIC:
865 uint64_t nanos = rqtp->tv_sec * IV_1E9 + rqtp->tv_nsec;
867 if ((flags & TIMER_ABSTIME)) {
869 timespec_init.tv_sec * IV_1E9 + timespec_init.tv_nsec;
870 nanos = nanos > back ? nanos - back : 0;
873 mach_wait_until(mach_absolute_time() + nanos) == KERN_SUCCESS;
875 /* In the relative sleep, the rmtp should be filled in with
876 * the 'unused' part of the rqtp in case the sleep gets
877 * interrupted by a signal. But it is unknown how signals
878 * interact with mach_wait_until(). In the absolute sleep,
879 * the rmtp should stay untouched. */
891 SETERRNO(EINVAL, LIB_INVARG);
895 #endif /* PERL_DARWIN */
897 #include "const-c.inc"
899 #if (defined(TIME_HIRES_NANOSLEEP)) || \
900 (defined(TIME_HIRES_CLOCK_NANOSLEEP) && defined(TIMER_ABSTIME))
903 nsec_without_unslept(struct timespec *sleepfor,
904 const struct timespec *unslept) {
905 sleepfor->tv_sec -= unslept->tv_sec;
906 sleepfor->tv_nsec -= unslept->tv_nsec;
907 if (sleepfor->tv_nsec < 0) {
909 sleepfor->tv_nsec += IV_1E9;
911 return ((NV)sleepfor->tv_sec) * NV_1E9 + ((NV)sleepfor->tv_nsec);
916 MODULE = Time::HiRes PACKAGE = Time::HiRes
925 #ifdef ATLEASTFIVEOHOHFIVE
926 # ifdef HAS_GETTIMEOFDAY
928 (void) hv_store(PL_modglobal, "Time::NVtime", 12,
929 newSViv(PTR2IV(myNVtime)), 0);
930 (void) hv_store(PL_modglobal, "Time::U2time", 12,
931 newSViv(PTR2IV(myU2time)), 0);
937 #if defined(USE_ITHREADS) && defined(MY_CXT_KEY)
946 INCLUDE: const-xs.inc
948 #if defined(HAS_USLEEP) && defined(HAS_GETTIMEOFDAY)
954 struct timeval Ta, Tb;
956 gettimeofday(&Ta, NULL);
958 if (useconds >= NV_1E6) {
959 IV seconds = (IV) (useconds / NV_1E6);
960 /* If usleep() has been implemented using setitimer()
961 * then this contortion is unnecessary-- but usleep()
962 * may be implemented in some other way, so let's contort. */
965 useconds -= NV_1E6 * seconds;
967 } else if (useconds < 0.0)
968 croak("Time::HiRes::usleep(%"NVgf"): negative time not invented yet", useconds);
969 usleep((U32)useconds);
972 gettimeofday(&Tb, NULL);
974 printf("[%ld %ld] [%ld %ld]\n", Tb.tv_sec, Tb.tv_usec, Ta.tv_sec, Ta.tv_usec);
976 RETVAL = NV_1E6*(Tb.tv_sec-Ta.tv_sec)+(NV)((IV)Tb.tv_usec-(IV)Ta.tv_usec);
981 #if defined(TIME_HIRES_NANOSLEEP)
987 struct timespec sleepfor, unslept;
990 croak("Time::HiRes::nanosleep(%"NVgf"): negative time not invented yet", nsec);
991 sleepfor.tv_sec = (Time_t)(nsec / NV_1E9);
992 sleepfor.tv_nsec = (long)(nsec - ((NV)sleepfor.tv_sec) * NV_1E9);
995 if (nanosleep(&sleepfor, &unslept) == 0) {
998 RETVAL = nsec_without_unslept(&sleepfor, &unslept);
1003 #else /* #if defined(TIME_HIRES_NANOSLEEP) */
1009 PERL_UNUSED_ARG(nsec);
1010 croak("Time::HiRes::nanosleep(): unimplemented in this platform");
1015 #endif /* #if defined(TIME_HIRES_NANOSLEEP) */
1020 struct timeval Ta, Tb;
1022 gettimeofday(&Ta, NULL);
1024 NV seconds = SvNV(ST(0));
1025 if (seconds >= 0.0) {
1026 UV useconds = (UV)(1E6 * (seconds - (UV)seconds));
1028 sleep((U32)seconds);
1029 if ((IV)useconds < 0) {
1030 #if defined(__sparc64__) && defined(__GNUC__)
1031 /* Sparc64 gcc 2.95.3 (e.g. on NetBSD) has a bug
1032 * where (0.5 - (UV)(0.5)) will under certain
1033 * circumstances (if the double is cast to UV more
1034 * than once?) evaluate to -0.5, instead of 0.5. */
1035 useconds = -(IV)useconds;
1036 #endif /* #if defined(__sparc64__) && defined(__GNUC__) */
1037 if ((IV)useconds < 0)
1038 croak("Time::HiRes::sleep(%"NVgf"): internal error: useconds < 0 (unsigned %"UVuf" signed %"IVdf")", seconds, useconds, (IV)useconds);
1042 croak("Time::HiRes::sleep(%"NVgf"): negative time not invented yet", seconds);
1045 gettimeofday(&Tb, NULL);
1047 printf("[%ld %ld] [%ld %ld]\n", Tb.tv_sec, Tb.tv_usec, Ta.tv_sec, Ta.tv_usec);
1049 RETVAL = (NV)(Tb.tv_sec-Ta.tv_sec)+0.000001*(NV)(Tb.tv_usec-Ta.tv_usec);
1054 #else /* #if defined(HAS_USLEEP) && defined(HAS_GETTIMEOFDAY) */
1060 PERL_UNUSED_ARG(useconds);
1061 croak("Time::HiRes::usleep(): unimplemented in this platform");
1066 #endif /* #if defined(HAS_USLEEP) && defined(HAS_GETTIMEOFDAY) */
1071 ualarm(useconds,uinterval=0)
1075 if (useconds < 0 || uinterval < 0)
1076 croak("Time::HiRes::ualarm(%d, %d): negative time not invented yet", useconds, uinterval);
1077 #if defined(HAS_SETITIMER) && defined(ITIMER_REAL)
1079 struct itimerval itv;
1080 if (hrt_ualarm_itimero(&itv, useconds, uinterval)) {
1081 /* To conform to ualarm's interface, we're actually ignoring
1085 RETVAL = itv.it_value.tv_sec * IV_1E6 + itv.it_value.tv_usec;
1089 if (useconds >= IV_1E6 || uinterval >= IV_1E6)
1090 croak("Time::HiRes::ualarm(%d, %d): useconds or uinterval equal to or more than %"IVdf, useconds, uinterval, IV_1E6);
1091 RETVAL = ualarm(useconds, uinterval);
1098 alarm(seconds,interval=0)
1102 if (seconds < 0.0 || interval < 0.0)
1103 croak("Time::HiRes::alarm(%"NVgf", %"NVgf"): negative time not invented yet", seconds, interval);
1105 IV iseconds = (IV)seconds;
1106 IV iinterval = (IV)interval;
1107 NV fseconds = seconds - iseconds;
1108 NV finterval = interval - iinterval;
1109 IV useconds, uinterval;
1110 if (fseconds >= 1.0 || finterval >= 1.0)
1111 croak("Time::HiRes::alarm(%"NVgf", %"NVgf"): seconds or interval too large to split correctly", seconds, interval);
1112 useconds = IV_1E6 * fseconds;
1113 uinterval = IV_1E6 * finterval;
1114 #if defined(HAS_SETITIMER) && defined(ITIMER_REAL)
1116 struct itimerval nitv, oitv;
1117 nitv.it_value.tv_sec = iseconds;
1118 nitv.it_value.tv_usec = useconds;
1119 nitv.it_interval.tv_sec = iinterval;
1120 nitv.it_interval.tv_usec = uinterval;
1121 if (setitimer(ITIMER_REAL, &nitv, &oitv)) {
1122 /* To conform to alarm's interface, we're actually ignoring
1126 RETVAL = oitv.it_value.tv_sec + ((NV)oitv.it_value.tv_usec) / NV_1E6;
1130 if (iseconds || iinterval)
1131 croak("Time::HiRes::alarm(%"NVgf", %"NVgf"): seconds or interval equal to or more than 1.0 ", seconds, interval);
1132 RETVAL = (NV)ualarm( useconds, uinterval ) / NV_1E6;
1142 ualarm(useconds,interval=0)
1146 PERL_UNUSED_ARG(useconds);
1147 PERL_UNUSED_ARG(interval);
1148 croak("Time::HiRes::ualarm(): unimplemented in this platform");
1154 alarm(seconds,interval=0)
1158 PERL_UNUSED_ARG(seconds);
1159 PERL_UNUSED_ARG(interval);
1160 croak("Time::HiRes::alarm(): unimplemented in this platform");
1165 #endif /* #ifdef HAS_UALARM */
1167 #ifdef HAS_GETTIMEOFDAY
1168 # ifdef MACOS_TRADITIONAL /* fix epoch TZ and use unsigned time_t */
1176 status = gettimeofday (&Tp, &Tz);
1179 Tp.tv_sec += Tz.tz_minuteswest * 60; /* adjust for TZ */
1180 if (GIMME == G_ARRAY) {
1182 /* Mac OS (Classic) has unsigned time_t */
1183 PUSHs(sv_2mortal(newSVuv(Tp.tv_sec)));
1184 PUSHs(sv_2mortal(newSViv(Tp.tv_usec)));
1187 PUSHs(sv_2mortal(newSVnv(Tp.tv_sec + (Tp.tv_usec / NV_1E6))));
1198 status = gettimeofday (&Tp, &Tz);
1200 Tp.tv_sec += Tz.tz_minuteswest * 60; /* adjust for TZ */
1201 RETVAL = Tp.tv_sec + (Tp.tv_usec / NV_1E6);
1208 # else /* MACOS_TRADITIONAL */
1215 status = gettimeofday (&Tp, NULL);
1217 if (GIMME == G_ARRAY) {
1219 PUSHs(sv_2mortal(newSViv(Tp.tv_sec)));
1220 PUSHs(sv_2mortal(newSViv(Tp.tv_usec)));
1223 PUSHs(sv_2mortal(newSVnv(Tp.tv_sec + (Tp.tv_usec / NV_1E6))));
1233 status = gettimeofday (&Tp, NULL);
1235 RETVAL = Tp.tv_sec + (Tp.tv_usec / NV_1E6);
1242 # endif /* MACOS_TRADITIONAL */
1243 #endif /* #ifdef HAS_GETTIMEOFDAY */
1245 #if defined(HAS_GETITIMER) && defined(HAS_SETITIMER)
1247 #define TV2NV(tv) ((NV)((tv).tv_sec) + 0.000001 * (NV)((tv).tv_usec))
1250 setitimer(which, seconds, interval = 0)
1255 struct itimerval newit;
1256 struct itimerval oldit;
1258 if (seconds < 0.0 || interval < 0.0)
1259 croak("Time::HiRes::setitimer(%"IVdf", %"NVgf", %"NVgf"): negative time not invented yet", (IV)which, seconds, interval);
1260 newit.it_value.tv_sec = (IV)seconds;
1261 newit.it_value.tv_usec =
1262 (IV)((seconds - (NV)newit.it_value.tv_sec) * NV_1E6);
1263 newit.it_interval.tv_sec = (IV)interval;
1264 newit.it_interval.tv_usec =
1265 (IV)((interval - (NV)newit.it_interval.tv_sec) * NV_1E6);
1266 /* on some platforms the 1st arg to setitimer is an enum, which
1267 * causes -Wc++-compat to complain about passing an int instead
1269 #ifdef GCC_DIAG_IGNORE
1270 GCC_DIAG_IGNORE(-Wc++-compat);
1272 if (setitimer(which, &newit, &oldit) == 0) {
1274 PUSHs(sv_2mortal(newSVnv(TV2NV(oldit.it_value))));
1275 if (GIMME == G_ARRAY) {
1277 PUSHs(sv_2mortal(newSVnv(TV2NV(oldit.it_interval))));
1280 #ifdef GCC_DIAG_RESTORE
1288 struct itimerval nowit;
1290 /* on some platforms the 1st arg to getitimer is an enum, which
1291 * causes -Wc++-compat to complain about passing an int instead
1293 #ifdef GCC_DIAG_IGNORE
1294 GCC_DIAG_IGNORE(-Wc++-compat);
1296 if (getitimer(which, &nowit) == 0) {
1298 PUSHs(sv_2mortal(newSVnv(TV2NV(nowit.it_value))));
1299 if (GIMME == G_ARRAY) {
1301 PUSHs(sv_2mortal(newSVnv(TV2NV(nowit.it_interval))));
1304 #ifdef GCC_DIAG_RESTORE
1308 #endif /* #if defined(HAS_GETITIMER) && defined(HAS_SETITIMER) */
1310 #if defined(TIME_HIRES_CLOCK_GETTIME)
1313 clock_gettime(clock_id = CLOCK_REALTIME)
1319 #ifdef TIME_HIRES_CLOCK_GETTIME_SYSCALL
1320 status = syscall(SYS_clock_gettime, clock_id, &ts);
1322 status = clock_gettime(clock_id, &ts);
1324 RETVAL = status == 0 ? ts.tv_sec + (NV) ts.tv_nsec / NV_1E9 : -1;
1329 #else /* if defined(TIME_HIRES_CLOCK_GETTIME) */
1332 clock_gettime(clock_id = 0)
1335 PERL_UNUSED_ARG(clock_id);
1336 croak("Time::HiRes::clock_gettime(): unimplemented in this platform");
1341 #endif /* #if defined(TIME_HIRES_CLOCK_GETTIME) */
1343 #if defined(TIME_HIRES_CLOCK_GETRES)
1346 clock_getres(clock_id = CLOCK_REALTIME)
1352 #ifdef TIME_HIRES_CLOCK_GETRES_SYSCALL
1353 status = syscall(SYS_clock_getres, clock_id, &ts);
1355 status = clock_getres(clock_id, &ts);
1357 RETVAL = status == 0 ? ts.tv_sec + (NV) ts.tv_nsec / NV_1E9 : -1;
1362 #else /* if defined(TIME_HIRES_CLOCK_GETRES) */
1365 clock_getres(clock_id = 0)
1368 PERL_UNUSED_ARG(clock_id);
1369 croak("Time::HiRes::clock_getres(): unimplemented in this platform");
1374 #endif /* #if defined(TIME_HIRES_CLOCK_GETRES) */
1376 #if defined(TIME_HIRES_CLOCK_NANOSLEEP) && defined(TIMER_ABSTIME)
1379 clock_nanosleep(clock_id, nsec, flags = 0)
1384 struct timespec sleepfor, unslept;
1387 croak("Time::HiRes::clock_nanosleep(..., %"NVgf"): negative time not invented yet", nsec);
1388 sleepfor.tv_sec = (Time_t)(nsec / NV_1E9);
1389 sleepfor.tv_nsec = (long)(nsec - ((NV)sleepfor.tv_sec) * NV_1E9);
1391 unslept.tv_nsec = 0;
1392 if (clock_nanosleep(clock_id, flags, &sleepfor, &unslept) == 0) {
1395 RETVAL = nsec_without_unslept(&sleepfor, &unslept);
1400 #else /* if defined(TIME_HIRES_CLOCK_NANOSLEEP) && defined(TIMER_ABSTIME) */
1403 clock_nanosleep(clock_id, nsec, flags = 0)
1408 PERL_UNUSED_ARG(clock_id);
1409 PERL_UNUSED_ARG(nsec);
1410 PERL_UNUSED_ARG(flags);
1411 croak("Time::HiRes::clock_nanosleep(): unimplemented in this platform");
1416 #endif /* #if defined(TIME_HIRES_CLOCK_NANOSLEEP) && defined(TIMER_ABSTIME) */
1418 #if defined(TIME_HIRES_CLOCK) && defined(CLOCKS_PER_SEC)
1426 RETVAL = clocks == (clock_t) -1 ? (clock_t) -1 : (NV)clocks / (NV)CLOCKS_PER_SEC;
1431 #else /* if defined(TIME_HIRES_CLOCK) && defined(CLOCKS_PER_SEC) */
1436 croak("Time::HiRes::clock(): unimplemented in this platform");
1441 #endif /* #if defined(TIME_HIRES_CLOCK) && defined(CLOCKS_PER_SEC) */
1450 Time::HiRes::lstat = 1
1452 XPUSHs(sv_2mortal(newSVsv(items == 1 ? ST(0) : DEFSV)));
1455 PL_laststatval = -1;
1457 Zero(&fakeop, 1, OP);
1458 fakeop.op_type = ix ? OP_LSTAT : OP_STAT;
1459 fakeop.op_ppaddr = PL_ppaddr[fakeop.op_type];
1460 fakeop.op_flags = GIMME_V == G_ARRAY ? OPf_WANT_LIST :
1461 GIMME_V == G_SCALAR ? OPf_WANT_SCALAR : OPf_WANT_VOID;
1463 (void)fakeop.op_ppaddr(aTHX);
1466 nret = SP+1 - &ST(0);
1468 UV atime = SvUV(ST( 8));
1469 UV mtime = SvUV(ST( 9));
1470 UV ctime = SvUV(ST(10));
1474 hrstatns(&atime_nsec, &mtime_nsec, &ctime_nsec);
1476 ST( 8) = sv_2mortal(newSVnv(atime + (NV) atime_nsec / NV_1E9));
1478 ST( 9) = sv_2mortal(newSVnv(mtime + (NV) mtime_nsec / NV_1E9));
1480 ST(10) = sv_2mortal(newSVnv(ctime + (NV) ctime_nsec / NV_1E9));