Example #1
0
 /**
  * Deattaches the passed synchronizable instance from the registry, destroys it, and
  * deletes all data from the APCu if no more instances are registered.
  *
  * @param \AppserverIo\Synchronizable\SynchronizableInterface $synchronizable The instance to be destroyed
  *
  * @return void
  * @throws \RuntimeException Is thrown if the data can't be deleted from APCu
  */
 public static function destroy(SynchronizableInterface $synchronizable)
 {
     // load the serial
     $serial = $synchronizable->__serial();
     // detach the instance
     Registry::detach($synchronizable);
     // destroy the instance
     unset($synchronizable);
     // check if we've to destroy the data here
     if (apc_exists($serial) && apc_fetch($serial) === 0 && Registry::hasData($serial)) {
         $iterator = new \ApcIterator('user', '/^' . $serial . '\\./');
         foreach ($iterator as $key => $value) {
             if (apc_delete($key) === false) {
                 throw new \RuntimeException(sprintf('Can\'t delete property for %s::%s (%s) instance', get_class($synchronizable), $key, $serial));
             }
         }
     }
 }
 /**
  * Tests synchronization by using a mutex.
  *
  * @return void
  */
 public function testSynchronizeCounterWithMutex()
 {
     // set the counter to ZERO
     $this->object->counter = 0;
     // initialize array containing the threads
     $threads = array();
     // initialize the mutex
     $mutex = \Mutex::create();
     // initialize the threads and start them
     for ($i = 0; $i < 2; $i++) {
         $threads[$i] = new RaiseCounterThread($this->object, $mutex);
         $threads[$i]->start();
     }
     // wait for the threads to be finished
     for ($i = 0; $i < 2; $i++) {
         $threads[$i]->join();
     }
     // check the object status
     $this->assertSame(10000, $this->object->counter);
     $this->assertSame(1, Registry::refCount($this->object));
     $this->assertTrue(Registry::hasData($this->object->__serial()));
 }