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

45 lines
870 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;
2016-11-15 00:43:44 +01:00
use Interop\Async\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();
}
}