Skip to content

Commit

Permalink
Optional non-libevent master mode.
Browse files Browse the repository at this point in the history
  • Loading branch information
kakserpom committed Jan 20, 2013
1 parent 1799813 commit 35918cf
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 19 deletions.
2 changes: 1 addition & 1 deletion lib/Daemon_IPCThread.php
Expand Up @@ -25,7 +25,7 @@ public function run() {
if (Daemon::$process instanceof Daemon_MasterThread) {
Daemon::$process->unregisterSignals();
}
event_reinit(Daemon::$process->eventBase);
//event_reinit(Daemon::$process->eventBase);
Daemon::$process = $this;
if (Daemon::$logpointerAsync) {
$oldfd = Daemon::$logpointerAsync->fd;
Expand Down
51 changes: 37 additions & 14 deletions lib/Daemon_MasterThread.php
Expand Up @@ -31,8 +31,13 @@ public function run() {
class_exists('Timer'); // ensure loading this class
gc_enable();

$this->eventBase = event_base_new();
$this->registerEventSignals();
//$this->eventBase = event_base_new();

if ($this->eventBase) {
$this->registerEventSignals();
} else {
$this->registerSignals();
}

$this->workers = new ThreadCollection;
$this->collections['workers'] = $this->workers;
Expand All @@ -48,7 +53,7 @@ class_exists('Timer'); // ensure loading this class
Daemon::$config->startworkers->value,
Daemon::$config->maxworkers->value
));
Timer::add(function($event) use (&$cbs) {
$this->timerCb = function($event) use (&$cbs) {
$self = Daemon::$process;

static $c = 0;
Expand Down Expand Up @@ -89,7 +94,9 @@ class_exists('Timer'); // ensure loading this class
if ($n > 0) {
Daemon::log('Spawning ' . $n . ' worker(s).');
$self->spawnWorkers($n);
event_base_loopbreak($self->eventBase);
if ($self->eventBase) {
event_base_loopbreak($self->eventBase);
}
}

$n = min(
Expand All @@ -103,16 +110,28 @@ class_exists('Timer'); // ensure loading this class
}
}
}


$event->timeout();
}, 1e6 * Daemon::$config->mpmdelay->value, 'MPM');

while (!$this->breakMainLoop) {
$this->callbacks->executeAll($this);
if ($this->eventBase) {
if ($event) {
$event->timeout();
}
};

if ($this->eventBase) {
Timer::add($this->timerCb, 1e6 * Daemon::$config->mpmdelay->value, 'MPM');
while (!$this->breakMainLoop) {
$this->callbacks->executeAll($this);
event_base_loop($this->eventBase);
}
} else {
$lastTimerCall = microtime(true);
while (!$this->breakMainLoop) {
$m = microtime(true);
$this->callbacks->executeAll($this);
if ($m > $lastTimerCall + Daemon::$config->mpmdelay->value) {
call_user_func($this->timerCb, null);
$lastTimerCall = $m;
}
$this->sigwait();
}
}
}

Expand Down Expand Up @@ -186,7 +205,9 @@ public function spawnWorkers($n) {

}
if ($n > 0) {
event_base_loopbreak($this->eventBase);
if ($this->eventBase) {
event_base_loopbreak($this->eventBase);
}
}
return true;
}
Expand All @@ -212,7 +233,9 @@ public function spawnIPCThread() {
exit;
}
});

if ($this->eventBase) {
event_base_loopbreak($this->eventBase);
}
return true;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/Daemon_WorkerThread.php
Expand Up @@ -40,7 +40,7 @@ public function run() {
if (Daemon::$process instanceof Daemon_MasterThread) {
Daemon::$process->unregisterSignals();
}
event_reinit(Daemon::$process->eventBase);
//event_reinit(Daemon::$process->eventBase);
Daemon::$process = $this;
if (Daemon::$logpointerAsync) {
$oldfd = Daemon::$logpointerAsync->fd;
Expand Down
5 changes: 4 additions & 1 deletion lib/IOStream.php
Expand Up @@ -75,6 +75,9 @@ public function setOnWrite($cb) {
public function setFd($fd) {
$this->fd = $fd;
$this->buffer = bufferevent_socket_new(Daemon::$process->eventBase, $this->fd, EVENT_BEV_OPT_CLOSE_ON_FREE | EVENT_BEV_OPT_DEFER_CALLBACKS);
if (!$this->buffer) {
return;
}
bufferevent_setcb($this->buffer, array($this, 'onReadEvent'), array($this, 'onWriteEvent'), array($this, 'onStateEvent'));
if ($this->priority !== null) {
bufferevent_priority_set($this->buffer, $this->priority);
Expand Down Expand Up @@ -285,7 +288,7 @@ public function stdin($buf) {}
* @return void
*/
public function close() {
if (isset($this->buffer)) {
if (is_resource($this->buffer)) {
bufferevent_free($this->buffer);
$this->buffer = null;
}
Expand Down
4 changes: 2 additions & 2 deletions lib/Thread.php
Expand Up @@ -350,9 +350,9 @@ protected function setproctitle($title) {
* @param int Nanoseconds
* @return void
*/
protected function sigwait($sec = 0, $nano = 1) {
protected function sigwait($sec = 0, $nano = 0.3e9) {
$siginfo = NULL;
$signo = pcntl_sigtimedwait(self::$signalsno, $siginfo, $sec, $nano);
$signo = @pcntl_sigtimedwait(self::$signalsno, $siginfo, $sec, $nano);

if (is_bool($signo)) {
return $signo;
Expand Down

0 comments on commit 35918cf

Please sign in to comment.