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-28 19:04:04 +02:00
|
|
|
* If the pool has not been initialized, a minimum and maximum size can be given to create the pool with.
|
|
|
|
*
|
|
|
|
* @param int|null $minSize The minimum number of workers the pool should spawn.
|
|
|
|
* @param int|null $maxSize The maximum number of workers the pool should spawn.
|
|
|
|
* @param WorkerFactoryInterface|null $factory A worker factory to be used to create new workers.
|
2015-08-28 07:18:50 +02:00
|
|
|
*
|
2015-08-29 03:30:53 +02:00
|
|
|
* @return Pool The global worker pool instance.
|
2015-08-28 07:18:50 +02:00
|
|
|
*/
|
2015-08-28 19:04:04 +02:00
|
|
|
function pool($minSize = null, $maxSize = null, WorkerFactoryInterface $factory = null)
|
2015-08-28 07:18:50 +02:00
|
|
|
{
|
|
|
|
static $instance;
|
|
|
|
|
2015-08-28 19:04:04 +02:00
|
|
|
if (null === $instance) {
|
2015-08-29 03:30:53 +02:00
|
|
|
$instance = new Pool($minSize, $maxSize, $factory);
|
|
|
|
$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.
|
|
|
|
*/
|
|
|
|
function enqueue(TaskInterface $task)
|
|
|
|
{
|
2015-08-29 03:30:53 +02:00
|
|
|
return new Coroutine(pool()->enqueue($task));
|
2015-08-28 07:18:50 +02:00
|
|
|
}
|
|
|
|
}
|