Handles syscall-event overflow in pthreads
[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 #define NUM_TEST_THREADS 20
15 #define NUM_TEST_LOOPS 1000
16
17 pthread_t my_threads[NUM_TEST_THREADS];
18 void *my_retvals[NUM_TEST_THREADS];
19
20 __thread int my_id;
21 void *block_thread(void* arg)
22 {       
23         assert(!in_vcore_context());
24         for (int i = 0; i < NUM_TEST_LOOPS; i++) {
25                 printf_safe("[A] pthread %d on vcore %d\n", pthread_self()->id, vcore_id());
26                 sys_block(5000 + pthread_self()->id);
27         }
28         return (void*)(pthread_self()->id);
29 }
30
31 int main(int argc, char** argv) 
32 {
33         for (int i = 0; i < NUM_TEST_THREADS; i++) {
34                 printf_safe("[A] About to create thread %d\n", i);
35                 pthread_create(&my_threads[i], NULL, &block_thread, NULL);
36         }
37         for (int i = 0; i < NUM_TEST_THREADS; i++) {
38                 printf_safe("[A] About to join on thread %d\n", i);
39                 pthread_join(my_threads[i], &my_retvals[i]);
40                 printf_safe("[A] Successfully joined on thread %d (retval: %p)\n", i,
41                             my_retvals[i]);
42         }
43         printf("All done, exiting cleanishly\n");
44