/**
  * Raise the counter inside the thread context.
  *
  * @return void
  */
 public function run()
 {
     // raise the counter by 1 till 5000
     for ($i = 0; $i < 5000; $i++) {
         \Mutex::lock($this->mutex);
         $this->object->counter++;
         \Mutex::unlock($this->mutex);
     }
 }
Пример #2
0
 public function run()
 {
     if ($this->mutex) {
         printf("LOCK(%d): %d\n", $this->getThreadId(), Mutex::lock($this->mutex));
     }
     if ($result = mysql_query("SHOW PROCESSLIST;", $this->mysql)) {
         while ($row = mysql_fetch_assoc($result)) {
             print_r($row);
         }
     }
     if ($this->mutex) {
         printf("UNLOCK(%d): %d\n", $this->getThreadId(), Mutex::unlock($this->mutex));
     }
 }
Пример #3
0
 public function run()
 {
     if ($this->mutex) {
         $locked = Mutex::lock($this->mutex);
     }
     printf("%s#%lu:<-", $locked ? "Y" : "N", $this->getThreadId());
     $i = 0;
     while ($i++ < $this->limit) {
         echo ".";
     }
     printf("->\n");
     if ($this->mutex) {
         Mutex::unlock($this->mutex);
     }
     return true;
 }
Пример #4
0
 public function run()
 {
     if ($this->mutex) {
         $locked = Mutex::lock($this->mutex);
     }
     $counter = shm_get_var($this->shmid, 1);
     $counter++;
     shm_put_var($this->shmid, 1, $counter);
     printf("Thread #%lu lock: %s says: %s\n", $this->getThreadId(), !empty($locked) ? "Y" : "N", $counter);
     //$this->lock();
     //$this->unlock();
     if ($this->mutex) {
         Mutex::unlock($this->mutex);
     }
     return true;
 }
Пример #5
0
 public function run()
 {
     /* see, first lock mutex */
     printf("Running(%d) %f\n", Mutex::lock($this->lock), microtime(true));
     /* print some stuff to standard output */
     $stdout = fopen("php://stdout", "w");
     while (++$i < rand(200, 400)) {
         echo ".";
         fflush($stdout);
     }
     echo "\n";
     fflush($stdout);
     /* and unlock mutex, making it ready for destruction */
     printf("Returning(%d) %f\n", Mutex::unlock($this->lock), microtime(true));
     fflush($stdout);
     /* you should close resources, or not, whatever; I'm not your mother ... */
     return null;
 }
Пример #6
0
 public function run()
 {
     if ($this->mutex) {
         $locked = Mutex::lock($this->mutex);
     }
     /*
             $this->synchronized(function($thread){
                 $thread->wait();
             }, $this);
     */
     $counter = intval(fgets($this->handle));
     $counter++;
     //$this->lock();
     //fseek($this->handle, 0);
     rewind($this->handle);
     fputs($this->handle, $counter);
     //fflush($this->handle);
     //$this->unlock();
     printf("Thread #%lu says: %s\n", $this->getThreadId(), $counter);
     if ($this->mutex) {
         Mutex::unlock($this->mutex);
     }
 }
Пример #7
0
 /**
  * Create a new timer instance with the passed data.
  *
  * @param \AppserverIo\Psr\EnterpriseBeans\TimerServiceInterface $timerService      The timer service to create the service for
  * @param \DateTime                                              $initialExpiration The date at which the first timeout should occur.
  *                                                                                  If the date is in the past, then the timeout is triggered immediately
  *                                                                                  when the timer moves to TimerState::ACTIVE
  * @param integer                                                $intervalDuration  The interval (in milli seconds) between consecutive timeouts for the newly created timer.
  *                                                                                  Cannot be a negative value. A value of 0 indicates a single timeout action
  * @param \Serializable                                          $info              Serializable info that will be made available through the newly created timers Timer::getInfo() method
  * @param boolean                                                $persistent        TRUE if the newly created timer has to be persistent
  *
  * @return \AppserverIo\Psr\EnterpriseBeans\TimerInterface Returns the newly created timer
  *
  * @throws \Exception
  */
 public function createTimer(TimerServiceInterface $timerService, \DateTime $initialExpiration, $intervalDuration = 0, \Serializable $info = null, $persistent = true)
 {
     // lock the method
     \Mutex::lock($this->mutex);
     // we're not dispatched
     $this->dispatched = false;
     // initialize the data
     $this->info = $info;
     $this->persistent = $persistent;
     $this->timerService = $timerService;
     $this->intervalDuration = $intervalDuration;
     $this->initialExpiration = $initialExpiration->format(ScheduleExpression::DATE_FORMAT);
     // notify the thread
     $this->notify();
     // wait till we've dispatched the request
     while ($this->dispatched === false) {
         usleep(100);
     }
     // unlock the method
     \Mutex::unlock($this->mutex);
     // return the created timer
     return $this->timer;
 }
Пример #8
0
         if (DEBUG_IMAGE) {
             debugLog("full-image:symlink original " . basename($image));
         }
         clearstatcache();
     } else {
         if (@copy($image_path, $cache_path)) {
             if (DEBUG_IMAGE) {
                 debugLog("full-image:copy original " . basename($image));
             }
             clearstatcache();
         }
     }
 } else {
     //	have to create the image
     $iMutex = new Mutex('i', getOption('imageProcessorConcurrency'));
     $iMutex->lock();
     $newim = zp_imageGet($image_path);
     if ($rotate) {
         $newim = zp_rotateImage($newim, $rotate);
     }
     if ($watermark_use_image) {
         $watermark_image = getWatermarkPath($watermark_use_image);
         if (!file_exists($watermark_image)) {
             $watermark_image = SERVERPATH . '/' . ZENFOLDER . '/images/imageDefault.png';
         }
         $offset_h = getOption('watermark_h_offset') / 100;
         $offset_w = getOption('watermark_w_offset') / 100;
         $watermark = zp_imageGet($watermark_image);
         $watermark_width = zp_imageWidth($watermark);
         $watermark_height = zp_imageHeight($watermark);
         $imw = zp_imageWidth($newim);
 /**
  * Create a calendar-based timer based on the input schedule expression.
  *
  * @param \AppserverIo\Psr\EnterpriseBeans\TimerServiceInterface $timerService  The timer service to create the service for
  * @param \AppserverIo\Psr\EnterpriseBeans\ScheduleExpression    $schedule      A schedule expression describing the timeouts for this timer
  * @param \Serializable                                          $info          Serializable info that will be made available through the newly created timers Timer::getInfo() method
  * @param boolean                                                $persistent    TRUE if the newly created timer has to be persistent
  * @param \AppserverIo\Lang\Reflection\MethodInterface           $timeoutMethod The timeout method to be invoked
  *
  * @return \AppserverIo\Psr\EnterpriseBeans\TimerInterface The newly created Timer.
  * @throws \AppserverIo\Psr\EnterpriseBeans\EnterpriseBeansException If this method could not complete due to a system-level failure.
  * @throws \Exception
  */
 public function createTimer(TimerServiceInterface $timerService, ScheduleExpression $schedule, \Serializable $info = null, $persistent = true, MethodInterface $timeoutMethod = null)
 {
     // lock the method
     \Mutex::lock($this->mutex);
     // we're not dispatched
     $this->dispatched = false;
     // initialize the data
     $this->info = $info;
     $this->schedule = $schedule;
     $this->persistent = $persistent;
     $this->timerService = $timerService;
     $this->timeoutMethod = $timeoutMethod;
     // notify the thread
     $this->notify();
     // wait till we've dispatched the request
     while ($this->dispatched === false) {
         usleep(100);
     }
     // unlock the method
     \Mutex::unlock($this->mutex);
     // return the created timer
     return $this->timer;
 }
 /**
  * Create a new instance with the passed data.
  *
  * @param string      $className The fully qualified class name to return the instance for
  * @param string|null $sessionId The session-ID, necessary to inject stateful session beans (SFBs)
  * @param array       $args      Arguments to pass to the constructor of the instance
  *
  * @return object The instance itself
  *
  * @throws \Exception
  */
 public function newInstance($className, $sessionId = null, array $args = array())
 {
     // lock the method
     \Mutex::lock($this->mutex);
     // we're not dispatched
     $this->dispatched = false;
     // initialize the data
     $this->args = $args;
     $this->sessionId = $sessionId;
     $this->className = $className;
     // notify the thread
     $this->notify();
     // wait till we've dispatched the request
     while ($this->dispatched === false) {
         usleep(100);
     }
     // try to load the last created instance
     if (isset($this->instances[$last = sizeof($this->instances) - 1])) {
         $instance = $this->instances[$last];
     } else {
         throw new \Exception('Requested instance can\'t be created');
     }
     // unlock the method
     \Mutex::unlock($this->mutex);
     // return the created instance
     return $instance;
 }
Пример #11
0
 /**
  * 获取写锁
  * @return true
  */
 public static function get()
 {
     \Mutex::lock(self::getSemFd());
 }
Пример #12
0
<?php

require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . '../lib/Mutex/Mutex.class.php';
$m = new Mutex('allo');
echo $m->getMutexType();
$fiction = true;
if ($fiction === false) {
    $x = new Mutex('allo');
    $x->lock();
    echo "Yay";
    $x->unlock();
}
$m->lock();
echo "locked";
$m->unlock();
echo "unlocked";
unset($m);
Пример #13
0
 /**
  * Handles the request processing.
  *
  * @param \AppserverIo\Psr\Servlet\Http\HttpServletRequestInterface  $servletRequest  The actual request instance
  * @param \AppserverIo\Psr\Servlet\Http\HttpServletResponseInterface $servletResponse The actual response instance
  *
  * @return void
  *
  * @throws \Exception
  */
 public function handleRequest(HttpServletRequestInterface $servletRequest, HttpServletResponseInterface $servletResponse)
 {
     // lock the method
     \Mutex::lock($this->mutex);
     do {
         // create a counter
         $counter = 0;
         // if this is the first loop
         if ($counter === 0) {
             // we're not dispatched
             $this->dispatched = false;
             // synchronize request/response
             $this->servletRequest = $servletRequest;
             $this->servletResponse = $servletResponse;
             // notify the thread
             $this->synchronized(function ($self) {
                 $self->notify();
             }, $this);
         }
         // raise the counter
         $counter++;
         // we wait for 100 iterations
         if ($counter > 100) {
             throw new \Exception('Can\'t handle request');
         }
         // lower system load a bit
         usleep(100);
     } while ($this->dispatched === false);
     // unlock the method
     \Mutex::unlock($this->mutex);
 }
Пример #14
0
 /**
  * Start an atomic operation on a record
  * Important: use this just if you need atomic logic between read and write,
  * for example visit counter.
  * Simple write operations are already automatically atomic, and don't need
  * this method to be called.
  */
 public function atomicBegin($label)
 {
     if (!$this->exists($label)) {
         self::$log->error("Trying to get Mutex for a record whose label does not exist");
         return false;
     }
     $m = new Mutex($this->label2key($this->getContext() . $label));
     $m->lock();
     return true;
 }