Skip to content

Commit 6746e1a

Browse files
committed
add test for robust
1 parent f07fc84 commit 6746e1a

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

test/test_pthread.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
2+
#include <thread>
3+
4+
#include "test.h"
5+
6+
#ifdef __linux__
7+
#include <pthread.h>
8+
#include <time.h>
9+
10+
TEST(PThread, Robust) {
11+
pthread_mutexattr_t ma;
12+
pthread_mutexattr_init(&ma);
13+
pthread_mutexattr_setrobust(&ma, PTHREAD_MUTEX_ROBUST);
14+
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
15+
pthread_mutex_init(&mutex, &ma);
16+
17+
std::thread{[&mutex] {
18+
pthread_mutex_lock(&mutex);
19+
// pthread_mutex_unlock(&mutex);
20+
}}.join();
21+
22+
struct timespec tout;
23+
clock_gettime(CLOCK_REALTIME, &tout);
24+
struct tm *tmp = localtime(&tout.tv_sec);
25+
tout.tv_sec += 1; /* 1 seconds from now */
26+
int r = pthread_mutex_timedlock(&mutex, &tout);
27+
EXPECT_EQ(r, EOWNERDEAD);
28+
29+
pthread_mutex_consistent(&mutex);
30+
pthread_mutex_unlock(&mutex);
31+
pthread_mutex_destroy(&mutex);
32+
}
33+
#endif // __linux__

0 commit comments

Comments
 (0)