2015-08-28 07:18:50 +02:00
|
|
|
<?php
|
|
|
|
namespace Icicle\Concurrent\Worker;
|
|
|
|
|
2015-08-29 03:30:53 +02:00
|
|
|
use Icicle\Coroutine\Coroutine;
|
|
|
|
|
2015-08-28 07:18:50 +02:00
|
|
|
if (!function_exists(__NAMESPACE__ . '\pool')) {
|
|
|
|
/**
|
2015-08-29 03:30:53 +02:00
|
|
|
* Returns the global worker pool for the current context.
|
2015-08-28 07:18:50 +02:00
|
|
|
*
|
2015-08-29 07:41:00 +02:00
|
|
|
* @param PoolInterface|null $pool A worker pool instance.
|
2015-08-28 19:04:04 +02:00
|
|
|
*
|
2015-08-29 07:41:00 +02:00
|
|
|
* @return PoolInterface The global worker pool instance.
|
2015-08-28 07:18:50 +02:00
|
|
|
*/
|
2015-08-29 07:41:00 +02:00
|
|
|
function pool(PoolInterface $pool = null)
|
2015-08-28 07:18:50 +02:00
|
|
|
{
|
|
|
|
static $instance;
|
|
|
|
|
2015-08-29 07:41:00 +02:00
|
|
|
if (null !== $pool) {
|
|
|
|
$instance = $pool;
|
|
|
|
} elseif (null === $instance) {
|
|
|
|
$instance = new Pool();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$instance->isRunning()) {
|
2015-08-29 03:30:53 +02:00
|
|
|
$instance->start();
|
2015-08-28 07:18:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enqueues a task to be executed by the worker pool.
|
|
|
|
*
|
|
|
|
* @param TaskInterface $task The task to enqueue.
|
|
|
|
*
|
2015-08-29 03:30:53 +02:00
|
|
|
* @return \Icicle\Promise\PromiseInterface
|
2015-08-28 07:18:50 +02:00
|
|
|
*
|
|
|
|
* @resolve mixed The return value of the task.
|
|
|
|
*/
|
2015-08-29 07:41:00 +02:00
|
|
|
function enqueue(TaskInterface $task /* , ...$args */)
|
2015-08-28 07:18:50 +02:00
|
|
|
{
|
2015-08-29 07:41:00 +02:00
|
|
|
return new Coroutine(call_user_func_array([pool(), 'enqueue'], func_get_args()));
|
2015-08-28 07:18:50 +02:00
|
|
|
}
|
|
|
|
}
|