Commit | Line | Data |
dcf686c9 |
1 | #ifdef __cplusplus |
2 | extern "C" { |
3 | #endif |
4 | #include "EXTERN.h" |
5 | #include "perl.h" |
6 | #include "XSUB.h" |
7 | #ifdef WIN32 |
8 | #include <time.h> |
9 | #else |
10 | #include <sys/time.h> |
11 | #endif |
12 | #ifdef __cplusplus |
13 | } |
14 | #endif |
15 | |
3c72ec00 |
16 | static IV |
17 | constant(char *name, int arg) |
18 | { |
19 | errno = 0; |
20 | switch (*name) { |
21 | case 'I': |
22 | if (strEQ(name, "ITIMER_REAL")) |
23 | #ifdef ITIMER_REAL |
24 | return ITIMER_REAL; |
25 | #else |
26 | goto not_there; |
27 | #endif |
28 | if (strEQ(name, "ITIMER_REALPROF")) |
29 | #ifdef ITIMER_REALPROF |
30 | return ITIMER_REALPROF; |
31 | #else |
32 | goto not_there; |
33 | #endif |
34 | if (strEQ(name, "ITIMER_VIRTUAL")) |
35 | #ifdef ITIMER_VIRTUAL |
36 | return ITIMER_VIRTUAL; |
37 | #else |
38 | goto not_there; |
39 | #endif |
40 | if (strEQ(name, "ITIMER_PROF")) |
41 | #ifdef ITIMER_PROF |
42 | return ITIMER_PROF; |
43 | #else |
44 | goto not_there; |
45 | #endif |
46 | break; |
47 | } |
48 | errno = EINVAL; |
49 | return 0; |
50 | |
51 | not_there: |
52 | errno = ENOENT; |
53 | return 0; |
54 | } |
55 | |
dcf686c9 |
56 | #if !defined(HAS_GETTIMEOFDAY) && defined(WIN32) |
57 | #define HAS_GETTIMEOFDAY |
58 | |
59 | /* shows up in winsock.h? |
60 | struct timeval { |
61 | long tv_sec; |
62 | long tv_usec; |
63 | } |
64 | */ |
65 | |
eb1a5092 |
66 | typedef union { |
67 | unsigned __int64 ft_i64; |
68 | FILETIME ft_val; |
69 | } FT_t; |
70 | |
71 | /* Number of 100 nanosecond units from 1/1/1601 to 1/1/1970 */ |
72 | #define EPOCH_BIAS 116444736000000000i64 |
73 | |
74 | /* NOTE: This does not compute the timezone info (doing so can be expensive, |
75 | * and appears to be unsupported even by glibc) */ |
dcf686c9 |
76 | int |
eb1a5092 |
77 | gettimeofday (struct timeval *tp, void *not_used) |
dcf686c9 |
78 | { |
eb1a5092 |
79 | FT_t ft; |
80 | |
81 | /* this returns time in 100-nanosecond units (i.e. tens of usecs) */ |
82 | GetSystemTimeAsFileTime(&ft.ft_val); |
83 | |
84 | /* seconds since epoch */ |
85 | tp->tv_sec = (long)((ft.ft_i64 - EPOCH_BIAS) / 10000000i64); |
86 | |
87 | /* microseconds remaining */ |
88 | tp->tv_usec = (long)((ft.ft_i64 / 10i64) % 1000000i64); |
89 | |
90 | return 0; |
dcf686c9 |
91 | } |
92 | #endif |
93 | |
94 | #if !defined(HAS_GETTIMEOFDAY) && defined(VMS) |
95 | #define HAS_GETTIMEOFDAY |
96 | |
9b6f56ad |
97 | #include <lnmdef.h> |
dcf686c9 |
98 | #include <time.h> /* gettimeofday */ |
99 | #include <stdlib.h> /* qdiv */ |
100 | #include <starlet.h> /* sys$gettim */ |
101 | #include <descrip.h> |
3785778e |
102 | #ifdef __VAX |
103 | #include <lib$routines.h> /* lib$ediv() */ |
104 | #endif |
dcf686c9 |
105 | |
106 | /* |
107 | VMS binary time is expressed in 100 nano-seconds since |
108 | system base time which is 17-NOV-1858 00:00:00.00 |
109 | */ |
110 | |
111 | #define DIV_100NS_TO_SECS 10000000L |
112 | #define DIV_100NS_TO_USECS 10L |
113 | |
114 | /* |
115 | gettimeofday is supposed to return times since the epoch |
116 | so need to determine this in terms of VMS base time |
117 | */ |
118 | static $DESCRIPTOR(dscepoch,"01-JAN-1970 00:00:00.00"); |
119 | |
5cdb7193 |
120 | #ifdef __VAX |
3785778e |
121 | static long base_adjust[2]={0L,0L}; |
5cdb7193 |
122 | #else |
dcf686c9 |
123 | static __int64 base_adjust=0; |
5cdb7193 |
124 | #endif |
dcf686c9 |
125 | |
9b6f56ad |
126 | /* |
127 | |
128 | If we don't have gettimeofday, then likely we are on a VMS machine that |
129 | operates on local time rather than UTC...so we have to zone-adjust. |
130 | This code gleefully swiped from VMS.C |
131 | |
132 | */ |
133 | /* method used to handle UTC conversions: |
134 | * 1 == CRTL gmtime(); 2 == SYS$TIMEZONE_DIFFERENTIAL; 3 == no correction |
135 | */ |
136 | static int gmtime_emulation_type; |
137 | /* number of secs to add to UTC POSIX-style time to get local time */ |
138 | static long int utc_offset_secs; |
139 | static struct dsc$descriptor_s fildevdsc = |
140 | { 12, DSC$K_DTYPE_T, DSC$K_CLASS_S, "LNM$FILE_DEV" }; |
141 | static struct dsc$descriptor_s *fildev[] = { &fildevdsc, NULL }; |
142 | |
143 | static time_t toutc_dst(time_t loc) { |
144 | struct tm *rsltmp; |
145 | |
146 | if ((rsltmp = localtime(&loc)) == NULL) return -1; |
147 | loc -= utc_offset_secs; |
148 | if (rsltmp->tm_isdst) loc -= 3600; |
149 | return loc; |
150 | } |
151 | |
152 | static time_t toloc_dst(time_t utc) { |
153 | struct tm *rsltmp; |
154 | |
155 | utc += utc_offset_secs; |
156 | if ((rsltmp = localtime(&utc)) == NULL) return -1; |
157 | if (rsltmp->tm_isdst) utc += 3600; |
158 | return utc; |
159 | } |
160 | |
161 | #define _toutc(secs) ((secs) == (time_t) -1 ? (time_t) -1 : \ |
162 | ((gmtime_emulation_type || timezone_setup()), \ |
163 | (gmtime_emulation_type == 1 ? toutc_dst(secs) : \ |
164 | ((secs) - utc_offset_secs)))) |
165 | |
166 | #define _toloc(secs) ((secs) == (time_t) -1 ? (time_t) -1 : \ |
167 | ((gmtime_emulation_type || timezone_setup()), \ |
168 | (gmtime_emulation_type == 1 ? toloc_dst(secs) : \ |
169 | ((secs) + utc_offset_secs)))) |
170 | |
171 | static int |
172 | timezone_setup(void) |
173 | { |
174 | struct tm *tm_p; |
175 | |
176 | if (gmtime_emulation_type == 0) { |
177 | int dstnow; |
178 | time_t base = 15 * 86400; /* 15jan71; to avoid month/year ends between */ |
179 | /* results of calls to gmtime() and localtime() */ |
180 | /* for same &base */ |
181 | |
182 | gmtime_emulation_type++; |
183 | if ((tm_p = gmtime(&base)) == NULL) { /* CRTL gmtime() is a fake */ |
184 | char off[LNM$C_NAMLENGTH+1];; |
185 | |
186 | gmtime_emulation_type++; |
187 | if (!Perl_vmstrnenv("SYS$TIMEZONE_DIFFERENTIAL",off,0,fildev,0)) { |
188 | gmtime_emulation_type++; |
189 | utc_offset_secs = 0; |
190 | Perl_warn(aTHX_ "no UTC offset information; assuming local time is UTC"); |
191 | } |
192 | else { utc_offset_secs = atol(off); } |
193 | } |
194 | else { /* We've got a working gmtime() */ |
195 | struct tm gmt, local; |
196 | |
197 | gmt = *tm_p; |
198 | tm_p = localtime(&base); |
199 | local = *tm_p; |
200 | utc_offset_secs = (local.tm_mday - gmt.tm_mday) * 86400; |
201 | utc_offset_secs += (local.tm_hour - gmt.tm_hour) * 3600; |
202 | utc_offset_secs += (local.tm_min - gmt.tm_min) * 60; |
203 | utc_offset_secs += (local.tm_sec - gmt.tm_sec); |
204 | } |
205 | } |
206 | return 1; |
207 | } |
208 | |
209 | |
dcf686c9 |
210 | int |
211 | gettimeofday (struct timeval *tp, void *tpz) |
212 | { |
213 | long ret; |
5cdb7193 |
214 | #ifdef __VAX |
3785778e |
215 | long quad[2]; |
216 | long quad1[2]; |
217 | long div_100ns_to_secs; |
218 | long div_100ns_to_usecs; |
219 | long quo,rem; |
220 | long quo1,rem1; |
5cdb7193 |
221 | #else |
dcf686c9 |
222 | __int64 quad; |
223 | __qdiv_t ans1,ans2; |
5cdb7193 |
224 | #endif |
dcf686c9 |
225 | /* |
226 | In case of error, tv_usec = 0 and tv_sec = VMS condition code. |
227 | The return from function is also set to -1. |
228 | This is not exactly as per the manual page. |
229 | */ |
230 | |
231 | tp->tv_usec = 0; |
232 | |
3785778e |
233 | #ifdef __VAX |
234 | if (base_adjust[0]==0 && base_adjust[1]==0) { |
235 | #else |
dcf686c9 |
236 | if (base_adjust==0) { /* Need to determine epoch adjustment */ |
3785778e |
237 | #endif |
dcf686c9 |
238 | ret=sys$bintim(&dscepoch,&base_adjust); |
239 | if (1 != (ret &&1)) { |
240 | tp->tv_sec = ret; |
241 | return -1; |
242 | } |
243 | } |
244 | |
245 | ret=sys$gettim(&quad); /* Get VMS system time */ |
246 | if ((1 && ret) == 1) { |
5cdb7193 |
247 | #ifdef __VAX |
3785778e |
248 | quad[0] -= base_adjust[0]; /* convert to epoch offset */ |
249 | quad[1] -= base_adjust[1]; /* convert 2nd half of quadword */ |
250 | div_100ns_to_secs = DIV_100NS_TO_SECS; |
251 | div_100ns_to_usecs = DIV_100NS_TO_USECS; |
252 | lib$ediv(&div_100ns_to_secs,&quad,&quo,&rem); |
253 | quad1[0] = rem; |
254 | quad1[1] = 0L; |
255 | lib$ediv(&div_100ns_to_usecs,&quad1,&quo1,&rem1); |
256 | tp->tv_sec = quo; /* Whole seconds */ |
257 | tp->tv_usec = quo1; /* Micro-seconds */ |
5cdb7193 |
258 | #else |
3785778e |
259 | quad -= base_adjust; /* convert to epoch offset */ |
dcf686c9 |
260 | ans1=qdiv(quad,DIV_100NS_TO_SECS); |
261 | ans2=qdiv(ans1.rem,DIV_100NS_TO_USECS); |
262 | tp->tv_sec = ans1.quot; /* Whole seconds */ |
263 | tp->tv_usec = ans2.quot; /* Micro-seconds */ |
3785778e |
264 | #endif |
dcf686c9 |
265 | } else { |
266 | tp->tv_sec = ret; |
267 | return -1; |
268 | } |
9b6f56ad |
269 | # ifdef VMSISH_TIME |
270 | # ifdef RTL_USES_UTC |
271 | if (VMSISH_TIME) tp->tv_sec = _toloc(tp->tv_sec); |
272 | # else |
273 | if (!VMSISH_TIME) tp->tv_sec = _toutc(tp->tv_sec); |
274 | # endif |
275 | # endif |
dcf686c9 |
276 | return 0; |
277 | } |
278 | #endif |
279 | |
280 | #if !defined(HAS_USLEEP) && defined(HAS_SELECT) |
281 | #ifndef SELECT_IS_BROKEN |
282 | #define HAS_USLEEP |
283 | #define usleep hrt_usleep /* could conflict with ncurses for static build */ |
284 | |
285 | void |
286 | hrt_usleep(unsigned long usec) |
287 | { |
288 | struct timeval tv; |
289 | tv.tv_sec = 0; |
290 | tv.tv_usec = usec; |
291 | select(0, (Select_fd_set_t)NULL, (Select_fd_set_t)NULL, |
292 | (Select_fd_set_t)NULL, &tv); |
293 | } |
294 | #endif |
295 | #endif |
296 | |
297 | #if !defined(HAS_USLEEP) && defined(WIN32) |
298 | #define HAS_USLEEP |
299 | #define usleep hrt_usleep /* could conflict with ncurses for static build */ |
300 | |
301 | void |
302 | hrt_usleep(unsigned long usec) |
303 | { |
304 | long msec; |
305 | msec = usec / 1000; |
306 | Sleep (msec); |
307 | } |
308 | #endif |
309 | |
310 | |
311 | #if !defined(HAS_UALARM) && defined(HAS_SETITIMER) |
312 | #define HAS_UALARM |
313 | #define ualarm hrt_ualarm /* could conflict with ncurses for static build */ |
314 | |
315 | int |
316 | hrt_ualarm(int usec, int interval) |
317 | { |
318 | struct itimerval itv; |
319 | itv.it_value.tv_sec = usec / 1000000; |
320 | itv.it_value.tv_usec = usec % 1000000; |
321 | itv.it_interval.tv_sec = interval / 1000000; |
322 | itv.it_interval.tv_usec = interval % 1000000; |
323 | return setitimer(ITIMER_REAL, &itv, 0); |
324 | } |
325 | #endif |
326 | |
ca40fe49 |
327 | #if !defined(HAS_UALARM) && defined(VMS) |
328 | #define HAS_UALARM |
329 | #define ualarm vms_ualarm |
330 | |
331 | #include <lib$routines.h> |
332 | #include <ssdef.h> |
333 | #include <starlet.h> |
334 | #include <descrip.h> |
335 | #include <signal.h> |
336 | #include <jpidef.h> |
337 | #include <psldef.h> |
338 | |
339 | #define VMSERR(s) (!((s)&1)) |
340 | |
341 | static void |
342 | us_to_VMS(useconds_t mseconds, unsigned long v[]) |
343 | { |
344 | int iss; |
345 | unsigned long qq[2]; |
346 | |
347 | qq[0] = mseconds; |
348 | qq[1] = 0; |
349 | v[0] = v[1] = 0; |
350 | |
351 | iss = lib$addx(qq,qq,qq); |
352 | if (VMSERR(iss)) lib$signal(iss); |
353 | iss = lib$subx(v,qq,v); |
354 | if (VMSERR(iss)) lib$signal(iss); |
355 | iss = lib$addx(qq,qq,qq); |
356 | if (VMSERR(iss)) lib$signal(iss); |
357 | iss = lib$subx(v,qq,v); |
358 | if (VMSERR(iss)) lib$signal(iss); |
359 | iss = lib$subx(v,qq,v); |
360 | if (VMSERR(iss)) lib$signal(iss); |
361 | } |
362 | |
363 | static int |
364 | VMS_to_us(unsigned long v[]) |
365 | { |
366 | int iss; |
367 | unsigned long div=10,quot, rem; |
368 | |
369 | iss = lib$ediv(&div,v,",&rem); |
370 | if (VMSERR(iss)) lib$signal(iss); |
371 | |
372 | return quot; |
373 | } |
374 | |
375 | typedef unsigned short word; |
376 | typedef struct _ualarm { |
377 | int function; |
378 | int repeat; |
379 | unsigned long delay[2]; |
380 | unsigned long interval[2]; |
381 | unsigned long remain[2]; |
382 | } Alarm; |
383 | |
384 | |
385 | static int alarm_ef; |
386 | static Alarm *a0, alarm_base; |
387 | #define UAL_NULL 0 |
388 | #define UAL_SET 1 |
389 | #define UAL_CLEAR 2 |
390 | #define UAL_ACTIVE 4 |
391 | static void ualarm_AST(Alarm *a); |
392 | |
393 | static int |
394 | vms_ualarm(int mseconds, int interval) |
395 | { |
396 | Alarm *a, abase; |
397 | struct item_list3 { |
398 | word length; |
399 | word code; |
400 | void *bufaddr; |
401 | void *retlenaddr; |
402 | } ; |
403 | static struct item_list3 itmlst[2]; |
404 | static int first = 1; |
405 | unsigned long asten; |
406 | int iss, enabled; |
407 | |
408 | if (first) { |
409 | first = 0; |
410 | itmlst[0].code = JPI$_ASTEN; |
411 | itmlst[0].length = sizeof(asten); |
412 | itmlst[0].retlenaddr = NULL; |
413 | itmlst[1].code = 0; |
414 | itmlst[1].length = 0; |
415 | itmlst[1].bufaddr = NULL; |
416 | itmlst[1].retlenaddr = NULL; |
417 | |
418 | iss = lib$get_ef(&alarm_ef); |
419 | if (VMSERR(iss)) lib$signal(iss); |
420 | |
421 | a0 = &alarm_base; |
422 | a0->function = UAL_NULL; |
423 | } |
424 | itmlst[0].bufaddr = &asten; |
425 | |
426 | iss = sys$getjpiw(0,0,0,itmlst,0,0,0); |
427 | if (VMSERR(iss)) lib$signal(iss); |
428 | if (!(asten&0x08)) return -1; |
429 | |
430 | a = &abase; |
431 | if (mseconds) { |
432 | a->function = UAL_SET; |
433 | } else { |
434 | a->function = UAL_CLEAR; |
435 | } |
436 | |
437 | us_to_VMS(mseconds, a->delay); |
438 | if (interval) { |
439 | us_to_VMS(interval, a->interval); |
440 | a->repeat = 1; |
441 | } else |
442 | a->repeat = 0; |
443 | |
444 | iss = sys$clref(alarm_ef); |
445 | if (VMSERR(iss)) lib$signal(iss); |
446 | |
447 | iss = sys$dclast(ualarm_AST,a,0); |
448 | if (VMSERR(iss)) lib$signal(iss); |
449 | |
450 | iss = sys$waitfr(alarm_ef); |
451 | if (VMSERR(iss)) lib$signal(iss); |
452 | |
453 | if (a->function == UAL_ACTIVE) |
454 | return VMS_to_us(a->remain); |
455 | else |
456 | return 0; |
457 | } |
458 | |
459 | |
460 | |
461 | static void |
462 | ualarm_AST(Alarm *a) |
463 | { |
464 | int iss; |
465 | unsigned long now[2]; |
466 | |
467 | iss = sys$gettim(now); |
468 | if (VMSERR(iss)) lib$signal(iss); |
469 | |
470 | if (a->function == UAL_SET || a->function == UAL_CLEAR) { |
471 | if (a0->function == UAL_ACTIVE) { |
472 | iss = sys$cantim(a0,PSL$C_USER); |
473 | if (VMSERR(iss)) lib$signal(iss); |
474 | |
475 | iss = lib$subx(a0->remain, now, a->remain); |
476 | if (VMSERR(iss)) lib$signal(iss); |
477 | |
478 | if (a->remain[1] & 0x80000000) |
479 | a->remain[0] = a->remain[1] = 0; |
480 | } |
481 | |
482 | if (a->function == UAL_SET) { |
483 | a->function = a0->function; |
484 | a0->function = UAL_ACTIVE; |
485 | a0->repeat = a->repeat; |
486 | if (a0->repeat) { |
487 | a0->interval[0] = a->interval[0]; |
488 | a0->interval[1] = a->interval[1]; |
489 | } |
490 | a0->delay[0] = a->delay[0]; |
491 | a0->delay[1] = a->delay[1]; |
492 | |
493 | iss = lib$subx(now, a0->delay, a0->remain); |
494 | if (VMSERR(iss)) lib$signal(iss); |
495 | |
496 | iss = sys$setimr(0,a0->delay,ualarm_AST,a0); |
497 | if (VMSERR(iss)) lib$signal(iss); |
498 | } else { |
499 | a->function = a0->function; |
500 | a0->function = UAL_NULL; |
501 | } |
502 | iss = sys$setef(alarm_ef); |
503 | if (VMSERR(iss)) lib$signal(iss); |
504 | } else if (a->function == UAL_ACTIVE) { |
505 | if (a->repeat) { |
506 | iss = lib$subx(now, a->interval, a->remain); |
507 | if (VMSERR(iss)) lib$signal(iss); |
508 | |
509 | iss = sys$setimr(0,a->interval,ualarm_AST,a); |
510 | if (VMSERR(iss)) lib$signal(iss); |
511 | } else { |
512 | a->function = UAL_NULL; |
513 | } |
514 | iss = sys$wake(0,0); |
515 | if (VMSERR(iss)) lib$signal(iss); |
516 | lib$signal(SS$_ASTFLT); |
517 | } else { |
518 | lib$signal(SS$_BADPARAM); |
519 | } |
520 | } |
521 | |
522 | #endif /* !HAS_UALARM && VMS */ |
523 | |
524 | |
525 | |
dcf686c9 |
526 | #ifdef HAS_GETTIMEOFDAY |
527 | |
a2e20b18 |
528 | static int |
dcf686c9 |
529 | myU2time(UV *ret) |
530 | { |
531 | struct timeval Tp; |
532 | int status; |
533 | status = gettimeofday (&Tp, NULL); |
534 | ret[0] = Tp.tv_sec; |
535 | ret[1] = Tp.tv_usec; |
a2e20b18 |
536 | return status; |
dcf686c9 |
537 | } |
538 | |
3c72ec00 |
539 | static NV |
dcf686c9 |
540 | myNVtime() |
541 | { |
542 | struct timeval Tp; |
543 | int status; |
544 | status = gettimeofday (&Tp, NULL); |
a2e20b18 |
545 | return status == 0 ? Tp.tv_sec + (Tp.tv_usec / 1000000.) : -1.0; |
dcf686c9 |
546 | } |
547 | |
548 | #endif |
549 | |
550 | MODULE = Time::HiRes PACKAGE = Time::HiRes |
551 | |
552 | PROTOTYPES: ENABLE |
553 | |
554 | BOOT: |
dcf686c9 |
555 | #ifdef HAS_GETTIMEOFDAY |
a2e20b18 |
556 | { |
557 | UV auv[2]; |
558 | hv_store(PL_modglobal, "Time::NVtime", 12, newSViv((IV) myNVtime()), 0); |
559 | if (myU2time(auv) == 0) |
560 | hv_store(PL_modglobal, "Time::U2time", 12, newSViv((IV) auv[0]), 0); |
561 | } |
dcf686c9 |
562 | #endif |
dcf686c9 |
563 | |
3c72ec00 |
564 | IV |
565 | constant(name, arg) |
566 | char * name |
567 | int arg |
568 | |
52d72fba |
569 | #if defined(HAS_USLEEP) && defined(HAS_GETTIMEOFDAY) |
dcf686c9 |
570 | |
92bc48ca |
571 | NV |
dcf686c9 |
572 | usleep(useconds) |
92bc48ca |
573 | NV useconds |
52d72fba |
574 | PREINIT: |
575 | struct timeval Ta, Tb; |
576 | CODE: |
577 | gettimeofday(&Ta, NULL); |
578 | if (items > 0) { |
92bc48ca |
579 | if (useconds > 1E6) { |
580 | IV seconds = (IV) (useconds / 1E6); |
f7916ddb |
581 | /* If usleep() has been implemented using setitimer() |
582 | * then this contortion is unnecessary-- but usleep() |
583 | * may be implemented in some other way, so let's contort. */ |
584 | if (seconds) { |
585 | sleep(seconds); |
586 | useconds -= 1E6 * seconds; |
587 | } |
588 | } else if (useconds < 0.0) |
589 | croak("Time::HiRes::usleep(%"NVgf"): negative time not invented yet", useconds); |
92bc48ca |
590 | usleep((UV)useconds); |
52d72fba |
591 | } else |
592 | PerlProc_pause(); |
593 | gettimeofday(&Tb, NULL); |
92bc48ca |
594 | #if 0 |
595 | printf("[%ld %ld] [%ld %ld]\n", Tb.tv_sec, Tb.tv_usec, Ta.tv_sec, Ta.tv_usec); |
596 | #endif |
597 | RETVAL = 1E6*(Tb.tv_sec-Ta.tv_sec)+(NV)((IV)Tb.tv_usec-(IV)Ta.tv_usec); |
dcf686c9 |
598 | |
52d72fba |
599 | OUTPUT: |
600 | RETVAL |
601 | |
602 | NV |
f9d00e57 |
603 | sleep(...) |
52d72fba |
604 | PREINIT: |
605 | struct timeval Ta, Tb; |
dcf686c9 |
606 | CODE: |
52d72fba |
607 | gettimeofday(&Ta, NULL); |
92bc48ca |
608 | if (items > 0) { |
609 | NV seconds = SvNV(ST(0)); |
f7916ddb |
610 | if (seconds >= 0.0) { |
611 | UV useconds = 1E6 * (seconds - (UV)seconds); |
612 | sleep((UV)seconds); |
613 | usleep(useconds); |
614 | } else |
615 | croak("Time::HiRes::sleep(%"NVgf"): negative time not invented yet", seconds); |
92bc48ca |
616 | } else |
f9d00e57 |
617 | PerlProc_pause(); |
52d72fba |
618 | gettimeofday(&Tb, NULL); |
92bc48ca |
619 | #if 0 |
620 | printf("[%ld %ld] [%ld %ld]\n", Tb.tv_sec, Tb.tv_usec, Ta.tv_sec, Ta.tv_usec); |
621 | #endif |
52d72fba |
622 | RETVAL = (NV)(Tb.tv_sec-Ta.tv_sec)+0.000001*(NV)(Tb.tv_usec-Ta.tv_usec); |
623 | |
624 | OUTPUT: |
625 | RETVAL |
dcf686c9 |
626 | |
627 | #endif |
628 | |
629 | #ifdef HAS_UALARM |
630 | |
3de7a4ec |
631 | int |
dcf686c9 |
632 | ualarm(useconds,interval=0) |
633 | int useconds |
634 | int interval |
f7916ddb |
635 | CODE: |
636 | if (useconds < 0 || interval < 0) |
3de7a4ec |
637 | croak("Time::HiRes::ualarm(%d, %d): negative time not invented yet", useconds, interval); |
f7916ddb |
638 | RETVAL = ualarm(useconds, interval); |
dcf686c9 |
639 | |
f7916ddb |
640 | OUTPUT: |
641 | RETVAL |
642 | |
643 | NV |
644 | alarm(seconds,interval=0) |
645 | NV seconds |
646 | NV interval |
dcf686c9 |
647 | CODE: |
f7916ddb |
648 | if (seconds < 0.0 || interval < 0.0) |
649 | croak("Time::HiRes::alarm(%"NVgf", %"NVgf"): negative time not invented yet", seconds, interval); |
650 | RETVAL = (NV)ualarm(seconds * 1000000, |
651 | interval * 1000000) / 1E6; |
dcf686c9 |
652 | |
c6c619a9 |
653 | OUTPUT: |
654 | RETVAL |
655 | |
dcf686c9 |
656 | #endif |
657 | |
658 | #ifdef HAS_GETTIMEOFDAY |
db835671 |
659 | # ifdef MACOS_TRADITIONAL /* fix epoch TZ and use unsigned time_t */ |
660 | void |
661 | gettimeofday() |
662 | PREINIT: |
663 | struct timeval Tp; |
664 | struct timezone Tz; |
665 | PPCODE: |
666 | int status; |
667 | status = gettimeofday (&Tp, &Tz); |
668 | Tp.tv_sec += Tz.tz_minuteswest * 60; /* adjust for TZ */ |
669 | |
670 | if (GIMME == G_ARRAY) { |
671 | EXTEND(sp, 2); |
672 | /* Mac OS (Classic) has unsigned time_t */ |
673 | PUSHs(sv_2mortal(newSVuv(Tp.tv_sec))); |
674 | PUSHs(sv_2mortal(newSViv(Tp.tv_usec))); |
675 | } else { |
676 | EXTEND(sp, 1); |
677 | PUSHs(sv_2mortal(newSVnv(Tp.tv_sec + (Tp.tv_usec / 1000000.0)))); |
678 | } |
679 | |
680 | NV |
681 | time() |
682 | PREINIT: |
683 | struct timeval Tp; |
684 | struct timezone Tz; |
685 | CODE: |
686 | int status; |
687 | status = gettimeofday (&Tp, &Tz); |
688 | Tp.tv_sec += Tz.tz_minuteswest * 60; /* adjust for TZ */ |
689 | RETVAL = Tp.tv_sec + (Tp.tv_usec / 1000000.0); |
690 | OUTPUT: |
691 | RETVAL |
dcf686c9 |
692 | |
db835671 |
693 | # else /* MACOS_TRADITIONAL */ |
dcf686c9 |
694 | void |
695 | gettimeofday() |
696 | PREINIT: |
697 | struct timeval Tp; |
698 | PPCODE: |
699 | int status; |
700 | status = gettimeofday (&Tp, NULL); |
701 | if (GIMME == G_ARRAY) { |
702 | EXTEND(sp, 2); |
703 | PUSHs(sv_2mortal(newSViv(Tp.tv_sec))); |
704 | PUSHs(sv_2mortal(newSViv(Tp.tv_usec))); |
705 | } else { |
706 | EXTEND(sp, 1); |
707 | PUSHs(sv_2mortal(newSVnv(Tp.tv_sec + (Tp.tv_usec / 1000000.0)))); |
708 | } |
709 | |
3c72ec00 |
710 | NV |
dcf686c9 |
711 | time() |
712 | PREINIT: |
713 | struct timeval Tp; |
714 | CODE: |
715 | int status; |
716 | status = gettimeofday (&Tp, NULL); |
717 | RETVAL = Tp.tv_sec + (Tp.tv_usec / 1000000.); |
718 | OUTPUT: |
719 | RETVAL |
720 | |
db835671 |
721 | # endif /* MACOS_TRADITIONAL */ |
dcf686c9 |
722 | #endif |
723 | |
3c72ec00 |
724 | #if defined(HAS_GETITIMER) && defined(HAS_SETITIMER) |
725 | |
726 | #define TV2NV(tv) ((NV)((tv).tv_sec) + 0.000001 * (NV)((tv).tv_usec)) |
727 | |
728 | void |
729 | setitimer(which, seconds, interval = 0) |
730 | int which |
731 | NV seconds |
732 | NV interval |
733 | PREINIT: |
734 | struct itimerval newit; |
735 | struct itimerval oldit; |
736 | PPCODE: |
f7916ddb |
737 | if (seconds < 0.0 || interval < 0.0) |
738 | croak("Time::HiRes::setitimer(%"IVdf", %"NVgf", %"NVgf"): negative time not invented yet", which, seconds, interval); |
3c72ec00 |
739 | newit.it_value.tv_sec = seconds; |
740 | newit.it_value.tv_usec = |
741 | (seconds - (NV)newit.it_value.tv_sec) * 1000000.0; |
742 | newit.it_interval.tv_sec = interval; |
743 | newit.it_interval.tv_usec = |
744 | (interval - (NV)newit.it_interval.tv_sec) * 1000000.0; |
745 | if (setitimer(which, &newit, &oldit) == 0) { |
746 | EXTEND(sp, 1); |
747 | PUSHs(sv_2mortal(newSVnv(TV2NV(oldit.it_value)))); |
748 | if (GIMME == G_ARRAY) { |
749 | EXTEND(sp, 1); |
750 | PUSHs(sv_2mortal(newSVnv(TV2NV(oldit.it_interval)))); |
751 | } |
752 | } |
753 | |
754 | void |
755 | getitimer(which) |
756 | int which |
757 | PREINIT: |
758 | struct itimerval nowit; |
759 | PPCODE: |
760 | if (getitimer(which, &nowit) == 0) { |
761 | EXTEND(sp, 1); |
762 | PUSHs(sv_2mortal(newSVnv(TV2NV(nowit.it_value)))); |
763 | if (GIMME == G_ARRAY) { |
764 | EXTEND(sp, 1); |
765 | PUSHs(sv_2mortal(newSVnv(TV2NV(nowit.it_interval)))); |
766 | } |
767 | } |
768 | |
769 | #endif |
770 | |
dcf686c9 |
771 | # $Id: HiRes.xs,v 1.11 1999/03/16 02:27:38 wegscd Exp wegscd $ |
772 | |
773 | # $Log: HiRes.xs,v $ |
774 | # Revision 1.11 1999/03/16 02:27:38 wegscd |
775 | # Add U2time, NVtime. Fix symbols for static link. |
776 | # |
777 | # Revision 1.10 1998/09/30 02:36:25 wegscd |
778 | # Add VMS changes. |
779 | # |
780 | # Revision 1.9 1998/07/07 02:42:06 wegscd |
781 | # Win32 usleep() |
782 | # |
783 | # Revision 1.8 1998/07/02 01:47:26 wegscd |
784 | # Add Win32 code for gettimeofday. |
785 | # |
786 | # Revision 1.7 1997/11/13 02:08:12 wegscd |
787 | # Add missing EXTEND in gettimeofday() scalar code. |
788 | # |
789 | # Revision 1.6 1997/11/11 02:32:35 wegscd |
790 | # Do something useful when calling gettimeofday() in a scalar context. |
791 | # The patch is courtesy of Gisle Aas. |
792 | # |
793 | # Revision 1.5 1997/11/06 03:10:47 wegscd |
794 | # Fake ualarm() if we have setitimer. |
795 | # |
796 | # Revision 1.4 1997/11/05 05:41:23 wegscd |
797 | # Turn prototypes ON (suggested by Gisle Aas) |
798 | # |
799 | # Revision 1.3 1997/10/13 20:56:15 wegscd |
800 | # Add PROTOTYPES: DISABLE |
801 | # |
802 | # Revision 1.2 1997/05/23 01:01:38 wegscd |
803 | # Conditional compilation, depending on what the OS gives us. |
804 | # |
805 | # Revision 1.1 1996/09/03 18:26:35 wegscd |
806 | # Initial revision |
807 | # |
808 | # |