forked from GithubBackups/vichan
7.4 compatibility
This commit is contained in:
parent
29ee5aeb1d
commit
f3f7c0c75c
38
inc/lock.php
38
inc/lock.php
@ -1,7 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
class Locks {
|
class Locks {
|
||||||
private static function filesystem(string $key): Lock|false {
|
private static function filesystem(string $key) {
|
||||||
$key = str_replace('/', '::', $key);
|
$key = str_replace('/', '::', $key);
|
||||||
$key = str_replace("\0", '', $key);
|
$key = str_replace("\0", '', $key);
|
||||||
|
|
||||||
@ -11,15 +10,14 @@ class Locks {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return new class($fd) implements Lock {
|
return new class($fd) implements Lock {
|
||||||
// Resources have no type in php.
|
// Resources have no type in PHP.
|
||||||
private mixed $f;
|
private $f;
|
||||||
|
|
||||||
|
public function __construct($fd) {
|
||||||
function __construct($fd) {
|
|
||||||
$this->f = $fd;
|
$this->f = $fd;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function get(bool $nonblock = false): Lock|false {
|
public function get(bool $nonblock = false) {
|
||||||
$wouldblock = false;
|
$wouldblock = false;
|
||||||
flock($this->f, LOCK_SH | ($nonblock ? LOCK_NB : 0), $wouldblock);
|
flock($this->f, LOCK_SH | ($nonblock ? LOCK_NB : 0), $wouldblock);
|
||||||
if ($nonblock && $wouldblock) {
|
if ($nonblock && $wouldblock) {
|
||||||
@ -28,7 +26,7 @@ class Locks {
|
|||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function get_ex(bool $nonblock = false): Lock|false {
|
public function get_ex(bool $nonblock = false) {
|
||||||
$wouldblock = false;
|
$wouldblock = false;
|
||||||
flock($this->f, LOCK_EX | ($nonblock ? LOCK_NB : 0), $wouldblock);
|
flock($this->f, LOCK_EX | ($nonblock ? LOCK_NB : 0), $wouldblock);
|
||||||
if ($nonblock && $wouldblock) {
|
if ($nonblock && $wouldblock) {
|
||||||
@ -37,33 +35,30 @@ class Locks {
|
|||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function free(): Lock {
|
public function free() {
|
||||||
flock($this->f, LOCK_UN);
|
flock($this->f, LOCK_UN);
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public static function none() {
|
||||||
* No-op. Can be used for mocking.
|
|
||||||
*/
|
|
||||||
public static function none(): Lock|false {
|
|
||||||
return new class() implements Lock {
|
return new class() implements Lock {
|
||||||
public function get(bool $nonblock = false): Lock|false {
|
public function get(bool $nonblock = false) {
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function get_ex(bool $nonblock = false): Lock|false {
|
public function get_ex(bool $nonblock = false) {
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function free(): Lock {
|
public function free() {
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function get_lock(array $config, string $key): Lock|false {
|
public static function get_lock(array $config, string $key) {
|
||||||
if ($config['lock']['enabled'] == 'fs') {
|
if ($config['lock']['enabled'] == 'fs') {
|
||||||
return self::filesystem($key);
|
return self::filesystem($key);
|
||||||
} else {
|
} else {
|
||||||
@ -73,12 +68,9 @@ class Locks {
|
|||||||
}
|
}
|
||||||
|
|
||||||
interface Lock {
|
interface Lock {
|
||||||
// Get a shared lock
|
public function get(bool $nonblock = false);
|
||||||
public function get(bool $nonblock = false): Lock|false;
|
|
||||||
|
|
||||||
// Get an exclusive lock
|
public function get_ex(bool $nonblock = false);
|
||||||
public function get_ex(bool $nonblock = false): Lock|false;
|
|
||||||
|
|
||||||
// Free a lock
|
public function free();
|
||||||
public function free(): Lock;
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user