3 * Copyright (c) 1996-2002 Douglas E. Wegscheid. All rights reserved.
5 * Copyright (c) 2002,2003,2004,2005,2006,2007,2008 Jarkko Hietaniemi.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the same terms as Perl itself.
15 #define PERL_NO_GET_CONTEXT
20 #if defined(__CYGWIN__) && defined(HAS_W32API_WINDOWS_H)
21 # include <w32api/windows.h>
22 # define CYGWIN_WITH_W32API
27 # include <sys/time.h>
31 # include <sys/select.h>
34 #if defined(TIME_HIRES_CLOCK_GETTIME_SYSCALL) || defined(TIME_HIRES_CLOCK_GETRES_SYSCALL)
41 /* At least ppport.h 3.13 gets this wrong: one really cannot
42 * have NVgf as anything else than "g" under Perl 5.6.x. */
43 #if PERL_REVISION == 5 && PERL_VERSION == 6
48 #define IV_1E6 1000000
49 #define IV_1E7 10000000
50 #define IV_1E9 1000000000
52 #define NV_1E6 1000000.0
53 #define NV_1E7 10000000.0
54 #define NV_1E9 1000000000.0
56 #ifndef PerlProc_pause
57 # define PerlProc_pause() Pause()
63 # undef Pause /* In case perl.h did it already. */
64 # define Pause() sleep(~0) /* Zzz for a long time. */
67 /* Though the cpp define ITIMER_VIRTUAL is available the functionality
68 * is not supported in Cygwin as of August 2004, ditto for Win32.
69 * Neither are ITIMER_PROF or ITIMER_REALPROF implemented. --jhi
71 #if defined(__CYGWIN__) || defined(WIN32)
72 # undef ITIMER_VIRTUAL
74 # undef ITIMER_REALPROF
77 #if defined(TIME_HIRES_CLOCK_GETTIME) && defined(_STRUCT_ITIMERSPEC)
79 /* HP-UX has CLOCK_XXX values but as enums, not as defines.
80 * The only way to detect these would be to test compile for each. */
82 /* However, it seems that at least in HP-UX 11.31 ia64 there *are*
83 * defines for these, so let's try detecting them. */
84 # ifndef CLOCK_REALTIME
85 # define CLOCK_REALTIME CLOCK_REALTIME
86 # define CLOCK_VIRTUAL CLOCK_VIRTUAL
87 # define CLOCK_PROFILE CLOCK_PROFILE
89 # endif /* # ifdef __hpux */
91 #endif /* #if defined(TIME_HIRES_CLOCK_GETTIME) && defined(_STRUCT_ITIMERSPEC) */
93 #if defined(WIN32) || defined(CYGWIN_WITH_W32API)
95 #ifndef HAS_GETTIMEOFDAY
96 # define HAS_GETTIMEOFDAY
99 /* shows up in winsock.h?
107 unsigned __int64 ft_i64;
111 #define MY_CXT_KEY "Time::HiRes_" XS_VERSION
114 unsigned long run_count;
115 unsigned __int64 base_ticks;
116 unsigned __int64 tick_frequency;
117 FT_t base_systime_as_filetime;
118 unsigned __int64 reset_time;
123 /* Number of 100 nanosecond units from 1/1/1601 to 1/1/1970 */
125 # define Const64(x) x##LL
127 # define Const64(x) x##i64
129 #define EPOCH_BIAS Const64(116444736000000000)
133 # define IV_1E6LL 1000000LL /* Needed because of Const64() ##-appends LL (or i64). */
134 # define IV_1E7LL 10000000LL
135 # define IV_1E9LL 1000000000LL
137 # define IV_1E6i64 1000000i64
138 # define IV_1E7i64 10000000i64
139 # define IV_1E9i64 1000000000i64
143 /* NOTE: This does not compute the timezone info (doing so can be expensive,
144 * and appears to be unsupported even by glibc) */
146 /* dMY_CXT needs a Perl context and we don't want to call PERL_GET_CONTEXT
147 for performance reasons */
150 #define gettimeofday(tp, not_used) _gettimeofday(aTHX_ tp, not_used)
152 /* If the performance counter delta drifts more than 0.5 seconds from the
153 * system time then we recalibrate to the system time. This means we may
154 * move *backwards* in time! */
155 #define MAX_PERF_COUNTER_SKEW Const64(5000000) /* 0.5 seconds */
157 /* Reset reading from the performance counter every five minutes.
158 * Many PC clocks just seem to be so bad. */
159 #define MAX_PERF_COUNTER_TICKS Const64(300000000) /* 300 seconds */
162 _gettimeofday(pTHX_ struct timeval *tp, void *not_used)
166 unsigned __int64 ticks;
169 if (MY_CXT.run_count++ == 0 ||
170 MY_CXT.base_systime_as_filetime.ft_i64 > MY_CXT.reset_time) {
171 QueryPerformanceFrequency((LARGE_INTEGER*)&MY_CXT.tick_frequency);
172 QueryPerformanceCounter((LARGE_INTEGER*)&MY_CXT.base_ticks);
173 GetSystemTimeAsFileTime(&MY_CXT.base_systime_as_filetime.ft_val);
174 ft.ft_i64 = MY_CXT.base_systime_as_filetime.ft_i64;
175 MY_CXT.reset_time = ft.ft_i64 + MAX_PERF_COUNTER_TICKS;
179 QueryPerformanceCounter((LARGE_INTEGER*)&ticks);
180 ticks -= MY_CXT.base_ticks;
181 ft.ft_i64 = MY_CXT.base_systime_as_filetime.ft_i64
182 + Const64(IV_1E7) * (ticks / MY_CXT.tick_frequency)
183 +(Const64(IV_1E7) * (ticks % MY_CXT.tick_frequency)) / MY_CXT.tick_frequency;
184 diff = ft.ft_i64 - MY_CXT.base_systime_as_filetime.ft_i64;
185 if (diff < -MAX_PERF_COUNTER_SKEW || diff > MAX_PERF_COUNTER_SKEW) {
186 MY_CXT.base_ticks += ticks;
187 GetSystemTimeAsFileTime(&MY_CXT.base_systime_as_filetime.ft_val);
188 ft.ft_i64 = MY_CXT.base_systime_as_filetime.ft_i64;
192 /* seconds since epoch */
193 tp->tv_sec = (long)((ft.ft_i64 - EPOCH_BIAS) / Const64(IV_1E7));
195 /* microseconds remaining */
196 tp->tv_usec = (long)((ft.ft_i64 / Const64(10)) % Const64(IV_1E6));
202 #if defined(WIN32) && !defined(ATLEASTFIVEOHOHFIVE)
204 sleep(unsigned int t)
211 #if !defined(HAS_GETTIMEOFDAY) && defined(VMS)
212 #define HAS_GETTIMEOFDAY
215 #include <time.h> /* gettimeofday */
216 #include <stdlib.h> /* qdiv */
217 #include <starlet.h> /* sys$gettim */
220 #include <lib$routines.h> /* lib$ediv() */
224 VMS binary time is expressed in 100 nano-seconds since
225 system base time which is 17-NOV-1858 00:00:00.00
228 #define DIV_100NS_TO_SECS 10000000L
229 #define DIV_100NS_TO_USECS 10L
232 gettimeofday is supposed to return times since the epoch
233 so need to determine this in terms of VMS base time
235 static $DESCRIPTOR(dscepoch,"01-JAN-1970 00:00:00.00");
238 static long base_adjust[2]={0L,0L};
240 static __int64 base_adjust=0;
245 If we don't have gettimeofday, then likely we are on a VMS machine that
246 operates on local time rather than UTC...so we have to zone-adjust.
247 This code gleefully swiped from VMS.C
250 /* method used to handle UTC conversions:
251 * 1 == CRTL gmtime(); 2 == SYS$TIMEZONE_DIFFERENTIAL; 3 == no correction
253 static int gmtime_emulation_type;
254 /* number of secs to add to UTC POSIX-style time to get local time */
255 static long int utc_offset_secs;
256 static struct dsc$descriptor_s fildevdsc =
257 { 12, DSC$K_DTYPE_T, DSC$K_CLASS_S, "LNM$FILE_DEV" };
258 static struct dsc$descriptor_s *fildev[] = { &fildevdsc, NULL };
260 static time_t toutc_dst(time_t loc) {
263 if ((rsltmp = localtime(&loc)) == NULL) return -1;
264 loc -= utc_offset_secs;
265 if (rsltmp->tm_isdst) loc -= 3600;
269 static time_t toloc_dst(time_t utc) {
272 utc += utc_offset_secs;
273 if ((rsltmp = localtime(&utc)) == NULL) return -1;
274 if (rsltmp->tm_isdst) utc += 3600;
278 #define _toutc(secs) ((secs) == (time_t) -1 ? (time_t) -1 : \
279 ((gmtime_emulation_type || timezone_setup()), \
280 (gmtime_emulation_type == 1 ? toutc_dst(secs) : \
281 ((secs) - utc_offset_secs))))
283 #define _toloc(secs) ((secs) == (time_t) -1 ? (time_t) -1 : \
284 ((gmtime_emulation_type || timezone_setup()), \
285 (gmtime_emulation_type == 1 ? toloc_dst(secs) : \
286 ((secs) + utc_offset_secs))))
293 if (gmtime_emulation_type == 0) {
295 time_t base = 15 * 86400; /* 15jan71; to avoid month/year ends between */
296 /* results of calls to gmtime() and localtime() */
299 gmtime_emulation_type++;
300 if ((tm_p = gmtime(&base)) == NULL) { /* CRTL gmtime() is a fake */
301 char off[LNM$C_NAMLENGTH+1];;
303 gmtime_emulation_type++;
304 if (!Perl_vmstrnenv("SYS$TIMEZONE_DIFFERENTIAL",off,0,fildev,0)) {
305 gmtime_emulation_type++;
307 Perl_warn(aTHX_ "no UTC offset information; assuming local time is UTC");
309 else { utc_offset_secs = atol(off); }
311 else { /* We've got a working gmtime() */
312 struct tm gmt, local;
315 tm_p = localtime(&base);
317 utc_offset_secs = (local.tm_mday - gmt.tm_mday) * 86400;
318 utc_offset_secs += (local.tm_hour - gmt.tm_hour) * 3600;
319 utc_offset_secs += (local.tm_min - gmt.tm_min) * 60;
320 utc_offset_secs += (local.tm_sec - gmt.tm_sec);
328 gettimeofday (struct timeval *tp, void *tpz)
334 long div_100ns_to_secs;
335 long div_100ns_to_usecs;
343 In case of error, tv_usec = 0 and tv_sec = VMS condition code.
344 The return from function is also set to -1.
345 This is not exactly as per the manual page.
351 if (base_adjust[0]==0 && base_adjust[1]==0) {
353 if (base_adjust==0) { /* Need to determine epoch adjustment */
355 ret=sys$bintim(&dscepoch,&base_adjust);
356 if (1 != (ret &&1)) {
362 ret=sys$gettim(&quad); /* Get VMS system time */
363 if ((1 && ret) == 1) {
365 quad[0] -= base_adjust[0]; /* convert to epoch offset */
366 quad[1] -= base_adjust[1]; /* convert 2nd half of quadword */
367 div_100ns_to_secs = DIV_100NS_TO_SECS;
368 div_100ns_to_usecs = DIV_100NS_TO_USECS;
369 lib$ediv(&div_100ns_to_secs,&quad,&quo,&rem);
372 lib$ediv(&div_100ns_to_usecs,&quad1,&quo1,&rem1);
373 tp->tv_sec = quo; /* Whole seconds */
374 tp->tv_usec = quo1; /* Micro-seconds */
376 quad -= base_adjust; /* convert to epoch offset */
377 ans1=qdiv(quad,DIV_100NS_TO_SECS);
378 ans2=qdiv(ans1.rem,DIV_100NS_TO_USECS);
379 tp->tv_sec = ans1.quot; /* Whole seconds */
380 tp->tv_usec = ans2.quot; /* Micro-seconds */
388 if (VMSISH_TIME) tp->tv_sec = _toloc(tp->tv_sec);
390 if (!VMSISH_TIME) tp->tv_sec = _toutc(tp->tv_sec);
398 /* Do not use H A S _ N A N O S L E E P
399 * so that Perl Configure doesn't scan for it (and pull in -lrt and
400 * the like which are not usually good ideas for the default Perl).
401 * (We are part of the core perl now.)
402 * The TIME_HIRES_NANOSLEEP is set by Makefile.PL. */
403 #if !defined(HAS_USLEEP) && defined(TIME_HIRES_NANOSLEEP)
405 #define usleep hrt_usleep /* could conflict with ncurses for static build */
408 hrt_usleep(unsigned long usec) /* This is used to emulate usleep. */
411 res.tv_sec = usec / IV_1E6;
412 res.tv_nsec = ( usec - res.tv_sec * IV_1E6 ) * 1000;
413 nanosleep(&res, NULL);
416 #endif /* #if !defined(HAS_USLEEP) && defined(TIME_HIRES_NANOSLEEP) */
418 #if !defined(HAS_USLEEP) && defined(HAS_SELECT)
419 #ifndef SELECT_IS_BROKEN
421 #define usleep hrt_usleep /* could conflict with ncurses for static build */
424 hrt_usleep(unsigned long usec)
429 select(0, (Select_fd_set_t)NULL, (Select_fd_set_t)NULL,
430 (Select_fd_set_t)NULL, &tv);
433 #endif /* #if !defined(HAS_USLEEP) && defined(HAS_SELECT) */
435 #if !defined(HAS_USLEEP) && defined(WIN32)
437 #define usleep hrt_usleep /* could conflict with ncurses for static build */
440 hrt_usleep(unsigned long usec)
446 #endif /* #if !defined(HAS_USLEEP) && defined(WIN32) */
448 #if !defined(HAS_USLEEP) && defined(HAS_POLL)
450 #define usleep hrt_usleep /* could conflict with ncurses for static build */
453 hrt_usleep(unsigned long usec)
455 int msec = usec / 1000;
459 #endif /* #if !defined(HAS_USLEEP) && defined(HAS_POLL) */
461 #if defined(HAS_SETITIMER) && defined(ITIMER_REAL)
464 hrt_ualarm_itimero(struct itimerval* itv, int usec, int uinterval)
466 itv->it_value.tv_sec = usec / IV_1E6;
467 itv->it_value.tv_usec = usec % IV_1E6;
468 itv->it_interval.tv_sec = uinterval / IV_1E6;
469 itv->it_interval.tv_usec = uinterval % IV_1E6;
470 return setitimer(ITIMER_REAL, itv, 0);
474 hrt_ualarm_itimer(int usec, int uinterval)
476 struct itimerval itv;
477 return hrt_ualarm_itimero(&itv, usec, uinterval);
482 hrt_ualarm(int usec, int interval) /* for binary compat before 1.91 */
484 return hrt_ualarm_itimer(usec, interval);
486 #endif /* #ifdef HAS_UALARM */
487 #endif /* #if !defined(HAS_UALARM) && defined(HAS_SETITIMER) */
489 #if !defined(HAS_UALARM) && defined(HAS_SETITIMER)
491 #define ualarm hrt_ualarm_itimer /* could conflict with ncurses for static build */
494 #if !defined(HAS_UALARM) && defined(VMS)
496 #define ualarm vms_ualarm
498 #include <lib$routines.h>
506 #define VMSERR(s) (!((s)&1))
509 us_to_VMS(useconds_t mseconds, unsigned long v[])
518 iss = lib$addx(qq,qq,qq);
519 if (VMSERR(iss)) lib$signal(iss);
520 iss = lib$subx(v,qq,v);
521 if (VMSERR(iss)) lib$signal(iss);
522 iss = lib$addx(qq,qq,qq);
523 if (VMSERR(iss)) lib$signal(iss);
524 iss = lib$subx(v,qq,v);
525 if (VMSERR(iss)) lib$signal(iss);
526 iss = lib$subx(v,qq,v);
527 if (VMSERR(iss)) lib$signal(iss);
531 VMS_to_us(unsigned long v[])
534 unsigned long div=10,quot, rem;
536 iss = lib$ediv(&div,v,",&rem);
537 if (VMSERR(iss)) lib$signal(iss);
542 typedef unsigned short word;
543 typedef struct _ualarm {
546 unsigned long delay[2];
547 unsigned long interval[2];
548 unsigned long remain[2];
553 static Alarm *a0, alarm_base;
558 static void ualarm_AST(Alarm *a);
561 vms_ualarm(int mseconds, int interval)
570 static struct item_list3 itmlst[2];
571 static int first = 1;
577 itmlst[0].code = JPI$_ASTEN;
578 itmlst[0].length = sizeof(asten);
579 itmlst[0].retlenaddr = NULL;
581 itmlst[1].length = 0;
582 itmlst[1].bufaddr = NULL;
583 itmlst[1].retlenaddr = NULL;
585 iss = lib$get_ef(&alarm_ef);
586 if (VMSERR(iss)) lib$signal(iss);
589 a0->function = UAL_NULL;
591 itmlst[0].bufaddr = &asten;
593 iss = sys$getjpiw(0,0,0,itmlst,0,0,0);
594 if (VMSERR(iss)) lib$signal(iss);
595 if (!(asten&0x08)) return -1;
599 a->function = UAL_SET;
601 a->function = UAL_CLEAR;
604 us_to_VMS(mseconds, a->delay);
606 us_to_VMS(interval, a->interval);
611 iss = sys$clref(alarm_ef);
612 if (VMSERR(iss)) lib$signal(iss);
614 iss = sys$dclast(ualarm_AST,a,0);
615 if (VMSERR(iss)) lib$signal(iss);
617 iss = sys$waitfr(alarm_ef);
618 if (VMSERR(iss)) lib$signal(iss);
620 if (a->function == UAL_ACTIVE)
621 return VMS_to_us(a->remain);
632 unsigned long now[2];
634 iss = sys$gettim(now);
635 if (VMSERR(iss)) lib$signal(iss);
637 if (a->function == UAL_SET || a->function == UAL_CLEAR) {
638 if (a0->function == UAL_ACTIVE) {
639 iss = sys$cantim(a0,PSL$C_USER);
640 if (VMSERR(iss)) lib$signal(iss);
642 iss = lib$subx(a0->remain, now, a->remain);
643 if (VMSERR(iss)) lib$signal(iss);
645 if (a->remain[1] & 0x80000000)
646 a->remain[0] = a->remain[1] = 0;
649 if (a->function == UAL_SET) {
650 a->function = a0->function;
651 a0->function = UAL_ACTIVE;
652 a0->repeat = a->repeat;
654 a0->interval[0] = a->interval[0];
655 a0->interval[1] = a->interval[1];
657 a0->delay[0] = a->delay[0];
658 a0->delay[1] = a->delay[1];
660 iss = lib$subx(now, a0->delay, a0->remain);
661 if (VMSERR(iss)) lib$signal(iss);
663 iss = sys$setimr(0,a0->delay,ualarm_AST,a0);
664 if (VMSERR(iss)) lib$signal(iss);
666 a->function = a0->function;
667 a0->function = UAL_NULL;
669 iss = sys$setef(alarm_ef);
670 if (VMSERR(iss)) lib$signal(iss);
671 } else if (a->function == UAL_ACTIVE) {
673 iss = lib$subx(now, a->interval, a->remain);
674 if (VMSERR(iss)) lib$signal(iss);
676 iss = sys$setimr(0,a->interval,ualarm_AST,a);
677 if (VMSERR(iss)) lib$signal(iss);
679 a->function = UAL_NULL;
682 if (VMSERR(iss)) lib$signal(iss);
683 lib$signal(SS$_ASTFLT);
685 lib$signal(SS$_BADPARAM);
689 #endif /* #if !defined(HAS_UALARM) && defined(VMS) */
691 #ifdef HAS_GETTIMEOFDAY
694 myU2time(pTHX_ UV *ret)
698 status = gettimeofday (&Tp, NULL);
712 status = gettimeofday (&Tp, NULL);
713 return status == 0 ? Tp.tv_sec + (Tp.tv_usec / NV_1E6) : -1.0;
716 #endif /* #ifdef HAS_GETTIMEOFDAY */
719 hrstatns(UV atime, UV mtime, UV ctime, UV *atime_nsec, UV *mtime_nsec, UV *ctime_nsec)
725 #ifdef TIME_HIRES_STAT
726 #if TIME_HIRES_STAT == 1
727 *atime_nsec = PL_statcache.st_atimespec.tv_nsec;
728 *mtime_nsec = PL_statcache.st_mtimespec.tv_nsec;
729 *ctime_nsec = PL_statcache.st_ctimespec.tv_nsec;
731 #if TIME_HIRES_STAT == 2
732 *atime_nsec = PL_statcache.st_atimensec;
733 *mtime_nsec = PL_statcache.st_mtimensec;
734 *ctime_nsec = PL_statcache.st_ctimensec;
736 #if TIME_HIRES_STAT == 3
737 *atime_nsec = PL_statcache.st_atime_n;
738 *mtime_nsec = PL_statcache.st_mtime_n;
739 *ctime_nsec = PL_statcache.st_ctime_n;
741 #if TIME_HIRES_STAT == 4
742 *atime_nsec = PL_statcache.st_atim.tv_nsec;
743 *mtime_nsec = PL_statcache.st_mtim.tv_nsec;
744 *ctime_nsec = PL_statcache.st_ctim.tv_nsec;
746 #if TIME_HIRES_STAT == 5
747 *atime_nsec = PL_statcache.st_uatime * 1000;
748 *mtime_nsec = PL_statcache.st_umtime * 1000;
749 *ctime_nsec = PL_statcache.st_uctime * 1000;
754 #include "const-c.inc"
756 MODULE = Time::HiRes PACKAGE = Time::HiRes
765 #ifdef ATLEASTFIVEOHOHFIVE
766 # ifdef HAS_GETTIMEOFDAY
768 hv_store(PL_modglobal, "Time::NVtime", 12, newSViv(PTR2IV(myNVtime)), 0);
769 hv_store(PL_modglobal, "Time::U2time", 12, newSViv(PTR2IV(myU2time)), 0);
775 #if defined(USE_ITHREADS) && defined(MY_CXT_KEY)
784 INCLUDE: const-xs.inc
786 #if defined(HAS_USLEEP) && defined(HAS_GETTIMEOFDAY)
792 struct timeval Ta, Tb;
794 gettimeofday(&Ta, NULL);
796 if (useconds > 1E6) {
797 IV seconds = (IV) (useconds / 1E6);
798 /* If usleep() has been implemented using setitimer()
799 * then this contortion is unnecessary-- but usleep()
800 * may be implemented in some other way, so let's contort. */
803 useconds -= 1E6 * seconds;
805 } else if (useconds < 0.0)
806 croak("Time::HiRes::usleep(%"NVgf"): negative time not invented yet", useconds);
807 usleep((U32)useconds);
810 gettimeofday(&Tb, NULL);
812 printf("[%ld %ld] [%ld %ld]\n", Tb.tv_sec, Tb.tv_usec, Ta.tv_sec, Ta.tv_usec);
814 RETVAL = 1E6*(Tb.tv_sec-Ta.tv_sec)+(NV)((IV)Tb.tv_usec-(IV)Ta.tv_usec);
819 #if defined(TIME_HIRES_NANOSLEEP)
825 struct timespec sleepfor, unslept;
828 croak("Time::HiRes::nanosleep(%"NVgf"): negative time not invented yet", nsec);
829 sleepfor.tv_sec = (Time_t)(nsec / 1e9);
830 sleepfor.tv_nsec = (long)(nsec - ((NV)sleepfor.tv_sec) * 1e9);
831 if (!nanosleep(&sleepfor, &unslept)) {
834 sleepfor.tv_sec -= unslept.tv_sec;
835 sleepfor.tv_nsec -= unslept.tv_nsec;
836 if (sleepfor.tv_nsec < 0) {
838 sleepfor.tv_nsec += 1000000000;
840 RETVAL = ((NV)sleepfor.tv_sec) * 1e9 + ((NV)sleepfor.tv_nsec);
845 #else /* #if defined(TIME_HIRES_NANOSLEEP) */
851 croak("Time::HiRes::nanosleep(): unimplemented in this platform");
854 #endif /* #if defined(TIME_HIRES_NANOSLEEP) */
859 struct timeval Ta, Tb;
861 gettimeofday(&Ta, NULL);
863 NV seconds = SvNV(ST(0));
864 if (seconds >= 0.0) {
865 UV useconds = (UV)(1E6 * (seconds - (UV)seconds));
868 if ((IV)useconds < 0) {
869 #if defined(__sparc64__) && defined(__GNUC__)
870 /* Sparc64 gcc 2.95.3 (e.g. on NetBSD) has a bug
871 * where (0.5 - (UV)(0.5)) will under certain
872 * circumstances (if the double is cast to UV more
873 * than once?) evaluate to -0.5, instead of 0.5. */
874 useconds = -(IV)useconds;
875 #endif /* #if defined(__sparc64__) && defined(__GNUC__) */
876 if ((IV)useconds < 0)
877 croak("Time::HiRes::sleep(%"NVgf"): internal error: useconds < 0 (unsigned %"UVuf" signed %"IVdf")", seconds, useconds, (IV)useconds);
881 croak("Time::HiRes::sleep(%"NVgf"): negative time not invented yet", seconds);
884 gettimeofday(&Tb, NULL);
886 printf("[%ld %ld] [%ld %ld]\n", Tb.tv_sec, Tb.tv_usec, Ta.tv_sec, Ta.tv_usec);
888 RETVAL = (NV)(Tb.tv_sec-Ta.tv_sec)+0.000001*(NV)(Tb.tv_usec-Ta.tv_usec);
893 #else /* #if defined(HAS_USLEEP) && defined(HAS_GETTIMEOFDAY) */
899 croak("Time::HiRes::usleep(): unimplemented in this platform");
902 #endif /* #if defined(HAS_USLEEP) && defined(HAS_GETTIMEOFDAY) */
907 ualarm(useconds,uinterval=0)
911 if (useconds < 0 || uinterval < 0)
912 croak("Time::HiRes::ualarm(%d, %d): negative time not invented yet", useconds, uinterval);
913 #if defined(HAS_SETITIMER) && defined(ITIMER_REAL)
915 struct itimerval itv;
916 if (hrt_ualarm_itimero(&itv, useconds, uinterval)) {
917 RETVAL = itv.it_value.tv_sec + IV_1E6 * itv.it_value.tv_usec;
923 if (useconds >= IV_1E6 || uinterval >= IV_1E6)
924 croak("Time::HiRes::ualarm(%d, %d): useconds or uinterval equal to or more than %"IVdf, useconds, uinterval, IV_1E6);
925 RETVAL = ualarm(useconds, uinterval);
932 alarm(seconds,interval=0)
936 if (seconds < 0.0 || interval < 0.0)
937 croak("Time::HiRes::alarm(%"NVgf", %"NVgf"): negative time not invented yet", seconds, interval);
939 IV useconds = IV_1E6 * seconds;
940 IV uinterval = IV_1E6 * interval;
941 #if defined(HAS_SETITIMER) && defined(ITIMER_REAL)
943 struct itimerval itv;
944 if (hrt_ualarm_itimero(&itv, useconds, uinterval)) {
945 RETVAL = (NV)itv.it_value.tv_sec + (NV)itv.it_value.tv_usec / NV_1E6;
951 if (useconds >= IV_1E6 || uinterval >= IV_1E6)
952 croak("Time::HiRes::alarm(%d, %d): seconds or interval equal to or more than 1.0 ", useconds, uinterval, IV_1E6);
953 RETVAL = (NV)ualarm( useconds, uinterval ) / NV_1E6;
963 ualarm(useconds,interval=0)
967 croak("Time::HiRes::ualarm(): unimplemented in this platform");
971 alarm(seconds,interval=0)
975 croak("Time::HiRes::alarm(): unimplemented in this platform");
978 #endif /* #ifdef HAS_UALARM */
980 #ifdef HAS_GETTIMEOFDAY
981 # ifdef MACOS_TRADITIONAL /* fix epoch TZ and use unsigned time_t */
989 status = gettimeofday (&Tp, &Tz);
992 Tp.tv_sec += Tz.tz_minuteswest * 60; /* adjust for TZ */
993 if (GIMME == G_ARRAY) {
995 /* Mac OS (Classic) has unsigned time_t */
996 PUSHs(sv_2mortal(newSVuv(Tp.tv_sec)));
997 PUSHs(sv_2mortal(newSViv(Tp.tv_usec)));
1000 PUSHs(sv_2mortal(newSVnv(Tp.tv_sec + (Tp.tv_usec / NV_1E6))));
1011 status = gettimeofday (&Tp, &Tz);
1013 Tp.tv_sec += Tz.tz_minuteswest * 60; /* adjust for TZ */
1014 RETVAL = Tp.tv_sec + (Tp.tv_usec / NV_1E6);
1021 # else /* MACOS_TRADITIONAL */
1028 status = gettimeofday (&Tp, NULL);
1030 if (GIMME == G_ARRAY) {
1032 PUSHs(sv_2mortal(newSViv(Tp.tv_sec)));
1033 PUSHs(sv_2mortal(newSViv(Tp.tv_usec)));
1036 PUSHs(sv_2mortal(newSVnv(Tp.tv_sec + (Tp.tv_usec / NV_1E6))));
1046 status = gettimeofday (&Tp, NULL);
1048 RETVAL = Tp.tv_sec + (Tp.tv_usec / NV_1E6);
1055 # endif /* MACOS_TRADITIONAL */
1056 #endif /* #ifdef HAS_GETTIMEOFDAY */
1058 #if defined(HAS_GETITIMER) && defined(HAS_SETITIMER)
1060 #define TV2NV(tv) ((NV)((tv).tv_sec) + 0.000001 * (NV)((tv).tv_usec))
1063 setitimer(which, seconds, interval = 0)
1068 struct itimerval newit;
1069 struct itimerval oldit;
1071 if (seconds < 0.0 || interval < 0.0)
1072 croak("Time::HiRes::setitimer(%"IVdf", %"NVgf", %"NVgf"): negative time not invented yet", (IV)which, seconds, interval);
1073 newit.it_value.tv_sec = (IV)seconds;
1074 newit.it_value.tv_usec =
1075 (IV)((seconds - (NV)newit.it_value.tv_sec) * NV_1E6);
1076 newit.it_interval.tv_sec = (IV)interval;
1077 newit.it_interval.tv_usec =
1078 (IV)((interval - (NV)newit.it_interval.tv_sec) * NV_1E6);
1079 if (setitimer(which, &newit, &oldit) == 0) {
1081 PUSHs(sv_2mortal(newSVnv(TV2NV(oldit.it_value))));
1082 if (GIMME == G_ARRAY) {
1084 PUSHs(sv_2mortal(newSVnv(TV2NV(oldit.it_interval))));
1092 struct itimerval nowit;
1094 if (getitimer(which, &nowit) == 0) {
1096 PUSHs(sv_2mortal(newSVnv(TV2NV(nowit.it_value))));
1097 if (GIMME == G_ARRAY) {
1099 PUSHs(sv_2mortal(newSVnv(TV2NV(nowit.it_interval))));
1103 #endif /* #if defined(HAS_GETITIMER) && defined(HAS_SETITIMER) */
1105 #if defined(TIME_HIRES_CLOCK_GETTIME)
1108 clock_gettime(clock_id = CLOCK_REALTIME)
1114 #ifdef TIME_HIRES_CLOCK_GETTIME_SYSCALL
1115 status = syscall(SYS_clock_gettime, clock_id, &ts);
1117 status = clock_gettime(clock_id, &ts);
1119 RETVAL = status == 0 ? ts.tv_sec + (NV) ts.tv_nsec / (NV) 1e9 : -1;
1124 #else /* if defined(TIME_HIRES_CLOCK_GETTIME) */
1127 clock_gettime(clock_id = 0)
1130 croak("Time::HiRes::clock_gettime(): unimplemented in this platform");
1133 #endif /* #if defined(TIME_HIRES_CLOCK_GETTIME) */
1135 #if defined(TIME_HIRES_CLOCK_GETRES)
1138 clock_getres(clock_id = CLOCK_REALTIME)
1144 #ifdef TIME_HIRES_CLOCK_GETRES_SYSCALL
1145 status = syscall(SYS_clock_getres, clock_id, &ts);
1147 status = clock_getres(clock_id, &ts);
1149 RETVAL = status == 0 ? ts.tv_sec + (NV) ts.tv_nsec / (NV) 1e9 : -1;
1154 #else /* if defined(TIME_HIRES_CLOCK_GETRES) */
1157 clock_getres(clock_id = 0)
1160 croak("Time::HiRes::clock_getres(): unimplemented in this platform");
1163 #endif /* #if defined(TIME_HIRES_CLOCK_GETRES) */
1165 #if defined(TIME_HIRES_CLOCK_NANOSLEEP) && defined(TIMER_ABSTIME)
1168 clock_nanosleep(clock_id, nsec, flags = 0)
1173 struct timespec sleepfor, unslept;
1176 croak("Time::HiRes::clock_nanosleep(..., %"NVgf"): negative time not invented yet", nsec);
1177 sleepfor.tv_sec = (Time_t)(nsec / 1e9);
1178 sleepfor.tv_nsec = (long)(nsec - ((NV)sleepfor.tv_sec) * 1e9);
1179 if (!clock_nanosleep(clock_id, flags, &sleepfor, &unslept)) {
1182 sleepfor.tv_sec -= unslept.tv_sec;
1183 sleepfor.tv_nsec -= unslept.tv_nsec;
1184 if (sleepfor.tv_nsec < 0) {
1186 sleepfor.tv_nsec += 1000000000;
1188 RETVAL = ((NV)sleepfor.tv_sec) * 1e9 + ((NV)sleepfor.tv_nsec);
1193 #else /* if defined(TIME_HIRES_CLOCK_NANOSLEEP) && defined(TIMER_ABSTIME) */
1198 croak("Time::HiRes::clock_nanosleep(): unimplemented in this platform");
1201 #endif /* #if defined(TIME_HIRES_CLOCK_NANOSLEEP) && defined(TIMER_ABSTIME) */
1203 #if defined(TIME_HIRES_CLOCK) && defined(CLOCKS_PER_SEC)
1211 RETVAL = clocks == -1 ? -1 : (NV)clocks / (NV)CLOCKS_PER_SEC;
1216 #else /* if defined(TIME_HIRES_CLOCK) && defined(CLOCKS_PER_SEC) */
1221 croak("Time::HiRes::clock(): unimplemented in this platform");
1224 #endif /* #if defined(TIME_HIRES_CLOCK) && defined(CLOCKS_PER_SEC) */
1231 XPUSHs(sv_2mortal(newSVsv(items == 1 ? ST(0) : DEFSV)));
1234 PL_laststatval = -1;
1235 (void)*(PL_ppaddr[OP_STAT])(aTHXR);
1238 if (PL_laststatval == 0) {
1239 /* We assume that pp_stat() left us with 13 valid stack items,
1240 * and that the timestamps are at offsets 8, 9, and 10. */
1241 UV atime = SvUV(ST( 8));
1242 UV mtime = SvUV(ST( 9));
1243 UV ctime = SvUV(ST(10));
1247 hrstatns(atime, mtime, ctime,
1248 &atime_nsec, &mtime_nsec, &ctime_nsec);
1250 ST( 8) = sv_2mortal(newSVnv(atime + 1e-9 * (NV) atime_nsec));
1252 ST( 9) = sv_2mortal(newSVnv(mtime + 1e-9 * (NV) mtime_nsec));
1254 ST(10) = sv_2mortal(newSVnv(ctime + 1e-9 * (NV) ctime_nsec));