multithreading.php
author Dan
Tue, 23 Dec 2008 20:37:36 -0500
changeset 66 f30ffbf92cdd
parent 53 a6b339665650
child 68 32f6e2ee15ab
permissions -rw-r--r--
Release: 0.1 alpha 4
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
48
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
     1
<?php
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
     2
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
     3
/**
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
     4
 * Multi-threading (well sort of) tools
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
     5
 * 
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
     6
 * Greyhound - real web management for Amarok
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
     7
 * Copyright (C) 2008 Dan Fuhry
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
     8
 *
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
     9
 * This program is Free Software; you can redistribute and/or modify it under the terms of the GNU General Public License
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
    10
 * as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
    11
 *
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
    12
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
    13
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for details.
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
    14
 */
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
    15
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
    16
require_once('json.php');
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
    17
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
    18
/**
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
    19
 * Global signal handler for SIGCHLD.
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
    20
 */
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
    21
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
    22
function Threader_SigChld()
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
    23
{
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
    24
  global $threader_instances;
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
    25
  foreach ( $threader_instances as &$mt )
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
    26
  {
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
    27
    if ( is_object($mt) )
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
    28
    {
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
    29
      $mt->event_sigchld();
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
    30
    }
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
    31
  }
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
    32
}
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
    33
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
    34
/**
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
    35
 * Global signal handler for SIGUSR2.
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
    36
 */
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
    37
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
    38
function Threader_SigUsr2()
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
    39
{
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
    40
  global $threader_instances;
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
    41
  foreach ( $threader_instances as &$mt )
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
    42
  {
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
    43
    if ( is_object($mt) )
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
    44
    {
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
    45
      $parchild = $mt->is_child() ? 'child' : 'parent';
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
    46
      $mt->event_sigusr2();
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
    47
    }
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
    48
  }
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
    49
}
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
    50
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
    51
/**
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
    52
 * List of Threader instances. Needed for global handling of signals.
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
    53
 * @var array
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
    54
 */
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
    55
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
    56
global $threader_instances;
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
    57
$threader_instances = array();
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
    58
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
    59
/**
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
    60
 * Tools for emulating multi-threaded operation in PHP scripts.
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
    61
 * @package Amarok
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
    62
 * @subpackage WebControl
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
    63
 * @author Dan Fuhry
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
    64
 * @license GNU General Public License <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
    65
 */
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
    66
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
    67
class Threader
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
    68
{
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
    69
  
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
    70
  /**
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
    71
   * Return value of fork() if the process is a child.
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
    72
   * @const int
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
    73
   */
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
    74
  
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
    75
  const FORK_CHILD = -1;
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
    76
  
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
    77
  /**
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
    78
   * Set to true if this is a child process. No exceptions.
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
    79
   * @var bool
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
    80
   * @access private
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
    81
   */
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
    82
  
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
    83
  private $is_child = false;
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
    84
  
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
    85
  /**
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
    86
   * Sockets for inter-process communication.
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
    87
   * @var array
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
    88
   * @access private
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
    89
   */
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
    90
  
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
    91
  protected $ipc_sockets = array();
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
    92
  
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
    93
  /**
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
    94
   * Socket for communication with the parent. Obviously only used after calling fork().
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
    95
   * @var resource
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
    96
   * @access private
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
    97
   */
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
    98
  
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
    99
  protected $parent_sock = false;
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   100
  
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   101
  /**
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   102
   * Services_JSON instance.
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   103
   * @var object
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   104
   * @access private
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   105
   */
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   106
  
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   107
  protected $json = false;
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   108
  
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   109
  /**
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   110
   * PID of the parent process.
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   111
   * @var int
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   112
   * @access private
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   113
   */
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   114
  
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   115
  protected $parent_pid = 1;
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   116
  
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   117
  /**
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   118
   * List of actions for IPC events.
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   119
   * @var array
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   120
   * @access private
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   121
   */
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   122
  
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   123
  protected $ipc_actions = array();
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   124
  
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   125
  /**
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   126
   * Constructor. Sets up signal handlers. Nothing to see here, move along.
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   127
   */
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   128
  
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   129
  public function __construct()
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   130
  {
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   131
    global $threader_instances;
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   132
    
53
a6b339665650 Fixed broken FORK_CHILD usage.
Dan
parents: 48
diff changeset
   133
    if ( function_exists('pcntl_signal') )
a6b339665650 Fixed broken FORK_CHILD usage.
Dan
parents: 48
diff changeset
   134
    {
a6b339665650 Fixed broken FORK_CHILD usage.
Dan
parents: 48
diff changeset
   135
      declare(ticks=1);
a6b339665650 Fixed broken FORK_CHILD usage.
Dan
parents: 48
diff changeset
   136
      
a6b339665650 Fixed broken FORK_CHILD usage.
Dan
parents: 48
diff changeset
   137
      $threader_instances[] =& $this;
a6b339665650 Fixed broken FORK_CHILD usage.
Dan
parents: 48
diff changeset
   138
      
a6b339665650 Fixed broken FORK_CHILD usage.
Dan
parents: 48
diff changeset
   139
      pcntl_signal(SIGUSR2, 'Threader_SigUsr2');
a6b339665650 Fixed broken FORK_CHILD usage.
Dan
parents: 48
diff changeset
   140
      pcntl_signal(SIGCHLD, 'Threader_SigChld');
a6b339665650 Fixed broken FORK_CHILD usage.
Dan
parents: 48
diff changeset
   141
    }
48
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   142
    
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   143
    $this->json = new Services_JSON(SERVICES_JSON_LOOSE_TYPE);
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   144
    $this->parent_pid = getmypid();
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   145
  }
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   146
  
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   147
  /**
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   148
   * Forks the current process. See your system's fork(2) man page for details.
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   149
   * @return int FORK_CHILD if child process, PID of child if parent process. Returns false on failure.
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   150
   */
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   151
  
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   152
  public function fork()
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   153
  {
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   154
    // create our new sockets for IPC
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   155
    $socket_pair = stream_socket_pair(STREAM_PF_UNIX, STREAM_SOCK_STREAM, STREAM_IPPROTO_IP);
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   156
    // fork (emoticon of the day: --<E)
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   157
    $fork_result = pcntl_fork();
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   158
    if ( $fork_result == -1 )
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   159
    {
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   160
      // fork failed.
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   161
      return false;
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   162
    }
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   163
    else if ( $fork_result )
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   164
    {
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   165
      // we are the parent - register the child
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   166
      fclose($socket_pair[0]);
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   167
      $this->ipc_sockets[$fork_result] = $socket_pair[1];
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   168
      return $fork_result;
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   169
    }
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   170
    else
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   171
    {
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   172
      // we are the child.
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   173
      fclose($socket_pair[1]);
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   174
      $this->parent_sock = $socket_pair[0];
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   175
      $this->is_child = true;
53
a6b339665650 Fixed broken FORK_CHILD usage.
Dan
parents: 48
diff changeset
   176
      return self::FORK_CHILD;
48
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   177
    }
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   178
  }
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   179
  
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   180
  /**
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   181
   * Are we a child?
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   182
   * @return bool
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   183
   */
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   184
  
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   185
  public function is_child()
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   186
  {
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   187
    return $this->is_child;
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   188
  }
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   189
  
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   190
  /**
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   191
   * Register an action so that when it is fired over IPC, a custom function can be called.
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   192
   * @param string Action
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   193
   * @param callback Function to call
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   194
   * @return true on success, false on failure
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   195
   */
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   196
  
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   197
  function ipc_register($action, $callback)
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   198
  {
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   199
    if ( !is_string($action) || empty($action) || !is_callable($callback) )
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   200
    {
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   201
      return false;
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   202
    }
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   203
    $this->ipc_actions[$action] = $callback;
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   204
    return true;
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   205
  }
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   206
  
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   207
  /**
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   208
   * Send through an IPC event. If this is a child, it only notifies the parent; if we're the parent, all children are notified. 
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   209
   * @param array Data to be sent through. This must be an associative array containing an "action" key at minimum. If this a key "propagate" set to true, a parent that receives this will propagate the message to all children.
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   210
   * @return null
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   211
   */
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   212
  
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   213
  function ipc_send($data)
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   214
  {
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   215
    if ( !isset($data['action']) )
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   216
    {
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   217
      return false;
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   218
    }
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   219
    $data = $this->json->encode($data);
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   220
    if ( $this->is_child() )
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   221
    {
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   222
      fwrite($this->parent_sock, "$data\n");
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   223
      // signal the parent that we've got an event
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   224
      posix_kill($this->parent_pid, SIGUSR2);
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   225
    }
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   226
    else
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   227
    {
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   228
      // signal each child
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   229
      foreach ( $this->ipc_sockets as $pid => $socket )
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   230
      {
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   231
        fwrite($socket, "$data\n");
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   232
        posix_kill($pid, SIGUSR2);
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   233
      }
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   234
    }
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   235
    return null;
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   236
  }
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   237
  
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   238
  /**
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   239
   * Handler for SIGCHLD events.
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   240
   * @access private
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   241
   */
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   242
  
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   243
  function event_sigchld()
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   244
  {
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   245
    // this should never happen to children.
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   246
    if ( $this->is_child() )
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   247
    {
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   248
      return null;
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   249
    }
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   250
    
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   251
    // wait for child to exit.
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   252
    pcntl_wait($status);
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   253
    // for each child PID, kill with signal 0 (effectively, test if process is alive)
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   254
    // if posix_kill fails, it's dead so remove it from the list.
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   255
    foreach ( $this->ipc_sockets as $pid => $socket )
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   256
    {
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   257
      if ( !@posix_kill($pid, 0) )
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   258
      {
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   259
        // signal failed.
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   260
        fclose($socket);
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   261
        unset($this->ipc_sockets[$pid]);
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   262
      }
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   263
    }
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   264
  }
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   265
  
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   266
  /**
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   267
   * Handler for IPC events.
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   268
   * @access private
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   269
   */
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   270
  
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   271
  function event_sigusr2()
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   272
  {
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   273
    if ( $this->is_child() )
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   274
    {
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   275
      // this is easy - the parent sent the signal.
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   276
      $command = rtrim(fgets($this->parent_sock, 102400), "\n");
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   277
    }
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   278
    else
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   279
    {
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   280
      // since we can't find which PID sent the signal, set the timeout to a very small amount
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   281
      // of time and try to read; if we get something, awesome.
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   282
      foreach ( $this->ipc_sockets as $pid => $socket )
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   283
      {
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   284
        // 1000 microseconds = 1/80th of the time it takes you to blink.
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   285
        @stream_set_timeout($socket, 0, 1000);
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   286
        $command = rtrim(@fgets($socket, 102400), "\n");
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   287
        if ( !empty($command) )
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   288
        {
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   289
          break;
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   290
        }
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   291
      }
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   292
    }
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   293
    if ( empty($command) )
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   294
    {
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   295
      // hmm, got a sigusr2 without an incoming command. oh well, ignore.
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   296
      return null;
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   297
    }
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   298
    $command = $this->json->decode($command);
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   299
    if ( !isset($command['action']) )
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   300
    {
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   301
      // no action = no way to figure out how to handle this.
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   302
      return null;
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   303
    }
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   304
    if ( !isset($this->ipc_actions[$command['action']]) )
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   305
    {
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   306
      // action not registered
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   307
      return null;
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   308
    }
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   309
    // should we propagate this event?
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   310
    if ( !$this->is_child() && isset($command['propagate']) && $command['propagate'] === true )
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   311
    {
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   312
      $this->ipc_send($command);
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   313
    }
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   314
    // we're good
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   315
    @call_user_func($this->ipc_actions[$command['action']], $command, $this);
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   316
  }
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   317
  
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   318
  /**
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   319
   * Kills all child processes.
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   320
   * @access public
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   321
   */
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   322
  
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   323
  public function kill_all_children()
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   324
  {
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   325
    foreach ( $this->ipc_sockets as $pid => $socket )
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   326
    {
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   327
      $socklen = count($this->ipc_sockets);
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   328
      posix_kill($pid, SIGTERM);
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   329
      // wait until we are conscious of this child's death
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   330
      while ( count($this->ipc_sockets) >= $socklen )
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   331
      {
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   332
        usleep(20000);
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   333
      }
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   334
    }
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   335
  }
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   336
  
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   337
}
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   338
d643bfb862d8 Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff changeset
   339
?>