2
# Copyright (c) 2005, 2006 MySQL AB, 2008 Sun Microsystems, Inc.
3
# Use is subject to license terms.
5
# This program is free software; you can redistribute it and/or modify
6
# it under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; version 2 of the License.
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
# GNU General Public License for more details.
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
# This is a library file used by the Perl version of mysql-test-run,
19
# and is part of the translation of the Bourne shell script with the
25
sub mtr_init_timers ();
26
sub mtr_timer_start($$$);
27
sub mtr_timer_stop($$);
28
sub mtr_timer_stop_all($);
31
##############################################################################
33
# Initiate the structure shared by all timers
35
##############################################################################
37
sub mtr_init_timers () {
38
my $timers = { timers => {}, pids => {}};
43
##############################################################################
45
# Start, stop and poll a timer
47
# As alarm() isn't portable to Windows, we use separate processes to
50
##############################################################################
52
sub mtr_timer_start($$$) {
53
my ($timers,$name,$duration)= @_;
55
if ( exists $timers->{'timers'}->{$name} )
57
# We have an old running timer, kill it
58
mtr_warning("There is an old timer running");
59
mtr_timer_stop($timers,$name);
66
if ( ! defined $tpid )
68
if ( $! == $!{EAGAIN} ) # See "perldoc Errno"
70
mtr_warning("Got EAGAIN from fork(), sleep 1 second and redo");
76
mtr_error("can't fork timer, error: $!");
82
# Parent, record the information
83
mtr_verbose("Starting timer for '$name',",
84
"duration: $duration, pid: $tpid");
85
$timers->{'timers'}->{$name}->{'pid'}= $tpid;
86
$timers->{'timers'}->{$name}->{'duration'}= $duration;
87
$timers->{'pids'}->{$tpid}= $name;
91
# Child, install signal handlers and sleep for "duration"
93
# Don't do the ^C cleanup in the timeout child processes!
94
# There is actually a race here, if we get ^C after fork(), but before
95
# clearing the signal handler.
99
mtr_verbose("timer $$ woke up, exiting!");
103
$0= "mtr_timer(timers,$name,$duration)";
105
mtr_verbose("timer $$ expired after $duration seconds");
112
sub mtr_timer_stop ($$) {
113
my ($timers,$name)= @_;
115
if ( exists $timers->{'timers'}->{$name} )
117
my $tpid= $timers->{'timers'}->{$name}->{'pid'};
118
mtr_verbose("Stopping timer for '$name' with pid $tpid");
120
# FIXME as Cygwin reuses pids fast, maybe check that is
121
# the expected process somehow?!
124
# As the timers are so simple programs, we trust them to terminate,
125
# and use blocking wait for it. We wait just to avoid a zombie.
128
delete $timers->{'timers'}->{$name}; # Remove the timer information
129
delete $timers->{'pids'}->{$tpid}; # and PID reference
134
mtr_error("Asked to stop timer '$name' not started");
138
sub mtr_timer_stop_all ($) {
141
foreach my $name ( keys %{$timers->{'timers'}} )
143
mtr_timer_stop($timers, $name);
149
sub mtr_timer_timeout ($$) {
150
my ($timers,$pid)= @_;
152
return "" unless exists $timers->{'pids'}->{$pid};
154
# Got a timeout(the process with $pid is recorded as being a timer)
155
# return the name of the timer
156
return $timers->{'pids'}->{$pid};