Upgrade to Time-HiRes-1.76
[p5sagit/p5-mst-13.2.git] / ext / Time / HiRes / HiRes.xs
CommitLineData
f8daf111 1/*
2 *
3 * Copyright (c) 1996-2002 Douglas E. Wegscheid. All rights reserved.
4 *
5 * Copyright (c) 2002,2003,2004,2005 Jarkko Hietaniemi. All rights reserved.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the same terms as Perl itself.
9 */
10
dcf686c9 11#ifdef __cplusplus
12extern "C" {
13#endif
0225372c 14#define PERL_NO_GET_CONTEXT
dcf686c9 15#include "EXTERN.h"
16#include "perl.h"
17#include "XSUB.h"
1fbb4de4 18#include "ppport.h"
4ed0e2d4 19#if defined(__CYGWIN__) && defined(HAS_W32API_WINDOWS_H)
20# include <w32api/windows.h>
21# define CYGWIN_WITH_W32API
22#endif
dcf686c9 23#ifdef WIN32
4ed0e2d4 24# include <time.h>
dcf686c9 25#else
4ed0e2d4 26# include <sys/time.h>
dcf686c9 27#endif
36df99d6 28#ifdef HAS_SELECT
29# ifdef I_SYS_SELECT
30# include <sys/select.h>
31# endif
32#endif
dcf686c9 33#ifdef __cplusplus
34}
35#endif
36
1fbb4de4 37#ifndef PerlProc_pause
38# define PerlProc_pause() Pause()
3f2ee006 39#endif
40
1fbb4de4 41#ifdef HAS_PAUSE
42# define Pause pause
3f2ee006 43#else
27da23d5 44# undef Pause /* In case perl.h did it already. */
64a7a97c 45# define Pause() sleep(~0) /* Zzz for a long time. */
3f2ee006 46#endif
47
48/* Though the cpp define ITIMER_VIRTUAL is available the functionality
4ed0e2d4 49 * is not supported in Cygwin as of August 2004, ditto for Win32.
3f2ee006 50 * Neither are ITIMER_PROF or ITIMER_REALPROF implemented. --jhi
51 */
52#if defined(__CYGWIN__) || defined(WIN32)
53# undef ITIMER_VIRTUAL
54# undef ITIMER_PROF
55# undef ITIMER_REALPROF
56#endif
57
98b50af3 58/* 5.004 doesn't define PL_sv_undef */
59#ifndef ATLEASTFIVEOHOHFIVE
4ed0e2d4 60# ifndef PL_sv_undef
61# define PL_sv_undef sv_undef
62# endif
3c72ec00 63#endif
3c72ec00 64
98b50af3 65#include "const-c.inc"
3c72ec00 66
4ed0e2d4 67#if defined(WIN32) || defined(CYGWIN_WITH_W32API)
0225372c 68
69#ifndef HAS_GETTIMEOFDAY
70# define HAS_GETTIMEOFDAY
71#endif
fd44fdfd 72
6e3b076d 73/* shows up in winsock.h?
74struct timeval {
75 long tv_sec;
76 long tv_usec;
77}
78*/
79
fd44fdfd 80typedef union {
81 unsigned __int64 ft_i64;
82 FILETIME ft_val;
83} FT_t;
84
0225372c 85#define MY_CXT_KEY "Time::HiRes_" XS_VERSION
86
87typedef struct {
88 unsigned long run_count;
89 unsigned __int64 base_ticks;
90 unsigned __int64 tick_frequency;
91 FT_t base_systime_as_filetime;
4ed0e2d4 92 unsigned __int64 reset_time;
0225372c 93} my_cxt_t;
94
95START_MY_CXT
96
6e3b076d 97/* Number of 100 nanosecond units from 1/1/1601 to 1/1/1970 */
fd44fdfd 98#ifdef __GNUC__
4ed0e2d4 99# define Const64(x) x##LL
fd44fdfd 100#else
4ed0e2d4 101# define Const64(x) x##i64
fd44fdfd 102#endif
fd44fdfd 103#define EPOCH_BIAS Const64(116444736000000000)
104
105/* NOTE: This does not compute the timezone info (doing so can be expensive,
106 * and appears to be unsupported even by glibc) */
0225372c 107
108/* dMY_CXT needs a Perl context and we don't want to call PERL_GET_CONTEXT
109 for performance reasons */
110
111#undef gettimeofday
112#define gettimeofday(tp, not_used) _gettimeofday(aTHX_ tp, not_used)
113
d8cb5b61 114/* If the performance counter delta drifts more than 0.5 seconds from the
115 * system time then we recalibrate to the system time. This means we may
c1dc6e7c 116 * move *backwards* in time! */
4ed0e2d4 117#define MAX_PERF_COUNTER_SKEW Const64(5000000) /* 0.5 seconds */
c1dc6e7c 118
4ed0e2d4 119/* Reset reading from the performance counter every five minutes.
120 * Many PC clocks just seem to be so bad. */
121#define MAX_PERF_COUNTER_TICKS Const64(300000000) /* 300 seconds */
c1dc6e7c 122
0225372c 123static int
124_gettimeofday(pTHX_ struct timeval *tp, void *not_used)
fd44fdfd 125{
0225372c 126 dMY_CXT;
127
128 unsigned __int64 ticks;
fd44fdfd 129 FT_t ft;
130
4ed0e2d4 131 if (MY_CXT.run_count++ == 0 ||
132 MY_CXT.base_systime_as_filetime.ft_i64 > MY_CXT.reset_time) {
133 QueryPerformanceFrequency((LARGE_INTEGER*)&MY_CXT.tick_frequency);
134 QueryPerformanceCounter((LARGE_INTEGER*)&MY_CXT.base_ticks);
135 GetSystemTimeAsFileTime(&MY_CXT.base_systime_as_filetime.ft_val);
136 ft.ft_i64 = MY_CXT.base_systime_as_filetime.ft_i64;
137 MY_CXT.reset_time = ft.ft_i64 + MAX_PERF_COUNTER_TICKS;
138 }
139 else {
c1dc6e7c 140 __int64 diff;
0225372c 141 QueryPerformanceCounter((LARGE_INTEGER*)&ticks);
142 ticks -= MY_CXT.base_ticks;
143 ft.ft_i64 = MY_CXT.base_systime_as_filetime.ft_i64
b6136f41 144 + Const64(10000000) * (ticks / MY_CXT.tick_frequency)
145 +(Const64(10000000) * (ticks % MY_CXT.tick_frequency)) / MY_CXT.tick_frequency;
c1dc6e7c 146 diff = ft.ft_i64 - MY_CXT.base_systime_as_filetime.ft_i64;
4ed0e2d4 147 if (diff < -MAX_PERF_COUNTER_SKEW || diff > MAX_PERF_COUNTER_SKEW) {
148 MY_CXT.base_ticks += ticks;
149 GetSystemTimeAsFileTime(&MY_CXT.base_systime_as_filetime.ft_val);
150 ft.ft_i64 = MY_CXT.base_systime_as_filetime.ft_i64;
c1dc6e7c 151 }
0225372c 152 }
fd44fdfd 153
154 /* seconds since epoch */
155 tp->tv_sec = (long)((ft.ft_i64 - EPOCH_BIAS) / Const64(10000000));
156
157 /* microseconds remaining */
158 tp->tv_usec = (long)((ft.ft_i64 / Const64(10)) % Const64(1000000));
159
160 return 0;
161}
6e3b076d 162#endif
fd44fdfd 163
0225372c 164#if defined(WIN32) && !defined(ATLEASTFIVEOHOHFIVE)
165static unsigned int
166sleep(unsigned int t)
167{
168 Sleep(t*1000);
169 return 0;
170}
171#endif
172
dcf686c9 173#if !defined(HAS_GETTIMEOFDAY) && defined(VMS)
174#define HAS_GETTIMEOFDAY
175
9b6f56ad 176#include <lnmdef.h>
dcf686c9 177#include <time.h> /* gettimeofday */
178#include <stdlib.h> /* qdiv */
179#include <starlet.h> /* sys$gettim */
180#include <descrip.h>
3785778e 181#ifdef __VAX
182#include <lib$routines.h> /* lib$ediv() */
183#endif
dcf686c9 184
185/*
186 VMS binary time is expressed in 100 nano-seconds since
187 system base time which is 17-NOV-1858 00:00:00.00
188*/
189
190#define DIV_100NS_TO_SECS 10000000L
191#define DIV_100NS_TO_USECS 10L
192
193/*
194 gettimeofday is supposed to return times since the epoch
195 so need to determine this in terms of VMS base time
196*/
197static $DESCRIPTOR(dscepoch,"01-JAN-1970 00:00:00.00");
198
5cdb7193 199#ifdef __VAX
3785778e 200static long base_adjust[2]={0L,0L};
5cdb7193 201#else
dcf686c9 202static __int64 base_adjust=0;
5cdb7193 203#endif
dcf686c9 204
9b6f56ad 205/*
206
207 If we don't have gettimeofday, then likely we are on a VMS machine that
208 operates on local time rather than UTC...so we have to zone-adjust.
209 This code gleefully swiped from VMS.C
210
211*/
212/* method used to handle UTC conversions:
213 * 1 == CRTL gmtime(); 2 == SYS$TIMEZONE_DIFFERENTIAL; 3 == no correction
214 */
215static int gmtime_emulation_type;
216/* number of secs to add to UTC POSIX-style time to get local time */
217static long int utc_offset_secs;
218static struct dsc$descriptor_s fildevdsc =
219 { 12, DSC$K_DTYPE_T, DSC$K_CLASS_S, "LNM$FILE_DEV" };
220static struct dsc$descriptor_s *fildev[] = { &fildevdsc, NULL };
221
222static time_t toutc_dst(time_t loc) {
223 struct tm *rsltmp;
224
225 if ((rsltmp = localtime(&loc)) == NULL) return -1;
226 loc -= utc_offset_secs;
227 if (rsltmp->tm_isdst) loc -= 3600;
228 return loc;
229}
230
231static time_t toloc_dst(time_t utc) {
232 struct tm *rsltmp;
233
234 utc += utc_offset_secs;
235 if ((rsltmp = localtime(&utc)) == NULL) return -1;
236 if (rsltmp->tm_isdst) utc += 3600;
237 return utc;
238}
239
240#define _toutc(secs) ((secs) == (time_t) -1 ? (time_t) -1 : \
241 ((gmtime_emulation_type || timezone_setup()), \
242 (gmtime_emulation_type == 1 ? toutc_dst(secs) : \
243 ((secs) - utc_offset_secs))))
244
245#define _toloc(secs) ((secs) == (time_t) -1 ? (time_t) -1 : \
246 ((gmtime_emulation_type || timezone_setup()), \
247 (gmtime_emulation_type == 1 ? toloc_dst(secs) : \
248 ((secs) + utc_offset_secs))))
249
250static int
251timezone_setup(void)
252{
253 struct tm *tm_p;
254
255 if (gmtime_emulation_type == 0) {
256 int dstnow;
257 time_t base = 15 * 86400; /* 15jan71; to avoid month/year ends between */
258 /* results of calls to gmtime() and localtime() */
259 /* for same &base */
260
261 gmtime_emulation_type++;
262 if ((tm_p = gmtime(&base)) == NULL) { /* CRTL gmtime() is a fake */
263 char off[LNM$C_NAMLENGTH+1];;
264
265 gmtime_emulation_type++;
266 if (!Perl_vmstrnenv("SYS$TIMEZONE_DIFFERENTIAL",off,0,fildev,0)) {
267 gmtime_emulation_type++;
268 utc_offset_secs = 0;
269 Perl_warn(aTHX_ "no UTC offset information; assuming local time is UTC");
270 }
271 else { utc_offset_secs = atol(off); }
272 }
273 else { /* We've got a working gmtime() */
274 struct tm gmt, local;
275
276 gmt = *tm_p;
277 tm_p = localtime(&base);
278 local = *tm_p;
279 utc_offset_secs = (local.tm_mday - gmt.tm_mday) * 86400;
280 utc_offset_secs += (local.tm_hour - gmt.tm_hour) * 3600;
281 utc_offset_secs += (local.tm_min - gmt.tm_min) * 60;
282 utc_offset_secs += (local.tm_sec - gmt.tm_sec);
283 }
284 }
285 return 1;
286}
287
288
dcf686c9 289int
290gettimeofday (struct timeval *tp, void *tpz)
291{
292 long ret;
5cdb7193 293#ifdef __VAX
3785778e 294 long quad[2];
295 long quad1[2];
296 long div_100ns_to_secs;
297 long div_100ns_to_usecs;
298 long quo,rem;
299 long quo1,rem1;
5cdb7193 300#else
dcf686c9 301 __int64 quad;
302 __qdiv_t ans1,ans2;
5cdb7193 303#endif
dcf686c9 304/*
305 In case of error, tv_usec = 0 and tv_sec = VMS condition code.
306 The return from function is also set to -1.
307 This is not exactly as per the manual page.
308*/
309
310 tp->tv_usec = 0;
311
3785778e 312#ifdef __VAX
313 if (base_adjust[0]==0 && base_adjust[1]==0) {
314#else
dcf686c9 315 if (base_adjust==0) { /* Need to determine epoch adjustment */
3785778e 316#endif
dcf686c9 317 ret=sys$bintim(&dscepoch,&base_adjust);
318 if (1 != (ret &&1)) {
319 tp->tv_sec = ret;
320 return -1;
321 }
322 }
323
324 ret=sys$gettim(&quad); /* Get VMS system time */
325 if ((1 && ret) == 1) {
5cdb7193 326#ifdef __VAX
3785778e 327 quad[0] -= base_adjust[0]; /* convert to epoch offset */
328 quad[1] -= base_adjust[1]; /* convert 2nd half of quadword */
329 div_100ns_to_secs = DIV_100NS_TO_SECS;
330 div_100ns_to_usecs = DIV_100NS_TO_USECS;
331 lib$ediv(&div_100ns_to_secs,&quad,&quo,&rem);
332 quad1[0] = rem;
333 quad1[1] = 0L;
334 lib$ediv(&div_100ns_to_usecs,&quad1,&quo1,&rem1);
335 tp->tv_sec = quo; /* Whole seconds */
336 tp->tv_usec = quo1; /* Micro-seconds */
5cdb7193 337#else
3785778e 338 quad -= base_adjust; /* convert to epoch offset */
dcf686c9 339 ans1=qdiv(quad,DIV_100NS_TO_SECS);
340 ans2=qdiv(ans1.rem,DIV_100NS_TO_USECS);
341 tp->tv_sec = ans1.quot; /* Whole seconds */
342 tp->tv_usec = ans2.quot; /* Micro-seconds */
3785778e 343#endif
dcf686c9 344 } else {
345 tp->tv_sec = ret;
346 return -1;
347 }
9b6f56ad 348# ifdef VMSISH_TIME
349# ifdef RTL_USES_UTC
350 if (VMSISH_TIME) tp->tv_sec = _toloc(tp->tv_sec);
351# else
352 if (!VMSISH_TIME) tp->tv_sec = _toutc(tp->tv_sec);
353# endif
354# endif
dcf686c9 355 return 0;
356}
357#endif
358
3f2ee006 359
046e3f33 360 /* Do not use H A S _ N A N O S L E E P
361 * so that Perl Configure doesn't scan for it.
362 * The TIME_HIRES_NANOSLEEP is set by Makefile.PL. */
363#if !defined(HAS_USLEEP) && defined(TIME_HIRES_NANOSLEEP)
3f2ee006 364#define HAS_USLEEP
3d0346a5 365#define usleep hrt_nanosleep /* could conflict with ncurses for static build */
3f2ee006 366
367void
3d0346a5 368hrt_nanosleep(unsigned long usec) /* This is used to emulate usleep. */
3f2ee006 369{
370 struct timespec res;
371 res.tv_sec = usec/1000/1000;
372 res.tv_nsec = ( usec - res.tv_sec*1000*1000 ) * 1000;
373 nanosleep(&res, NULL);
374}
3f2ee006 375
44d3ce20 376#endif /* #if !defined(HAS_USLEEP) && defined(TIME_HIRES_NANOSLEEP) */
3f2ee006 377
dcf686c9 378#if !defined(HAS_USLEEP) && defined(HAS_SELECT)
379#ifndef SELECT_IS_BROKEN
380#define HAS_USLEEP
381#define usleep hrt_usleep /* could conflict with ncurses for static build */
382
383void
384hrt_usleep(unsigned long usec)
385{
386 struct timeval tv;
387 tv.tv_sec = 0;
388 tv.tv_usec = usec;
389 select(0, (Select_fd_set_t)NULL, (Select_fd_set_t)NULL,
390 (Select_fd_set_t)NULL, &tv);
391}
392#endif
44d3ce20 393#endif /* #if !defined(HAS_USLEEP) && defined(HAS_SELECT) */
dcf686c9 394
395#if !defined(HAS_USLEEP) && defined(WIN32)
396#define HAS_USLEEP
397#define usleep hrt_usleep /* could conflict with ncurses for static build */
398
399void
400hrt_usleep(unsigned long usec)
401{
402 long msec;
403 msec = usec / 1000;
404 Sleep (msec);
405}
44d3ce20 406#endif /* #if !defined(HAS_USLEEP) && defined(WIN32) */
dcf686c9 407
408
409#if !defined(HAS_UALARM) && defined(HAS_SETITIMER)
410#define HAS_UALARM
411#define ualarm hrt_ualarm /* could conflict with ncurses for static build */
412
413int
414hrt_ualarm(int usec, int interval)
415{
416 struct itimerval itv;
417 itv.it_value.tv_sec = usec / 1000000;
418 itv.it_value.tv_usec = usec % 1000000;
419 itv.it_interval.tv_sec = interval / 1000000;
420 itv.it_interval.tv_usec = interval % 1000000;
421 return setitimer(ITIMER_REAL, &itv, 0);
422}
44d3ce20 423#endif /* #if !defined(HAS_UALARM) && defined(HAS_SETITIMER) */
dcf686c9 424
ca40fe49 425#if !defined(HAS_UALARM) && defined(VMS)
426#define HAS_UALARM
427#define ualarm vms_ualarm
428
429#include <lib$routines.h>
430#include <ssdef.h>
431#include <starlet.h>
432#include <descrip.h>
433#include <signal.h>
434#include <jpidef.h>
435#include <psldef.h>
436
437#define VMSERR(s) (!((s)&1))
438
439static void
440us_to_VMS(useconds_t mseconds, unsigned long v[])
441{
442 int iss;
443 unsigned long qq[2];
444
445 qq[0] = mseconds;
446 qq[1] = 0;
447 v[0] = v[1] = 0;
448
449 iss = lib$addx(qq,qq,qq);
450 if (VMSERR(iss)) lib$signal(iss);
451 iss = lib$subx(v,qq,v);
452 if (VMSERR(iss)) lib$signal(iss);
453 iss = lib$addx(qq,qq,qq);
454 if (VMSERR(iss)) lib$signal(iss);
455 iss = lib$subx(v,qq,v);
456 if (VMSERR(iss)) lib$signal(iss);
457 iss = lib$subx(v,qq,v);
458 if (VMSERR(iss)) lib$signal(iss);
459}
460
461static int
462VMS_to_us(unsigned long v[])
463{
464 int iss;
465 unsigned long div=10,quot, rem;
466
467 iss = lib$ediv(&div,v,&quot,&rem);
468 if (VMSERR(iss)) lib$signal(iss);
469
470 return quot;
471}
472
473typedef unsigned short word;
474typedef struct _ualarm {
475 int function;
476 int repeat;
477 unsigned long delay[2];
478 unsigned long interval[2];
479 unsigned long remain[2];
480} Alarm;
481
482
483static int alarm_ef;
484static Alarm *a0, alarm_base;
485#define UAL_NULL 0
486#define UAL_SET 1
487#define UAL_CLEAR 2
488#define UAL_ACTIVE 4
489static void ualarm_AST(Alarm *a);
490
491static int
492vms_ualarm(int mseconds, int interval)
493{
494 Alarm *a, abase;
495 struct item_list3 {
496 word length;
497 word code;
498 void *bufaddr;
499 void *retlenaddr;
500 } ;
501 static struct item_list3 itmlst[2];
502 static int first = 1;
503 unsigned long asten;
504 int iss, enabled;
505
506 if (first) {
507 first = 0;
508 itmlst[0].code = JPI$_ASTEN;
509 itmlst[0].length = sizeof(asten);
510 itmlst[0].retlenaddr = NULL;
511 itmlst[1].code = 0;
512 itmlst[1].length = 0;
513 itmlst[1].bufaddr = NULL;
514 itmlst[1].retlenaddr = NULL;
515
516 iss = lib$get_ef(&alarm_ef);
517 if (VMSERR(iss)) lib$signal(iss);
518
519 a0 = &alarm_base;
520 a0->function = UAL_NULL;
521 }
522 itmlst[0].bufaddr = &asten;
523
524 iss = sys$getjpiw(0,0,0,itmlst,0,0,0);
525 if (VMSERR(iss)) lib$signal(iss);
526 if (!(asten&0x08)) return -1;
527
528 a = &abase;
529 if (mseconds) {
530 a->function = UAL_SET;
531 } else {
532 a->function = UAL_CLEAR;
533 }
534
535 us_to_VMS(mseconds, a->delay);
536 if (interval) {
537 us_to_VMS(interval, a->interval);
538 a->repeat = 1;
539 } else
540 a->repeat = 0;
541
542 iss = sys$clref(alarm_ef);
543 if (VMSERR(iss)) lib$signal(iss);
544
545 iss = sys$dclast(ualarm_AST,a,0);
546 if (VMSERR(iss)) lib$signal(iss);
547
548 iss = sys$waitfr(alarm_ef);
549 if (VMSERR(iss)) lib$signal(iss);
550
551 if (a->function == UAL_ACTIVE)
552 return VMS_to_us(a->remain);
553 else
554 return 0;
555}
556
557
558
559static void
560ualarm_AST(Alarm *a)
561{
562 int iss;
563 unsigned long now[2];
564
565 iss = sys$gettim(now);
566 if (VMSERR(iss)) lib$signal(iss);
567
568 if (a->function == UAL_SET || a->function == UAL_CLEAR) {
569 if (a0->function == UAL_ACTIVE) {
570 iss = sys$cantim(a0,PSL$C_USER);
571 if (VMSERR(iss)) lib$signal(iss);
572
573 iss = lib$subx(a0->remain, now, a->remain);
574 if (VMSERR(iss)) lib$signal(iss);
575
576 if (a->remain[1] & 0x80000000)
577 a->remain[0] = a->remain[1] = 0;
578 }
579
580 if (a->function == UAL_SET) {
581 a->function = a0->function;
582 a0->function = UAL_ACTIVE;
583 a0->repeat = a->repeat;
584 if (a0->repeat) {
585 a0->interval[0] = a->interval[0];
586 a0->interval[1] = a->interval[1];
587 }
588 a0->delay[0] = a->delay[0];
589 a0->delay[1] = a->delay[1];
590
591 iss = lib$subx(now, a0->delay, a0->remain);
592 if (VMSERR(iss)) lib$signal(iss);
593
594 iss = sys$setimr(0,a0->delay,ualarm_AST,a0);
595 if (VMSERR(iss)) lib$signal(iss);
596 } else {
597 a->function = a0->function;
598 a0->function = UAL_NULL;
599 }
600 iss = sys$setef(alarm_ef);
601 if (VMSERR(iss)) lib$signal(iss);
602 } else if (a->function == UAL_ACTIVE) {
603 if (a->repeat) {
604 iss = lib$subx(now, a->interval, a->remain);
605 if (VMSERR(iss)) lib$signal(iss);
606
607 iss = sys$setimr(0,a->interval,ualarm_AST,a);
608 if (VMSERR(iss)) lib$signal(iss);
609 } else {
610 a->function = UAL_NULL;
611 }
612 iss = sys$wake(0,0);
613 if (VMSERR(iss)) lib$signal(iss);
614 lib$signal(SS$_ASTFLT);
615 } else {
616 lib$signal(SS$_BADPARAM);
617 }
618}
619
44d3ce20 620#endif /* #if !defined(HAS_UALARM) && defined(VMS) */
ca40fe49 621
dcf686c9 622#ifdef HAS_GETTIMEOFDAY
623
a2e20b18 624static int
0225372c 625myU2time(pTHX_ UV *ret)
dcf686c9 626{
627 struct timeval Tp;
628 int status;
6e3b076d 629 status = gettimeofday (&Tp, NULL);
dcf686c9 630 ret[0] = Tp.tv_sec;
631 ret[1] = Tp.tv_usec;
a2e20b18 632 return status;
dcf686c9 633}
634
3c72ec00 635static NV
dcf686c9 636myNVtime()
637{
0225372c 638#ifdef WIN32
4ed0e2d4 639 dTHX;
0225372c 640#endif
dcf686c9 641 struct timeval Tp;
642 int status;
6e3b076d 643 status = gettimeofday (&Tp, NULL);
a2e20b18 644 return status == 0 ? Tp.tv_sec + (Tp.tv_usec / 1000000.) : -1.0;
dcf686c9 645}
646
44d3ce20 647#endif /* #ifdef HAS_GETTIMEOFDAY */
dcf686c9 648
649MODULE = Time::HiRes PACKAGE = Time::HiRes
650
651PROTOTYPES: ENABLE
652
653BOOT:
0225372c 654{
655#ifdef MY_CXT_KEY
656 MY_CXT_INIT;
657#endif
3f2ee006 658#ifdef ATLEASTFIVEOHOHFIVE
dcf686c9 659#ifdef HAS_GETTIMEOFDAY
0225372c 660 {
0225372c 661 hv_store(PL_modglobal, "Time::NVtime", 12, newSViv(PTR2IV(myNVtime)), 0);
06252d99 662 hv_store(PL_modglobal, "Time::U2time", 12, newSViv(PTR2IV(myU2time)), 0);
0225372c 663 }
664#endif
dcf686c9 665#endif
0225372c 666}
667
668#if defined(USE_ITHREADS) && defined(MY_CXT_KEY)
669
670void
671CLONE(...)
672 CODE:
673 MY_CXT_CLONE;
674
3f2ee006 675#endif
dcf686c9 676
98b50af3 677INCLUDE: const-xs.inc
3c72ec00 678
52d72fba 679#if defined(HAS_USLEEP) && defined(HAS_GETTIMEOFDAY)
dcf686c9 680
92bc48ca 681NV
dcf686c9 682usleep(useconds)
92bc48ca 683 NV useconds
52d72fba 684 PREINIT:
685 struct timeval Ta, Tb;
686 CODE:
6e3b076d 687 gettimeofday(&Ta, NULL);
52d72fba 688 if (items > 0) {
92bc48ca 689 if (useconds > 1E6) {
690 IV seconds = (IV) (useconds / 1E6);
f7916ddb 691 /* If usleep() has been implemented using setitimer()
692 * then this contortion is unnecessary-- but usleep()
693 * may be implemented in some other way, so let's contort. */
694 if (seconds) {
695 sleep(seconds);
696 useconds -= 1E6 * seconds;
697 }
698 } else if (useconds < 0.0)
699 croak("Time::HiRes::usleep(%"NVgf"): negative time not invented yet", useconds);
9a2ac92c 700 usleep((U32)useconds);
52d72fba 701 } else
702 PerlProc_pause();
6e3b076d 703 gettimeofday(&Tb, NULL);
92bc48ca 704#if 0
705 printf("[%ld %ld] [%ld %ld]\n", Tb.tv_sec, Tb.tv_usec, Ta.tv_sec, Ta.tv_usec);
706#endif
707 RETVAL = 1E6*(Tb.tv_sec-Ta.tv_sec)+(NV)((IV)Tb.tv_usec-(IV)Ta.tv_usec);
dcf686c9 708
52d72fba 709 OUTPUT:
710 RETVAL
711
44d3ce20 712#if defined(TIME_HIRES_NANOSLEEP)
713
714NV
715nanosleep(nseconds)
716 NV nseconds
717 PREINIT:
718 struct timeval Ta, Tb;
719 CODE:
720 gettimeofday(&Ta, NULL);
721 if (items > 0) {
722 struct timespec tsa;
723 if (nseconds > 1E9) {
724 IV seconds = (IV) (nseconds / 1E9);
725 if (seconds) {
726 sleep(seconds);
727 nseconds -= 1E9 * seconds;
728 }
729 } else if (nseconds < 0.0)
730 croak("Time::HiRes::nanosleep(%"NVgf"): negative time not invented yet", nseconds);
731 tsa.tv_sec = (IV) (nseconds / 1E9);
732 tsa.tv_nsec = (IV) nseconds - tsa.tv_sec * 1E9;
733 nanosleep(&tsa, NULL);
734 } else
735 PerlProc_pause();
736 gettimeofday(&Tb, NULL);
737 RETVAL = 1E3*(1E6*(Tb.tv_sec-Ta.tv_sec)+(NV)((IV)Tb.tv_usec-(IV)Ta.tv_usec));
738
739 OUTPUT:
740 RETVAL
741
742#endif /* #if defined(TIME_HIRES_NANOSLEEP) */
743
52d72fba 744NV
f9d00e57 745sleep(...)
52d72fba 746 PREINIT:
747 struct timeval Ta, Tb;
dcf686c9 748 CODE:
6e3b076d 749 gettimeofday(&Ta, NULL);
92bc48ca 750 if (items > 0) {
751 NV seconds = SvNV(ST(0));
f7916ddb 752 if (seconds >= 0.0) {
7c436af3 753 UV useconds = (UV)(1E6 * (seconds - (UV)seconds));
4880edd6 754 if (seconds >= 1.0)
9a2ac92c 755 sleep((U32)seconds);
db0b859f 756 if ((IV)useconds < 0) {
757#if defined(__sparc64__) && defined(__GNUC__)
758 /* Sparc64 gcc 2.95.3 (e.g. on NetBSD) has a bug
759 * where (0.5 - (UV)(0.5)) will under certain
760 * circumstances (if the double is cast to UV more
761 * than once?) evaluate to -0.5, instead of 0.5. */
762 useconds = -(IV)useconds;
44d3ce20 763#endif /* #if defined(__sparc64__) && defined(__GNUC__) */
db0b859f 764 if ((IV)useconds < 0)
765 croak("Time::HiRes::sleep(%"NVgf"): internal error: useconds < 0 (unsigned %"UVuf" signed %"IVdf")", seconds, useconds, (IV)useconds);
766 }
f7916ddb 767 usleep(useconds);
768 } else
769 croak("Time::HiRes::sleep(%"NVgf"): negative time not invented yet", seconds);
92bc48ca 770 } else
f9d00e57 771 PerlProc_pause();
6e3b076d 772 gettimeofday(&Tb, NULL);
92bc48ca 773#if 0
774 printf("[%ld %ld] [%ld %ld]\n", Tb.tv_sec, Tb.tv_usec, Ta.tv_sec, Ta.tv_usec);
775#endif
52d72fba 776 RETVAL = (NV)(Tb.tv_sec-Ta.tv_sec)+0.000001*(NV)(Tb.tv_usec-Ta.tv_usec);
777
778 OUTPUT:
779 RETVAL
dcf686c9 780
44d3ce20 781#endif /* #if defined(HAS_USLEEP) && defined(HAS_GETTIMEOFDAY) */
dcf686c9 782
783#ifdef HAS_UALARM
784
3de7a4ec 785int
dcf686c9 786ualarm(useconds,interval=0)
787 int useconds
788 int interval
f7916ddb 789 CODE:
790 if (useconds < 0 || interval < 0)
3de7a4ec 791 croak("Time::HiRes::ualarm(%d, %d): negative time not invented yet", useconds, interval);
f7916ddb 792 RETVAL = ualarm(useconds, interval);
dcf686c9 793
f7916ddb 794 OUTPUT:
795 RETVAL
796
797NV
798alarm(seconds,interval=0)
799 NV seconds
800 NV interval
dcf686c9 801 CODE:
f7916ddb 802 if (seconds < 0.0 || interval < 0.0)
803 croak("Time::HiRes::alarm(%"NVgf", %"NVgf"): negative time not invented yet", seconds, interval);
804 RETVAL = (NV)ualarm(seconds * 1000000,
805 interval * 1000000) / 1E6;
dcf686c9 806
c6c619a9 807 OUTPUT:
808 RETVAL
809
44d3ce20 810#endif /* #ifdef HAS_UALARM */
dcf686c9 811
812#ifdef HAS_GETTIMEOFDAY
db835671 813# ifdef MACOS_TRADITIONAL /* fix epoch TZ and use unsigned time_t */
814void
815gettimeofday()
816 PREINIT:
817 struct timeval Tp;
818 struct timezone Tz;
819 PPCODE:
820 int status;
6e3b076d 821 status = gettimeofday (&Tp, &Tz);
db835671 822
993164ab 823 if (status == 0) {
824 Tp.tv_sec += Tz.tz_minuteswest * 60; /* adjust for TZ */
825 if (GIMME == G_ARRAY) {
826 EXTEND(sp, 2);
827 /* Mac OS (Classic) has unsigned time_t */
828 PUSHs(sv_2mortal(newSVuv(Tp.tv_sec)));
829 PUSHs(sv_2mortal(newSViv(Tp.tv_usec)));
830 } else {
831 EXTEND(sp, 1);
832 PUSHs(sv_2mortal(newSVnv(Tp.tv_sec + (Tp.tv_usec / 1000000.0))));
833 }
db835671 834 }
835
836NV
837time()
838 PREINIT:
839 struct timeval Tp;
840 struct timezone Tz;
841 CODE:
842 int status;
6e3b076d 843 status = gettimeofday (&Tp, &Tz);
993164ab 844 if (status == 0) {
845 Tp.tv_sec += Tz.tz_minuteswest * 60; /* adjust for TZ */
846 RETVAL = Tp.tv_sec + (Tp.tv_usec / 1000000.0);
847 } else {
848 RETVAL = -1.0;
849 }
db835671 850 OUTPUT:
851 RETVAL
dcf686c9 852
db835671 853# else /* MACOS_TRADITIONAL */
dcf686c9 854void
855gettimeofday()
856 PREINIT:
857 struct timeval Tp;
858 PPCODE:
859 int status;
6e3b076d 860 status = gettimeofday (&Tp, NULL);
993164ab 861 if (status == 0) {
862 if (GIMME == G_ARRAY) {
863 EXTEND(sp, 2);
864 PUSHs(sv_2mortal(newSViv(Tp.tv_sec)));
865 PUSHs(sv_2mortal(newSViv(Tp.tv_usec)));
866 } else {
867 EXTEND(sp, 1);
868 PUSHs(sv_2mortal(newSVnv(Tp.tv_sec + (Tp.tv_usec / 1000000.0))));
869 }
dcf686c9 870 }
871
3c72ec00 872NV
dcf686c9 873time()
874 PREINIT:
875 struct timeval Tp;
876 CODE:
877 int status;
6e3b076d 878 status = gettimeofday (&Tp, NULL);
993164ab 879 if (status == 0) {
880 RETVAL = Tp.tv_sec + (Tp.tv_usec / 1000000.);
881 } else {
882 RETVAL = -1.0;
883 }
dcf686c9 884 OUTPUT:
885 RETVAL
886
db835671 887# endif /* MACOS_TRADITIONAL */
44d3ce20 888#endif /* #ifdef HAS_GETTIMEOFDAY */
dcf686c9 889
3c72ec00 890#if defined(HAS_GETITIMER) && defined(HAS_SETITIMER)
891
892#define TV2NV(tv) ((NV)((tv).tv_sec) + 0.000001 * (NV)((tv).tv_usec))
893
894void
895setitimer(which, seconds, interval = 0)
896 int which
897 NV seconds
898 NV interval
899 PREINIT:
900 struct itimerval newit;
901 struct itimerval oldit;
902 PPCODE:
f7916ddb 903 if (seconds < 0.0 || interval < 0.0)
436c6dd3 904 croak("Time::HiRes::setitimer(%"IVdf", %"NVgf", %"NVgf"): negative time not invented yet", (IV)which, seconds, interval);
3c72ec00 905 newit.it_value.tv_sec = seconds;
906 newit.it_value.tv_usec =
907 (seconds - (NV)newit.it_value.tv_sec) * 1000000.0;
908 newit.it_interval.tv_sec = interval;
909 newit.it_interval.tv_usec =
910 (interval - (NV)newit.it_interval.tv_sec) * 1000000.0;
911 if (setitimer(which, &newit, &oldit) == 0) {
912 EXTEND(sp, 1);
913 PUSHs(sv_2mortal(newSVnv(TV2NV(oldit.it_value))));
914 if (GIMME == G_ARRAY) {
915 EXTEND(sp, 1);
916 PUSHs(sv_2mortal(newSVnv(TV2NV(oldit.it_interval))));
917 }
918 }
919
920void
921getitimer(which)
922 int which
923 PREINIT:
924 struct itimerval nowit;
925 PPCODE:
926 if (getitimer(which, &nowit) == 0) {
927 EXTEND(sp, 1);
928 PUSHs(sv_2mortal(newSVnv(TV2NV(nowit.it_value))));
929 if (GIMME == G_ARRAY) {
930 EXTEND(sp, 1);
931 PUSHs(sv_2mortal(newSVnv(TV2NV(nowit.it_interval))));
932 }
933 }
934
44d3ce20 935#endif /* #if defined(HAS_GETITIMER) && defined(HAS_SETITIMER) */
936