14 # include <sys/select.h>
27 # if defined(USE_LONG_DOUBLE) && defined(HAS_LONG_DOUBLE)
28 # define NVTYPE long double
30 # define NVTYPE double
37 # if IVSIZE == LONGSIZE
40 # if IVSIZE == INTSIZE
50 # if defined(USE_LONG_DOUBLE) && defined(HAS_LONG_DOUBLE) && \
51 defined(PERL_PRIgldbl) /* Not very likely, but let's try anyway. */
52 # define NVgf PERL_PRIgldbl
60 #if (IVSIZE == PTRSIZE) && (UVSIZE == PTRSIZE)
62 # define INT2PTR(any,d) (any)(d)
64 # if PTRSIZE == LONGSIZE
65 # define PTRV unsigned long
67 # define PTRV unsigned
69 # define INT2PTR(any,d) (any)(PTRV)(d)
71 #define PTR2IV(p) INT2PTR(IV,p)
77 sv_2pv_nolen(pTHX_ register SV *sv)
80 return sv_2pv(sv, &n_a);
82 # define SvPV_nolen(sv) \
83 ((SvFLAGS(sv) & (SVf_POK)) == SVf_POK \
84 ? SvPVX(sv) : sv_2pv_nolen(sv))
87 #ifndef PerlProc_pause
88 # define PerlProc_pause() Pause()
91 /* Though the cpp define ITIMER_VIRTUAL is available the functionality
92 * is not supported in Cygwin as of August 2002, ditto for Win32.
93 * Neither are ITIMER_PROF or ITIMER_REALPROF implemented. --jhi
95 #if defined(__CYGWIN__) || defined(WIN32)
96 # undef ITIMER_VIRTUAL
98 # undef ITIMER_REALPROF
102 constant(char *name, int arg)
107 if (strEQ(name, "d_getitimer"))
113 if (strEQ(name, "d_nanosleep"))
119 if (strEQ(name, "d_setitimer"))
125 if (strEQ(name, "d_ualarm"))
131 if (strEQ(name, "d_usleep"))
139 if (strEQ(name, "ITIMER_REAL"))
145 if (strEQ(name, "ITIMER_REALPROF"))
146 #ifdef ITIMER_REALPROF
147 return ITIMER_REALPROF;
151 if (strEQ(name, "ITIMER_VIRTUAL"))
152 #ifdef ITIMER_VIRTUAL
153 return ITIMER_VIRTUAL;
157 if (strEQ(name, "ITIMER_PROF"))
173 #if !defined(HAS_GETTIMEOFDAY) && defined(WIN32)
174 #define HAS_GETTIMEOFDAY
176 /* shows up in winsock.h?
184 unsigned __int64 ft_i64;
188 /* Number of 100 nanosecond units from 1/1/1601 to 1/1/1970 */
190 #define Const64(x) x##LL
192 #define Const64(x) x##i64
194 #define EPOCH_BIAS Const64(116444736000000000)
196 /* NOTE: This does not compute the timezone info (doing so can be expensive,
197 * and appears to be unsupported even by glibc) */
199 gettimeofday (struct timeval *tp, void *not_used)
203 /* this returns time in 100-nanosecond units (i.e. tens of usecs) */
204 GetSystemTimeAsFileTime(&ft.ft_val);
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(HAS_GETTIMEOFDAY) && defined(VMS)
217 #define HAS_GETTIMEOFDAY
220 #include <time.h> /* gettimeofday */
221 #include <stdlib.h> /* qdiv */
222 #include <starlet.h> /* sys$gettim */
225 #include <lib$routines.h> /* lib$ediv() */
229 VMS binary time is expressed in 100 nano-seconds since
230 system base time which is 17-NOV-1858 00:00:00.00
233 #define DIV_100NS_TO_SECS 10000000L
234 #define DIV_100NS_TO_USECS 10L
237 gettimeofday is supposed to return times since the epoch
238 so need to determine this in terms of VMS base time
240 static $DESCRIPTOR(dscepoch,"01-JAN-1970 00:00:00.00");
243 static long base_adjust[2]={0L,0L};
245 static __int64 base_adjust=0;
250 If we don't have gettimeofday, then likely we are on a VMS machine that
251 operates on local time rather than UTC...so we have to zone-adjust.
252 This code gleefully swiped from VMS.C
255 /* method used to handle UTC conversions:
256 * 1 == CRTL gmtime(); 2 == SYS$TIMEZONE_DIFFERENTIAL; 3 == no correction
258 static int gmtime_emulation_type;
259 /* number of secs to add to UTC POSIX-style time to get local time */
260 static long int utc_offset_secs;
261 static struct dsc$descriptor_s fildevdsc =
262 { 12, DSC$K_DTYPE_T, DSC$K_CLASS_S, "LNM$FILE_DEV" };
263 static struct dsc$descriptor_s *fildev[] = { &fildevdsc, NULL };
265 static time_t toutc_dst(time_t loc) {
268 if ((rsltmp = localtime(&loc)) == NULL) return -1;
269 loc -= utc_offset_secs;
270 if (rsltmp->tm_isdst) loc -= 3600;
274 static time_t toloc_dst(time_t utc) {
277 utc += utc_offset_secs;
278 if ((rsltmp = localtime(&utc)) == NULL) return -1;
279 if (rsltmp->tm_isdst) utc += 3600;
283 #define _toutc(secs) ((secs) == (time_t) -1 ? (time_t) -1 : \
284 ((gmtime_emulation_type || timezone_setup()), \
285 (gmtime_emulation_type == 1 ? toutc_dst(secs) : \
286 ((secs) - utc_offset_secs))))
288 #define _toloc(secs) ((secs) == (time_t) -1 ? (time_t) -1 : \
289 ((gmtime_emulation_type || timezone_setup()), \
290 (gmtime_emulation_type == 1 ? toloc_dst(secs) : \
291 ((secs) + utc_offset_secs))))
298 if (gmtime_emulation_type == 0) {
300 time_t base = 15 * 86400; /* 15jan71; to avoid month/year ends between */
301 /* results of calls to gmtime() and localtime() */
304 gmtime_emulation_type++;
305 if ((tm_p = gmtime(&base)) == NULL) { /* CRTL gmtime() is a fake */
306 char off[LNM$C_NAMLENGTH+1];;
308 gmtime_emulation_type++;
309 if (!Perl_vmstrnenv("SYS$TIMEZONE_DIFFERENTIAL",off,0,fildev,0)) {
310 gmtime_emulation_type++;
312 Perl_warn(aTHX_ "no UTC offset information; assuming local time is UTC");
314 else { utc_offset_secs = atol(off); }
316 else { /* We've got a working gmtime() */
317 struct tm gmt, local;
320 tm_p = localtime(&base);
322 utc_offset_secs = (local.tm_mday - gmt.tm_mday) * 86400;
323 utc_offset_secs += (local.tm_hour - gmt.tm_hour) * 3600;
324 utc_offset_secs += (local.tm_min - gmt.tm_min) * 60;
325 utc_offset_secs += (local.tm_sec - gmt.tm_sec);
333 gettimeofday (struct timeval *tp, void *tpz)
339 long div_100ns_to_secs;
340 long div_100ns_to_usecs;
348 In case of error, tv_usec = 0 and tv_sec = VMS condition code.
349 The return from function is also set to -1.
350 This is not exactly as per the manual page.
356 if (base_adjust[0]==0 && base_adjust[1]==0) {
358 if (base_adjust==0) { /* Need to determine epoch adjustment */
360 ret=sys$bintim(&dscepoch,&base_adjust);
361 if (1 != (ret &&1)) {
367 ret=sys$gettim(&quad); /* Get VMS system time */
368 if ((1 && ret) == 1) {
370 quad[0] -= base_adjust[0]; /* convert to epoch offset */
371 quad[1] -= base_adjust[1]; /* convert 2nd half of quadword */
372 div_100ns_to_secs = DIV_100NS_TO_SECS;
373 div_100ns_to_usecs = DIV_100NS_TO_USECS;
374 lib$ediv(&div_100ns_to_secs,&quad,&quo,&rem);
377 lib$ediv(&div_100ns_to_usecs,&quad1,&quo1,&rem1);
378 tp->tv_sec = quo; /* Whole seconds */
379 tp->tv_usec = quo1; /* Micro-seconds */
381 quad -= base_adjust; /* convert to epoch offset */
382 ans1=qdiv(quad,DIV_100NS_TO_SECS);
383 ans2=qdiv(ans1.rem,DIV_100NS_TO_USECS);
384 tp->tv_sec = ans1.quot; /* Whole seconds */
385 tp->tv_usec = ans2.quot; /* Micro-seconds */
393 if (VMSISH_TIME) tp->tv_sec = _toloc(tp->tv_sec);
395 if (!VMSISH_TIME) tp->tv_sec = _toutc(tp->tv_sec);
403 #if !defined(HAS_USLEEP) && defined(HAS_NANOSLEEP)
405 #define usleep hrt_nanosleep /* could conflict with ncurses for static build */
408 hrt_nanosleep(unsigned long usec)
411 res.tv_sec = usec/1000/1000;
412 res.tv_nsec = ( usec - res.tv_sec*1000*1000 ) * 1000;
413 nanosleep(&res, NULL);
418 #if !defined(HAS_USLEEP) && defined(HAS_SELECT)
419 #ifndef SELECT_IS_BROKEN
421 #define usleep hrt_usleep /* could conflict with ncurses for static build */
424 hrt_usleep(unsigned long usec)
429 select(0, (Select_fd_set_t)NULL, (Select_fd_set_t)NULL,
430 (Select_fd_set_t)NULL, &tv);
435 #if !defined(HAS_USLEEP) && defined(WIN32)
437 #define usleep hrt_usleep /* could conflict with ncurses for static build */
440 hrt_usleep(unsigned long usec)
449 #if !defined(HAS_UALARM) && defined(HAS_SETITIMER)
451 #define ualarm hrt_ualarm /* could conflict with ncurses for static build */
454 hrt_ualarm(int usec, int interval)
456 struct itimerval itv;
457 itv.it_value.tv_sec = usec / 1000000;
458 itv.it_value.tv_usec = usec % 1000000;
459 itv.it_interval.tv_sec = interval / 1000000;
460 itv.it_interval.tv_usec = interval % 1000000;
461 return setitimer(ITIMER_REAL, &itv, 0);
465 #if !defined(HAS_UALARM) && defined(VMS)
467 #define ualarm vms_ualarm
469 #include <lib$routines.h>
477 #define VMSERR(s) (!((s)&1))
480 us_to_VMS(useconds_t mseconds, unsigned long v[])
489 iss = lib$addx(qq,qq,qq);
490 if (VMSERR(iss)) lib$signal(iss);
491 iss = lib$subx(v,qq,v);
492 if (VMSERR(iss)) lib$signal(iss);
493 iss = lib$addx(qq,qq,qq);
494 if (VMSERR(iss)) lib$signal(iss);
495 iss = lib$subx(v,qq,v);
496 if (VMSERR(iss)) lib$signal(iss);
497 iss = lib$subx(v,qq,v);
498 if (VMSERR(iss)) lib$signal(iss);
502 VMS_to_us(unsigned long v[])
505 unsigned long div=10,quot, rem;
507 iss = lib$ediv(&div,v,",&rem);
508 if (VMSERR(iss)) lib$signal(iss);
513 typedef unsigned short word;
514 typedef struct _ualarm {
517 unsigned long delay[2];
518 unsigned long interval[2];
519 unsigned long remain[2];
524 static Alarm *a0, alarm_base;
529 static void ualarm_AST(Alarm *a);
532 vms_ualarm(int mseconds, int interval)
541 static struct item_list3 itmlst[2];
542 static int first = 1;
548 itmlst[0].code = JPI$_ASTEN;
549 itmlst[0].length = sizeof(asten);
550 itmlst[0].retlenaddr = NULL;
552 itmlst[1].length = 0;
553 itmlst[1].bufaddr = NULL;
554 itmlst[1].retlenaddr = NULL;
556 iss = lib$get_ef(&alarm_ef);
557 if (VMSERR(iss)) lib$signal(iss);
560 a0->function = UAL_NULL;
562 itmlst[0].bufaddr = &asten;
564 iss = sys$getjpiw(0,0,0,itmlst,0,0,0);
565 if (VMSERR(iss)) lib$signal(iss);
566 if (!(asten&0x08)) return -1;
570 a->function = UAL_SET;
572 a->function = UAL_CLEAR;
575 us_to_VMS(mseconds, a->delay);
577 us_to_VMS(interval, a->interval);
582 iss = sys$clref(alarm_ef);
583 if (VMSERR(iss)) lib$signal(iss);
585 iss = sys$dclast(ualarm_AST,a,0);
586 if (VMSERR(iss)) lib$signal(iss);
588 iss = sys$waitfr(alarm_ef);
589 if (VMSERR(iss)) lib$signal(iss);
591 if (a->function == UAL_ACTIVE)
592 return VMS_to_us(a->remain);
603 unsigned long now[2];
605 iss = sys$gettim(now);
606 if (VMSERR(iss)) lib$signal(iss);
608 if (a->function == UAL_SET || a->function == UAL_CLEAR) {
609 if (a0->function == UAL_ACTIVE) {
610 iss = sys$cantim(a0,PSL$C_USER);
611 if (VMSERR(iss)) lib$signal(iss);
613 iss = lib$subx(a0->remain, now, a->remain);
614 if (VMSERR(iss)) lib$signal(iss);
616 if (a->remain[1] & 0x80000000)
617 a->remain[0] = a->remain[1] = 0;
620 if (a->function == UAL_SET) {
621 a->function = a0->function;
622 a0->function = UAL_ACTIVE;
623 a0->repeat = a->repeat;
625 a0->interval[0] = a->interval[0];
626 a0->interval[1] = a->interval[1];
628 a0->delay[0] = a->delay[0];
629 a0->delay[1] = a->delay[1];
631 iss = lib$subx(now, a0->delay, a0->remain);
632 if (VMSERR(iss)) lib$signal(iss);
634 iss = sys$setimr(0,a0->delay,ualarm_AST,a0);
635 if (VMSERR(iss)) lib$signal(iss);
637 a->function = a0->function;
638 a0->function = UAL_NULL;
640 iss = sys$setef(alarm_ef);
641 if (VMSERR(iss)) lib$signal(iss);
642 } else if (a->function == UAL_ACTIVE) {
644 iss = lib$subx(now, a->interval, a->remain);
645 if (VMSERR(iss)) lib$signal(iss);
647 iss = sys$setimr(0,a->interval,ualarm_AST,a);
648 if (VMSERR(iss)) lib$signal(iss);
650 a->function = UAL_NULL;
653 if (VMSERR(iss)) lib$signal(iss);
654 lib$signal(SS$_ASTFLT);
656 lib$signal(SS$_BADPARAM);
660 #endif /* !HAS_UALARM && VMS */
662 #ifdef HAS_GETTIMEOFDAY
669 status = gettimeofday (&Tp, NULL);
680 status = gettimeofday (&Tp, NULL);
681 return status == 0 ? Tp.tv_sec + (Tp.tv_usec / 1000000.) : -1.0;
686 MODULE = Time::HiRes PACKAGE = Time::HiRes
691 #ifdef ATLEASTFIVEOHOHFIVE
692 #ifdef HAS_GETTIMEOFDAY
695 hv_store(PL_modglobal, "Time::NVtime", 12, newSViv(PTR2IV(myNVtime)), 0);
696 if (myU2time(auv) == 0)
697 hv_store(PL_modglobal, "Time::U2time", 12, newSViv((IV) auv[0]), 0);
707 #if defined(HAS_USLEEP) && defined(HAS_GETTIMEOFDAY)
713 struct timeval Ta, Tb;
715 gettimeofday(&Ta, NULL);
717 if (useconds > 1E6) {
718 IV seconds = (IV) (useconds / 1E6);
719 /* If usleep() has been implemented using setitimer()
720 * then this contortion is unnecessary-- but usleep()
721 * may be implemented in some other way, so let's contort. */
724 useconds -= 1E6 * seconds;
726 } else if (useconds < 0.0)
727 croak("Time::HiRes::usleep(%"NVgf"): negative time not invented yet", useconds);
728 usleep((U32)useconds);
731 gettimeofday(&Tb, NULL);
733 printf("[%ld %ld] [%ld %ld]\n", Tb.tv_sec, Tb.tv_usec, Ta.tv_sec, Ta.tv_usec);
735 RETVAL = 1E6*(Tb.tv_sec-Ta.tv_sec)+(NV)((IV)Tb.tv_usec-(IV)Ta.tv_usec);
743 struct timeval Ta, Tb;
745 gettimeofday(&Ta, NULL);
747 NV seconds = SvNV(ST(0));
748 if (seconds >= 0.0) {
749 UV useconds = (UV)(1E6 * (seconds - (UV)seconds));
754 croak("Time::HiRes::sleep(%"NVgf"): negative time not invented yet", seconds);
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 = (NV)(Tb.tv_sec-Ta.tv_sec)+0.000001*(NV)(Tb.tv_usec-Ta.tv_usec);
771 ualarm(useconds,interval=0)
775 if (useconds < 0 || interval < 0)
776 croak("Time::HiRes::ualarm(%d, %d): negative time not invented yet", useconds, interval);
777 RETVAL = ualarm(useconds, interval);
783 alarm(seconds,interval=0)
787 if (seconds < 0.0 || interval < 0.0)
788 croak("Time::HiRes::alarm(%"NVgf", %"NVgf"): negative time not invented yet", seconds, interval);
789 RETVAL = (NV)ualarm(seconds * 1000000,
790 interval * 1000000) / 1E6;
797 #ifdef HAS_GETTIMEOFDAY
798 # ifdef MACOS_TRADITIONAL /* fix epoch TZ and use unsigned time_t */
806 status = gettimeofday (&Tp, &Tz);
807 Tp.tv_sec += Tz.tz_minuteswest * 60; /* adjust for TZ */
809 if (GIMME == G_ARRAY) {
811 /* Mac OS (Classic) has unsigned time_t */
812 PUSHs(sv_2mortal(newSVuv(Tp.tv_sec)));
813 PUSHs(sv_2mortal(newSViv(Tp.tv_usec)));
816 PUSHs(sv_2mortal(newSVnv(Tp.tv_sec + (Tp.tv_usec / 1000000.0))));
826 status = gettimeofday (&Tp, &Tz);
827 Tp.tv_sec += Tz.tz_minuteswest * 60; /* adjust for TZ */
828 RETVAL = Tp.tv_sec + (Tp.tv_usec / 1000000.0);
832 # else /* MACOS_TRADITIONAL */
839 status = gettimeofday (&Tp, NULL);
840 if (GIMME == G_ARRAY) {
842 PUSHs(sv_2mortal(newSViv(Tp.tv_sec)));
843 PUSHs(sv_2mortal(newSViv(Tp.tv_usec)));
846 PUSHs(sv_2mortal(newSVnv(Tp.tv_sec + (Tp.tv_usec / 1000000.0))));
855 status = gettimeofday (&Tp, NULL);
856 RETVAL = Tp.tv_sec + (Tp.tv_usec / 1000000.);
860 # endif /* MACOS_TRADITIONAL */
863 #if defined(HAS_GETITIMER) && defined(HAS_SETITIMER)
865 #define TV2NV(tv) ((NV)((tv).tv_sec) + 0.000001 * (NV)((tv).tv_usec))
868 setitimer(which, seconds, interval = 0)
873 struct itimerval newit;
874 struct itimerval oldit;
876 if (seconds < 0.0 || interval < 0.0)
877 croak("Time::HiRes::setitimer(%"IVdf", %"NVgf", %"NVgf"): negative time not invented yet", (IV)which, seconds, interval);
878 newit.it_value.tv_sec = seconds;
879 newit.it_value.tv_usec =
880 (seconds - (NV)newit.it_value.tv_sec) * 1000000.0;
881 newit.it_interval.tv_sec = interval;
882 newit.it_interval.tv_usec =
883 (interval - (NV)newit.it_interval.tv_sec) * 1000000.0;
884 if (setitimer(which, &newit, &oldit) == 0) {
886 PUSHs(sv_2mortal(newSVnv(TV2NV(oldit.it_value))));
887 if (GIMME == G_ARRAY) {
889 PUSHs(sv_2mortal(newSVnv(TV2NV(oldit.it_interval))));
897 struct itimerval nowit;
899 if (getitimer(which, &nowit) == 0) {
901 PUSHs(sv_2mortal(newSVnv(TV2NV(nowit.it_value))));
902 if (GIMME == G_ARRAY) {
904 PUSHs(sv_2mortal(newSVnv(TV2NV(nowit.it_interval))));