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