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