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