mirror of
https://github.com/danog/parallel.git
synced 2024-12-03 10:07:49 +01:00
45 lines
869 B
PHP
45 lines
869 B
PHP
<?php
|
|
|
|
namespace Amp\Parallel\Threading;
|
|
|
|
use Amp\Parallel\Sync\Mutex as SyncMutex;
|
|
use AsyncInterop\Promise;
|
|
|
|
/**
|
|
* A thread-safe, asynchronous mutex using the pthreads locking mechanism.
|
|
*
|
|
* Compatible with POSIX systems and Microsoft Windows.
|
|
*/
|
|
class Mutex implements SyncMutex {
|
|
/** @var \Amp\Parallel\Threading\Internal\Mutex */
|
|
private $mutex;
|
|
|
|
/**
|
|
* Creates a new threaded mutex.
|
|
*/
|
|
public function __construct() {
|
|
$this->init();
|
|
}
|
|
|
|
/**
|
|
* Initializes the mutex.
|
|
*/
|
|
private function init() {
|
|
$this->mutex = new Internal\Mutex;
|
|
}
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public function acquire(): Promise {
|
|
return $this->mutex->acquire();
|
|
}
|
|
|
|
/**
|
|
* Makes a copy of the mutex in the unlocked state.
|
|
*/
|
|
public function __clone() {
|
|
$this->init();
|
|
}
|
|
}
|