1
0
mirror of https://github.com/danog/parallel.git synced 2024-12-04 18:47:50 +01:00
parallel/lib/Threading/Mutex.php

45 lines
860 B
PHP
Raw Normal View History

2016-12-30 02:16:04 +01:00
<?php
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;
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 {
2016-08-26 17:10:03 +02:00
/** @var \Amp\Parallel\Threading\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();
}
}