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 #include "const-c.inc"
70 #if defined(WIN32) || defined(CYGWIN_WITH_W32API)
72 #ifndef HAS_GETTIMEOFDAY
73 # define HAS_GETTIMEOFDAY
76 /* shows up in winsock.h?
84 unsigned __int64 ft_i64;
88 #define MY_CXT_KEY "Time::HiRes_" XS_VERSION
91 unsigned long run_count;
92 unsigned __int64 base_ticks;
93 unsigned __int64 tick_frequency;
94 FT_t base_systime_as_filetime;
95 unsigned __int64 reset_time;
100 /* Number of 100 nanosecond units from 1/1/1601 to 1/1/1970 */
102 # define Const64(x) x##LL
104 # define Const64(x) x##i64
106 #define EPOCH_BIAS Const64(116444736000000000)
108 /* NOTE: This does not compute the timezone info (doing so can be expensive,
109 * and appears to be unsupported even by glibc) */
111 /* dMY_CXT needs a Perl context and we don't want to call PERL_GET_CONTEXT
112 for performance reasons */
115 #define gettimeofday(tp, not_used) _gettimeofday(aTHX_ tp, not_used)
117 /* If the performance counter delta drifts more than 0.5 seconds from the
118 * system time then we recalibrate to the system time. This means we may
119 * move *backwards* in time! */
120 #define MAX_PERF_COUNTER_SKEW Const64(5000000) /* 0.5 seconds */
122 /* Reset reading from the performance counter every five minutes.
123 * Many PC clocks just seem to be so bad. */
124 #define MAX_PERF_COUNTER_TICKS Const64(300000000) /* 300 seconds */
127 _gettimeofday(pTHX_ struct timeval *tp, void *not_used)
131 unsigned __int64 ticks;
134 if (MY_CXT.run_count++ == 0 ||
135 MY_CXT.base_systime_as_filetime.ft_i64 > MY_CXT.reset_time) {
136 QueryPerformanceFrequency((LARGE_INTEGER*)&MY_CXT.tick_frequency);
137 QueryPerformanceCounter((LARGE_INTEGER*)&MY_CXT.base_ticks);
138 GetSystemTimeAsFileTime(&MY_CXT.base_systime_as_filetime.ft_val);
139 ft.ft_i64 = MY_CXT.base_systime_as_filetime.ft_i64;
140 MY_CXT.reset_time = ft.ft_i64 + MAX_PERF_COUNTER_TICKS;
144 QueryPerformanceCounter((LARGE_INTEGER*)&ticks);
145 ticks -= MY_CXT.base_ticks;
146 ft.ft_i64 = MY_CXT.base_systime_as_filetime.ft_i64
147 + Const64(10000000) * (ticks / MY_CXT.tick_frequency)
148 +(Const64(10000000) * (ticks % MY_CXT.tick_frequency)) / MY_CXT.tick_frequency;
149 diff = ft.ft_i64 - MY_CXT.base_systime_as_filetime.ft_i64;
150 if (diff < -MAX_PERF_COUNTER_SKEW || diff > MAX_PERF_COUNTER_SKEW) {
151 MY_CXT.base_ticks += ticks;
152 GetSystemTimeAsFileTime(&MY_CXT.base_systime_as_filetime.ft_val);
153 ft.ft_i64 = MY_CXT.base_systime_as_filetime.ft_i64;
157 /* seconds since epoch */
158 tp->tv_sec = (long)((ft.ft_i64 - EPOCH_BIAS) / Const64(10000000));
160 /* microseconds remaining */
161 tp->tv_usec = (long)((ft.ft_i64 / Const64(10)) % Const64(1000000));
167 #if defined(WIN32) && !defined(ATLEASTFIVEOHOHFIVE)
169 sleep(unsigned int t)
176 #if !defined(HAS_GETTIMEOFDAY) && defined(VMS)
177 #define HAS_GETTIMEOFDAY
180 #include <time.h> /* gettimeofday */
181 #include <stdlib.h> /* qdiv */
182 #include <starlet.h> /* sys$gettim */
185 #include <lib$routines.h> /* lib$ediv() */
189 VMS binary time is expressed in 100 nano-seconds since
190 system base time which is 17-NOV-1858 00:00:00.00
193 #define DIV_100NS_TO_SECS 10000000L
194 #define DIV_100NS_TO_USECS 10L
197 gettimeofday is supposed to return times since the epoch
198 so need to determine this in terms of VMS base time
200 static $DESCRIPTOR(dscepoch,"01-JAN-1970 00:00:00.00");
203 static long base_adjust[2]={0L,0L};
205 static __int64 base_adjust=0;
210 If we don't have gettimeofday, then likely we are on a VMS machine that
211 operates on local time rather than UTC...so we have to zone-adjust.
212 This code gleefully swiped from VMS.C
215 /* method used to handle UTC conversions:
216 * 1 == CRTL gmtime(); 2 == SYS$TIMEZONE_DIFFERENTIAL; 3 == no correction
218 static int gmtime_emulation_type;
219 /* number of secs to add to UTC POSIX-style time to get local time */
220 static long int utc_offset_secs;
221 static struct dsc$descriptor_s fildevdsc =
222 { 12, DSC$K_DTYPE_T, DSC$K_CLASS_S, "LNM$FILE_DEV" };
223 static struct dsc$descriptor_s *fildev[] = { &fildevdsc, NULL };
225 static time_t toutc_dst(time_t loc) {
228 if ((rsltmp = localtime(&loc)) == NULL) return -1;
229 loc -= utc_offset_secs;
230 if (rsltmp->tm_isdst) loc -= 3600;
234 static time_t toloc_dst(time_t utc) {
237 utc += utc_offset_secs;
238 if ((rsltmp = localtime(&utc)) == NULL) return -1;
239 if (rsltmp->tm_isdst) utc += 3600;
243 #define _toutc(secs) ((secs) == (time_t) -1 ? (time_t) -1 : \
244 ((gmtime_emulation_type || timezone_setup()), \
245 (gmtime_emulation_type == 1 ? toutc_dst(secs) : \
246 ((secs) - utc_offset_secs))))
248 #define _toloc(secs) ((secs) == (time_t) -1 ? (time_t) -1 : \
249 ((gmtime_emulation_type || timezone_setup()), \
250 (gmtime_emulation_type == 1 ? toloc_dst(secs) : \
251 ((secs) + utc_offset_secs))))
258 if (gmtime_emulation_type == 0) {
260 time_t base = 15 * 86400; /* 15jan71; to avoid month/year ends between */
261 /* results of calls to gmtime() and localtime() */
264 gmtime_emulation_type++;
265 if ((tm_p = gmtime(&base)) == NULL) { /* CRTL gmtime() is a fake */
266 char off[LNM$C_NAMLENGTH+1];;
268 gmtime_emulation_type++;
269 if (!Perl_vmstrnenv("SYS$TIMEZONE_DIFFERENTIAL",off,0,fildev,0)) {
270 gmtime_emulation_type++;
272 Perl_warn(aTHX_ "no UTC offset information; assuming local time is UTC");
274 else { utc_offset_secs = atol(off); }
276 else { /* We've got a working gmtime() */
277 struct tm gmt, local;
280 tm_p = localtime(&base);
282 utc_offset_secs = (local.tm_mday - gmt.tm_mday) * 86400;
283 utc_offset_secs += (local.tm_hour - gmt.tm_hour) * 3600;
284 utc_offset_secs += (local.tm_min - gmt.tm_min) * 60;
285 utc_offset_secs += (local.tm_sec - gmt.tm_sec);
293 gettimeofday (struct timeval *tp, void *tpz)
299 long div_100ns_to_secs;
300 long div_100ns_to_usecs;
308 In case of error, tv_usec = 0 and tv_sec = VMS condition code.
309 The return from function is also set to -1.
310 This is not exactly as per the manual page.
316 if (base_adjust[0]==0 && base_adjust[1]==0) {
318 if (base_adjust==0) { /* Need to determine epoch adjustment */
320 ret=sys$bintim(&dscepoch,&base_adjust);
321 if (1 != (ret &&1)) {
327 ret=sys$gettim(&quad); /* Get VMS system time */
328 if ((1 && ret) == 1) {
330 quad[0] -= base_adjust[0]; /* convert to epoch offset */
331 quad[1] -= base_adjust[1]; /* convert 2nd half of quadword */
332 div_100ns_to_secs = DIV_100NS_TO_SECS;
333 div_100ns_to_usecs = DIV_100NS_TO_USECS;
334 lib$ediv(&div_100ns_to_secs,&quad,&quo,&rem);
337 lib$ediv(&div_100ns_to_usecs,&quad1,&quo1,&rem1);
338 tp->tv_sec = quo; /* Whole seconds */
339 tp->tv_usec = quo1; /* Micro-seconds */
341 quad -= base_adjust; /* convert to epoch offset */
342 ans1=qdiv(quad,DIV_100NS_TO_SECS);
343 ans2=qdiv(ans1.rem,DIV_100NS_TO_USECS);
344 tp->tv_sec = ans1.quot; /* Whole seconds */
345 tp->tv_usec = ans2.quot; /* Micro-seconds */
353 if (VMSISH_TIME) tp->tv_sec = _toloc(tp->tv_sec);
355 if (!VMSISH_TIME) tp->tv_sec = _toutc(tp->tv_sec);
363 /* Do not use H A S _ N A N O S L E E P
364 * so that Perl Configure doesn't scan for it.
365 * (We are part of the core perl now.)
366 * The TIME_HIRES_NANOSLEEP is set by Makefile.PL. */
367 #if !defined(HAS_USLEEP) && defined(TIME_HIRES_NANOSLEEP)
369 #define usleep hrt_nanosleep /* could conflict with ncurses for static build */
372 hrt_nanosleep(unsigned long usec) /* This is used to emulate usleep. */
375 res.tv_sec = usec/1000/1000;
376 res.tv_nsec = ( usec - res.tv_sec*1000*1000 ) * 1000;
377 nanosleep(&res, NULL);
380 #endif /* #if !defined(HAS_USLEEP) && defined(TIME_HIRES_NANOSLEEP) */
382 #if !defined(HAS_USLEEP) && defined(HAS_SELECT)
383 #ifndef SELECT_IS_BROKEN
385 #define usleep hrt_usleep /* could conflict with ncurses for static build */
388 hrt_usleep(unsigned long usec)
393 select(0, (Select_fd_set_t)NULL, (Select_fd_set_t)NULL,
394 (Select_fd_set_t)NULL, &tv);
397 #endif /* #if !defined(HAS_USLEEP) && defined(HAS_SELECT) */
399 #if !defined(HAS_USLEEP) && defined(WIN32)
401 #define usleep hrt_usleep /* could conflict with ncurses for static build */
404 hrt_usleep(unsigned long usec)
410 #endif /* #if !defined(HAS_USLEEP) && defined(WIN32) */
412 #if !defined(HAS_USLEEP) && defined(HAS_NANOSLEEP)
414 #define usleep hrt_usleep /* could conflict with ncurses for static build */
417 hrt_usleep(unsigned long usec)
420 tsa.tv_sec = usec * 1000; /* Ignoring wraparound. */
422 nanosleep(&tsa, NULL);
425 #endif /* #if !defined(HAS_USLEEP) && defined(HAS_POLL) */
427 #if !defined(HAS_USLEEP) && defined(HAS_POLL)
429 #define usleep hrt_usleep /* could conflict with ncurses for static build */
432 hrt_usleep(unsigned long usec)
434 int msec = usec / 1000;
438 #endif /* #if !defined(HAS_USLEEP) && defined(HAS_POLL) */
440 #if !defined(HAS_UALARM) && defined(HAS_SETITIMER)
442 #define ualarm hrt_ualarm /* could conflict with ncurses for static build */
445 hrt_ualarm(int usec, int interval)
447 struct itimerval itv;
448 itv.it_value.tv_sec = usec / 1000000;
449 itv.it_value.tv_usec = usec % 1000000;
450 itv.it_interval.tv_sec = interval / 1000000;
451 itv.it_interval.tv_usec = interval % 1000000;
452 return setitimer(ITIMER_REAL, &itv, 0);
454 #endif /* #if !defined(HAS_UALARM) && defined(HAS_SETITIMER) */
456 #if !defined(HAS_UALARM) && defined(VMS)
458 #define ualarm vms_ualarm
460 #include <lib$routines.h>
468 #define VMSERR(s) (!((s)&1))
471 us_to_VMS(useconds_t mseconds, unsigned long v[])
480 iss = lib$addx(qq,qq,qq);
481 if (VMSERR(iss)) lib$signal(iss);
482 iss = lib$subx(v,qq,v);
483 if (VMSERR(iss)) lib$signal(iss);
484 iss = lib$addx(qq,qq,qq);
485 if (VMSERR(iss)) lib$signal(iss);
486 iss = lib$subx(v,qq,v);
487 if (VMSERR(iss)) lib$signal(iss);
488 iss = lib$subx(v,qq,v);
489 if (VMSERR(iss)) lib$signal(iss);
493 VMS_to_us(unsigned long v[])
496 unsigned long div=10,quot, rem;
498 iss = lib$ediv(&div,v,",&rem);
499 if (VMSERR(iss)) lib$signal(iss);
504 typedef unsigned short word;
505 typedef struct _ualarm {
508 unsigned long delay[2];
509 unsigned long interval[2];
510 unsigned long remain[2];
515 static Alarm *a0, alarm_base;
520 static void ualarm_AST(Alarm *a);
523 vms_ualarm(int mseconds, int interval)
532 static struct item_list3 itmlst[2];
533 static int first = 1;
539 itmlst[0].code = JPI$_ASTEN;
540 itmlst[0].length = sizeof(asten);
541 itmlst[0].retlenaddr = NULL;
543 itmlst[1].length = 0;
544 itmlst[1].bufaddr = NULL;
545 itmlst[1].retlenaddr = NULL;
547 iss = lib$get_ef(&alarm_ef);
548 if (VMSERR(iss)) lib$signal(iss);
551 a0->function = UAL_NULL;
553 itmlst[0].bufaddr = &asten;
555 iss = sys$getjpiw(0,0,0,itmlst,0,0,0);
556 if (VMSERR(iss)) lib$signal(iss);
557 if (!(asten&0x08)) return -1;
561 a->function = UAL_SET;
563 a->function = UAL_CLEAR;
566 us_to_VMS(mseconds, a->delay);
568 us_to_VMS(interval, a->interval);
573 iss = sys$clref(alarm_ef);
574 if (VMSERR(iss)) lib$signal(iss);
576 iss = sys$dclast(ualarm_AST,a,0);
577 if (VMSERR(iss)) lib$signal(iss);
579 iss = sys$waitfr(alarm_ef);
580 if (VMSERR(iss)) lib$signal(iss);
582 if (a->function == UAL_ACTIVE)
583 return VMS_to_us(a->remain);
594 unsigned long now[2];
596 iss = sys$gettim(now);
597 if (VMSERR(iss)) lib$signal(iss);
599 if (a->function == UAL_SET || a->function == UAL_CLEAR) {
600 if (a0->function == UAL_ACTIVE) {
601 iss = sys$cantim(a0,PSL$C_USER);
602 if (VMSERR(iss)) lib$signal(iss);
604 iss = lib$subx(a0->remain, now, a->remain);
605 if (VMSERR(iss)) lib$signal(iss);
607 if (a->remain[1] & 0x80000000)
608 a->remain[0] = a->remain[1] = 0;
611 if (a->function == UAL_SET) {
612 a->function = a0->function;
613 a0->function = UAL_ACTIVE;
614 a0->repeat = a->repeat;
616 a0->interval[0] = a->interval[0];
617 a0->interval[1] = a->interval[1];
619 a0->delay[0] = a->delay[0];
620 a0->delay[1] = a->delay[1];
622 iss = lib$subx(now, a0->delay, a0->remain);
623 if (VMSERR(iss)) lib$signal(iss);
625 iss = sys$setimr(0,a0->delay,ualarm_AST,a0);
626 if (VMSERR(iss)) lib$signal(iss);
628 a->function = a0->function;
629 a0->function = UAL_NULL;
631 iss = sys$setef(alarm_ef);
632 if (VMSERR(iss)) lib$signal(iss);
633 } else if (a->function == UAL_ACTIVE) {
635 iss = lib$subx(now, a->interval, a->remain);
636 if (VMSERR(iss)) lib$signal(iss);
638 iss = sys$setimr(0,a->interval,ualarm_AST,a);
639 if (VMSERR(iss)) lib$signal(iss);
641 a->function = UAL_NULL;
644 if (VMSERR(iss)) lib$signal(iss);
645 lib$signal(SS$_ASTFLT);
647 lib$signal(SS$_BADPARAM);
651 #endif /* #if !defined(HAS_UALARM) && defined(VMS) */
653 #ifdef HAS_GETTIMEOFDAY
656 myU2time(pTHX_ UV *ret)
660 status = gettimeofday (&Tp, NULL);
674 status = gettimeofday (&Tp, NULL);
675 return status == 0 ? Tp.tv_sec + (Tp.tv_usec / 1000000.) : -1.0;
678 #endif /* #ifdef HAS_GETTIMEOFDAY */
680 MODULE = Time::HiRes PACKAGE = Time::HiRes
689 #ifdef ATLEASTFIVEOHOHFIVE
690 #ifdef HAS_GETTIMEOFDAY
692 hv_store(PL_modglobal, "Time::NVtime", 12, newSViv(PTR2IV(myNVtime)), 0);
693 hv_store(PL_modglobal, "Time::U2time", 12, newSViv(PTR2IV(myU2time)), 0);
699 #if defined(USE_ITHREADS) && defined(MY_CXT_KEY)
708 INCLUDE: const-xs.inc
710 #if defined(HAS_USLEEP) && defined(HAS_GETTIMEOFDAY)
716 struct timeval Ta, Tb;
718 gettimeofday(&Ta, NULL);
720 if (useconds > 1E6) {
721 IV seconds = (IV) (useconds / 1E6);
722 /* If usleep() has been implemented using setitimer()
723 * then this contortion is unnecessary-- but usleep()
724 * may be implemented in some other way, so let's contort. */
727 useconds -= 1E6 * seconds;
729 } else if (useconds < 0.0)
730 croak("Time::HiRes::usleep(%"NVgf"): negative time not invented yet", useconds);
731 usleep((U32)useconds);
734 gettimeofday(&Tb, NULL);
736 printf("[%ld %ld] [%ld %ld]\n", Tb.tv_sec, Tb.tv_usec, Ta.tv_sec, Ta.tv_usec);
738 RETVAL = 1E6*(Tb.tv_sec-Ta.tv_sec)+(NV)((IV)Tb.tv_usec-(IV)Ta.tv_usec);
743 #if defined(TIME_HIRES_NANOSLEEP)
749 struct timeval Ta, Tb;
751 gettimeofday(&Ta, NULL);
754 if (nseconds > 1E9) {
755 IV seconds = (IV) (nseconds / 1E9);
758 nseconds -= 1E9 * seconds;
760 } else if (nseconds < 0.0)
761 croak("Time::HiRes::nanosleep(%"NVgf"): negative time not invented yet", nseconds);
762 tsa.tv_sec = (IV) (nseconds / 1E9);
763 tsa.tv_nsec = (IV) nseconds - tsa.tv_sec * 1E9;
764 nanosleep(&tsa, NULL);
767 gettimeofday(&Tb, NULL);
768 RETVAL = 1E3*(1E6*(Tb.tv_sec-Ta.tv_sec)+(NV)((IV)Tb.tv_usec-(IV)Ta.tv_usec));
773 #else /* #if defined(TIME_HIRES_NANOSLEEP) */
779 croak("Time::HiRes::nanosleep(): unimplemented in this platform");
782 #endif /* #if defined(TIME_HIRES_NANOSLEEP) */
787 struct timeval Ta, Tb;
789 gettimeofday(&Ta, NULL);
791 NV seconds = SvNV(ST(0));
792 if (seconds >= 0.0) {
793 UV useconds = (UV)(1E6 * (seconds - (UV)seconds));
796 if ((IV)useconds < 0) {
797 #if defined(__sparc64__) && defined(__GNUC__)
798 /* Sparc64 gcc 2.95.3 (e.g. on NetBSD) has a bug
799 * where (0.5 - (UV)(0.5)) will under certain
800 * circumstances (if the double is cast to UV more
801 * than once?) evaluate to -0.5, instead of 0.5. */
802 useconds = -(IV)useconds;
803 #endif /* #if defined(__sparc64__) && defined(__GNUC__) */
804 if ((IV)useconds < 0)
805 croak("Time::HiRes::sleep(%"NVgf"): internal error: useconds < 0 (unsigned %"UVuf" signed %"IVdf")", seconds, useconds, (IV)useconds);
809 croak("Time::HiRes::sleep(%"NVgf"): negative time not invented yet", seconds);
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 = (NV)(Tb.tv_sec-Ta.tv_sec)+0.000001*(NV)(Tb.tv_usec-Ta.tv_usec);
821 #else /* #if defined(HAS_USLEEP) && defined(HAS_GETTIMEOFDAY) */
827 croak("Time::HiRes::usleep(): unimplemented in this platform");
830 #endif /* #if defined(HAS_USLEEP) && defined(HAS_GETTIMEOFDAY) */
835 ualarm(useconds,interval=0)
839 if (useconds < 0 || interval < 0)
840 croak("Time::HiRes::ualarm(%d, %d): negative time not invented yet", useconds, interval);
841 RETVAL = ualarm(useconds, interval);
847 alarm(seconds,interval=0)
851 if (seconds < 0.0 || interval < 0.0)
852 croak("Time::HiRes::alarm(%"NVgf", %"NVgf"): negative time not invented yet", seconds, interval);
853 RETVAL = (NV)ualarm(seconds * 1000000,
854 interval * 1000000) / 1E6;
862 ualarm(useconds,interval=0)
866 croak("Time::HiRes::ualarm(): unimplemented in this platform");
870 alarm(seconds,interval=0)
874 croak("Time::HiRes::alarm(): unimplemented in this platform");
877 #endif /* #ifdef HAS_UALARM */
879 #ifdef HAS_GETTIMEOFDAY
880 # ifdef MACOS_TRADITIONAL /* fix epoch TZ and use unsigned time_t */
888 status = gettimeofday (&Tp, &Tz);
891 Tp.tv_sec += Tz.tz_minuteswest * 60; /* adjust for TZ */
892 if (GIMME == G_ARRAY) {
894 /* Mac OS (Classic) has unsigned time_t */
895 PUSHs(sv_2mortal(newSVuv(Tp.tv_sec)));
896 PUSHs(sv_2mortal(newSViv(Tp.tv_usec)));
899 PUSHs(sv_2mortal(newSVnv(Tp.tv_sec + (Tp.tv_usec / 1000000.0))));
910 status = gettimeofday (&Tp, &Tz);
912 Tp.tv_sec += Tz.tz_minuteswest * 60; /* adjust for TZ */
913 RETVAL = Tp.tv_sec + (Tp.tv_usec / 1000000.0);
920 # else /* MACOS_TRADITIONAL */
927 status = gettimeofday (&Tp, NULL);
929 if (GIMME == G_ARRAY) {
931 PUSHs(sv_2mortal(newSViv(Tp.tv_sec)));
932 PUSHs(sv_2mortal(newSViv(Tp.tv_usec)));
935 PUSHs(sv_2mortal(newSVnv(Tp.tv_sec + (Tp.tv_usec / 1000000.0))));
945 status = gettimeofday (&Tp, NULL);
947 RETVAL = Tp.tv_sec + (Tp.tv_usec / 1000000.);
954 # endif /* MACOS_TRADITIONAL */
955 #endif /* #ifdef HAS_GETTIMEOFDAY */
957 #if defined(HAS_GETITIMER) && defined(HAS_SETITIMER)
959 #define TV2NV(tv) ((NV)((tv).tv_sec) + 0.000001 * (NV)((tv).tv_usec))
962 setitimer(which, seconds, interval = 0)
967 struct itimerval newit;
968 struct itimerval oldit;
970 if (seconds < 0.0 || interval < 0.0)
971 croak("Time::HiRes::setitimer(%"IVdf", %"NVgf", %"NVgf"): negative time not invented yet", (IV)which, seconds, interval);
972 newit.it_value.tv_sec = seconds;
973 newit.it_value.tv_usec =
974 (seconds - (NV)newit.it_value.tv_sec) * 1000000.0;
975 newit.it_interval.tv_sec = interval;
976 newit.it_interval.tv_usec =
977 (interval - (NV)newit.it_interval.tv_sec) * 1000000.0;
978 if (setitimer(which, &newit, &oldit) == 0) {
980 PUSHs(sv_2mortal(newSVnv(TV2NV(oldit.it_value))));
981 if (GIMME == G_ARRAY) {
983 PUSHs(sv_2mortal(newSVnv(TV2NV(oldit.it_interval))));
991 struct itimerval nowit;
993 if (getitimer(which, &nowit) == 0) {
995 PUSHs(sv_2mortal(newSVnv(TV2NV(nowit.it_value))));
996 if (GIMME == G_ARRAY) {
998 PUSHs(sv_2mortal(newSVnv(TV2NV(nowit.it_interval))));
1002 #endif /* #if defined(HAS_GETITIMER) && defined(HAS_SETITIMER) */
1004 #if defined(TIME_HIRES_CLOCK_GETTIME)
1007 clock_gettime(clock_id = CLOCK_REALTIME)
1013 #ifdef TIME_HIRES_CLOCK_GETTIME_SYSCALL
1014 status = syscall(SYS_clock_gettime, clock_id, &ts);
1016 status = clock_gettime(clock_id, &ts);
1018 RETVAL = status == 0 ? ts.tv_sec + (NV) ts.tv_nsec / (NV) 1e9 : -1;
1023 #else /* if defined(TIME_HIRES_CLOCK_GETTIME) */
1026 clock_gettime(clock_id = 0)
1029 croak("Time::HiRes::clock_gettime(): unimplemented in this platform");
1032 #endif /* #if defined(TIME_HIRES_CLOCK_GETTIME) */
1034 #if defined(TIME_HIRES_CLOCK_GETRES)
1037 clock_getres(clock_id = CLOCK_REALTIME)
1043 #ifdef TIME_HIRES_CLOCK_GETRES_SYSCALL
1044 status = syscall(SYS_clock_getres, clock_id, &ts);
1046 status = clock_getres(clock_id, &ts);
1048 RETVAL = status == 0 ? ts.tv_sec + (NV) ts.tv_nsec / (NV) 1e9 : -1;
1053 #else /* if defined(TIME_HIRES_CLOCK_GETRES) */
1056 clock_getres(clock_id = 0)
1059 croak("Time::HiRes::clock_getres(): unimplemented in this platform");
1062 #endif /* #if defined(TIME_HIRES_CLOCK_GETRES) */