2015-08-08 05:45:41 +02:00
|
|
|
<?php
|
2015-08-29 08:40:10 +02:00
|
|
|
|
2016-08-18 18:04:48 +02:00
|
|
|
namespace Amp\Concurrent\Threading;
|
|
|
|
|
|
|
|
use Amp\Concurrent\Sync\Mutex as SyncMutex;
|
|
|
|
use Interop\Async\Awaitable;
|
2015-08-08 05:45:41 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A thread-safe, asynchronous mutex using the pthreads locking mechanism.
|
|
|
|
*
|
|
|
|
* Compatible with POSIX systems and Microsoft Windows.
|
|
|
|
*/
|
2016-08-18 18:04:48 +02:00
|
|
|
class Mutex implements SyncMutex {
|
2015-08-08 05:45:41 +02:00
|
|
|
/**
|
2016-08-18 18:04:48 +02:00
|
|
|
* @var \Amp\Concurrent\Threading\Internal\Mutex
|
2015-08-08 05:45:41 +02:00
|
|
|
*/
|
2015-08-10 05:30:11 +02:00
|
|
|
private $mutex;
|
2015-08-08 05:45:41 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new threaded mutex.
|
|
|
|
*/
|
2016-08-18 18:04:48 +02:00
|
|
|
public function __construct() {
|
2015-09-04 01:31:29 +02:00
|
|
|
$this->init();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initializes the mutex.
|
|
|
|
*/
|
2016-08-18 18:04:48 +02:00
|
|
|
private function init() {
|
2015-08-29 08:40:10 +02:00
|
|
|
$this->mutex = new Internal\Mutex();
|
2015-08-08 05:45:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2016-08-18 18:04:48 +02:00
|
|
|
public function acquire(): Awaitable {
|
2015-08-10 05:30:11 +02:00
|
|
|
return $this->mutex->acquire();
|
2015-08-08 05:45:41 +02:00
|
|
|
}
|
2015-09-04 01:31:29 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Makes a copy of the mutex in the unlocked state.
|
|
|
|
*/
|
2016-08-18 18:04:48 +02:00
|
|
|
public function __clone() {
|
2015-09-04 01:31:29 +02:00
|
|
|
$this->init();
|
|
|
|
}
|
2015-08-08 05:45:41 +02:00
|
|
|
}
|