File indexing completed on 2024-12-15 04:01:20

0001 /*
0002  * SPDX-FileCopyrightText: 2019-2023 Mattia Basaglia <dev@dragon.best>
0003  *
0004  * SPDX-License-Identifier: GPL-3.0-or-later
0005  */
0006 
0007 #pragma once
0008 
0009 #include <mutex>
0010 
0011 namespace glaxnimate::utils {
0012 
0013 class PseudoMutex
0014 {
0015 public:
0016     bool try_lock() noexcept
0017     {
0018         if ( locked )
0019             return false;
0020 
0021         locked = true;
0022         return true;
0023     }
0024 
0025     void lock() noexcept
0026     {
0027         locked = true;
0028     }
0029 
0030     void unlock() noexcept
0031     {
0032         locked = false;
0033     }
0034 
0035     explicit operator bool() const noexcept
0036     {
0037         return locked;
0038     }
0039 
0040     std::unique_lock<PseudoMutex> get_lock()
0041     {
0042         return std::unique_lock<PseudoMutex>(*this, std::try_to_lock);
0043     }
0044 
0045 private:
0046     bool locked = false;
0047 };
0048 
0049 } // namespace glaxnimate::utils