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