The version of vichan running on lainchan.org
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

40 lignes
1.0KB

  1. <?php
  2. class Lock {
  3. function __construct($key) { global $config;
  4. if ($config['lock']['enabled'] == 'fs') {
  5. $key = str_replace('/', '::', $key);
  6. $key = str_replace("\0", '', $key);
  7. $this->f = fopen("tmp/locks/$key", "w");
  8. }
  9. }
  10. // Get a shared lock
  11. function get($nonblock = false) { global $config;
  12. if ($config['lock']['enabled'] == 'fs') {
  13. $wouldblock = false;
  14. flock($this->f, LOCK_SH | ($nonblock ? LOCK_NB : 0), $wouldblock);
  15. if ($nonblock && $wouldblock) return false;
  16. }
  17. return $this;
  18. }
  19. // Get an exclusive lock
  20. function get_ex($nonblock = false) { global $config;
  21. if ($config['lock']['enabled'] == 'fs') {
  22. $wouldblock = false;
  23. flock($this->f, LOCK_EX | ($nonblock ? LOCK_NB : 0), $wouldblock);
  24. if ($nonblock && $wouldblock) return false;
  25. }
  26. return $this;
  27. }
  28. // Free a lock
  29. function free() { global $config;
  30. if ($config['lock']['enabled'] == 'fs') {
  31. flock($this->f, LOCK_UN);
  32. }
  33. return $this;
  34. }
  35. }