1
0
mirror of https://github.com/danog/parallel.git synced 2024-11-27 12:54:55 +01:00
parallel/lib/Thread/Mutex.php

45 lines
854 B
PHP
Raw Normal View History

2016-12-30 02:16:04 +01:00
<?php
2017-11-10 16:59:47 +01:00
namespace Amp\Parallel\Thread;
2016-08-18 18:04:48 +02:00
2016-08-23 23:47:40 +02:00
use Amp\Parallel\Sync\Mutex as SyncMutex;
2017-05-18 09:51:31 +02:00
use Amp\Promise;
/**
* 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 {
2017-11-10 16:59:47 +01:00
/** @var \Amp\Parallel\Thread\Internal\Mutex */
private $mutex;
/**
* Creates a new threaded mutex.
*/
2016-08-18 18:04:48 +02:00
public function __construct() {
$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;
}
/**
* {@inheritdoc}
*/
2016-11-15 00:43:44 +01:00
public function acquire(): Promise {
return $this->mutex->acquire();
}
/**
* Makes a copy of the mutex in the unlocked state.
*/
2016-08-18 18:04:48 +02:00
public function __clone() {
$this->init();
}
}