Exemplo n.º 1
0
 /**
  * Initializes the critical section.
  * @param int $afterForkPid The process identifier obtained after the execution of a fork() used to differentiate between different processes - pseudo threads.
  * @param int $threadId The internal to the GPhpThread identifier of the current thread instance which is unique identifier in the context of the current process.
  * @return bool On success returns true otherwise false.
  */
 public function initialize($afterForkPid, $threadId)
 {
     // {{{
     $this->myPid = getmypid();
     $retriesLimit = 60;
     if ($this->myPid == $this->creatorPid) {
         // parent
         $i = 0;
         do {
             $intercomWrite = new GPhpThreadIntercom("{$this->pipeDir}gphpthread_{$this->uniqueId}_s{$this->myPid}-d{$afterForkPid}", false, true);
             if ($intercomWrite->isInitialized()) {
                 $i = $retriesLimit;
             } else {
                 ++$i;
                 usleep(mt_rand(5000, 80000));
             }
         } while ($i < $retriesLimit);
         $i = 0;
         do {
             $intercomRead = new GPhpThreadIntercom("{$this->pipeDir}gphpthread_{$this->uniqueId}_s{$afterForkPid}-d{$this->myPid}", true, true);
             if ($intercomRead->isInitialized()) {
                 $i = $retriesLimit;
             } else {
                 ++$i;
                 usleep(mt_rand(5000, 80000));
             }
         } while ($i < $retriesLimit);
         if ($intercomWrite->isInitialized() && $intercomRead->isInitialized()) {
             $this->mastersThreadSpecificData[$threadId] = array('intercomRead' => $intercomRead, 'intercomWrite' => $intercomWrite, 'intercomInterlocutorPid' => $afterForkPid, 'dispatchPriority' => 0);
             return true;
         }
         return false;
     } else {
         // child
         self::$instancesCreatedEverAArr = null;
         // the child must not know for its neighbours
         $this->mastersThreadSpecificData = null;
         // and any details for the threads inside cs instance simulation
         self::$threadsForRemovalAArr = null;
         // these point to the same memory location and also become
         // aliases of each other which is strange?!?
         // so we need to recreate them
         unset($this->intercomWrite);
         unset($this->intercomRead);
         unset($this->intercomInterlocutorPid);
         $this->intercomWrite = null;
         $this->intercomRead = null;
         $this->intercomInterlocutorPid = null;
         $this->intercomInterlocutorPid = $this->creatorPid;
         $i = 0;
         do {
             $this->intercomWrite = new GPhpThreadIntercom("{$this->pipeDir}gphpthread_{$this->uniqueId}_s{$this->myPid}-d{$this->creatorPid}", false, true);
             if ($this->intercomWrite->isInitialized()) {
                 $i = $retriesLimit;
             } else {
                 ++$i;
                 usleep(mt_rand(5000, 80000));
             }
         } while ($i < $retriesLimit);
         $i = 0;
         do {
             $this->intercomRead = new GPhpThreadIntercom("{$this->pipeDir}gphpthread_{$this->uniqueId}_s{$this->creatorPid}-d{$this->myPid}", true, false);
             if ($this->intercomRead->isInitialized()) {
                 $i = $retriesLimit;
             } else {
                 ++$i;
                 usleep(mt_rand(5000, 80000));
             }
         } while ($i < $retriesLimit);
         if (!$this->intercomWrite->isInitialized()) {
             $this->intercomWrite = null;
         }
         if (!$this->intercomRead->isInitialized()) {
             $this->intercomRead = null;
         }
         if ($this->intercomWrite == null || $this->intercomRead == null) {
             $this->intercomInterlocutorPid = null;
             return false;
         }
         return true;
     }
     return false;
 }