1
0
mirror of https://github.com/vichan-devel/vichan.git synced 2025-02-02 04:47:57 +01:00
vichan/inc/lock.php

44 lines
969 B
PHP
Raw Normal View History

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