[Israel.pm] critical section

Chanan Berler bc.other at gmail.com
Tue Oct 19 06:18:17 PDT 2010


Hello to all perl programmers
I am trying to lock some code as critical section:
my code excepts mode_lock, mutex (filename to lock), and timeout and
will lock / unlock the file.
I am running this on RedHOT linux and Perl version 5.10.

Q1: calling this function to lock the file, will I lose the LOCK_FILE
due to scope issue ?
Q2: what will happen if I open a critical section, within another
critical section ? with diffrent mutex files, will it still work for
me ?
      (means will it release the right LOCK_FILE handler ?
Q3: is there a better way to lock / unlock critical sections ?
thanks
Chanan


sub _get_lock_priority_
{
   my ($server, $params) = @_;
   my $succeded_operation = 0;
   my $mutex_filename = $params->{mutex} . ".lock";

   # enable critical section
   if ($params->{mode_lock} eq "GET_LOCK_MODE_ID")
   {

       open (LOCK_FILE, "> $mutex_filename");
       foreach (0 .. ($params->{timeout} - 1))
       {
            $succeded_operation = 1 if flock (LOCK_FILE , 2|4);
            last if ($succeded_operation);
            sleep(1);
       }
   } else
   {
      # disable critical section
      $succeded_operation close(LOCK_FILE);
      foreach (0 .. ($params->{timeout}-1))
      {
           if (-e $mutex_filename)
           {
               $succeded_operation = 1;
               last;
           }
           sleep(1);      }
   }
   return $succeded_operation;
}

-- 
===================
----     Chanan Berler    ----
===================


More information about the Perl mailing list