<?php
namespace Claromentis\Core\Lock;

$GLOBALS["__65da1b1ec2d6653e8a0ab7848" . "e536215"] = (@$GLOBALS["__65da1b1ec2d" . "6653e8a0ab784" . "8e536215"] ?: array()) + array("_3cea3828c93d32f" . "03b72c61969936d0" . "7" => base64_decode("dXNsZWV" . "w"), "_14ae1876cf3a634118f7390d6109d6" . "3a" => base64_decode("IHNlY29uZH" . "M="), "_b944e941cf3e96d71ca57de69f" . "63951d" => base64_decode("Q291bGQgbm90IGFjc" . "XVpcmUgbG9jayB3aX" . "RoaW4g"), "_0284d38f97f9e8b" . "9c7b03bb8928afae" . "1" => base64_decode("K" . "Q" . "=" . "="), "_bb55a1c1933480eebfc5c" . "0784681f244" => base64_decode("TG9jayBhY3F1aXNpdGlvbiBhdHR" . "lbXB0IGZhaWxlZCwgc2xlZXBpbm" . "cgZm9yIH4="), "_7e09d700055e6ea9c" . "c6a3e34b08e57be" => base64_decode("b" . "X" . "M" . "="), "_04eddfbb35ad7c78f53e467998e" . "6f361" => base64_decode("cmFuZG9tX" . "2ludA=="), "_cacda84c05d3266a2ccc76" . "b15553369c" => base64_decode("bWljcm9" . "0aW1l"), "_77f1ede5742394cb8ecbb2de770b8b" . "5b" => base64_decode("RmFpbGVkIHRvIGdlbmVyYXRlIHJhbmRvbSBpbnRlZ2VyIGZvciBzcGlubG" . "9jaw=="), "_80058da3810e342c42fe077578fc6" . "5a7" => base64_decode("IChsYXN0IGVycm9yOiA" . "="));
use Claromentis\Core\Lock\Exception\LockAcquisitionException;
use Throwable;
trait SpinLockTrait
{
    
    
    
    
    protected bool $spin = false;
    
    
    
    
    protected int $timeout = 5;
    
    
    
    
    protected $lock_counts = [];
    
    
    
    
    
    
    protected int $spin_delay = 500;
    
    
    
    
    
    
    protected int $random_spin = 100;
    
    
    
    
    private $last_error = null;
    
    
    
    
    
    
    public function acquireLock(string $key_name)
    {
        $this->lock_counts[$key_name] = $this->lock_counts[$key_name] ?? 0;
        if ($this->lock_counts[$key_name] == 0) {
            $begin = $GLOBALS["__65da1b1ec2d6" . "653e8a0ab7848e" . "536215"]["_cacda84c05d32" . "66a2ccc76b1555" . "3369c"](true);
            $success = true;
            while (!$this->attemptLock($key_name)) {
                $random_int = 0;
                if ($this->random_spin > 0) {
                    try {
                        $random_int = $GLOBALS["__65da1b1ec2d6653e8a0ab7848e" . "536215"]["_04eddfbb35ad7c78f53e" . "467998e6f361"](0, $this->random_spin);
                    } catch (Throwable $throwable) {
                        $this->logger->warning($GLOBALS["__65da1b1e" . "c2d6653e8a" . "0ab7848e53" . "6215"]["_77f1ede5742394cb8ec" . "bb2de770b8b5b"]);
                    }
                }
                $spin_delay = $this->spin_delay + $random_int;
                $this->logger->debug($GLOBALS["__65da1b1e" . "c2d6653e8a" . "0ab7848e53" . "6215"]["_bb55a1c19" . "33480eebfc" . "5c0784681f" . "244"] . $spin_delay . $GLOBALS["__65da1b1ec" . "2d6653e8a0a" . "b7848e53621" . "5"]["_7e09d700055e6ea9cc6" . "a3e34b08e57be"]);
                $GLOBALS["__65da1b1ec2d6653e8a0ab7848e5" . "36215"]["_3cea3828c93d32f03b72c6196993" . "6d07"]($spin_delay * 1000);
                $now = $GLOBALS["__65da1b1ec2d66" . "53e8a0ab7848e53" . "6215"]["_cacda84c05d3266a" . "2ccc76b15553369c"](true);
                if (!$this->spin || $now - $begin > $this->timeout) {
                    $success = false;
                    break;
                }
            }
            if (!$success) {
                $message = $GLOBALS["__65da1b1ec2d6653" . "e8a0ab7848e536215"]["_b944e941cf3e96d71ca57de69f6" . "3951d"] . $this->timeout . $GLOBALS["__65da1b1ec2d6653e8a0ab7848e53" . "6215"]["_14ae1876cf3a634118f" . "7390d6109d63a"];
                if (isset($this->last_error)) {
                    $message .= $GLOBALS["__65da1b1ec2d6653e8a" . "0ab7848e536215"]["_80058da3810e342c42fe077578fc" . "65a7"] . $this->last_error . $GLOBALS["__65da1b1ec2d6653e" . "8a0ab7848e536215"]["_0284d38f97f9e8b9c7b03" . "bb8928afae1"];
                    $this->last_error = null;
                }
                throw new LockAcquisitionException($message);
            }
            ++$this->lock_counts[$key_name];
        }
    }
    
    
    
    
    
    
    public function setSpinDelay(int $spin_delay) : void
    {
        $this->spin_delay = $spin_delay;
    }
}