Format lock.php

This commit is contained in:
Zankaria 2024-02-16 14:18:55 +01:00
parent c3de90075e
commit 00b05099f3

View File

@ -1,39 +1,43 @@
<?php <?php
class Lock { class Lock {
function __construct($key) { global $config; function __construct($key) {
if ($config['lock']['enabled'] == 'fs') { global $config;
$key = str_replace('/', '::', $key); if ($config['lock']['enabled'] == 'fs') {
$key = str_replace("\0", '', $key); $key = str_replace('/', '::', $key);
$key = str_replace("\0", '', $key);
$this->f = fopen("tmp/locks/$key", "w"); $this->f = fopen("tmp/locks/$key", "w");
} }
} }
// Get a shared lock // Get a shared lock
function get($nonblock = false) { global $config; function get($nonblock = false) {
if ($config['lock']['enabled'] == 'fs') { global $config;
$wouldblock = false; if ($config['lock']['enabled'] == 'fs') {
flock($this->f, LOCK_SH | ($nonblock ? LOCK_NB : 0), $wouldblock); $wouldblock = false;
if ($nonblock && $wouldblock) return false; flock($this->f, LOCK_SH | ($nonblock ? LOCK_NB : 0), $wouldblock);
} if ($nonblock && $wouldblock) return false;
return $this; }
} return $this;
}
// Get an exclusive lock // Get an exclusive lock
function get_ex($nonblock = false) { global $config; function get_ex($nonblock = false) {
if ($config['lock']['enabled'] == 'fs') { global $config;
$wouldblock = false; if ($config['lock']['enabled'] == 'fs') {
flock($this->f, LOCK_EX | ($nonblock ? LOCK_NB : 0), $wouldblock); $wouldblock = false;
if ($nonblock && $wouldblock) return false; flock($this->f, LOCK_EX | ($nonblock ? LOCK_NB : 0), $wouldblock);
} if ($nonblock && $wouldblock) return false;
return $this; }
} return $this;
}
// Free a lock // Free a lock
function free() { global $config; function free() {
if ($config['lock']['enabled'] == 'fs') { global $config;
flock($this->f, LOCK_UN); if ($config['lock']['enabled'] == 'fs') {
} flock($this->f, LOCK_UN);
return $this; }
} return $this;
}
} }