vcore_id() is now implemented using TLS
[akaros.git] / tests / pthread_test.c
1 #include <rstdio.h>
2 #include <pthread.h>
3 #include <stdlib.h>
4 #include <parlib.h>
5 #include <unistd.h>
6
7 pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
8 #define printf_safe(...) {}
9 //#define printf_safe(...) \
10         pthread_mutex_lock(&lock); \
11         printf(__VA_ARGS__); \
12         pthread_mutex_unlock(&lock);
13
14 pthread_t t1;
15 pthread_t t2;
16 pthread_t t3;
17
18 #define NUM_TEST_THREADS 1000
19
20 pthread_t my_threads[NUM_TEST_THREADS];
21 void *my_retvals[NUM_TEST_THREADS];
22
23 __thread int my_id;
24 void *yield_thread(void* arg)
25 {       
26         for (int i = 0; i < 10; i++) {
27                 printf_safe("[A] pthread %d on vcore %d\n", pthread_self()->id, vcore_id());
28                 pthread_yield();
29                 printf_safe("[A] pthread %d returned from yield on vcore %d\n",
30                             pthread_self()->id, vcore_id());
31         }
32         return (void*)(pthread_self()->id);
33 }
34
35 void *hello_thread(void* arg)
36 {       
37         printf_safe("[A] pthread %d on vcore %d\n", pthread_self()->id, vcore_id());
38         return (void*)(pthread_self()->id);
39 }
40
41 int main(int argc, char** argv) 
42 {
43         void *retval1 = 0;
44         void *retval2 = 0;
45         void *retval3 = 0;
46
47         /* yield test */
48         printf_safe("[A] About to create thread 1\n");
49         pthread_create(&t1, NULL, &yield_thread, NULL);
50         printf_safe("[A] About to create thread 2\n");
51         pthread_create(&t2, NULL, &yield_thread, NULL);
52         printf_safe("[A] About to create thread 3\n");
53         pthread_create(&t3, NULL, &yield_thread, NULL);
54         /* join on them */
55         printf_safe("[A] About to join on thread 1\n");
56         pthread_join(t1, &retval1);
57         printf_safe("[A] Successfully joined on thread 1 (retval: %p)\n", retval1);
58         printf_safe("[A] About to join on thread 2\n");
59         pthread_join(t2, &retval2);
60         printf_safe("[A] Successfully joined on thread 2 (retval: %p)\n", retval2);
61         printf_safe("[A] About to join on thread 3\n");
62         pthread_join(t3, NULL);
63         printf_safe("[A] Successfully joined on thread 3 (retval: %p)\n", retval3);
64
65         /* create and join on hellos */
66         while (1) {
67                 for (int i = 1; i < NUM_TEST_THREADS; i++) {
68                         printf_safe("[A] About to create thread %d\n", i);
69                         pthread_create(&my_threads[i], NULL, &hello_thread, NULL);
70                 }
71                 for (int i = 1; i < NUM_TEST_THREADS; i++) {
72                         printf_safe("[A] About to join on thread %d\n", i);
73                         pthread_join(my_threads[i], &my_retvals[i]);
74                         printf_safe("[A] Successfully joined on thread %d (retval: %p)\n", i,
75                                     my_retvals[i]);
76                 }
77                 break;
78         }
79