1 | #include <fstream.hfa>
|
---|
2 | #include <thread.hfa>
|
---|
3 | /*
|
---|
4 | test pthread_mutex to provide mutual exclusion
|
---|
5 | test pthread_mutex_trylock not block when lock is acquired by others; test pthread_mutex_trylock can acquire the lock
|
---|
6 | */
|
---|
7 | volatile int cnt_nolock = 0;
|
---|
8 | volatile int cnt_lock = 0;
|
---|
9 | volatile int cnt_trylock = 0;
|
---|
10 | extern "C"{
|
---|
11 | static pthread_mutex_t _mutex;
|
---|
12 | }
|
---|
13 |
|
---|
14 | /* mutex pthread routine */
|
---|
15 | // unlocked increnment
|
---|
16 | void* inc_unlock(void* cnt){
|
---|
17 | for (uintptr_t i = 0; i < (uintptr_t)cnt; i++){
|
---|
18 | cnt_nolock++;
|
---|
19 | } // for
|
---|
20 | return NULL;
|
---|
21 | }
|
---|
22 | // locked increment
|
---|
23 | void* inc_lock(void* cnt){
|
---|
24 | pthread_mutex_lock(&_mutex);
|
---|
25 | for (uintptr_t i = 0; i < (uintptr_t)cnt; i++){
|
---|
26 | cnt_lock++;
|
---|
27 | } // for
|
---|
28 | pthread_mutex_unlock(&_mutex);
|
---|
29 | return NULL;
|
---|
30 | }
|
---|
31 |
|
---|
32 | /* test lock vs unlock */
|
---|
33 | void test_unlock(){
|
---|
34 | pthread_t threads[20];
|
---|
35 | for (int i = 0; i < 20; i++){
|
---|
36 | pthread_create(&threads[i], NULL, inc_unlock, (void*)100000000);
|
---|
37 | }
|
---|
38 | for (int i = 0; i < 20; i++){
|
---|
39 | void * res = NULL;
|
---|
40 | pthread_join(threads[i], &res);
|
---|
41 | }
|
---|
42 | sout | "unlock res is" | cnt_nolock;
|
---|
43 | cnt_nolock = 0;
|
---|
44 | }
|
---|
45 | extern "C"{
|
---|
46 | void test_lock(){
|
---|
47 | pthread_mutex_init(&_mutex, NULL);
|
---|
48 | pthread_t threads[20];
|
---|
49 | for (int i = 0; i < 20; i++){
|
---|
50 |
|
---|
51 | pthread_create(&threads[i], NULL, inc_lock, (void*)100000000);
|
---|
52 | }
|
---|
53 | for (int i = 0; i < 20; i++){
|
---|
54 | void * res = NULL;
|
---|
55 | pthread_join(threads[i], &res);
|
---|
56 | }
|
---|
57 | sout | "lock res is" | cnt_lock;
|
---|
58 | pthread_mutex_destroy(&_mutex);
|
---|
59 | if (cnt_lock != 100000000 * 20) {
|
---|
60 | sout | "pthread mutex not working";
|
---|
61 | exit(1);
|
---|
62 | }
|
---|
63 | cnt_lock = 0;
|
---|
64 | }
|
---|
65 | }
|
---|
66 |
|
---|
67 |
|
---|
68 | /* mutex trylock pthread routine */
|
---|
69 | void* trylock_test2(void* arg){
|
---|
70 | int res = pthread_mutex_trylock(&_mutex);
|
---|
71 | sout | "in trylocktest2 res1 is" | res;
|
---|
72 | res = pthread_mutex_trylock(&_mutex);
|
---|
73 | sout | "in trylocktest2 res2 is" | res;
|
---|
74 | pthread_mutex_lock(&_mutex);
|
---|
75 | for (uintptr_t i = 0; i < (uintptr_t)arg; i++) cnt_trylock++;
|
---|
76 | pthread_mutex_unlock(&_mutex);
|
---|
77 | return NULL;
|
---|
78 | }
|
---|
79 |
|
---|
80 | void* trylock_test1(void* arg){
|
---|
81 | int res = pthread_mutex_trylock(&_mutex);
|
---|
82 | sout | "in trylocktest1 res1 is" | res;
|
---|
83 | res = pthread_mutex_trylock(&_mutex);
|
---|
84 | sout | "in trylocktest1 res2 is" | res;
|
---|
85 | pthread_t task2;
|
---|
86 | pthread_create(&task2, NULL, trylock_test2, (void*)100000000);
|
---|
87 |
|
---|
88 | // inc cnt then release the lock
|
---|
89 | for (uintptr_t i = 0; i < (uintptr_t)arg; i++) cnt_trylock++;
|
---|
90 | pthread_mutex_unlock(&_mutex);
|
---|
91 | pthread_mutex_unlock(&_mutex);
|
---|
92 | void * dummy = NULL;
|
---|
93 | pthread_join(task2, &dummy);
|
---|
94 | sout | "cnt_trylock is " | cnt_trylock;
|
---|
95 | return NULL;
|
---|
96 | }
|
---|
97 |
|
---|
98 | // trylock test
|
---|
99 | void test_trylock(){
|
---|
100 | pthread_mutex_init(&_mutex, NULL);
|
---|
101 | pthread_t task1;
|
---|
102 | pthread_create(&task1, NULL, trylock_test1, (void*)100000000);
|
---|
103 | void * dummy = NULL;
|
---|
104 | pthread_join(task1,&dummy);
|
---|
105 | pthread_mutex_destroy(&_mutex);
|
---|
106 | if (cnt_trylock != 100000000 * 2) {
|
---|
107 | sout | "pthread try mutex not working";
|
---|
108 | exit(1);
|
---|
109 | }
|
---|
110 | cnt_trylock = 0;
|
---|
111 | }
|
---|
112 |
|
---|
113 |
|
---|
114 |
|
---|
115 | int main()
|
---|
116 | {
|
---|
117 |
|
---|
118 | // compare unlock vs lock
|
---|
119 | test_lock();
|
---|
120 | // test trylock
|
---|
121 | test_trylock();
|
---|
122 |
|
---|
123 | return 0;
|
---|
124 | }
|
---|