MPC TLB shootdowns
[akaros.git] / tests / pthread_test.c
index 76e548a..425934c 100644 (file)
@@ -1,41 +1,78 @@
 #include <rstdio.h>
 #include <pthread.h>
+#include <stdlib.h>
+#include <parlib.h>
+#include <unistd.h>
 
 pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
+//#define printf_safe(...) {}
 #define printf_safe(...) \
        pthread_mutex_lock(&lock); \
        printf(__VA_ARGS__); \
        pthread_mutex_unlock(&lock);
 
-int thread_num;
-pthread_t t0;
 pthread_t t1;
 pthread_t t2;
+pthread_t t3;
+
+#define NUM_TEST_THREADS 10
+
+pthread_t my_threads[NUM_TEST_THREADS];
+void *my_retvals[NUM_TEST_THREADS];
 
 __thread int my_id;
-void *thread(void* arg)
+void *yield_thread(void* arg)
 {      
-       my_id = thread_num++;
-       printf_safe("thread %d\n", my_id);
+       for (int i = 0; i < 10; i++) {
+               printf_safe("[A] pthread %d on vcore %d\n", pthread_self()->id, vcore_id());
+               pthread_yield();
+               printf_safe("[A] pthread %d returned from yield on vcore %d\n",
+                           pthread_self()->id, vcore_id());
+       }
+       return (void*)(pthread_self()->id);
+}
+
+void *hello_thread(void* arg)
+{      
+       printf_safe("[A] pthread %d on vcore %d\n", pthread_self()->id, vcore_id());
+       return (void*)(pthread_self()->id);
 }
 
 int main(int argc, char** argv) 
 {
-       thread_num = 0;
-       printf_safe("About to create thread 0\n");
-       pthread_create(&t0, NULL, &thread, NULL);
-       printf("returned from creating him, spinning\n");
-       while(1);
-
-//     printf_safe("About to create thread 1\n");
-//     pthread_create(&t1, NULL, &thread, NULL);
-//     printf_safe("About to create thread 2\n");
-//     pthread_create(&t2, NULL, &thread, NULL);
-
-       printf_safe("About to join on thread 0\n");
-       pthread_join(t0, NULL);
-//     printf_safe("About to join on thread 1\n");
-//     pthread_join(t1, NULL);
-//     printf_safe("About to join on thread 2\n");
-//     pthread_join(t2, NULL);
+       void *retval1 = 0;
+       void *retval2 = 0;
+       void *retval3 = 0;
+
+       /* yield test */
+       printf_safe("[A] About to create thread 1\n");
+       pthread_create(&t1, NULL, &yield_thread, NULL);
+       printf_safe("[A] About to create thread 2\n");
+       pthread_create(&t2, NULL, &yield_thread, NULL);
+       printf_safe("[A] About to create thread 3\n");
+       pthread_create(&t3, NULL, &yield_thread, NULL);
+       /* join on them */
+       printf_safe("[A] About to join on thread 1\n");
+       pthread_join(t1, &retval1);
+       printf_safe("[A] Successfully joined on thread 1 (retval: %p)\n", retval1);
+       printf_safe("[A] About to join on thread 2\n");
+       pthread_join(t2, &retval2);
+       printf_safe("[A] Successfully joined on thread 2 (retval: %p)\n", retval2);
+       printf_safe("[A] About to join on thread 3\n");
+       pthread_join(t3, NULL);
+       printf_safe("[A] Successfully joined on thread 3 (retval: %p)\n", retval3);
+
+       /* create and join on hellos */
+       while (1) {
+               for (int i = 1; i < NUM_TEST_THREADS; i++) {
+                       printf_safe("[A] About to create thread %d\n", i);
+                       pthread_create(&my_threads[i], NULL, &hello_thread, NULL);
+               }
+               for (int i = 1; i < NUM_TEST_THREADS; i++) {
+                       printf_safe("[A] About to join on thread %d\n", i);
+                       pthread_join(my_threads[i], &my_retvals[i]);
+                       printf_safe("[A] Successfully joined on thread %d (retval: %p)\n", i,
+                                   my_retvals[i]);
+               }
+       }
 }