3 * Copyright (c) 1996-2002 Douglas E. Wegscheid. All rights reserved.
5 * Copyright (c) 2002-2010 Jarkko Hietaniemi.
8 * Copyright (C) 2011 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 /* At least ppport.h 3.13 gets this wrong: one really cannot
44 * have NVgf as anything else than "g" under Perl 5.6.x. */
45 #if PERL_REVISION == 5 && PERL_VERSION == 6
50 #define IV_1E6 1000000
51 #define IV_1E7 10000000
52 #define IV_1E9 1000000000
54 #define NV_1E6 1000000.0
55 #define NV_1E7 10000000.0
56 #define NV_1E9 1000000000.0
58 #ifndef PerlProc_pause
59 # define PerlProc_pause() Pause()
65 # undef Pause /* In case perl.h did it already. */
66 # define Pause() sleep(~0) /* Zzz for a long time. */
69 /* Though the cpp define ITIMER_VIRTUAL is available the functionality
70 * is not supported in Cygwin as of August 2004, ditto for Win32.
71 * Neither are ITIMER_PROF or ITIMER_REALPROF implemented. --jhi
73 #if defined(__CYGWIN__) || defined(WIN32)
74 # undef ITIMER_VIRTUAL
76 # undef ITIMER_REALPROF
79 #if defined(TIME_HIRES_CLOCK_GETTIME) && defined(_STRUCT_ITIMERSPEC)
81 /* HP-UX has CLOCK_XXX values but as enums, not as defines.
82 * The only way to detect these would be to test compile for each. */
84 /* However, it seems that at least in HP-UX 11.31 ia64 there *are*
85 * defines for these, so let's try detecting them. */
86 # ifndef CLOCK_REALTIME
87 # define CLOCK_REALTIME CLOCK_REALTIME
88 # define CLOCK_VIRTUAL CLOCK_VIRTUAL
89 # define CLOCK_PROFILE CLOCK_PROFILE
91 # endif /* # ifdef __hpux */
93 #endif /* #if defined(TIME_HIRES_CLOCK_GETTIME) && defined(_STRUCT_ITIMERSPEC) */
95 #if defined(WIN32) || defined(CYGWIN_WITH_W32API)
97 #ifndef HAS_GETTIMEOFDAY
98 # define HAS_GETTIMEOFDAY
101 /* shows up in winsock.h?
109 unsigned __int64 ft_i64;
113 #define MY_CXT_KEY "Time::HiRes_" XS_VERSION
116 unsigned long run_count;
117 unsigned __int64 base_ticks;
118 unsigned __int64 tick_frequency;
119 FT_t base_systime_as_filetime;
120 unsigned __int64 reset_time;
125 /* Number of 100 nanosecond units from 1/1/1601 to 1/1/1970 */
127 # define Const64(x) x##LL
129 # define Const64(x) x##i64
131 #define EPOCH_BIAS Const64(116444736000000000)
135 # define IV_1E6LL 1000000LL /* Needed because of Const64() ##-appends LL (or i64). */
136 # define IV_1E7LL 10000000LL
137 # define IV_1E9LL 1000000000LL
139 # define IV_1E6i64 1000000i64
140 # define IV_1E7i64 10000000i64
141 # define IV_1E9i64 1000000000i64
145 /* NOTE: This does not compute the timezone info (doing so can be expensive,
146 * and appears to be unsupported even by glibc) */
148 /* dMY_CXT needs a Perl context and we don't want to call PERL_GET_CONTEXT
149 for performance reasons */
152 #define gettimeofday(tp, not_used) _gettimeofday(aTHX_ tp, not_used)
154 /* If the performance counter delta drifts more than 0.5 seconds from the
155 * system time then we recalibrate to the system time. This means we may
156 * move *backwards* in time! */
157 #define MAX_PERF_COUNTER_SKEW Const64(5000000) /* 0.5 seconds */
159 /* Reset reading from the performance counter every five minutes.
160 * Many PC clocks just seem to be so bad. */
161 #define MAX_PERF_COUNTER_TICKS Const64(300000000) /* 300 seconds */
164 _gettimeofday(pTHX_ struct timeval *tp, void *not_used)
168 unsigned __int64 ticks;
171 if (MY_CXT.run_count++ == 0 ||
172 MY_CXT.base_systime_as_filetime.ft_i64 > MY_CXT.reset_time) {
173 QueryPerformanceFrequency((LARGE_INTEGER*)&MY_CXT.tick_frequency);
174 QueryPerformanceCounter((LARGE_INTEGER*)&MY_CXT.base_ticks);
175 GetSystemTimeAsFileTime(&MY_CXT.base_systime_as_filetime.ft_val);
176 ft.ft_i64 = MY_CXT.base_systime_as_filetime.ft_i64;
177 MY_CXT.reset_time = ft.ft_i64 + MAX_PERF_COUNTER_TICKS;
181 QueryPerformanceCounter((LARGE_INTEGER*)&ticks);
182 ticks -= MY_CXT.base_ticks;
183 ft.ft_i64 = MY_CXT.base_systime_as_filetime.ft_i64
184 + Const64(IV_1E7) * (ticks / MY_CXT.tick_frequency)
185 +(Const64(IV_1E7) * (ticks % MY_CXT.tick_frequency)) / MY_CXT.tick_frequency;
186 diff = ft.ft_i64 - MY_CXT.base_systime_as_filetime.ft_i64;
187 if (diff < -MAX_PERF_COUNTER_SKEW || diff > MAX_PERF_COUNTER_SKEW) {
188 MY_CXT.base_ticks += ticks;
189 GetSystemTimeAsFileTime(&MY_CXT.base_systime_as_filetime.ft_val);
190 ft.ft_i64 = MY_CXT.base_systime_as_filetime.ft_i64;
194 /* seconds since epoch */
195 tp->tv_sec = (long)((ft.ft_i64 - EPOCH_BIAS) / Const64(IV_1E7));
197 /* microseconds remaining */
198 tp->tv_usec = (long)((ft.ft_i64 / Const64(10)) % Const64(IV_1E6));
204 #if defined(WIN32) && !defined(ATLEASTFIVEOHOHFIVE)
206 sleep(unsigned int t)
213 #if !defined(HAS_GETTIMEOFDAY) && defined(VMS)
214 #define HAS_GETTIMEOFDAY
217 #include <time.h> /* gettimeofday */
218 #include <stdlib.h> /* qdiv */
219 #include <starlet.h> /* sys$gettim */
222 #include <lib$routines.h> /* lib$ediv() */
226 VMS binary time is expressed in 100 nano-seconds since
227 system base time which is 17-NOV-1858 00:00:00.00
230 #define DIV_100NS_TO_SECS 10000000L
231 #define DIV_100NS_TO_USECS 10L
234 gettimeofday is supposed to return times since the epoch
235 so need to determine this in terms of VMS base time
237 static $DESCRIPTOR(dscepoch,"01-JAN-1970 00:00:00.00");
240 static long base_adjust[2]={0L,0L};
242 static __int64 base_adjust=0;
247 If we don't have gettimeofday, then likely we are on a VMS machine that
248 operates on local time rather than UTC...so we have to zone-adjust.
249 This code gleefully swiped from VMS.C
252 /* method used to handle UTC conversions:
253 * 1 == CRTL gmtime(); 2 == SYS$TIMEZONE_DIFFERENTIAL; 3 == no correction
255 static int gmtime_emulation_type;
256 /* number of secs to add to UTC POSIX-style time to get local time */
257 static long int utc_offset_secs;
258 static struct dsc$descriptor_s fildevdsc =
259 { 12, DSC$K_DTYPE_T, DSC$K_CLASS_S, "LNM$FILE_DEV" };
260 static struct dsc$descriptor_s *fildev[] = { &fildevdsc, NULL };
262 static time_t toutc_dst(time_t loc) {
265 if ((rsltmp = localtime(&loc)) == NULL) return -1;
266 loc -= utc_offset_secs;
267 if (rsltmp->tm_isdst) loc -= 3600;
271 static time_t toloc_dst(time_t utc) {
274 utc += utc_offset_secs;
275 if ((rsltmp = localtime(&utc)) == NULL) return -1;
276 if (rsltmp->tm_isdst) utc += 3600;
280 #define _toutc(secs) ((secs) == (time_t) -1 ? (time_t) -1 : \
281 ((gmtime_emulation_type || timezone_setup()), \
282 (gmtime_emulation_type == 1 ? toutc_dst(secs) : \
283 ((secs) - utc_offset_secs))))
285 #define _toloc(secs) ((secs) == (time_t) -1 ? (time_t) -1 : \
286 ((gmtime_emulation_type || timezone_setup()), \
287 (gmtime_emulation_type == 1 ? toloc_dst(secs) : \
288 ((secs) + utc_offset_secs))))
295 if (gmtime_emulation_type == 0) {
297 time_t base = 15 * 86400; /* 15jan71; to avoid month/year ends between */
298 /* results of calls to gmtime() and localtime() */
301 gmtime_emulation_type++;
302 if ((tm_p = gmtime(&base)) == NULL) { /* CRTL gmtime() is a fake */
303 char off[LNM$C_NAMLENGTH+1];;
305 gmtime_emulation_type++;
306 if (!Perl_vmstrnenv("SYS$TIMEZONE_DIFFERENTIAL",off,0,fildev,0)) {
307 gmtime_emulation_type++;
309 Perl_warn(aTHX_ "no UTC offset information; assuming local time is UTC");
311 else { utc_offset_secs = atol(off); }
313 else { /* We've got a working gmtime() */
314 struct tm gmt, local;
317 tm_p = localtime(&base);
319 utc_offset_secs = (local.tm_mday - gmt.tm_mday) * 86400;
320 utc_offset_secs += (local.tm_hour - gmt.tm_hour) * 3600;
321 utc_offset_secs += (local.tm_min - gmt.tm_min) * 60;
322 utc_offset_secs += (local.tm_sec - gmt.tm_sec);
330 gettimeofday (struct timeval *tp, void *tpz)
336 long div_100ns_to_secs;
337 long div_100ns_to_usecs;
345 In case of error, tv_usec = 0 and tv_sec = VMS condition code.
346 The return from function is also set to -1.
347 This is not exactly as per the manual page.
353 if (base_adjust[0]==0 && base_adjust[1]==0) {
355 if (base_adjust==0) { /* Need to determine epoch adjustment */
357 ret=sys$bintim(&dscepoch,&base_adjust);
358 if (1 != (ret &&1)) {
364 ret=sys$gettim(&quad); /* Get VMS system time */
365 if ((1 && ret) == 1) {
367 quad[0] -= base_adjust[0]; /* convert to epoch offset */
368 quad[1] -= base_adjust[1]; /* convert 2nd half of quadword */
369 div_100ns_to_secs = DIV_100NS_TO_SECS;
370 div_100ns_to_usecs = DIV_100NS_TO_USECS;
371 lib$ediv(&div_100ns_to_secs,&quad,&quo,&rem);
374 lib$ediv(&div_100ns_to_usecs,&quad1,&quo1,&rem1);
375 tp->tv_sec = quo; /* Whole seconds */
376 tp->tv_usec = quo1; /* Micro-seconds */
378 quad -= base_adjust; /* convert to epoch offset */
379 ans1=qdiv(quad,DIV_100NS_TO_SECS);
380 ans2=qdiv(ans1.rem,DIV_100NS_TO_USECS);
381 tp->tv_sec = ans1.quot; /* Whole seconds */
382 tp->tv_usec = ans2.quot; /* Micro-seconds */
390 if (VMSISH_TIME) tp->tv_sec = _toloc(tp->tv_sec);
392 if (!VMSISH_TIME) tp->tv_sec = _toutc(tp->tv_sec);
400 /* Do not use H A S _ N A N O S L E E P
401 * so that Perl Configure doesn't scan for it (and pull in -lrt and
402 * the like which are not usually good ideas for the default Perl).
403 * (We are part of the core perl now.)
404 * The TIME_HIRES_NANOSLEEP is set by Makefile.PL. */
405 #if !defined(HAS_USLEEP) && defined(TIME_HIRES_NANOSLEEP)
407 #define usleep hrt_usleep /* could conflict with ncurses for static build */
410 hrt_usleep(unsigned long usec) /* This is used to emulate usleep. */
413 res.tv_sec = usec / IV_1E6;
414 res.tv_nsec = ( usec - res.tv_sec * IV_1E6 ) * 1000;
415 nanosleep(&res, NULL);
418 #endif /* #if !defined(HAS_USLEEP) && defined(TIME_HIRES_NANOSLEEP) */
420 #if !defined(HAS_USLEEP) && defined(HAS_SELECT)
421 #ifndef SELECT_IS_BROKEN
423 #define usleep hrt_usleep /* could conflict with ncurses for static build */
426 hrt_usleep(unsigned long usec)
431 select(0, (Select_fd_set_t)NULL, (Select_fd_set_t)NULL,
432 (Select_fd_set_t)NULL, &tv);
435 #endif /* #if !defined(HAS_USLEEP) && defined(HAS_SELECT) */
437 #if !defined(HAS_USLEEP) && defined(WIN32)
439 #define usleep hrt_usleep /* could conflict with ncurses for static build */
442 hrt_usleep(unsigned long usec)
448 #endif /* #if !defined(HAS_USLEEP) && defined(WIN32) */
450 #if !defined(HAS_USLEEP) && defined(HAS_POLL)
452 #define usleep hrt_usleep /* could conflict with ncurses for static build */
455 hrt_usleep(unsigned long usec)
457 int msec = usec / 1000;
461 #endif /* #if !defined(HAS_USLEEP) && defined(HAS_POLL) */
463 #if defined(HAS_SETITIMER) && defined(ITIMER_REAL)
466 hrt_ualarm_itimero(struct itimerval *oitv, int usec, int uinterval)
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 = uinterval / IV_1E6;
472 itv.it_interval.tv_usec = uinterval % IV_1E6;
473 return setitimer(ITIMER_REAL, &itv, oitv);
477 hrt_ualarm_itimer(int usec, int uinterval)
479 return hrt_ualarm_itimero(NULL, usec, uinterval);
484 hrt_ualarm(int usec, int interval) /* for binary compat before 1.91 */
486 return hrt_ualarm_itimer(usec, interval);
488 #endif /* #ifdef HAS_UALARM */
489 #endif /* #if !defined(HAS_UALARM) && defined(HAS_SETITIMER) */
491 #if !defined(HAS_UALARM) && defined(HAS_SETITIMER)
493 #define ualarm hrt_ualarm_itimer /* could conflict with ncurses for static build */
496 #if !defined(HAS_UALARM) && defined(VMS)
498 #define ualarm vms_ualarm
500 #include <lib$routines.h>
508 #define VMSERR(s) (!((s)&1))
511 us_to_VMS(useconds_t mseconds, unsigned long v[])
520 iss = lib$addx(qq,qq,qq);
521 if (VMSERR(iss)) lib$signal(iss);
522 iss = lib$subx(v,qq,v);
523 if (VMSERR(iss)) lib$signal(iss);
524 iss = lib$addx(qq,qq,qq);
525 if (VMSERR(iss)) lib$signal(iss);
526 iss = lib$subx(v,qq,v);
527 if (VMSERR(iss)) lib$signal(iss);
528 iss = lib$subx(v,qq,v);
529 if (VMSERR(iss)) lib$signal(iss);
533 VMS_to_us(unsigned long v[])
536 unsigned long div=10,quot, rem;
538 iss = lib$ediv(&div,v,",&rem);
539 if (VMSERR(iss)) lib$signal(iss);
544 typedef unsigned short word;
545 typedef struct _ualarm {
548 unsigned long delay[2];
549 unsigned long interval[2];
550 unsigned long remain[2];
555 static Alarm *a0, alarm_base;
560 static void ualarm_AST(Alarm *a);
563 vms_ualarm(int mseconds, int interval)
572 static struct item_list3 itmlst[2];
573 static int first = 1;
579 itmlst[0].code = JPI$_ASTEN;
580 itmlst[0].length = sizeof(asten);
581 itmlst[0].retlenaddr = NULL;
583 itmlst[1].length = 0;
584 itmlst[1].bufaddr = NULL;
585 itmlst[1].retlenaddr = NULL;
587 iss = lib$get_ef(&alarm_ef);
588 if (VMSERR(iss)) lib$signal(iss);
591 a0->function = UAL_NULL;
593 itmlst[0].bufaddr = &asten;
595 iss = sys$getjpiw(0,0,0,itmlst,0,0,0);
596 if (VMSERR(iss)) lib$signal(iss);
597 if (!(asten&0x08)) return -1;
601 a->function = UAL_SET;
603 a->function = UAL_CLEAR;
606 us_to_VMS(mseconds, a->delay);
608 us_to_VMS(interval, a->interval);
613 iss = sys$clref(alarm_ef);
614 if (VMSERR(iss)) lib$signal(iss);
616 iss = sys$dclast(ualarm_AST,a,0);
617 if (VMSERR(iss)) lib$signal(iss);
619 iss = sys$waitfr(alarm_ef);
620 if (VMSERR(iss)) lib$signal(iss);
622 if (a->function == UAL_ACTIVE)
623 return VMS_to_us(a->remain);
634 unsigned long now[2];
636 iss = sys$gettim(now);
637 if (VMSERR(iss)) lib$signal(iss);
639 if (a->function == UAL_SET || a->function == UAL_CLEAR) {
640 if (a0->function == UAL_ACTIVE) {
641 iss = sys$cantim(a0,PSL$C_USER);
642 if (VMSERR(iss)) lib$signal(iss);
644 iss = lib$subx(a0->remain, now, a->remain);
645 if (VMSERR(iss)) lib$signal(iss);
647 if (a->remain[1] & 0x80000000)
648 a->remain[0] = a->remain[1] = 0;
651 if (a->function == UAL_SET) {
652 a->function = a0->function;
653 a0->function = UAL_ACTIVE;
654 a0->repeat = a->repeat;
656 a0->interval[0] = a->interval[0];
657 a0->interval[1] = a->interval[1];
659 a0->delay[0] = a->delay[0];
660 a0->delay[1] = a->delay[1];
662 iss = lib$subx(now, a0->delay, a0->remain);
663 if (VMSERR(iss)) lib$signal(iss);
665 iss = sys$setimr(0,a0->delay,ualarm_AST,a0);
666 if (VMSERR(iss)) lib$signal(iss);
668 a->function = a0->function;
669 a0->function = UAL_NULL;
671 iss = sys$setef(alarm_ef);
672 if (VMSERR(iss)) lib$signal(iss);
673 } else if (a->function == UAL_ACTIVE) {
675 iss = lib$subx(now, a->interval, a->remain);
676 if (VMSERR(iss)) lib$signal(iss);
678 iss = sys$setimr(0,a->interval,ualarm_AST,a);
679 if (VMSERR(iss)) lib$signal(iss);
681 a->function = UAL_NULL;
684 if (VMSERR(iss)) lib$signal(iss);
685 lib$signal(SS$_ASTFLT);
687 lib$signal(SS$_BADPARAM);
691 #endif /* #if !defined(HAS_UALARM) && defined(VMS) */
693 #ifdef HAS_GETTIMEOFDAY
696 myU2time(pTHX_ UV *ret)
700 status = gettimeofday (&Tp, NULL);
714 status = gettimeofday (&Tp, NULL);
715 return status == 0 ? Tp.tv_sec + (Tp.tv_usec / NV_1E6) : -1.0;
718 #endif /* #ifdef HAS_GETTIMEOFDAY */
721 hrstatns(UV *atime_nsec, UV *mtime_nsec, UV *ctime_nsec)
724 #if TIME_HIRES_STAT == 1
725 *atime_nsec = PL_statcache.st_atimespec.tv_nsec;
726 *mtime_nsec = PL_statcache.st_mtimespec.tv_nsec;
727 *ctime_nsec = PL_statcache.st_ctimespec.tv_nsec;
728 #elif TIME_HIRES_STAT == 2
729 *atime_nsec = PL_statcache.st_atimensec;
730 *mtime_nsec = PL_statcache.st_mtimensec;
731 *ctime_nsec = PL_statcache.st_ctimensec;
732 #elif TIME_HIRES_STAT == 3
733 *atime_nsec = PL_statcache.st_atime_n;
734 *mtime_nsec = PL_statcache.st_mtime_n;
735 *ctime_nsec = PL_statcache.st_ctime_n;
736 #elif 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;
740 #elif TIME_HIRES_STAT == 5
741 *atime_nsec = PL_statcache.st_uatime * 1000;
742 *mtime_nsec = PL_statcache.st_umtime * 1000;
743 *ctime_nsec = PL_statcache.st_uctime * 1000;
744 #else /* !TIME_HIRES_STAT */
748 #endif /* !TIME_HIRES_STAT */
751 #include "const-c.inc"
753 MODULE = Time::HiRes PACKAGE = Time::HiRes
762 #ifdef ATLEASTFIVEOHOHFIVE
763 # ifdef HAS_GETTIMEOFDAY
765 (void) hv_store(PL_modglobal, "Time::NVtime", 12,
766 newSViv(PTR2IV(myNVtime)), 0);
767 (void) hv_store(PL_modglobal, "Time::U2time", 12,
768 newSViv(PTR2IV(myU2time)), 0);
774 #if defined(USE_ITHREADS) && defined(MY_CXT_KEY)
783 INCLUDE: const-xs.inc
785 #if defined(HAS_USLEEP) && defined(HAS_GETTIMEOFDAY)
791 struct timeval Ta, Tb;
793 gettimeofday(&Ta, NULL);
795 if (useconds > 1E6) {
796 IV seconds = (IV) (useconds / 1E6);
797 /* If usleep() has been implemented using setitimer()
798 * then this contortion is unnecessary-- but usleep()
799 * may be implemented in some other way, so let's contort. */
802 useconds -= 1E6 * seconds;
804 } else if (useconds < 0.0)
805 croak("Time::HiRes::usleep(%"NVgf"): negative time not invented yet", useconds);
806 usleep((U32)useconds);
809 gettimeofday(&Tb, NULL);
811 printf("[%ld %ld] [%ld %ld]\n", Tb.tv_sec, Tb.tv_usec, Ta.tv_sec, Ta.tv_usec);
813 RETVAL = 1E6*(Tb.tv_sec-Ta.tv_sec)+(NV)((IV)Tb.tv_usec-(IV)Ta.tv_usec);
818 #if defined(TIME_HIRES_NANOSLEEP)
824 struct timespec sleepfor, unslept;
827 croak("Time::HiRes::nanosleep(%"NVgf"): negative time not invented yet", nsec);
828 sleepfor.tv_sec = (Time_t)(nsec / 1e9);
829 sleepfor.tv_nsec = (long)(nsec - ((NV)sleepfor.tv_sec) * 1e9);
830 if (!nanosleep(&sleepfor, &unslept)) {
833 sleepfor.tv_sec -= unslept.tv_sec;
834 sleepfor.tv_nsec -= unslept.tv_nsec;
835 if (sleepfor.tv_nsec < 0) {
837 sleepfor.tv_nsec += 1000000000;
839 RETVAL = ((NV)sleepfor.tv_sec) * 1e9 + ((NV)sleepfor.tv_nsec);
844 #else /* #if defined(TIME_HIRES_NANOSLEEP) */
850 croak("Time::HiRes::nanosleep(): unimplemented in this platform");
853 #endif /* #if defined(TIME_HIRES_NANOSLEEP) */
858 struct timeval Ta, Tb;
860 gettimeofday(&Ta, NULL);
862 NV seconds = SvNV(ST(0));
863 if (seconds >= 0.0) {
864 UV useconds = (UV)(1E6 * (seconds - (UV)seconds));
867 if ((IV)useconds < 0) {
868 #if defined(__sparc64__) && defined(__GNUC__)
869 /* Sparc64 gcc 2.95.3 (e.g. on NetBSD) has a bug
870 * where (0.5 - (UV)(0.5)) will under certain
871 * circumstances (if the double is cast to UV more
872 * than once?) evaluate to -0.5, instead of 0.5. */
873 useconds = -(IV)useconds;
874 #endif /* #if defined(__sparc64__) && defined(__GNUC__) */
875 if ((IV)useconds < 0)
876 croak("Time::HiRes::sleep(%"NVgf"): internal error: useconds < 0 (unsigned %"UVuf" signed %"IVdf")", seconds, useconds, (IV)useconds);
880 croak("Time::HiRes::sleep(%"NVgf"): negative time not invented yet", seconds);
883 gettimeofday(&Tb, NULL);
885 printf("[%ld %ld] [%ld %ld]\n", Tb.tv_sec, Tb.tv_usec, Ta.tv_sec, Ta.tv_usec);
887 RETVAL = (NV)(Tb.tv_sec-Ta.tv_sec)+0.000001*(NV)(Tb.tv_usec-Ta.tv_usec);
892 #else /* #if defined(HAS_USLEEP) && defined(HAS_GETTIMEOFDAY) */
898 croak("Time::HiRes::usleep(): unimplemented in this platform");
901 #endif /* #if defined(HAS_USLEEP) && defined(HAS_GETTIMEOFDAY) */
906 ualarm(useconds,uinterval=0)
910 if (useconds < 0 || uinterval < 0)
911 croak("Time::HiRes::ualarm(%d, %d): negative time not invented yet", useconds, uinterval);
912 #if defined(HAS_SETITIMER) && defined(ITIMER_REAL)
914 struct itimerval itv;
915 if (hrt_ualarm_itimero(&itv, useconds, uinterval)) {
916 /* To conform to ualarm's interface, we're actually ignoring
920 RETVAL = itv.it_value.tv_sec * IV_1E6 + itv.it_value.tv_usec;
924 if (useconds >= IV_1E6 || uinterval >= IV_1E6)
925 croak("Time::HiRes::ualarm(%d, %d): useconds or uinterval equal to or more than %"IVdf, useconds, uinterval, IV_1E6);
926 RETVAL = ualarm(useconds, uinterval);
933 alarm(seconds,interval=0)
937 if (seconds < 0.0 || interval < 0.0)
938 croak("Time::HiRes::alarm(%"NVgf", %"NVgf"): negative time not invented yet", seconds, interval);
940 IV useconds = IV_1E6 * seconds;
941 IV uinterval = IV_1E6 * interval;
942 #if defined(HAS_SETITIMER) && defined(ITIMER_REAL)
944 struct itimerval itv;
945 if (hrt_ualarm_itimero(&itv, useconds, uinterval)) {
946 /* To conform to alarm's interface, we're actually ignoring
950 RETVAL = itv.it_value.tv_sec + ((NV)itv.it_value.tv_usec) / NV_1E6;
954 if (useconds >= IV_1E6 || uinterval >= IV_1E6)
955 croak("Time::HiRes::alarm(%d, %d): seconds or interval equal to or more than 1.0 ", useconds, uinterval, IV_1E6);
956 RETVAL = (NV)ualarm( useconds, uinterval ) / NV_1E6;
966 ualarm(useconds,interval=0)
970 croak("Time::HiRes::ualarm(): unimplemented in this platform");
974 alarm(seconds,interval=0)
978 croak("Time::HiRes::alarm(): unimplemented in this platform");
981 #endif /* #ifdef HAS_UALARM */
983 #ifdef HAS_GETTIMEOFDAY
984 # ifdef MACOS_TRADITIONAL /* fix epoch TZ and use unsigned time_t */
992 status = gettimeofday (&Tp, &Tz);
995 Tp.tv_sec += Tz.tz_minuteswest * 60; /* adjust for TZ */
996 if (GIMME == G_ARRAY) {
998 /* Mac OS (Classic) has unsigned time_t */
999 PUSHs(sv_2mortal(newSVuv(Tp.tv_sec)));
1000 PUSHs(sv_2mortal(newSViv(Tp.tv_usec)));
1003 PUSHs(sv_2mortal(newSVnv(Tp.tv_sec + (Tp.tv_usec / NV_1E6))));
1014 status = gettimeofday (&Tp, &Tz);
1016 Tp.tv_sec += Tz.tz_minuteswest * 60; /* adjust for TZ */
1017 RETVAL = Tp.tv_sec + (Tp.tv_usec / NV_1E6);
1024 # else /* MACOS_TRADITIONAL */
1031 status = gettimeofday (&Tp, NULL);
1033 if (GIMME == G_ARRAY) {
1035 PUSHs(sv_2mortal(newSViv(Tp.tv_sec)));
1036 PUSHs(sv_2mortal(newSViv(Tp.tv_usec)));
1039 PUSHs(sv_2mortal(newSVnv(Tp.tv_sec + (Tp.tv_usec / NV_1E6))));
1049 status = gettimeofday (&Tp, NULL);
1051 RETVAL = Tp.tv_sec + (Tp.tv_usec / NV_1E6);
1058 # endif /* MACOS_TRADITIONAL */
1059 #endif /* #ifdef HAS_GETTIMEOFDAY */
1061 #if defined(HAS_GETITIMER) && defined(HAS_SETITIMER)
1063 #define TV2NV(tv) ((NV)((tv).tv_sec) + 0.000001 * (NV)((tv).tv_usec))
1066 setitimer(which, seconds, interval = 0)
1071 struct itimerval newit;
1072 struct itimerval oldit;
1074 if (seconds < 0.0 || interval < 0.0)
1075 croak("Time::HiRes::setitimer(%"IVdf", %"NVgf", %"NVgf"): negative time not invented yet", (IV)which, seconds, interval);
1076 newit.it_value.tv_sec = (IV)seconds;
1077 newit.it_value.tv_usec =
1078 (IV)((seconds - (NV)newit.it_value.tv_sec) * NV_1E6);
1079 newit.it_interval.tv_sec = (IV)interval;
1080 newit.it_interval.tv_usec =
1081 (IV)((interval - (NV)newit.it_interval.tv_sec) * NV_1E6);
1082 if (setitimer(which, &newit, &oldit) == 0) {
1084 PUSHs(sv_2mortal(newSVnv(TV2NV(oldit.it_value))));
1085 if (GIMME == G_ARRAY) {
1087 PUSHs(sv_2mortal(newSVnv(TV2NV(oldit.it_interval))));
1095 struct itimerval nowit;
1097 if (getitimer(which, &nowit) == 0) {
1099 PUSHs(sv_2mortal(newSVnv(TV2NV(nowit.it_value))));
1100 if (GIMME == G_ARRAY) {
1102 PUSHs(sv_2mortal(newSVnv(TV2NV(nowit.it_interval))));
1106 #endif /* #if defined(HAS_GETITIMER) && defined(HAS_SETITIMER) */
1108 #if defined(TIME_HIRES_CLOCK_GETTIME)
1111 clock_gettime(clock_id = CLOCK_REALTIME)
1117 #ifdef TIME_HIRES_CLOCK_GETTIME_SYSCALL
1118 status = syscall(SYS_clock_gettime, clock_id, &ts);
1120 status = clock_gettime(clock_id, &ts);
1122 RETVAL = status == 0 ? ts.tv_sec + (NV) ts.tv_nsec / (NV) 1e9 : -1;
1127 #else /* if defined(TIME_HIRES_CLOCK_GETTIME) */
1130 clock_gettime(clock_id = 0)
1133 croak("Time::HiRes::clock_gettime(): unimplemented in this platform");
1136 #endif /* #if defined(TIME_HIRES_CLOCK_GETTIME) */
1138 #if defined(TIME_HIRES_CLOCK_GETRES)
1141 clock_getres(clock_id = CLOCK_REALTIME)
1147 #ifdef TIME_HIRES_CLOCK_GETRES_SYSCALL
1148 status = syscall(SYS_clock_getres, clock_id, &ts);
1150 status = clock_getres(clock_id, &ts);
1152 RETVAL = status == 0 ? ts.tv_sec + (NV) ts.tv_nsec / (NV) 1e9 : -1;
1157 #else /* if defined(TIME_HIRES_CLOCK_GETRES) */
1160 clock_getres(clock_id = 0)
1163 croak("Time::HiRes::clock_getres(): unimplemented in this platform");
1166 #endif /* #if defined(TIME_HIRES_CLOCK_GETRES) */
1168 #if defined(TIME_HIRES_CLOCK_NANOSLEEP) && defined(TIMER_ABSTIME)
1171 clock_nanosleep(clock_id, nsec, flags = 0)
1176 struct timespec sleepfor, unslept;
1179 croak("Time::HiRes::clock_nanosleep(..., %"NVgf"): negative time not invented yet", nsec);
1180 sleepfor.tv_sec = (Time_t)(nsec / 1e9);
1181 sleepfor.tv_nsec = (long)(nsec - ((NV)sleepfor.tv_sec) * 1e9);
1182 if (!clock_nanosleep(clock_id, flags, &sleepfor, &unslept)) {
1185 sleepfor.tv_sec -= unslept.tv_sec;
1186 sleepfor.tv_nsec -= unslept.tv_nsec;
1187 if (sleepfor.tv_nsec < 0) {
1189 sleepfor.tv_nsec += 1000000000;
1191 RETVAL = ((NV)sleepfor.tv_sec) * 1e9 + ((NV)sleepfor.tv_nsec);
1196 #else /* if defined(TIME_HIRES_CLOCK_NANOSLEEP) && defined(TIMER_ABSTIME) */
1201 croak("Time::HiRes::clock_nanosleep(): unimplemented in this platform");
1204 #endif /* #if defined(TIME_HIRES_CLOCK_NANOSLEEP) && defined(TIMER_ABSTIME) */
1206 #if defined(TIME_HIRES_CLOCK) && defined(CLOCKS_PER_SEC)
1214 RETVAL = clocks == -1 ? -1 : (NV)clocks / (NV)CLOCKS_PER_SEC;
1219 #else /* if defined(TIME_HIRES_CLOCK) && defined(CLOCKS_PER_SEC) */
1224 croak("Time::HiRes::clock(): unimplemented in this platform");
1227 #endif /* #if defined(TIME_HIRES_CLOCK) && defined(CLOCKS_PER_SEC) */
1234 XPUSHs(sv_2mortal(newSVsv(items == 1 ? ST(0) : DEFSV)));
1237 PL_laststatval = -1;
1238 (void)*(PL_ppaddr[OP_STAT])(aTHXR);
1241 if (PL_laststatval == 0) {
1242 /* We assume that pp_stat() left us with 13 valid stack items,
1243 * and that the timestamps are at offsets 8, 9, and 10. */
1244 UV atime = SvUV(ST( 8));
1245 UV mtime = SvUV(ST( 9));
1246 UV ctime = SvUV(ST(10));
1250 hrstatns(&atime_nsec, &mtime_nsec, &ctime_nsec);
1252 ST( 8) = sv_2mortal(newSVnv(atime + 1e-9 * (NV) atime_nsec));
1254 ST( 9) = sv_2mortal(newSVnv(mtime + 1e-9 * (NV) mtime_nsec));
1256 ST(10) = sv_2mortal(newSVnv(ctime + 1e-9 * (NV) ctime_nsec));