note 82920 added to function.sem-get

From: Date: Fri, 02 May 2008 00:51:02 +0000
Subject: note 82920 added to function.sem-get
Groups: php.notes 
Request: Send a blank email to php-notes+get-138981@lists.php.net to get a copy of this message
Implementation of a read-write semaphore in PHP: <?php class rw_semaphore { const READ_ACCESS = 0; const WRITE_ACCESS = 1; /** * @access private * @var resource - mutex semaphore */ private $mutex; /** * @access private * @var resource - read/write semaphore */ private $resource; /** * @access private * @var int */ private $writers = 0; /** * @access private * @var int */ private $readers = 0; /** * Default constructor * * Initialize the read/write semaphore */ public function __construct() { $mutex_key = ftok('/home/cyrus/development/php/sysvipc/rw_semaphore.php', 'm'); $resource_key = ftok('/home/cyrus/development/php/sysvipc/rw_semaphore.php', 'r'); $this->mutex = sem_get($mutex_key, 1); $this->resource = sem_get($resource_key, 1); } /** * Destructor * * Remove the read/write semaphore */ public function __destruct() { sem_remove($this->mutex); sem_remove($this->resource); } /** * Request acess to the resource * * @param int $mode * @return void */ private function request_access($access_type = self::READ_ACCESS) { if ($access_type == self::WRITE_ACCESS) { sem_acquire($this->mutex); /* update the writers counter */ $this->writers++; sem_release($this->mutex); sem_acquire($this->resource); } else { sem_acquire($this->mutex); if ($this->writers > 0 || $this->readers == 0) { sem_release($this->mutex); sem_acquire($this->resource); sem_acquire($this->mutex); } /* update the readers counter */ $this->readers++; sem_release($this->mutex); } } private function request_release($access_type = self::READ_ACCESS) { if ($access_type == self::WRITE_ACCESS) { sem_acquire($this->mutex); /* update the writers counter */ $this->writers--; sem_release($this->mutex); sem_release($this->resource); } else { sem_acquire($this->mutex); /* update the readers counter */ $this->readers--; if ($this->readers == 0) sem_release($this->resource); sem_release($this->mutex); } } /** * Request read access to the resource * * @return void */ public function read_access() { $this->request_access(self::READ_ACCESS); } /** * Release read access to the resource * * @return void */ public function read_release() { $this->request_release(self::READ_ACCESS); } /** * Request write access to the resource * * @return void */ public function write_access() { $this->request_access(self::WRITE_ACCESS); } /** * Release write access to the resource * * @return void */ public function write_release() { $this->request_release(self::WRITE_ACCESS); } } ?> ---- Server IP: 209.41.74.194 Probable Submitter: 24.20.125.119 ---- Manual Page -- http://www.php.net/manual/en/function.sem-get.php Edit -- https://master.php.net/note/edit/82920 Del: integrated -- https://master.php.net/note/delete/82920/integrated Del: useless -- https://master.php.net/note/delete/82920/useless Del: bad code -- https://master.php.net/note/delete/82920/bad+code Del: spam -- https://master.php.net/note/delete/82920/spam Del: non-english -- https://master.php.net/note/delete/82920/non-english Del: in docs -- https://master.php.net/note/delete/82920/in+docs Del: other reasons-- https://master.php.net/note/delete/82920 Reject -- https://master.php.net/note/reject/82920 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#138981) next »