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