1
0
mirror of https://github.com/danog/parallel.git synced 2024-11-27 04:44:56 +01:00

Refactor ContextInterface and add ProcessInterface

This commit is contained in:
Aaron Piotrowski 2015-10-20 00:06:43 -05:00
parent 80d5c4cd08
commit c25608c893
6 changed files with 57 additions and 8 deletions

View File

@ -1,7 +1,7 @@
<?php
namespace Icicle\Concurrent;
interface ContextInterface extends Sync\ChannelInterface
interface ContextInterface
{
/**
* @return bool

View File

@ -1,11 +1,11 @@
<?php
namespace Icicle\Concurrent\Forking;
use Icicle\Concurrent\ContextInterface;
use Icicle\Concurrent\Exception\ForkException;
use Icicle\Concurrent\Exception\InvalidArgumentError;
use Icicle\Concurrent\Exception\StatusError;
use Icicle\Concurrent\Exception\SynchronizationError;
use Icicle\Concurrent\ProcessInterface;
use Icicle\Concurrent\Sync\Channel;
use Icicle\Concurrent\Sync\ChannelInterface;
use Icicle\Concurrent\Sync\Internal\ExitFailure;
@ -19,7 +19,7 @@ use Icicle\Stream\Pipe\DuplexPipe;
/**
* Implements a UNIX-compatible context using forked processes.
*/
class Fork implements ContextInterface
class Fork implements ChannelInterface, ProcessInterface
{
/**
* @var \Icicle\Concurrent\Sync\Channel A channel for communicating with the child.
@ -255,6 +255,20 @@ class Fork implements ContextInterface
$this->channel = null;
}
/**
* @param int $signo
*
* @throws \Icicle\Concurrent\Exception\StatusError
*/
public function signal($signo)
{
if (0 === $this->pid) {
throw new StatusError('The fork has not been started or has already finished.');
}
posix_kill($this->pid, (int) $signo);
}
/**
* @coroutine
*

View File

@ -1,14 +1,15 @@
<?php
namespace Icicle\Concurrent\Process;
use Icicle\Concurrent\ContextInterface;
use Icicle\Concurrent\Exception\InvalidArgumentError;
use Icicle\Concurrent\Exception\StatusError;
use Icicle\Concurrent\Exception\SynchronizationError;
use Icicle\Concurrent\ProcessInterface;
use Icicle\Concurrent\Sync\Channel;
use Icicle\Concurrent\Sync\ChannelInterface;
use Icicle\Concurrent\Sync\Internal\ExitStatusInterface;
class ChannelledProcess implements ContextInterface
class ChannelledProcess implements ChannelInterface, ProcessInterface
{
/**
* @var \Icicle\Concurrent\Process\Process
@ -125,4 +126,20 @@ class ChannelledProcess implements ContextInterface
$this->process->kill();
$this->channel = null;
}
/**
* {@inheritdoc}
*/
public function getPid()
{
return $this->process->getPid();
}
/**
* {@inheritdoc}
*/
public function signal($signo)
{
$this->process->signal($signo);
}
}

View File

@ -1,15 +1,15 @@
<?php
namespace Icicle\Concurrent\Process;
use Exception;
use Icicle\Concurrent\Exception\ProcessException;
use Icicle\Concurrent\Exception\StatusError;
use Icicle\Concurrent\ProcessInterface;
use Icicle\Loop;
use Icicle\Promise\Promise;
use Icicle\Stream\Pipe\ReadablePipe;
use Icicle\Stream\Pipe\WritablePipe;
class Process
class Process implements ProcessInterface
{
/**
* @var resource|null

17
src/ProcessInterface.php Normal file
View File

@ -0,0 +1,17 @@
<?php
namespace Icicle\Concurrent;
interface ProcessInterface extends ContextInterface
{
/**
* @return int PID of process.
*/
public function getPid();
/**
* @param int $signo
*
* @throws \Icicle\Concurrent\Exception\StatusError
*/
public function signal($signo);
}

View File

@ -7,6 +7,7 @@ use Icicle\Concurrent\Exception\StatusError;
use Icicle\Concurrent\Exception\SynchronizationError;
use Icicle\Concurrent\Exception\ThreadException;
use Icicle\Concurrent\Sync\Channel;
use Icicle\Concurrent\Sync\ChannelInterface;
use Icicle\Concurrent\Sync\Internal\ExitStatusInterface;
use Icicle\Coroutine;
use Icicle\Stream;
@ -19,7 +20,7 @@ use Icicle\Stream\Pipe\DuplexPipe;
* maintained both in the context that creates the thread and in the thread
* itself.
*/
class Thread implements ContextInterface
class Thread implements ChannelInterface, ContextInterface
{
const LATENCY_TIMEOUT = 0.01; // 10 ms