This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Remove the last STRLEN n_a;s that my compiler can reach.
[perl5.git] / ext / Time / HiRes / HiRes.xs
CommitLineData
f8daf111
RGS
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
JH
11#ifdef __cplusplus
12extern "C" {
13#endif
0225372c 14#define PERL_NO_GET_CONTEXT
dcf686c9
JH
15#include "EXTERN.h"
16#include "perl.h"
17#include "XSUB.h"
1fbb4de4 18#include "ppport.h"
4ed0e2d4
RGS
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
PG
28#ifdef HAS_SELECT
29# ifdef I_SYS_SELECT
30# include <sys/select.h>
31# endif
32#endif
dcf686c9
JH
33#ifdef __cplusplus
34}
35#endif
36
1fbb4de4
RGS
37#ifndef PerlProc_pause
38# define PerlProc_pause() Pause()
3f2ee006
HS
39#endif
40
1fbb4de4
RGS
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
HS
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
HS
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
JH
58/* 5.004 doesn't define PL_sv_undef */
59#ifndef ATLEASTFIVEOHOHFIVE
4ed0e2d4
RGS
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
RGS
68
69#ifndef HAS_GETTIMEOFDAY
70# define HAS_GETTIMEOFDAY
71#endif
fd44fdfd 72
6e3b076d
JH
73/* shows up in winsock.h?
74struct timeval {
75 long tv_sec;
76 long tv_usec;
77}
78*/
79
fd44fdfd
JH
80typedef union {
81 unsigned __int64 ft_i64;
82 FILETIME ft_val;
83} FT_t;
84
0225372c
RGS
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
RGS
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
JH
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
RGS
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
RGS
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
RGS
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
RGS
123static int
124_gettimeofday(pTHX_ struct timeval *tp, void *not_used)
fd44fdfd 125{
0225372c
RGS
126 dMY_CXT;
127
128 unsigned __int64 ticks;
fd44fdfd
JH
129 FT_t ft;
130
4ed0e2d4
RGS
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
RGS
141 QueryPerformanceCounter((LARGE_INTEGER*)&ticks);
142 ticks -= MY_CXT.base_ticks;
143 ft.ft_i64 = MY_CXT.base_systime_as_filetime.ft_i64
b6136f41
RGS
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
RGS
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
JH
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
RGS
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
JH
173#if !defined(HAS_GETTIMEOFDAY) && defined(VMS)
174#define HAS_GETTIMEOFDAY
175
9b6f56ad 176#include <lnmdef.h>
dcf686c9
JH
177#include <time.h> /* gettimeofday */
178#include <stdlib.h> /* qdiv */
179#include <starlet.h> /* sys$gettim */
180#include <descrip.h>
3785778e
PP
181#ifdef __VAX
182#include <lib$routines.h> /* lib$ediv() */
183#endif
dcf686c9
JH
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
CL
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
JH
289int
290gettimeofday (struct timeval *tp, void *tpz)
291{
292 long ret;
5cdb7193 293#ifdef __VAX
3785778e
PP
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
JH
301 __int64 quad;
302 __qdiv_t ans1,ans2;
5cdb7193 303#endif
dcf686c9
JH
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
PP
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
JH
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
PP
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
JH
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
JH
344 } else {
345 tp->tv_sec = ret;
346 return -1;
347 }
9b6f56ad
CL
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
JH
355 return 0;
356}
357#endif
358
3f2ee006 359
046e3f33
JH
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
44d3ce20 365#define usleep hrt_unanosleep /* could conflict with ncurses for static build */
3f2ee006
HS
366
367void
44d3ce20 368hrt_unanosleep(unsigned long usec) /* This is used to emulate usleep. */
3f2ee006
HS
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
JH
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
JH
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
JH
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
CL
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
JH
622#ifdef HAS_GETTIMEOFDAY
623
a2e20b18 624static int
0225372c 625myU2time(pTHX_ UV *ret)
dcf686c9
JH
626{
627 struct timeval Tp;
628 int status;
6e3b076d 629 status = gettimeofday (&Tp, NULL);
dcf686c9
JH
630 ret[0] = Tp.tv_sec;
631 ret[1] = Tp.tv_usec;
a2e20b18 632 return status;
dcf686c9
JH
633}
634
3c72ec00 635static NV
dcf686c9
JH
636myNVtime()
637{
0225372c 638#ifdef WIN32
4ed0e2d4 639 dTHX;
0225372c 640#endif
dcf686c9
JH
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
JH
645}
646
44d3ce20 647#endif /* #ifdef HAS_GETTIMEOFDAY */
dcf686c9
JH
648
649MODULE = Time::HiRes PACKAGE = Time::HiRes
650
651PROTOTYPES: ENABLE
652
653BOOT:
0225372c
RGS
654{
655#ifdef MY_CXT_KEY
656 MY_CXT_INIT;
657#endif
3f2ee006 658#ifdef ATLEASTFIVEOHOHFIVE
dcf686c9 659#ifdef HAS_GETTIMEOFDAY
0225372c
RGS
660 {
661 UV auv[2];
662 hv_store(PL_modglobal, "Time::NVtime", 12, newSViv(PTR2IV(myNVtime)), 0);
663 if (myU2time(aTHX_ auv) == 0)
664 hv_store(PL_modglobal, "Time::U2time", 12, newSViv((IV) auv[0]), 0);
665 }
666#endif
dcf686c9 667#endif
0225372c
RGS
668}
669
670#if defined(USE_ITHREADS) && defined(MY_CXT_KEY)
671
672void
673CLONE(...)
674 CODE:
675 MY_CXT_CLONE;
676
3f2ee006 677#endif
dcf686c9 678
98b50af3 679INCLUDE: const-xs.inc
3c72ec00 680
52d72fba 681#if defined(HAS_USLEEP) && defined(HAS_GETTIMEOFDAY)
dcf686c9 682
92bc48ca 683NV
dcf686c9 684usleep(useconds)
92bc48ca 685 NV useconds
52d72fba
JH
686 PREINIT:
687 struct timeval Ta, Tb;
688 CODE:
6e3b076d 689 gettimeofday(&Ta, NULL);
52d72fba 690 if (items > 0) {
92bc48ca
JH
691 if (useconds > 1E6) {
692 IV seconds = (IV) (useconds / 1E6);
f7916ddb
JH
693 /* If usleep() has been implemented using setitimer()
694 * then this contortion is unnecessary-- but usleep()
695 * may be implemented in some other way, so let's contort. */
696 if (seconds) {
697 sleep(seconds);
698 useconds -= 1E6 * seconds;
699 }
700 } else if (useconds < 0.0)
701 croak("Time::HiRes::usleep(%"NVgf"): negative time not invented yet", useconds);
9a2ac92c 702 usleep((U32)useconds);
52d72fba
JH
703 } else
704 PerlProc_pause();
6e3b076d 705 gettimeofday(&Tb, NULL);
92bc48ca
JH
706#if 0
707 printf("[%ld %ld] [%ld %ld]\n", Tb.tv_sec, Tb.tv_usec, Ta.tv_sec, Ta.tv_usec);
708#endif
709 RETVAL = 1E6*(Tb.tv_sec-Ta.tv_sec)+(NV)((IV)Tb.tv_usec-(IV)Ta.tv_usec);
dcf686c9 710
52d72fba
JH
711 OUTPUT:
712 RETVAL
713
44d3ce20
RGS
714#if defined(TIME_HIRES_NANOSLEEP)
715
716NV
717nanosleep(nseconds)
718 NV nseconds
719 PREINIT:
720 struct timeval Ta, Tb;
721 CODE:
722 gettimeofday(&Ta, NULL);
723 if (items > 0) {
724 struct timespec tsa;
725 if (nseconds > 1E9) {
726 IV seconds = (IV) (nseconds / 1E9);
727 if (seconds) {
728 sleep(seconds);
729 nseconds -= 1E9 * seconds;
730 }
731 } else if (nseconds < 0.0)
732 croak("Time::HiRes::nanosleep(%"NVgf"): negative time not invented yet", nseconds);
733 tsa.tv_sec = (IV) (nseconds / 1E9);
734 tsa.tv_nsec = (IV) nseconds - tsa.tv_sec * 1E9;
735 nanosleep(&tsa, NULL);
736 } else
737 PerlProc_pause();
738 gettimeofday(&Tb, NULL);
739 RETVAL = 1E3*(1E6*(Tb.tv_sec-Ta.tv_sec)+(NV)((IV)Tb.tv_usec-(IV)Ta.tv_usec));
740
741 OUTPUT:
742 RETVAL
743
744#endif /* #if defined(TIME_HIRES_NANOSLEEP) */
745
52d72fba 746NV
f9d00e57 747sleep(...)
52d72fba
JH
748 PREINIT:
749 struct timeval Ta, Tb;
dcf686c9 750 CODE:
6e3b076d 751 gettimeofday(&Ta, NULL);
92bc48ca
JH
752 if (items > 0) {
753 NV seconds = SvNV(ST(0));
f7916ddb 754 if (seconds >= 0.0) {
7c436af3 755 UV useconds = (UV)(1E6 * (seconds - (UV)seconds));
4880edd6 756 if (seconds >= 1.0)
9a2ac92c 757 sleep((U32)seconds);
db0b859f
JH
758 if ((IV)useconds < 0) {
759#if defined(__sparc64__) && defined(__GNUC__)
760 /* Sparc64 gcc 2.95.3 (e.g. on NetBSD) has a bug
761 * where (0.5 - (UV)(0.5)) will under certain
762 * circumstances (if the double is cast to UV more
763 * than once?) evaluate to -0.5, instead of 0.5. */
764 useconds = -(IV)useconds;
44d3ce20 765#endif /* #if defined(__sparc64__) && defined(__GNUC__) */
db0b859f
JH
766 if ((IV)useconds < 0)
767 croak("Time::HiRes::sleep(%"NVgf"): internal error: useconds < 0 (unsigned %"UVuf" signed %"IVdf")", seconds, useconds, (IV)useconds);
768 }
f7916ddb
JH
769 usleep(useconds);
770 } else
771 croak("Time::HiRes::sleep(%"NVgf"): negative time not invented yet", seconds);
92bc48ca 772 } else
f9d00e57 773 PerlProc_pause();
6e3b076d 774 gettimeofday(&Tb, NULL);
92bc48ca
JH
775#if 0
776 printf("[%ld %ld] [%ld %ld]\n", Tb.tv_sec, Tb.tv_usec, Ta.tv_sec, Ta.tv_usec);
777#endif
52d72fba
JH
778 RETVAL = (NV)(Tb.tv_sec-Ta.tv_sec)+0.000001*(NV)(Tb.tv_usec-Ta.tv_usec);
779
780 OUTPUT:
781 RETVAL
dcf686c9 782
44d3ce20 783#endif /* #if defined(HAS_USLEEP) && defined(HAS_GETTIMEOFDAY) */
dcf686c9
JH
784
785#ifdef HAS_UALARM
786
3de7a4ec 787int
dcf686c9
JH
788ualarm(useconds,interval=0)
789 int useconds
790 int interval
f7916ddb
JH
791 CODE:
792 if (useconds < 0 || interval < 0)
3de7a4ec 793 croak("Time::HiRes::ualarm(%d, %d): negative time not invented yet", useconds, interval);
f7916ddb 794 RETVAL = ualarm(useconds, interval);
dcf686c9 795
f7916ddb
JH
796 OUTPUT:
797 RETVAL
798
799NV
800alarm(seconds,interval=0)
801 NV seconds
802 NV interval
dcf686c9 803 CODE:
f7916ddb
JH
804 if (seconds < 0.0 || interval < 0.0)
805 croak("Time::HiRes::alarm(%"NVgf", %"NVgf"): negative time not invented yet", seconds, interval);
806 RETVAL = (NV)ualarm(seconds * 1000000,
807 interval * 1000000) / 1E6;
dcf686c9 808
c6c619a9
DM
809 OUTPUT:
810 RETVAL
811
44d3ce20 812#endif /* #ifdef HAS_UALARM */
dcf686c9
JH
813
814#ifdef HAS_GETTIMEOFDAY
db835671
JH
815# ifdef MACOS_TRADITIONAL /* fix epoch TZ and use unsigned time_t */
816void
817gettimeofday()
818 PREINIT:
819 struct timeval Tp;
820 struct timezone Tz;
821 PPCODE:
822 int status;
6e3b076d 823 status = gettimeofday (&Tp, &Tz);
db835671 824
993164ab
RGS
825 if (status == 0) {
826 Tp.tv_sec += Tz.tz_minuteswest * 60; /* adjust for TZ */
827 if (GIMME == G_ARRAY) {
828 EXTEND(sp, 2);
829 /* Mac OS (Classic) has unsigned time_t */
830 PUSHs(sv_2mortal(newSVuv(Tp.tv_sec)));
831 PUSHs(sv_2mortal(newSViv(Tp.tv_usec)));
832 } else {
833 EXTEND(sp, 1);
834 PUSHs(sv_2mortal(newSVnv(Tp.tv_sec + (Tp.tv_usec / 1000000.0))));
835 }
db835671
JH
836 }
837
838NV
839time()
840 PREINIT:
841 struct timeval Tp;
842 struct timezone Tz;
843 CODE:
844 int status;
6e3b076d 845 status = gettimeofday (&Tp, &Tz);
993164ab
RGS
846 if (status == 0) {
847 Tp.tv_sec += Tz.tz_minuteswest * 60; /* adjust for TZ */
848 RETVAL = Tp.tv_sec + (Tp.tv_usec / 1000000.0);
849 } else {
850 RETVAL = -1.0;
851 }
db835671
JH
852 OUTPUT:
853 RETVAL
dcf686c9 854
db835671 855# else /* MACOS_TRADITIONAL */
dcf686c9
JH
856void
857gettimeofday()
858 PREINIT:
859 struct timeval Tp;
860 PPCODE:
861 int status;
6e3b076d 862 status = gettimeofday (&Tp, NULL);
993164ab
RGS
863 if (status == 0) {
864 if (GIMME == G_ARRAY) {
865 EXTEND(sp, 2);
866 PUSHs(sv_2mortal(newSViv(Tp.tv_sec)));
867 PUSHs(sv_2mortal(newSViv(Tp.tv_usec)));
868 } else {
869 EXTEND(sp, 1);
870 PUSHs(sv_2mortal(newSVnv(Tp.tv_sec + (Tp.tv_usec / 1000000.0))));
871 }
dcf686c9
JH
872 }
873
3c72ec00 874NV
dcf686c9
JH
875time()
876 PREINIT:
877 struct timeval Tp;
878 CODE:
879 int status;
6e3b076d 880 status = gettimeofday (&Tp, NULL);
993164ab
RGS
881 if (status == 0) {
882 RETVAL = Tp.tv_sec + (Tp.tv_usec / 1000000.);
883 } else {
884 RETVAL = -1.0;
885 }
dcf686c9
JH
886 OUTPUT:
887 RETVAL
888
db835671 889# endif /* MACOS_TRADITIONAL */
44d3ce20 890#endif /* #ifdef HAS_GETTIMEOFDAY */
dcf686c9 891
3c72ec00
JH
892#if defined(HAS_GETITIMER) && defined(HAS_SETITIMER)
893
894#define TV2NV(tv) ((NV)((tv).tv_sec) + 0.000001 * (NV)((tv).tv_usec))
895
896void
897setitimer(which, seconds, interval = 0)
898 int which
899 NV seconds
900 NV interval
901 PREINIT:
902 struct itimerval newit;
903 struct itimerval oldit;
904 PPCODE:
f7916ddb 905 if (seconds < 0.0 || interval < 0.0)
436c6dd3 906 croak("Time::HiRes::setitimer(%"IVdf", %"NVgf", %"NVgf"): negative time not invented yet", (IV)which, seconds, interval);
3c72ec00
JH
907 newit.it_value.tv_sec = seconds;
908 newit.it_value.tv_usec =
909 (seconds - (NV)newit.it_value.tv_sec) * 1000000.0;
910 newit.it_interval.tv_sec = interval;
911 newit.it_interval.tv_usec =
912 (interval - (NV)newit.it_interval.tv_sec) * 1000000.0;
913 if (setitimer(which, &newit, &oldit) == 0) {
914 EXTEND(sp, 1);
915 PUSHs(sv_2mortal(newSVnv(TV2NV(oldit.it_value))));
916 if (GIMME == G_ARRAY) {
917 EXTEND(sp, 1);
918 PUSHs(sv_2mortal(newSVnv(TV2NV(oldit.it_interval))));
919 }
920 }
921
922void
923getitimer(which)
924 int which
925 PREINIT:
926 struct itimerval nowit;
927 PPCODE:
928 if (getitimer(which, &nowit) == 0) {
929 EXTEND(sp, 1);
930 PUSHs(sv_2mortal(newSVnv(TV2NV(nowit.it_value))));
931 if (GIMME == G_ARRAY) {
932 EXTEND(sp, 1);
933 PUSHs(sv_2mortal(newSVnv(TV2NV(nowit.it_interval))));
934 }
935 }
936
44d3ce20
RGS
937#endif /* #if defined(HAS_GETITIMER) && defined(HAS_SETITIMER) */
938
3c72ec00 939