. */ /** * This file contains the Semaphore mutex implementation. * This method uses the Semaphore extension to create a mutex. * * @author Dinu Florin * @package Core * @subpackage Mutex */ require_once 'MutexInterface.class.php'; /** * Semaphore mutex class. * * @author Dinu Florin * @package Core * @subpackage Mutex */ class SemaphoreMutex implements MutexInterface { protected $key = null; protected $sem = null; /** * @see MutexInterface::canInstantiate() */ public static function canInstantiate() { return function_exists('sem_get'); } /** * Constructor. * * @see MutexInterface::__construct() */ public function __construct($name) { $this->key = Util::makeIntFromString($name); $this->sem = sem_get($this->key, 1, 0666, 1); if($this->sem === false) throw new MutexException('Error geting semaphore'); } /** * Lock the mutex. * * @see MutexInterface::lock() */ public function lock() { if(!sem_acquire($this->sem)) { throw new MutexException('Error locking mutex'); } } /** * Unlock the mutex. * * @see MutexInterface::unlock() */ public function unlock() { if(!sem_release($this->sem)) { throw new MutexException('Error unlocking mutex'); } } } ?>