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