2
0
mirror of https://github.com/fhem/fhem-mirror.git synced 2025-04-20 07:16:03 +00:00

Added AbortFn

git-svn-id: https://svn.fhem.de/fhem/trunk@3054 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
rudolfkoenig 2013-04-08 15:56:14 +00:00
parent f9185de861
commit c074b553d9

View File

@ -4,10 +4,11 @@ package main;
=pod =pod
### Usage: ### Usage:
sub TestBlocking() { BlockingCall("DoSleep", 5, "SleepDone", 8); } sub TestBlocking() { BlockingCall("DoSleep", 5, "SleepDone", 8, "AbortFn", "AbortArg"); }
sub DoSleep($) { sleep(shift); return "I'm done"; } sub DoSleep($) { sleep(shift); return "I'm done"; }
sub SleepDone($) { Log 1, "SleepDone: " . shift; } sub SleepDone($) { Log 1, "SleepDone: " . shift; }
sub TestBlocking2() { BlockingCall("DoSleep", 5, "SleepDone", 2); } sub AbortFn($) { Log 1, "Aborted: " . shift; }
sub TestBlocking2() { BlockingCall("DoSleep", 5, "SleepDone", 2, "AbortFn", "AbortArg"); }
=cut =cut
@ -25,7 +26,7 @@ my $telnetDevice;
sub sub
BlockingCall($$@) BlockingCall($$@)
{ {
my ($blockingFn, $arg, $finishFn, $timeout) = @_; my ($blockingFn, $arg, $finishFn, $timeout, $abortFn, $abortArg) = @_;
# Look for the telnetport # Look for the telnetport
# must be done before forking to be able to create a temporary device # must be done before forking to be able to create a temporary device
@ -66,7 +67,8 @@ BlockingCall($$@)
} }
if($pid) { if($pid) {
my %h = ( pid=>$pid, fn=>$blockingFn, finishFn=>$finishFn ); my %h = ( pid=>$pid, fn=>$blockingFn, finishFn=>$finishFn,
abortFn=>$abortFn, abortArg=>$abortArg );
if($timeout) { if($timeout) {
InternalTimer(gettimeofday()+$timeout, "BlockingKill", \%h, 0); InternalTimer(gettimeofday()+$timeout, "BlockingKill", \%h, 0);
} }
@ -131,10 +133,16 @@ BlockingKill($)
if($^O !~ m/Win/) { if($^O !~ m/Win/) {
if($h->{pid} && kill(9, $h->{pid})) { if($h->{pid} && kill(9, $h->{pid})) {
Log 1, "Timeout for $h->{fn} reached, terminated process $h->{pid}"; Log 1, "Timeout for $h->{fn} reached, terminated process $h->{pid}";
if($h->{finishFn}) { if($h->{abortFn}) {
no strict "refs";
my $ret = &{$h->{abortFn}}($h->{abortArg});
use strict "refs";
} elsif($h->{finishFn}) {
no strict "refs"; no strict "refs";
my $ret = &{$h->{finishFn}}(); my $ret = &{$h->{finishFn}}();
use strict "refs"; use strict "refs";
} }
} }
} }