2016-12-30 02:16:04 +01:00
|
|
|
<?php
|
2016-08-18 18:04:48 +02:00
|
|
|
|
2016-08-23 23:47:40 +02:00
|
|
|
namespace Amp\Parallel\Sync;
|
2015-08-10 05:16:34 +02:00
|
|
|
|
2017-11-27 06:03:18 +01:00
|
|
|
use Amp\Promise;
|
|
|
|
|
2015-08-10 05:16:34 +02:00
|
|
|
/**
|
2015-12-05 06:50:32 +01:00
|
|
|
* A parcel object for sharing data across execution contexts.
|
2015-08-10 23:36:01 +02:00
|
|
|
*
|
2015-12-05 06:50:32 +01:00
|
|
|
* A parcel is an object that stores a value in a safe way that can be shared
|
|
|
|
* between different threads or processes. Different handles to the same parcel
|
|
|
|
* will access the same data, and a parcel handle itself is serializable and
|
|
|
|
* can be transported to other execution contexts.
|
2015-08-31 11:19:49 +02:00
|
|
|
*
|
2015-12-05 06:50:32 +01:00
|
|
|
* Wrapping and unwrapping values in the parcel are not atomic. To prevent race
|
|
|
|
* conditions and guarantee safety, you should use the provided synchronization
|
|
|
|
* methods to acquire a lock for exclusive access to the parcel first before
|
|
|
|
* accessing the contained value.
|
2015-08-10 05:16:34 +02:00
|
|
|
*/
|
2018-10-07 16:50:45 +02:00
|
|
|
interface Parcel
|
|
|
|
{
|
2017-11-27 06:03:18 +01:00
|
|
|
/**
|
|
|
|
* Asynchronously invokes a callback while maintaining an exclusive lock on the parcel. The current value of the
|
|
|
|
* parcel is provided as the first argument to the callback function.
|
|
|
|
*
|
2017-11-29 21:40:07 +01:00
|
|
|
* @param callable $callback The synchronized callback to invoke. The parcel value is given as the single argument
|
|
|
|
* to the callback function. The callback may be a regular function or a coroutine.
|
2017-11-27 06:03:18 +01:00
|
|
|
*
|
|
|
|
* @return \Amp\Promise<mixed> Resolves with the return value of $callback or fails if $callback
|
|
|
|
* throws an exception.
|
|
|
|
*/
|
|
|
|
public function synchronized(callable $callback): Promise;
|
|
|
|
|
2015-08-10 05:16:34 +02:00
|
|
|
/**
|
2017-11-29 21:40:07 +01:00
|
|
|
* @return \Amp\Promise<mixed> A promise for the value inside the parcel.
|
2015-08-10 23:36:01 +02:00
|
|
|
*/
|
2017-11-27 06:03:18 +01:00
|
|
|
public function unwrap(): Promise;
|
2015-08-10 05:16:34 +02:00
|
|
|
}
|