3 * Copyright (c) 1996-2002 Douglas E. Wegscheid. All rights reserved.
5 * Copyright (c) 2002,2003,2004,2005,2006,2007 Jarkko Hietaniemi. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the same terms as Perl itself.
14 #define PERL_NO_GET_CONTEXT
19 #if defined(__CYGWIN__) && defined(HAS_W32API_WINDOWS_H)
20 # include <w32api/windows.h>
21 # define CYGWIN_WITH_W32API
26 # include <sys/time.h>
30 # include <sys/select.h>
33 #if defined(TIME_HIRES_CLOCK_GETTIME_SYSCALL) || defined(TIME_HIRES_CLOCK_GETRES_SYSCALL)
40 #define IV_1E6 1000000
41 #define IV_1E7 10000000
42 #define IV_1E9 1000000000
44 #define NV_1E6 1000000.0
45 #define NV_1E7 10000000.0
46 #define NV_1E9 1000000000.0
48 #ifndef PerlProc_pause
49 # define PerlProc_pause() Pause()
55 # undef Pause /* In case perl.h did it already. */
56 # define Pause() sleep(~0) /* Zzz for a long time. */
59 /* Though the cpp define ITIMER_VIRTUAL is available the functionality
60 * is not supported in Cygwin as of August 2004, ditto for Win32.
61 * Neither are ITIMER_PROF or ITIMER_REALPROF implemented. --jhi
63 #if defined(__CYGWIN__) || defined(WIN32)
64 # undef ITIMER_VIRTUAL
66 # undef ITIMER_REALPROF
69 #if defined(TIME_HIRES_CLOCK_GETTIME) && defined(_STRUCT_ITIMERSPEC)
71 /* HP-UX has CLOCK_XXX values but as enums, not as defines.
72 * The only way to detect these would be to test compile for each. */
74 # define CLOCK_REALTIME CLOCK_REALTIME
75 # define CLOCK_VIRTUAL CLOCK_VIRTUAL
76 # define CLOCK_PROFILE CLOCK_PROFILE
77 # endif /* # ifdef __hpux */
79 #endif /* #if defined(TIME_HIRES_CLOCK_GETTIME) && defined(_STRUCT_ITIMERSPEC) */
81 #if defined(WIN32) || defined(CYGWIN_WITH_W32API)
83 #ifndef HAS_GETTIMEOFDAY
84 # define HAS_GETTIMEOFDAY
87 /* shows up in winsock.h?
95 unsigned __int64 ft_i64;
99 #define MY_CXT_KEY "Time::HiRes_" XS_VERSION
102 unsigned long run_count;
103 unsigned __int64 base_ticks;
104 unsigned __int64 tick_frequency;
105 FT_t base_systime_as_filetime;
106 unsigned __int64 reset_time;
111 /* Number of 100 nanosecond units from 1/1/1601 to 1/1/1970 */
113 # define Const64(x) x##LL
115 # define Const64(x) x##i64
117 #define EPOCH_BIAS Const64(116444736000000000)
121 # define IV_1E6LL 1000000LL /* Needed because of Const64() ##-appends LL (or i64). */
122 # define IV_1E7LL 10000000LL
123 # define IV_1E9LL 1000000000LL
125 # define IV_1E6i64 1000000i64
126 # define IV_1E7i64 10000000i64
127 # define IV_1E9i64 1000000000i64
131 /* NOTE: This does not compute the timezone info (doing so can be expensive,
132 * and appears to be unsupported even by glibc) */
134 /* dMY_CXT needs a Perl context and we don't want to call PERL_GET_CONTEXT
135 for performance reasons */
138 #define gettimeofday(tp, not_used) _gettimeofday(aTHX_ tp, not_used)
140 /* If the performance counter delta drifts more than 0.5 seconds from the
141 * system time then we recalibrate to the system time. This means we may
142 * move *backwards* in time! */
143 #define MAX_PERF_COUNTER_SKEW Const64(5000000) /* 0.5 seconds */
145 /* Reset reading from the performance counter every five minutes.
146 * Many PC clocks just seem to be so bad. */
147 #define MAX_PERF_COUNTER_TICKS Const64(300000000) /* 300 seconds */
150 _gettimeofday(pTHX_ struct timeval *tp, void *not_used)
154 unsigned __int64 ticks;
157 if (MY_CXT.run_count++ == 0 ||
158 MY_CXT.base_systime_as_filetime.ft_i64 > MY_CXT.reset_time) {
159 QueryPerformanceFrequency((LARGE_INTEGER*)&MY_CXT.tick_frequency);
160 QueryPerformanceCounter((LARGE_INTEGER*)&MY_CXT.base_ticks);
161 GetSystemTimeAsFileTime(&MY_CXT.base_systime_as_filetime.ft_val);
162 ft.ft_i64 = MY_CXT.base_systime_as_filetime.ft_i64;
163 MY_CXT.reset_time = ft.ft_i64 + MAX_PERF_COUNTER_TICKS;
167 QueryPerformanceCounter((LARGE_INTEGER*)&ticks);
168 ticks -= MY_CXT.base_ticks;
169 ft.ft_i64 = MY_CXT.base_systime_as_filetime.ft_i64
170 + Const64(IV_1E7) * (ticks / MY_CXT.tick_frequency)
171 +(Const64(IV_1E7) * (ticks % MY_CXT.tick_frequency)) / MY_CXT.tick_frequency;
172 diff = ft.ft_i64 - MY_CXT.base_systime_as_filetime.ft_i64;
173 if (diff < -MAX_PERF_COUNTER_SKEW || diff > MAX_PERF_COUNTER_SKEW) {
174 MY_CXT.base_ticks += ticks;
175 GetSystemTimeAsFileTime(&MY_CXT.base_systime_as_filetime.ft_val);
176 ft.ft_i64 = MY_CXT.base_systime_as_filetime.ft_i64;
180 /* seconds since epoch */
181 tp->tv_sec = (long)((ft.ft_i64 - EPOCH_BIAS) / Const64(IV_1E7));
183 /* microseconds remaining */
184 tp->tv_usec = (long)((ft.ft_i64 / Const64(10)) % Const64(IV_1E6));
190 #if defined(WIN32) && !defined(ATLEASTFIVEOHOHFIVE)
192 sleep(unsigned int t)
199 #if !defined(HAS_GETTIMEOFDAY) && defined(VMS)
200 #define HAS_GETTIMEOFDAY
203 #include <time.h> /* gettimeofday */
204 #include <stdlib.h> /* qdiv */
205 #include <starlet.h> /* sys$gettim */
208 #include <lib$routines.h> /* lib$ediv() */
212 VMS binary time is expressed in 100 nano-seconds since
213 system base time which is 17-NOV-1858 00:00:00.00
216 #define DIV_100NS_TO_SECS 10000000L
217 #define DIV_100NS_TO_USECS 10L
220 gettimeofday is supposed to return times since the epoch
221 so need to determine this in terms of VMS base time
223 static $DESCRIPTOR(dscepoch,"01-JAN-1970 00:00:00.00");
226 static long base_adjust[2]={0L,0L};
228 static __int64 base_adjust=0;
233 If we don't have gettimeofday, then likely we are on a VMS machine that
234 operates on local time rather than UTC...so we have to zone-adjust.
235 This code gleefully swiped from VMS.C
238 /* method used to handle UTC conversions:
239 * 1 == CRTL gmtime(); 2 == SYS$TIMEZONE_DIFFERENTIAL; 3 == no correction
241 static int gmtime_emulation_type;
242 /* number of secs to add to UTC POSIX-style time to get local time */
243 static long int utc_offset_secs;
244 static struct dsc$descriptor_s fildevdsc =
245 { 12, DSC$K_DTYPE_T, DSC$K_CLASS_S, "LNM$FILE_DEV" };
246 static struct dsc$descriptor_s *fildev[] = { &fildevdsc, NULL };
248 static time_t toutc_dst(time_t loc) {
251 if ((rsltmp = localtime(&loc)) == NULL) return -1;
252 loc -= utc_offset_secs;
253 if (rsltmp->tm_isdst) loc -= 3600;
257 static time_t toloc_dst(time_t utc) {
260 utc += utc_offset_secs;
261 if ((rsltmp = localtime(&utc)) == NULL) return -1;
262 if (rsltmp->tm_isdst) utc += 3600;
266 #define _toutc(secs) ((secs) == (time_t) -1 ? (time_t) -1 : \
267 ((gmtime_emulation_type || timezone_setup()), \
268 (gmtime_emulation_type == 1 ? toutc_dst(secs) : \
269 ((secs) - utc_offset_secs))))
271 #define _toloc(secs) ((secs) == (time_t) -1 ? (time_t) -1 : \
272 ((gmtime_emulation_type || timezone_setup()), \
273 (gmtime_emulation_type == 1 ? toloc_dst(secs) : \
274 ((secs) + utc_offset_secs))))
281 if (gmtime_emulation_type == 0) {
283 time_t base = 15 * 86400; /* 15jan71; to avoid month/year ends between */
284 /* results of calls to gmtime() and localtime() */
287 gmtime_emulation_type++;
288 if ((tm_p = gmtime(&base)) == NULL) { /* CRTL gmtime() is a fake */
289 char off[LNM$C_NAMLENGTH+1];;
291 gmtime_emulation_type++;
292 if (!Perl_vmstrnenv("SYS$TIMEZONE_DIFFERENTIAL",off,0,fildev,0)) {
293 gmtime_emulation_type++;
295 Perl_warn(aTHX_ "no UTC offset information; assuming local time is UTC");
297 else { utc_offset_secs = atol(off); }
299 else { /* We've got a working gmtime() */
300 struct tm gmt, local;
303 tm_p = localtime(&base);
305 utc_offset_secs = (local.tm_mday - gmt.tm_mday) * 86400;
306 utc_offset_secs += (local.tm_hour - gmt.tm_hour) * 3600;
307 utc_offset_secs += (local.tm_min - gmt.tm_min) * 60;
308 utc_offset_secs += (local.tm_sec - gmt.tm_sec);
316 gettimeofday (struct timeval *tp, void *tpz)
322 long div_100ns_to_secs;
323 long div_100ns_to_usecs;
331 In case of error, tv_usec = 0 and tv_sec = VMS condition code.
332 The return from function is also set to -1.
333 This is not exactly as per the manual page.
339 if (base_adjust[0]==0 && base_adjust[1]==0) {
341 if (base_adjust==0) { /* Need to determine epoch adjustment */
343 ret=sys$bintim(&dscepoch,&base_adjust);
344 if (1 != (ret &&1)) {
350 ret=sys$gettim(&quad); /* Get VMS system time */
351 if ((1 && ret) == 1) {
353 quad[0] -= base_adjust[0]; /* convert to epoch offset */
354 quad[1] -= base_adjust[1]; /* convert 2nd half of quadword */
355 div_100ns_to_secs = DIV_100NS_TO_SECS;
356 div_100ns_to_usecs = DIV_100NS_TO_USECS;
357 lib$ediv(&div_100ns_to_secs,&quad,&quo,&rem);
360 lib$ediv(&div_100ns_to_usecs,&quad1,&quo1,&rem1);
361 tp->tv_sec = quo; /* Whole seconds */
362 tp->tv_usec = quo1; /* Micro-seconds */
364 quad -= base_adjust; /* convert to epoch offset */
365 ans1=qdiv(quad,DIV_100NS_TO_SECS);
366 ans2=qdiv(ans1.rem,DIV_100NS_TO_USECS);
367 tp->tv_sec = ans1.quot; /* Whole seconds */
368 tp->tv_usec = ans2.quot; /* Micro-seconds */
376 if (VMSISH_TIME) tp->tv_sec = _toloc(tp->tv_sec);
378 if (!VMSISH_TIME) tp->tv_sec = _toutc(tp->tv_sec);
386 /* Do not use H A S _ N A N O S L E E P
387 * so that Perl Configure doesn't scan for it (and pull in -lrt and
388 * the like which are not usually good ideas for the default Perl).
389 * (We are part of the core perl now.)
390 * The TIME_HIRES_NANOSLEEP is set by Makefile.PL. */
391 #if !defined(HAS_USLEEP) && defined(TIME_HIRES_NANOSLEEP)
393 #define usleep hrt_nanosleep /* could conflict with ncurses for static build */
396 hrt_nanosleep(unsigned long usec) /* This is used to emulate usleep. */
399 res.tv_sec = usec / IV_1E6;
400 res.tv_nsec = ( usec - res.tv_sec * IV_1E6 ) * 1000;
401 nanosleep(&res, NULL);
404 #endif /* #if !defined(HAS_USLEEP) && defined(TIME_HIRES_NANOSLEEP) */
406 #if !defined(HAS_USLEEP) && defined(HAS_SELECT)
407 #ifndef SELECT_IS_BROKEN
409 #define usleep hrt_usleep /* could conflict with ncurses for static build */
412 hrt_usleep(unsigned long usec)
417 select(0, (Select_fd_set_t)NULL, (Select_fd_set_t)NULL,
418 (Select_fd_set_t)NULL, &tv);
421 #endif /* #if !defined(HAS_USLEEP) && defined(HAS_SELECT) */
423 #if !defined(HAS_USLEEP) && defined(WIN32)
425 #define usleep hrt_usleep /* could conflict with ncurses for static build */
428 hrt_usleep(unsigned long usec)
434 #endif /* #if !defined(HAS_USLEEP) && defined(WIN32) */
436 #if !defined(HAS_USLEEP) && defined(TIME_HIRES_NANOSLEEP)
438 #define usleep hrt_usleep /* could conflict with ncurses for static build */
441 hrt_usleep(unsigned long usec)
444 ts1.tv_sec = usec * 1000; /* Ignoring wraparound. */
446 nanosleep(&ts1, NULL);
449 #endif /* #if !defined(HAS_USLEEP) && defined(TIME_HIRES_NANOSLEEP) */
451 #if !defined(HAS_USLEEP) && defined(HAS_POLL)
453 #define usleep hrt_usleep /* could conflict with ncurses for static build */
456 hrt_usleep(unsigned long usec)
458 int msec = usec / 1000;
462 #endif /* #if !defined(HAS_USLEEP) && defined(HAS_POLL) */
464 #if defined(HAS_SETITIMER) && defined(ITIMER_REAL)
466 hrt_ualarm_itimer(int usec, int interval)
468 struct itimerval itv;
469 itv.it_value.tv_sec = usec / IV_1E6;
470 itv.it_value.tv_usec = usec % IV_1E6;
471 itv.it_interval.tv_sec = interval / IV_1E6;
472 itv.it_interval.tv_usec = interval % IV_1E6;
473 return setitimer(ITIMER_REAL, &itv, 0);
477 hrt_ualarm(int usec, int interval) /* for binary compat before 1.91 */
479 return hrt_ualarm_itimer(usec, interval);
481 #endif /* #ifdef HAS_UALARM */
482 #endif /* #if !defined(HAS_UALARM) && defined(HAS_SETITIMER) */
484 #if !defined(HAS_UALARM) && defined(HAS_SETITIMER)
486 #define ualarm hrt_ualarm_itimer /* could conflict with ncurses for static build */
489 #if !defined(HAS_UALARM) && defined(VMS)
491 #define ualarm vms_ualarm
493 #include <lib$routines.h>
501 #define VMSERR(s) (!((s)&1))
504 us_to_VMS(useconds_t mseconds, unsigned long v[])
513 iss = lib$addx(qq,qq,qq);
514 if (VMSERR(iss)) lib$signal(iss);
515 iss = lib$subx(v,qq,v);
516 if (VMSERR(iss)) lib$signal(iss);
517 iss = lib$addx(qq,qq,qq);
518 if (VMSERR(iss)) lib$signal(iss);
519 iss = lib$subx(v,qq,v);
520 if (VMSERR(iss)) lib$signal(iss);
521 iss = lib$subx(v,qq,v);
522 if (VMSERR(iss)) lib$signal(iss);
526 VMS_to_us(unsigned long v[])
529 unsigned long div=10,quot, rem;
531 iss = lib$ediv(&div,v,",&rem);
532 if (VMSERR(iss)) lib$signal(iss);
537 typedef unsigned short word;
538 typedef struct _ualarm {
541 unsigned long delay[2];
542 unsigned long interval[2];
543 unsigned long remain[2];
548 static Alarm *a0, alarm_base;
553 static void ualarm_AST(Alarm *a);
556 vms_ualarm(int mseconds, int interval)
565 static struct item_list3 itmlst[2];
566 static int first = 1;
572 itmlst[0].code = JPI$_ASTEN;
573 itmlst[0].length = sizeof(asten);
574 itmlst[0].retlenaddr = NULL;
576 itmlst[1].length = 0;
577 itmlst[1].bufaddr = NULL;
578 itmlst[1].retlenaddr = NULL;
580 iss = lib$get_ef(&alarm_ef);
581 if (VMSERR(iss)) lib$signal(iss);
584 a0->function = UAL_NULL;
586 itmlst[0].bufaddr = &asten;
588 iss = sys$getjpiw(0,0,0,itmlst,0,0,0);
589 if (VMSERR(iss)) lib$signal(iss);
590 if (!(asten&0x08)) return -1;
594 a->function = UAL_SET;
596 a->function = UAL_CLEAR;
599 us_to_VMS(mseconds, a->delay);
601 us_to_VMS(interval, a->interval);
606 iss = sys$clref(alarm_ef);
607 if (VMSERR(iss)) lib$signal(iss);
609 iss = sys$dclast(ualarm_AST,a,0);
610 if (VMSERR(iss)) lib$signal(iss);
612 iss = sys$waitfr(alarm_ef);
613 if (VMSERR(iss)) lib$signal(iss);
615 if (a->function == UAL_ACTIVE)
616 return VMS_to_us(a->remain);
627 unsigned long now[2];
629 iss = sys$gettim(now);
630 if (VMSERR(iss)) lib$signal(iss);
632 if (a->function == UAL_SET || a->function == UAL_CLEAR) {
633 if (a0->function == UAL_ACTIVE) {
634 iss = sys$cantim(a0,PSL$C_USER);
635 if (VMSERR(iss)) lib$signal(iss);
637 iss = lib$subx(a0->remain, now, a->remain);
638 if (VMSERR(iss)) lib$signal(iss);
640 if (a->remain[1] & 0x80000000)
641 a->remain[0] = a->remain[1] = 0;
644 if (a->function == UAL_SET) {
645 a->function = a0->function;
646 a0->function = UAL_ACTIVE;
647 a0->repeat = a->repeat;
649 a0->interval[0] = a->interval[0];
650 a0->interval[1] = a->interval[1];
652 a0->delay[0] = a->delay[0];
653 a0->delay[1] = a->delay[1];
655 iss = lib$subx(now, a0->delay, a0->remain);
656 if (VMSERR(iss)) lib$signal(iss);
658 iss = sys$setimr(0,a0->delay,ualarm_AST,a0);
659 if (VMSERR(iss)) lib$signal(iss);
661 a->function = a0->function;
662 a0->function = UAL_NULL;
664 iss = sys$setef(alarm_ef);
665 if (VMSERR(iss)) lib$signal(iss);
666 } else if (a->function == UAL_ACTIVE) {
668 iss = lib$subx(now, a->interval, a->remain);
669 if (VMSERR(iss)) lib$signal(iss);
671 iss = sys$setimr(0,a->interval,ualarm_AST,a);
672 if (VMSERR(iss)) lib$signal(iss);
674 a->function = UAL_NULL;
677 if (VMSERR(iss)) lib$signal(iss);
678 lib$signal(SS$_ASTFLT);
680 lib$signal(SS$_BADPARAM);
684 #endif /* #if !defined(HAS_UALARM) && defined(VMS) */
686 #ifdef HAS_GETTIMEOFDAY
689 myU2time(pTHX_ UV *ret)
693 status = gettimeofday (&Tp, NULL);
707 status = gettimeofday (&Tp, NULL);
708 return status == 0 ? Tp.tv_sec + (Tp.tv_usec / NV_1E6) : -1.0;
711 #endif /* #ifdef HAS_GETTIMEOFDAY */
714 hrstatns(UV atime, UV mtime, UV ctime, UV *atime_nsec, UV *mtime_nsec, UV *ctime_nsec)
720 #ifdef TIME_HIRES_STAT
721 #if TIME_HIRES_STAT == 1
722 *atime_nsec = PL_statcache.st_atimespec.tv_nsec;
723 *mtime_nsec = PL_statcache.st_mtimespec.tv_nsec;
724 *ctime_nsec = PL_statcache.st_ctimespec.tv_nsec;
726 #if TIME_HIRES_STAT == 2
727 *atime_nsec = PL_statcache.st_atimensec;
728 *mtime_nsec = PL_statcache.st_mtimensec;
729 *ctime_nsec = PL_statcache.st_ctimensec;
731 #if TIME_HIRES_STAT == 3
732 *atime_nsec = PL_statcache.st_atime_n;
733 *mtime_nsec = PL_statcache.st_mtime_n;
734 *ctime_nsec = PL_statcache.st_ctime_n;
736 #if TIME_HIRES_STAT == 4
737 *atime_nsec = PL_statcache.st_atim.tv_nsec;
738 *mtime_nsec = PL_statcache.st_mtim.tv_nsec;
739 *ctime_nsec = PL_statcache.st_ctim.tv_nsec;
741 #if TIME_HIRES_STAT == 5
742 *atime_nsec = PL_statcache.st_uatime * 1000;
743 *mtime_nsec = PL_statcache.st_umtime * 1000;
744 *ctime_nsec = PL_statcache.st_uctime * 1000;
749 #include "const-c.inc"
751 MODULE = Time::HiRes PACKAGE = Time::HiRes
760 #ifdef ATLEASTFIVEOHOHFIVE
761 # ifdef HAS_GETTIMEOFDAY
763 hv_store(PL_modglobal, "Time::NVtime", 12, newSViv(PTR2IV(myNVtime)), 0);
764 hv_store(PL_modglobal, "Time::U2time", 12, newSViv(PTR2IV(myU2time)), 0);
770 #if defined(USE_ITHREADS) && defined(MY_CXT_KEY)
779 INCLUDE: const-xs.inc
781 #if defined(HAS_USLEEP) && defined(HAS_GETTIMEOFDAY)
787 struct timeval Ta, Tb;
789 gettimeofday(&Ta, NULL);
791 if (useconds > 1E6) {
792 IV seconds = (IV) (useconds / 1E6);
793 /* If usleep() has been implemented using setitimer()
794 * then this contortion is unnecessary-- but usleep()
795 * may be implemented in some other way, so let's contort. */
798 useconds -= 1E6 * seconds;
800 } else if (useconds < 0.0)
801 croak("Time::HiRes::usleep(%"NVgf"): negative time not invented yet", useconds);
802 usleep((U32)useconds);
805 gettimeofday(&Tb, NULL);
807 printf("[%ld %ld] [%ld %ld]\n", Tb.tv_sec, Tb.tv_usec, Ta.tv_sec, Ta.tv_usec);
809 RETVAL = 1E6*(Tb.tv_sec-Ta.tv_sec)+(NV)((IV)Tb.tv_usec-(IV)Ta.tv_usec);
814 #if defined(TIME_HIRES_NANOSLEEP)
821 struct timeval Ta, Tb;
823 gettimeofday(&Ta, NULL);
827 IV sec = (IV) (nsec / 1E9);
832 } else if (nsec < 0.0)
833 croak("Time::HiRes::nanosleep(%"NVgf"): negative time not invented yet", nsec);
834 ts1.tv_sec = (IV) (nsec / 1E9);
835 ts1.tv_nsec = (IV) nsec - (IV) (ts1.tv_sec * NV_1E9);
836 status = nanosleep(&ts1, NULL);
841 gettimeofday(&Tb, NULL);
842 RETVAL = status == 0 ? 1E3*(1E6*(Tb.tv_sec-Ta.tv_sec)+(NV)((IV)Tb.tv_usec-(IV)Ta.tv_usec)) : -1;
847 #else /* #if defined(TIME_HIRES_NANOSLEEP) */
853 croak("Time::HiRes::nanosleep(): unimplemented in this platform");
856 #endif /* #if defined(TIME_HIRES_NANOSLEEP) */
861 struct timeval Ta, Tb;
863 gettimeofday(&Ta, NULL);
865 NV seconds = SvNV(ST(0));
866 if (seconds >= 0.0) {
867 UV useconds = (UV)(1E6 * (seconds - (UV)seconds));
870 if ((IV)useconds < 0) {
871 #if defined(__sparc64__) && defined(__GNUC__)
872 /* Sparc64 gcc 2.95.3 (e.g. on NetBSD) has a bug
873 * where (0.5 - (UV)(0.5)) will under certain
874 * circumstances (if the double is cast to UV more
875 * than once?) evaluate to -0.5, instead of 0.5. */
876 useconds = -(IV)useconds;
877 #endif /* #if defined(__sparc64__) && defined(__GNUC__) */
878 if ((IV)useconds < 0)
879 croak("Time::HiRes::sleep(%"NVgf"): internal error: useconds < 0 (unsigned %"UVuf" signed %"IVdf")", seconds, useconds, (IV)useconds);
883 croak("Time::HiRes::sleep(%"NVgf"): negative time not invented yet", seconds);
886 gettimeofday(&Tb, NULL);
888 printf("[%ld %ld] [%ld %ld]\n", Tb.tv_sec, Tb.tv_usec, Ta.tv_sec, Ta.tv_usec);
890 RETVAL = (NV)(Tb.tv_sec-Ta.tv_sec)+0.000001*(NV)(Tb.tv_usec-Ta.tv_usec);
895 #else /* #if defined(HAS_USLEEP) && defined(HAS_GETTIMEOFDAY) */
901 croak("Time::HiRes::usleep(): unimplemented in this platform");
904 #endif /* #if defined(HAS_USLEEP) && defined(HAS_GETTIMEOFDAY) */
909 ualarm(useconds,interval=0)
913 if (useconds < 0 || interval < 0)
914 croak("Time::HiRes::ualarm(%d, %d): negative time not invented yet", useconds, interval);
915 if (useconds >= IV_1E6 || interval >= IV_1E6)
916 #if defined(HAS_SETITIMER) && defined(ITIMER_REAL)
917 RETVAL = hrt_ualarm_itimer(useconds, interval);
919 croak("Time::HiRes::ualarm(%d, %d): useconds or interval equal or more than %"IVdf, useconds, interval, IV_1E6);
922 RETVAL = ualarm(useconds, interval);
928 alarm(seconds,interval=0)
932 if (seconds < 0.0 || interval < 0.0)
933 croak("Time::HiRes::alarm(%"NVgf", %"NVgf"): negative time not invented yet", seconds, interval);
934 RETVAL = (NV)ualarm((IV)(seconds * IV_1E6),
935 (IV)(interval * IV_1E6)) / NV_1E6;
943 ualarm(useconds,interval=0)
947 croak("Time::HiRes::ualarm(): unimplemented in this platform");
951 alarm(seconds,interval=0)
955 croak("Time::HiRes::alarm(): unimplemented in this platform");
958 #endif /* #ifdef HAS_UALARM */
960 #ifdef HAS_GETTIMEOFDAY
961 # ifdef MACOS_TRADITIONAL /* fix epoch TZ and use unsigned time_t */
969 status = gettimeofday (&Tp, &Tz);
972 Tp.tv_sec += Tz.tz_minuteswest * 60; /* adjust for TZ */
973 if (GIMME == G_ARRAY) {
975 /* Mac OS (Classic) has unsigned time_t */
976 PUSHs(sv_2mortal(newSVuv(Tp.tv_sec)));
977 PUSHs(sv_2mortal(newSViv(Tp.tv_usec)));
980 PUSHs(sv_2mortal(newSVnv(Tp.tv_sec + (Tp.tv_usec / NV_1E6))));
991 status = gettimeofday (&Tp, &Tz);
993 Tp.tv_sec += Tz.tz_minuteswest * 60; /* adjust for TZ */
994 RETVAL = Tp.tv_sec + (Tp.tv_usec / NV_1E6);
1001 # else /* MACOS_TRADITIONAL */
1008 status = gettimeofday (&Tp, NULL);
1010 if (GIMME == G_ARRAY) {
1012 PUSHs(sv_2mortal(newSViv(Tp.tv_sec)));
1013 PUSHs(sv_2mortal(newSViv(Tp.tv_usec)));
1016 PUSHs(sv_2mortal(newSVnv(Tp.tv_sec + (Tp.tv_usec / NV_1E6))));
1026 status = gettimeofday (&Tp, NULL);
1028 RETVAL = Tp.tv_sec + (Tp.tv_usec / NV_1E6);
1035 # endif /* MACOS_TRADITIONAL */
1036 #endif /* #ifdef HAS_GETTIMEOFDAY */
1038 #if defined(HAS_GETITIMER) && defined(HAS_SETITIMER)
1040 #define TV2NV(tv) ((NV)((tv).tv_sec) + 0.000001 * (NV)((tv).tv_usec))
1043 setitimer(which, seconds, interval = 0)
1048 struct itimerval newit;
1049 struct itimerval oldit;
1051 if (seconds < 0.0 || interval < 0.0)
1052 croak("Time::HiRes::setitimer(%"IVdf", %"NVgf", %"NVgf"): negative time not invented yet", (IV)which, seconds, interval);
1053 newit.it_value.tv_sec = (IV)seconds;
1054 newit.it_value.tv_usec =
1055 (IV)((seconds - (NV)newit.it_value.tv_sec) * NV_1E6);
1056 newit.it_interval.tv_sec = (IV)interval;
1057 newit.it_interval.tv_usec =
1058 (IV)((interval - (NV)newit.it_interval.tv_sec) * NV_1E6);
1059 if (setitimer(which, &newit, &oldit) == 0) {
1061 PUSHs(sv_2mortal(newSVnv(TV2NV(oldit.it_value))));
1062 if (GIMME == G_ARRAY) {
1064 PUSHs(sv_2mortal(newSVnv(TV2NV(oldit.it_interval))));
1072 struct itimerval nowit;
1074 if (getitimer(which, &nowit) == 0) {
1076 PUSHs(sv_2mortal(newSVnv(TV2NV(nowit.it_value))));
1077 if (GIMME == G_ARRAY) {
1079 PUSHs(sv_2mortal(newSVnv(TV2NV(nowit.it_interval))));
1083 #endif /* #if defined(HAS_GETITIMER) && defined(HAS_SETITIMER) */
1085 #if defined(TIME_HIRES_CLOCK_GETTIME)
1088 clock_gettime(clock_id = CLOCK_REALTIME)
1094 #ifdef TIME_HIRES_CLOCK_GETTIME_SYSCALL
1095 status = syscall(SYS_clock_gettime, clock_id, &ts);
1097 status = clock_gettime(clock_id, &ts);
1099 RETVAL = status == 0 ? ts.tv_sec + (NV) ts.tv_nsec / (NV) 1e9 : -1;
1104 #else /* if defined(TIME_HIRES_CLOCK_GETTIME) */
1107 clock_gettime(clock_id = 0)
1110 croak("Time::HiRes::clock_gettime(): unimplemented in this platform");
1113 #endif /* #if defined(TIME_HIRES_CLOCK_GETTIME) */
1115 #if defined(TIME_HIRES_CLOCK_GETRES)
1118 clock_getres(clock_id = CLOCK_REALTIME)
1124 #ifdef TIME_HIRES_CLOCK_GETRES_SYSCALL
1125 status = syscall(SYS_clock_getres, clock_id, &ts);
1127 status = clock_getres(clock_id, &ts);
1129 RETVAL = status == 0 ? ts.tv_sec + (NV) ts.tv_nsec / (NV) 1e9 : -1;
1134 #else /* if defined(TIME_HIRES_CLOCK_GETRES) */
1137 clock_getres(clock_id = 0)
1140 croak("Time::HiRes::clock_getres(): unimplemented in this platform");
1143 #endif /* #if defined(TIME_HIRES_CLOCK_GETRES) */
1145 #if defined(TIME_HIRES_CLOCK_NANOSLEEP) && defined(TIMER_ABSTIME)
1148 clock_nanosleep(clock_id = CLOCK_REALTIME, sec = 0.0, flags = 0)
1155 struct timeval Ta, Tb;
1157 gettimeofday(&Ta, NULL);
1159 ts.tv_sec = (IV) sec;
1160 ts.tv_nsec = (sec - (NV) ts.tv_sec) * (NV) 1E9;
1161 status = clock_nanosleep(clock_id, flags, &ts, NULL);
1166 gettimeofday(&Tb, NULL);
1167 RETVAL = status == 0 ? 1E3*(1E6*(Tb.tv_sec-Ta.tv_sec)+(NV)((IV)Tb.tv_usec-(IV)Ta.tv_usec)) : -1;
1172 #else /* if defined(TIME_HIRES_CLOCK_NANOSLEEP) && defined(TIMER_ABSTIME) */
1177 croak("Time::HiRes::clock_nanosleep(): unimplemented in this platform");
1180 #endif /* #if defined(TIME_HIRES_CLOCK_NANOSLEEP) && defined(TIMER_ABSTIME) */
1182 #if defined(TIME_HIRES_CLOCK) && defined(CLOCKS_PER_SEC)
1190 RETVAL = clocks == -1 ? -1 : (NV)clocks / (NV)CLOCKS_PER_SEC;
1195 #else /* if defined(TIME_HIRES_CLOCK) && defined(CLOCKS_PER_SEC) */
1200 croak("Time::HiRes::clock(): unimplemented in this platform");
1203 #endif /* #if defined(TIME_HIRES_CLOCK) && defined(CLOCKS_PER_SEC) */
1210 XPUSHs(sv_2mortal(newSVsv(items == 1 ? ST(0) : DEFSV)));
1213 PL_laststatval = -1;
1214 (void)*(PL_ppaddr[OP_STAT])(aTHXR);
1217 if (PL_laststatval == 0) {
1218 /* We assume that pp_stat() left us with 13 valid stack items,
1219 * and that the timestamps are at offsets 8, 9, and 10. */
1220 UV atime = SvUV(ST( 8));
1221 UV mtime = SvUV(ST( 9));
1222 UV ctime = SvUV(ST(10));
1226 hrstatns(atime, mtime, ctime,
1227 &atime_nsec, &mtime_nsec, &ctime_nsec);
1229 ST( 8) = sv_2mortal(newSVnv(atime + 1e-9 * (NV) atime_nsec));
1231 ST( 9) = sv_2mortal(newSVnv(mtime + 1e-9 * (NV) mtime_nsec));
1233 ST(10) = sv_2mortal(newSVnv(ctime + 1e-9 * (NV) ctime_nsec));