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