Skip to content

Commit 87b24fa

Browse files
refs #15 @1h
1 parent 12ebcb4 commit 87b24fa

File tree

3 files changed

+81
-34
lines changed

3 files changed

+81
-34
lines changed

include/pthread/mutex.hpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,15 @@ namespace pthread {
4646
void lock();
4747

4848
/**
49-
The function pthread_mutex_trylock is identical to pthread_mutex_lock except tha
50-
if the mutex object referenced by mutex is currently locked (by any thread,
51-
including the current thread), the call returns immediately.
52-
53-
@throw mutex_exception if error conditions preventing this method to succeed.
54-
@see lock
49+
* The function pthread_mutex_trylock is identical to pthread_mutex_lock except that
50+
* if the mutex object referenced by mutex is currently locked (by any thread,
51+
* including the current thread), the call returns immediately.
52+
*
53+
* @return
54+
* @throw mutex_exception if error conditions preventing this method to succeed.
55+
* @see lock
5556
*/
56-
void try_lock();
57+
bool try_lock();
5758

5859
/**
5960
The pthread_mutex_unlock function releases the mutex object referenced by mutex. The manner in which a mutex is released is dependent upon the mutex's type attribute. If there are threads blocked on the mutex object referenced by mutex when unlock is called, resulting in the mutex becoming available, the scheduling policy is used to determine which thread shall acquire the mutex.

src/mutex.cpp

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,38 +10,41 @@
1010

1111
namespace pthread {
1212

13-
mutex::mutex () {
14-
auto rc = pthread_mutex_init (&_mutex, NULL);
15-
if ( rc != 0 ) {
16-
throw mutex_exception ( "In constructor of mutex pthread_mutex_init(&mutex, NULL) failed. ", rc );
13+
mutex::mutex() {
14+
auto rc = pthread_mutex_init(&_mutex, NULL);
15+
if (rc != 0) {
16+
throw mutex_exception("In constructor of mutex pthread_mutex_init(&mutex, NULL) failed. ", rc);
17+
}
1718
}
18-
}
1919

20-
mutex::~mutex () {
21-
pthread_mutex_destroy (&_mutex);
22-
}
20+
mutex::~mutex() {
21+
pthread_mutex_destroy(&_mutex);
22+
}
2323

24-
void mutex::lock () {
25-
int rc = -1;
26-
rc = pthread_mutex_lock ( &_mutex );
27-
if ( rc != 0 ){
28-
throw mutex_exception("pthread_mutex_lock failed.", rc);
24+
void mutex::lock() {
25+
int rc = -1;
26+
rc = pthread_mutex_lock(&_mutex);
27+
if (rc != 0) {
28+
throw mutex_exception("pthread_mutex_lock failed.", rc);
29+
}
2930
}
30-
}
3131

32-
void mutex::try_lock () {
32+
bool mutex::try_lock() {
33+
bool status = false;
3334

34-
auto rc = pthread_mutex_trylock ( &_mutex );
35-
if ( rc != 0 ){
36-
throw mutex_exception("pthread_mutex_trylock failed, already locked.", rc);
35+
auto rc = pthread_mutex_trylock(&_mutex);
36+
if (rc == 0) {
37+
status = true;
38+
} else {
39+
throw mutex_exception("pthread_mutex_trylock failed, already locked.", rc);
40+
}
3741
}
38-
}
3942

40-
void mutex::unlock () {
41-
auto rc = pthread_mutex_unlock ( &_mutex );
42-
if ( rc != 0 ){
43-
throw mutex_exception("pthread_mutex_unlock failed.", rc);
43+
void mutex::unlock() {
44+
auto rc = pthread_mutex_unlock(&_mutex);
45+
if (rc != 0) {
46+
throw mutex_exception("pthread_mutex_unlock failed.", rc);
47+
}
4448
}
45-
}
4649

4750
}

tests/concurrency_tests.cpp

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ TEST(concurrency, mutex) {
1818
try {
1919
pthread::lock_guard<pthread::mutex> lock(mutex);
2020
success = true;
21-
// std::cout << "in critical section (mutex)" << std::endl;
22-
2321
} catch ( std::exception &err) {
2422
std::cerr << "something went wrong: " << err.what() << std::endl;
2523
}catch ( ... ){
@@ -54,4 +52,49 @@ TEST(concurrency, read_write_lock) {
5452
}
5553

5654
EXPECT_TRUE(success);
57-
}
55+
}
56+
57+
TEST(concurrency, condition_variable_wait_for){
58+
pthread::condition_variable condition;
59+
pthread::mutex mutex;
60+
61+
EXPECT_EQ(pthread::cv_status::timedout, condition.wait_for(mutex, 1*1000));
62+
EXPECT_THROW(mutex.try_lock(), pthread::pthread_exception);
63+
mutex.unlock();
64+
65+
{
66+
pthread::lock_guard<pthread::mutex> lock{mutex};
67+
EXPECT_EQ(pthread::cv_status::timedout, condition.wait_for(lock, 1 * 1000));
68+
}
69+
EXPECT_NO_THROW(mutex.try_lock());
70+
}
71+
72+
/* NOSONAR for later use
73+
class test_thread: public pthread::abstract_thread{
74+
public:
75+
void run() noexcept override{
76+
pthread::this_thread::sleep_for(5*1000);
77+
std::cout << "thread is waiting for mutex" << std::endl;
78+
pthread::lock_guard<pthread::mutex> lock(*_mutex);
79+
std::cout << "thread got mutex" << std::endl;
80+
}
81+
82+
test_thread( pthread::mutex *mutex): _mutex(mutex){
83+
84+
}
85+
private:
86+
pthread::mutex *_mutex;
87+
};
88+
89+
pthread::mutex mutex;
90+
91+
test_thread thread{&mutex};
92+
thread.start();
93+
{
94+
pthread::lock_guard<pthread::mutex> lock(mutex);
95+
pthread::this_thread::sleep_for(10*1000);
96+
}
97+
98+
thread.join();
99+
100+
*/

0 commit comments

Comments
 (0)