コード例 #1
0
ファイル: mutex.php プロジェクト: noccy80/lepton-ng
 /**
  * Constructor
  */
 function __construct($name, $timeoutms = 5000)
 {
     if (!function_exists('shm_has_var')) {
         throw new BaseException("No mutex support on this platform");
     }
     $this->_lockname = $name;
     // Block until lock can be acquired
     if (Mutex::$instance == 0) {
         Console::debug("Creating mutex manager");
         Mutex::$resource = shm_attach(Mutex::SHM_KEY);
         if (!shm_has_var(Mutex::$resource, Mutex::SHM_LOCKS)) {
             shm_put_var(Mutex::$resource, Mutex::SHM_LOCKS, array());
         }
     }
     $this->enterCriticalSection();
     Console::debug("Waiting for lock %s", $this->_lockname);
     $t = new timer(true);
     while (true) {
         $ls = shm_get_var(Mutex::$resource, Mutex::SHM_LOCKS);
         if (!isset($ls[$name])) {
             break;
         }
         usleep(100000);
         if ($t->getElapsed() > $timeoutms / 1000) {
             $this->exitCriticalSection();
             throw new MutexException("Timed out waiting for lock");
         }
     }
     Console::debug("Acquiring lock %s", $this->_lockname);
     $ls = shm_get_var(Mutex::$resource, Mutex::SHM_LOCKS);
     $ls[$name] = true;
     shm_put_var(Mutex::$resource, Mutex::SHM_LOCKS, $ls);
     Mutex::$instance++;
     $this->_lockstate = true;
     $this->exitCriticalSection();
 }