On Sat, 3 Mar 2007 16:17:59 +0100
"Jonas B. Nielsen" <[EMAIL PROTECTED]> wrote:

> Thanks for you feedback, but you have to explain your thoughts to me  
> a bit more explicitly.
> 
> I am standing at the Copenhagen Perl Mongers booth at LinuxForum in  
> Copenhagen and yes I am a bit slow too.
> 
> As I see your two suggestions for methods, time_atmost is what would  
> be another name for the sub I have already implemented (time_ok and  
> its evil twin time_nok).
> 
> As for time_atmost, this one troubles me a bit, as I see it would be  
> fun to include, but it's use is somewhat vague to me :)

You mention time_atmost there twice - I think one of those is a typo, but
I can't see which one :)

I've had a bit more of a thought, and it now looks like this in my head:


  time_between( $code, $atleast, $atmost, $message );
     # Check that $code->() takes at least $atleast seconds, and no more
     # than $atmost. Optionally install an alarm handler at, say, 
     # $atmost + 2 seconds (perhaps that 2 is configurable).


Then the other primitives fall out almost trivially:


  time_atleast( $code, $atleast, $message )  ==
     time_between( $code, $atleast, undef, $message );

  time_atmost( $code, $atmost, $message ) ==
     time_between( $code, 0, $atmost, $message );

  time_ok( $code, $time, $message ) ==
     time_between( $code, $time - $delta, $time + $delta, $message );


where $delta is some other configurable. Perhaps detect if Time::HiRes is
loaded, and use $delta = 0.1 if it is, and 1 if it isn't..? Random
thoughts..

Be careful of subtle races - remember what e.g. sleep guarantees.
sleep(5) for example, waits at least 4 seconds and no more than 5.


> Your suggestion for a use of $SIG{ALRM} is very good and will be  
> implemented prior to the first release, since it seems very essential  
> and addresses a problem I did not see coming, good thing we have this  
> list.

It also occurs to me, that if we can't use SIGALRM, then maybe the
following might be better:

  my $pid = $$;
  my $kid;
  if( ( $kid = fork() ) == 0 ) {
     # child
     sleep( $timeout );
     kill SIGTERM, $pid;
     exit( 0 );
  }

  $code->();
  kill SIGKILL, $kid;

This way, $code->() isn't affected by SIGALRM.

-- 
Paul "LeoNerd" Evans

[EMAIL PROTECTED]
ICQ# 4135350       |  Registered Linux# 179460
http://www.leonerd.org.uk/

Reply via email to