4 #define PERL_NO_GET_CONTEXT
15 # include <sys/select.h>
26 # define dNOOP extern int Perl___notused
37 # define MY_CXT_CLONE \
39 my_cxt_t *my_cxtp = (my_cxt_t*)SvPVX(newSV(sizeof(my_cxt_t)-1));\
40 Copy(INT2PTR(my_cxt_t*, SvUV(my_cxt_sv)), my_cxtp, 1, my_cxt_t); \
41 sv_setuv(my_cxt_sv, PTR2UV(my_cxtp))
44 # define START_MY_CXT static my_cxt_t my_cxt;
45 # define dMY_CXT dNOOP
46 # define MY_CXT_INIT NOOP
47 # define MY_CXT_CLONE NOOP
48 # define MY_CXT my_cxt
52 # if defined(USE_LONG_DOUBLE) && defined(HAS_LONG_DOUBLE)
53 # define NVTYPE long double
55 # define NVTYPE double
62 # if IVSIZE == LONGSIZE
66 # if IVSIZE == INTSIZE
78 # if defined(USE_LONG_DOUBLE) && defined(HAS_LONG_DOUBLE) && \
79 defined(PERL_PRIgldbl) /* Not very likely, but let's try anyway. */
80 # define NVgf PERL_PRIgldbl
88 #if (IVSIZE == PTRSIZE) && (UVSIZE == PTRSIZE)
90 # define INT2PTR(any,d) (any)(d)
92 # if PTRSIZE == LONGSIZE
93 # define PTRV unsigned long
95 # define PTRV unsigned
97 # define INT2PTR(any,d) (any)(PTRV)(d)
99 #define PTR2IV(p) INT2PTR(IV,p)
101 #endif /* !INT2PTR */
105 sv_2pv_nolen(pTHX_ register SV *sv)
108 return sv_2pv(sv, &n_a);
110 # define SvPV_nolen(sv) \
111 ((SvFLAGS(sv) & (SVf_POK)) == SVf_POK \
112 ? SvPVX(sv) : sv_2pv_nolen(sv))
115 #ifndef PerlProc_pause
116 # define PerlProc_pause() Pause()
119 /* Though the cpp define ITIMER_VIRTUAL is available the functionality
120 * is not supported in Cygwin as of August 2002, ditto for Win32.
121 * Neither are ITIMER_PROF or ITIMER_REALPROF implemented. --jhi
123 #if defined(__CYGWIN__) || defined(WIN32)
124 # undef ITIMER_VIRTUAL
126 # undef ITIMER_REALPROF
129 /* 5.004 doesn't define PL_sv_undef */
130 #ifndef ATLEASTFIVEOHOHFIVE
132 #define PL_sv_undef sv_undef
136 #include "const-c.inc"
140 #ifndef HAS_GETTIMEOFDAY
141 # define HAS_GETTIMEOFDAY
144 /* shows up in winsock.h?
152 unsigned __int64 ft_i64;
156 #define MY_CXT_KEY "Time::HiRes_" XS_VERSION
159 unsigned long run_count;
160 unsigned __int64 base_ticks;
161 unsigned __int64 tick_frequency;
162 FT_t base_systime_as_filetime;
167 /* Number of 100 nanosecond units from 1/1/1601 to 1/1/1970 */
169 #define Const64(x) x##LL
171 #define Const64(x) x##i64
173 #define EPOCH_BIAS Const64(116444736000000000)
175 /* NOTE: This does not compute the timezone info (doing so can be expensive,
176 * and appears to be unsupported even by glibc) */
178 /* dMY_CXT needs a Perl context and we don't want to call PERL_GET_CONTEXT
179 for performance reasons */
182 #define gettimeofday(tp, not_used) _gettimeofday(aTHX_ tp, not_used)
184 /* If the performance counter delta drifts more than 2 seconds from the
185 * system time then we recalibrate to system time. This means we may
186 * move *backwards* in time! */
188 #define MAX_DIFF Const64(20000000)
191 _gettimeofday(pTHX_ struct timeval *tp, void *not_used)
195 unsigned __int64 ticks;
198 if (MY_CXT.run_count++) {
201 GetSystemTimeAsFileTime(&filtim.ft_val);
202 QueryPerformanceCounter((LARGE_INTEGER*)&ticks);
203 ticks -= MY_CXT.base_ticks;
204 ft.ft_i64 = MY_CXT.base_systime_as_filetime.ft_i64
205 + Const64(10000000) * (ticks / MY_CXT.tick_frequency)
206 +(Const64(10000000) * (ticks % MY_CXT.tick_frequency)) / MY_CXT.tick_frequency;
207 diff = ft.ft_i64 - MY_CXT.base_systime_as_filetime.ft_i64;
208 if (diff < -MAX_DIFF || diff > MAX_DIFF) {
209 MY_CXT.base_ticks = ticks;
210 ft.ft_i64 = filtim.ft_i64;
214 QueryPerformanceFrequency((LARGE_INTEGER*)&MY_CXT.tick_frequency);
215 QueryPerformanceCounter((LARGE_INTEGER*)&MY_CXT.base_ticks);
216 GetSystemTimeAsFileTime(&MY_CXT.base_systime_as_filetime.ft_val);
217 ft.ft_i64 = MY_CXT.base_systime_as_filetime.ft_i64;
220 /* seconds since epoch */
221 tp->tv_sec = (long)((ft.ft_i64 - EPOCH_BIAS) / Const64(10000000));
223 /* microseconds remaining */
224 tp->tv_usec = (long)((ft.ft_i64 / Const64(10)) % Const64(1000000));
230 #if defined(WIN32) && !defined(ATLEASTFIVEOHOHFIVE)
232 sleep(unsigned int t)
239 #if !defined(HAS_GETTIMEOFDAY) && defined(VMS)
240 #define HAS_GETTIMEOFDAY
243 #include <time.h> /* gettimeofday */
244 #include <stdlib.h> /* qdiv */
245 #include <starlet.h> /* sys$gettim */
248 #include <lib$routines.h> /* lib$ediv() */
252 VMS binary time is expressed in 100 nano-seconds since
253 system base time which is 17-NOV-1858 00:00:00.00
256 #define DIV_100NS_TO_SECS 10000000L
257 #define DIV_100NS_TO_USECS 10L
260 gettimeofday is supposed to return times since the epoch
261 so need to determine this in terms of VMS base time
263 static $DESCRIPTOR(dscepoch,"01-JAN-1970 00:00:00.00");
266 static long base_adjust[2]={0L,0L};
268 static __int64 base_adjust=0;
273 If we don't have gettimeofday, then likely we are on a VMS machine that
274 operates on local time rather than UTC...so we have to zone-adjust.
275 This code gleefully swiped from VMS.C
278 /* method used to handle UTC conversions:
279 * 1 == CRTL gmtime(); 2 == SYS$TIMEZONE_DIFFERENTIAL; 3 == no correction
281 static int gmtime_emulation_type;
282 /* number of secs to add to UTC POSIX-style time to get local time */
283 static long int utc_offset_secs;
284 static struct dsc$descriptor_s fildevdsc =
285 { 12, DSC$K_DTYPE_T, DSC$K_CLASS_S, "LNM$FILE_DEV" };
286 static struct dsc$descriptor_s *fildev[] = { &fildevdsc, NULL };
288 static time_t toutc_dst(time_t loc) {
291 if ((rsltmp = localtime(&loc)) == NULL) return -1;
292 loc -= utc_offset_secs;
293 if (rsltmp->tm_isdst) loc -= 3600;
297 static time_t toloc_dst(time_t utc) {
300 utc += utc_offset_secs;
301 if ((rsltmp = localtime(&utc)) == NULL) return -1;
302 if (rsltmp->tm_isdst) utc += 3600;
306 #define _toutc(secs) ((secs) == (time_t) -1 ? (time_t) -1 : \
307 ((gmtime_emulation_type || timezone_setup()), \
308 (gmtime_emulation_type == 1 ? toutc_dst(secs) : \
309 ((secs) - utc_offset_secs))))
311 #define _toloc(secs) ((secs) == (time_t) -1 ? (time_t) -1 : \
312 ((gmtime_emulation_type || timezone_setup()), \
313 (gmtime_emulation_type == 1 ? toloc_dst(secs) : \
314 ((secs) + utc_offset_secs))))
321 if (gmtime_emulation_type == 0) {
323 time_t base = 15 * 86400; /* 15jan71; to avoid month/year ends between */
324 /* results of calls to gmtime() and localtime() */
327 gmtime_emulation_type++;
328 if ((tm_p = gmtime(&base)) == NULL) { /* CRTL gmtime() is a fake */
329 char off[LNM$C_NAMLENGTH+1];;
331 gmtime_emulation_type++;
332 if (!Perl_vmstrnenv("SYS$TIMEZONE_DIFFERENTIAL",off,0,fildev,0)) {
333 gmtime_emulation_type++;
335 Perl_warn(aTHX_ "no UTC offset information; assuming local time is UTC");
337 else { utc_offset_secs = atol(off); }
339 else { /* We've got a working gmtime() */
340 struct tm gmt, local;
343 tm_p = localtime(&base);
345 utc_offset_secs = (local.tm_mday - gmt.tm_mday) * 86400;
346 utc_offset_secs += (local.tm_hour - gmt.tm_hour) * 3600;
347 utc_offset_secs += (local.tm_min - gmt.tm_min) * 60;
348 utc_offset_secs += (local.tm_sec - gmt.tm_sec);
356 gettimeofday (struct timeval *tp, void *tpz)
362 long div_100ns_to_secs;
363 long div_100ns_to_usecs;
371 In case of error, tv_usec = 0 and tv_sec = VMS condition code.
372 The return from function is also set to -1.
373 This is not exactly as per the manual page.
379 if (base_adjust[0]==0 && base_adjust[1]==0) {
381 if (base_adjust==0) { /* Need to determine epoch adjustment */
383 ret=sys$bintim(&dscepoch,&base_adjust);
384 if (1 != (ret &&1)) {
390 ret=sys$gettim(&quad); /* Get VMS system time */
391 if ((1 && ret) == 1) {
393 quad[0] -= base_adjust[0]; /* convert to epoch offset */
394 quad[1] -= base_adjust[1]; /* convert 2nd half of quadword */
395 div_100ns_to_secs = DIV_100NS_TO_SECS;
396 div_100ns_to_usecs = DIV_100NS_TO_USECS;
397 lib$ediv(&div_100ns_to_secs,&quad,&quo,&rem);
400 lib$ediv(&div_100ns_to_usecs,&quad1,&quo1,&rem1);
401 tp->tv_sec = quo; /* Whole seconds */
402 tp->tv_usec = quo1; /* Micro-seconds */
404 quad -= base_adjust; /* convert to epoch offset */
405 ans1=qdiv(quad,DIV_100NS_TO_SECS);
406 ans2=qdiv(ans1.rem,DIV_100NS_TO_USECS);
407 tp->tv_sec = ans1.quot; /* Whole seconds */
408 tp->tv_usec = ans2.quot; /* Micro-seconds */
416 if (VMSISH_TIME) tp->tv_sec = _toloc(tp->tv_sec);
418 if (!VMSISH_TIME) tp->tv_sec = _toutc(tp->tv_sec);
426 /* Do not use H A S _ N A N O S L E E P
427 * so that Perl Configure doesn't scan for it.
428 * The TIME_HIRES_NANOSLEEP is set by Makefile.PL. */
429 #if !defined(HAS_USLEEP) && defined(TIME_HIRES_NANOSLEEP)
431 #define usleep hrt_nanosleep /* could conflict with ncurses for static build */
434 hrt_nanosleep(unsigned long usec)
437 res.tv_sec = usec/1000/1000;
438 res.tv_nsec = ( usec - res.tv_sec*1000*1000 ) * 1000;
439 nanosleep(&res, NULL);
444 #if !defined(HAS_USLEEP) && defined(HAS_SELECT)
445 #ifndef SELECT_IS_BROKEN
447 #define usleep hrt_usleep /* could conflict with ncurses for static build */
450 hrt_usleep(unsigned long usec)
455 select(0, (Select_fd_set_t)NULL, (Select_fd_set_t)NULL,
456 (Select_fd_set_t)NULL, &tv);
461 #if !defined(HAS_USLEEP) && defined(WIN32)
463 #define usleep hrt_usleep /* could conflict with ncurses for static build */
466 hrt_usleep(unsigned long usec)
475 #if !defined(HAS_UALARM) && defined(HAS_SETITIMER)
477 #define ualarm hrt_ualarm /* could conflict with ncurses for static build */
480 hrt_ualarm(int usec, int interval)
482 struct itimerval itv;
483 itv.it_value.tv_sec = usec / 1000000;
484 itv.it_value.tv_usec = usec % 1000000;
485 itv.it_interval.tv_sec = interval / 1000000;
486 itv.it_interval.tv_usec = interval % 1000000;
487 return setitimer(ITIMER_REAL, &itv, 0);
491 #if !defined(HAS_UALARM) && defined(VMS)
493 #define ualarm vms_ualarm
495 #include <lib$routines.h>
503 #define VMSERR(s) (!((s)&1))
506 us_to_VMS(useconds_t mseconds, unsigned long v[])
515 iss = lib$addx(qq,qq,qq);
516 if (VMSERR(iss)) lib$signal(iss);
517 iss = lib$subx(v,qq,v);
518 if (VMSERR(iss)) lib$signal(iss);
519 iss = lib$addx(qq,qq,qq);
520 if (VMSERR(iss)) lib$signal(iss);
521 iss = lib$subx(v,qq,v);
522 if (VMSERR(iss)) lib$signal(iss);
523 iss = lib$subx(v,qq,v);
524 if (VMSERR(iss)) lib$signal(iss);
528 VMS_to_us(unsigned long v[])
531 unsigned long div=10,quot, rem;
533 iss = lib$ediv(&div,v,",&rem);
534 if (VMSERR(iss)) lib$signal(iss);
539 typedef unsigned short word;
540 typedef struct _ualarm {
543 unsigned long delay[2];
544 unsigned long interval[2];
545 unsigned long remain[2];
550 static Alarm *a0, alarm_base;
555 static void ualarm_AST(Alarm *a);
558 vms_ualarm(int mseconds, int interval)
567 static struct item_list3 itmlst[2];
568 static int first = 1;
574 itmlst[0].code = JPI$_ASTEN;
575 itmlst[0].length = sizeof(asten);
576 itmlst[0].retlenaddr = NULL;
578 itmlst[1].length = 0;
579 itmlst[1].bufaddr = NULL;
580 itmlst[1].retlenaddr = NULL;
582 iss = lib$get_ef(&alarm_ef);
583 if (VMSERR(iss)) lib$signal(iss);
586 a0->function = UAL_NULL;
588 itmlst[0].bufaddr = &asten;
590 iss = sys$getjpiw(0,0,0,itmlst,0,0,0);
591 if (VMSERR(iss)) lib$signal(iss);
592 if (!(asten&0x08)) return -1;
596 a->function = UAL_SET;
598 a->function = UAL_CLEAR;
601 us_to_VMS(mseconds, a->delay);
603 us_to_VMS(interval, a->interval);
608 iss = sys$clref(alarm_ef);
609 if (VMSERR(iss)) lib$signal(iss);
611 iss = sys$dclast(ualarm_AST,a,0);
612 if (VMSERR(iss)) lib$signal(iss);
614 iss = sys$waitfr(alarm_ef);
615 if (VMSERR(iss)) lib$signal(iss);
617 if (a->function == UAL_ACTIVE)
618 return VMS_to_us(a->remain);
629 unsigned long now[2];
631 iss = sys$gettim(now);
632 if (VMSERR(iss)) lib$signal(iss);
634 if (a->function == UAL_SET || a->function == UAL_CLEAR) {
635 if (a0->function == UAL_ACTIVE) {
636 iss = sys$cantim(a0,PSL$C_USER);
637 if (VMSERR(iss)) lib$signal(iss);
639 iss = lib$subx(a0->remain, now, a->remain);
640 if (VMSERR(iss)) lib$signal(iss);
642 if (a->remain[1] & 0x80000000)
643 a->remain[0] = a->remain[1] = 0;
646 if (a->function == UAL_SET) {
647 a->function = a0->function;
648 a0->function = UAL_ACTIVE;
649 a0->repeat = a->repeat;
651 a0->interval[0] = a->interval[0];
652 a0->interval[1] = a->interval[1];
654 a0->delay[0] = a->delay[0];
655 a0->delay[1] = a->delay[1];
657 iss = lib$subx(now, a0->delay, a0->remain);
658 if (VMSERR(iss)) lib$signal(iss);
660 iss = sys$setimr(0,a0->delay,ualarm_AST,a0);
661 if (VMSERR(iss)) lib$signal(iss);
663 a->function = a0->function;
664 a0->function = UAL_NULL;
666 iss = sys$setef(alarm_ef);
667 if (VMSERR(iss)) lib$signal(iss);
668 } else if (a->function == UAL_ACTIVE) {
670 iss = lib$subx(now, a->interval, a->remain);
671 if (VMSERR(iss)) lib$signal(iss);
673 iss = sys$setimr(0,a->interval,ualarm_AST,a);
674 if (VMSERR(iss)) lib$signal(iss);
676 a->function = UAL_NULL;
679 if (VMSERR(iss)) lib$signal(iss);
680 lib$signal(SS$_ASTFLT);
682 lib$signal(SS$_BADPARAM);
686 #endif /* !HAS_UALARM && VMS */
688 #ifdef HAS_GETTIMEOFDAY
691 myU2time(pTHX_ UV *ret)
695 status = gettimeofday (&Tp, NULL);
709 status = gettimeofday (&Tp, NULL);
710 return status == 0 ? Tp.tv_sec + (Tp.tv_usec / 1000000.) : -1.0;
715 MODULE = Time::HiRes PACKAGE = Time::HiRes
724 #ifdef ATLEASTFIVEOHOHFIVE
725 #ifdef HAS_GETTIMEOFDAY
728 hv_store(PL_modglobal, "Time::NVtime", 12, newSViv(PTR2IV(myNVtime)), 0);
729 if (myU2time(aTHX_ auv) == 0)
730 hv_store(PL_modglobal, "Time::U2time", 12, newSViv((IV) auv[0]), 0);
736 #if defined(USE_ITHREADS) && defined(MY_CXT_KEY)
745 INCLUDE: const-xs.inc
747 #if defined(HAS_USLEEP) && defined(HAS_GETTIMEOFDAY)
753 struct timeval Ta, Tb;
755 gettimeofday(&Ta, NULL);
757 if (useconds > 1E6) {
758 IV seconds = (IV) (useconds / 1E6);
759 /* If usleep() has been implemented using setitimer()
760 * then this contortion is unnecessary-- but usleep()
761 * may be implemented in some other way, so let's contort. */
764 useconds -= 1E6 * seconds;
766 } else if (useconds < 0.0)
767 croak("Time::HiRes::usleep(%"NVgf"): negative time not invented yet", useconds);
768 usleep((U32)useconds);
771 gettimeofday(&Tb, NULL);
773 printf("[%ld %ld] [%ld %ld]\n", Tb.tv_sec, Tb.tv_usec, Ta.tv_sec, Ta.tv_usec);
775 RETVAL = 1E6*(Tb.tv_sec-Ta.tv_sec)+(NV)((IV)Tb.tv_usec-(IV)Ta.tv_usec);
783 struct timeval Ta, Tb;
785 gettimeofday(&Ta, NULL);
787 NV seconds = SvNV(ST(0));
788 if (seconds >= 0.0) {
789 UV useconds = (UV)(1E6 * (seconds - (UV)seconds));
792 if ((IV)useconds < 0) {
793 #if defined(__sparc64__) && defined(__GNUC__)
794 /* Sparc64 gcc 2.95.3 (e.g. on NetBSD) has a bug
795 * where (0.5 - (UV)(0.5)) will under certain
796 * circumstances (if the double is cast to UV more
797 * than once?) evaluate to -0.5, instead of 0.5. */
798 useconds = -(IV)useconds;
800 if ((IV)useconds < 0)
801 croak("Time::HiRes::sleep(%"NVgf"): internal error: useconds < 0 (unsigned %"UVuf" signed %"IVdf")", seconds, useconds, (IV)useconds);
805 croak("Time::HiRes::sleep(%"NVgf"): negative time not invented yet", seconds);
808 gettimeofday(&Tb, NULL);
810 printf("[%ld %ld] [%ld %ld]\n", Tb.tv_sec, Tb.tv_usec, Ta.tv_sec, Ta.tv_usec);
812 RETVAL = (NV)(Tb.tv_sec-Ta.tv_sec)+0.000001*(NV)(Tb.tv_usec-Ta.tv_usec);
822 ualarm(useconds,interval=0)
826 if (useconds < 0 || interval < 0)
827 croak("Time::HiRes::ualarm(%d, %d): negative time not invented yet", useconds, interval);
828 RETVAL = ualarm(useconds, interval);
834 alarm(seconds,interval=0)
838 if (seconds < 0.0 || interval < 0.0)
839 croak("Time::HiRes::alarm(%"NVgf", %"NVgf"): negative time not invented yet", seconds, interval);
840 RETVAL = (NV)ualarm(seconds * 1000000,
841 interval * 1000000) / 1E6;
848 #ifdef HAS_GETTIMEOFDAY
849 # ifdef MACOS_TRADITIONAL /* fix epoch TZ and use unsigned time_t */
857 status = gettimeofday (&Tp, &Tz);
858 Tp.tv_sec += Tz.tz_minuteswest * 60; /* adjust for TZ */
860 if (GIMME == G_ARRAY) {
862 /* Mac OS (Classic) has unsigned time_t */
863 PUSHs(sv_2mortal(newSVuv(Tp.tv_sec)));
864 PUSHs(sv_2mortal(newSViv(Tp.tv_usec)));
867 PUSHs(sv_2mortal(newSVnv(Tp.tv_sec + (Tp.tv_usec / 1000000.0))));
877 status = gettimeofday (&Tp, &Tz);
878 Tp.tv_sec += Tz.tz_minuteswest * 60; /* adjust for TZ */
879 RETVAL = Tp.tv_sec + (Tp.tv_usec / 1000000.0);
883 # else /* MACOS_TRADITIONAL */
890 status = gettimeofday (&Tp, NULL);
891 if (GIMME == G_ARRAY) {
893 PUSHs(sv_2mortal(newSViv(Tp.tv_sec)));
894 PUSHs(sv_2mortal(newSViv(Tp.tv_usec)));
897 PUSHs(sv_2mortal(newSVnv(Tp.tv_sec + (Tp.tv_usec / 1000000.0))));
906 status = gettimeofday (&Tp, NULL);
907 RETVAL = Tp.tv_sec + (Tp.tv_usec / 1000000.);
911 # endif /* MACOS_TRADITIONAL */
914 #if defined(HAS_GETITIMER) && defined(HAS_SETITIMER)
916 #define TV2NV(tv) ((NV)((tv).tv_sec) + 0.000001 * (NV)((tv).tv_usec))
919 setitimer(which, seconds, interval = 0)
924 struct itimerval newit;
925 struct itimerval oldit;
927 if (seconds < 0.0 || interval < 0.0)
928 croak("Time::HiRes::setitimer(%"IVdf", %"NVgf", %"NVgf"): negative time not invented yet", (IV)which, seconds, interval);
929 newit.it_value.tv_sec = seconds;
930 newit.it_value.tv_usec =
931 (seconds - (NV)newit.it_value.tv_sec) * 1000000.0;
932 newit.it_interval.tv_sec = interval;
933 newit.it_interval.tv_usec =
934 (interval - (NV)newit.it_interval.tv_sec) * 1000000.0;
935 if (setitimer(which, &newit, &oldit) == 0) {
937 PUSHs(sv_2mortal(newSVnv(TV2NV(oldit.it_value))));
938 if (GIMME == G_ARRAY) {
940 PUSHs(sv_2mortal(newSVnv(TV2NV(oldit.it_interval))));
948 struct itimerval nowit;
950 if (getitimer(which, &nowit) == 0) {
952 PUSHs(sv_2mortal(newSVnv(TV2NV(nowit.it_value))));
953 if (GIMME == G_ARRAY) {
955 PUSHs(sv_2mortal(newSVnv(TV2NV(nowit.it_interval))));