14 # include <sys/select.h>
22 constant(char *name, int arg)
27 if (strEQ(name, "ITIMER_REAL"))
33 if (strEQ(name, "ITIMER_REALPROF"))
34 #ifdef ITIMER_REALPROF
35 return ITIMER_REALPROF;
39 if (strEQ(name, "ITIMER_VIRTUAL"))
41 return ITIMER_VIRTUAL;
45 if (strEQ(name, "ITIMER_PROF"))
61 #if !defined(HAS_GETTIMEOFDAY) && defined(WIN32)
62 #define HAS_GETTIMEOFDAY
65 unsigned __int64 ft_i64;
70 #define Const64(x) x##LL
72 #define Const64(x) x##i64
74 /* Number of 100 nanosecond units from 1/1/1601 to 1/1/1970 */
75 #define EPOCH_BIAS Const64(116444736000000000)
77 /* NOTE: This does not compute the timezone info (doing so can be expensive,
78 * and appears to be unsupported even by glibc) */
80 gettimeofday(struct timeval *tp, void *not_used)
84 /* this returns time in 100-nanosecond units (i.e. tens of usecs) */
85 GetSystemTimeAsFileTime(&ft.ft_val);
87 /* seconds since epoch */
88 tp->tv_sec = (long)((ft.ft_i64 - EPOCH_BIAS) / Const64(10000000));
90 /* microseconds remaining */
91 tp->tv_usec = (long)((ft.ft_i64 / Const64(10)) % Const64(1000000));
98 #if !defined(HAS_GETTIMEOFDAY) && defined(VMS)
99 #define HAS_GETTIMEOFDAY
102 #include <time.h> /* gettimeofday */
103 #include <stdlib.h> /* qdiv */
104 #include <starlet.h> /* sys$gettim */
107 #include <lib$routines.h> /* lib$ediv() */
111 VMS binary time is expressed in 100 nano-seconds since
112 system base time which is 17-NOV-1858 00:00:00.00
115 #define DIV_100NS_TO_SECS 10000000L
116 #define DIV_100NS_TO_USECS 10L
119 gettimeofday is supposed to return times since the epoch
120 so need to determine this in terms of VMS base time
122 static $DESCRIPTOR(dscepoch,"01-JAN-1970 00:00:00.00");
125 static long base_adjust[2]={0L,0L};
127 static __int64 base_adjust=0;
132 If we don't have gettimeofday, then likely we are on a VMS machine that
133 operates on local time rather than UTC...so we have to zone-adjust.
134 This code gleefully swiped from VMS.C
137 /* method used to handle UTC conversions:
138 * 1 == CRTL gmtime(); 2 == SYS$TIMEZONE_DIFFERENTIAL; 3 == no correction
140 static int gmtime_emulation_type;
141 /* number of secs to add to UTC POSIX-style time to get local time */
142 static long int utc_offset_secs;
143 static struct dsc$descriptor_s fildevdsc =
144 { 12, DSC$K_DTYPE_T, DSC$K_CLASS_S, "LNM$FILE_DEV" };
145 static struct dsc$descriptor_s *fildev[] = { &fildevdsc, NULL };
147 static time_t toutc_dst(time_t loc) {
150 if ((rsltmp = localtime(&loc)) == NULL) return -1;
151 loc -= utc_offset_secs;
152 if (rsltmp->tm_isdst) loc -= 3600;
156 static time_t toloc_dst(time_t utc) {
159 utc += utc_offset_secs;
160 if ((rsltmp = localtime(&utc)) == NULL) return -1;
161 if (rsltmp->tm_isdst) utc += 3600;
165 #define _toutc(secs) ((secs) == (time_t) -1 ? (time_t) -1 : \
166 ((gmtime_emulation_type || timezone_setup()), \
167 (gmtime_emulation_type == 1 ? toutc_dst(secs) : \
168 ((secs) - utc_offset_secs))))
170 #define _toloc(secs) ((secs) == (time_t) -1 ? (time_t) -1 : \
171 ((gmtime_emulation_type || timezone_setup()), \
172 (gmtime_emulation_type == 1 ? toloc_dst(secs) : \
173 ((secs) + utc_offset_secs))))
180 if (gmtime_emulation_type == 0) {
182 time_t base = 15 * 86400; /* 15jan71; to avoid month/year ends between */
183 /* results of calls to gmtime() and localtime() */
186 gmtime_emulation_type++;
187 if ((tm_p = gmtime(&base)) == NULL) { /* CRTL gmtime() is a fake */
188 char off[LNM$C_NAMLENGTH+1];;
190 gmtime_emulation_type++;
191 if (!Perl_vmstrnenv("SYS$TIMEZONE_DIFFERENTIAL",off,0,fildev,0)) {
192 gmtime_emulation_type++;
194 Perl_warn(aTHX_ "no UTC offset information; assuming local time is UTC");
196 else { utc_offset_secs = atol(off); }
198 else { /* We've got a working gmtime() */
199 struct tm gmt, local;
202 tm_p = localtime(&base);
204 utc_offset_secs = (local.tm_mday - gmt.tm_mday) * 86400;
205 utc_offset_secs += (local.tm_hour - gmt.tm_hour) * 3600;
206 utc_offset_secs += (local.tm_min - gmt.tm_min) * 60;
207 utc_offset_secs += (local.tm_sec - gmt.tm_sec);
215 gettimeofday (struct timeval *tp, void *tpz)
221 long div_100ns_to_secs;
222 long div_100ns_to_usecs;
230 In case of error, tv_usec = 0 and tv_sec = VMS condition code.
231 The return from function is also set to -1.
232 This is not exactly as per the manual page.
238 if (base_adjust[0]==0 && base_adjust[1]==0) {
240 if (base_adjust==0) { /* Need to determine epoch adjustment */
242 ret=sys$bintim(&dscepoch,&base_adjust);
243 if (1 != (ret &&1)) {
249 ret=sys$gettim(&quad); /* Get VMS system time */
250 if ((1 && ret) == 1) {
252 quad[0] -= base_adjust[0]; /* convert to epoch offset */
253 quad[1] -= base_adjust[1]; /* convert 2nd half of quadword */
254 div_100ns_to_secs = DIV_100NS_TO_SECS;
255 div_100ns_to_usecs = DIV_100NS_TO_USECS;
256 lib$ediv(&div_100ns_to_secs,&quad,&quo,&rem);
259 lib$ediv(&div_100ns_to_usecs,&quad1,&quo1,&rem1);
260 tp->tv_sec = quo; /* Whole seconds */
261 tp->tv_usec = quo1; /* Micro-seconds */
263 quad -= base_adjust; /* convert to epoch offset */
264 ans1=qdiv(quad,DIV_100NS_TO_SECS);
265 ans2=qdiv(ans1.rem,DIV_100NS_TO_USECS);
266 tp->tv_sec = ans1.quot; /* Whole seconds */
267 tp->tv_usec = ans2.quot; /* Micro-seconds */
275 if (VMSISH_TIME) tp->tv_sec = _toloc(tp->tv_sec);
277 if (!VMSISH_TIME) tp->tv_sec = _toutc(tp->tv_sec);
284 #if !defined(HAS_USLEEP) && defined(HAS_SELECT)
285 #ifndef SELECT_IS_BROKEN
287 #define usleep hrt_usleep /* could conflict with ncurses for static build */
290 hrt_usleep(unsigned long usec)
295 select(0, (Select_fd_set_t)NULL, (Select_fd_set_t)NULL,
296 (Select_fd_set_t)NULL, &tv);
301 #if !defined(HAS_USLEEP) && defined(WIN32)
303 #define usleep hrt_usleep /* could conflict with ncurses for static build */
306 hrt_usleep(unsigned long usec)
315 #if !defined(HAS_UALARM) && defined(HAS_SETITIMER)
317 #define ualarm hrt_ualarm /* could conflict with ncurses for static build */
320 hrt_ualarm(int usec, int interval)
322 struct itimerval itv;
323 itv.it_value.tv_sec = usec / 1000000;
324 itv.it_value.tv_usec = usec % 1000000;
325 itv.it_interval.tv_sec = interval / 1000000;
326 itv.it_interval.tv_usec = interval % 1000000;
327 return setitimer(ITIMER_REAL, &itv, 0);
331 #if !defined(HAS_UALARM) && defined(VMS)
333 #define ualarm vms_ualarm
335 #include <lib$routines.h>
343 #define VMSERR(s) (!((s)&1))
346 us_to_VMS(useconds_t mseconds, unsigned long v[])
355 iss = lib$addx(qq,qq,qq);
356 if (VMSERR(iss)) lib$signal(iss);
357 iss = lib$subx(v,qq,v);
358 if (VMSERR(iss)) lib$signal(iss);
359 iss = lib$addx(qq,qq,qq);
360 if (VMSERR(iss)) lib$signal(iss);
361 iss = lib$subx(v,qq,v);
362 if (VMSERR(iss)) lib$signal(iss);
363 iss = lib$subx(v,qq,v);
364 if (VMSERR(iss)) lib$signal(iss);
368 VMS_to_us(unsigned long v[])
371 unsigned long div=10,quot, rem;
373 iss = lib$ediv(&div,v,",&rem);
374 if (VMSERR(iss)) lib$signal(iss);
379 typedef unsigned short word;
380 typedef struct _ualarm {
383 unsigned long delay[2];
384 unsigned long interval[2];
385 unsigned long remain[2];
390 static Alarm *a0, alarm_base;
395 static void ualarm_AST(Alarm *a);
398 vms_ualarm(int mseconds, int interval)
407 static struct item_list3 itmlst[2];
408 static int first = 1;
414 itmlst[0].code = JPI$_ASTEN;
415 itmlst[0].length = sizeof(asten);
416 itmlst[0].retlenaddr = NULL;
418 itmlst[1].length = 0;
419 itmlst[1].bufaddr = NULL;
420 itmlst[1].retlenaddr = NULL;
422 iss = lib$get_ef(&alarm_ef);
423 if (VMSERR(iss)) lib$signal(iss);
426 a0->function = UAL_NULL;
428 itmlst[0].bufaddr = &asten;
430 iss = sys$getjpiw(0,0,0,itmlst,0,0,0);
431 if (VMSERR(iss)) lib$signal(iss);
432 if (!(asten&0x08)) return -1;
436 a->function = UAL_SET;
438 a->function = UAL_CLEAR;
441 us_to_VMS(mseconds, a->delay);
443 us_to_VMS(interval, a->interval);
448 iss = sys$clref(alarm_ef);
449 if (VMSERR(iss)) lib$signal(iss);
451 iss = sys$dclast(ualarm_AST,a,0);
452 if (VMSERR(iss)) lib$signal(iss);
454 iss = sys$waitfr(alarm_ef);
455 if (VMSERR(iss)) lib$signal(iss);
457 if (a->function == UAL_ACTIVE)
458 return VMS_to_us(a->remain);
469 unsigned long now[2];
471 iss = sys$gettim(now);
472 if (VMSERR(iss)) lib$signal(iss);
474 if (a->function == UAL_SET || a->function == UAL_CLEAR) {
475 if (a0->function == UAL_ACTIVE) {
476 iss = sys$cantim(a0,PSL$C_USER);
477 if (VMSERR(iss)) lib$signal(iss);
479 iss = lib$subx(a0->remain, now, a->remain);
480 if (VMSERR(iss)) lib$signal(iss);
482 if (a->remain[1] & 0x80000000)
483 a->remain[0] = a->remain[1] = 0;
486 if (a->function == UAL_SET) {
487 a->function = a0->function;
488 a0->function = UAL_ACTIVE;
489 a0->repeat = a->repeat;
491 a0->interval[0] = a->interval[0];
492 a0->interval[1] = a->interval[1];
494 a0->delay[0] = a->delay[0];
495 a0->delay[1] = a->delay[1];
497 iss = lib$subx(now, a0->delay, a0->remain);
498 if (VMSERR(iss)) lib$signal(iss);
500 iss = sys$setimr(0,a0->delay,ualarm_AST,a0);
501 if (VMSERR(iss)) lib$signal(iss);
503 a->function = a0->function;
504 a0->function = UAL_NULL;
506 iss = sys$setef(alarm_ef);
507 if (VMSERR(iss)) lib$signal(iss);
508 } else if (a->function == UAL_ACTIVE) {
510 iss = lib$subx(now, a->interval, a->remain);
511 if (VMSERR(iss)) lib$signal(iss);
513 iss = sys$setimr(0,a->interval,ualarm_AST,a);
514 if (VMSERR(iss)) lib$signal(iss);
516 a->function = UAL_NULL;
519 if (VMSERR(iss)) lib$signal(iss);
520 lib$signal(SS$_ASTFLT);
522 lib$signal(SS$_BADPARAM);
526 #endif /* !HAS_UALARM && VMS */
530 #ifdef HAS_GETTIMEOFDAY
537 status = PerlProc_gettimeofday(&Tp,NULL);
548 status = PerlProc_gettimeofday(&Tp,NULL);
549 return status == 0 ? Tp.tv_sec + (Tp.tv_usec / 1000000.) : -1.0;
554 MODULE = Time::HiRes PACKAGE = Time::HiRes
559 #ifdef HAS_GETTIMEOFDAY
562 hv_store(PL_modglobal, "Time::NVtime", 12, newSViv(PTR2IV(myNVtime)), 0);
563 if (myU2time(auv) == 0)
564 hv_store(PL_modglobal, "Time::U2time", 12, newSViv((IV) auv[0]), 0);
573 #if defined(HAS_USLEEP) && defined(HAS_GETTIMEOFDAY)
579 struct timeval Ta, Tb;
581 PerlProc_gettimeofday(&Ta,NULL);
583 if (useconds > 1E6) {
584 IV seconds = (IV) (useconds / 1E6);
585 /* If usleep() has been implemented using setitimer()
586 * then this contortion is unnecessary-- but usleep()
587 * may be implemented in some other way, so let's contort. */
590 useconds -= 1E6 * seconds;
592 } else if (useconds < 0.0)
593 croak("Time::HiRes::usleep(%"NVgf"): negative time not invented yet", useconds);
594 usleep((UV)useconds);
597 PerlProc_gettimeofday(&Tb,NULL);
599 printf("[%ld %ld] [%ld %ld]\n", Tb.tv_sec, Tb.tv_usec, Ta.tv_sec, Ta.tv_usec);
601 RETVAL = 1E6*(Tb.tv_sec-Ta.tv_sec)+(NV)((IV)Tb.tv_usec-(IV)Ta.tv_usec);
609 struct timeval Ta, Tb;
611 PerlProc_gettimeofday(&Ta,NULL);
613 NV seconds = SvNV(ST(0));
614 if (seconds >= 0.0) {
615 UV useconds = (UV)(1E6 * (seconds - (UV)seconds));
620 croak("Time::HiRes::sleep(%"NVgf"): negative time not invented yet", seconds);
623 PerlProc_gettimeofday(&Tb,NULL);
625 printf("[%ld %ld] [%ld %ld]\n", Tb.tv_sec, Tb.tv_usec, Ta.tv_sec, Ta.tv_usec);
627 RETVAL = (NV)(Tb.tv_sec-Ta.tv_sec)+0.000001*(NV)(Tb.tv_usec-Ta.tv_usec);
637 ualarm(useconds,interval=0)
641 if (useconds < 0 || interval < 0)
642 croak("Time::HiRes::ualarm(%d, %d): negative time not invented yet", useconds, interval);
643 RETVAL = ualarm(useconds, interval);
649 alarm(seconds,interval=0)
653 if (seconds < 0.0 || interval < 0.0)
654 croak("Time::HiRes::alarm(%"NVgf", %"NVgf"): negative time not invented yet", seconds, interval);
655 RETVAL = (NV)ualarm(seconds * 1000000,
656 interval * 1000000) / 1E6;
663 #ifdef HAS_GETTIMEOFDAY
664 # ifdef MACOS_TRADITIONAL /* fix epoch TZ and use unsigned time_t */
672 status = PerlProc_gettimeofday(&Tp,&Tz);
673 Tp.tv_sec += Tz.tz_minuteswest * 60; /* adjust for TZ */
675 if (GIMME == G_ARRAY) {
677 /* Mac OS (Classic) has unsigned time_t */
678 PUSHs(sv_2mortal(newSVuv(Tp.tv_sec)));
679 PUSHs(sv_2mortal(newSViv(Tp.tv_usec)));
682 PUSHs(sv_2mortal(newSVnv(Tp.tv_sec + (Tp.tv_usec / 1000000.0))));
692 status = PerlProc_gettimeofday(&Tp,&Tz);
693 Tp.tv_sec += Tz.tz_minuteswest * 60; /* adjust for TZ */
694 RETVAL = Tp.tv_sec + (Tp.tv_usec / 1000000.0);
698 # else /* MACOS_TRADITIONAL */
705 status = PerlProc_gettimeofday(&Tp,NULL);
706 if (GIMME == G_ARRAY) {
708 PUSHs(sv_2mortal(newSViv(Tp.tv_sec)));
709 PUSHs(sv_2mortal(newSViv(Tp.tv_usec)));
712 PUSHs(sv_2mortal(newSVnv(Tp.tv_sec + (Tp.tv_usec / 1000000.0))));
721 status = PerlProc_gettimeofday(&Tp,NULL);
722 RETVAL = Tp.tv_sec + (Tp.tv_usec / 1000000.);
726 # endif /* MACOS_TRADITIONAL */
729 #if defined(HAS_GETITIMER) && defined(HAS_SETITIMER)
731 #define TV2NV(tv) ((NV)((tv).tv_sec) + 0.000001 * (NV)((tv).tv_usec))
734 setitimer(which, seconds, interval = 0)
739 struct itimerval newit;
740 struct itimerval oldit;
742 if (seconds < 0.0 || interval < 0.0)
743 croak("Time::HiRes::setitimer(%"IVdf", %"NVgf", %"NVgf"): negative time not invented yet", which, seconds, interval);
744 newit.it_value.tv_sec = seconds;
745 newit.it_value.tv_usec =
746 (seconds - (NV)newit.it_value.tv_sec) * 1000000.0;
747 newit.it_interval.tv_sec = interval;
748 newit.it_interval.tv_usec =
749 (interval - (NV)newit.it_interval.tv_sec) * 1000000.0;
750 if (setitimer(which, &newit, &oldit) == 0) {
752 PUSHs(sv_2mortal(newSVnv(TV2NV(oldit.it_value))));
753 if (GIMME == G_ARRAY) {
755 PUSHs(sv_2mortal(newSVnv(TV2NV(oldit.it_interval))));
763 struct itimerval nowit;
765 if (getitimer(which, &nowit) == 0) {
767 PUSHs(sv_2mortal(newSVnv(TV2NV(nowit.it_value))));
768 if (GIMME == G_ARRAY) {
770 PUSHs(sv_2mortal(newSVnv(TV2NV(nowit.it_interval))));
776 # $Id: HiRes.xs,v 1.11 1999/03/16 02:27:38 wegscd Exp wegscd $
779 # Revision 1.11 1999/03/16 02:27:38 wegscd
780 # Add U2time, NVtime. Fix symbols for static link.
782 # Revision 1.10 1998/09/30 02:36:25 wegscd
785 # Revision 1.9 1998/07/07 02:42:06 wegscd
788 # Revision 1.8 1998/07/02 01:47:26 wegscd
789 # Add Win32 code for gettimeofday.
791 # Revision 1.7 1997/11/13 02:08:12 wegscd
792 # Add missing EXTEND in gettimeofday() scalar code.
794 # Revision 1.6 1997/11/11 02:32:35 wegscd
795 # Do something useful when calling gettimeofday() in a scalar context.
796 # The patch is courtesy of Gisle Aas.
798 # Revision 1.5 1997/11/06 03:10:47 wegscd
799 # Fake ualarm() if we have setitimer.
801 # Revision 1.4 1997/11/05 05:41:23 wegscd
802 # Turn prototypes ON (suggested by Gisle Aas)
804 # Revision 1.3 1997/10/13 20:56:15 wegscd
805 # Add PROTOTYPES: DISABLE
807 # Revision 1.2 1997/05/23 01:01:38 wegscd
808 # Conditional compilation, depending on what the OS gives us.
810 # Revision 1.1 1996/09/03 18:26:35 wegscd