This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Autosplit patch for VMS
[perl5.git] / ext / Time / HiRes / HiRes.xs
CommitLineData
dcf686c9
JH
1#ifdef __cplusplus
2extern "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
JH
16static IV
17constant(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
51not_there:
52 errno = ENOENT;
53 return 0;
54}
55
dcf686c9
JH
56#if !defined(HAS_GETTIMEOFDAY) && defined(WIN32)
57#define HAS_GETTIMEOFDAY
58
59/* shows up in winsock.h?
60struct timeval {
61 long tv_sec;
62 long tv_usec;
63}
64*/
65
eb1a5092
GS
66typedef 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 76int
eb1a5092 77gettimeofday (struct timeval *tp, void *not_used)
dcf686c9 78{
eb1a5092
GS
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
JH
91}
92#endif
93
94#if !defined(HAS_GETTIMEOFDAY) && defined(VMS)
95#define HAS_GETTIMEOFDAY
96
97#include <time.h> /* gettimeofday */
98#include <stdlib.h> /* qdiv */
99#include <starlet.h> /* sys$gettim */
100#include <descrip.h>
3785778e
PP
101#ifdef __VAX
102#include <lib$routines.h> /* lib$ediv() */
103#endif
dcf686c9
JH
104
105/*
106 VMS binary time is expressed in 100 nano-seconds since
107 system base time which is 17-NOV-1858 00:00:00.00
108*/
109
110#define DIV_100NS_TO_SECS 10000000L
111#define DIV_100NS_TO_USECS 10L
112
113/*
114 gettimeofday is supposed to return times since the epoch
115 so need to determine this in terms of VMS base time
116*/
117static $DESCRIPTOR(dscepoch,"01-JAN-1970 00:00:00.00");
118
5cdb7193 119#ifdef __VAX
3785778e 120static long base_adjust[2]={0L,0L};
5cdb7193 121#else
dcf686c9 122static __int64 base_adjust=0;
5cdb7193 123#endif
dcf686c9
JH
124
125int
126gettimeofday (struct timeval *tp, void *tpz)
127{
128 long ret;
5cdb7193 129#ifdef __VAX
3785778e
PP
130 long quad[2];
131 long quad1[2];
132 long div_100ns_to_secs;
133 long div_100ns_to_usecs;
134 long quo,rem;
135 long quo1,rem1;
5cdb7193 136#else
dcf686c9
JH
137 __int64 quad;
138 __qdiv_t ans1,ans2;
5cdb7193 139#endif
dcf686c9
JH
140/*
141 In case of error, tv_usec = 0 and tv_sec = VMS condition code.
142 The return from function is also set to -1.
143 This is not exactly as per the manual page.
144*/
145
146 tp->tv_usec = 0;
147
3785778e
PP
148#ifdef __VAX
149 if (base_adjust[0]==0 && base_adjust[1]==0) {
150#else
dcf686c9 151 if (base_adjust==0) { /* Need to determine epoch adjustment */
3785778e 152#endif
dcf686c9
JH
153 ret=sys$bintim(&dscepoch,&base_adjust);
154 if (1 != (ret &&1)) {
155 tp->tv_sec = ret;
156 return -1;
157 }
158 }
159
160 ret=sys$gettim(&quad); /* Get VMS system time */
161 if ((1 && ret) == 1) {
5cdb7193 162#ifdef __VAX
3785778e
PP
163 quad[0] -= base_adjust[0]; /* convert to epoch offset */
164 quad[1] -= base_adjust[1]; /* convert 2nd half of quadword */
165 div_100ns_to_secs = DIV_100NS_TO_SECS;
166 div_100ns_to_usecs = DIV_100NS_TO_USECS;
167 lib$ediv(&div_100ns_to_secs,&quad,&quo,&rem);
168 quad1[0] = rem;
169 quad1[1] = 0L;
170 lib$ediv(&div_100ns_to_usecs,&quad1,&quo1,&rem1);
171 tp->tv_sec = quo; /* Whole seconds */
172 tp->tv_usec = quo1; /* Micro-seconds */
5cdb7193 173#else
3785778e 174 quad -= base_adjust; /* convert to epoch offset */
dcf686c9
JH
175 ans1=qdiv(quad,DIV_100NS_TO_SECS);
176 ans2=qdiv(ans1.rem,DIV_100NS_TO_USECS);
177 tp->tv_sec = ans1.quot; /* Whole seconds */
178 tp->tv_usec = ans2.quot; /* Micro-seconds */
3785778e 179#endif
dcf686c9
JH
180 } else {
181 tp->tv_sec = ret;
182 return -1;
183 }
184 return 0;
185}
186#endif
187
188#if !defined(HAS_USLEEP) && defined(HAS_SELECT)
189#ifndef SELECT_IS_BROKEN
190#define HAS_USLEEP
191#define usleep hrt_usleep /* could conflict with ncurses for static build */
192
193void
194hrt_usleep(unsigned long usec)
195{
196 struct timeval tv;
197 tv.tv_sec = 0;
198 tv.tv_usec = usec;
199 select(0, (Select_fd_set_t)NULL, (Select_fd_set_t)NULL,
200 (Select_fd_set_t)NULL, &tv);
201}
202#endif
203#endif
204
205#if !defined(HAS_USLEEP) && defined(WIN32)
206#define HAS_USLEEP
207#define usleep hrt_usleep /* could conflict with ncurses for static build */
208
209void
210hrt_usleep(unsigned long usec)
211{
212 long msec;
213 msec = usec / 1000;
214 Sleep (msec);
215}
216#endif
217
218
219#if !defined(HAS_UALARM) && defined(HAS_SETITIMER)
220#define HAS_UALARM
221#define ualarm hrt_ualarm /* could conflict with ncurses for static build */
222
223int
224hrt_ualarm(int usec, int interval)
225{
226 struct itimerval itv;
227 itv.it_value.tv_sec = usec / 1000000;
228 itv.it_value.tv_usec = usec % 1000000;
229 itv.it_interval.tv_sec = interval / 1000000;
230 itv.it_interval.tv_usec = interval % 1000000;
231 return setitimer(ITIMER_REAL, &itv, 0);
232}
233#endif
234
235#ifdef HAS_GETTIMEOFDAY
236
a2e20b18 237static int
dcf686c9
JH
238myU2time(UV *ret)
239{
240 struct timeval Tp;
241 int status;
242 status = gettimeofday (&Tp, NULL);
243 ret[0] = Tp.tv_sec;
244 ret[1] = Tp.tv_usec;
a2e20b18 245 return status;
dcf686c9
JH
246}
247
3c72ec00 248static NV
dcf686c9
JH
249myNVtime()
250{
251 struct timeval Tp;
252 int status;
253 status = gettimeofday (&Tp, NULL);
a2e20b18 254 return status == 0 ? Tp.tv_sec + (Tp.tv_usec / 1000000.) : -1.0;
dcf686c9
JH
255}
256
257#endif
258
259MODULE = Time::HiRes PACKAGE = Time::HiRes
260
261PROTOTYPES: ENABLE
262
263BOOT:
dcf686c9 264#ifdef HAS_GETTIMEOFDAY
a2e20b18
JH
265{
266 UV auv[2];
267 hv_store(PL_modglobal, "Time::NVtime", 12, newSViv((IV) myNVtime()), 0);
268 if (myU2time(auv) == 0)
269 hv_store(PL_modglobal, "Time::U2time", 12, newSViv((IV) auv[0]), 0);
270}
dcf686c9 271#endif
dcf686c9 272
3c72ec00
JH
273IV
274constant(name, arg)
275 char * name
276 int arg
277
dcf686c9
JH
278#ifdef HAS_USLEEP
279
280void
281usleep(useconds)
282 int useconds
283
284void
f9d00e57 285sleep(...)
89c2f7cb 286 PROTOTYPE: ;$
dcf686c9 287 CODE:
f9d00e57
AMS
288 if (items > 0)
289 usleep((int)(SvNV(ST(0)) * 1000000));
290 else
291 PerlProc_pause();
dcf686c9
JH
292
293#endif
294
295#ifdef HAS_UALARM
296
297int
298ualarm(useconds,interval=0)
299 int useconds
300 int interval
301
302int
303alarm(fseconds,finterval=0)
3c72ec00
JH
304 NV fseconds
305 NV finterval
dcf686c9
JH
306 PREINIT:
307 int useconds, uinterval;
308 CODE:
309 useconds = fseconds * 1000000;
310 uinterval = finterval * 1000000;
311 RETVAL = ualarm (useconds, uinterval);
312
c6c619a9
DM
313 OUTPUT:
314 RETVAL
315
dcf686c9
JH
316#endif
317
318#ifdef HAS_GETTIMEOFDAY
319
320void
321gettimeofday()
322 PREINIT:
323 struct timeval Tp;
324 PPCODE:
325 int status;
326 status = gettimeofday (&Tp, NULL);
327 if (GIMME == G_ARRAY) {
328 EXTEND(sp, 2);
329 PUSHs(sv_2mortal(newSViv(Tp.tv_sec)));
330 PUSHs(sv_2mortal(newSViv(Tp.tv_usec)));
331 } else {
332 EXTEND(sp, 1);
333 PUSHs(sv_2mortal(newSVnv(Tp.tv_sec + (Tp.tv_usec / 1000000.0))));
334 }
335
3c72ec00 336NV
dcf686c9
JH
337time()
338 PREINIT:
339 struct timeval Tp;
340 CODE:
341 int status;
342 status = gettimeofday (&Tp, NULL);
343 RETVAL = Tp.tv_sec + (Tp.tv_usec / 1000000.);
344 OUTPUT:
345 RETVAL
346
347#endif
348
3c72ec00
JH
349#if defined(HAS_GETITIMER) && defined(HAS_SETITIMER)
350
351#define TV2NV(tv) ((NV)((tv).tv_sec) + 0.000001 * (NV)((tv).tv_usec))
352
353void
354setitimer(which, seconds, interval = 0)
355 int which
356 NV seconds
357 NV interval
358 PREINIT:
359 struct itimerval newit;
360 struct itimerval oldit;
361 PPCODE:
362 newit.it_value.tv_sec = seconds;
363 newit.it_value.tv_usec =
364 (seconds - (NV)newit.it_value.tv_sec) * 1000000.0;
365 newit.it_interval.tv_sec = interval;
366 newit.it_interval.tv_usec =
367 (interval - (NV)newit.it_interval.tv_sec) * 1000000.0;
368 if (setitimer(which, &newit, &oldit) == 0) {
369 EXTEND(sp, 1);
370 PUSHs(sv_2mortal(newSVnv(TV2NV(oldit.it_value))));
371 if (GIMME == G_ARRAY) {
372 EXTEND(sp, 1);
373 PUSHs(sv_2mortal(newSVnv(TV2NV(oldit.it_interval))));
374 }
375 }
376
377void
378getitimer(which)
379 int which
380 PREINIT:
381 struct itimerval nowit;
382 PPCODE:
383 if (getitimer(which, &nowit) == 0) {
384 EXTEND(sp, 1);
385 PUSHs(sv_2mortal(newSVnv(TV2NV(nowit.it_value))));
386 if (GIMME == G_ARRAY) {
387 EXTEND(sp, 1);
388 PUSHs(sv_2mortal(newSVnv(TV2NV(nowit.it_interval))));
389 }
390 }
391
392#endif
393
dcf686c9
JH
394# $Id: HiRes.xs,v 1.11 1999/03/16 02:27:38 wegscd Exp wegscd $
395
396# $Log: HiRes.xs,v $
397# Revision 1.11 1999/03/16 02:27:38 wegscd
398# Add U2time, NVtime. Fix symbols for static link.
399#
400# Revision 1.10 1998/09/30 02:36:25 wegscd
401# Add VMS changes.
402#
403# Revision 1.9 1998/07/07 02:42:06 wegscd
404# Win32 usleep()
405#
406# Revision 1.8 1998/07/02 01:47:26 wegscd
407# Add Win32 code for gettimeofday.
408#
409# Revision 1.7 1997/11/13 02:08:12 wegscd
410# Add missing EXTEND in gettimeofday() scalar code.
411#
412# Revision 1.6 1997/11/11 02:32:35 wegscd
413# Do something useful when calling gettimeofday() in a scalar context.
414# The patch is courtesy of Gisle Aas.
415#
416# Revision 1.5 1997/11/06 03:10:47 wegscd
417# Fake ualarm() if we have setitimer.
418#
419# Revision 1.4 1997/11/05 05:41:23 wegscd
420# Turn prototypes ON (suggested by Gisle Aas)
421#
422# Revision 1.3 1997/10/13 20:56:15 wegscd
423# Add PROTOTYPES: DISABLE
424#
425# Revision 1.2 1997/05/23 01:01:38 wegscd
426# Conditional compilation, depending on what the OS gives us.
427#
428# Revision 1.1 1996/09/03 18:26:35 wegscd
429# Initial revision
430#
431#