Commit | Line | Data |
---|---|---|
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 |
15 | extern "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? |
113 | struct timeval { | |
114 | long tv_sec; | |
115 | long tv_usec; | |
116 | } | |
117 | */ | |
118 | ||
fd44fdfd JH |
119 | typedef 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 | ||
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; | |
4ed0e2d4 | 131 | unsigned __int64 reset_time; |
0225372c RGS |
132 | } my_cxt_t; |
133 | ||
134 | START_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 |
174 | static 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 | ||
4ed0e2d4 RGS |
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 { | |
c1dc6e7c | 191 | __int64 diff; |
0225372c RGS |
192 | QueryPerformanceCounter((LARGE_INTEGER*)&ticks); |
193 | ticks -= MY_CXT.base_ticks; | |
194 | ft.ft_i64 = MY_CXT.base_systime_as_filetime.ft_i64 | |
47e797f6 SP |
195 | + Const64(IV_1E7) * (ticks / MY_CXT.tick_frequency) |
196 | +(Const64(IV_1E7) * (ticks % MY_CXT.tick_frequency)) / MY_CXT.tick_frequency; | |
c1dc6e7c | 197 | diff = ft.ft_i64 - MY_CXT.base_systime_as_filetime.ft_i64; |
4ed0e2d4 RGS |
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; | |
c1dc6e7c | 202 | } |
0225372c | 203 | } |
fd44fdfd JH |
204 | |
205 | /* seconds since epoch */ | |
47e797f6 | 206 | tp->tv_sec = (long)((ft.ft_i64 - EPOCH_BIAS) / Const64(IV_1E7)); |
fd44fdfd JH |
207 | |
208 | /* microseconds remaining */ | |
47e797f6 | 209 | tp->tv_usec = (long)((ft.ft_i64 / Const64(10)) % Const64(IV_1E6)); |
fd44fdfd JH |
210 | |
211 | return 0; | |
212 | } | |
6e3b076d | 213 | #endif |
fd44fdfd | 214 | |
0225372c RGS |
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 | ||
dcf686c9 JH |
224 | #if !defined(HAS_GETTIMEOFDAY) && defined(VMS) |
225 | #define HAS_GETTIMEOFDAY | |
226 | ||
9b6f56ad | 227 | #include <lnmdef.h> |
dcf686c9 JH |
228 | #include <time.h> /* gettimeofday */ |
229 | #include <stdlib.h> /* qdiv */ | |
230 | #include <starlet.h> /* sys$gettim */ | |
231 | #include <descrip.h> | |
3785778e PP |
232 | #ifdef __VAX |
233 | #include <lib$routines.h> /* lib$ediv() */ | |
234 | #endif | |
dcf686c9 JH |
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 | ||
5cdb7193 | 250 | #ifdef __VAX |
3785778e | 251 | static long base_adjust[2]={0L,0L}; |
5cdb7193 | 252 | #else |
dcf686c9 | 253 | static __int64 base_adjust=0; |
5cdb7193 | 254 | #endif |
dcf686c9 | 255 | |
9b6f56ad CL |
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 | ||
dcf686c9 JH |
340 | int |
341 | gettimeofday (struct timeval *tp, void *tpz) | |
342 | { | |
343 | long ret; | |
5cdb7193 | 344 | #ifdef __VAX |
3785778e PP |
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; | |
5cdb7193 | 351 | #else |
dcf686c9 JH |
352 | __int64 quad; |
353 | __qdiv_t ans1,ans2; | |
5cdb7193 | 354 | #endif |
dcf686c9 JH |
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 | ||
3785778e PP |
363 | #ifdef __VAX |
364 | if (base_adjust[0]==0 && base_adjust[1]==0) { | |
365 | #else | |
dcf686c9 | 366 | if (base_adjust==0) { /* Need to determine epoch adjustment */ |
3785778e | 367 | #endif |
dcf686c9 JH |
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) { | |
5cdb7193 | 377 | #ifdef __VAX |
3785778e PP |
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 */ | |
5cdb7193 | 388 | #else |
3785778e | 389 | quad -= base_adjust; /* convert to epoch offset */ |
dcf686c9 JH |
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 */ | |
3785778e | 394 | #endif |
dcf686c9 JH |
395 | } else { |
396 | tp->tv_sec = ret; | |
397 | return -1; | |
398 | } | |
9b6f56ad CL |
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 | |
dcf686c9 JH |
406 | return 0; |
407 | } | |
408 | #endif | |
409 | ||
3f2ee006 | 410 | |
046e3f33 | 411 | /* Do not use H A S _ N A N O S L E E P |
70cf0185 SP |
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). | |
e5433ad8 | 414 | * (We are part of the core perl now.) |
046e3f33 JH |
415 | * The TIME_HIRES_NANOSLEEP is set by Makefile.PL. */ |
416 | #if !defined(HAS_USLEEP) && defined(TIME_HIRES_NANOSLEEP) | |
3f2ee006 | 417 | #define HAS_USLEEP |
e5620114 | 418 | #define usleep hrt_usleep /* could conflict with ncurses for static build */ |
3f2ee006 | 419 | |
4e922c26 | 420 | static void |
e5620114 | 421 | hrt_usleep(unsigned long usec) /* This is used to emulate usleep. */ |
3f2ee006 HS |
422 | { |
423 | struct timespec res; | |
47e797f6 SP |
424 | res.tv_sec = usec / IV_1E6; |
425 | res.tv_nsec = ( usec - res.tv_sec * IV_1E6 ) * 1000; | |
3f2ee006 HS |
426 | nanosleep(&res, NULL); |
427 | } | |
3f2ee006 | 428 | |
44d3ce20 | 429 | #endif /* #if !defined(HAS_USLEEP) && defined(TIME_HIRES_NANOSLEEP) */ |
3f2ee006 | 430 | |
dcf686c9 JH |
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 | ||
4e922c26 | 436 | static void |
dcf686c9 JH |
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 | |
44d3ce20 | 446 | #endif /* #if !defined(HAS_USLEEP) && defined(HAS_SELECT) */ |
dcf686c9 JH |
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 | ||
4e922c26 | 452 | static void |
dcf686c9 JH |
453 | hrt_usleep(unsigned long usec) |
454 | { | |
455 | long msec; | |
456 | msec = usec / 1000; | |
457 | Sleep (msec); | |
458 | } | |
44d3ce20 | 459 | #endif /* #if !defined(HAS_USLEEP) && defined(WIN32) */ |
dcf686c9 | 460 | |
e5433ad8 SP |
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 | ||
4e922c26 | 465 | static void |
e5433ad8 SP |
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) */ | |
dcf686c9 | 473 | |
34f69483 | 474 | #if defined(HAS_SETITIMER) && defined(ITIMER_REAL) |
bf8300de RGS |
475 | |
476 | static int | |
68c5b4db | 477 | hrt_ualarm_itimero(struct itimerval *oitv, int usec, int uinterval) |
bf8300de | 478 | { |
68c5b4db CBW |
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); | |
bf8300de RGS |
485 | } |
486 | ||
4e922c26 | 487 | static int |
bf8300de | 488 | hrt_ualarm_itimer(int usec, int uinterval) |
dcf686c9 | 489 | { |
68c5b4db | 490 | return hrt_ualarm_itimero(NULL, usec, uinterval); |
dcf686c9 | 491 | } |
bf8300de | 492 | |
75d5269b | 493 | #ifdef HAS_UALARM |
4e922c26 | 494 | static int |
75d5269b SP |
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 */ | |
44d3ce20 | 500 | #endif /* #if !defined(HAS_UALARM) && defined(HAS_SETITIMER) */ |
dcf686c9 | 501 | |
34f69483 SP |
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 | ||
ca40fe49 CL |
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,",&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 | ||
44d3ce20 | 702 | #endif /* #if !defined(HAS_UALARM) && defined(VMS) */ |
ca40fe49 | 703 | |
dcf686c9 JH |
704 | #ifdef HAS_GETTIMEOFDAY |
705 | ||
a2e20b18 | 706 | static int |
0225372c | 707 | myU2time(pTHX_ UV *ret) |
dcf686c9 JH |
708 | { |
709 | struct timeval Tp; | |
710 | int status; | |
6e3b076d | 711 | status = gettimeofday (&Tp, NULL); |
dcf686c9 JH |
712 | ret[0] = Tp.tv_sec; |
713 | ret[1] = Tp.tv_usec; | |
a2e20b18 | 714 | return status; |
dcf686c9 JH |
715 | } |
716 | ||
3c72ec00 | 717 | static NV |
dcf686c9 JH |
718 | myNVtime() |
719 | { | |
0225372c | 720 | #ifdef WIN32 |
4ed0e2d4 | 721 | dTHX; |
0225372c | 722 | #endif |
dcf686c9 JH |
723 | struct timeval Tp; |
724 | int status; | |
6e3b076d | 725 | status = gettimeofday (&Tp, NULL); |
47e797f6 | 726 | return status == 0 ? Tp.tv_sec + (Tp.tv_usec / NV_1E6) : -1.0; |
dcf686c9 JH |
727 | } |
728 | ||
44d3ce20 | 729 | #endif /* #ifdef HAS_GETTIMEOFDAY */ |
dcf686c9 | 730 | |
75d5269b | 731 | static void |
91a2e9f6 | 732 | hrstatns(UV *atime_nsec, UV *mtime_nsec, UV *ctime_nsec) |
75d5269b | 733 | { |
120b53f9 | 734 | dTHX; |
75d5269b SP |
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; | |
91a2e9f6 | 739 | #elif TIME_HIRES_STAT == 2 |
75d5269b SP |
740 | *atime_nsec = PL_statcache.st_atimensec; |
741 | *mtime_nsec = PL_statcache.st_mtimensec; | |
742 | *ctime_nsec = PL_statcache.st_ctimensec; | |
91a2e9f6 | 743 | #elif TIME_HIRES_STAT == 3 |
75d5269b SP |
744 | *atime_nsec = PL_statcache.st_atime_n; |
745 | *mtime_nsec = PL_statcache.st_mtime_n; | |
746 | *ctime_nsec = PL_statcache.st_ctime_n; | |
91a2e9f6 | 747 | #elif TIME_HIRES_STAT == 4 |
75d5269b SP |
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; | |
91a2e9f6 | 751 | #elif TIME_HIRES_STAT == 5 |
75d5269b SP |
752 | *atime_nsec = PL_statcache.st_uatime * 1000; |
753 | *mtime_nsec = PL_statcache.st_umtime * 1000; | |
754 | *ctime_nsec = PL_statcache.st_uctime * 1000; | |
91a2e9f6 Z |
755 | #else /* !TIME_HIRES_STAT */ |
756 | *atime_nsec = 0; | |
757 | *mtime_nsec = 0; | |
758 | *ctime_nsec = 0; | |
759 | #endif /* !TIME_HIRES_STAT */ | |
75d5269b SP |
760 | } |
761 | ||
1e845d0c JH |
762 | /* Until Apple implements clock_gettime() (ditto clock_getres()) |
763 | * we will emulate it using Mach interfaces. */ | |
764 | #if defined(PERL_DARWIN) && !defined(CLOCK_REALTIME) | |
765 | ||
766 | # include <mach/mach_time.h> | |
767 | ||
768 | # define CLOCK_REALTIME 0x01 | |
769 | # define CLOCK_MONOTONIC 0x02 | |
770 | ||
771 | # define TIMER_ABSTIME 0x01 | |
772 | ||
773 | #ifdef USE_ITHREADS | |
774 | STATIC perl_mutex darwin_time_mutex; | |
775 | #endif | |
776 | ||
777 | static uint64_t absolute_time_init; | |
778 | static mach_timebase_info_data_t timebase_info; | |
779 | static struct timespec timespec_init; | |
780 | ||
781 | static int darwin_time_init() { | |
782 | #ifdef USE_ITHREAD | |
783 | PERL_MUTEX_LOCK(&darwin_time_mutex); | |
784 | #endif | |
785 | struct timeval tv; | |
786 | int success = 1; | |
787 | if (absolute_time_init == 0) { | |
788 | /* mach_absolute_time() cannot fail */ | |
789 | absolute_time_init = mach_absolute_time(); | |
790 | success = mach_timebase_info(&timebase_info) == KERN_SUCCESS; | |
791 | if (success) { | |
792 | success = gettimeofday(&tv, NULL) == 0; | |
793 | if (success) { | |
794 | timespec_init.tv_sec = tv.tv_sec; | |
795 | timespec_init.tv_nsec = tv.tv_usec * 1000; | |
796 | } | |
797 | } | |
798 | } | |
799 | #ifdef USE_ITHREAD | |
800 | PERL_MUTEX_UNLOCK(&darwin_time_mutex); | |
801 | #endif | |
802 | return success; | |
803 | } | |
804 | ||
805 | static int clock_gettime(int clock_id, struct timespec *ts) { | |
806 | if (darwin_time_init() && timebase_info.denom) { | |
807 | switch (clock_id) { | |
808 | case CLOCK_REALTIME: | |
809 | { | |
810 | uint64_t nanos = | |
811 | ((mach_absolute_time() - absolute_time_init) * | |
812 | (uint64_t)timebase_info.numer) / (uint64_t)timebase_info.denom; | |
813 | ts->tv_sec = timespec_init.tv_sec + nanos / IV_1E9; | |
814 | ts->tv_nsec = timespec_init.tv_nsec + nanos % IV_1E9; | |
815 | return 0; | |
816 | } | |
817 | ||
818 | case CLOCK_MONOTONIC: | |
819 | { | |
820 | uint64_t nanos = | |
821 | (mach_absolute_time() * | |
822 | (uint64_t)timebase_info.numer) / (uint64_t)timebase_info.denom; | |
823 | ts->tv_sec = nanos / IV_1E9; | |
824 | ts->tv_nsec = nanos - ts->tv_sec * IV_1E9; | |
825 | return 0; | |
826 | } | |
827 | ||
828 | default: | |
829 | break; | |
830 | } | |
831 | } | |
832 | ||
833 | SETERRNO(EINVAL, LIB_INVARG); | |
834 | return -1; | |
835 | } | |
836 | ||
837 | static int clock_getres(int clock_id, struct timespec *ts) { | |
838 | if (darwin_time_init() && timebase_info.denom) { | |
839 | switch (clock_id) { | |
840 | case CLOCK_REALTIME: | |
841 | case CLOCK_MONOTONIC: | |
842 | ts->tv_sec = 0; | |
843 | /* In newer kernels both the numer and denom are one, | |
844 | * resulting in conversion factor of one, which is of | |
845 | * course unrealistic. */ | |
846 | ts->tv_nsec = timebase_info.numer / timebase_info.denom; | |
847 | return 0; | |
848 | default: | |
849 | break; | |
850 | } | |
851 | } | |
852 | ||
853 | SETERRNO(EINVAL, LIB_INVARG); | |
854 | return -1; | |
855 | } | |
856 | ||
3b614a59 JH |
857 | static int clock_nanosleep(int clock_id, int flags, |
858 | const struct timespec *rqtp, | |
859 | struct timespec *rmtp) { | |
860 | if (darwin_time_init()) { | |
861 | switch (clock_id) { | |
862 | case CLOCK_REALTIME: | |
863 | case CLOCK_MONOTONIC: | |
864 | { | |
865 | uint64_t nanos = rqtp->tv_sec * IV_1E9 + rqtp->tv_nsec; | |
866 | int success; | |
867 | if ((flags & TIMER_ABSTIME)) { | |
868 | uint64_t back = | |
869 | timespec_init.tv_sec * IV_1E9 + timespec_init.tv_nsec; | |
870 | nanos = nanos > back ? nanos - back : 0; | |
871 | } | |
872 | success = | |
873 | mach_wait_until(mach_absolute_time() + nanos) == KERN_SUCCESS; | |
874 | ||
875 | /* In the relative sleep, the rmtp should be filled in with | |
876 | * the 'unused' part of the rqtp in case the sleep gets | |
877 | * interrupted by a signal. But it is unknown how signals | |
878 | * interact with mach_wait_until(). In the absolute sleep, | |
879 | * the rmtp should stay untouched. */ | |
880 | rmtp->tv_sec = 0; | |
881 | rmtp->tv_nsec = 0; | |
882 | ||
883 | return success; | |
884 | } | |
885 | ||
886 | default: | |
887 | break; | |
888 | } | |
889 | } | |
890 | ||
891 | SETERRNO(EINVAL, LIB_INVARG); | |
892 | return -1; | |
893 | } | |
894 | ||
1e845d0c JH |
895 | #endif /* PERL_DARWIN */ |
896 | ||
82cbdcc3 SP |
897 | #include "const-c.inc" |
898 | ||
24e1662b JH |
899 | #if (defined(TIME_HIRES_NANOSLEEP)) || \ |
900 | (defined(TIME_HIRES_CLOCK_NANOSLEEP) && defined(TIMER_ABSTIME)) | |
901 | ||
902 | static NV | |
903 | nsec_without_unslept(struct timespec *sleepfor, | |
904 | const struct timespec *unslept) { | |
ea21af8c JH |
905 | if (sleepfor->tv_sec >= unslept->tv_sec) { |
906 | sleepfor->tv_sec -= unslept->tv_sec; | |
907 | if (sleepfor->tv_nsec >= unslept->tv_nsec) { | |
908 | sleepfor->tv_nsec -= unslept->tv_nsec; | |
909 | } else if (sleepfor->tv_sec > 0) { | |
910 | sleepfor->tv_sec--; | |
911 | sleepfor->tv_nsec += IV_1E9; | |
912 | sleepfor->tv_nsec -= unslept->tv_nsec; | |
913 | } else { | |
914 | sleepfor->tv_sec = 0; | |
915 | sleepfor->tv_nsec = 0; | |
916 | } | |
917 | } else { | |
918 | sleepfor->tv_sec = 0; | |
919 | sleepfor->tv_nsec = 0; | |
24e1662b JH |
920 | } |
921 | return ((NV)sleepfor->tv_sec) * NV_1E9 + ((NV)sleepfor->tv_nsec); | |
922 | } | |
923 | ||
924 | #endif | |
925 | ||
dcf686c9 JH |
926 | MODULE = Time::HiRes PACKAGE = Time::HiRes |
927 | ||
928 | PROTOTYPES: ENABLE | |
929 | ||
930 | BOOT: | |
0225372c RGS |
931 | { |
932 | #ifdef MY_CXT_KEY | |
933 | MY_CXT_INIT; | |
934 | #endif | |
3f2ee006 | 935 | #ifdef ATLEASTFIVEOHOHFIVE |
9e000d5b | 936 | # ifdef HAS_GETTIMEOFDAY |
0225372c | 937 | { |
91a2e9f6 Z |
938 | (void) hv_store(PL_modglobal, "Time::NVtime", 12, |
939 | newSViv(PTR2IV(myNVtime)), 0); | |
940 | (void) hv_store(PL_modglobal, "Time::U2time", 12, | |
941 | newSViv(PTR2IV(myU2time)), 0); | |
0225372c | 942 | } |
9e000d5b | 943 | # endif |
dcf686c9 | 944 | #endif |
0225372c RGS |
945 | } |
946 | ||
947 | #if defined(USE_ITHREADS) && defined(MY_CXT_KEY) | |
948 | ||
949 | void | |
950 | CLONE(...) | |
951 | CODE: | |
952 | MY_CXT_CLONE; | |
953 | ||
3f2ee006 | 954 | #endif |
dcf686c9 | 955 | |
98b50af3 | 956 | INCLUDE: const-xs.inc |
3c72ec00 | 957 | |
52d72fba | 958 | #if defined(HAS_USLEEP) && defined(HAS_GETTIMEOFDAY) |
dcf686c9 | 959 | |
92bc48ca | 960 | NV |
dcf686c9 | 961 | usleep(useconds) |
92bc48ca | 962 | NV useconds |
52d72fba JH |
963 | PREINIT: |
964 | struct timeval Ta, Tb; | |
965 | CODE: | |
6e3b076d | 966 | gettimeofday(&Ta, NULL); |
52d72fba | 967 | if (items > 0) { |
de17b44f JH |
968 | if (useconds >= NV_1E6) { |
969 | IV seconds = (IV) (useconds / NV_1E6); | |
f7916ddb JH |
970 | /* If usleep() has been implemented using setitimer() |
971 | * then this contortion is unnecessary-- but usleep() | |
972 | * may be implemented in some other way, so let's contort. */ | |
973 | if (seconds) { | |
974 | sleep(seconds); | |
de17b44f | 975 | useconds -= NV_1E6 * seconds; |
f7916ddb JH |
976 | } |
977 | } else if (useconds < 0.0) | |
978 | croak("Time::HiRes::usleep(%"NVgf"): negative time not invented yet", useconds); | |
9a2ac92c | 979 | usleep((U32)useconds); |
52d72fba JH |
980 | } else |
981 | PerlProc_pause(); | |
6e3b076d | 982 | gettimeofday(&Tb, NULL); |
92bc48ca JH |
983 | #if 0 |
984 | printf("[%ld %ld] [%ld %ld]\n", Tb.tv_sec, Tb.tv_usec, Ta.tv_sec, Ta.tv_usec); | |
985 | #endif | |
de17b44f | 986 | RETVAL = NV_1E6*(Tb.tv_sec-Ta.tv_sec)+(NV)((IV)Tb.tv_usec-(IV)Ta.tv_usec); |
dcf686c9 | 987 | |
52d72fba JH |
988 | OUTPUT: |
989 | RETVAL | |
990 | ||
44d3ce20 RGS |
991 | #if defined(TIME_HIRES_NANOSLEEP) |
992 | ||
993 | NV | |
170c5524 SP |
994 | nanosleep(nsec) |
995 | NV nsec | |
44d3ce20 | 996 | PREINIT: |
a8fb48f7 | 997 | struct timespec sleepfor, unslept; |
44d3ce20 | 998 | CODE: |
a8fb48f7 SP |
999 | if (nsec < 0.0) |
1000 | croak("Time::HiRes::nanosleep(%"NVgf"): negative time not invented yet", nsec); | |
de17b44f JH |
1001 | sleepfor.tv_sec = (Time_t)(nsec / NV_1E9); |
1002 | sleepfor.tv_nsec = (long)(nsec - ((NV)sleepfor.tv_sec) * NV_1E9); | |
c8ea02b8 JH |
1003 | unslept.tv_sec = 0; |
1004 | unslept.tv_nsec = 0; | |
6ac7c900 | 1005 | if (nanosleep(&sleepfor, &unslept) == 0) { |
a8fb48f7 | 1006 | RETVAL = nsec; |
170c5524 | 1007 | } else { |
24e1662b | 1008 | RETVAL = nsec_without_unslept(&sleepfor, &unslept); |
170c5524 | 1009 | } |
a8fb48f7 | 1010 | OUTPUT: |
44d3ce20 RGS |
1011 | RETVAL |
1012 | ||
ced84e60 SP |
1013 | #else /* #if defined(TIME_HIRES_NANOSLEEP) */ |
1014 | ||
1015 | NV | |
170c5524 SP |
1016 | nanosleep(nsec) |
1017 | NV nsec | |
ced84e60 | 1018 | CODE: |
4451fd4e | 1019 | PERL_UNUSED_ARG(nsec); |
ced84e60 SP |
1020 | croak("Time::HiRes::nanosleep(): unimplemented in this platform"); |
1021 | RETVAL = 0.0; | |
858dcda5 Z |
1022 | OUTPUT: |
1023 | RETVAL | |
ced84e60 | 1024 | |
44d3ce20 RGS |
1025 | #endif /* #if defined(TIME_HIRES_NANOSLEEP) */ |
1026 | ||
52d72fba | 1027 | NV |
f9d00e57 | 1028 | sleep(...) |
52d72fba JH |
1029 | PREINIT: |
1030 | struct timeval Ta, Tb; | |
dcf686c9 | 1031 | CODE: |
6e3b076d | 1032 | gettimeofday(&Ta, NULL); |
92bc48ca JH |
1033 | if (items > 0) { |
1034 | NV seconds = SvNV(ST(0)); | |
f7916ddb | 1035 | if (seconds >= 0.0) { |
7c436af3 | 1036 | UV useconds = (UV)(1E6 * (seconds - (UV)seconds)); |
4880edd6 | 1037 | if (seconds >= 1.0) |
9a2ac92c | 1038 | sleep((U32)seconds); |
db0b859f JH |
1039 | if ((IV)useconds < 0) { |
1040 | #if defined(__sparc64__) && defined(__GNUC__) | |
1041 | /* Sparc64 gcc 2.95.3 (e.g. on NetBSD) has a bug | |
1042 | * where (0.5 - (UV)(0.5)) will under certain | |
1043 | * circumstances (if the double is cast to UV more | |
1044 | * than once?) evaluate to -0.5, instead of 0.5. */ | |
1045 | useconds = -(IV)useconds; | |
44d3ce20 | 1046 | #endif /* #if defined(__sparc64__) && defined(__GNUC__) */ |
db0b859f JH |
1047 | if ((IV)useconds < 0) |
1048 | croak("Time::HiRes::sleep(%"NVgf"): internal error: useconds < 0 (unsigned %"UVuf" signed %"IVdf")", seconds, useconds, (IV)useconds); | |
1049 | } | |
f7916ddb JH |
1050 | usleep(useconds); |
1051 | } else | |
1052 | croak("Time::HiRes::sleep(%"NVgf"): negative time not invented yet", seconds); | |
92bc48ca | 1053 | } else |
f9d00e57 | 1054 | PerlProc_pause(); |
6e3b076d | 1055 | gettimeofday(&Tb, NULL); |
92bc48ca JH |
1056 | #if 0 |
1057 | printf("[%ld %ld] [%ld %ld]\n", Tb.tv_sec, Tb.tv_usec, Ta.tv_sec, Ta.tv_usec); | |
1058 | #endif | |
52d72fba JH |
1059 | RETVAL = (NV)(Tb.tv_sec-Ta.tv_sec)+0.000001*(NV)(Tb.tv_usec-Ta.tv_usec); |
1060 | ||
1061 | OUTPUT: | |
1062 | RETVAL | |
dcf686c9 | 1063 | |
ced84e60 SP |
1064 | #else /* #if defined(HAS_USLEEP) && defined(HAS_GETTIMEOFDAY) */ |
1065 | ||
1066 | NV | |
1067 | usleep(useconds) | |
1068 | NV useconds | |
1069 | CODE: | |
4451fd4e | 1070 | PERL_UNUSED_ARG(useconds); |
ced84e60 SP |
1071 | croak("Time::HiRes::usleep(): unimplemented in this platform"); |
1072 | RETVAL = 0.0; | |
858dcda5 Z |
1073 | OUTPUT: |
1074 | RETVAL | |
ced84e60 | 1075 | |
44d3ce20 | 1076 | #endif /* #if defined(HAS_USLEEP) && defined(HAS_GETTIMEOFDAY) */ |
dcf686c9 JH |
1077 | |
1078 | #ifdef HAS_UALARM | |
1079 | ||
bf8300de RGS |
1080 | IV |
1081 | ualarm(useconds,uinterval=0) | |
dcf686c9 | 1082 | int useconds |
bf8300de | 1083 | int uinterval |
f7916ddb | 1084 | CODE: |
bf8300de RGS |
1085 | if (useconds < 0 || uinterval < 0) |
1086 | croak("Time::HiRes::ualarm(%d, %d): negative time not invented yet", useconds, uinterval); | |
34f69483 | 1087 | #if defined(HAS_SETITIMER) && defined(ITIMER_REAL) |
bf8300de RGS |
1088 | { |
1089 | struct itimerval itv; | |
1090 | if (hrt_ualarm_itimero(&itv, useconds, uinterval)) { | |
68c5b4db CBW |
1091 | /* To conform to ualarm's interface, we're actually ignoring |
1092 | an error here. */ | |
bf8300de | 1093 | RETVAL = 0; |
68c5b4db CBW |
1094 | } else { |
1095 | RETVAL = itv.it_value.tv_sec * IV_1E6 + itv.it_value.tv_usec; | |
bf8300de RGS |
1096 | } |
1097 | } | |
34f69483 | 1098 | #else |
e5620114 | 1099 | if (useconds >= IV_1E6 || uinterval >= IV_1E6) |
bf8300de | 1100 | croak("Time::HiRes::ualarm(%d, %d): useconds or uinterval equal to or more than %"IVdf, useconds, uinterval, IV_1E6); |
e5620114 | 1101 | RETVAL = ualarm(useconds, uinterval); |
34f69483 | 1102 | #endif |
dcf686c9 | 1103 | |
f7916ddb JH |
1104 | OUTPUT: |
1105 | RETVAL | |
1106 | ||
1107 | NV | |
1108 | alarm(seconds,interval=0) | |
1109 | NV seconds | |
1110 | NV interval | |
dcf686c9 | 1111 | CODE: |
f7916ddb JH |
1112 | if (seconds < 0.0 || interval < 0.0) |
1113 | croak("Time::HiRes::alarm(%"NVgf", %"NVgf"): negative time not invented yet", seconds, interval); | |
bf8300de | 1114 | { |
0f0eae2c Z |
1115 | IV iseconds = (IV)seconds; |
1116 | IV iinterval = (IV)interval; | |
1117 | NV fseconds = seconds - iseconds; | |
1118 | NV finterval = interval - iinterval; | |
1119 | IV useconds, uinterval; | |
1120 | if (fseconds >= 1.0 || finterval >= 1.0) | |
1121 | croak("Time::HiRes::alarm(%"NVgf", %"NVgf"): seconds or interval too large to split correctly", seconds, interval); | |
1122 | useconds = IV_1E6 * fseconds; | |
1123 | uinterval = IV_1E6 * finterval; | |
bf8300de RGS |
1124 | #if defined(HAS_SETITIMER) && defined(ITIMER_REAL) |
1125 | { | |
0f0eae2c Z |
1126 | struct itimerval nitv, oitv; |
1127 | nitv.it_value.tv_sec = iseconds; | |
1128 | nitv.it_value.tv_usec = useconds; | |
1129 | nitv.it_interval.tv_sec = iinterval; | |
1130 | nitv.it_interval.tv_usec = uinterval; | |
1131 | if (setitimer(ITIMER_REAL, &nitv, &oitv)) { | |
68c5b4db CBW |
1132 | /* To conform to alarm's interface, we're actually ignoring |
1133 | an error here. */ | |
bf8300de | 1134 | RETVAL = 0; |
68c5b4db | 1135 | } else { |
0f0eae2c | 1136 | RETVAL = oitv.it_value.tv_sec + ((NV)oitv.it_value.tv_usec) / NV_1E6; |
bf8300de RGS |
1137 | } |
1138 | } | |
1139 | #else | |
0f0eae2c Z |
1140 | if (iseconds || iinterval) |
1141 | croak("Time::HiRes::alarm(%"NVgf", %"NVgf"): seconds or interval equal to or more than 1.0 ", seconds, interval); | |
e5620114 | 1142 | RETVAL = (NV)ualarm( useconds, uinterval ) / NV_1E6; |
bf8300de RGS |
1143 | #endif |
1144 | } | |
dcf686c9 | 1145 | |
c6c619a9 DM |
1146 | OUTPUT: |
1147 | RETVAL | |
1148 | ||
ced84e60 SP |
1149 | #else |
1150 | ||
1151 | int | |
1152 | ualarm(useconds,interval=0) | |
1153 | int useconds | |
1154 | int interval | |
1155 | CODE: | |
4451fd4e SH |
1156 | PERL_UNUSED_ARG(useconds); |
1157 | PERL_UNUSED_ARG(interval); | |
ced84e60 SP |
1158 | croak("Time::HiRes::ualarm(): unimplemented in this platform"); |
1159 | RETVAL = -1; | |
858dcda5 Z |
1160 | OUTPUT: |
1161 | RETVAL | |
ced84e60 SP |
1162 | |
1163 | NV | |
1164 | alarm(seconds,interval=0) | |
1165 | NV seconds | |
1166 | NV interval | |
1167 | CODE: | |
4451fd4e SH |
1168 | PERL_UNUSED_ARG(seconds); |
1169 | PERL_UNUSED_ARG(interval); | |
ced84e60 SP |
1170 | croak("Time::HiRes::alarm(): unimplemented in this platform"); |
1171 | RETVAL = 0.0; | |
858dcda5 Z |
1172 | OUTPUT: |
1173 | RETVAL | |
ced84e60 | 1174 | |
44d3ce20 | 1175 | #endif /* #ifdef HAS_UALARM */ |
dcf686c9 JH |
1176 | |
1177 | #ifdef HAS_GETTIMEOFDAY | |
db835671 JH |
1178 | # ifdef MACOS_TRADITIONAL /* fix epoch TZ and use unsigned time_t */ |
1179 | void | |
1180 | gettimeofday() | |
1181 | PREINIT: | |
1182 | struct timeval Tp; | |
1183 | struct timezone Tz; | |
1184 | PPCODE: | |
1185 | int status; | |
6e3b076d | 1186 | status = gettimeofday (&Tp, &Tz); |
db835671 | 1187 | |
993164ab RGS |
1188 | if (status == 0) { |
1189 | Tp.tv_sec += Tz.tz_minuteswest * 60; /* adjust for TZ */ | |
1190 | if (GIMME == G_ARRAY) { | |
1191 | EXTEND(sp, 2); | |
1192 | /* Mac OS (Classic) has unsigned time_t */ | |
1193 | PUSHs(sv_2mortal(newSVuv(Tp.tv_sec))); | |
1194 | PUSHs(sv_2mortal(newSViv(Tp.tv_usec))); | |
1195 | } else { | |
1196 | EXTEND(sp, 1); | |
47e797f6 | 1197 | PUSHs(sv_2mortal(newSVnv(Tp.tv_sec + (Tp.tv_usec / NV_1E6)))); |
993164ab | 1198 | } |
db835671 JH |
1199 | } |
1200 | ||
1201 | NV | |
1202 | time() | |
1203 | PREINIT: | |
1204 | struct timeval Tp; | |
1205 | struct timezone Tz; | |
1206 | CODE: | |
1207 | int status; | |
6e3b076d | 1208 | status = gettimeofday (&Tp, &Tz); |
993164ab RGS |
1209 | if (status == 0) { |
1210 | Tp.tv_sec += Tz.tz_minuteswest * 60; /* adjust for TZ */ | |
47e797f6 | 1211 | RETVAL = Tp.tv_sec + (Tp.tv_usec / NV_1E6); |
993164ab RGS |
1212 | } else { |
1213 | RETVAL = -1.0; | |
1214 | } | |
db835671 JH |
1215 | OUTPUT: |
1216 | RETVAL | |
dcf686c9 | 1217 | |
db835671 | 1218 | # else /* MACOS_TRADITIONAL */ |
dcf686c9 JH |
1219 | void |
1220 | gettimeofday() | |
1221 | PREINIT: | |
1222 | struct timeval Tp; | |
1223 | PPCODE: | |
1224 | int status; | |
6e3b076d | 1225 | status = gettimeofday (&Tp, NULL); |
993164ab RGS |
1226 | if (status == 0) { |
1227 | if (GIMME == G_ARRAY) { | |
1228 | EXTEND(sp, 2); | |
1229 | PUSHs(sv_2mortal(newSViv(Tp.tv_sec))); | |
1230 | PUSHs(sv_2mortal(newSViv(Tp.tv_usec))); | |
1231 | } else { | |
1232 | EXTEND(sp, 1); | |
47e797f6 | 1233 | PUSHs(sv_2mortal(newSVnv(Tp.tv_sec + (Tp.tv_usec / NV_1E6)))); |
993164ab | 1234 | } |
dcf686c9 JH |
1235 | } |
1236 | ||
3c72ec00 | 1237 | NV |
dcf686c9 JH |
1238 | time() |
1239 | PREINIT: | |
1240 | struct timeval Tp; | |
1241 | CODE: | |
1242 | int status; | |
6e3b076d | 1243 | status = gettimeofday (&Tp, NULL); |
993164ab | 1244 | if (status == 0) { |
47e797f6 | 1245 | RETVAL = Tp.tv_sec + (Tp.tv_usec / NV_1E6); |
993164ab RGS |
1246 | } else { |
1247 | RETVAL = -1.0; | |
1248 | } | |
dcf686c9 JH |
1249 | OUTPUT: |
1250 | RETVAL | |
1251 | ||
db835671 | 1252 | # endif /* MACOS_TRADITIONAL */ |
44d3ce20 | 1253 | #endif /* #ifdef HAS_GETTIMEOFDAY */ |
dcf686c9 | 1254 | |
3c72ec00 JH |
1255 | #if defined(HAS_GETITIMER) && defined(HAS_SETITIMER) |
1256 | ||
1257 | #define TV2NV(tv) ((NV)((tv).tv_sec) + 0.000001 * (NV)((tv).tv_usec)) | |
1258 | ||
1259 | void | |
1260 | setitimer(which, seconds, interval = 0) | |
1261 | int which | |
1262 | NV seconds | |
1263 | NV interval | |
1264 | PREINIT: | |
1265 | struct itimerval newit; | |
1266 | struct itimerval oldit; | |
1267 | PPCODE: | |
f7916ddb | 1268 | if (seconds < 0.0 || interval < 0.0) |
436c6dd3 | 1269 | croak("Time::HiRes::setitimer(%"IVdf", %"NVgf", %"NVgf"): negative time not invented yet", (IV)which, seconds, interval); |
47e797f6 | 1270 | newit.it_value.tv_sec = (IV)seconds; |
3c72ec00 | 1271 | newit.it_value.tv_usec = |
47e797f6 SP |
1272 | (IV)((seconds - (NV)newit.it_value.tv_sec) * NV_1E6); |
1273 | newit.it_interval.tv_sec = (IV)interval; | |
3c72ec00 | 1274 | newit.it_interval.tv_usec = |
47e797f6 | 1275 | (IV)((interval - (NV)newit.it_interval.tv_sec) * NV_1E6); |
120b53f9 RS |
1276 | /* on some platforms the 1st arg to setitimer is an enum, which |
1277 | * causes -Wc++-compat to complain about passing an int instead | |
1278 | */ | |
1279 | #ifdef GCC_DIAG_IGNORE | |
1280 | GCC_DIAG_IGNORE(-Wc++-compat); | |
1281 | #endif | |
3c72ec00 JH |
1282 | if (setitimer(which, &newit, &oldit) == 0) { |
1283 | EXTEND(sp, 1); | |
1284 | PUSHs(sv_2mortal(newSVnv(TV2NV(oldit.it_value)))); | |
1285 | if (GIMME == G_ARRAY) { | |
1286 | EXTEND(sp, 1); | |
1287 | PUSHs(sv_2mortal(newSVnv(TV2NV(oldit.it_interval)))); | |
1288 | } | |
1289 | } | |
120b53f9 RS |
1290 | #ifdef GCC_DIAG_RESTORE |
1291 | GCC_DIAG_RESTORE; | |
1292 | #endif | |
3c72ec00 JH |
1293 | |
1294 | void | |
1295 | getitimer(which) | |
1296 | int which | |
1297 | PREINIT: | |
1298 | struct itimerval nowit; | |
1299 | PPCODE: | |
120b53f9 RS |
1300 | /* on some platforms the 1st arg to getitimer is an enum, which |
1301 | * causes -Wc++-compat to complain about passing an int instead | |
1302 | */ | |
1303 | #ifdef GCC_DIAG_IGNORE | |
1304 | GCC_DIAG_IGNORE(-Wc++-compat); | |
1305 | #endif | |
3c72ec00 JH |
1306 | if (getitimer(which, &nowit) == 0) { |
1307 | EXTEND(sp, 1); | |
1308 | PUSHs(sv_2mortal(newSVnv(TV2NV(nowit.it_value)))); | |
1309 | if (GIMME == G_ARRAY) { | |
1310 | EXTEND(sp, 1); | |
1311 | PUSHs(sv_2mortal(newSVnv(TV2NV(nowit.it_interval)))); | |
1312 | } | |
1313 | } | |
120b53f9 RS |
1314 | #ifdef GCC_DIAG_RESTORE |
1315 | GCC_DIAG_RESTORE; | |
1316 | #endif | |
3c72ec00 | 1317 | |
44d3ce20 RGS |
1318 | #endif /* #if defined(HAS_GETITIMER) && defined(HAS_SETITIMER) */ |
1319 | ||
ced84e60 SP |
1320 | #if defined(TIME_HIRES_CLOCK_GETTIME) |
1321 | ||
1322 | NV | |
1323 | clock_gettime(clock_id = CLOCK_REALTIME) | |
1324 | int clock_id | |
1325 | PREINIT: | |
1326 | struct timespec ts; | |
1327 | int status = -1; | |
1328 | CODE: | |
1329 | #ifdef TIME_HIRES_CLOCK_GETTIME_SYSCALL | |
1330 | status = syscall(SYS_clock_gettime, clock_id, &ts); | |
1331 | #else | |
1332 | status = clock_gettime(clock_id, &ts); | |
1333 | #endif | |
de17b44f | 1334 | RETVAL = status == 0 ? ts.tv_sec + (NV) ts.tv_nsec / NV_1E9 : -1; |
ced84e60 SP |
1335 | |
1336 | OUTPUT: | |
1337 | RETVAL | |
1338 | ||
1339 | #else /* if defined(TIME_HIRES_CLOCK_GETTIME) */ | |
1340 | ||
1341 | NV | |
1342 | clock_gettime(clock_id = 0) | |
1343 | int clock_id | |
1344 | CODE: | |
4451fd4e | 1345 | PERL_UNUSED_ARG(clock_id); |
ced84e60 SP |
1346 | croak("Time::HiRes::clock_gettime(): unimplemented in this platform"); |
1347 | RETVAL = 0.0; | |
858dcda5 Z |
1348 | OUTPUT: |
1349 | RETVAL | |
ced84e60 SP |
1350 | |
1351 | #endif /* #if defined(TIME_HIRES_CLOCK_GETTIME) */ | |
1352 | ||
1353 | #if defined(TIME_HIRES_CLOCK_GETRES) | |
1354 | ||
1355 | NV | |
1356 | clock_getres(clock_id = CLOCK_REALTIME) | |
1357 | int clock_id | |
1358 | PREINIT: | |
1359 | int status = -1; | |
1360 | struct timespec ts; | |
1361 | CODE: | |
1362 | #ifdef TIME_HIRES_CLOCK_GETRES_SYSCALL | |
1363 | status = syscall(SYS_clock_getres, clock_id, &ts); | |
1364 | #else | |
1365 | status = clock_getres(clock_id, &ts); | |
1366 | #endif | |
de17b44f | 1367 | RETVAL = status == 0 ? ts.tv_sec + (NV) ts.tv_nsec / NV_1E9 : -1; |
ced84e60 SP |
1368 | |
1369 | OUTPUT: | |
1370 | RETVAL | |
1371 | ||
1372 | #else /* if defined(TIME_HIRES_CLOCK_GETRES) */ | |
1373 | ||
1374 | NV | |
1375 | clock_getres(clock_id = 0) | |
1376 | int clock_id | |
1377 | CODE: | |
4451fd4e | 1378 | PERL_UNUSED_ARG(clock_id); |
ced84e60 SP |
1379 | croak("Time::HiRes::clock_getres(): unimplemented in this platform"); |
1380 | RETVAL = 0.0; | |
858dcda5 Z |
1381 | OUTPUT: |
1382 | RETVAL | |
ced84e60 SP |
1383 | |
1384 | #endif /* #if defined(TIME_HIRES_CLOCK_GETRES) */ | |
1385 | ||
170c5524 SP |
1386 | #if defined(TIME_HIRES_CLOCK_NANOSLEEP) && defined(TIMER_ABSTIME) |
1387 | ||
1388 | NV | |
a8fb48f7 | 1389 | clock_nanosleep(clock_id, nsec, flags = 0) |
170c5524 | 1390 | int clock_id |
a8fb48f7 | 1391 | NV nsec |
170c5524 SP |
1392 | int flags |
1393 | PREINIT: | |
a8fb48f7 | 1394 | struct timespec sleepfor, unslept; |
170c5524 | 1395 | CODE: |
a8fb48f7 SP |
1396 | if (nsec < 0.0) |
1397 | croak("Time::HiRes::clock_nanosleep(..., %"NVgf"): negative time not invented yet", nsec); | |
de17b44f JH |
1398 | sleepfor.tv_sec = (Time_t)(nsec / NV_1E9); |
1399 | sleepfor.tv_nsec = (long)(nsec - ((NV)sleepfor.tv_sec) * NV_1E9); | |
c8ea02b8 JH |
1400 | unslept.tv_sec = 0; |
1401 | unslept.tv_nsec = 0; | |
6ac7c900 | 1402 | if (clock_nanosleep(clock_id, flags, &sleepfor, &unslept) == 0) { |
a8fb48f7 | 1403 | RETVAL = nsec; |
170c5524 | 1404 | } else { |
24e1662b | 1405 | RETVAL = nsec_without_unslept(&sleepfor, &unslept); |
170c5524 | 1406 | } |
170c5524 SP |
1407 | OUTPUT: |
1408 | RETVAL | |
1409 | ||
1410 | #else /* if defined(TIME_HIRES_CLOCK_NANOSLEEP) && defined(TIMER_ABSTIME) */ | |
1411 | ||
1412 | NV | |
90e44bf6 | 1413 | clock_nanosleep(clock_id, nsec, flags = 0) |
4451fd4e SH |
1414 | int clock_id |
1415 | NV nsec | |
1416 | int flags | |
170c5524 | 1417 | CODE: |
4451fd4e SH |
1418 | PERL_UNUSED_ARG(clock_id); |
1419 | PERL_UNUSED_ARG(nsec); | |
1420 | PERL_UNUSED_ARG(flags); | |
170c5524 SP |
1421 | croak("Time::HiRes::clock_nanosleep(): unimplemented in this platform"); |
1422 | RETVAL = 0.0; | |
858dcda5 Z |
1423 | OUTPUT: |
1424 | RETVAL | |
170c5524 SP |
1425 | |
1426 | #endif /* #if defined(TIME_HIRES_CLOCK_NANOSLEEP) && defined(TIMER_ABSTIME) */ | |
1427 | ||
1428 | #if defined(TIME_HIRES_CLOCK) && defined(CLOCKS_PER_SEC) | |
1429 | ||
1430 | NV | |
1431 | clock() | |
1432 | PREINIT: | |
1433 | clock_t clocks; | |
1434 | CODE: | |
1435 | clocks = clock(); | |
120b53f9 | 1436 | RETVAL = clocks == (clock_t) -1 ? (clock_t) -1 : (NV)clocks / (NV)CLOCKS_PER_SEC; |
170c5524 SP |
1437 | |
1438 | OUTPUT: | |
1439 | RETVAL | |
1440 | ||
1441 | #else /* if defined(TIME_HIRES_CLOCK) && defined(CLOCKS_PER_SEC) */ | |
1442 | ||
1443 | NV | |
1444 | clock() | |
1445 | CODE: | |
1446 | croak("Time::HiRes::clock(): unimplemented in this platform"); | |
1447 | RETVAL = 0.0; | |
858dcda5 Z |
1448 | OUTPUT: |
1449 | RETVAL | |
170c5524 SP |
1450 | |
1451 | #endif /* #if defined(TIME_HIRES_CLOCK) && defined(CLOCKS_PER_SEC) */ | |
1452 | ||
86413ec0 | 1453 | void |
75d5269b SP |
1454 | stat(...) |
1455 | PROTOTYPE: ;$ | |
0f0eae2c Z |
1456 | PREINIT: |
1457 | OP fakeop; | |
1458 | int nret; | |
1459 | ALIAS: | |
1460 | Time::HiRes::lstat = 1 | |
75d5269b | 1461 | PPCODE: |
75d5269b SP |
1462 | XPUSHs(sv_2mortal(newSVsv(items == 1 ? ST(0) : DEFSV))); |
1463 | PUTBACK; | |
1464 | ENTER; | |
1465 | PL_laststatval = -1; | |
0f0eae2c Z |
1466 | SAVEOP(); |
1467 | Zero(&fakeop, 1, OP); | |
1468 | fakeop.op_type = ix ? OP_LSTAT : OP_STAT; | |
1469 | fakeop.op_ppaddr = PL_ppaddr[fakeop.op_type]; | |
1470 | fakeop.op_flags = GIMME_V == G_ARRAY ? OPf_WANT_LIST : | |
1471 | GIMME_V == G_SCALAR ? OPf_WANT_SCALAR : OPf_WANT_VOID; | |
1472 | PL_op = &fakeop; | |
120b53f9 | 1473 | (void)fakeop.op_ppaddr(aTHX); |
75d5269b SP |
1474 | SPAGAIN; |
1475 | LEAVE; | |
0f0eae2c Z |
1476 | nret = SP+1 - &ST(0); |
1477 | if (nret == 13) { | |
75d5269b SP |
1478 | UV atime = SvUV(ST( 8)); |
1479 | UV mtime = SvUV(ST( 9)); | |
1480 | UV ctime = SvUV(ST(10)); | |
1481 | UV atime_nsec; | |
1482 | UV mtime_nsec; | |
1483 | UV ctime_nsec; | |
91a2e9f6 | 1484 | hrstatns(&atime_nsec, &mtime_nsec, &ctime_nsec); |
75d5269b | 1485 | if (atime_nsec) |
de17b44f | 1486 | ST( 8) = sv_2mortal(newSVnv(atime + (NV) atime_nsec / NV_1E9)); |
75d5269b | 1487 | if (mtime_nsec) |
de17b44f | 1488 | ST( 9) = sv_2mortal(newSVnv(mtime + (NV) mtime_nsec / NV_1E9)); |
75d5269b | 1489 | if (ctime_nsec) |
de17b44f | 1490 | ST(10) = sv_2mortal(newSVnv(ctime + (NV) ctime_nsec / NV_1E9)); |
75d5269b | 1491 | } |
0f0eae2c | 1492 | XSRETURN(nret); |