Fix long standing memory leak with pop and shift!
[p5sagit/p5-mst-13.2.git] / ext / Time / HiRes / HiRes.pm
CommitLineData
dcf686c9 1package Time::HiRes;
2
3use strict;
105cd853 4use vars qw($VERSION $XS_VERSION @ISA @EXPORT @EXPORT_OK $AUTOLOAD);
dcf686c9 5
6require Exporter;
3f2ee006 7require DynaLoader;
dcf686c9 8
3f2ee006 9@ISA = qw(Exporter DynaLoader);
dcf686c9 10
11@EXPORT = qw( );
3c72ec00 12@EXPORT_OK = qw (usleep sleep ualarm alarm gettimeofday time tv_interval
3f2ee006 13 getitimer setitimer
14 ITIMER_REAL ITIMER_VIRTUAL ITIMER_PROF ITIMER_REALPROF
15 d_usleep d_ualarm d_gettimeofday d_getitimer d_setitimer
16 d_nanosleep);
17
6a20eacc 18$VERSION = '1.39';
105cd853 19$XS_VERSION = $VERSION;
20$VERSION = eval $VERSION;
3c72ec00 21
22sub AUTOLOAD {
23 my $constname;
24 ($constname= $AUTOLOAD) =~ s/.*:://;
25 my $val = constant($constname, @_ ? $_[0] : 0);
26 if ($!) {
27 my ($pack,$file,$line) = caller;
28 die "Your vendor has not defined Time::HiRes macro $constname, used at $file line $line.\n";
29 }
30 {
31 no strict 'refs';
32 *$AUTOLOAD = sub { $val };
33 }
34 goto &$AUTOLOAD;
35}
dcf686c9 36
3f2ee006 37bootstrap Time::HiRes;
dcf686c9 38
39# Preloaded methods go here.
40
41sub tv_interval {
42 # probably could have been done in C
43 my ($a, $b) = @_;
44 $b = [gettimeofday()] unless defined($b);
45 (${$b}[0] - ${$a}[0]) + ((${$b}[1] - ${$a}[1]) / 1_000_000);
46}
47
dcf686c9 48# Autoload methods go after =cut, and are processed by the autosplit program.
49
501;
51__END__
52
53=head1 NAME
54
f7916ddb 55Time::HiRes - High resolution alarm, sleep, gettimeofday, interval timers
dcf686c9 56
57=head1 SYNOPSIS
58
59 use Time::HiRes qw( usleep ualarm gettimeofday tv_interval );
60
61 usleep ($microseconds);
62
63 ualarm ($microseconds);
64 ualarm ($microseconds, $interval_microseconds);
65
66 $t0 = [gettimeofday];
67 ($seconds, $microseconds) = gettimeofday;
68
69 $elapsed = tv_interval ( $t0, [$seconds, $microseconds]);
70 $elapsed = tv_interval ( $t0, [gettimeofday]);
71 $elapsed = tv_interval ( $t0 );
72
73 use Time::HiRes qw ( time alarm sleep );
3c72ec00 74
dcf686c9 75 $now_fractions = time;
76 sleep ($floating_seconds);
77 alarm ($floating_seconds);
78 alarm ($floating_seconds, $floating_interval);
79
3c72ec00 80 use Time::HiRes qw( setitimer getitimer
3f2ee006 81 ITIMER_REAL ITIMER_VIRTUAL ITIMER_PROF ITIMER_REALPROF );
3c72ec00 82
83 setitimer ($which, $floating_seconds, $floating_interval );
84 getitimer ($which);
85
dcf686c9 86=head1 DESCRIPTION
87
f7916ddb 88The C<Time::HiRes> module implements a Perl interface to the usleep,
89ualarm, gettimeofday, and setitimer/getitimer system calls. See the
90EXAMPLES section below and the test scripts for usage; see your system
3f2ee006 91documentation for the description of the underlying nanosleep or usleep,
92ualarm, gettimeofday, and setitimer/getitimer calls.
dcf686c9 93
f7916ddb 94If your system lacks gettimeofday(2) or an emulation of it you don't
3f2ee006 95get gettimeofday() or the one-arg form of tv_interval(). If you don't
96have nanosleep() or usleep(3) or select(2) you don't get Time::HiRes::usleep()
f7916ddb 97or sleep(). If your system don't have ualarm(3) or setitimer(2) you
3f2ee006 98don't get Time::HiRes::ualarm() or alarm().
99
100If you try to import an unimplemented function in the C<use> statement
101it will fail at compile time.
102
103If your subsecond sleeping is implemented with nanosleep() instead of
104usleep(), you can mix subsecond sleeping with signals since
105nanosleep() does not use signals. This, however, is unportable
106behavior, and you should first check for the truth value of
107C<&Time::HiRes::d_nanosleep> to see whether you have nanosleep,
108and then read carefully your nanosleep() C API documentation for
109any peculiarities. (There is no separate interface to call nanosleep();
110just use Time::HiRes::sleep() or usleep() with small enough values. Also,
111think twice whether using nanosecond accuracies in a Perl program is what
112you should be doing.)
dcf686c9 113
3c72ec00 114The following functions can be imported from this module.
115No functions are exported by default.
dcf686c9 116
117=over 4
118
119=item gettimeofday ()
120
f7916ddb 121In array context returns a 2 element array with the seconds and
122microseconds since the epoch. In scalar context returns floating
dcf686c9 123seconds like Time::HiRes::time() (see below).
124
125=item usleep ( $useconds )
126
f7916ddb 127Sleeps for the number of microseconds specified. Returns the number
128of microseconds actually slept. Can sleep for more than one second
129unlike the usleep system call. See also Time::HiRes::sleep() below.
dcf686c9 130
131=item ualarm ( $useconds [, $interval_useconds ] )
132
133Issues a ualarm call; interval_useconds is optional and will be 0 if
134unspecified, resulting in alarm-like behaviour.
135
443572f5 136=item tv_interval
137
5cb3728c 138C<tv_interval ( $ref_to_gettimeofday [, $ref_to_later_gettimeofday] )>
dcf686c9 139
f7916ddb 140Returns the floating seconds between the two times, which should have
141been returned by gettimeofday(). If the second argument is omitted,
142then the current time is used.
dcf686c9 143
144=item time ()
145
f7916ddb 146Returns a floating seconds since the epoch. This function can be
147imported, resulting in a nice drop-in replacement for the C<time>
148provided with core Perl, see the EXAMPLES below.
dcf686c9 149
f7916ddb 150B<NOTE 1>: this higher resolution timer can return values either less or
151more than the core time(), depending on whether your platforms rounds
152the higher resolution timer values up, down, or to the nearest to get
153the core time(), but naturally the difference should be never more than
154half a second.
155
156B<NOTE 2>: Since Sunday, September 9th, 2001 at 01:46:40 AM GMT
b8ec5d27 157(when the time() seconds since epoch rolled over to 1_000_000_000),
158the default floating point format of Perl and the seconds since epoch
159have conspired to produce an apparent bug: if you print the value of
160Time::HiRes::time() you seem to be getting only five decimals, not six
161as promised (microseconds). Not to worry, the microseconds are there
162(assuming your platform supports such granularity). What is going on
163is that the default floating point format of Perl only outputs 15
164digits. In this case that means ten digits before the decimal
165separator and five after. To see the microseconds you can use either
166printf/sprintf with C<%.6f>, or the gettimeofday() function in list
167context, which will give you the seconds and microseconds as two
389199d8 168separate values.
169
dcf686c9 170=item sleep ( $floating_seconds )
171
f7916ddb 172Sleeps for the specified amount of seconds. Returns the number of
173seconds actually slept (a floating point value). This function can be
174imported, resulting in a nice drop-in replacement for the C<sleep>
175provided with perl, see the EXAMPLES below.
dcf686c9 176
177=item alarm ( $floating_seconds [, $interval_floating_seconds ] )
178
3f2ee006 179The SIGALRM signal is sent after the specified number of seconds.
f7916ddb 180Implemented using ualarm(). The $interval_floating_seconds argument
181is optional and will be 0 if unspecified, resulting in alarm()-like
dcf686c9 182behaviour. This function can be imported, resulting in a nice drop-in
183replacement for the C<alarm> provided with perl, see the EXAMPLES below.
184
3f2ee006 185B<NOTE 1>: With some platform - Perl release combinations select()
186gets restarted by SIGALRM, instead of dropping out of select().
187This means that an alarm() followed by a select() may together take
188the sum of the times specified for the the alarm() and the select(),
189not just the time of the alarm().
190
443572f5 191=item setitimer
192
5cb3728c 193C<setitimer ( $which, $floating_seconds [, $interval_floating_seconds ] )>
3c72ec00 194
09fa32a4 195Start up an interval timer: after a certain time, a signal arrives,
196and more signals may keep arriving at certain intervals. To disable
197a timer, use time of zero. If interval is set to zero (or unspecified),
198the timer is disabled B<after> the next delivered signal.
3c72ec00 199
200Use of interval timers may interfere with alarm(), sleep(), and usleep().
201In standard-speak the "interaction is unspecified", which means that
202I<anything> may happen: it may work, it may not.
203
204In scalar context, the remaining time in the timer is returned.
205
206In list context, both the remaining time and the interval are returned.
207
3f2ee006 208There are usually three or four interval timers available: the $which
209can be ITIMER_REAL, ITIMER_VIRTUAL, ITIMER_PROF, or ITIMER_REALPROF.
210Note that which ones are available depends: true UNIX platforms have
211usually all first three, but for example Win32 and Cygwin only have
212ITIMER_REAL, and only Solaris seems to have ITIMER_REALPROF (which is
213used to profile multithreaded programs).
3c72ec00 214
215ITIMER_REAL results in alarm()-like behavior. Time is counted in
216I<real time>, that is, wallclock time. SIGALRM is delivered when
217the timer expires.
218
219ITIMER_VIRTUAL counts time in (process) I<virtual time>, that is, only
09fa32a4 220when the process is running. In multiprocessor/user/CPU systems this
221may be more or less than real or wallclock time. (This time is also
222known as the I<user time>.) SIGVTALRM is delivered when the timer expires.
3c72ec00 223
224ITIMER_PROF counts time when either the process virtual time or when
225the operating system is running on behalf of the process (such as
226I/O). (This time is also known as the I<system time>.) (Collectively
227these times are also known as the I<CPU time>.) SIGPROF is delivered
228when the timer expires. SIGPROF can interrupt system calls.
229
230The semantics of interval timers for multithreaded programs are
231system-specific, and some systems may support additional interval
232timers. See your setitimer() documentation.
233
234=item getitimer ( $which )
235
236Return the remaining time in the interval timer specified by $which.
237
238In scalar context, the remaining time is returned.
239
240In list context, both the remaining time and the interval are returned.
241The interval is always what you put in using setitimer().
242
dcf686c9 243=back
244
245=head1 EXAMPLES
246
247 use Time::HiRes qw(usleep ualarm gettimeofday tv_interval);
248
249 $microseconds = 750_000;
250 usleep $microseconds;
251
252 # signal alarm in 2.5s & every .1s thereafter
253 ualarm 2_500_000, 100_000;
254
255 # get seconds and microseconds since the epoch
256 ($s, $usec) = gettimeofday;
257
258 # measure elapsed time
259 # (could also do by subtracting 2 gettimeofday return values)
260 $t0 = [gettimeofday];
261 # do bunch of stuff here
262 $t1 = [gettimeofday];
263 # do more stuff here
264 $t0_t1 = tv_interval $t0, $t1;
265
266 $elapsed = tv_interval ($t0, [gettimeofday]);
267 $elapsed = tv_interval ($t0); # equivalent code
268
269 #
270 # replacements for time, alarm and sleep that know about
271 # floating seconds
272 #
273 use Time::HiRes;
274 $now_fractions = Time::HiRes::time;
275 Time::HiRes::sleep (2.5);
276 Time::HiRes::alarm (10.6666666);
277
278 use Time::HiRes qw ( time alarm sleep );
279 $now_fractions = time;
280 sleep (2.5);
281 alarm (10.6666666);
282
3c72ec00 283 # Arm an interval timer to go off first at 10 seconds and
284 # after that every 2.5 seconds, in process virtual time
285
286 use Time::HiRes qw ( setitimer ITIMER_VIRTUAL time );
287
288 $SIG{VTLARM} = sub { print time, "\n" };
289 setitimer(ITIMER_VIRTUAL, 10, 2.5);
290
dcf686c9 291=head1 C API
292
293In addition to the perl API described above, a C API is available for
294extension writers. The following C functions are available in the
295modglobal hash:
296
297 name C prototype
298 --------------- ----------------------
299 Time::NVtime double (*)()
300 Time::U2time void (*)(UV ret[2])
301
302Both functions return equivalent information (like C<gettimeofday>)
303but with different representations. The names C<NVtime> and C<U2time>
304were selected mainly because they are operating system independent.
305(C<gettimeofday> is Un*x-centric.)
306
307Here is an example of using NVtime from C:
308
309 double (*myNVtime)();
310 SV **svp = hv_fetch(PL_modglobal, "Time::NVtime", 12, 0);
311 if (!svp) croak("Time::HiRes is required");
312 if (!SvIOK(*svp)) croak("Time::NVtime isn't a function pointer");
356234a5 313 myNVtime = INT2PTR(double(*)(), SvIV(*svp));
dcf686c9 314 printf("The current time is: %f\n", (*myNVtime)());
315
f03b998d 316=head1 CAVEATS
317
318Notice that the core time() maybe rounding rather than truncating.
319What this means that the core time() may be giving time one second
320later than gettimeofday(), also known as Time::HiRes::time().
321
dcf686c9 322=head1 AUTHORS
323
324D. Wegscheid <wegscd@whirlpool.com>
325R. Schertler <roderick@argon.org>
326J. Hietaniemi <jhi@iki.fi>
327G. Aas <gisle@aas.no>
328
3f2ee006 329=head1 COPYRIGHT AND LICENSE
dcf686c9 330
3f2ee006 331Copyright (c) 1996-2002 Douglas E. Wegscheid. All rights reserved.
dcf686c9 332
3f2ee006 333Copyright (c) 2002 Jarkko Hietaniemi. All rights reserved.
dcf686c9 334
3f2ee006 335This program is free software; you can redistribute it and/or modify
336it under the same terms as Perl itself.
dcf686c9 337
338=cut