4 #define PERL_NO_GET_CONTEXT
9 #if defined(__CYGWIN__) && defined(HAS_W32API_WINDOWS_H)
10 # include <w32api/windows.h>
11 # define CYGWIN_WITH_W32API
16 # include <sys/time.h>
20 # include <sys/select.h>
27 #ifndef PerlProc_pause
28 # define PerlProc_pause() Pause()
34 # define Pause() sleep(~0) /* Zzz for a long time. */
37 /* Though the cpp define ITIMER_VIRTUAL is available the functionality
38 * is not supported in Cygwin as of August 2004, ditto for Win32.
39 * Neither are ITIMER_PROF or ITIMER_REALPROF implemented. --jhi
41 #if defined(__CYGWIN__) || defined(WIN32)
42 # undef ITIMER_VIRTUAL
44 # undef ITIMER_REALPROF
47 /* 5.004 doesn't define PL_sv_undef */
48 #ifndef ATLEASTFIVEOHOHFIVE
50 # define PL_sv_undef sv_undef
54 #include "const-c.inc"
56 #if defined(WIN32) || defined(CYGWIN_WITH_W32API)
58 #ifndef HAS_GETTIMEOFDAY
59 # define HAS_GETTIMEOFDAY
62 /* shows up in winsock.h?
70 unsigned __int64 ft_i64;
74 #define MY_CXT_KEY "Time::HiRes_" XS_VERSION
77 unsigned long run_count;
78 unsigned __int64 base_ticks;
79 unsigned __int64 tick_frequency;
80 FT_t base_systime_as_filetime;
81 unsigned __int64 reset_time;
86 /* Number of 100 nanosecond units from 1/1/1601 to 1/1/1970 */
88 # define Const64(x) x##LL
90 # define Const64(x) x##i64
92 #define EPOCH_BIAS Const64(116444736000000000)
94 /* NOTE: This does not compute the timezone info (doing so can be expensive,
95 * and appears to be unsupported even by glibc) */
97 /* dMY_CXT needs a Perl context and we don't want to call PERL_GET_CONTEXT
98 for performance reasons */
101 #define gettimeofday(tp, not_used) _gettimeofday(aTHX_ tp, not_used)
103 /* If the performance counter delta drifts more than 0.5 seconds from the
104 * system time then we recalibrate to the system time. This means we may
105 * move *backwards* in time! */
106 #define MAX_PERF_COUNTER_SKEW Const64(5000000) /* 0.5 seconds */
108 /* Reset reading from the performance counter every five minutes.
109 * Many PC clocks just seem to be so bad. */
110 #define MAX_PERF_COUNTER_TICKS Const64(300000000) /* 300 seconds */
113 _gettimeofday(pTHX_ struct timeval *tp, void *not_used)
117 unsigned __int64 ticks;
120 if (MY_CXT.run_count++ == 0 ||
121 MY_CXT.base_systime_as_filetime.ft_i64 > MY_CXT.reset_time) {
122 QueryPerformanceFrequency((LARGE_INTEGER*)&MY_CXT.tick_frequency);
123 QueryPerformanceCounter((LARGE_INTEGER*)&MY_CXT.base_ticks);
124 GetSystemTimeAsFileTime(&MY_CXT.base_systime_as_filetime.ft_val);
125 ft.ft_i64 = MY_CXT.base_systime_as_filetime.ft_i64;
126 MY_CXT.reset_time = ft.ft_i64 + MAX_PERF_COUNTER_TICKS;
130 QueryPerformanceCounter((LARGE_INTEGER*)&ticks);
131 ticks -= MY_CXT.base_ticks;
132 ft.ft_i64 = MY_CXT.base_systime_as_filetime.ft_i64
133 + Const64(10000000) * (ticks / MY_CXT.tick_frequency)
134 +(Const64(10000000) * (ticks % MY_CXT.tick_frequency)) / MY_CXT.tick_frequency;
135 diff = ft.ft_i64 - MY_CXT.base_systime_as_filetime.ft_i64;
136 if (diff < -MAX_PERF_COUNTER_SKEW || diff > MAX_PERF_COUNTER_SKEW) {
137 MY_CXT.base_ticks += ticks;
138 GetSystemTimeAsFileTime(&MY_CXT.base_systime_as_filetime.ft_val);
139 ft.ft_i64 = MY_CXT.base_systime_as_filetime.ft_i64;
143 /* seconds since epoch */
144 tp->tv_sec = (long)((ft.ft_i64 - EPOCH_BIAS) / Const64(10000000));
146 /* microseconds remaining */
147 tp->tv_usec = (long)((ft.ft_i64 / Const64(10)) % Const64(1000000));
153 #if defined(WIN32) && !defined(ATLEASTFIVEOHOHFIVE)
155 sleep(unsigned int t)
162 #if !defined(HAS_GETTIMEOFDAY) && defined(VMS)
163 #define HAS_GETTIMEOFDAY
166 #include <time.h> /* gettimeofday */
167 #include <stdlib.h> /* qdiv */
168 #include <starlet.h> /* sys$gettim */
171 #include <lib$routines.h> /* lib$ediv() */
175 VMS binary time is expressed in 100 nano-seconds since
176 system base time which is 17-NOV-1858 00:00:00.00
179 #define DIV_100NS_TO_SECS 10000000L
180 #define DIV_100NS_TO_USECS 10L
183 gettimeofday is supposed to return times since the epoch
184 so need to determine this in terms of VMS base time
186 static $DESCRIPTOR(dscepoch,"01-JAN-1970 00:00:00.00");
189 static long base_adjust[2]={0L,0L};
191 static __int64 base_adjust=0;
196 If we don't have gettimeofday, then likely we are on a VMS machine that
197 operates on local time rather than UTC...so we have to zone-adjust.
198 This code gleefully swiped from VMS.C
201 /* method used to handle UTC conversions:
202 * 1 == CRTL gmtime(); 2 == SYS$TIMEZONE_DIFFERENTIAL; 3 == no correction
204 static int gmtime_emulation_type;
205 /* number of secs to add to UTC POSIX-style time to get local time */
206 static long int utc_offset_secs;
207 static struct dsc$descriptor_s fildevdsc =
208 { 12, DSC$K_DTYPE_T, DSC$K_CLASS_S, "LNM$FILE_DEV" };
209 static struct dsc$descriptor_s *fildev[] = { &fildevdsc, NULL };
211 static time_t toutc_dst(time_t loc) {
214 if ((rsltmp = localtime(&loc)) == NULL) return -1;
215 loc -= utc_offset_secs;
216 if (rsltmp->tm_isdst) loc -= 3600;
220 static time_t toloc_dst(time_t utc) {
223 utc += utc_offset_secs;
224 if ((rsltmp = localtime(&utc)) == NULL) return -1;
225 if (rsltmp->tm_isdst) utc += 3600;
229 #define _toutc(secs) ((secs) == (time_t) -1 ? (time_t) -1 : \
230 ((gmtime_emulation_type || timezone_setup()), \
231 (gmtime_emulation_type == 1 ? toutc_dst(secs) : \
232 ((secs) - utc_offset_secs))))
234 #define _toloc(secs) ((secs) == (time_t) -1 ? (time_t) -1 : \
235 ((gmtime_emulation_type || timezone_setup()), \
236 (gmtime_emulation_type == 1 ? toloc_dst(secs) : \
237 ((secs) + utc_offset_secs))))
244 if (gmtime_emulation_type == 0) {
246 time_t base = 15 * 86400; /* 15jan71; to avoid month/year ends between */
247 /* results of calls to gmtime() and localtime() */
250 gmtime_emulation_type++;
251 if ((tm_p = gmtime(&base)) == NULL) { /* CRTL gmtime() is a fake */
252 char off[LNM$C_NAMLENGTH+1];;
254 gmtime_emulation_type++;
255 if (!Perl_vmstrnenv("SYS$TIMEZONE_DIFFERENTIAL",off,0,fildev,0)) {
256 gmtime_emulation_type++;
258 Perl_warn(aTHX_ "no UTC offset information; assuming local time is UTC");
260 else { utc_offset_secs = atol(off); }
262 else { /* We've got a working gmtime() */
263 struct tm gmt, local;
266 tm_p = localtime(&base);
268 utc_offset_secs = (local.tm_mday - gmt.tm_mday) * 86400;
269 utc_offset_secs += (local.tm_hour - gmt.tm_hour) * 3600;
270 utc_offset_secs += (local.tm_min - gmt.tm_min) * 60;
271 utc_offset_secs += (local.tm_sec - gmt.tm_sec);
279 gettimeofday (struct timeval *tp, void *tpz)
285 long div_100ns_to_secs;
286 long div_100ns_to_usecs;
294 In case of error, tv_usec = 0 and tv_sec = VMS condition code.
295 The return from function is also set to -1.
296 This is not exactly as per the manual page.
302 if (base_adjust[0]==0 && base_adjust[1]==0) {
304 if (base_adjust==0) { /* Need to determine epoch adjustment */
306 ret=sys$bintim(&dscepoch,&base_adjust);
307 if (1 != (ret &&1)) {
313 ret=sys$gettim(&quad); /* Get VMS system time */
314 if ((1 && ret) == 1) {
316 quad[0] -= base_adjust[0]; /* convert to epoch offset */
317 quad[1] -= base_adjust[1]; /* convert 2nd half of quadword */
318 div_100ns_to_secs = DIV_100NS_TO_SECS;
319 div_100ns_to_usecs = DIV_100NS_TO_USECS;
320 lib$ediv(&div_100ns_to_secs,&quad,&quo,&rem);
323 lib$ediv(&div_100ns_to_usecs,&quad1,&quo1,&rem1);
324 tp->tv_sec = quo; /* Whole seconds */
325 tp->tv_usec = quo1; /* Micro-seconds */
327 quad -= base_adjust; /* convert to epoch offset */
328 ans1=qdiv(quad,DIV_100NS_TO_SECS);
329 ans2=qdiv(ans1.rem,DIV_100NS_TO_USECS);
330 tp->tv_sec = ans1.quot; /* Whole seconds */
331 tp->tv_usec = ans2.quot; /* Micro-seconds */
339 if (VMSISH_TIME) tp->tv_sec = _toloc(tp->tv_sec);
341 if (!VMSISH_TIME) tp->tv_sec = _toutc(tp->tv_sec);
349 /* Do not use H A S _ N A N O S L E E P
350 * so that Perl Configure doesn't scan for it.
351 * The TIME_HIRES_NANOSLEEP is set by Makefile.PL. */
352 #if !defined(HAS_USLEEP) && defined(TIME_HIRES_NANOSLEEP)
354 #define usleep hrt_nanosleep /* could conflict with ncurses for static build */
357 hrt_nanosleep(unsigned long usec)
360 res.tv_sec = usec/1000/1000;
361 res.tv_nsec = ( usec - res.tv_sec*1000*1000 ) * 1000;
362 nanosleep(&res, NULL);
367 #if !defined(HAS_USLEEP) && defined(HAS_SELECT)
368 #ifndef SELECT_IS_BROKEN
370 #define usleep hrt_usleep /* could conflict with ncurses for static build */
373 hrt_usleep(unsigned long usec)
378 select(0, (Select_fd_set_t)NULL, (Select_fd_set_t)NULL,
379 (Select_fd_set_t)NULL, &tv);
384 #if !defined(HAS_USLEEP) && defined(WIN32)
386 #define usleep hrt_usleep /* could conflict with ncurses for static build */
389 hrt_usleep(unsigned long usec)
398 #if !defined(HAS_UALARM) && defined(HAS_SETITIMER)
400 #define ualarm hrt_ualarm /* could conflict with ncurses for static build */
403 hrt_ualarm(int usec, int interval)
405 struct itimerval itv;
406 itv.it_value.tv_sec = usec / 1000000;
407 itv.it_value.tv_usec = usec % 1000000;
408 itv.it_interval.tv_sec = interval / 1000000;
409 itv.it_interval.tv_usec = interval % 1000000;
410 return setitimer(ITIMER_REAL, &itv, 0);
414 #if !defined(HAS_UALARM) && defined(VMS)
416 #define ualarm vms_ualarm
418 #include <lib$routines.h>
426 #define VMSERR(s) (!((s)&1))
429 us_to_VMS(useconds_t mseconds, unsigned long v[])
438 iss = lib$addx(qq,qq,qq);
439 if (VMSERR(iss)) lib$signal(iss);
440 iss = lib$subx(v,qq,v);
441 if (VMSERR(iss)) lib$signal(iss);
442 iss = lib$addx(qq,qq,qq);
443 if (VMSERR(iss)) lib$signal(iss);
444 iss = lib$subx(v,qq,v);
445 if (VMSERR(iss)) lib$signal(iss);
446 iss = lib$subx(v,qq,v);
447 if (VMSERR(iss)) lib$signal(iss);
451 VMS_to_us(unsigned long v[])
454 unsigned long div=10,quot, rem;
456 iss = lib$ediv(&div,v,",&rem);
457 if (VMSERR(iss)) lib$signal(iss);
462 typedef unsigned short word;
463 typedef struct _ualarm {
466 unsigned long delay[2];
467 unsigned long interval[2];
468 unsigned long remain[2];
473 static Alarm *a0, alarm_base;
478 static void ualarm_AST(Alarm *a);
481 vms_ualarm(int mseconds, int interval)
490 static struct item_list3 itmlst[2];
491 static int first = 1;
497 itmlst[0].code = JPI$_ASTEN;
498 itmlst[0].length = sizeof(asten);
499 itmlst[0].retlenaddr = NULL;
501 itmlst[1].length = 0;
502 itmlst[1].bufaddr = NULL;
503 itmlst[1].retlenaddr = NULL;
505 iss = lib$get_ef(&alarm_ef);
506 if (VMSERR(iss)) lib$signal(iss);
509 a0->function = UAL_NULL;
511 itmlst[0].bufaddr = &asten;
513 iss = sys$getjpiw(0,0,0,itmlst,0,0,0);
514 if (VMSERR(iss)) lib$signal(iss);
515 if (!(asten&0x08)) return -1;
519 a->function = UAL_SET;
521 a->function = UAL_CLEAR;
524 us_to_VMS(mseconds, a->delay);
526 us_to_VMS(interval, a->interval);
531 iss = sys$clref(alarm_ef);
532 if (VMSERR(iss)) lib$signal(iss);
534 iss = sys$dclast(ualarm_AST,a,0);
535 if (VMSERR(iss)) lib$signal(iss);
537 iss = sys$waitfr(alarm_ef);
538 if (VMSERR(iss)) lib$signal(iss);
540 if (a->function == UAL_ACTIVE)
541 return VMS_to_us(a->remain);
552 unsigned long now[2];
554 iss = sys$gettim(now);
555 if (VMSERR(iss)) lib$signal(iss);
557 if (a->function == UAL_SET || a->function == UAL_CLEAR) {
558 if (a0->function == UAL_ACTIVE) {
559 iss = sys$cantim(a0,PSL$C_USER);
560 if (VMSERR(iss)) lib$signal(iss);
562 iss = lib$subx(a0->remain, now, a->remain);
563 if (VMSERR(iss)) lib$signal(iss);
565 if (a->remain[1] & 0x80000000)
566 a->remain[0] = a->remain[1] = 0;
569 if (a->function == UAL_SET) {
570 a->function = a0->function;
571 a0->function = UAL_ACTIVE;
572 a0->repeat = a->repeat;
574 a0->interval[0] = a->interval[0];
575 a0->interval[1] = a->interval[1];
577 a0->delay[0] = a->delay[0];
578 a0->delay[1] = a->delay[1];
580 iss = lib$subx(now, a0->delay, a0->remain);
581 if (VMSERR(iss)) lib$signal(iss);
583 iss = sys$setimr(0,a0->delay,ualarm_AST,a0);
584 if (VMSERR(iss)) lib$signal(iss);
586 a->function = a0->function;
587 a0->function = UAL_NULL;
589 iss = sys$setef(alarm_ef);
590 if (VMSERR(iss)) lib$signal(iss);
591 } else if (a->function == UAL_ACTIVE) {
593 iss = lib$subx(now, a->interval, a->remain);
594 if (VMSERR(iss)) lib$signal(iss);
596 iss = sys$setimr(0,a->interval,ualarm_AST,a);
597 if (VMSERR(iss)) lib$signal(iss);
599 a->function = UAL_NULL;
602 if (VMSERR(iss)) lib$signal(iss);
603 lib$signal(SS$_ASTFLT);
605 lib$signal(SS$_BADPARAM);
609 #endif /* !HAS_UALARM && VMS */
611 #ifdef HAS_GETTIMEOFDAY
614 myU2time(pTHX_ UV *ret)
618 status = gettimeofday (&Tp, NULL);
632 status = gettimeofday (&Tp, NULL);
633 return status == 0 ? Tp.tv_sec + (Tp.tv_usec / 1000000.) : -1.0;
638 MODULE = Time::HiRes PACKAGE = Time::HiRes
647 #ifdef ATLEASTFIVEOHOHFIVE
648 #ifdef HAS_GETTIMEOFDAY
651 hv_store(PL_modglobal, "Time::NVtime", 12, newSViv(PTR2IV(myNVtime)), 0);
652 if (myU2time(aTHX_ auv) == 0)
653 hv_store(PL_modglobal, "Time::U2time", 12, newSViv((IV) auv[0]), 0);
659 #if defined(USE_ITHREADS) && defined(MY_CXT_KEY)
668 INCLUDE: const-xs.inc
670 #if defined(HAS_USLEEP) && defined(HAS_GETTIMEOFDAY)
676 struct timeval Ta, Tb;
678 gettimeofday(&Ta, NULL);
680 if (useconds > 1E6) {
681 IV seconds = (IV) (useconds / 1E6);
682 /* If usleep() has been implemented using setitimer()
683 * then this contortion is unnecessary-- but usleep()
684 * may be implemented in some other way, so let's contort. */
687 useconds -= 1E6 * seconds;
689 } else if (useconds < 0.0)
690 croak("Time::HiRes::usleep(%"NVgf"): negative time not invented yet", useconds);
691 usleep((U32)useconds);
694 gettimeofday(&Tb, NULL);
696 printf("[%ld %ld] [%ld %ld]\n", Tb.tv_sec, Tb.tv_usec, Ta.tv_sec, Ta.tv_usec);
698 RETVAL = 1E6*(Tb.tv_sec-Ta.tv_sec)+(NV)((IV)Tb.tv_usec-(IV)Ta.tv_usec);
706 struct timeval Ta, Tb;
708 gettimeofday(&Ta, NULL);
710 NV seconds = SvNV(ST(0));
711 if (seconds >= 0.0) {
712 UV useconds = (UV)(1E6 * (seconds - (UV)seconds));
715 if ((IV)useconds < 0) {
716 #if defined(__sparc64__) && defined(__GNUC__)
717 /* Sparc64 gcc 2.95.3 (e.g. on NetBSD) has a bug
718 * where (0.5 - (UV)(0.5)) will under certain
719 * circumstances (if the double is cast to UV more
720 * than once?) evaluate to -0.5, instead of 0.5. */
721 useconds = -(IV)useconds;
723 if ((IV)useconds < 0)
724 croak("Time::HiRes::sleep(%"NVgf"): internal error: useconds < 0 (unsigned %"UVuf" signed %"IVdf")", seconds, useconds, (IV)useconds);
728 croak("Time::HiRes::sleep(%"NVgf"): negative time not invented yet", seconds);
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 = (NV)(Tb.tv_sec-Ta.tv_sec)+0.000001*(NV)(Tb.tv_usec-Ta.tv_usec);
745 ualarm(useconds,interval=0)
749 if (useconds < 0 || interval < 0)
750 croak("Time::HiRes::ualarm(%d, %d): negative time not invented yet", useconds, interval);
751 RETVAL = ualarm(useconds, interval);
757 alarm(seconds,interval=0)
761 if (seconds < 0.0 || interval < 0.0)
762 croak("Time::HiRes::alarm(%"NVgf", %"NVgf"): negative time not invented yet", seconds, interval);
763 RETVAL = (NV)ualarm(seconds * 1000000,
764 interval * 1000000) / 1E6;
771 #ifdef HAS_GETTIMEOFDAY
772 # ifdef MACOS_TRADITIONAL /* fix epoch TZ and use unsigned time_t */
780 status = gettimeofday (&Tp, &Tz);
781 Tp.tv_sec += Tz.tz_minuteswest * 60; /* adjust for TZ */
783 if (GIMME == G_ARRAY) {
785 /* Mac OS (Classic) has unsigned time_t */
786 PUSHs(sv_2mortal(newSVuv(Tp.tv_sec)));
787 PUSHs(sv_2mortal(newSViv(Tp.tv_usec)));
790 PUSHs(sv_2mortal(newSVnv(Tp.tv_sec + (Tp.tv_usec / 1000000.0))));
800 status = gettimeofday (&Tp, &Tz);
801 Tp.tv_sec += Tz.tz_minuteswest * 60; /* adjust for TZ */
802 RETVAL = Tp.tv_sec + (Tp.tv_usec / 1000000.0);
806 # else /* MACOS_TRADITIONAL */
813 status = gettimeofday (&Tp, NULL);
814 if (GIMME == G_ARRAY) {
816 PUSHs(sv_2mortal(newSViv(Tp.tv_sec)));
817 PUSHs(sv_2mortal(newSViv(Tp.tv_usec)));
820 PUSHs(sv_2mortal(newSVnv(Tp.tv_sec + (Tp.tv_usec / 1000000.0))));
829 status = gettimeofday (&Tp, NULL);
830 RETVAL = Tp.tv_sec + (Tp.tv_usec / 1000000.);
834 # endif /* MACOS_TRADITIONAL */
837 #if defined(HAS_GETITIMER) && defined(HAS_SETITIMER)
839 #define TV2NV(tv) ((NV)((tv).tv_sec) + 0.000001 * (NV)((tv).tv_usec))
842 setitimer(which, seconds, interval = 0)
847 struct itimerval newit;
848 struct itimerval oldit;
850 if (seconds < 0.0 || interval < 0.0)
851 croak("Time::HiRes::setitimer(%"IVdf", %"NVgf", %"NVgf"): negative time not invented yet", (IV)which, seconds, interval);
852 newit.it_value.tv_sec = seconds;
853 newit.it_value.tv_usec =
854 (seconds - (NV)newit.it_value.tv_sec) * 1000000.0;
855 newit.it_interval.tv_sec = interval;
856 newit.it_interval.tv_usec =
857 (interval - (NV)newit.it_interval.tv_sec) * 1000000.0;
858 if (setitimer(which, &newit, &oldit) == 0) {
860 PUSHs(sv_2mortal(newSVnv(TV2NV(oldit.it_value))));
861 if (GIMME == G_ARRAY) {
863 PUSHs(sv_2mortal(newSVnv(TV2NV(oldit.it_interval))));
871 struct itimerval nowit;
873 if (getitimer(which, &nowit) == 0) {
875 PUSHs(sv_2mortal(newSVnv(TV2NV(nowit.it_value))));
876 if (GIMME == G_ARRAY) {
878 PUSHs(sv_2mortal(newSVnv(TV2NV(nowit.it_interval))));