Skip to content

Commit

Permalink
rev2
Browse files Browse the repository at this point in the history
  • Loading branch information
dr3mro committed Jan 2, 2025
1 parent 32a96a6 commit 4f92e33
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/database/inuseguard.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
#include "database/inuseguard.hpp"

#include <atomic>
#include <chrono>
#include <condition_variable>
#include <mutex>

InUseGuard::InUseGuard(std::atomic<bool>& in_use, std::mutex& mtx, std::condition_variable& _cv) : isConnectionInUse_(in_use), mutex_(mtx), conditionVar_(_cv)
{
std::unique_lock<std::mutex> lock(mutex_);

conditionVar_.wait(lock, [this] { return !isConnectionInUse_.load(); });
conditionVar_.wait_for(lock, std::chrono::seconds(1), [this]() { return !isConnectionInUse_.load(); });

isConnectionInUse_.store(true);
}
Expand Down
6 changes: 3 additions & 3 deletions src/database/inuseguard.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
class InUseGuard
{
public:
explicit InUseGuard(std::atomic<bool>& in_use, std::mutex& mtx, std::condition_variable& _cv);
InUseGuard() = delete;
InUseGuard(const InUseGuard&) = default;
InUseGuard(InUseGuard&&) = delete;
InUseGuard& operator=(const InUseGuard&) = delete;
InUseGuard& operator=(InUseGuard&&) = delete;
InUseGuard(std::atomic<bool>& in_use, std::mutex& mtx, std::condition_variable& _cv);

~InUseGuard();
virtual ~InUseGuard();

private:
std::atomic<bool>& isConnectionInUse_; /*NOLINT*/
Expand Down

0 comments on commit 4f92e33

Please sign in to comment.