multithreading.php
changeset 68 32f6e2ee15ab
parent 53 a6b339665650
child 76 487a16c7117c
equal deleted inserted replaced
67:c4aefad02ce4 68:32f6e2ee15ab
    35  * Global signal handler for SIGUSR2.
    35  * Global signal handler for SIGUSR2.
    36  */
    36  */
    37 
    37 
    38 function Threader_SigUsr2()
    38 function Threader_SigUsr2()
    39 {
    39 {
    40   global $threader_instances;
    40   global $threader_instances, $threader_notick;
       
    41   if ( @$threader_notick )
       
    42     return;
    41   foreach ( $threader_instances as &$mt )
    43   foreach ( $threader_instances as &$mt )
    42   {
    44   {
    43     if ( is_object($mt) )
    45     if ( is_object($mt) )
    44     {
    46     {
    45       $parchild = $mt->is_child() ? 'child' : 'parent';
       
    46       $mt->event_sigusr2();
    47       $mt->event_sigusr2();
    47     }
    48     }
    48   }
    49   }
    49 }
    50 }
    50 
    51 
   134     {
   135     {
   135       declare(ticks=1);
   136       declare(ticks=1);
   136       
   137       
   137       $threader_instances[] =& $this;
   138       $threader_instances[] =& $this;
   138       
   139       
       
   140       pcntl_signal(SIGCHLD, 'Threader_SigChld');
   139       pcntl_signal(SIGUSR2, 'Threader_SigUsr2');
   141       pcntl_signal(SIGUSR2, 'Threader_SigUsr2');
   140       pcntl_signal(SIGCHLD, 'Threader_SigChld');
       
   141     }
   142     }
   142     
   143     
   143     $this->json = new Services_JSON(SERVICES_JSON_LOOSE_TYPE);
   144     $this->json = new Services_JSON(SERVICES_JSON_LOOSE_TYPE);
   144     $this->parent_pid = getmypid();
   145     $this->parent_pid = getmypid();
   145   }
   146   }
   271   function event_sigusr2()
   272   function event_sigusr2()
   272   {
   273   {
   273     if ( $this->is_child() )
   274     if ( $this->is_child() )
   274     {
   275     {
   275       // this is easy - the parent sent the signal.
   276       // this is easy - the parent sent the signal.
       
   277       @stream_set_blocking($this->parent_sock, 0);
   276       $command = rtrim(fgets($this->parent_sock, 102400), "\n");
   278       $command = rtrim(fgets($this->parent_sock, 102400), "\n");
   277     }
   279     }
   278     else
   280     else
   279     {
   281     {
   280       // since we can't find which PID sent the signal, set the timeout to a very small amount
   282       // since we can't find which PID sent the signal, set the timeout to a very small amount
   281       // of time and try to read; if we get something, awesome.
   283       // of time and try to read; if we get something, awesome.
   282       foreach ( $this->ipc_sockets as $pid => $socket )
   284       foreach ( $this->ipc_sockets as $pid => $socket )
   283       {
   285       {
   284         // 1000 microseconds = 1/80th of the time it takes you to blink.
   286         @stream_set_blocking($socket, 0);
   285         @stream_set_timeout($socket, 0, 1000);
       
   286         $command = rtrim(@fgets($socket, 102400), "\n");
   287         $command = rtrim(@fgets($socket, 102400), "\n");
   287         if ( !empty($command) )
   288         if ( !empty($command) )
   288         {
   289         {
   289           break;
   290           break;
   290         }
   291         }