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