2015-08-28 00:18:50 -05:00
|
|
|
<?php
|
|
|
|
namespace Icicle\Concurrent\Worker;
|
|
|
|
|
|
|
|
if (!function_exists(__NAMESPACE__ . '\pool')) {
|
|
|
|
/**
|
2015-08-28 20:30:53 -05:00
|
|
|
* Returns the global worker pool for the current context.
|
2015-08-28 00:18:50 -05:00
|
|
|
*
|
2015-12-04 23:50:32 -06:00
|
|
|
* @param \Icicle\Concurrent\Worker\Pool|null $pool A worker pool instance.
|
2015-08-28 12:04:04 -05:00
|
|
|
*
|
2015-12-04 23:50:32 -06:00
|
|
|
* @return \Icicle\Concurrent\Worker\Pool The global worker pool instance.
|
2015-08-28 00:18:50 -05:00
|
|
|
*/
|
2016-01-23 00:00:56 -06:00
|
|
|
function pool(Pool $pool = null): Pool
|
2015-08-28 00:18:50 -05:00
|
|
|
{
|
|
|
|
static $instance;
|
|
|
|
|
2015-08-29 00:41:00 -05:00
|
|
|
if (null !== $pool) {
|
|
|
|
$instance = $pool;
|
|
|
|
} elseif (null === $instance) {
|
2015-12-04 23:50:32 -06:00
|
|
|
$instance = new DefaultPool();
|
2015-08-29 00:41:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!$instance->isRunning()) {
|
2015-08-28 20:30:53 -05:00
|
|
|
$instance->start();
|
2015-08-28 00:18:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return $instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-09-01 16:20:57 -05:00
|
|
|
* @coroutine
|
|
|
|
*
|
2016-01-11 09:32:06 -06:00
|
|
|
* Enqueues a task to be executed by the global worker pool.
|
2015-08-28 00:18:50 -05:00
|
|
|
*
|
2015-12-04 23:50:32 -06:00
|
|
|
* @param \Icicle\Concurrent\Worker\Task $task The task to enqueue.
|
2015-08-28 00:18:50 -05:00
|
|
|
*
|
2015-09-01 16:20:57 -05:00
|
|
|
* @return \Generator
|
2015-08-28 00:18:50 -05:00
|
|
|
*
|
|
|
|
* @resolve mixed The return value of the task.
|
|
|
|
*/
|
2016-01-23 00:00:56 -06:00
|
|
|
function enqueue(Task $task): \Generator
|
2015-08-28 00:18:50 -05:00
|
|
|
{
|
2016-01-23 00:00:56 -06:00
|
|
|
return pool()->enqueue($task);
|
2015-08-28 00:18:50 -05:00
|
|
|
}
|
2015-09-02 08:51:59 -05:00
|
|
|
|
|
|
|
/**
|
2016-01-11 09:32:06 -06:00
|
|
|
* Creates a worker using the global worker factory.
|
2015-09-02 08:51:59 -05:00
|
|
|
*
|
2015-12-04 23:50:32 -06:00
|
|
|
* @return \Icicle\Concurrent\Worker\Worker
|
2015-09-02 08:51:59 -05:00
|
|
|
*/
|
2016-01-23 00:00:56 -06:00
|
|
|
function create(): Worker
|
2016-01-11 09:32:06 -06:00
|
|
|
{
|
|
|
|
$worker = factory()->create();
|
|
|
|
$worker->start();
|
|
|
|
return $worker;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets or sets the global worker factory.
|
|
|
|
*
|
|
|
|
* @param \Icicle\Concurrent\Worker\WorkerFactory|null $factory
|
|
|
|
*
|
|
|
|
* @return \Icicle\Concurrent\Worker\WorkerFactory
|
|
|
|
*/
|
2016-01-23 00:00:56 -06:00
|
|
|
function factory(WorkerFactory $factory = null): WorkerFactory
|
2015-09-02 08:51:59 -05:00
|
|
|
{
|
|
|
|
static $instance;
|
|
|
|
|
|
|
|
if (null !== $factory) {
|
|
|
|
$instance = $factory;
|
|
|
|
} elseif (null === $instance) {
|
2015-12-04 23:50:32 -06:00
|
|
|
$instance = new DefaultWorkerFactory();
|
2015-09-02 08:51:59 -05:00
|
|
|
}
|
|
|
|
|
2016-01-14 18:08:39 -06:00
|
|
|
return $instance;
|
2016-01-11 09:32:06 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-01-14 18:54:53 -06:00
|
|
|
* Gets a worker from the global worker pool.
|
2016-01-11 09:32:06 -06:00
|
|
|
*
|
|
|
|
* @return \Icicle\Concurrent\Worker\Worker
|
|
|
|
*/
|
2016-01-23 00:00:56 -06:00
|
|
|
function get(): Worker
|
2016-01-11 09:32:06 -06:00
|
|
|
{
|
2016-01-14 18:54:53 -06:00
|
|
|
return pool()->get();
|
2016-01-11 09:32:06 -06:00
|
|
|
}
|
2015-08-28 00:18:50 -05:00
|
|
|
}
|