12 __print_and_sleep_randsec(void* value)
14 pthread_t tid = pthread_self();
15 printf("thread %d: gets number %d\n", tid, (int)value);
17 int fd = open("/dev/rand", O_RDONLY | O_DIRECT);
19 printf("thread %d: failed to get random source: %d\n", tid, errno);
23 unsigned char rand = 0;
24 if (read(fd, &rand, sizeof(rand)) != 1) {
25 printf("thread %d: failed to get random number: %d\n", tid, errno);
32 printf("thread %d: going to sleep %ds\n", tid, rand);
35 printf("thread %d: exit\n", tid);
40 __print_and_sleep_seq(void* value)
42 pthread_t tid = pthread_self();
43 printf("thread %d: gets number %d\n", tid, (int)value);
45 int second = (int)value % 30;
47 printf("thread %d: going to sleep %ds\n", tid, second);
50 printf("thread %d: exit\n", tid);
55 __print_and_sleep(void* value)
57 pthread_t tid = pthread_self();
58 printf("thread %d: gets number %d\n", tid, (int)value);
61 printf("thread %d: exit\n", tid);
65 long long __counter_shared = 0;
68 __inc_number(void* value)
70 for (int i = 0; i < 10000000; i++)
75 printf("thread %d: exit\n", pthread_self());
80 __spawn_and_quit(void* value)
83 printf("thread %d: exit\n", pthread_self());
92 spawn_detached_thread(void* (*fn)(void *), int amount)
97 for (int i = 0; i < amount; i++) {
98 err = pthread_create(&created, NULL, fn, (void*)i);
100 printf("unable to create thread: %d\n", err);
103 if((err = pthread_detach(created))) {
104 printf("failed to detach: %d\n", err);
106 printf("created %d-th\n", i);
112 pthread_test_rand_sleep(int param)
114 printf("spawning %d threads\n", param);
115 spawn_detached_thread(__print_and_sleep_randsec, param);
116 // wait for max 30 seconds
117 printf("wait for completion\n");
122 pthread_test_seq_sleep(int param)
124 printf("spawning %d threads\n", param);
125 spawn_detached_thread(__print_and_sleep_seq, param);
126 // wait for max 30 seconds
127 printf("wait for completion\n");
132 pthread_test_join(int param)
137 for (int i = 0; i < param; i++)
139 err = pthread_create(&created, NULL, __print_and_sleep, (void*)i);
141 printf("unable to create thread: %d\n", err);
144 pthread_join(created, &v);
150 pthread_test_shared_race(int param)
152 __counter_shared = 0;
154 spawn_detached_thread(__inc_number, param);
157 printf("counter val: %ld\n", __counter_shared);
161 pthread_test_quit(int param)
163 spawn_detached_thread(__spawn_and_quit, param);
168 #define run_test(testn, note, ...) \
170 printf("** [%s] test start\n", note); \
171 pthread_test_##testn(__VA_ARGS__); \
172 printf("** [%s] test passed\n"); \
177 run_test(rand_sleep, "rand_sleep5", 5);
178 run_test(rand_sleep, "rand_sleep10", 10);
179 run_test(rand_sleep, "rand_sleep50", 50);
181 run_test(seq_sleep, "seq_sleep50", 50);
182 run_test(seq_sleep, "seq_sleep100", 100);
183 run_test(seq_sleep, "seq_sleep200", 200);
185 run_test(join, "join5", 5);
186 run_test(join, "join20", 20);
188 run_test(quit, "quit10", 10);
189 run_test(quit, "quit50", 50);
190 run_test(quit, "quit100", 100);
192 // FIXME not good, this panic the kernel upon exit, need investigate
193 run_test(shared_race, "shared_race10", 10);
194 run_test(shared_race, "shared_race40", 40);
196 // TODO test pthread + signal
197 printf("All test passed.\n");