Adds sys_mkdir() and sys_rmdir() (XCC)
[akaros.git] / tests / pthread_barrier_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 #define NUM_TEST_THREADS 32
15 pthread_t my_threads[NUM_TEST_THREADS];
16 void *my_retvals[NUM_TEST_THREADS];
17 pthread_barrier_t barrier;
18
19 void *thread(void* arg)
20 {       
21         for(int i=0; i<NUM_TEST_THREADS; i++) {
22                 //printf_safe("[A] pthread %d on vcore %d\n", pthread_self()->id, vcore_id());
23                 pthread_barrier_wait(&barrier);
24         }
25         return (void*)(pthread_self()->id);
26 }
27
28 int main(int argc, char** argv) 
29 {
30         pthread_barrier_init(&barrier, NULL, NUM_TEST_THREADS);
31         #define NUM_ITERATIONS 5000
32         for(int j=0; j<NUM_ITERATIONS; j++) {
33 //      while (1) {
34                 for (int i = 1; i <= NUM_TEST_THREADS; i++) {
35                         pthread_create(&my_threads[i-1], NULL, &thread, NULL);
36                 }
37                 for (int i = 1; i <= NUM_TEST_THREADS; i++) {
38                         pthread_join(my_threads[i-1], &my_retvals[i-1]);
39                 }
40                 printf("Iteration %d of %d\n", j, NUM_ITERATIONS);
41         }
42         pthread_barrier_destroy(&barrier);
43         sys_proc_destroy(getpid(), 0);
44