Skip to content

Commit b29f08b

Browse files
committed
Add mutex encapsulation
1 parent c9fe15e commit b29f08b

4 files changed

Lines changed: 131 additions & 0 deletions

File tree

include/cppbase/mutex-inl.hpp

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#pragma once
2+
3+
#ifndef CPPBASE_MUTEX_INL_HPP
4+
#define CPPBASE_MUTEX_INL_HPP
5+
6+
#include "mutex.hpp"
7+
8+
namespace cppbase {
9+
10+
template<typename Mtx>
11+
void Mutex<Mtx>::lock()
12+
{
13+
mutex_.lock();
14+
}
15+
16+
template<typename Mtx>
17+
bool Mutex<Mtx>::try_lock() noexcept
18+
{
19+
return mutex_.try_lock();
20+
}
21+
22+
template<typename Mtx>
23+
void Mutex<Mtx>::unlock() noexcept
24+
{
25+
mutex_.unlock();
26+
}
27+
28+
template<typename Mtx>
29+
template <class Rep, class Period>
30+
auto Mutex<Mtx>::try_lock_for(const std::chrono::duration<Rep, Period>& rel_time)
31+
-> decltype(std::declval<Mtx>().try_lock_for(rel_time), bool())
32+
{
33+
static_assert(
34+
std::is_same_v<Mtx, std::timed_mutex> ||
35+
std::is_same_v<Mtx, std::recursive_timed_mutex>,
36+
"Mutex<try_lock_for>: Mtx must be std::timed_mutex or std::recursive_timed_mutex!"
37+
);
38+
return mutex_.try_lock_for(rel_time);
39+
}
40+
41+
template<typename Mtx>
42+
template <class Clock, class Duration>
43+
auto Mutex<Mtx>::try_lock_until(const std::chrono::time_point<Clock, Duration>& abs_time)
44+
-> decltype(std::declval<Mtx>().try_lock_until(abs_time), bool())
45+
{
46+
static_assert(
47+
std::is_same_v<Mtx, std::timed_mutex> ||
48+
std::is_same_v<Mtx, std::recursive_timed_mutex>,
49+
"Mutex<try_lock_until>: Mtx must be std::timed_mutex or std::recursive_timed_mutex!"
50+
);
51+
return mutex_.try_lock_until(abs_time);
52+
}
53+
54+
template<typename Mtx>
55+
typename Mtx::native_handle_type Mutex<Mtx>::native_handle() noexcept
56+
{
57+
return mutex_.native_handle();
58+
}
59+
60+
} // namespace cppbase
61+
62+
#endif // CPPBASE_MUTEX_INL_HPP

include/cppbase/mutex.hpp

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#pragma once
2+
#ifndef CPPBASE_MUTEX_HPP
3+
#define CPPBASE_MUTEX_HPP
4+
5+
#include <mutex>
6+
#include <chrono>
7+
#include <type_traits>
8+
#include "nocopyable.hpp"
9+
#include "nomoveable.hpp"
10+
11+
namespace cppbase {
12+
13+
/**
14+
* @brief Generic template mutex class, based on standard mutex type encapsulation
15+
* @tparam Mtx Underlying mutex type, default is std::mutex
16+
* @note Template implementation is in mutex-inl.hpp, must be included after declaration
17+
*/
18+
template<typename Mtx = std::mutex>
19+
class Mutex final: public NoCopyable, public NoMoveable
20+
{
21+
public:
22+
void lock();
23+
bool try_lock() noexcept;
24+
void unlock() noexcept;
25+
26+
template <class Rep, class Period>
27+
auto try_lock_for(const std::chrono::duration<Rep, Period>& rel_time)
28+
-> decltype(std::declval<Mtx>().try_lock_for(rel_time), bool());
29+
30+
template <class Clock, class Duration>
31+
auto try_lock_until(const std::chrono::time_point<Clock, Duration>& abs_time)
32+
-> decltype(std::declval<Mtx>().try_lock_until(abs_time), bool());
33+
34+
typename Mtx::native_handle_type native_handle() noexcept;
35+
36+
private:
37+
Mtx mutex_;
38+
};
39+
40+
/**
41+
* @brief RAII wrapper for Mutex, auto lock/unlock
42+
* @tparam Mtx Underlying mutex type (same as Mutex's template parameter)
43+
* @note Only works with cppbase::Mutex, not std::mutex directly
44+
*/
45+
template<typename Mtx = std::mutex>
46+
class LockGuard final: public NoCopyable, public NoMoveable
47+
{
48+
public:
49+
explicit LockGuard(Mutex<Mtx> &mtx) noexcept
50+
: mutex_(mtx)
51+
{
52+
mutex_.lock();
53+
}
54+
55+
~LockGuard() noexcept
56+
{
57+
mutex_.unlock();
58+
}
59+
60+
private:
61+
Mutex<Mtx> &mutex_;
62+
};
63+
64+
} // namespace cppbase
65+
66+
#include "mutex-inl.hpp"
67+
#endif // CPPBASE_MUTEX_HPP

include/cppbase/nocopyable.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#pragma once
12
#ifndef CPPBASE_NONCOPYABLE_HPP
23
#define CPPBASE_NONCOPYABLE_HPP
34

include/cppbase/nomoveable.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#pragma once
12
#ifndef CPPBASE_NONMOVABLE_HPP
23
#define CPPBASE_NONMOVABLE_HPP
34

0 commit comments

Comments
 (0)