This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
No matter how hard you proof read it, something always slips through.
[perl5.git] / ext / Time / HiRes / HiRes.xs
CommitLineData
dcf686c9
JH
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
PG
12#ifdef HAS_SELECT
13# ifdef I_SYS_SELECT
14# include <sys/select.h>
15# endif
16#endif
dcf686c9
JH
17#ifdef __cplusplus
18}
19#endif
20
3f2ee006
HS
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
HS
40# else
41# if IVSIZE == INTSIZE
42# define IVdf "d"
db0b859f 43# define UVuf "u"
3f2ee006
HS
44# endif
45# endif
46# else
47# define IVdf "ld"
df16a331 48# define UVuf "lu"
3f2ee006
HS
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
JH
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
JH
113#if !defined(HAS_GETTIMEOFDAY) && defined(WIN32)
114#define HAS_GETTIMEOFDAY
115
6e3b076d
JH
116/* shows up in winsock.h?
117struct timeval {
118 long tv_sec;
119 long tv_usec;
120}
121*/
122
fd44fdfd
JH
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
JH
129#ifdef __GNUC__
130#define Const64(x) x##LL
131#else
132#define Const64(x) x##i64
133#endif
fd44fdfd
JH
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
JH
138int
139gettimeofday (struct timeval *tp, void *not_used)
fd44fdfd
JH
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
JH
156#if !defined(HAS_GETTIMEOFDAY) && defined(VMS)
157#define HAS_GETTIMEOFDAY
158
9b6f56ad 159#include <lnmdef.h>
dcf686c9
JH
160#include <time.h> /* gettimeofday */
161#include <stdlib.h> /* qdiv */
162#include <starlet.h> /* sys$gettim */
163#include <descrip.h>
3785778e
PP
164#ifdef __VAX
165#include <lib$routines.h> /* lib$ediv() */
166#endif
dcf686c9
JH
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
CL
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
JH
272int
273gettimeofday (struct timeval *tp, void *tpz)
274{
275 long ret;
5cdb7193 276#ifdef __VAX
3785778e
PP
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
JH
284 __int64 quad;
285 __qdiv_t ans1,ans2;
5cdb7193 286#endif
dcf686c9
JH
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
PP
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
JH
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
PP
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
JH
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
JH
327 } else {
328 tp->tv_sec = ret;
329 return -1;
330 }
9b6f56ad
CL
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
JH
338 return 0;
339}
340#endif
341
3f2ee006 342
046e3f33
JH
343 /* Do not use H A S _ N A N O S L E E P
344 * so that Perl Configure doesn't scan for it.
345 * The TIME_HIRES_NANOSLEEP is set by Makefile.PL. */
346#if !defined(HAS_USLEEP) && defined(TIME_HIRES_NANOSLEEP)
3f2ee006
HS
347#define HAS_USLEEP
348#define usleep hrt_nanosleep /* could conflict with ncurses for static build */
349
350void
351hrt_nanosleep(unsigned long usec)
352{
353 struct timespec res;
354 res.tv_sec = usec/1000/1000;
355 res.tv_nsec = ( usec - res.tv_sec*1000*1000 ) * 1000;
356 nanosleep(&res, NULL);
357}
358#endif
359
360
dcf686c9
JH
361#if !defined(HAS_USLEEP) && defined(HAS_SELECT)
362#ifndef SELECT_IS_BROKEN
363#define HAS_USLEEP
364#define usleep hrt_usleep /* could conflict with ncurses for static build */
365
366void
367hrt_usleep(unsigned long usec)
368{
369 struct timeval tv;
370 tv.tv_sec = 0;
371 tv.tv_usec = usec;
372 select(0, (Select_fd_set_t)NULL, (Select_fd_set_t)NULL,
373 (Select_fd_set_t)NULL, &tv);
374}
375#endif
376#endif
377
378#if !defined(HAS_USLEEP) && defined(WIN32)
379#define HAS_USLEEP
380#define usleep hrt_usleep /* could conflict with ncurses for static build */
381
382void
383hrt_usleep(unsigned long usec)
384{
385 long msec;
386 msec = usec / 1000;
387 Sleep (msec);
388}
389#endif
390
391
392#if !defined(HAS_UALARM) && defined(HAS_SETITIMER)
393#define HAS_UALARM
394#define ualarm hrt_ualarm /* could conflict with ncurses for static build */
395
396int
397hrt_ualarm(int usec, int interval)
398{
399 struct itimerval itv;
400 itv.it_value.tv_sec = usec / 1000000;
401 itv.it_value.tv_usec = usec % 1000000;
402 itv.it_interval.tv_sec = interval / 1000000;
403 itv.it_interval.tv_usec = interval % 1000000;
404 return setitimer(ITIMER_REAL, &itv, 0);
405}
406#endif
407
ca40fe49
CL
408#if !defined(HAS_UALARM) && defined(VMS)
409#define HAS_UALARM
410#define ualarm vms_ualarm
411
412#include <lib$routines.h>
413#include <ssdef.h>
414#include <starlet.h>
415#include <descrip.h>
416#include <signal.h>
417#include <jpidef.h>
418#include <psldef.h>
419
420#define VMSERR(s) (!((s)&1))
421
422static void
423us_to_VMS(useconds_t mseconds, unsigned long v[])
424{
425 int iss;
426 unsigned long qq[2];
427
428 qq[0] = mseconds;
429 qq[1] = 0;
430 v[0] = v[1] = 0;
431
432 iss = lib$addx(qq,qq,qq);
433 if (VMSERR(iss)) lib$signal(iss);
434 iss = lib$subx(v,qq,v);
435 if (VMSERR(iss)) lib$signal(iss);
436 iss = lib$addx(qq,qq,qq);
437 if (VMSERR(iss)) lib$signal(iss);
438 iss = lib$subx(v,qq,v);
439 if (VMSERR(iss)) lib$signal(iss);
440 iss = lib$subx(v,qq,v);
441 if (VMSERR(iss)) lib$signal(iss);
442}
443
444static int
445VMS_to_us(unsigned long v[])
446{
447 int iss;
448 unsigned long div=10,quot, rem;
449
450 iss = lib$ediv(&div,v,&quot,&rem);
451 if (VMSERR(iss)) lib$signal(iss);
452
453 return quot;
454}
455
456typedef unsigned short word;
457typedef struct _ualarm {
458 int function;
459 int repeat;
460 unsigned long delay[2];
461 unsigned long interval[2];
462 unsigned long remain[2];
463} Alarm;
464
465
466static int alarm_ef;
467static Alarm *a0, alarm_base;
468#define UAL_NULL 0
469#define UAL_SET 1
470#define UAL_CLEAR 2
471#define UAL_ACTIVE 4
472static void ualarm_AST(Alarm *a);
473
474static int
475vms_ualarm(int mseconds, int interval)
476{
477 Alarm *a, abase;
478 struct item_list3 {
479 word length;
480 word code;
481 void *bufaddr;
482 void *retlenaddr;
483 } ;
484 static struct item_list3 itmlst[2];
485 static int first = 1;
486 unsigned long asten;
487 int iss, enabled;
488
489 if (first) {
490 first = 0;
491 itmlst[0].code = JPI$_ASTEN;
492 itmlst[0].length = sizeof(asten);
493 itmlst[0].retlenaddr = NULL;
494 itmlst[1].code = 0;
495 itmlst[1].length = 0;
496 itmlst[1].bufaddr = NULL;
497 itmlst[1].retlenaddr = NULL;
498
499 iss = lib$get_ef(&alarm_ef);
500 if (VMSERR(iss)) lib$signal(iss);
501
502 a0 = &alarm_base;
503 a0->function = UAL_NULL;
504 }
505 itmlst[0].bufaddr = &asten;
506
507 iss = sys$getjpiw(0,0,0,itmlst,0,0,0);
508 if (VMSERR(iss)) lib$signal(iss);
509 if (!(asten&0x08)) return -1;
510
511 a = &abase;
512 if (mseconds) {
513 a->function = UAL_SET;
514 } else {
515 a->function = UAL_CLEAR;
516 }
517
518 us_to_VMS(mseconds, a->delay);
519 if (interval) {
520 us_to_VMS(interval, a->interval);
521 a->repeat = 1;
522 } else
523 a->repeat = 0;
524
525 iss = sys$clref(alarm_ef);
526 if (VMSERR(iss)) lib$signal(iss);
527
528 iss = sys$dclast(ualarm_AST,a,0);
529 if (VMSERR(iss)) lib$signal(iss);
530
531 iss = sys$waitfr(alarm_ef);
532 if (VMSERR(iss)) lib$signal(iss);
533
534 if (a->function == UAL_ACTIVE)
535 return VMS_to_us(a->remain);
536 else
537 return 0;
538}
539
540
541
542static void
543ualarm_AST(Alarm *a)
544{
545 int iss;
546 unsigned long now[2];
547
548 iss = sys$gettim(now);
549 if (VMSERR(iss)) lib$signal(iss);
550
551 if (a->function == UAL_SET || a->function == UAL_CLEAR) {
552 if (a0->function == UAL_ACTIVE) {
553 iss = sys$cantim(a0,PSL$C_USER);
554 if (VMSERR(iss)) lib$signal(iss);
555
556 iss = lib$subx(a0->remain, now, a->remain);
557 if (VMSERR(iss)) lib$signal(iss);
558
559 if (a->remain[1] & 0x80000000)
560 a->remain[0] = a->remain[1] = 0;
561 }
562
563 if (a->function == UAL_SET) {
564 a->function = a0->function;
565 a0->function = UAL_ACTIVE;
566 a0->repeat = a->repeat;
567 if (a0->repeat) {
568 a0->interval[0] = a->interval[0];
569 a0->interval[1] = a->interval[1];
570 }
571 a0->delay[0] = a->delay[0];
572 a0->delay[1] = a->delay[1];
573
574 iss = lib$subx(now, a0->delay, a0->remain);
575 if (VMSERR(iss)) lib$signal(iss);
576
577 iss = sys$setimr(0,a0->delay,ualarm_AST,a0);
578 if (VMSERR(iss)) lib$signal(iss);
579 } else {
580 a->function = a0->function;
581 a0->function = UAL_NULL;
582 }
583 iss = sys$setef(alarm_ef);
584 if (VMSERR(iss)) lib$signal(iss);
585 } else if (a->function == UAL_ACTIVE) {
586 if (a->repeat) {
587 iss = lib$subx(now, a->interval, a->remain);
588 if (VMSERR(iss)) lib$signal(iss);
589
590 iss = sys$setimr(0,a->interval,ualarm_AST,a);
591 if (VMSERR(iss)) lib$signal(iss);
592 } else {
593 a->function = UAL_NULL;
594 }
595 iss = sys$wake(0,0);
596 if (VMSERR(iss)) lib$signal(iss);
597 lib$signal(SS$_ASTFLT);
598 } else {
599 lib$signal(SS$_BADPARAM);
600 }
601}
602
603#endif /* !HAS_UALARM && VMS */
604
dcf686c9
JH
605#ifdef HAS_GETTIMEOFDAY
606
a2e20b18 607static int
dcf686c9
JH
608myU2time(UV *ret)
609{
610 struct timeval Tp;
611 int status;
6e3b076d 612 status = gettimeofday (&Tp, NULL);
dcf686c9
JH
613 ret[0] = Tp.tv_sec;
614 ret[1] = Tp.tv_usec;
a2e20b18 615 return status;
dcf686c9
JH
616}
617
3c72ec00 618static NV
dcf686c9
JH
619myNVtime()
620{
621 struct timeval Tp;
622 int status;
6e3b076d 623 status = gettimeofday (&Tp, NULL);
a2e20b18 624 return status == 0 ? Tp.tv_sec + (Tp.tv_usec / 1000000.) : -1.0;
dcf686c9
JH
625}
626
627#endif
628
629MODULE = Time::HiRes PACKAGE = Time::HiRes
630
631PROTOTYPES: ENABLE
632
633BOOT:
3f2ee006 634#ifdef ATLEASTFIVEOHOHFIVE
dcf686c9 635#ifdef HAS_GETTIMEOFDAY
a2e20b18
JH
636{
637 UV auv[2];
cfb1f18c 638 hv_store(PL_modglobal, "Time::NVtime", 12, newSViv(PTR2IV(myNVtime)), 0);
a2e20b18
JH
639 if (myU2time(auv) == 0)
640 hv_store(PL_modglobal, "Time::U2time", 12, newSViv((IV) auv[0]), 0);
641}
dcf686c9 642#endif
3f2ee006 643#endif
dcf686c9 644
98b50af3 645INCLUDE: const-xs.inc
3c72ec00 646
52d72fba 647#if defined(HAS_USLEEP) && defined(HAS_GETTIMEOFDAY)
dcf686c9 648
92bc48ca 649NV
dcf686c9 650usleep(useconds)
92bc48ca 651 NV useconds
52d72fba
JH
652 PREINIT:
653 struct timeval Ta, Tb;
654 CODE:
6e3b076d 655 gettimeofday(&Ta, NULL);
52d72fba 656 if (items > 0) {
92bc48ca
JH
657 if (useconds > 1E6) {
658 IV seconds = (IV) (useconds / 1E6);
f7916ddb
JH
659 /* If usleep() has been implemented using setitimer()
660 * then this contortion is unnecessary-- but usleep()
661 * may be implemented in some other way, so let's contort. */
662 if (seconds) {
663 sleep(seconds);
664 useconds -= 1E6 * seconds;
665 }
666 } else if (useconds < 0.0)
667 croak("Time::HiRes::usleep(%"NVgf"): negative time not invented yet", useconds);
9a2ac92c 668 usleep((U32)useconds);
52d72fba
JH
669 } else
670 PerlProc_pause();
6e3b076d 671 gettimeofday(&Tb, NULL);
92bc48ca
JH
672#if 0
673 printf("[%ld %ld] [%ld %ld]\n", Tb.tv_sec, Tb.tv_usec, Ta.tv_sec, Ta.tv_usec);
674#endif
675 RETVAL = 1E6*(Tb.tv_sec-Ta.tv_sec)+(NV)((IV)Tb.tv_usec-(IV)Ta.tv_usec);
dcf686c9 676
52d72fba
JH
677 OUTPUT:
678 RETVAL
679
680NV
f9d00e57 681sleep(...)
52d72fba
JH
682 PREINIT:
683 struct timeval Ta, Tb;
dcf686c9 684 CODE:
6e3b076d 685 gettimeofday(&Ta, NULL);
92bc48ca
JH
686 if (items > 0) {
687 NV seconds = SvNV(ST(0));
f7916ddb 688 if (seconds >= 0.0) {
7c436af3 689 UV useconds = (UV)(1E6 * (seconds - (UV)seconds));
4880edd6 690 if (seconds >= 1.0)
9a2ac92c 691 sleep((U32)seconds);
db0b859f
JH
692 if ((IV)useconds < 0) {
693#if defined(__sparc64__) && defined(__GNUC__)
694 /* Sparc64 gcc 2.95.3 (e.g. on NetBSD) has a bug
695 * where (0.5 - (UV)(0.5)) will under certain
696 * circumstances (if the double is cast to UV more
697 * than once?) evaluate to -0.5, instead of 0.5. */
698 useconds = -(IV)useconds;
699#endif
700 if ((IV)useconds < 0)
701 croak("Time::HiRes::sleep(%"NVgf"): internal error: useconds < 0 (unsigned %"UVuf" signed %"IVdf")", seconds, useconds, (IV)useconds);
702 }
f7916ddb
JH
703 usleep(useconds);
704 } else
705 croak("Time::HiRes::sleep(%"NVgf"): negative time not invented yet", seconds);
92bc48ca 706 } else
f9d00e57 707 PerlProc_pause();
6e3b076d 708 gettimeofday(&Tb, NULL);
92bc48ca
JH
709#if 0
710 printf("[%ld %ld] [%ld %ld]\n", Tb.tv_sec, Tb.tv_usec, Ta.tv_sec, Ta.tv_usec);
711#endif
52d72fba
JH
712 RETVAL = (NV)(Tb.tv_sec-Ta.tv_sec)+0.000001*(NV)(Tb.tv_usec-Ta.tv_usec);
713
714 OUTPUT:
715 RETVAL
dcf686c9
JH
716
717#endif
718
719#ifdef HAS_UALARM
720
3de7a4ec 721int
dcf686c9
JH
722ualarm(useconds,interval=0)
723 int useconds
724 int interval
f7916ddb
JH
725 CODE:
726 if (useconds < 0 || interval < 0)
3de7a4ec 727 croak("Time::HiRes::ualarm(%d, %d): negative time not invented yet", useconds, interval);
f7916ddb 728 RETVAL = ualarm(useconds, interval);
dcf686c9 729
f7916ddb
JH
730 OUTPUT:
731 RETVAL
732
733NV
734alarm(seconds,interval=0)
735 NV seconds
736 NV interval
dcf686c9 737 CODE:
f7916ddb
JH
738 if (seconds < 0.0 || interval < 0.0)
739 croak("Time::HiRes::alarm(%"NVgf", %"NVgf"): negative time not invented yet", seconds, interval);
740 RETVAL = (NV)ualarm(seconds * 1000000,
741 interval * 1000000) / 1E6;
dcf686c9 742
c6c619a9
DM
743 OUTPUT:
744 RETVAL
745
dcf686c9
JH
746#endif
747
748#ifdef HAS_GETTIMEOFDAY
db835671
JH
749# ifdef MACOS_TRADITIONAL /* fix epoch TZ and use unsigned time_t */
750void
751gettimeofday()
752 PREINIT:
753 struct timeval Tp;
754 struct timezone Tz;
755 PPCODE:
756 int status;
6e3b076d 757 status = gettimeofday (&Tp, &Tz);
db835671
JH
758 Tp.tv_sec += Tz.tz_minuteswest * 60; /* adjust for TZ */
759
760 if (GIMME == G_ARRAY) {
761 EXTEND(sp, 2);
762 /* Mac OS (Classic) has unsigned time_t */
763 PUSHs(sv_2mortal(newSVuv(Tp.tv_sec)));
764 PUSHs(sv_2mortal(newSViv(Tp.tv_usec)));
765 } else {
766 EXTEND(sp, 1);
767 PUSHs(sv_2mortal(newSVnv(Tp.tv_sec + (Tp.tv_usec / 1000000.0))));
768 }
769
770NV
771time()
772 PREINIT:
773 struct timeval Tp;
774 struct timezone Tz;
775 CODE:
776 int status;
6e3b076d 777 status = gettimeofday (&Tp, &Tz);
db835671
JH
778 Tp.tv_sec += Tz.tz_minuteswest * 60; /* adjust for TZ */
779 RETVAL = Tp.tv_sec + (Tp.tv_usec / 1000000.0);
780 OUTPUT:
781 RETVAL
dcf686c9 782
db835671 783# else /* MACOS_TRADITIONAL */
dcf686c9
JH
784void
785gettimeofday()
786 PREINIT:
787 struct timeval Tp;
788 PPCODE:
789 int status;
6e3b076d 790 status = gettimeofday (&Tp, NULL);
dcf686c9
JH
791 if (GIMME == G_ARRAY) {
792 EXTEND(sp, 2);
793 PUSHs(sv_2mortal(newSViv(Tp.tv_sec)));
794 PUSHs(sv_2mortal(newSViv(Tp.tv_usec)));
795 } else {
796 EXTEND(sp, 1);
797 PUSHs(sv_2mortal(newSVnv(Tp.tv_sec + (Tp.tv_usec / 1000000.0))));
798 }
799
3c72ec00 800NV
dcf686c9
JH
801time()
802 PREINIT:
803 struct timeval Tp;
804 CODE:
805 int status;
6e3b076d 806 status = gettimeofday (&Tp, NULL);
dcf686c9
JH
807 RETVAL = Tp.tv_sec + (Tp.tv_usec / 1000000.);
808 OUTPUT:
809 RETVAL
810
db835671 811# endif /* MACOS_TRADITIONAL */
dcf686c9
JH
812#endif
813
3c72ec00
JH
814#if defined(HAS_GETITIMER) && defined(HAS_SETITIMER)
815
816#define TV2NV(tv) ((NV)((tv).tv_sec) + 0.000001 * (NV)((tv).tv_usec))
817
818void
819setitimer(which, seconds, interval = 0)
820 int which
821 NV seconds
822 NV interval
823 PREINIT:
824 struct itimerval newit;
825 struct itimerval oldit;
826 PPCODE:
f7916ddb 827 if (seconds < 0.0 || interval < 0.0)
436c6dd3 828 croak("Time::HiRes::setitimer(%"IVdf", %"NVgf", %"NVgf"): negative time not invented yet", (IV)which, seconds, interval);
3c72ec00
JH
829 newit.it_value.tv_sec = seconds;
830 newit.it_value.tv_usec =
831 (seconds - (NV)newit.it_value.tv_sec) * 1000000.0;
832 newit.it_interval.tv_sec = interval;
833 newit.it_interval.tv_usec =
834 (interval - (NV)newit.it_interval.tv_sec) * 1000000.0;
835 if (setitimer(which, &newit, &oldit) == 0) {
836 EXTEND(sp, 1);
837 PUSHs(sv_2mortal(newSVnv(TV2NV(oldit.it_value))));
838 if (GIMME == G_ARRAY) {
839 EXTEND(sp, 1);
840 PUSHs(sv_2mortal(newSVnv(TV2NV(oldit.it_interval))));
841 }
842 }
843
844void
845getitimer(which)
846 int which
847 PREINIT:
848 struct itimerval nowit;
849 PPCODE:
850 if (getitimer(which, &nowit) == 0) {
851 EXTEND(sp, 1);
852 PUSHs(sv_2mortal(newSVnv(TV2NV(nowit.it_value))));
853 if (GIMME == G_ARRAY) {
854 EXTEND(sp, 1);
855 PUSHs(sv_2mortal(newSVnv(TV2NV(nowit.it_interval))));
856 }
857 }
858
859#endif
860