1ed7a54f99a9f8285abb555525c71cf57f166f20
[akaros.git] / tests / block_test.c
1 #include <stdio.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 *block_thread(void* arg)
25 {       
26         assert(!in_vcore_context());
27         for (int i = 0; i < 1000; i++) {
28                 printf_safe("[A] pthread %d on vcore %d\n", pthread_self()->id, vcore_id());
29                 ros_syscall(SYS_block, 0, 0, 0, 0, 0, 0);
30         }
31         return (void*)(pthread_self()->id);
32 }
33
34 void *hello_thread(void* arg)
35 {       
36         printf_safe("[A] pthread %d on vcore %d\n", pthread_self()->id, vcore_id());
37         return (void*)(pthread_self()->id);
38 }
39
40 int main(int argc, char** argv) 
41 {
42         void *retval1 = 0;
43         void *retval2 = 0;
44         void *retval3 = 0;
45
46         /* yield test */
47         printf_safe("[A] About to create thread 1\n");
48         pthread_create(&t1, NULL, &block_thread, NULL);
49         #if 0
50         /* sys_block can't handle more than one at a time yet */
51         printf_safe("[A] About to create thread 2\n");
52         pthread_create(&t2, NULL, &block_thread, NULL);
53         printf_safe("[A] About to create thread 3\n");
54         pthread_create(&t3, NULL, &block_thread, NULL);
55         #endif
56         /* join on them */
57         printf_safe("[A] About to join on thread 1\n");
58         pthread_join(t1, &retval1);
59         printf_safe("[A] Successfully joined on thread 1 (retval: %p)\n", retval1);
60         #if 0
61         printf_safe("[A] About to join on thread 2\n");
62         pthread_join(t2, &retval2);
63         printf_safe("[A] Successfully joined on thread 2 (retval: %p)\n", retval2);
64         printf_safe("[A] About to join on thread 3\n");
65         pthread_join(t3, NULL);
66         printf_safe("[A] Successfully joined on thread 3 (retval: %p)\n", retval3);
67         #endif
68
69         #if 0
70         /* create and join on hellos */
71         while (1) {
72                 for (int i = 1; i < NUM_TEST_THREADS; i++) {
73                         printf_safe("[A] About to create thread %d\n", i);
74                         pthread_create(&my_threads[i], NULL, &hello_thread, NULL);
75                 }
76                 for (int i = 1; i < NUM_TEST_THREADS; i++) {
77                         printf_safe("[A] About to join on thread %d\n", i);
78                         pthread_join(my_threads[i], &my_retvals[i]);
79                         printf_safe("[A] Successfully joined on thread %d (retval: %p)\n", i,
80                                     my_retvals[i]);
81                 }
82                 break;
83         }
84         #endif
85