1 // by Jens Gustedt from http://www.openwall.com/lists/musl/2014/08/11/1
2 // c11 threads test was removed and t_error messages were added
3 // the test deadlocks with a broken cond var implementation so
4 // cond_waits were changed to cond_timedwaits with short timeout
14 # define VERSION "POSIX threads"
16 typedef pthread_mutex_t mutex;
17 typedef pthread_cond_t condition;
18 typedef pthread_t thread;
19 typedef void* thread_ret;
21 # define mutex_init(M) pthread_mutex_init((M), 0)
22 # define mutex_destroy pthread_mutex_destroy
23 # define mutex_lock pthread_mutex_lock
24 # define mutex_unlock pthread_mutex_unlock
26 # define condition_init(C) pthread_cond_init((C), 0)
27 # define condition_destroy pthread_cond_destroy
28 # define condition_wait pthread_cond_wait
29 # define condition_timedwait pthread_cond_timedwait
30 # define condition_signal pthread_cond_signal
31 # define condition_broadcast pthread_cond_broadcast
34 # define thread_create(ID, START, ARG) pthread_create(ID, 0, START, ARG)
35 # define thread_join pthread_join
37 # define gettime(TS) clock_gettime(CLOCK_REALTIME, (TS))
39 # define errorstring strerror
42 # define LIBRARY "glibc"
44 # define LIBRARY "unidentified"
47 #define trace2(L, ...) fprintf(stderr, __FILE__ ":" #L ": " __VA_ARGS__)
48 #define trace1(L, ...) trace2(L, __VA_ARGS__)
50 # define trace(...) trace1(__LINE__, __VA_ARGS__)
52 # define trace(...) do { if (0) trace1(__LINE__, __VA_ARGS__); } while (0)
55 //#define tell(...) trace1(__LINE__, __VA_ARGS__)
56 #define tell(...) trace(__VA_ARGS__)
63 static thread id[threads];
64 static unsigned args[threads];
66 static mutex mut[phases];
67 static unsigned inside[phases];
69 static condition cond_client;
70 static condition cond_main;
71 static unsigned volatile phase;
73 static void settimeout(struct timespec *ts)
75 if (clock_gettime(CLOCK_REALTIME, ts))
76 t_error("clock_gettime failed: %s\n", strerror(errno));
77 ts->tv_nsec += 500*1000*1000;
80 static thread_ret client(void *arg) {
82 unsigned * number = arg;
83 for (unsigned i = 0; i < phases; ++i) {
84 trace("thread %u in phase %u\n", *number, i);
87 if (inside[i] == threads) {
88 trace("thread %u is last, signalling main\n", *number);
89 int ret = condition_signal(&cond_main);
90 trace("thread %u is last, signalling main, %s\n", *number, errorstring(ret));
92 t_error("thread %u is last in phase %u, signalling main failed: %s\n", *number, i, errorstring(ret));
95 tell("thread %u in phase %u (%u), waiting\n", *number, i, phase);
97 int ret = condition_timedwait(&cond_client, &mut[i], &ts);
98 trace("thread %u in phase %u (%u), finished, %s\n", *number, i, phase, errorstring(ret));
100 t_error("thread %u in phase %u (%u) finished waiting: %s\n", *number, i, phase, errorstring(ret));
104 int ret = mutex_unlock(&mut[i]);
105 trace("thread %u in phase %u (%u), has unlocked mutex: %s\n", *number, i, phase, errorstring(ret));
107 t_error("thread %u in phase %u (%u), failed to unlock: %s\n", *number, i, phase, errorstring(ret));
116 tell("start up of main, using %s, library %s\n", VERSION, LIBRARY);
117 condition_init(&cond_client);
118 condition_init(&cond_main);
119 for (unsigned i = 0; i < phases; ++i) {
124 for (unsigned i = 0; i < threads; ++i) {
126 thread_create(&id[i], client, &args[i]);
129 while (phase < phases) {
130 while (inside[phase] < threads) {
131 trace("main seeing %u threads in phase %u, waiting\n", inside[phase], phase);
133 int ret = condition_timedwait(&cond_main, &mut[phase], &ts);
134 tell("main seeing %u threads in phase %u, %s\n", inside[phase], phase, errorstring(ret));
136 t_error("main thread in phase %u (%u threads inside), finished waiting: %s\n", phase, inside[phase], errorstring(ret));
140 /* now we know that everybody is waiting inside, lock the next
141 mutex, if any, such that nobody can enter the next phase
142 without our permission. */
143 if (phase < phases-1)
144 mutex_lock(&mut[phase+1]);
145 /* Now signal all clients, update the phase count and release the
146 mutex they are waiting for. */
147 int ret = condition_broadcast(&cond_client);
148 trace("main has broadcast to %u: %s\n", phase, errorstring(ret));
150 t_error("main broadcast in phase %u failed: %s\n", phase, errorstring(ret));
152 ret = mutex_unlock(&mut[phase-1]);
153 trace("main has unlocked mutex %u: %s\n", phase-1, errorstring(ret));
155 t_error("main failed to unlock mutex %u: %s\n", phase-1, errorstring(ret));
160 trace("main finished loop\n");
162 for (unsigned i = 0; i < threads; ++i) {
163 trace("main joining thread %u\n", i);
164 int ret = thread_join(id[i], &(thread_ret){0});
165 trace("main joining thread %u: %s\n", i, errorstring(ret));
167 t_error("main failed join thread %u: %s\n", i, errorstring(ret));
170 /* C functions to destroy the control structures don't return error
171 information, so we can't check for errors, here. */
172 for (unsigned i = 0; i < phases; ++i) {
173 mutex_destroy(&mut[i]);
175 condition_destroy(&cond_main);
176 condition_destroy(&cond_client);
178 tell("shut down of main, using %s, library %s\n", VERSION, LIBRARY);