User code can tell if it is in vcore context (XCC)
[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         assert(!in_vcore_context());
27         for (int i = 0; i < 10; i++) {
28                 printf_safe("[A] pthread %d on vcore %d\n", pthread_self()->id, vcore_id());
29                 pthread_yield();
30                 printf_safe("[A] pthread %d returned from yield on vcore %d\n",
31                             pthread_self()->id, vcore_id());
32         }
33         return (void*)(pthread_self()->id);
34 }
35
36 void *hello_thread(void* arg)
37 {       
38         printf_safe("[A] pthread %d on vcore %d\n", pthread_self()->id, vcore_id());
39         return (void*)(pthread_self()->id);
40 }
41
42 int main(int argc, char** argv) 
43 {
44         void *retval1 = 0;
45         void *retval2 = 0;
46         void *retval3 = 0;
47
48         /* yield test */
49         printf_safe("[A] About to create thread 1\n");
50         pthread_create(&t1, NULL, &yield_thread, NULL);
51         printf_safe("[A] About to create thread 2\n");
52         pthread_create(&t2, NULL, &yield_thread, NULL);
53         printf_safe("[A] About to create thread 3\n");
54         pthread_create(&t3, NULL, &yield_thread, NULL);
55         /* join on them */
56         printf_safe("[A] About to join on thread 1\n");
57         pthread_join(t1, &retval1);
58         printf_safe("[A] Successfully joined on thread 1 (retval: %p)\n", retval1);
59         printf_safe("[A] About to join on thread 2\n");
60         pthread_join(t2, &retval2);
61         printf_safe("[A] Successfully joined on thread 2 (retval: %p)\n", retval2);
62         printf_safe("[A] About to join on thread 3\n");
63         pthread_join(t3, NULL);
64         printf_safe("[A] Successfully joined on thread 3 (retval: %p)\n", retval3);
65
66         /* create and join on hellos */
67         while (1) {
68                 for (int i = 1; i < NUM_TEST_THREADS; i++) {
69                         printf_safe("[A] About to create thread %d\n", i);
70                         pthread_create(&my_threads[i], NULL, &hello_thread, NULL);
71                 }
72                 for (int i = 1; i < NUM_TEST_THREADS; i++) {
73                         printf_safe("[A] About to join on thread %d\n", i);
74                         pthread_join(my_threads[i], &my_retvals[i]);
75                         printf_safe("[A] Successfully joined on thread %d (retval: %p)\n", i,
76                                     my_retvals[i]);
77                 }
78                 break;
79         }
80