Make Time::HiRes::sleep() and usleep() to return
[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 __cplusplus
13 }
14 #endif
15
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
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
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) */
76 int
77 gettimeofday (struct timeval *tp, void *not_used)
78 {
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;
91 }
92 #endif
93
94 #if !defined(HAS_GETTIMEOFDAY) && defined(VMS)
95 #define HAS_GETTIMEOFDAY
96
97 #include <lnmdef.h>
98 #include <time.h> /* gettimeofday */
99 #include <stdlib.h> /* qdiv */
100 #include <starlet.h> /* sys$gettim */
101 #include <descrip.h>
102 #ifdef __VAX
103 #include <lib$routines.h> /* lib$ediv() */
104 #endif
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
120 #ifdef __VAX
121 static long base_adjust[2]={0L,0L};
122 #else
123 static __int64 base_adjust=0;
124 #endif
125
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
210 int
211 gettimeofday (struct timeval *tp, void *tpz)
212 {
213  long ret;
214 #ifdef __VAX
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;
221 #else
222  __int64 quad;
223  __qdiv_t ans1,ans2;
224 #endif
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
233 #ifdef __VAX
234  if (base_adjust[0]==0 && base_adjust[1]==0) {
235 #else
236  if (base_adjust==0) { /* Need to determine epoch adjustment */
237 #endif
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) {
247 #ifdef __VAX
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 */
258 #else
259         quad -= base_adjust; /* convert to epoch offset */
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 */
264 #endif
265  } else {
266         tp->tv_sec = ret;
267         return -1;
268  }
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
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
327 #ifdef HAS_GETTIMEOFDAY
328
329 static int
330 myU2time(UV *ret)
331 {
332   struct timeval Tp;
333   int status;
334   status = gettimeofday (&Tp, NULL);
335   ret[0] = Tp.tv_sec;
336   ret[1] = Tp.tv_usec;
337   return status;
338 }
339
340 static NV
341 myNVtime()
342 {
343   struct timeval Tp;
344   int status;
345   status = gettimeofday (&Tp, NULL);
346   return status == 0 ? Tp.tv_sec + (Tp.tv_usec / 1000000.) : -1.0;
347 }
348
349 #endif
350
351 MODULE = Time::HiRes            PACKAGE = Time::HiRes
352
353 PROTOTYPES: ENABLE
354
355 BOOT:
356 #ifdef HAS_GETTIMEOFDAY
357 {
358   UV auv[2];
359   hv_store(PL_modglobal, "Time::NVtime", 12, newSViv((IV) myNVtime()), 0);
360   if (myU2time(auv) == 0)
361     hv_store(PL_modglobal, "Time::U2time", 12, newSViv((IV) auv[0]), 0);
362 }
363 #endif
364
365 IV
366 constant(name, arg)
367         char *          name
368         int             arg
369
370 #if defined(HAS_USLEEP) && defined(HAS_GETTIMEOFDAY)
371
372 int
373 usleep(useconds)
374         int useconds 
375         PREINIT:
376         struct timeval Ta, Tb;
377         CODE:
378         gettimeofday(&Ta, NULL);
379         if (items > 0) {
380             if (useconds > 1000000)
381                 croak("usleep: useconds must be between 0 and 1000000 (inclusive)");
382             usleep(useconds);
383         } else
384             PerlProc_pause();
385         gettimeofday(&Tb, NULL);
386         RETVAL = 1000000*(Tb.tv_sec-Ta.tv_sec)+(Tb.tv_usec-Ta.tv_usec);
387
388         OUTPUT:
389         RETVAL
390
391 NV
392 sleep(...)
393         PREINIT:
394         struct timeval Ta, Tb;
395         CODE:
396         gettimeofday(&Ta, NULL);
397         if (items > 0)
398             usleep((int)(SvNV(ST(0)) * 1000000));
399         else
400             PerlProc_pause();
401         gettimeofday(&Tb, NULL);
402         RETVAL = (NV)(Tb.tv_sec-Ta.tv_sec)+0.000001*(NV)(Tb.tv_usec-Ta.tv_usec);
403
404         OUTPUT:
405         RETVAL
406
407 #endif
408
409 #ifdef HAS_UALARM
410
411 int
412 ualarm(useconds,interval=0)
413         int useconds
414         int interval
415
416 int
417 alarm(fseconds,finterval=0)
418         NV fseconds
419         NV finterval
420         PREINIT:
421         int useconds, uinterval;
422         CODE:
423         useconds = fseconds * 1000000;
424         uinterval = finterval * 1000000;
425         RETVAL = ualarm (useconds, uinterval);
426
427         OUTPUT:
428         RETVAL
429
430 #endif
431
432 #ifdef HAS_GETTIMEOFDAY
433
434 void
435 gettimeofday()
436         PREINIT:
437         struct timeval Tp;
438         PPCODE:
439         int status;
440         status = gettimeofday (&Tp, NULL);
441         if (GIMME == G_ARRAY) {
442              EXTEND(sp, 2);
443              PUSHs(sv_2mortal(newSViv(Tp.tv_sec)));
444              PUSHs(sv_2mortal(newSViv(Tp.tv_usec)));
445         } else {
446              EXTEND(sp, 1);
447              PUSHs(sv_2mortal(newSVnv(Tp.tv_sec + (Tp.tv_usec / 1000000.0))));
448         }
449
450 NV
451 time()
452         PREINIT:
453         struct timeval Tp;
454         CODE:
455         int status;
456         status = gettimeofday (&Tp, NULL);
457         RETVAL = Tp.tv_sec + (Tp.tv_usec / 1000000.);
458         OUTPUT:
459         RETVAL
460
461 #endif
462
463 #if defined(HAS_GETITIMER) && defined(HAS_SETITIMER)
464
465 #define TV2NV(tv) ((NV)((tv).tv_sec) + 0.000001 * (NV)((tv).tv_usec))
466
467 void
468 setitimer(which, seconds, interval = 0)
469         int which
470         NV seconds
471         NV interval
472     PREINIT:
473         struct itimerval newit;
474         struct itimerval oldit;
475     PPCODE:
476         newit.it_value.tv_sec  = seconds;
477         newit.it_value.tv_usec =
478           (seconds  - (NV)newit.it_value.tv_sec)    * 1000000.0;
479         newit.it_interval.tv_sec  = interval;
480         newit.it_interval.tv_usec =
481           (interval - (NV)newit.it_interval.tv_sec) * 1000000.0;
482         if (setitimer(which, &newit, &oldit) == 0) {
483           EXTEND(sp, 1);
484           PUSHs(sv_2mortal(newSVnv(TV2NV(oldit.it_value))));
485           if (GIMME == G_ARRAY) {
486             EXTEND(sp, 1);
487             PUSHs(sv_2mortal(newSVnv(TV2NV(oldit.it_interval))));
488           }
489         }
490
491 void
492 getitimer(which)
493         int which
494     PREINIT:
495         struct itimerval nowit;
496     PPCODE:
497         if (getitimer(which, &nowit) == 0) {
498           EXTEND(sp, 1);
499           PUSHs(sv_2mortal(newSVnv(TV2NV(nowit.it_value))));
500           if (GIMME == G_ARRAY) {
501             EXTEND(sp, 1);
502             PUSHs(sv_2mortal(newSVnv(TV2NV(nowit.it_interval))));
503           }
504         }
505
506 #endif
507
508 # $Id: HiRes.xs,v 1.11 1999/03/16 02:27:38 wegscd Exp wegscd $
509
510 # $Log: HiRes.xs,v $
511 # Revision 1.11  1999/03/16 02:27:38  wegscd
512 # Add U2time, NVtime. Fix symbols for static link.
513 #
514 # Revision 1.10  1998/09/30 02:36:25  wegscd
515 # Add VMS changes.
516 #
517 # Revision 1.9  1998/07/07 02:42:06  wegscd
518 # Win32 usleep()
519 #
520 # Revision 1.8  1998/07/02 01:47:26  wegscd
521 # Add Win32 code for gettimeofday.
522 #
523 # Revision 1.7  1997/11/13 02:08:12  wegscd
524 # Add missing EXTEND in gettimeofday() scalar code.
525 #
526 # Revision 1.6  1997/11/11 02:32:35  wegscd
527 # Do something useful when calling gettimeofday() in a scalar context.
528 # The patch is courtesy of Gisle Aas.
529 #
530 # Revision 1.5  1997/11/06 03:10:47  wegscd
531 # Fake ualarm() if we have setitimer.
532 #
533 # Revision 1.4  1997/11/05 05:41:23  wegscd
534 # Turn prototypes ON (suggested by Gisle Aas)
535 #
536 # Revision 1.3  1997/10/13 20:56:15  wegscd
537 # Add PROTOTYPES: DISABLE
538 #
539 # Revision 1.2  1997/05/23 01:01:38  wegscd
540 # Conditional compilation, depending on what the OS gives us.
541 #
542 # Revision 1.1  1996/09/03 18:26:35  wegscd
543 # Initial revision
544 #
545 #