title: nested lock/free calls in shared memory synchronization using semaphores
body: <h2>[EDIT] Complete rewrite with added Background (original question below)</h2>
  
  <p>In an application running in PHP I used shared memory to store values temporarily for performance reasons (database has too much overhead and files are too slow). </p>
  
  <p>I built a really simple shared memory class that gives scripts access to variables stored in shared memory and has the ability to synchronize calls using semaphores. The code is here (no error handling as of yet):</p>
  
  <p>
  
  <pre><code>class SHM {
  
      private static $defaultSize = 10000;
  
      private static function getIdentifier ($identFile, $projId) {
          return ftok($identFile, $projId);
      }
  
      private $sem = NULL;
      private $shm = NULL;
      private $identFile;
      private $projId;
      private $size;
  
      public function __construct($identFile, $projId, $size=NULL) {
          if ($size === NULL) $size = self::$defaultSize;
          $this-&gt;identFile = $identFile;
          $this-&gt;projId    = $projId;
          $this-&gt;size      = $size;
      }
  
      public function __destruct() {
          if ($this-&gt;sem) {
              $this-&gt;lock();
              if ($this-&gt;shm) {
                  shm_detach($this-&gt;shm);
              }
              $this-&gt;free();
          }
      }
  
      public function exists ($key) {
          return shm_has_var($this-&gt;getShm(), $key);
      }
  
      public function get ($key, $lock=true) {
          if ($this-&gt;exists ($key)) {
              if ($lock) $this-&gt;lock();
              $var = shm_get_var($this-&gt;getShm(), $key);
              if ($lock) $this-&gt;free();
              return $var;
          } else return NULL;
      }
  
      public function set ($key, $var, $lock=true) {
          if ($lock) $this-&gt;lock();
          shm_put_var($this-&gt;getShm(), $key, $var);
          if ($lock) $this-&gt;free();
      }
  
      public function remove ($key, $lock=true) {
          if ($this-&gt;exists ($key)) {
              if ($lock) $this-&gt;lock();
              $result = shm_remove_var($this-&gt;getShm(), $key);
              if ($lock) $this-&gt;free();
              return $result;
          } else return NULL;
      }
  
      public function clean () {
          $this-&gt;lock();
          shm_remove($this-&gt;shm);
          $this-&gt;free();
          sem_remove($this-&gt;sem);
          $this-&gt;shm = NULL;
          $this-&gt;sem = NULL;
      }
  
      private function getSem () {
          if ($this-&gt;sem === NULL) {
              $this-&gt;sem = sem_get(self::getIdentifier($this-&gt;identFile, $this-&gt;projId));
          }
          return $this-&gt;sem;
      }
  
      private function lock () {
          return sem_acquire($this-&gt;getSem());
      }
  
      private function free () {
          return sem_release($this-&gt;getSem());
      }
  
      private function getShm () {
          if ($this-&gt;shm === NULL) {
              $this-&gt;shm = shm_attach(self::getIdentifier($this-&gt;identFile, $this-&gt;projId), $this-&gt;size);
          }
          return $this-&gt;shm;
      }
  }
  </code></pre>
  
  <p>I now have another class that uses this shared memory class and needs to perform a "get, modify and write" operation on one variable. Basically, this:</p>
  
  <pre><code>function getModifyWrite () {
     $var = $mySHM-&gt;get('var');
     $var += 42;
     $mySHM-&gt;set('var', $var);
  }
  </code></pre>
  
  <p>The way it is now, this would lock the semaphore, free it, lock it again, and free it. I would love to have the code executed with the semaphore locke dthe whole time.</p>
  
  <p>Previously, I had the code surrounded by one <code>sem_acquire</code> and <code>sem_release</code> pair. Unfortunately it turned out (thanks @Ben) that System V binary semaphores block on additional <code>lock</code> calls by the same process.</p>
  
  <p>There are no monitors in PHP either (that would actually solve it), and I'm not too keen on implementing them on my own using some shared-memory varialbes (also I guess I could do this...) plus the traditional semaphores.
  I need exclusive access, so non-binary semaphores aren't an option either.</p>
  
  <p>Any sugestions on hwo to do this, without violating DRY principles?</p>
  
  <h2>original question</h2>
  
  <p>Just a quick question on how System V semaphores work and how PHP uses them:  </p>
  
  <p>If I lock (<code>sem_acquire</code>)  one semaphore in one process multiple times, is the semaphore value actually increased with every call (so I need to free (<code>sem_release</code>) it as often as I locked it), or do additional calls of <code>sem_acquire</code> just continues without counting up if the process already owns the semaphore (so the first <code>free</code> always unlocks the semaphore)?</p>
  
  <p>When in doubt, a hint on how to reasonably test this would be enough ^^</p>
  
  <p>Example:</p>
  
  <pre><code>$sem = sem_get(ftok('/some/file', 'a'));
  
  function doSomething1 () {
       sem_acquire($sem);
       doSomething2();
       // do something else
       sem_release($sem);
  }
  
  function doSomething2 () {
       sem_acquire($sem);
       // do stuff
       sem_release($sem);
  }
  </code></pre>
  
  <p>In the code above, If I call <code>doSomething1</code>, would the <code>sem_release</code> inside <code>doSomething2</code> already free the semaphore for other processes, or was the semaphore counter actually set to "2" (even though it only has a capacity of one, as nothing else was specified in <code>sem_get</code>) and the semaphore stays locked until released the second time?</p>
  
  <p>I need it to stay locked until <code>doSOmething1</code> has finished its work, obviously. I could, of course, ujst copy the contets of <code>doSomething2</code>, but this violates DRY principles and I want to avoid it. I coulkd, of course, also pack the work inside <code>doSOmething2</code> inside a private function and call that one from both others, but that is additional, potntially unnecessary overhead, too - so I'm aksing first before doing it. And, of course ³, the real thing isn't that simple.</p>
  
  <p>I DO know how semaphores in general work, but as there are multiple implementation strategies, I want to make sure System V semaphores work the way I'd expect them to work (that is, increasing the counter and requireng as many calls to <code>free</code>as they received <code>lock</code> calls).</p>