1
2 #include <assert.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6
7 #include <pthread.h>
8
9 #define test_assert(_expr) \
10 if (!(_expr)) { \
11 __assert(__func__, __FILE__, __LINE__, #_expr); \
12 }
13
14
15 pthread_mutex_t mtx;
16 pthread_cond_t cnd;
17
18 void *
thread_simple(void * arg)19 thread_simple(void *arg)
20 {
21 printf("thread_simple %p!\n", arg);
22
23 return arg;
24 }
25
26 void *
thread_lock(void * arg)27 thread_lock(void *arg)
28 {
29 int i;
30 int status;
31
32 for (i = 0; i < 100; i++) {
33 status = pthread_mutex_lock(&mtx);
34 test_assert(status == 0);
35 pthread_yield();
36 status = pthread_mutex_unlock(&mtx);
37 test_assert(status == 0);
38 }
39
40 return NULL;
41 }
42
43 void *
thread_cond(void * arg)44 thread_cond(void *arg)
45 {
46 int i;
47 int status;
48
49 for (i = 0; i < 100; i++) {
50 status = pthread_cond_wait(&cnd, NULL);
51 test_assert(status == 0);
52 status = pthread_cond_signal(&cnd);
53 test_assert(status == 0);
54 }
55
56 return NULL;
57 }
58
59 int
main(int argc,const char * argv[])60 main(int argc, const char *argv[])
61 {
62 int i;
63 int status;
64 pthread_t thr;
65 void *result;
66
67 printf("PThread Test\n");
68
69 // Simple thread Test
70 printf("simple test: ");
71 status = pthread_create(&thr, NULL, thread_simple, NULL);
72 test_assert(status == 0);
73 status = pthread_join(thr, &result);
74 test_assert(status == 0);
75 test_assert(result == NULL);
76 printf("OK\n");
77
78 // Return value Test
79 printf("return value test: ");
80 status = pthread_create(&thr, NULL, thread_simple, (void *)1);
81 test_assert(status == 0);
82 status = pthread_join(thr, &result);
83 test_assert(status == 0);
84 test_assert(result == (void *)1);
85 printf("OK\n");
86
87 // Mutex Test
88 printf("simple mutex lock test: ");
89 status = pthread_mutex_init(&mtx, NULL);
90 test_assert(status == 0);
91 status = pthread_mutex_lock(&mtx);
92 test_assert(status == 0);
93 status = pthread_mutex_unlock(&mtx);
94 test_assert(status == 0);
95 status = pthread_mutex_destroy(&mtx);
96 test_assert(status == 0);
97 printf("OK\n");
98
99 // Mutex Contention Test
100 printf("contended mutex lock test: ");
101 pthread_mutex_init(&mtx, NULL);
102 status = pthread_create(&thr, NULL, thread_lock, (void *)1);
103 test_assert(status == 0);
104 for (i = 0; i < 100; i++) {
105 status = pthread_mutex_lock(&mtx);
106 test_assert(status == 0);
107 pthread_yield();
108 pthread_mutex_unlock(&mtx);
109 test_assert(status == 0);
110 }
111 status = pthread_join(thr, &result);
112 test_assert(status == 0);
113 status = pthread_mutex_destroy(&mtx);
114 test_assert(status == 0);
115 printf("OK\n");
116
117 // Condition Variable Test
118 printf("simple condition variable test: ");
119 status = pthread_cond_init(&cnd, NULL);
120 test_assert(status == 0);
121 status = pthread_cond_signal(&cnd);
122 test_assert(status == 0);
123 status = pthread_cond_wait(&cnd, NULL);
124 test_assert(status == 0);
125 status = pthread_cond_destroy(&cnd);
126 test_assert(status == 0);
127 printf("OK\n");
128
129 printf("threaded condition variable test: OK");
130 status = pthread_cond_init(&cnd, NULL);
131 test_assert(status == 0);
132 for (i = 0; i < 100; i++) {
133 status = pthread_cond_signal(&cnd);
134 test_assert(status == 0);
135 status = pthread_cond_wait(&cnd, NULL);
136 test_assert(status == 0);
137 }
138 status = pthread_cond_destroy(&cnd);
139 test_assert(status == 0);
140 printf("\n");
141
142 printf("Success!\n");
143
144 return 0;
145 }
146
147