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)
185 _gettimeofday(pTHX_ struct timeval *tp, void *not_used)
189 unsigned __int64 ticks;
192 if (MY_CXT.run_count++) {
193 QueryPerformanceCounter((LARGE_INTEGER*)&ticks);
194 ticks -= MY_CXT.base_ticks;
195 ft.ft_i64 = MY_CXT.base_systime_as_filetime.ft_i64
196 + 10000000i64 * (ticks / MY_CXT.tick_frequency)
197 +(10000000i64 * (ticks % MY_CXT.tick_frequency)) / MY_CXT.tick_frequency;
200 QueryPerformanceFrequency((LARGE_INTEGER*)&MY_CXT.tick_frequency);
201 QueryPerformanceCounter((LARGE_INTEGER*)&MY_CXT.base_ticks);
202 GetSystemTimeAsFileTime(&MY_CXT.base_systime_as_filetime.ft_val);
203 ft.ft_i64 = MY_CXT.base_systime_as_filetime.ft_i64;
206 /* seconds since epoch */
207 tp->tv_sec = (long)((ft.ft_i64 - EPOCH_BIAS) / Const64(10000000));
209 /* microseconds remaining */
210 tp->tv_usec = (long)((ft.ft_i64 / Const64(10)) % Const64(1000000));
216 #if defined(WIN32) && !defined(ATLEASTFIVEOHOHFIVE)
218 sleep(unsigned int t)
225 #if !defined(HAS_GETTIMEOFDAY) && defined(VMS)
226 #define HAS_GETTIMEOFDAY
229 #include <time.h> /* gettimeofday */
230 #include <stdlib.h> /* qdiv */
231 #include <starlet.h> /* sys$gettim */
234 #include <lib$routines.h> /* lib$ediv() */
238 VMS binary time is expressed in 100 nano-seconds since
239 system base time which is 17-NOV-1858 00:00:00.00
242 #define DIV_100NS_TO_SECS 10000000L
243 #define DIV_100NS_TO_USECS 10L
246 gettimeofday is supposed to return times since the epoch
247 so need to determine this in terms of VMS base time
249 static $DESCRIPTOR(dscepoch,"01-JAN-1970 00:00:00.00");
252 static long base_adjust[2]={0L,0L};
254 static __int64 base_adjust=0;
259 If we don't have gettimeofday, then likely we are on a VMS machine that
260 operates on local time rather than UTC...so we have to zone-adjust.
261 This code gleefully swiped from VMS.C
264 /* method used to handle UTC conversions:
265 * 1 == CRTL gmtime(); 2 == SYS$TIMEZONE_DIFFERENTIAL; 3 == no correction
267 static int gmtime_emulation_type;
268 /* number of secs to add to UTC POSIX-style time to get local time */
269 static long int utc_offset_secs;
270 static struct dsc$descriptor_s fildevdsc =
271 { 12, DSC$K_DTYPE_T, DSC$K_CLASS_S, "LNM$FILE_DEV" };
272 static struct dsc$descriptor_s *fildev[] = { &fildevdsc, NULL };
274 static time_t toutc_dst(time_t loc) {
277 if ((rsltmp = localtime(&loc)) == NULL) return -1;
278 loc -= utc_offset_secs;
279 if (rsltmp->tm_isdst) loc -= 3600;
283 static time_t toloc_dst(time_t utc) {
286 utc += utc_offset_secs;
287 if ((rsltmp = localtime(&utc)) == NULL) return -1;
288 if (rsltmp->tm_isdst) utc += 3600;
292 #define _toutc(secs) ((secs) == (time_t) -1 ? (time_t) -1 : \
293 ((gmtime_emulation_type || timezone_setup()), \
294 (gmtime_emulation_type == 1 ? toutc_dst(secs) : \
295 ((secs) - utc_offset_secs))))
297 #define _toloc(secs) ((secs) == (time_t) -1 ? (time_t) -1 : \
298 ((gmtime_emulation_type || timezone_setup()), \
299 (gmtime_emulation_type == 1 ? toloc_dst(secs) : \
300 ((secs) + utc_offset_secs))))
307 if (gmtime_emulation_type == 0) {
309 time_t base = 15 * 86400; /* 15jan71; to avoid month/year ends between */
310 /* results of calls to gmtime() and localtime() */
313 gmtime_emulation_type++;
314 if ((tm_p = gmtime(&base)) == NULL) { /* CRTL gmtime() is a fake */
315 char off[LNM$C_NAMLENGTH+1];;
317 gmtime_emulation_type++;
318 if (!Perl_vmstrnenv("SYS$TIMEZONE_DIFFERENTIAL",off,0,fildev,0)) {
319 gmtime_emulation_type++;
321 Perl_warn(aTHX_ "no UTC offset information; assuming local time is UTC");
323 else { utc_offset_secs = atol(off); }
325 else { /* We've got a working gmtime() */
326 struct tm gmt, local;
329 tm_p = localtime(&base);
331 utc_offset_secs = (local.tm_mday - gmt.tm_mday) * 86400;
332 utc_offset_secs += (local.tm_hour - gmt.tm_hour) * 3600;
333 utc_offset_secs += (local.tm_min - gmt.tm_min) * 60;
334 utc_offset_secs += (local.tm_sec - gmt.tm_sec);
342 gettimeofday (struct timeval *tp, void *tpz)
348 long div_100ns_to_secs;
349 long div_100ns_to_usecs;
357 In case of error, tv_usec = 0 and tv_sec = VMS condition code.
358 The return from function is also set to -1.
359 This is not exactly as per the manual page.
365 if (base_adjust[0]==0 && base_adjust[1]==0) {
367 if (base_adjust==0) { /* Need to determine epoch adjustment */
369 ret=sys$bintim(&dscepoch,&base_adjust);
370 if (1 != (ret &&1)) {
376 ret=sys$gettim(&quad); /* Get VMS system time */
377 if ((1 && ret) == 1) {
379 quad[0] -= base_adjust[0]; /* convert to epoch offset */
380 quad[1] -= base_adjust[1]; /* convert 2nd half of quadword */
381 div_100ns_to_secs = DIV_100NS_TO_SECS;
382 div_100ns_to_usecs = DIV_100NS_TO_USECS;
383 lib$ediv(&div_100ns_to_secs,&quad,&quo,&rem);
386 lib$ediv(&div_100ns_to_usecs,&quad1,&quo1,&rem1);
387 tp->tv_sec = quo; /* Whole seconds */
388 tp->tv_usec = quo1; /* Micro-seconds */
390 quad -= base_adjust; /* convert to epoch offset */
391 ans1=qdiv(quad,DIV_100NS_TO_SECS);
392 ans2=qdiv(ans1.rem,DIV_100NS_TO_USECS);
393 tp->tv_sec = ans1.quot; /* Whole seconds */
394 tp->tv_usec = ans2.quot; /* Micro-seconds */
402 if (VMSISH_TIME) tp->tv_sec = _toloc(tp->tv_sec);
404 if (!VMSISH_TIME) tp->tv_sec = _toutc(tp->tv_sec);
412 /* Do not use H A S _ N A N O S L E E P
413 * so that Perl Configure doesn't scan for it.
414 * The TIME_HIRES_NANOSLEEP is set by Makefile.PL. */
415 #if !defined(HAS_USLEEP) && defined(TIME_HIRES_NANOSLEEP)
417 #define usleep hrt_nanosleep /* could conflict with ncurses for static build */
420 hrt_nanosleep(unsigned long usec)
423 res.tv_sec = usec/1000/1000;
424 res.tv_nsec = ( usec - res.tv_sec*1000*1000 ) * 1000;
425 nanosleep(&res, NULL);
430 #if !defined(HAS_USLEEP) && defined(HAS_SELECT)
431 #ifndef SELECT_IS_BROKEN
433 #define usleep hrt_usleep /* could conflict with ncurses for static build */
436 hrt_usleep(unsigned long usec)
441 select(0, (Select_fd_set_t)NULL, (Select_fd_set_t)NULL,
442 (Select_fd_set_t)NULL, &tv);
447 #if !defined(HAS_USLEEP) && defined(WIN32)
449 #define usleep hrt_usleep /* could conflict with ncurses for static build */
452 hrt_usleep(unsigned long usec)
461 #if !defined(HAS_UALARM) && defined(HAS_SETITIMER)
463 #define ualarm hrt_ualarm /* could conflict with ncurses for static build */
466 hrt_ualarm(int usec, int interval)
468 struct itimerval itv;
469 itv.it_value.tv_sec = usec / 1000000;
470 itv.it_value.tv_usec = usec % 1000000;
471 itv.it_interval.tv_sec = interval / 1000000;
472 itv.it_interval.tv_usec = interval % 1000000;
473 return setitimer(ITIMER_REAL, &itv, 0);
477 #if !defined(HAS_UALARM) && defined(VMS)
479 #define ualarm vms_ualarm
481 #include <lib$routines.h>
489 #define VMSERR(s) (!((s)&1))
492 us_to_VMS(useconds_t mseconds, unsigned long v[])
501 iss = lib$addx(qq,qq,qq);
502 if (VMSERR(iss)) lib$signal(iss);
503 iss = lib$subx(v,qq,v);
504 if (VMSERR(iss)) lib$signal(iss);
505 iss = lib$addx(qq,qq,qq);
506 if (VMSERR(iss)) lib$signal(iss);
507 iss = lib$subx(v,qq,v);
508 if (VMSERR(iss)) lib$signal(iss);
509 iss = lib$subx(v,qq,v);
510 if (VMSERR(iss)) lib$signal(iss);
514 VMS_to_us(unsigned long v[])
517 unsigned long div=10,quot, rem;
519 iss = lib$ediv(&div,v,",&rem);
520 if (VMSERR(iss)) lib$signal(iss);
525 typedef unsigned short word;
526 typedef struct _ualarm {
529 unsigned long delay[2];
530 unsigned long interval[2];
531 unsigned long remain[2];
536 static Alarm *a0, alarm_base;
541 static void ualarm_AST(Alarm *a);
544 vms_ualarm(int mseconds, int interval)
553 static struct item_list3 itmlst[2];
554 static int first = 1;
560 itmlst[0].code = JPI$_ASTEN;
561 itmlst[0].length = sizeof(asten);
562 itmlst[0].retlenaddr = NULL;
564 itmlst[1].length = 0;
565 itmlst[1].bufaddr = NULL;
566 itmlst[1].retlenaddr = NULL;
568 iss = lib$get_ef(&alarm_ef);
569 if (VMSERR(iss)) lib$signal(iss);
572 a0->function = UAL_NULL;
574 itmlst[0].bufaddr = &asten;
576 iss = sys$getjpiw(0,0,0,itmlst,0,0,0);
577 if (VMSERR(iss)) lib$signal(iss);
578 if (!(asten&0x08)) return -1;
582 a->function = UAL_SET;
584 a->function = UAL_CLEAR;
587 us_to_VMS(mseconds, a->delay);
589 us_to_VMS(interval, a->interval);
594 iss = sys$clref(alarm_ef);
595 if (VMSERR(iss)) lib$signal(iss);
597 iss = sys$dclast(ualarm_AST,a,0);
598 if (VMSERR(iss)) lib$signal(iss);
600 iss = sys$waitfr(alarm_ef);
601 if (VMSERR(iss)) lib$signal(iss);
603 if (a->function == UAL_ACTIVE)
604 return VMS_to_us(a->remain);
615 unsigned long now[2];
617 iss = sys$gettim(now);
618 if (VMSERR(iss)) lib$signal(iss);
620 if (a->function == UAL_SET || a->function == UAL_CLEAR) {
621 if (a0->function == UAL_ACTIVE) {
622 iss = sys$cantim(a0,PSL$C_USER);
623 if (VMSERR(iss)) lib$signal(iss);
625 iss = lib$subx(a0->remain, now, a->remain);
626 if (VMSERR(iss)) lib$signal(iss);
628 if (a->remain[1] & 0x80000000)
629 a->remain[0] = a->remain[1] = 0;
632 if (a->function == UAL_SET) {
633 a->function = a0->function;
634 a0->function = UAL_ACTIVE;
635 a0->repeat = a->repeat;
637 a0->interval[0] = a->interval[0];
638 a0->interval[1] = a->interval[1];
640 a0->delay[0] = a->delay[0];
641 a0->delay[1] = a->delay[1];
643 iss = lib$subx(now, a0->delay, a0->remain);
644 if (VMSERR(iss)) lib$signal(iss);
646 iss = sys$setimr(0,a0->delay,ualarm_AST,a0);
647 if (VMSERR(iss)) lib$signal(iss);
649 a->function = a0->function;
650 a0->function = UAL_NULL;
652 iss = sys$setef(alarm_ef);
653 if (VMSERR(iss)) lib$signal(iss);
654 } else if (a->function == UAL_ACTIVE) {
656 iss = lib$subx(now, a->interval, a->remain);
657 if (VMSERR(iss)) lib$signal(iss);
659 iss = sys$setimr(0,a->interval,ualarm_AST,a);
660 if (VMSERR(iss)) lib$signal(iss);
662 a->function = UAL_NULL;
665 if (VMSERR(iss)) lib$signal(iss);
666 lib$signal(SS$_ASTFLT);
668 lib$signal(SS$_BADPARAM);
672 #endif /* !HAS_UALARM && VMS */
674 #ifdef HAS_GETTIMEOFDAY
677 myU2time(pTHX_ UV *ret)
681 status = gettimeofday (&Tp, NULL);
695 status = gettimeofday (&Tp, NULL);
696 return status == 0 ? Tp.tv_sec + (Tp.tv_usec / 1000000.) : -1.0;
701 MODULE = Time::HiRes PACKAGE = Time::HiRes
710 #ifdef ATLEASTFIVEOHOHFIVE
711 #ifdef HAS_GETTIMEOFDAY
714 hv_store(PL_modglobal, "Time::NVtime", 12, newSViv(PTR2IV(myNVtime)), 0);
715 if (myU2time(aTHX_ auv) == 0)
716 hv_store(PL_modglobal, "Time::U2time", 12, newSViv((IV) auv[0]), 0);
722 #if defined(USE_ITHREADS) && defined(MY_CXT_KEY)
731 INCLUDE: const-xs.inc
733 #if defined(HAS_USLEEP) && defined(HAS_GETTIMEOFDAY)
739 struct timeval Ta, Tb;
741 gettimeofday(&Ta, NULL);
743 if (useconds > 1E6) {
744 IV seconds = (IV) (useconds / 1E6);
745 /* If usleep() has been implemented using setitimer()
746 * then this contortion is unnecessary-- but usleep()
747 * may be implemented in some other way, so let's contort. */
750 useconds -= 1E6 * seconds;
752 } else if (useconds < 0.0)
753 croak("Time::HiRes::usleep(%"NVgf"): negative time not invented yet", useconds);
754 usleep((U32)useconds);
757 gettimeofday(&Tb, NULL);
759 printf("[%ld %ld] [%ld %ld]\n", Tb.tv_sec, Tb.tv_usec, Ta.tv_sec, Ta.tv_usec);
761 RETVAL = 1E6*(Tb.tv_sec-Ta.tv_sec)+(NV)((IV)Tb.tv_usec-(IV)Ta.tv_usec);
769 struct timeval Ta, Tb;
771 gettimeofday(&Ta, NULL);
773 NV seconds = SvNV(ST(0));
774 if (seconds >= 0.0) {
775 UV useconds = (UV)(1E6 * (seconds - (UV)seconds));
778 if ((IV)useconds < 0) {
779 #if defined(__sparc64__) && defined(__GNUC__)
780 /* Sparc64 gcc 2.95.3 (e.g. on NetBSD) has a bug
781 * where (0.5 - (UV)(0.5)) will under certain
782 * circumstances (if the double is cast to UV more
783 * than once?) evaluate to -0.5, instead of 0.5. */
784 useconds = -(IV)useconds;
786 if ((IV)useconds < 0)
787 croak("Time::HiRes::sleep(%"NVgf"): internal error: useconds < 0 (unsigned %"UVuf" signed %"IVdf")", seconds, useconds, (IV)useconds);
791 croak("Time::HiRes::sleep(%"NVgf"): negative time not invented yet", seconds);
794 gettimeofday(&Tb, NULL);
796 printf("[%ld %ld] [%ld %ld]\n", Tb.tv_sec, Tb.tv_usec, Ta.tv_sec, Ta.tv_usec);
798 RETVAL = (NV)(Tb.tv_sec-Ta.tv_sec)+0.000001*(NV)(Tb.tv_usec-Ta.tv_usec);
808 ualarm(useconds,interval=0)
812 if (useconds < 0 || interval < 0)
813 croak("Time::HiRes::ualarm(%d, %d): negative time not invented yet", useconds, interval);
814 RETVAL = ualarm(useconds, interval);
820 alarm(seconds,interval=0)
824 if (seconds < 0.0 || interval < 0.0)
825 croak("Time::HiRes::alarm(%"NVgf", %"NVgf"): negative time not invented yet", seconds, interval);
826 RETVAL = (NV)ualarm(seconds * 1000000,
827 interval * 1000000) / 1E6;
834 #ifdef HAS_GETTIMEOFDAY
835 # ifdef MACOS_TRADITIONAL /* fix epoch TZ and use unsigned time_t */
843 status = gettimeofday (&Tp, &Tz);
844 Tp.tv_sec += Tz.tz_minuteswest * 60; /* adjust for TZ */
846 if (GIMME == G_ARRAY) {
848 /* Mac OS (Classic) has unsigned time_t */
849 PUSHs(sv_2mortal(newSVuv(Tp.tv_sec)));
850 PUSHs(sv_2mortal(newSViv(Tp.tv_usec)));
853 PUSHs(sv_2mortal(newSVnv(Tp.tv_sec + (Tp.tv_usec / 1000000.0))));
863 status = gettimeofday (&Tp, &Tz);
864 Tp.tv_sec += Tz.tz_minuteswest * 60; /* adjust for TZ */
865 RETVAL = Tp.tv_sec + (Tp.tv_usec / 1000000.0);
869 # else /* MACOS_TRADITIONAL */
876 status = gettimeofday (&Tp, NULL);
877 if (GIMME == G_ARRAY) {
879 PUSHs(sv_2mortal(newSViv(Tp.tv_sec)));
880 PUSHs(sv_2mortal(newSViv(Tp.tv_usec)));
883 PUSHs(sv_2mortal(newSVnv(Tp.tv_sec + (Tp.tv_usec / 1000000.0))));
892 status = gettimeofday (&Tp, NULL);
893 RETVAL = Tp.tv_sec + (Tp.tv_usec / 1000000.);
897 # endif /* MACOS_TRADITIONAL */
900 #if defined(HAS_GETITIMER) && defined(HAS_SETITIMER)
902 #define TV2NV(tv) ((NV)((tv).tv_sec) + 0.000001 * (NV)((tv).tv_usec))
905 setitimer(which, seconds, interval = 0)
910 struct itimerval newit;
911 struct itimerval oldit;
913 if (seconds < 0.0 || interval < 0.0)
914 croak("Time::HiRes::setitimer(%"IVdf", %"NVgf", %"NVgf"): negative time not invented yet", (IV)which, seconds, interval);
915 newit.it_value.tv_sec = seconds;
916 newit.it_value.tv_usec =
917 (seconds - (NV)newit.it_value.tv_sec) * 1000000.0;
918 newit.it_interval.tv_sec = interval;
919 newit.it_interval.tv_usec =
920 (interval - (NV)newit.it_interval.tv_sec) * 1000000.0;
921 if (setitimer(which, &newit, &oldit) == 0) {
923 PUSHs(sv_2mortal(newSVnv(TV2NV(oldit.it_value))));
924 if (GIMME == G_ARRAY) {
926 PUSHs(sv_2mortal(newSVnv(TV2NV(oldit.it_interval))));
934 struct itimerval nowit;
936 if (getitimer(which, &nowit) == 0) {
938 PUSHs(sv_2mortal(newSVnv(TV2NV(nowit.it_value))));
939 if (GIMME == G_ARRAY) {
941 PUSHs(sv_2mortal(newSVnv(TV2NV(nowit.it_interval))));