Program Listing for File lockable.hpp
↰ Return to documentation for file (rcppsw/multithread/lockable.hpp)
#pragma once
/*******************************************************************************
 * Includes
 ******************************************************************************/
#include <shared_mutex>
#include "rcppsw/rcppsw.hpp"
/*******************************************************************************
 * Namespaces/Decls
 ******************************************************************************/
namespace rcppsw::multithread {
/*******************************************************************************
 * Class Definitions
 ******************************************************************************/
class lockable {
 public:
  lockable(void) = default;
  virtual ~lockable(void) = default;
  /* Not move/copy constructable/assignable by default */
  lockable(const lockable&) = delete;
  const lockable& operator=(const lockable&) = delete;
  lockable(lockable&&) = delete;
  lockable& operator=(lockable&&) = delete;
  void maybe_lock_wr(std::shared_mutex* mtx, bool cond) {
    if (cond) {
      mtx->lock();
    }
  }
  void maybe_unlock_wr(std::shared_mutex* mtx, bool cond) {
    if (cond) {
      mtx->unlock();
    }
  }
  void maybe_lock_rd(std::shared_mutex* mtx, bool cond) const {
    if (cond) {
      mtx->lock_shared();
    }
  }
  void maybe_unlock_rd(std::shared_mutex* mtx, bool cond) const {
    if (cond) {
      mtx->unlock_shared();
    }
  }
  void lock_wr(std::shared_mutex* mtx) { maybe_lock_wr(mtx, true); }
  void unlock_wr(std::shared_mutex* mtx) { maybe_unlock_wr(mtx, true); }
  void lock_rd(std::shared_mutex* mtx) const { maybe_lock_rd(mtx, true); }
  void unlock_rd(std::shared_mutex* mtx) const { maybe_unlock_rd(mtx, true); }
};
} /* namespace rcppsw::multithread */