2016-08-22 06:40:48 +02:00
|
|
|
<?php declare(strict_types = 1);
|
2016-08-18 18:04:48 +02:00
|
|
|
|
|
|
|
namespace Amp\Concurrent\Worker;
|
|
|
|
|
|
|
|
use Interop\Async\Awaitable;
|
2015-08-27 16:10:08 +02:00
|
|
|
|
2015-12-05 06:50:32 +01:00
|
|
|
/**
|
|
|
|
* An interface for a parallel worker thread that runs a queue of tasks.
|
|
|
|
*/
|
2016-08-18 18:04:48 +02:00
|
|
|
interface Worker {
|
2015-08-27 16:10:08 +02:00
|
|
|
/**
|
2015-12-05 06:50:32 +01:00
|
|
|
* Checks if the worker is running.
|
|
|
|
*
|
|
|
|
* @return bool True if the worker is running, otherwise false.
|
2015-08-27 16:10:08 +02:00
|
|
|
*/
|
2016-01-23 07:00:56 +01:00
|
|
|
public function isRunning(): bool;
|
2015-08-27 16:10:08 +02:00
|
|
|
|
|
|
|
/**
|
2015-12-05 06:50:32 +01:00
|
|
|
* Checks if the worker is currently idle.
|
|
|
|
*
|
|
|
|
* @return bool
|
2015-08-27 16:10:08 +02:00
|
|
|
*/
|
2016-01-23 07:00:56 +01:00
|
|
|
public function isIdle(): bool;
|
2015-08-27 16:10:08 +02:00
|
|
|
|
|
|
|
/**
|
2015-12-05 06:50:32 +01:00
|
|
|
* Starts the context execution.
|
2015-08-27 16:10:08 +02:00
|
|
|
*/
|
2015-12-05 06:50:32 +01:00
|
|
|
public function start();
|
2015-08-27 16:10:08 +02:00
|
|
|
|
|
|
|
/**
|
2015-12-05 06:50:32 +01:00
|
|
|
* Enqueues a task to be executed by the worker.
|
|
|
|
*
|
|
|
|
* @param Task $task The task to enqueue.
|
|
|
|
*
|
2016-08-18 18:04:48 +02:00
|
|
|
* @return \Interop\Async\Awaitable<mixed> Resolves with the return value of Task::run().
|
2015-08-27 16:10:08 +02:00
|
|
|
*/
|
2016-08-18 18:04:48 +02:00
|
|
|
public function enqueue(Task $task): Awaitable;
|
2015-08-27 16:10:08 +02:00
|
|
|
|
|
|
|
/**
|
2016-08-18 18:04:48 +02:00
|
|
|
* @return \Interop\Async\Awaitable<int> Exit code.
|
2015-08-27 16:10:08 +02:00
|
|
|
*/
|
2016-08-18 18:04:48 +02:00
|
|
|
public function shutdown(): Awaitable;
|
2015-08-29 03:30:53 +02:00
|
|
|
|
|
|
|
/**
|
2015-12-05 06:50:32 +01:00
|
|
|
* Immediately kills the context.
|
2015-08-29 03:30:53 +02:00
|
|
|
*/
|
2015-12-05 06:50:32 +01:00
|
|
|
public function kill();
|
2015-08-29 03:30:53 +02:00
|
|
|
}
|