Cosmetic changes, biggest being current_thread renamed to current_uthread
[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 #include <vcore.h>
7
8 pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
9 #define printf_safe(...) {}
10 //#define printf_safe(...) \
11         pthread_mutex_lock(&lock); \
12         printf(__VA_ARGS__); \
13         pthread_mutex_unlock(&lock);
14
15 pthread_t t1;
16 pthread_t t2;
17 pthread_t t3;
18
19 #define NUM_TEST_THREADS 100
20
21 pthread_t my_threads[NUM_TEST_THREADS];
22 void *my_retvals[NUM_TEST_THREADS];
23
24 __thread int my_id;
25 void *yield_thread(void* arg)
26 {       
27         for (int i = 0; i < 100; i++) {
28                 printf_safe("[A] pthread %p on vcore %d, itr: %d\n", pthread_self(), vcore_id(), i);
29                 pthread_yield();
30                 printf_safe("[A] pthread %p returned from yield on vcore %d, itr: %d\n",
31                             pthread_self(), vcore_id(), i);
32         }
33         return (void*)(pthread_self());
34 }
35
36 void *hello_thread(void* arg)
37 {       
38         printf_safe("[A] pthread %p on vcore %d\n", pthread_self(), vcore_id());
39         return (void*)(pthread_self());
40 }
41
42 //int main(int argc, char** argv) 
43 //{
44 //      void *retval1 = 0;
45 //      printf_safe("[A] About to create thread 1\n");
46 //      pthread_create(&t1, NULL, &yield_thread, NULL);
47 //      printf_safe("[A] About to join on thread 1\n");
48 //      pthread_join(t1, &retval1);
49 //}
50
51 int main(int argc, char** argv) 
52 {
53         void *retval1 = 0;
54         void *retval2 = 0;
55         void *retval3 = 0;
56
57         /* yield test */
58         printf_safe("[A] About to create thread 1\n");
59         pthread_create(&t1, NULL, &yield_thread, NULL);
60         printf_safe("[A] About to create thread 2\n");
61         pthread_create(&t2, NULL, &yield_thread, NULL);
62         printf_safe("[A] About to create thread 3\n");
63         pthread_create(&t3, NULL, &yield_thread, NULL);
64         /* join on them */
65         printf_safe("[A] About to join on thread 1\n");
66         pthread_join(t1, &retval1);
67         printf_safe("[A] Successfully joined on thread 1 (retval: %p)\n", retval1);
68         printf_safe("[A] About to join on thread 2\n");
69         pthread_join(t2, &retval2);
70         printf_safe("[A] Successfully joined on thread 2 (retval: %p)\n", retval2);
71         printf_safe("[A] About to join on thread 3\n");
72         pthread_join(t3, &retval3);
73         printf_safe("[A] Successfully joined on thread 3 (retval: %p)\n", retval3);
74
75         /* create and join on hellos */
76         for (int i = 0; i < NUM_TEST_THREADS; i++) {
77                 printf_safe("[A] About to create thread %d\n", i);
78                 pthread_create(&my_threads[i], NULL, &yield_thread, NULL);
79         }
80         for (int i = 0; i < NUM_TEST_THREADS; i++) {
81                 printf_safe("[A] About to join on thread %d\n", i);
82                 pthread_join(my_threads[i], &my_retvals[i]);
83                 printf_safe("[A] Successfully joined on thread %d (retval: %p)\n", i,
84                             my_retvals[i]);
85         }
86         printf("Exiting cleanly!\n");
87