Example #1
0
 /**
  * @param string $point_name
  * @param \Jamm\Memory\KeyAutoUnlocker $ExitWatcher set variable here to get Watcher into scope.
  *                                                  When this object will be destructed, event will be generated, to catch exiting from scope of function or loop.
  * @see      http://en.wikipedia.org/wiki/Resource_Acquisition_Is_Initialization
  * @return \Jamm\Memory\KeyAutoUnlocker
  */
 public function setWatchedPoint($point_name, \Jamm\Memory\KeyAutoUnlocker &$ExitWatcher = NULL)
 {
     $this->setPoint($point_name);
     if (empty($ExitWatcher)) {
         $ExitWatcher = new \Jamm\Memory\KeyAutoUnlocker(array($this, 'watchFunctionExit'));
     }
     $ExitWatcher->setKey($point_name);
     return $ExitWatcher;
 }
Example #2
0
 /**
  * Get exclusive mutex for key. Key will be still accessible to read and write, but
  * another process can exclude dog-pile effect, if before updating the key he will try to get this mutex.
  * @param mixed $key
  * @param mixed $auto_unlocker_variable - pass empty, just declared variable
  */
 public function lock_key($key, &$auto_unlocker_variable)
 {
     $auto_unlocker = NULL;
     if (!$this->sem->get_access_write($auto_unlocker)) {
         return false;
     }
     $this->readmemory();
     $key = (string) $key;
     if (isset($this->mem[self::map_key_locks][$key])) {
         return false;
     }
     $this->mem[self::map_key_locks][$key] = 1;
     if ($this->refresh()) {
         $auto_unlocker_variable = new \Jamm\Memory\KeyAutoUnlocker(array($this, 'unlock_key'));
         $auto_unlocker_variable->setKey($key);
         return true;
     } else {
         return false;
     }
 }
 /**
  * Get exclusive mutex for key. Key will be still accessible to read and write, but
  * another process can exclude dog-pile effect, if before updating the key he will try to get this mutex.
  * Example:
  * Process 1 reads key simultaneously with Process 2.
  * Value of this key are too old, so Process 1 going to refresh it. Simultaneously with Process 2.
  * But both of them trying to lock_key, and Process 1 only will refresh value of key (taking it from database, e.g.),
  * and Process 2 can decide, what he want to do - use old value and not spent time to database, or something else.
  * @param mixed $key
  * @param mixed $auto_unlocker_variable - pass empty, just declared variable
  */
 public function lock_key($key, &$auto_unlocker_variable)
 {
     $r = $this->mem_object->add(self::lock_key_prefix . $key, 1, $this->key_lock_time);
     if (!$r) {
         return false;
     }
     $auto_unlocker_variable = new \Jamm\Memory\KeyAutoUnlocker(array($this, 'unlock_key'));
     $auto_unlocker_variable->setKey($key);
     return true;
 }