Skip to content

Commit

Permalink
thread_util: add rule of 5; remove reference member
Browse files Browse the repository at this point in the history
  • Loading branch information
sebsura authored and BareosBot committed Sep 22, 2023
1 parent 903668b commit c06771b
Showing 1 changed file with 28 additions and 18 deletions.
46 changes: 28 additions & 18 deletions core/src/lib/thread_util.h
Expand Up @@ -29,29 +29,39 @@
template <typename T, typename Mutex, template <typename> typename Lock>
class locked {
public:
locked(Mutex& mut, T& data) : lock{mut}, data(data) {}
locked(Mutex& mut, T* data) : lock{mut}, data(data) {}
locked(Lock<Mutex> lock, T* data) : lock{std::move(lock)}, data(data) {}

locked(Lock<Mutex> lock, T& data) : lock{std::move(lock)}, data(data) {}
locked(const locked&) = delete;
locked& operator=(const locked&) = delete;
locked(locked&& that) : lock(std::move(that.lock)), data(that.data)
{
that.data = nullptr;
}

const T& get() const { return data; }
T& get() { return data; }
locked& operator=(locked&& that)
{
std::swap(lock, that.lock);
std::swap(data, that.data);
return *this;
}

T& get() { return data; }
T& operator*() { return data; }
T* operator->() { return &data; }
T& get() { return *data; }
T& operator*() { return *data; }
T* operator->() { return data; }

const T& get() const { return data; }
const T& operator*() const { return data; }
const T* operator->() const { return &data; }
const T& get() const { return *data; }
const T& operator*() const { return *data; }
const T* operator->() const { return data; }

template <typename Pred> void wait(std::condition_variable& cv, Pred&& p)
{
cv.wait(lock, p);
cv.wait(lock, [this, p = std::move(p)] { return p(*data); });
}

private:
Lock<Mutex> lock;
T& data;
T* data;
};

template <typename T>
Expand All @@ -77,19 +87,19 @@ template <typename T> class synchronized {
std::unique_lock _{mut};
}

unique_locked<T> lock() { return {mut, data}; }
[[nodiscard]] unique_locked<T> lock() { return {mut, &data}; }

std::optional<unique_locked<T>> try_lock()
[[nodiscard]] std::optional<unique_locked<T>> try_lock()
{
std::unique_lock l(mut, std::try_to_lock);
if (l.owns_lock()) {
return unique_locked<T>{std::move(l), data};
return unique_locked<T>{std::move(l), &data};
} else {
return std::nullopt;
}
}

unique_locked<const T> lock() const { return {mut, data}; }
[[nodiscard]] unique_locked<const T> lock() const { return {mut, &data}; }

private:
mutable std::mutex mut{};
Expand All @@ -108,7 +118,7 @@ template <typename T> class rw_synchronized {
{
std::unique_lock l(mut, std::try_to_lock);
if (l.owns_lock()) {
return {std::move(l), data};
return {std::move(l), &data};
} else {
return std::nullopt;
}
Expand All @@ -119,7 +129,7 @@ template <typename T> class rw_synchronized {
{
std::shared_lock l(mut, std::try_to_lock);
if (l.owns_lock()) {
return {std::move(l), data};
return {std::move(l), &data};
} else {
return std::nullopt;
}
Expand Down

0 comments on commit c06771b

Please sign in to comment.