Skip to content

Commit 8569f65

Browse files
committed
Priority mutex
1 parent 3f4d83f commit 8569f65

File tree

3 files changed

+100
-0
lines changed

3 files changed

+100
-0
lines changed

src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ else()
4949
include/cpputils2/linux/futex/shared_futex.hpp
5050
include/cpputils2/linux/thread/thread.hpp
5151
include/cpputils2/linux/sched/sched.hpp
52+
include/cpputils2/linux/priomutex/priomutex.hpp
5253
)
5354

5455
endif()
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
#pragma once
2+
3+
#include "cpputils2/common/types.hpp"
4+
5+
#include <pthread.h>
6+
7+
namespace CppUtils2
8+
{
9+
10+
class priomutex
11+
{
12+
13+
public:
14+
using native_handle_type = pthread_mutex_t *;
15+
16+
priomutex()
17+
{
18+
initialized = false;
19+
pthread_mutexattr_t attr;
20+
21+
int ret = pthread_mutexattr_init(&attr);
22+
if (ret != 0)
23+
{
24+
return;
25+
}
26+
27+
ret = pthread_mutexattr_setprotocol(&attr, PTHREAD_PRIO_INHERIT);
28+
if (ret != 0)
29+
{
30+
return;
31+
}
32+
33+
ret = pthread_mutex_init(&pt_mutex, &attr);
34+
if (ret != 0)
35+
{
36+
return;
37+
}
38+
39+
initialized = true;
40+
}
41+
42+
virtual ~priomutex()
43+
{
44+
pthread_mutex_destroy(&pt_mutex);
45+
}
46+
47+
priomutex(const priomutex &) = delete;
48+
priomutex &operator=(const priomutex &) = delete;
49+
50+
void lock()
51+
{
52+
auto ret = pthread_mutex_lock(&pt_mutex);
53+
if (ret != 0)
54+
{
55+
// (yangosoft) TODO handle error
56+
initialized = false;
57+
}
58+
}
59+
60+
void unlock() noexcept
61+
{
62+
pthread_mutex_unlock(&pt_mutex);
63+
}
64+
65+
bool try_lock() noexcept
66+
{
67+
return pthread_mutex_trylock(&pt_mutex) == 0;
68+
}
69+
70+
native_handle_type native_handle() noexcept
71+
{
72+
return &pt_mutex;
73+
}
74+
75+
bool is_lock_free() const noexcept
76+
{
77+
return false;
78+
}
79+
80+
bool is_initialized() const noexcept
81+
{
82+
return initialized;
83+
}
84+
85+
private:
86+
pthread_mutex_t pt_mutex;
87+
bool initialized;
88+
};
89+
} // namespace CppUtils2

src/test/cpputils2_tests.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "cpputils2/linux/net/socket/udpsocketserver.hpp"
1515
#include "cpputils2/linux/net/socket/udsclient.hpp"
1616
#include "cpputils2/linux/net/socket/udsserver.hpp"
17+
#include "cpputils2/linux/priomutex/priomutex.hpp"
1718
#include "cpputils2/linux/sched/sched.hpp"
1819
#include "cpputils2/linux/shm/shm.hpp"
1920
#include "cpputils2/linux/thread/thread.hpp"
@@ -168,6 +169,15 @@ namespace
168169
EXPECT_EQ(CppUtils2::Result::RET_OK, CppUtils2::Result::RET_OK);
169170
}
170171

172+
TEST(PriorityMutex, PriorityMutex)
173+
{
174+
CppUtils2::priomutex mutex;
175+
bool is_initialized = mutex.is_initialized();
176+
EXPECT_TRUE(is_initialized);
177+
mutex.lock();
178+
mutex.unlock();
179+
}
180+
171181
#endif
172182

173183
#ifdef _WIN32

0 commit comments

Comments
 (0)