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