Example #1
0
 /**
  * 获得SemFd
  */
 protected static function getSemFd()
 {
     if (!self::$semFd) {
         self::$semFd = \Mutex::create();
     }
     return self::$semFd;
 }
Example #2
0
    {
        $this->mysql = $mysql;
        $this->mutex = $mutex;
    }
    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));
        }
    }
}
$mysql = mysql_connect("127.0.0.1", "root", "");
if ($mysql) {
    $mutex = Mutex::create();
    $instances = array(new MyShared($mysql, $mutex), new MyShared($mysql, $mutex), new MyShared($mysql, $mutex));
    foreach ($instances as $instance) {
        $instance->start();
    }
    foreach ($instances as $instance) {
        $instance->join();
    }
    Mutex::destroy($mutex);
}
Example #3
0
 /**
  * Initializes and the timer factory.
  */
 public function __construct()
 {
     $this->dispatched = true;
     $this->mutex = \Mutex::create();
 }
Example #4
0
        }
        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;
    }
}
$timer = microtime(true);
/* create and lock a mutex */
$mutex = Mutex::create(true);
/* create workers */
$workers = array();
for ($i = 0; $i < 50; $i++) {
    $workers[$i] = new MyWorkerThread(rand(30, 100), $mutex);
    /* they cannot go anywhere, I have the mutex */
    $workers[$i]->start();
}
printf("Release the (muzzled) hounds ... :\n");
Mutex::unlock($mutex);
foreach ($workers as $i => $worker) {
    $workers[$i]->join();
}
printf("Muzzled: %f seconds\n", microtime(true) - $timer);
/* please remember to destroy mutex and condition variables */
Mutex::destroy($mutex);
 /**
  * Test the new instance method.
  *
  * @return void
  */
 public function testNewInstance()
 {
     $className = 'TechDivision\\ApplicationServer\\Mock\\MockContainerThread';
     $instance = $this->server->newInstance($className, array($this->server->getInitialContext(), \Mutex::create(false), $id = 1));
     $this->assertInstanceOf($className, $instance);
 }
 /**
  * Test object destruction when passing to a threads constructor.
  *
  * @return void
  */
 public function testPassThroughThreadConstructor()
 {
     // set the counter to ZERO
     $this->object->counter = 0;
     // check the reference counter
     $this->assertSame(1, Registry::refCount($this->object));
     // initialize the mutex
     $mutex = \Mutex::create();
     // execute the thread
     $thread = new PassThroughConstructorThread($this->object, $mutex);
     $thread->start();
     $thread->join();
     // check the reference counter
     $this->assertSame(1, Registry::refCount($this->object));
 }
Example #7
0
        /* 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;
    }
}
/* create a mutex and acquire lock */
$lock = Mutex::create(true);
/* create new thread, passing the lock as parameter */
$thread = new Syncrhonizing($lock);
/* start executing the child ... */
$thread->start();
/* the child will block until you release the mutex */
printf("Allow(%d):  %f\n", Mutex::unlock($lock), microtime(true));
/* ... continue normal not thread related stuff ... */
/* and then: */
$thread->join();
/* 
	another option would be to lock the mutex and unlock it and let the thread destroy itgetThreadId without calling join,
	when this is stable that will definitely be an option but right now I suggest you join everything you create somewhere
*/
printf("Cleaup(%d):  %f\n", Mutex::destroy($lock), microtime(true));
Example #8
0
 /**
  * Initialize the pool of persistent request handlers per application.
  *
  * @return void
  */
 public function initRequestHandlers()
 {
     // initialize the storage for the request handlers
     $this->requestHandlers = new GenericStackable();
     // iterate over the applications and initialize a pool of request handlers for each
     foreach ($this->applications as $application) {
         // initialize the pool
         $pool = new GenericStackable();
         // initialize 10 request handlers per for each application
         for ($i = 0; $i < 10; $i++) {
             // create a mutex
             $mutex = \Mutex::create();
             // initialize the request handler
             $requestHandler = new RequestHandler();
             $requestHandler->injectMutex($mutex);
             $requestHandler->injectValves($this->valves);
             $requestHandler->injectApplication($application);
             $requestHandler->start(PTHREADS_INHERIT_NONE | PTHREADS_INHERIT_CONSTANTS);
             // add it to the pool
             $pool[] = $requestHandler;
         }
         // add the pool to the pool of request handlers
         $this->requestHandlers[$application->getName()] = $pool;
     }
 }