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_nanosleep /* could conflict with ncurses for static build */
408 hrt_nanosleep(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(TIME_HIRES_NANOSLEEP)
450 #define usleep hrt_usleep /* could conflict with ncurses for static build */
453 hrt_usleep(unsigned long usec)
456 ts1.tv_sec = usec * 1000; /* Ignoring wraparound. */
458 nanosleep(&ts1, NULL);
461 #endif /* #if !defined(HAS_USLEEP) && defined(TIME_HIRES_NANOSLEEP) */
463 #if !defined(HAS_USLEEP) && defined(HAS_POLL)
465 #define usleep hrt_usleep /* could conflict with ncurses for static build */
468 hrt_usleep(unsigned long usec)
470 int msec = usec / 1000;
474 #endif /* #if !defined(HAS_USLEEP) && defined(HAS_POLL) */
476 #if defined(HAS_SETITIMER) && defined(ITIMER_REAL)
479 hrt_ualarm_itimero(struct itimerval* itv, int usec, int uinterval)
481 itv->it_value.tv_sec = usec / IV_1E6;
482 itv->it_value.tv_usec = usec % IV_1E6;
483 itv->it_interval.tv_sec = uinterval / IV_1E6;
484 itv->it_interval.tv_usec = uinterval % IV_1E6;
485 return setitimer(ITIMER_REAL, itv, 0);
489 hrt_ualarm_itimer(int usec, int uinterval)
491 struct itimerval itv;
492 return hrt_ualarm_itimero(&itv, usec, uinterval);
497 hrt_ualarm(int usec, int interval) /* for binary compat before 1.91 */
499 return hrt_ualarm_itimer(usec, interval);
501 #endif /* #ifdef HAS_UALARM */
502 #endif /* #if !defined(HAS_UALARM) && defined(HAS_SETITIMER) */
504 #if !defined(HAS_UALARM) && defined(HAS_SETITIMER)
506 #define ualarm hrt_ualarm_itimer /* could conflict with ncurses for static build */
509 #if !defined(HAS_UALARM) && defined(VMS)
511 #define ualarm vms_ualarm
513 #include <lib$routines.h>
521 #define VMSERR(s) (!((s)&1))
524 us_to_VMS(useconds_t mseconds, unsigned long v[])
533 iss = lib$addx(qq,qq,qq);
534 if (VMSERR(iss)) lib$signal(iss);
535 iss = lib$subx(v,qq,v);
536 if (VMSERR(iss)) lib$signal(iss);
537 iss = lib$addx(qq,qq,qq);
538 if (VMSERR(iss)) lib$signal(iss);
539 iss = lib$subx(v,qq,v);
540 if (VMSERR(iss)) lib$signal(iss);
541 iss = lib$subx(v,qq,v);
542 if (VMSERR(iss)) lib$signal(iss);
546 VMS_to_us(unsigned long v[])
549 unsigned long div=10,quot, rem;
551 iss = lib$ediv(&div,v,",&rem);
552 if (VMSERR(iss)) lib$signal(iss);
557 typedef unsigned short word;
558 typedef struct _ualarm {
561 unsigned long delay[2];
562 unsigned long interval[2];
563 unsigned long remain[2];
568 static Alarm *a0, alarm_base;
573 static void ualarm_AST(Alarm *a);
576 vms_ualarm(int mseconds, int interval)
585 static struct item_list3 itmlst[2];
586 static int first = 1;
592 itmlst[0].code = JPI$_ASTEN;
593 itmlst[0].length = sizeof(asten);
594 itmlst[0].retlenaddr = NULL;
596 itmlst[1].length = 0;
597 itmlst[1].bufaddr = NULL;
598 itmlst[1].retlenaddr = NULL;
600 iss = lib$get_ef(&alarm_ef);
601 if (VMSERR(iss)) lib$signal(iss);
604 a0->function = UAL_NULL;
606 itmlst[0].bufaddr = &asten;
608 iss = sys$getjpiw(0,0,0,itmlst,0,0,0);
609 if (VMSERR(iss)) lib$signal(iss);
610 if (!(asten&0x08)) return -1;
614 a->function = UAL_SET;
616 a->function = UAL_CLEAR;
619 us_to_VMS(mseconds, a->delay);
621 us_to_VMS(interval, a->interval);
626 iss = sys$clref(alarm_ef);
627 if (VMSERR(iss)) lib$signal(iss);
629 iss = sys$dclast(ualarm_AST,a,0);
630 if (VMSERR(iss)) lib$signal(iss);
632 iss = sys$waitfr(alarm_ef);
633 if (VMSERR(iss)) lib$signal(iss);
635 if (a->function == UAL_ACTIVE)
636 return VMS_to_us(a->remain);
647 unsigned long now[2];
649 iss = sys$gettim(now);
650 if (VMSERR(iss)) lib$signal(iss);
652 if (a->function == UAL_SET || a->function == UAL_CLEAR) {
653 if (a0->function == UAL_ACTIVE) {
654 iss = sys$cantim(a0,PSL$C_USER);
655 if (VMSERR(iss)) lib$signal(iss);
657 iss = lib$subx(a0->remain, now, a->remain);
658 if (VMSERR(iss)) lib$signal(iss);
660 if (a->remain[1] & 0x80000000)
661 a->remain[0] = a->remain[1] = 0;
664 if (a->function == UAL_SET) {
665 a->function = a0->function;
666 a0->function = UAL_ACTIVE;
667 a0->repeat = a->repeat;
669 a0->interval[0] = a->interval[0];
670 a0->interval[1] = a->interval[1];
672 a0->delay[0] = a->delay[0];
673 a0->delay[1] = a->delay[1];
675 iss = lib$subx(now, a0->delay, a0->remain);
676 if (VMSERR(iss)) lib$signal(iss);
678 iss = sys$setimr(0,a0->delay,ualarm_AST,a0);
679 if (VMSERR(iss)) lib$signal(iss);
681 a->function = a0->function;
682 a0->function = UAL_NULL;
684 iss = sys$setef(alarm_ef);
685 if (VMSERR(iss)) lib$signal(iss);
686 } else if (a->function == UAL_ACTIVE) {
688 iss = lib$subx(now, a->interval, a->remain);
689 if (VMSERR(iss)) lib$signal(iss);
691 iss = sys$setimr(0,a->interval,ualarm_AST,a);
692 if (VMSERR(iss)) lib$signal(iss);
694 a->function = UAL_NULL;
697 if (VMSERR(iss)) lib$signal(iss);
698 lib$signal(SS$_ASTFLT);
700 lib$signal(SS$_BADPARAM);
704 #endif /* #if !defined(HAS_UALARM) && defined(VMS) */
706 #ifdef HAS_GETTIMEOFDAY
709 myU2time(pTHX_ UV *ret)
713 status = gettimeofday (&Tp, NULL);
727 status = gettimeofday (&Tp, NULL);
728 return status == 0 ? Tp.tv_sec + (Tp.tv_usec / NV_1E6) : -1.0;
731 #endif /* #ifdef HAS_GETTIMEOFDAY */
734 hrstatns(UV atime, UV mtime, UV ctime, UV *atime_nsec, UV *mtime_nsec, UV *ctime_nsec)
740 #ifdef TIME_HIRES_STAT
741 #if TIME_HIRES_STAT == 1
742 *atime_nsec = PL_statcache.st_atimespec.tv_nsec;
743 *mtime_nsec = PL_statcache.st_mtimespec.tv_nsec;
744 *ctime_nsec = PL_statcache.st_ctimespec.tv_nsec;
746 #if TIME_HIRES_STAT == 2
747 *atime_nsec = PL_statcache.st_atimensec;
748 *mtime_nsec = PL_statcache.st_mtimensec;
749 *ctime_nsec = PL_statcache.st_ctimensec;
751 #if TIME_HIRES_STAT == 3
752 *atime_nsec = PL_statcache.st_atime_n;
753 *mtime_nsec = PL_statcache.st_mtime_n;
754 *ctime_nsec = PL_statcache.st_ctime_n;
756 #if TIME_HIRES_STAT == 4
757 *atime_nsec = PL_statcache.st_atim.tv_nsec;
758 *mtime_nsec = PL_statcache.st_mtim.tv_nsec;
759 *ctime_nsec = PL_statcache.st_ctim.tv_nsec;
761 #if TIME_HIRES_STAT == 5
762 *atime_nsec = PL_statcache.st_uatime * 1000;
763 *mtime_nsec = PL_statcache.st_umtime * 1000;
764 *ctime_nsec = PL_statcache.st_uctime * 1000;
769 #include "const-c.inc"
771 MODULE = Time::HiRes PACKAGE = Time::HiRes
780 #ifdef ATLEASTFIVEOHOHFIVE
781 # ifdef HAS_GETTIMEOFDAY
783 hv_store(PL_modglobal, "Time::NVtime", 12, newSViv(PTR2IV(myNVtime)), 0);
784 hv_store(PL_modglobal, "Time::U2time", 12, newSViv(PTR2IV(myU2time)), 0);
790 #if defined(USE_ITHREADS) && defined(MY_CXT_KEY)
799 INCLUDE: const-xs.inc
801 #if defined(HAS_USLEEP) && defined(HAS_GETTIMEOFDAY)
807 struct timeval Ta, Tb;
809 gettimeofday(&Ta, NULL);
811 if (useconds > 1E6) {
812 IV seconds = (IV) (useconds / 1E6);
813 /* If usleep() has been implemented using setitimer()
814 * then this contortion is unnecessary-- but usleep()
815 * may be implemented in some other way, so let's contort. */
818 useconds -= 1E6 * seconds;
820 } else if (useconds < 0.0)
821 croak("Time::HiRes::usleep(%"NVgf"): negative time not invented yet", useconds);
822 usleep((U32)useconds);
825 gettimeofday(&Tb, NULL);
827 printf("[%ld %ld] [%ld %ld]\n", Tb.tv_sec, Tb.tv_usec, Ta.tv_sec, Ta.tv_usec);
829 RETVAL = 1E6*(Tb.tv_sec-Ta.tv_sec)+(NV)((IV)Tb.tv_usec-(IV)Ta.tv_usec);
834 #if defined(TIME_HIRES_NANOSLEEP)
840 struct timespec sleepfor, unslept;
843 croak("Time::HiRes::nanosleep(%"NVgf"): negative time not invented yet", nsec);
844 sleepfor.tv_sec = (Time_t)(nsec / 1e9);
845 sleepfor.tv_nsec = (long)(nsec - ((NV)sleepfor.tv_sec) * 1e9);
846 if (!nanosleep(&sleepfor, &unslept)) {
849 sleepfor.tv_sec -= unslept.tv_sec;
850 sleepfor.tv_nsec -= unslept.tv_nsec;
851 if (sleepfor.tv_nsec < 0) {
853 sleepfor.tv_nsec += 1000000000;
855 RETVAL = ((NV)sleepfor.tv_sec) * 1e9 + ((NV)sleepfor.tv_nsec);
860 #else /* #if defined(TIME_HIRES_NANOSLEEP) */
866 croak("Time::HiRes::nanosleep(): unimplemented in this platform");
869 #endif /* #if defined(TIME_HIRES_NANOSLEEP) */
874 struct timeval Ta, Tb;
876 gettimeofday(&Ta, NULL);
878 NV seconds = SvNV(ST(0));
879 if (seconds >= 0.0) {
880 UV useconds = (UV)(1E6 * (seconds - (UV)seconds));
883 if ((IV)useconds < 0) {
884 #if defined(__sparc64__) && defined(__GNUC__)
885 /* Sparc64 gcc 2.95.3 (e.g. on NetBSD) has a bug
886 * where (0.5 - (UV)(0.5)) will under certain
887 * circumstances (if the double is cast to UV more
888 * than once?) evaluate to -0.5, instead of 0.5. */
889 useconds = -(IV)useconds;
890 #endif /* #if defined(__sparc64__) && defined(__GNUC__) */
891 if ((IV)useconds < 0)
892 croak("Time::HiRes::sleep(%"NVgf"): internal error: useconds < 0 (unsigned %"UVuf" signed %"IVdf")", seconds, useconds, (IV)useconds);
896 croak("Time::HiRes::sleep(%"NVgf"): negative time not invented yet", seconds);
899 gettimeofday(&Tb, NULL);
901 printf("[%ld %ld] [%ld %ld]\n", Tb.tv_sec, Tb.tv_usec, Ta.tv_sec, Ta.tv_usec);
903 RETVAL = (NV)(Tb.tv_sec-Ta.tv_sec)+0.000001*(NV)(Tb.tv_usec-Ta.tv_usec);
908 #else /* #if defined(HAS_USLEEP) && defined(HAS_GETTIMEOFDAY) */
914 croak("Time::HiRes::usleep(): unimplemented in this platform");
917 #endif /* #if defined(HAS_USLEEP) && defined(HAS_GETTIMEOFDAY) */
922 ualarm(useconds,uinterval=0)
926 if (useconds < 0 || uinterval < 0)
927 croak("Time::HiRes::ualarm(%d, %d): negative time not invented yet", useconds, uinterval);
928 if (useconds >= IV_1E6 || uinterval >= IV_1E6)
929 #if defined(HAS_SETITIMER) && defined(ITIMER_REAL)
931 struct itimerval itv;
932 if (hrt_ualarm_itimero(&itv, useconds, uinterval)) {
933 RETVAL = itv.it_value.tv_sec + IV_1E6 * itv.it_value.tv_usec;
939 croak("Time::HiRes::ualarm(%d, %d): useconds or uinterval equal to or more than %"IVdf, useconds, uinterval, IV_1E6);
942 RETVAL = ualarm(useconds, uinterval);
948 alarm(seconds,interval=0)
952 if (seconds < 0.0 || interval < 0.0)
953 croak("Time::HiRes::alarm(%"NVgf", %"NVgf"): negative time not invented yet", seconds, interval);
955 IV useconds = IV_1E6 * seconds;
956 IV uinterval = IV_1E6 * interval;
957 if (seconds >= IV_1E6 || interval >= IV_1E6)
958 #if defined(HAS_SETITIMER) && defined(ITIMER_REAL)
960 struct itimerval itv;
961 if (hrt_ualarm_itimero(&itv, useconds, uinterval)) {
962 RETVAL = (NV)itv.it_value.tv_sec + (NV)itv.it_value.tv_usec / NV_1E6;
968 RETVAL = (NV)ualarm((IV)(seconds * IV_1E6),
969 (IV)(interval * IV_1E6)) / NV_1E6;
979 ualarm(useconds,interval=0)
983 croak("Time::HiRes::ualarm(): unimplemented in this platform");
987 alarm(seconds,interval=0)
991 croak("Time::HiRes::alarm(): unimplemented in this platform");
994 #endif /* #ifdef HAS_UALARM */
996 #ifdef HAS_GETTIMEOFDAY
997 # ifdef MACOS_TRADITIONAL /* fix epoch TZ and use unsigned time_t */
1005 status = gettimeofday (&Tp, &Tz);
1008 Tp.tv_sec += Tz.tz_minuteswest * 60; /* adjust for TZ */
1009 if (GIMME == G_ARRAY) {
1011 /* Mac OS (Classic) has unsigned time_t */
1012 PUSHs(sv_2mortal(newSVuv(Tp.tv_sec)));
1013 PUSHs(sv_2mortal(newSViv(Tp.tv_usec)));
1016 PUSHs(sv_2mortal(newSVnv(Tp.tv_sec + (Tp.tv_usec / NV_1E6))));
1027 status = gettimeofday (&Tp, &Tz);
1029 Tp.tv_sec += Tz.tz_minuteswest * 60; /* adjust for TZ */
1030 RETVAL = Tp.tv_sec + (Tp.tv_usec / NV_1E6);
1037 # else /* MACOS_TRADITIONAL */
1044 status = gettimeofday (&Tp, NULL);
1046 if (GIMME == G_ARRAY) {
1048 PUSHs(sv_2mortal(newSViv(Tp.tv_sec)));
1049 PUSHs(sv_2mortal(newSViv(Tp.tv_usec)));
1052 PUSHs(sv_2mortal(newSVnv(Tp.tv_sec + (Tp.tv_usec / NV_1E6))));
1062 status = gettimeofday (&Tp, NULL);
1064 RETVAL = Tp.tv_sec + (Tp.tv_usec / NV_1E6);
1071 # endif /* MACOS_TRADITIONAL */
1072 #endif /* #ifdef HAS_GETTIMEOFDAY */
1074 #if defined(HAS_GETITIMER) && defined(HAS_SETITIMER)
1076 #define TV2NV(tv) ((NV)((tv).tv_sec) + 0.000001 * (NV)((tv).tv_usec))
1079 setitimer(which, seconds, interval = 0)
1084 struct itimerval newit;
1085 struct itimerval oldit;
1087 if (seconds < 0.0 || interval < 0.0)
1088 croak("Time::HiRes::setitimer(%"IVdf", %"NVgf", %"NVgf"): negative time not invented yet", (IV)which, seconds, interval);
1089 newit.it_value.tv_sec = (IV)seconds;
1090 newit.it_value.tv_usec =
1091 (IV)((seconds - (NV)newit.it_value.tv_sec) * NV_1E6);
1092 newit.it_interval.tv_sec = (IV)interval;
1093 newit.it_interval.tv_usec =
1094 (IV)((interval - (NV)newit.it_interval.tv_sec) * NV_1E6);
1095 if (setitimer(which, &newit, &oldit) == 0) {
1097 PUSHs(sv_2mortal(newSVnv(TV2NV(oldit.it_value))));
1098 if (GIMME == G_ARRAY) {
1100 PUSHs(sv_2mortal(newSVnv(TV2NV(oldit.it_interval))));
1108 struct itimerval nowit;
1110 if (getitimer(which, &nowit) == 0) {
1112 PUSHs(sv_2mortal(newSVnv(TV2NV(nowit.it_value))));
1113 if (GIMME == G_ARRAY) {
1115 PUSHs(sv_2mortal(newSVnv(TV2NV(nowit.it_interval))));
1119 #endif /* #if defined(HAS_GETITIMER) && defined(HAS_SETITIMER) */
1121 #if defined(TIME_HIRES_CLOCK_GETTIME)
1124 clock_gettime(clock_id = CLOCK_REALTIME)
1130 #ifdef TIME_HIRES_CLOCK_GETTIME_SYSCALL
1131 status = syscall(SYS_clock_gettime, clock_id, &ts);
1133 status = clock_gettime(clock_id, &ts);
1135 RETVAL = status == 0 ? ts.tv_sec + (NV) ts.tv_nsec / (NV) 1e9 : -1;
1140 #else /* if defined(TIME_HIRES_CLOCK_GETTIME) */
1143 clock_gettime(clock_id = 0)
1146 croak("Time::HiRes::clock_gettime(): unimplemented in this platform");
1149 #endif /* #if defined(TIME_HIRES_CLOCK_GETTIME) */
1151 #if defined(TIME_HIRES_CLOCK_GETRES)
1154 clock_getres(clock_id = CLOCK_REALTIME)
1160 #ifdef TIME_HIRES_CLOCK_GETRES_SYSCALL
1161 status = syscall(SYS_clock_getres, clock_id, &ts);
1163 status = clock_getres(clock_id, &ts);
1165 RETVAL = status == 0 ? ts.tv_sec + (NV) ts.tv_nsec / (NV) 1e9 : -1;
1170 #else /* if defined(TIME_HIRES_CLOCK_GETRES) */
1173 clock_getres(clock_id = 0)
1176 croak("Time::HiRes::clock_getres(): unimplemented in this platform");
1179 #endif /* #if defined(TIME_HIRES_CLOCK_GETRES) */
1181 #if defined(TIME_HIRES_CLOCK_NANOSLEEP) && defined(TIMER_ABSTIME)
1184 clock_nanosleep(clock_id, nsec, flags = 0)
1189 struct timespec sleepfor, unslept;
1192 croak("Time::HiRes::clock_nanosleep(..., %"NVgf"): negative time not invented yet", nsec);
1193 sleepfor.tv_sec = (Time_t)(nsec / 1e9);
1194 sleepfor.tv_nsec = (long)(nsec - ((NV)sleepfor.tv_sec) * 1e9);
1195 if (!clock_nanosleep(clock_id, flags, &sleepfor, &unslept)) {
1198 sleepfor.tv_sec -= unslept.tv_sec;
1199 sleepfor.tv_nsec -= unslept.tv_nsec;
1200 if (sleepfor.tv_nsec < 0) {
1202 sleepfor.tv_nsec += 1000000000;
1204 RETVAL = ((NV)sleepfor.tv_sec) * 1e9 + ((NV)sleepfor.tv_nsec);
1209 #else /* if defined(TIME_HIRES_CLOCK_NANOSLEEP) && defined(TIMER_ABSTIME) */
1214 croak("Time::HiRes::clock_nanosleep(): unimplemented in this platform");
1217 #endif /* #if defined(TIME_HIRES_CLOCK_NANOSLEEP) && defined(TIMER_ABSTIME) */
1219 #if defined(TIME_HIRES_CLOCK) && defined(CLOCKS_PER_SEC)
1227 RETVAL = clocks == -1 ? -1 : (NV)clocks / (NV)CLOCKS_PER_SEC;
1232 #else /* if defined(TIME_HIRES_CLOCK) && defined(CLOCKS_PER_SEC) */
1237 croak("Time::HiRes::clock(): unimplemented in this platform");
1240 #endif /* #if defined(TIME_HIRES_CLOCK) && defined(CLOCKS_PER_SEC) */
1247 XPUSHs(sv_2mortal(newSVsv(items == 1 ? ST(0) : DEFSV)));
1250 PL_laststatval = -1;
1251 (void)*(PL_ppaddr[OP_STAT])(aTHXR);
1254 if (PL_laststatval == 0) {
1255 /* We assume that pp_stat() left us with 13 valid stack items,
1256 * and that the timestamps are at offsets 8, 9, and 10. */
1257 UV atime = SvUV(ST( 8));
1258 UV mtime = SvUV(ST( 9));
1259 UV ctime = SvUV(ST(10));
1263 hrstatns(atime, mtime, ctime,
1264 &atime_nsec, &mtime_nsec, &ctime_nsec);
1266 ST( 8) = sv_2mortal(newSVnv(atime + 1e-9 * (NV) atime_nsec));
1268 ST( 9) = sv_2mortal(newSVnv(mtime + 1e-9 * (NV) mtime_nsec));
1270 ST(10) = sv_2mortal(newSVnv(ctime + 1e-9 * (NV) ctime_nsec));