2016-12-30 02:16:04 +01:00
|
|
|
<?php
|
2015-08-29 08:40:10 +02:00
|
|
|
|
2016-08-23 23:47:40 +02:00
|
|
|
namespace Amp\Parallel\Threading;
|
2016-08-18 18:04:48 +02:00
|
|
|
|
2016-08-23 23:47:40 +02:00
|
|
|
use Amp\Parallel\Sync\Mutex as SyncMutex;
|
2016-11-15 00:43:44 +01:00
|
|
|
use Interop\Async\Promise;
|
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 {
|
2016-08-26 17:10:03 +02:00
|
|
|
/** @var \Amp\Parallel\Threading\Internal\Mutex */
|
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() {
|
2016-08-23 01:25:19 +02:00
|
|
|
$this->mutex = new Internal\Mutex;
|
2015-08-08 05:45:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2016-11-15 00:43:44 +01:00
|
|
|
public function acquire(): Promise {
|
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
|
|
|
}
|