3 * Copyright (c) 1996-2002 Douglas E. Wegscheid. All rights reserved.
5 * Copyright (c) 2002,2003,2004,2005,2006 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 /* 5.004 doesn't define PL_sv_undef */
70 #ifndef ATLEASTFIVEOHOHFIVE
72 # define PL_sv_undef sv_undef
76 #if defined(TIME_HIRES_CLOCK_GETTIME) && defined(_STRUCT_ITIMERSPEC)
78 /* HP-UX has CLOCK_XXX values but as enums, not as defines.
79 * The only way to detect these would be to test compile for each. */
81 # define CLOCK_REALTIME CLOCK_REALTIME
82 # define CLOCK_VIRTUAL CLOCK_VIRTUAL
83 # define CLOCK_PROFILE CLOCK_PROFILE
84 # endif /* # ifdef __hpux */
86 #endif /* #if defined(TIME_HIRES_CLOCK_GETTIME) && defined(_STRUCT_ITIMERSPEC) */
88 #if defined(WIN32) || defined(CYGWIN_WITH_W32API)
90 #ifndef HAS_GETTIMEOFDAY
91 # define HAS_GETTIMEOFDAY
94 /* shows up in winsock.h?
102 unsigned __int64 ft_i64;
106 #define MY_CXT_KEY "Time::HiRes_" XS_VERSION
109 unsigned long run_count;
110 unsigned __int64 base_ticks;
111 unsigned __int64 tick_frequency;
112 FT_t base_systime_as_filetime;
113 unsigned __int64 reset_time;
118 /* Number of 100 nanosecond units from 1/1/1601 to 1/1/1970 */
120 # define Const64(x) x##LL
122 # define Const64(x) x##i64
124 #define EPOCH_BIAS Const64(116444736000000000)
128 # define IV_1E6LL 1000000LL /* Needed because of Const64() ##-appends LL (or i64). */
129 # define IV_1E7LL 10000000LL
130 # define IV_1E9LL 1000000000LL
132 # define IV_1E6i64 1000000i64
133 # define IV_1E7i64 10000000i64
134 # define IV_1E9i64 1000000000i64
138 /* NOTE: This does not compute the timezone info (doing so can be expensive,
139 * and appears to be unsupported even by glibc) */
141 /* dMY_CXT needs a Perl context and we don't want to call PERL_GET_CONTEXT
142 for performance reasons */
145 #define gettimeofday(tp, not_used) _gettimeofday(aTHX_ tp, not_used)
147 /* If the performance counter delta drifts more than 0.5 seconds from the
148 * system time then we recalibrate to the system time. This means we may
149 * move *backwards* in time! */
150 #define MAX_PERF_COUNTER_SKEW Const64(5000000) /* 0.5 seconds */
152 /* Reset reading from the performance counter every five minutes.
153 * Many PC clocks just seem to be so bad. */
154 #define MAX_PERF_COUNTER_TICKS Const64(300000000) /* 300 seconds */
157 _gettimeofday(pTHX_ struct timeval *tp, void *not_used)
161 unsigned __int64 ticks;
164 if (MY_CXT.run_count++ == 0 ||
165 MY_CXT.base_systime_as_filetime.ft_i64 > MY_CXT.reset_time) {
166 QueryPerformanceFrequency((LARGE_INTEGER*)&MY_CXT.tick_frequency);
167 QueryPerformanceCounter((LARGE_INTEGER*)&MY_CXT.base_ticks);
168 GetSystemTimeAsFileTime(&MY_CXT.base_systime_as_filetime.ft_val);
169 ft.ft_i64 = MY_CXT.base_systime_as_filetime.ft_i64;
170 MY_CXT.reset_time = ft.ft_i64 + MAX_PERF_COUNTER_TICKS;
174 QueryPerformanceCounter((LARGE_INTEGER*)&ticks);
175 ticks -= MY_CXT.base_ticks;
176 ft.ft_i64 = MY_CXT.base_systime_as_filetime.ft_i64
177 + Const64(IV_1E7) * (ticks / MY_CXT.tick_frequency)
178 +(Const64(IV_1E7) * (ticks % MY_CXT.tick_frequency)) / MY_CXT.tick_frequency;
179 diff = ft.ft_i64 - MY_CXT.base_systime_as_filetime.ft_i64;
180 if (diff < -MAX_PERF_COUNTER_SKEW || diff > MAX_PERF_COUNTER_SKEW) {
181 MY_CXT.base_ticks += ticks;
182 GetSystemTimeAsFileTime(&MY_CXT.base_systime_as_filetime.ft_val);
183 ft.ft_i64 = MY_CXT.base_systime_as_filetime.ft_i64;
187 /* seconds since epoch */
188 tp->tv_sec = (long)((ft.ft_i64 - EPOCH_BIAS) / Const64(IV_1E7));
190 /* microseconds remaining */
191 tp->tv_usec = (long)((ft.ft_i64 / Const64(10)) % Const64(IV_1E6));
197 #if defined(WIN32) && !defined(ATLEASTFIVEOHOHFIVE)
199 sleep(unsigned int t)
206 #if !defined(HAS_GETTIMEOFDAY) && defined(VMS)
207 #define HAS_GETTIMEOFDAY
210 #include <time.h> /* gettimeofday */
211 #include <stdlib.h> /* qdiv */
212 #include <starlet.h> /* sys$gettim */
215 #include <lib$routines.h> /* lib$ediv() */
219 VMS binary time is expressed in 100 nano-seconds since
220 system base time which is 17-NOV-1858 00:00:00.00
223 #define DIV_100NS_TO_SECS 10000000L
224 #define DIV_100NS_TO_USECS 10L
227 gettimeofday is supposed to return times since the epoch
228 so need to determine this in terms of VMS base time
230 static $DESCRIPTOR(dscepoch,"01-JAN-1970 00:00:00.00");
233 static long base_adjust[2]={0L,0L};
235 static __int64 base_adjust=0;
240 If we don't have gettimeofday, then likely we are on a VMS machine that
241 operates on local time rather than UTC...so we have to zone-adjust.
242 This code gleefully swiped from VMS.C
245 /* method used to handle UTC conversions:
246 * 1 == CRTL gmtime(); 2 == SYS$TIMEZONE_DIFFERENTIAL; 3 == no correction
248 static int gmtime_emulation_type;
249 /* number of secs to add to UTC POSIX-style time to get local time */
250 static long int utc_offset_secs;
251 static struct dsc$descriptor_s fildevdsc =
252 { 12, DSC$K_DTYPE_T, DSC$K_CLASS_S, "LNM$FILE_DEV" };
253 static struct dsc$descriptor_s *fildev[] = { &fildevdsc, NULL };
255 static time_t toutc_dst(time_t loc) {
258 if ((rsltmp = localtime(&loc)) == NULL) return -1;
259 loc -= utc_offset_secs;
260 if (rsltmp->tm_isdst) loc -= 3600;
264 static time_t toloc_dst(time_t utc) {
267 utc += utc_offset_secs;
268 if ((rsltmp = localtime(&utc)) == NULL) return -1;
269 if (rsltmp->tm_isdst) utc += 3600;
273 #define _toutc(secs) ((secs) == (time_t) -1 ? (time_t) -1 : \
274 ((gmtime_emulation_type || timezone_setup()), \
275 (gmtime_emulation_type == 1 ? toutc_dst(secs) : \
276 ((secs) - utc_offset_secs))))
278 #define _toloc(secs) ((secs) == (time_t) -1 ? (time_t) -1 : \
279 ((gmtime_emulation_type || timezone_setup()), \
280 (gmtime_emulation_type == 1 ? toloc_dst(secs) : \
281 ((secs) + utc_offset_secs))))
288 if (gmtime_emulation_type == 0) {
290 time_t base = 15 * 86400; /* 15jan71; to avoid month/year ends between */
291 /* results of calls to gmtime() and localtime() */
294 gmtime_emulation_type++;
295 if ((tm_p = gmtime(&base)) == NULL) { /* CRTL gmtime() is a fake */
296 char off[LNM$C_NAMLENGTH+1];;
298 gmtime_emulation_type++;
299 if (!Perl_vmstrnenv("SYS$TIMEZONE_DIFFERENTIAL",off,0,fildev,0)) {
300 gmtime_emulation_type++;
302 Perl_warn(aTHX_ "no UTC offset information; assuming local time is UTC");
304 else { utc_offset_secs = atol(off); }
306 else { /* We've got a working gmtime() */
307 struct tm gmt, local;
310 tm_p = localtime(&base);
312 utc_offset_secs = (local.tm_mday - gmt.tm_mday) * 86400;
313 utc_offset_secs += (local.tm_hour - gmt.tm_hour) * 3600;
314 utc_offset_secs += (local.tm_min - gmt.tm_min) * 60;
315 utc_offset_secs += (local.tm_sec - gmt.tm_sec);
323 gettimeofday (struct timeval *tp, void *tpz)
329 long div_100ns_to_secs;
330 long div_100ns_to_usecs;
338 In case of error, tv_usec = 0 and tv_sec = VMS condition code.
339 The return from function is also set to -1.
340 This is not exactly as per the manual page.
346 if (base_adjust[0]==0 && base_adjust[1]==0) {
348 if (base_adjust==0) { /* Need to determine epoch adjustment */
350 ret=sys$bintim(&dscepoch,&base_adjust);
351 if (1 != (ret &&1)) {
357 ret=sys$gettim(&quad); /* Get VMS system time */
358 if ((1 && ret) == 1) {
360 quad[0] -= base_adjust[0]; /* convert to epoch offset */
361 quad[1] -= base_adjust[1]; /* convert 2nd half of quadword */
362 div_100ns_to_secs = DIV_100NS_TO_SECS;
363 div_100ns_to_usecs = DIV_100NS_TO_USECS;
364 lib$ediv(&div_100ns_to_secs,&quad,&quo,&rem);
367 lib$ediv(&div_100ns_to_usecs,&quad1,&quo1,&rem1);
368 tp->tv_sec = quo; /* Whole seconds */
369 tp->tv_usec = quo1; /* Micro-seconds */
371 quad -= base_adjust; /* convert to epoch offset */
372 ans1=qdiv(quad,DIV_100NS_TO_SECS);
373 ans2=qdiv(ans1.rem,DIV_100NS_TO_USECS);
374 tp->tv_sec = ans1.quot; /* Whole seconds */
375 tp->tv_usec = ans2.quot; /* Micro-seconds */
383 if (VMSISH_TIME) tp->tv_sec = _toloc(tp->tv_sec);
385 if (!VMSISH_TIME) tp->tv_sec = _toutc(tp->tv_sec);
393 /* Do not use H A S _ N A N O S L E E P
394 * so that Perl Configure doesn't scan for it (and pull in -lrt and
395 * the like which are not usually good ideas for the default Perl).
396 * (We are part of the core perl now.)
397 * The TIME_HIRES_NANOSLEEP is set by Makefile.PL. */
398 #if !defined(HAS_USLEEP) && defined(TIME_HIRES_NANOSLEEP)
400 #define usleep hrt_nanosleep /* could conflict with ncurses for static build */
403 hrt_nanosleep(unsigned long usec) /* This is used to emulate usleep. */
406 res.tv_sec = usec / IV_1E6;
407 res.tv_nsec = ( usec - res.tv_sec * IV_1E6 ) * 1000;
408 nanosleep(&res, NULL);
411 #endif /* #if !defined(HAS_USLEEP) && defined(TIME_HIRES_NANOSLEEP) */
413 #if !defined(HAS_USLEEP) && defined(HAS_SELECT)
414 #ifndef SELECT_IS_BROKEN
416 #define usleep hrt_usleep /* could conflict with ncurses for static build */
419 hrt_usleep(unsigned long usec)
424 select(0, (Select_fd_set_t)NULL, (Select_fd_set_t)NULL,
425 (Select_fd_set_t)NULL, &tv);
428 #endif /* #if !defined(HAS_USLEEP) && defined(HAS_SELECT) */
430 #if !defined(HAS_USLEEP) && defined(WIN32)
432 #define usleep hrt_usleep /* could conflict with ncurses for static build */
435 hrt_usleep(unsigned long usec)
441 #endif /* #if !defined(HAS_USLEEP) && defined(WIN32) */
443 #if !defined(HAS_USLEEP) && defined(TIME_HIRES_NANOSLEEP)
445 #define usleep hrt_usleep /* could conflict with ncurses for static build */
448 hrt_usleep(unsigned long usec)
451 ts1.tv_sec = usec * 1000; /* Ignoring wraparound. */
453 nanosleep(&ts1, NULL);
456 #endif /* #if !defined(HAS_USLEEP) && defined(TIME_HIRES_NANOSLEEP) */
458 #if !defined(HAS_USLEEP) && defined(HAS_POLL)
460 #define usleep hrt_usleep /* could conflict with ncurses for static build */
463 hrt_usleep(unsigned long usec)
465 int msec = usec / 1000;
469 #endif /* #if !defined(HAS_USLEEP) && defined(HAS_POLL) */
471 #if defined(HAS_SETITIMER) && defined(ITIMER_REAL)
473 hrt_ualarm_itimer(int usec, int interval)
475 struct itimerval itv;
476 itv.it_value.tv_sec = usec / IV_1E6;
477 itv.it_value.tv_usec = usec % IV_1E6;
478 itv.it_interval.tv_sec = interval / IV_1E6;
479 itv.it_interval.tv_usec = interval % IV_1E6;
480 return setitimer(ITIMER_REAL, &itv, 0);
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, UV mtime, UV ctime, UV *atime_nsec, UV *mtime_nsec, UV *ctime_nsec)
727 #ifdef TIME_HIRES_STAT
728 #if TIME_HIRES_STAT == 1
729 *atime_nsec = PL_statcache.st_atimespec.tv_nsec;
730 *mtime_nsec = PL_statcache.st_mtimespec.tv_nsec;
731 *ctime_nsec = PL_statcache.st_ctimespec.tv_nsec;
733 #if TIME_HIRES_STAT == 2
734 *atime_nsec = PL_statcache.st_atimensec;
735 *mtime_nsec = PL_statcache.st_mtimensec;
736 *ctime_nsec = PL_statcache.st_ctimensec;
738 #if TIME_HIRES_STAT == 3
739 *atime_nsec = PL_statcache.st_atime_n;
740 *mtime_nsec = PL_statcache.st_mtime_n;
741 *ctime_nsec = PL_statcache.st_ctime_n;
743 #if TIME_HIRES_STAT == 4
744 *atime_nsec = PL_statcache.st_atim.tv_nsec;
745 *mtime_nsec = PL_statcache.st_mtim.tv_nsec;
746 *ctime_nsec = PL_statcache.st_ctim.tv_nsec;
748 #if TIME_HIRES_STAT == 5
749 *atime_nsec = PL_statcache.st_uatime * 1000;
750 *mtime_nsec = PL_statcache.st_umtime * 1000;
751 *ctime_nsec = PL_statcache.st_uctime * 1000;
756 #include "const-c.inc"
758 MODULE = Time::HiRes PACKAGE = Time::HiRes
767 #ifdef ATLEASTFIVEOHOHFIVE
768 #ifdef HAS_GETTIMEOFDAY
770 hv_store(PL_modglobal, "Time::NVtime", 12, newSViv(PTR2IV(myNVtime)), 0);
771 hv_store(PL_modglobal, "Time::U2time", 12, newSViv(PTR2IV(myU2time)), 0);
777 #if defined(USE_ITHREADS) && defined(MY_CXT_KEY)
786 INCLUDE: const-xs.inc
788 #if defined(HAS_USLEEP) && defined(HAS_GETTIMEOFDAY)
794 struct timeval Ta, Tb;
796 gettimeofday(&Ta, NULL);
798 if (useconds > 1E6) {
799 IV seconds = (IV) (useconds / 1E6);
800 /* If usleep() has been implemented using setitimer()
801 * then this contortion is unnecessary-- but usleep()
802 * may be implemented in some other way, so let's contort. */
805 useconds -= 1E6 * seconds;
807 } else if (useconds < 0.0)
808 croak("Time::HiRes::usleep(%"NVgf"): negative time not invented yet", useconds);
809 usleep((U32)useconds);
812 gettimeofday(&Tb, NULL);
814 printf("[%ld %ld] [%ld %ld]\n", Tb.tv_sec, Tb.tv_usec, Ta.tv_sec, Ta.tv_usec);
816 RETVAL = 1E6*(Tb.tv_sec-Ta.tv_sec)+(NV)((IV)Tb.tv_usec-(IV)Ta.tv_usec);
821 #if defined(TIME_HIRES_NANOSLEEP)
828 struct timeval Ta, Tb;
830 gettimeofday(&Ta, NULL);
834 IV sec = (IV) (nsec / 1E9);
839 } else if (nsec < 0.0)
840 croak("Time::HiRes::nanosleep(%"NVgf"): negative time not invented yet", nsec);
841 ts1.tv_sec = (IV) (nsec / 1E9);
842 ts1.tv_nsec = (IV) nsec - (IV) (ts1.tv_sec * NV_1E9);
843 status = nanosleep(&ts1, NULL);
848 gettimeofday(&Tb, NULL);
849 RETVAL = status == 0 ? 1E3*(1E6*(Tb.tv_sec-Ta.tv_sec)+(NV)((IV)Tb.tv_usec-(IV)Ta.tv_usec)) : -1;
854 #else /* #if defined(TIME_HIRES_NANOSLEEP) */
860 croak("Time::HiRes::nanosleep(): unimplemented in this platform");
863 #endif /* #if defined(TIME_HIRES_NANOSLEEP) */
868 struct timeval Ta, Tb;
870 gettimeofday(&Ta, NULL);
872 NV seconds = SvNV(ST(0));
873 if (seconds >= 0.0) {
874 UV useconds = (UV)(1E6 * (seconds - (UV)seconds));
877 if ((IV)useconds < 0) {
878 #if defined(__sparc64__) && defined(__GNUC__)
879 /* Sparc64 gcc 2.95.3 (e.g. on NetBSD) has a bug
880 * where (0.5 - (UV)(0.5)) will under certain
881 * circumstances (if the double is cast to UV more
882 * than once?) evaluate to -0.5, instead of 0.5. */
883 useconds = -(IV)useconds;
884 #endif /* #if defined(__sparc64__) && defined(__GNUC__) */
885 if ((IV)useconds < 0)
886 croak("Time::HiRes::sleep(%"NVgf"): internal error: useconds < 0 (unsigned %"UVuf" signed %"IVdf")", seconds, useconds, (IV)useconds);
890 croak("Time::HiRes::sleep(%"NVgf"): negative time not invented yet", seconds);
893 gettimeofday(&Tb, NULL);
895 printf("[%ld %ld] [%ld %ld]\n", Tb.tv_sec, Tb.tv_usec, Ta.tv_sec, Ta.tv_usec);
897 RETVAL = (NV)(Tb.tv_sec-Ta.tv_sec)+0.000001*(NV)(Tb.tv_usec-Ta.tv_usec);
902 #else /* #if defined(HAS_USLEEP) && defined(HAS_GETTIMEOFDAY) */
908 croak("Time::HiRes::usleep(): unimplemented in this platform");
911 #endif /* #if defined(HAS_USLEEP) && defined(HAS_GETTIMEOFDAY) */
916 ualarm(useconds,interval=0)
920 if (useconds < 0 || interval < 0)
921 croak("Time::HiRes::ualarm(%d, %d): negative time not invented yet", useconds, interval);
922 if (useconds >= IV_1E6 || interval >= IV_1E6)
923 #if defined(HAS_SETITIMER) && defined(ITIMER_REAL)
924 RETVAL = hrt_ualarm_itimer(useconds, interval);
926 croak("Time::HiRes::ualarm(%d, %d): useconds or interval equal or more than %"IVdf, useconds, interval, IV_1E6);
929 RETVAL = ualarm(useconds, interval);
935 alarm(seconds,interval=0)
939 if (seconds < 0.0 || interval < 0.0)
940 croak("Time::HiRes::alarm(%"NVgf", %"NVgf"): negative time not invented yet", seconds, interval);
941 RETVAL = (NV)ualarm((IV)(seconds * IV_1E6),
942 (IV)(interval * IV_1E6)) / NV_1E6;
950 ualarm(useconds,interval=0)
954 croak("Time::HiRes::ualarm(): unimplemented in this platform");
958 alarm(seconds,interval=0)
962 croak("Time::HiRes::alarm(): unimplemented in this platform");
965 #endif /* #ifdef HAS_UALARM */
967 #ifdef HAS_GETTIMEOFDAY
968 # ifdef MACOS_TRADITIONAL /* fix epoch TZ and use unsigned time_t */
976 status = gettimeofday (&Tp, &Tz);
979 Tp.tv_sec += Tz.tz_minuteswest * 60; /* adjust for TZ */
980 if (GIMME == G_ARRAY) {
982 /* Mac OS (Classic) has unsigned time_t */
983 PUSHs(sv_2mortal(newSVuv(Tp.tv_sec)));
984 PUSHs(sv_2mortal(newSViv(Tp.tv_usec)));
987 PUSHs(sv_2mortal(newSVnv(Tp.tv_sec + (Tp.tv_usec / NV_1E6))));
998 status = gettimeofday (&Tp, &Tz);
1000 Tp.tv_sec += Tz.tz_minuteswest * 60; /* adjust for TZ */
1001 RETVAL = Tp.tv_sec + (Tp.tv_usec / NV_1E6);
1008 # else /* MACOS_TRADITIONAL */
1015 status = gettimeofday (&Tp, NULL);
1017 if (GIMME == G_ARRAY) {
1019 PUSHs(sv_2mortal(newSViv(Tp.tv_sec)));
1020 PUSHs(sv_2mortal(newSViv(Tp.tv_usec)));
1023 PUSHs(sv_2mortal(newSVnv(Tp.tv_sec + (Tp.tv_usec / NV_1E6))));
1033 status = gettimeofday (&Tp, NULL);
1035 RETVAL = Tp.tv_sec + (Tp.tv_usec / NV_1E6);
1042 # endif /* MACOS_TRADITIONAL */
1043 #endif /* #ifdef HAS_GETTIMEOFDAY */
1045 #if defined(HAS_GETITIMER) && defined(HAS_SETITIMER)
1047 #define TV2NV(tv) ((NV)((tv).tv_sec) + 0.000001 * (NV)((tv).tv_usec))
1050 setitimer(which, seconds, interval = 0)
1055 struct itimerval newit;
1056 struct itimerval oldit;
1058 if (seconds < 0.0 || interval < 0.0)
1059 croak("Time::HiRes::setitimer(%"IVdf", %"NVgf", %"NVgf"): negative time not invented yet", (IV)which, seconds, interval);
1060 newit.it_value.tv_sec = (IV)seconds;
1061 newit.it_value.tv_usec =
1062 (IV)((seconds - (NV)newit.it_value.tv_sec) * NV_1E6);
1063 newit.it_interval.tv_sec = (IV)interval;
1064 newit.it_interval.tv_usec =
1065 (IV)((interval - (NV)newit.it_interval.tv_sec) * NV_1E6);
1066 if (setitimer(which, &newit, &oldit) == 0) {
1068 PUSHs(sv_2mortal(newSVnv(TV2NV(oldit.it_value))));
1069 if (GIMME == G_ARRAY) {
1071 PUSHs(sv_2mortal(newSVnv(TV2NV(oldit.it_interval))));
1079 struct itimerval nowit;
1081 if (getitimer(which, &nowit) == 0) {
1083 PUSHs(sv_2mortal(newSVnv(TV2NV(nowit.it_value))));
1084 if (GIMME == G_ARRAY) {
1086 PUSHs(sv_2mortal(newSVnv(TV2NV(nowit.it_interval))));
1090 #endif /* #if defined(HAS_GETITIMER) && defined(HAS_SETITIMER) */
1092 #if defined(TIME_HIRES_CLOCK_GETTIME)
1095 clock_gettime(clock_id = CLOCK_REALTIME)
1101 #ifdef TIME_HIRES_CLOCK_GETTIME_SYSCALL
1102 status = syscall(SYS_clock_gettime, clock_id, &ts);
1104 status = clock_gettime(clock_id, &ts);
1106 RETVAL = status == 0 ? ts.tv_sec + (NV) ts.tv_nsec / (NV) 1e9 : -1;
1111 #else /* if defined(TIME_HIRES_CLOCK_GETTIME) */
1114 clock_gettime(clock_id = 0)
1117 croak("Time::HiRes::clock_gettime(): unimplemented in this platform");
1120 #endif /* #if defined(TIME_HIRES_CLOCK_GETTIME) */
1122 #if defined(TIME_HIRES_CLOCK_GETRES)
1125 clock_getres(clock_id = CLOCK_REALTIME)
1131 #ifdef TIME_HIRES_CLOCK_GETRES_SYSCALL
1132 status = syscall(SYS_clock_getres, clock_id, &ts);
1134 status = clock_getres(clock_id, &ts);
1136 RETVAL = status == 0 ? ts.tv_sec + (NV) ts.tv_nsec / (NV) 1e9 : -1;
1141 #else /* if defined(TIME_HIRES_CLOCK_GETRES) */
1144 clock_getres(clock_id = 0)
1147 croak("Time::HiRes::clock_getres(): unimplemented in this platform");
1150 #endif /* #if defined(TIME_HIRES_CLOCK_GETRES) */
1152 #if defined(TIME_HIRES_CLOCK_NANOSLEEP) && defined(TIMER_ABSTIME)
1155 clock_nanosleep(clock_id = CLOCK_REALTIME, sec = 0.0, flags = 0)
1162 struct timeval Ta, Tb;
1164 gettimeofday(&Ta, NULL);
1166 ts.tv_sec = (IV) sec;
1167 ts.tv_nsec = (sec - (NV) ts.tv_sec) * (NV) 1E9;
1168 status = clock_nanosleep(clock_id, flags, &ts, NULL);
1173 gettimeofday(&Tb, NULL);
1174 RETVAL = status == 0 ? 1E3*(1E6*(Tb.tv_sec-Ta.tv_sec)+(NV)((IV)Tb.tv_usec-(IV)Ta.tv_usec)) : -1;
1179 #else /* if defined(TIME_HIRES_CLOCK_NANOSLEEP) && defined(TIMER_ABSTIME) */
1184 croak("Time::HiRes::clock_nanosleep(): unimplemented in this platform");
1187 #endif /* #if defined(TIME_HIRES_CLOCK_NANOSLEEP) && defined(TIMER_ABSTIME) */
1189 #if defined(TIME_HIRES_CLOCK) && defined(CLOCKS_PER_SEC)
1197 RETVAL = clocks == -1 ? -1 : (NV)clocks / (NV)CLOCKS_PER_SEC;
1202 #else /* if defined(TIME_HIRES_CLOCK) && defined(CLOCKS_PER_SEC) */
1207 croak("Time::HiRes::clock(): unimplemented in this platform");
1210 #endif /* #if defined(TIME_HIRES_CLOCK) && defined(CLOCKS_PER_SEC) */
1217 XPUSHs(sv_2mortal(newSVsv(items == 1 ? ST(0) : DEFSV)));
1220 PL_laststatval = -1;
1221 (void)*(PL_ppaddr[OP_STAT])(aTHX);
1224 if (PL_laststatval == 0) {
1225 /* We assume that pp_stat() left us with 13 valid stack items. */
1226 UV atime = SvUV(ST( 8));
1227 UV mtime = SvUV(ST( 9));
1228 UV ctime = SvUV(ST(10));
1232 hrstatns(atime, mtime, ctime,
1233 &atime_nsec, &mtime_nsec, &ctime_nsec);
1235 ST( 8) = sv_2mortal(newSVnv(atime + 1e-9 * (NV) atime_nsec));
1237 ST( 9) = sv_2mortal(newSVnv(mtime + 1e-9 * (NV) mtime_nsec));
1239 ST(10) = sv_2mortal(newSVnv(ctime + 1e-9 * (NV) ctime_nsec));