1
0
mirror of https://github.com/danog/parallel.git synced 2024-12-03 18:17:52 +01:00
parallel/src/Sync/FileMutex.php

72 lines
2.0 KiB
PHP
Raw Normal View History

2015-08-02 04:07:33 +02:00
<?php
namespace Icicle\Concurrent\Sync;
use Icicle\Concurrent\Exception\MutexException;
use Icicle\Coroutine;
2015-08-02 04:07:33 +02:00
/**
* A cross-platform mutex that uses exclusive files as the lock mechanism.
2015-08-02 04:07:33 +02:00
*
* This mutex implementation is not always atomic and depends on the operating
* system's implementation of file creation operations. Use this implementation
2015-08-02 04:07:33 +02:00
* only if no other mutex types are available.
*
2015-10-17 06:32:01 +02:00
* This implementation avoids using [flock()](http://php.net/flock)
* because flock() is known to have some atomicity issues on some systems. In
* addition, flock() does not work as expected when trying to lock a file
* multiple times in the same process on Linux. Instead, exclusive file creation
* is used to create a lock file, which is atomic on most systems.
*
2015-10-17 06:32:01 +02:00
* @see http://php.net/fopen
2015-08-02 04:07:33 +02:00
*/
class FileMutex implements MutexInterface
{
const LATENCY_TIMEOUT = 0.01; // 10 ms
2015-08-02 04:07:33 +02:00
/**
* @var string The full path to the lock file.
*/
private $fileName;
/**
* Creates a new mutex.
*/
public function __construct()
{
$this->fileName = tempnam(sys_get_temp_dir(), 'mutex-') . '.lock';
2015-08-02 04:07:33 +02:00
}
/**
* {@inheritdoc}
*/
2015-08-08 06:12:21 +02:00
public function acquire()
2015-08-02 04:07:33 +02:00
{
// Try to create the lock file. If the file already exists, someone else
// has the lock, so set an asynchronous timer and try again.
while (($handle = @fopen($this->fileName, 'x')) === false) {
2015-08-08 06:12:21 +02:00
yield Coroutine\sleep(self::LATENCY_TIMEOUT);
2015-08-02 04:07:33 +02:00
}
2015-08-08 06:12:21 +02:00
// Return a lock object that can be used to release the lock on the mutex.
yield new Lock(function (Lock $lock) {
$this->release();
});
fclose($handle);
}
2015-08-02 04:07:33 +02:00
/**
2015-08-31 00:52:00 +02:00
* Releases the lock on the mutex.
*
* @throws MutexException If the unlock operation failed.
2015-08-02 04:07:33 +02:00
*/
2015-08-08 06:12:21 +02:00
protected function release()
2015-08-02 04:07:33 +02:00
{
$success = @unlink($this->fileName);
2015-08-02 04:07:33 +02:00
if (!$success) {
2015-08-08 06:12:21 +02:00
throw new MutexException('Failed to unlock the mutex file.');
2015-08-02 04:07:33 +02:00
}
}
}