3 * Copyright (c) 1996-2002 Douglas E. Wegscheid. All rights reserved.
5 * Copyright (c) 2002,2003,2004,2005 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 #ifndef PerlProc_pause
41 # define PerlProc_pause() Pause()
47 # undef Pause /* In case perl.h did it already. */
48 # define Pause() sleep(~0) /* Zzz for a long time. */
51 /* Though the cpp define ITIMER_VIRTUAL is available the functionality
52 * is not supported in Cygwin as of August 2004, ditto for Win32.
53 * Neither are ITIMER_PROF or ITIMER_REALPROF implemented. --jhi
55 #if defined(__CYGWIN__) || defined(WIN32)
56 # undef ITIMER_VIRTUAL
58 # undef ITIMER_REALPROF
61 /* 5.004 doesn't define PL_sv_undef */
62 #ifndef ATLEASTFIVEOHOHFIVE
64 # define PL_sv_undef sv_undef
68 #if defined(TIME_HIRES_CLOCK_GETTIME) && defined(_STRUCT_ITIMERSPEC)
70 /* HP-UX has CLOCK_XXX values but as enums, not as defines.
71 * The only way to detect these would be to test compile for each. */
73 # define CLOCK_REALTIME CLOCK_REALTIME
74 # define CLOCK_VIRTUAL CLOCK_VIRTUAL
75 # define CLOCK_PROFILE CLOCK_PROFILE
76 # endif /* # ifdef __hpux */
78 #endif /* #if defined(TIME_HIRES_CLOCK_GETTIME) && defined(_STRUCT_ITIMERSPEC) */
80 #if defined(WIN32) || defined(CYGWIN_WITH_W32API)
82 #ifndef HAS_GETTIMEOFDAY
83 # define HAS_GETTIMEOFDAY
86 /* shows up in winsock.h?
94 unsigned __int64 ft_i64;
98 #define MY_CXT_KEY "Time::HiRes_" XS_VERSION
101 unsigned long run_count;
102 unsigned __int64 base_ticks;
103 unsigned __int64 tick_frequency;
104 FT_t base_systime_as_filetime;
105 unsigned __int64 reset_time;
110 /* Number of 100 nanosecond units from 1/1/1601 to 1/1/1970 */
112 # define Const64(x) x##LL
114 # define Const64(x) x##i64
116 #define EPOCH_BIAS Const64(116444736000000000)
118 /* NOTE: This does not compute the timezone info (doing so can be expensive,
119 * and appears to be unsupported even by glibc) */
121 /* dMY_CXT needs a Perl context and we don't want to call PERL_GET_CONTEXT
122 for performance reasons */
125 #define gettimeofday(tp, not_used) _gettimeofday(aTHX_ tp, not_used)
127 /* If the performance counter delta drifts more than 0.5 seconds from the
128 * system time then we recalibrate to the system time. This means we may
129 * move *backwards* in time! */
130 #define MAX_PERF_COUNTER_SKEW Const64(5000000) /* 0.5 seconds */
132 /* Reset reading from the performance counter every five minutes.
133 * Many PC clocks just seem to be so bad. */
134 #define MAX_PERF_COUNTER_TICKS Const64(300000000) /* 300 seconds */
137 _gettimeofday(pTHX_ struct timeval *tp, void *not_used)
141 unsigned __int64 ticks;
144 if (MY_CXT.run_count++ == 0 ||
145 MY_CXT.base_systime_as_filetime.ft_i64 > MY_CXT.reset_time) {
146 QueryPerformanceFrequency((LARGE_INTEGER*)&MY_CXT.tick_frequency);
147 QueryPerformanceCounter((LARGE_INTEGER*)&MY_CXT.base_ticks);
148 GetSystemTimeAsFileTime(&MY_CXT.base_systime_as_filetime.ft_val);
149 ft.ft_i64 = MY_CXT.base_systime_as_filetime.ft_i64;
150 MY_CXT.reset_time = ft.ft_i64 + MAX_PERF_COUNTER_TICKS;
154 QueryPerformanceCounter((LARGE_INTEGER*)&ticks);
155 ticks -= MY_CXT.base_ticks;
156 ft.ft_i64 = MY_CXT.base_systime_as_filetime.ft_i64
157 + Const64(10000000) * (ticks / MY_CXT.tick_frequency)
158 +(Const64(10000000) * (ticks % MY_CXT.tick_frequency)) / MY_CXT.tick_frequency;
159 diff = ft.ft_i64 - MY_CXT.base_systime_as_filetime.ft_i64;
160 if (diff < -MAX_PERF_COUNTER_SKEW || diff > MAX_PERF_COUNTER_SKEW) {
161 MY_CXT.base_ticks += ticks;
162 GetSystemTimeAsFileTime(&MY_CXT.base_systime_as_filetime.ft_val);
163 ft.ft_i64 = MY_CXT.base_systime_as_filetime.ft_i64;
167 /* seconds since epoch */
168 tp->tv_sec = (long)((ft.ft_i64 - EPOCH_BIAS) / Const64(10000000));
170 /* microseconds remaining */
171 tp->tv_usec = (long)((ft.ft_i64 / Const64(10)) % Const64(1000000));
177 #if defined(WIN32) && !defined(ATLEASTFIVEOHOHFIVE)
179 sleep(unsigned int t)
186 #if !defined(HAS_GETTIMEOFDAY) && defined(VMS)
187 #define HAS_GETTIMEOFDAY
190 #include <time.h> /* gettimeofday */
191 #include <stdlib.h> /* qdiv */
192 #include <starlet.h> /* sys$gettim */
195 #include <lib$routines.h> /* lib$ediv() */
199 VMS binary time is expressed in 100 nano-seconds since
200 system base time which is 17-NOV-1858 00:00:00.00
203 #define DIV_100NS_TO_SECS 10000000L
204 #define DIV_100NS_TO_USECS 10L
207 gettimeofday is supposed to return times since the epoch
208 so need to determine this in terms of VMS base time
210 static $DESCRIPTOR(dscepoch,"01-JAN-1970 00:00:00.00");
213 static long base_adjust[2]={0L,0L};
215 static __int64 base_adjust=0;
220 If we don't have gettimeofday, then likely we are on a VMS machine that
221 operates on local time rather than UTC...so we have to zone-adjust.
222 This code gleefully swiped from VMS.C
225 /* method used to handle UTC conversions:
226 * 1 == CRTL gmtime(); 2 == SYS$TIMEZONE_DIFFERENTIAL; 3 == no correction
228 static int gmtime_emulation_type;
229 /* number of secs to add to UTC POSIX-style time to get local time */
230 static long int utc_offset_secs;
231 static struct dsc$descriptor_s fildevdsc =
232 { 12, DSC$K_DTYPE_T, DSC$K_CLASS_S, "LNM$FILE_DEV" };
233 static struct dsc$descriptor_s *fildev[] = { &fildevdsc, NULL };
235 static time_t toutc_dst(time_t loc) {
238 if ((rsltmp = localtime(&loc)) == NULL) return -1;
239 loc -= utc_offset_secs;
240 if (rsltmp->tm_isdst) loc -= 3600;
244 static time_t toloc_dst(time_t utc) {
247 utc += utc_offset_secs;
248 if ((rsltmp = localtime(&utc)) == NULL) return -1;
249 if (rsltmp->tm_isdst) utc += 3600;
253 #define _toutc(secs) ((secs) == (time_t) -1 ? (time_t) -1 : \
254 ((gmtime_emulation_type || timezone_setup()), \
255 (gmtime_emulation_type == 1 ? toutc_dst(secs) : \
256 ((secs) - utc_offset_secs))))
258 #define _toloc(secs) ((secs) == (time_t) -1 ? (time_t) -1 : \
259 ((gmtime_emulation_type || timezone_setup()), \
260 (gmtime_emulation_type == 1 ? toloc_dst(secs) : \
261 ((secs) + utc_offset_secs))))
268 if (gmtime_emulation_type == 0) {
270 time_t base = 15 * 86400; /* 15jan71; to avoid month/year ends between */
271 /* results of calls to gmtime() and localtime() */
274 gmtime_emulation_type++;
275 if ((tm_p = gmtime(&base)) == NULL) { /* CRTL gmtime() is a fake */
276 char off[LNM$C_NAMLENGTH+1];;
278 gmtime_emulation_type++;
279 if (!Perl_vmstrnenv("SYS$TIMEZONE_DIFFERENTIAL",off,0,fildev,0)) {
280 gmtime_emulation_type++;
282 Perl_warn(aTHX_ "no UTC offset information; assuming local time is UTC");
284 else { utc_offset_secs = atol(off); }
286 else { /* We've got a working gmtime() */
287 struct tm gmt, local;
290 tm_p = localtime(&base);
292 utc_offset_secs = (local.tm_mday - gmt.tm_mday) * 86400;
293 utc_offset_secs += (local.tm_hour - gmt.tm_hour) * 3600;
294 utc_offset_secs += (local.tm_min - gmt.tm_min) * 60;
295 utc_offset_secs += (local.tm_sec - gmt.tm_sec);
303 gettimeofday (struct timeval *tp, void *tpz)
309 long div_100ns_to_secs;
310 long div_100ns_to_usecs;
318 In case of error, tv_usec = 0 and tv_sec = VMS condition code.
319 The return from function is also set to -1.
320 This is not exactly as per the manual page.
326 if (base_adjust[0]==0 && base_adjust[1]==0) {
328 if (base_adjust==0) { /* Need to determine epoch adjustment */
330 ret=sys$bintim(&dscepoch,&base_adjust);
331 if (1 != (ret &&1)) {
337 ret=sys$gettim(&quad); /* Get VMS system time */
338 if ((1 && ret) == 1) {
340 quad[0] -= base_adjust[0]; /* convert to epoch offset */
341 quad[1] -= base_adjust[1]; /* convert 2nd half of quadword */
342 div_100ns_to_secs = DIV_100NS_TO_SECS;
343 div_100ns_to_usecs = DIV_100NS_TO_USECS;
344 lib$ediv(&div_100ns_to_secs,&quad,&quo,&rem);
347 lib$ediv(&div_100ns_to_usecs,&quad1,&quo1,&rem1);
348 tp->tv_sec = quo; /* Whole seconds */
349 tp->tv_usec = quo1; /* Micro-seconds */
351 quad -= base_adjust; /* convert to epoch offset */
352 ans1=qdiv(quad,DIV_100NS_TO_SECS);
353 ans2=qdiv(ans1.rem,DIV_100NS_TO_USECS);
354 tp->tv_sec = ans1.quot; /* Whole seconds */
355 tp->tv_usec = ans2.quot; /* Micro-seconds */
363 if (VMSISH_TIME) tp->tv_sec = _toloc(tp->tv_sec);
365 if (!VMSISH_TIME) tp->tv_sec = _toutc(tp->tv_sec);
373 /* Do not use H A S _ N A N O S L E E P
374 * so that Perl Configure doesn't scan for it (and pull in -lrt and
375 * the like which are not usually good ideas for the default Perl).
376 * (We are part of the core perl now.)
377 * The TIME_HIRES_NANOSLEEP is set by Makefile.PL. */
378 #if !defined(HAS_USLEEP) && defined(TIME_HIRES_NANOSLEEP)
380 #define usleep hrt_nanosleep /* could conflict with ncurses for static build */
383 hrt_nanosleep(unsigned long usec) /* This is used to emulate usleep. */
386 res.tv_sec = usec/1000/1000;
387 res.tv_nsec = ( usec - res.tv_sec*1000*1000 ) * 1000;
388 nanosleep(&res, NULL);
391 #endif /* #if !defined(HAS_USLEEP) && defined(TIME_HIRES_NANOSLEEP) */
393 #if !defined(HAS_USLEEP) && defined(HAS_SELECT)
394 #ifndef SELECT_IS_BROKEN
396 #define usleep hrt_usleep /* could conflict with ncurses for static build */
399 hrt_usleep(unsigned long usec)
404 select(0, (Select_fd_set_t)NULL, (Select_fd_set_t)NULL,
405 (Select_fd_set_t)NULL, &tv);
408 #endif /* #if !defined(HAS_USLEEP) && defined(HAS_SELECT) */
410 #if !defined(HAS_USLEEP) && defined(WIN32)
412 #define usleep hrt_usleep /* could conflict with ncurses for static build */
415 hrt_usleep(unsigned long usec)
421 #endif /* #if !defined(HAS_USLEEP) && defined(WIN32) */
423 #if !defined(HAS_USLEEP) && defined(TIME_HIRES_NANOSLEEP)
425 #define usleep hrt_usleep /* could conflict with ncurses for static build */
428 hrt_usleep(unsigned long usec)
431 ts1.tv_sec = usec * 1000; /* Ignoring wraparound. */
433 nanosleep(&ts1, NULL);
436 #endif /* #if !defined(HAS_USLEEP) && defined(TIME_HIRES_NANOSLEEP) */
438 #if !defined(HAS_USLEEP) && defined(HAS_POLL)
440 #define usleep hrt_usleep /* could conflict with ncurses for static build */
443 hrt_usleep(unsigned long usec)
445 int msec = usec / 1000;
449 #endif /* #if !defined(HAS_USLEEP) && defined(HAS_POLL) */
451 #if !defined(HAS_UALARM) && defined(HAS_SETITIMER)
453 #define ualarm hrt_ualarm /* could conflict with ncurses for static build */
456 hrt_ualarm(int usec, int interval)
458 struct itimerval itv;
459 itv.it_value.tv_sec = usec / 1000000;
460 itv.it_value.tv_usec = usec % 1000000;
461 itv.it_interval.tv_sec = interval / 1000000;
462 itv.it_interval.tv_usec = interval % 1000000;
463 return setitimer(ITIMER_REAL, &itv, 0);
465 #endif /* #if !defined(HAS_UALARM) && defined(HAS_SETITIMER) */
467 #if !defined(HAS_UALARM) && defined(VMS)
469 #define ualarm vms_ualarm
471 #include <lib$routines.h>
479 #define VMSERR(s) (!((s)&1))
482 us_to_VMS(useconds_t mseconds, unsigned long v[])
491 iss = lib$addx(qq,qq,qq);
492 if (VMSERR(iss)) lib$signal(iss);
493 iss = lib$subx(v,qq,v);
494 if (VMSERR(iss)) lib$signal(iss);
495 iss = lib$addx(qq,qq,qq);
496 if (VMSERR(iss)) lib$signal(iss);
497 iss = lib$subx(v,qq,v);
498 if (VMSERR(iss)) lib$signal(iss);
499 iss = lib$subx(v,qq,v);
500 if (VMSERR(iss)) lib$signal(iss);
504 VMS_to_us(unsigned long v[])
507 unsigned long div=10,quot, rem;
509 iss = lib$ediv(&div,v,",&rem);
510 if (VMSERR(iss)) lib$signal(iss);
515 typedef unsigned short word;
516 typedef struct _ualarm {
519 unsigned long delay[2];
520 unsigned long interval[2];
521 unsigned long remain[2];
526 static Alarm *a0, alarm_base;
531 static void ualarm_AST(Alarm *a);
534 vms_ualarm(int mseconds, int interval)
543 static struct item_list3 itmlst[2];
544 static int first = 1;
550 itmlst[0].code = JPI$_ASTEN;
551 itmlst[0].length = sizeof(asten);
552 itmlst[0].retlenaddr = NULL;
554 itmlst[1].length = 0;
555 itmlst[1].bufaddr = NULL;
556 itmlst[1].retlenaddr = NULL;
558 iss = lib$get_ef(&alarm_ef);
559 if (VMSERR(iss)) lib$signal(iss);
562 a0->function = UAL_NULL;
564 itmlst[0].bufaddr = &asten;
566 iss = sys$getjpiw(0,0,0,itmlst,0,0,0);
567 if (VMSERR(iss)) lib$signal(iss);
568 if (!(asten&0x08)) return -1;
572 a->function = UAL_SET;
574 a->function = UAL_CLEAR;
577 us_to_VMS(mseconds, a->delay);
579 us_to_VMS(interval, a->interval);
584 iss = sys$clref(alarm_ef);
585 if (VMSERR(iss)) lib$signal(iss);
587 iss = sys$dclast(ualarm_AST,a,0);
588 if (VMSERR(iss)) lib$signal(iss);
590 iss = sys$waitfr(alarm_ef);
591 if (VMSERR(iss)) lib$signal(iss);
593 if (a->function == UAL_ACTIVE)
594 return VMS_to_us(a->remain);
605 unsigned long now[2];
607 iss = sys$gettim(now);
608 if (VMSERR(iss)) lib$signal(iss);
610 if (a->function == UAL_SET || a->function == UAL_CLEAR) {
611 if (a0->function == UAL_ACTIVE) {
612 iss = sys$cantim(a0,PSL$C_USER);
613 if (VMSERR(iss)) lib$signal(iss);
615 iss = lib$subx(a0->remain, now, a->remain);
616 if (VMSERR(iss)) lib$signal(iss);
618 if (a->remain[1] & 0x80000000)
619 a->remain[0] = a->remain[1] = 0;
622 if (a->function == UAL_SET) {
623 a->function = a0->function;
624 a0->function = UAL_ACTIVE;
625 a0->repeat = a->repeat;
627 a0->interval[0] = a->interval[0];
628 a0->interval[1] = a->interval[1];
630 a0->delay[0] = a->delay[0];
631 a0->delay[1] = a->delay[1];
633 iss = lib$subx(now, a0->delay, a0->remain);
634 if (VMSERR(iss)) lib$signal(iss);
636 iss = sys$setimr(0,a0->delay,ualarm_AST,a0);
637 if (VMSERR(iss)) lib$signal(iss);
639 a->function = a0->function;
640 a0->function = UAL_NULL;
642 iss = sys$setef(alarm_ef);
643 if (VMSERR(iss)) lib$signal(iss);
644 } else if (a->function == UAL_ACTIVE) {
646 iss = lib$subx(now, a->interval, a->remain);
647 if (VMSERR(iss)) lib$signal(iss);
649 iss = sys$setimr(0,a->interval,ualarm_AST,a);
650 if (VMSERR(iss)) lib$signal(iss);
652 a->function = UAL_NULL;
655 if (VMSERR(iss)) lib$signal(iss);
656 lib$signal(SS$_ASTFLT);
658 lib$signal(SS$_BADPARAM);
662 #endif /* #if !defined(HAS_UALARM) && defined(VMS) */
664 #ifdef HAS_GETTIMEOFDAY
667 myU2time(pTHX_ UV *ret)
671 status = gettimeofday (&Tp, NULL);
685 status = gettimeofday (&Tp, NULL);
686 return status == 0 ? Tp.tv_sec + (Tp.tv_usec / 1000000.) : -1.0;
689 #endif /* #ifdef HAS_GETTIMEOFDAY */
691 #include "const-c.inc"
693 MODULE = Time::HiRes PACKAGE = Time::HiRes
702 #ifdef ATLEASTFIVEOHOHFIVE
703 #ifdef HAS_GETTIMEOFDAY
705 hv_store(PL_modglobal, "Time::NVtime", 12, newSViv(PTR2IV(myNVtime)), 0);
706 hv_store(PL_modglobal, "Time::U2time", 12, newSViv(PTR2IV(myU2time)), 0);
712 #if defined(USE_ITHREADS) && defined(MY_CXT_KEY)
721 INCLUDE: const-xs.inc
723 #if defined(HAS_USLEEP) && defined(HAS_GETTIMEOFDAY)
729 struct timeval Ta, Tb;
731 gettimeofday(&Ta, NULL);
733 if (useconds > 1E6) {
734 IV seconds = (IV) (useconds / 1E6);
735 /* If usleep() has been implemented using setitimer()
736 * then this contortion is unnecessary-- but usleep()
737 * may be implemented in some other way, so let's contort. */
740 useconds -= 1E6 * seconds;
742 } else if (useconds < 0.0)
743 croak("Time::HiRes::usleep(%"NVgf"): negative time not invented yet", useconds);
744 usleep((U32)useconds);
747 gettimeofday(&Tb, NULL);
749 printf("[%ld %ld] [%ld %ld]\n", Tb.tv_sec, Tb.tv_usec, Ta.tv_sec, Ta.tv_usec);
751 RETVAL = 1E6*(Tb.tv_sec-Ta.tv_sec)+(NV)((IV)Tb.tv_usec-(IV)Ta.tv_usec);
756 #if defined(TIME_HIRES_NANOSLEEP)
763 struct timeval Ta, Tb;
765 gettimeofday(&Ta, NULL);
769 IV sec = (IV) (nsec / 1E9);
774 } else if (nsec < 0.0)
775 croak("Time::HiRes::nanosleep(%"NVgf"): negative time not invented yet", nsec);
776 ts1.tv_sec = (IV) (nsec / 1E9);
777 ts1.tv_nsec = (IV) nsec - ts1.tv_sec * 1E9;
778 status = nanosleep(&ts1, NULL);
783 gettimeofday(&Tb, NULL);
784 RETVAL = status == 0 ? 1E3*(1E6*(Tb.tv_sec-Ta.tv_sec)+(NV)((IV)Tb.tv_usec-(IV)Ta.tv_usec)) : -1;
789 #else /* #if defined(TIME_HIRES_NANOSLEEP) */
795 croak("Time::HiRes::nanosleep(): unimplemented in this platform");
798 #endif /* #if defined(TIME_HIRES_NANOSLEEP) */
803 struct timeval Ta, Tb;
805 gettimeofday(&Ta, NULL);
807 NV seconds = SvNV(ST(0));
808 if (seconds >= 0.0) {
809 UV useconds = (UV)(1E6 * (seconds - (UV)seconds));
812 if ((IV)useconds < 0) {
813 #if defined(__sparc64__) && defined(__GNUC__)
814 /* Sparc64 gcc 2.95.3 (e.g. on NetBSD) has a bug
815 * where (0.5 - (UV)(0.5)) will under certain
816 * circumstances (if the double is cast to UV more
817 * than once?) evaluate to -0.5, instead of 0.5. */
818 useconds = -(IV)useconds;
819 #endif /* #if defined(__sparc64__) && defined(__GNUC__) */
820 if ((IV)useconds < 0)
821 croak("Time::HiRes::sleep(%"NVgf"): internal error: useconds < 0 (unsigned %"UVuf" signed %"IVdf")", seconds, useconds, (IV)useconds);
825 croak("Time::HiRes::sleep(%"NVgf"): negative time not invented yet", seconds);
828 gettimeofday(&Tb, NULL);
830 printf("[%ld %ld] [%ld %ld]\n", Tb.tv_sec, Tb.tv_usec, Ta.tv_sec, Ta.tv_usec);
832 RETVAL = (NV)(Tb.tv_sec-Ta.tv_sec)+0.000001*(NV)(Tb.tv_usec-Ta.tv_usec);
837 #else /* #if defined(HAS_USLEEP) && defined(HAS_GETTIMEOFDAY) */
843 croak("Time::HiRes::usleep(): unimplemented in this platform");
846 #endif /* #if defined(HAS_USLEEP) && defined(HAS_GETTIMEOFDAY) */
851 ualarm(useconds,interval=0)
855 if (useconds < 0 || interval < 0)
856 croak("Time::HiRes::ualarm(%d, %d): negative time not invented yet", useconds, interval);
857 RETVAL = ualarm(useconds, interval);
863 alarm(seconds,interval=0)
867 if (seconds < 0.0 || interval < 0.0)
868 croak("Time::HiRes::alarm(%"NVgf", %"NVgf"): negative time not invented yet", seconds, interval);
869 RETVAL = (NV)ualarm(seconds * 1000000,
870 interval * 1000000) / 1E6;
878 ualarm(useconds,interval=0)
882 croak("Time::HiRes::ualarm(): unimplemented in this platform");
886 alarm(seconds,interval=0)
890 croak("Time::HiRes::alarm(): unimplemented in this platform");
893 #endif /* #ifdef HAS_UALARM */
895 #ifdef HAS_GETTIMEOFDAY
896 # ifdef MACOS_TRADITIONAL /* fix epoch TZ and use unsigned time_t */
904 status = gettimeofday (&Tp, &Tz);
907 Tp.tv_sec += Tz.tz_minuteswest * 60; /* adjust for TZ */
908 if (GIMME == G_ARRAY) {
910 /* Mac OS (Classic) has unsigned time_t */
911 PUSHs(sv_2mortal(newSVuv(Tp.tv_sec)));
912 PUSHs(sv_2mortal(newSViv(Tp.tv_usec)));
915 PUSHs(sv_2mortal(newSVnv(Tp.tv_sec + (Tp.tv_usec / 1000000.0))));
926 status = gettimeofday (&Tp, &Tz);
928 Tp.tv_sec += Tz.tz_minuteswest * 60; /* adjust for TZ */
929 RETVAL = Tp.tv_sec + (Tp.tv_usec / 1000000.0);
936 # else /* MACOS_TRADITIONAL */
943 status = gettimeofday (&Tp, NULL);
945 if (GIMME == G_ARRAY) {
947 PUSHs(sv_2mortal(newSViv(Tp.tv_sec)));
948 PUSHs(sv_2mortal(newSViv(Tp.tv_usec)));
951 PUSHs(sv_2mortal(newSVnv(Tp.tv_sec + (Tp.tv_usec / 1000000.0))));
961 status = gettimeofday (&Tp, NULL);
963 RETVAL = Tp.tv_sec + (Tp.tv_usec / 1000000.);
970 # endif /* MACOS_TRADITIONAL */
971 #endif /* #ifdef HAS_GETTIMEOFDAY */
973 #if defined(HAS_GETITIMER) && defined(HAS_SETITIMER)
975 #define TV2NV(tv) ((NV)((tv).tv_sec) + 0.000001 * (NV)((tv).tv_usec))
978 setitimer(which, seconds, interval = 0)
983 struct itimerval newit;
984 struct itimerval oldit;
986 if (seconds < 0.0 || interval < 0.0)
987 croak("Time::HiRes::setitimer(%"IVdf", %"NVgf", %"NVgf"): negative time not invented yet", (IV)which, seconds, interval);
988 newit.it_value.tv_sec = seconds;
989 newit.it_value.tv_usec =
990 (seconds - (NV)newit.it_value.tv_sec) * 1000000.0;
991 newit.it_interval.tv_sec = interval;
992 newit.it_interval.tv_usec =
993 (interval - (NV)newit.it_interval.tv_sec) * 1000000.0;
994 if (setitimer(which, &newit, &oldit) == 0) {
996 PUSHs(sv_2mortal(newSVnv(TV2NV(oldit.it_value))));
997 if (GIMME == G_ARRAY) {
999 PUSHs(sv_2mortal(newSVnv(TV2NV(oldit.it_interval))));
1007 struct itimerval nowit;
1009 if (getitimer(which, &nowit) == 0) {
1011 PUSHs(sv_2mortal(newSVnv(TV2NV(nowit.it_value))));
1012 if (GIMME == G_ARRAY) {
1014 PUSHs(sv_2mortal(newSVnv(TV2NV(nowit.it_interval))));
1018 #endif /* #if defined(HAS_GETITIMER) && defined(HAS_SETITIMER) */
1020 #if defined(TIME_HIRES_CLOCK_GETTIME)
1023 clock_gettime(clock_id = CLOCK_REALTIME)
1029 #ifdef TIME_HIRES_CLOCK_GETTIME_SYSCALL
1030 status = syscall(SYS_clock_gettime, clock_id, &ts);
1032 status = clock_gettime(clock_id, &ts);
1034 RETVAL = status == 0 ? ts.tv_sec + (NV) ts.tv_nsec / (NV) 1e9 : -1;
1039 #else /* if defined(TIME_HIRES_CLOCK_GETTIME) */
1042 clock_gettime(clock_id = 0)
1045 croak("Time::HiRes::clock_gettime(): unimplemented in this platform");
1048 #endif /* #if defined(TIME_HIRES_CLOCK_GETTIME) */
1050 #if defined(TIME_HIRES_CLOCK_GETRES)
1053 clock_getres(clock_id = CLOCK_REALTIME)
1059 #ifdef TIME_HIRES_CLOCK_GETRES_SYSCALL
1060 status = syscall(SYS_clock_getres, clock_id, &ts);
1062 status = clock_getres(clock_id, &ts);
1064 RETVAL = status == 0 ? ts.tv_sec + (NV) ts.tv_nsec / (NV) 1e9 : -1;
1069 #else /* if defined(TIME_HIRES_CLOCK_GETRES) */
1072 clock_getres(clock_id = 0)
1075 croak("Time::HiRes::clock_getres(): unimplemented in this platform");
1078 #endif /* #if defined(TIME_HIRES_CLOCK_GETRES) */
1080 #if defined(TIME_HIRES_CLOCK_NANOSLEEP) && defined(TIMER_ABSTIME)
1083 clock_nanosleep(clock_id = CLOCK_REALTIME, sec = 0.0, flags = 0)
1090 struct timeval Ta, Tb;
1092 gettimeofday(&Ta, NULL);
1094 ts.tv_sec = (IV) sec;
1095 ts.tv_nsec = (sec - (NV) ts.tv_sec) * (NV) 1E9;
1096 status = clock_nanosleep(clock_id, flags, &ts, NULL);
1101 gettimeofday(&Tb, NULL);
1102 RETVAL = status == 0 ? 1E3*(1E6*(Tb.tv_sec-Ta.tv_sec)+(NV)((IV)Tb.tv_usec-(IV)Ta.tv_usec)) : -1;
1107 #else /* if defined(TIME_HIRES_CLOCK_NANOSLEEP) && defined(TIMER_ABSTIME) */
1112 croak("Time::HiRes::clock_nanosleep(): unimplemented in this platform");
1115 #endif /* #if defined(TIME_HIRES_CLOCK_NANOSLEEP) && defined(TIMER_ABSTIME) */
1117 #if defined(TIME_HIRES_CLOCK) && defined(CLOCKS_PER_SEC)
1125 RETVAL = clocks == -1 ? -1 : (NV)clocks / (NV)CLOCKS_PER_SEC;
1130 #else /* if defined(TIME_HIRES_CLOCK) && defined(CLOCKS_PER_SEC) */
1135 croak("Time::HiRes::clock(): unimplemented in this platform");
1138 #endif /* #if defined(TIME_HIRES_CLOCK) && defined(CLOCKS_PER_SEC) */