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