11 #define __int(ptr) ((int)(unsigned long)(ptr))
12 #define __ptr(ptr) ((void*)(unsigned long)(ptr))
15 __print_and_sleep_randsec(void* value)
17 pthread_t tid = pthread_self();
18 printf("thread %d: gets number %d\n", tid, __int(value));
20 int fd = open("/dev/rand", O_RDONLY | O_DIRECT);
22 printf("thread %d: failed to get random source: %d\n", tid, errno);
26 unsigned char rand = 0;
27 if (read(fd, &rand, sizeof(rand)) != 1) {
28 printf("thread %d: failed to get random number: %d\n", tid, errno);
35 printf("thread %d: going to sleep %ds\n", tid, rand);
38 printf("thread %d: exit\n", tid);
43 __print_and_sleep_seq(void* value)
45 pthread_t tid = pthread_self();
46 printf("thread %d: gets number %d\n", tid, __int(value));
48 int second = __int(value) % 30;
50 printf("thread %d: going to sleep %ds\n", tid, second);
53 printf("thread %d: exit\n", tid);
58 __print_and_sleep(void* value)
60 pthread_t tid = pthread_self();
61 printf("thread %d: gets number %d\n", tid, __int(value));
64 printf("thread %d: exit\n", tid);
68 long long __counter_shared = 0;
71 __inc_number(void* value)
73 for (int i = 0; i < 10000000; i++)
78 printf("thread %d: exit\n", pthread_self());
83 __spawn_and_quit(void* value)
86 printf("thread %d: exit\n", pthread_self());
95 spawn_detached_thread(void* (*fn)(void *), int amount)
100 for (int i = 0; i < amount; i++) {
101 err = pthread_create(&created, NULL, fn, __ptr(i));
103 printf("unable to create thread: %d\n", err);
106 if((err = pthread_detach(created))) {
107 printf("failed to detach: %d\n", err);
109 printf("created %d-th\n", i);
115 pthread_test_rand_sleep(int param)
117 printf("spawning %d threads\n", param);
118 spawn_detached_thread(__print_and_sleep_randsec, param);
119 // wait for max 30 seconds
120 printf("wait for completion\n");
125 pthread_test_seq_sleep(int param)
127 printf("spawning %d threads\n", param);
128 spawn_detached_thread(__print_and_sleep_seq, param);
129 // wait for max 30 seconds
130 printf("wait for completion\n");
135 pthread_test_join(int param)
140 for (int i = 0; i < param; i++)
142 err = pthread_create(&created, NULL, __print_and_sleep, __ptr(i));
144 printf("unable to create thread: %d\n", err);
147 pthread_join(created, &v);
153 pthread_test_shared_race(int param)
155 __counter_shared = 0;
157 spawn_detached_thread(__inc_number, param);
160 printf("counter val: %ld\n", __counter_shared);
164 pthread_test_quit(int param)
166 spawn_detached_thread(__spawn_and_quit, param);
171 #define run_test(testn, note, ...) \
173 printf("** [%s] test start\n", note); \
174 pthread_test_##testn(__VA_ARGS__); \
175 printf("** [%s] test passed\n", note); \
180 run_test(rand_sleep, "rand_sleep5", 5);
181 run_test(rand_sleep, "rand_sleep10", 10);
182 run_test(rand_sleep, "rand_sleep50", 50);
184 run_test(seq_sleep, "seq_sleep50", 50);
185 run_test(seq_sleep, "seq_sleep100", 100);
186 run_test(seq_sleep, "seq_sleep200", 200);
188 run_test(join, "join5", 5);
189 run_test(join, "join20", 20);
191 run_test(quit, "quit10", 10);
192 run_test(quit, "quit50", 50);
193 run_test(quit, "quit100", 100);
195 // FIXME not good, this panic the kernel upon exit, need investigate
196 run_test(shared_race, "shared_race10", 10);
197 run_test(shared_race, "shared_race40", 40);
199 // TODO test pthread + signal
200 printf("All test passed.\n");