Skip to content
Snippets Groups Projects
lock.hh 1.32 KiB
Newer Older
  • Learn to ignore specific revisions
  • #ifndef __VEREIGN_KVSTORE_LOCK_HH
    #define __VEREIGN_KVSTORE_LOCK_HH
    
    #include <vereign/kvstore/storage.hh>
    #include <chrono>
    
    namespace vereign::kvstore {
    
    
    Daniel Lyubomirov's avatar
    Daniel Lyubomirov committed
    /**
     * Lock guard used for lock/unlock Storage within a scope.
     *
     * When the Lock is constructed it locks the Storage, and when it is destroyed it unlock it.
     */
    
    class Lock {
    public:
    
    Daniel Lyubomirov's avatar
    Daniel Lyubomirov committed
      /**
       * Creates a Lock and locks the Storage.
       *
       * @param storage The storage to lock.
       * @throws LockError when the lock is held by another process.
       */
    
      explicit Lock(Storage& storage);
    
    Daniel Lyubomirov's avatar
    Daniel Lyubomirov committed
    
      /**
       * Creates a Lock and locks the Storage.
       *
       * If the lock is not possible it retries `retry_count` and sleeps between retries `sleep_interval`.
       *
       * @param storage The storage to lock.
       * @param retry_count How many times to retry if the lock is held by another process.
       * @param sleep_interval How many time to sleep between retries.
       *
       * @throws LockError If the lock could not be held after `retry_count` retries.
       */
    
      Lock(Storage& storage, int retry_count, std::chrono::milliseconds sleep_interval);
    
    Daniel Lyubomirov's avatar
    Daniel Lyubomirov committed
    
      /**
       * Unlocks the storage.
       */
    
      ~Lock() noexcept;
    
    
    Daniel Lyubomirov's avatar
    Daniel Lyubomirov committed
      // copying is disabled.
    
      Lock(const Lock&) = delete;
      auto operator=(const Lock&) -> Lock&  = delete;
    
    private:
      Storage& storage_;
    };
    
    
    } // namespace vereign::kvstore
    
    #endif // __VEREIGN_KVSTORE_LOCK_HH