akaros/tests/block_test.c
<<
>>
Prefs
   1#include <stdio.h>
   2#include <pthread.h>
   3#include <stdlib.h>
   4#include <parlib/parlib.h>
   5#include <unistd.h>
   6#include <sys/time.h>
   7
   8pthread_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#define NUM_TEST_THREADS 500
  16#define NUM_TEST_LOOPS 1000
  17
  18pthread_t my_threads[NUM_TEST_THREADS];
  19void *my_retvals[NUM_TEST_THREADS];
  20
  21__thread int my_id;
  22void *block_thread(void* arg)
  23{       
  24        assert(!in_vcore_context());
  25        for (int i = 0; i < NUM_TEST_LOOPS; i++) {
  26                printf_safe("[A] pthread %d on vcore %d\n", pthread_self()->id,
  27                            vcore_id());
  28                sys_block(5000 + pthread_self()->id);
  29        }
  30        return (void*)(long)pthread_self()->id;
  31}
  32
  33int main(int argc, char** argv) 
  34{
  35        struct timeval tv = {0};
  36
  37        if (gettimeofday(&tv, 0))
  38                perror("Time error...");
  39        printf("Start time: %dsec %dusec\n", tv.tv_sec, tv.tv_usec);
  40        for (int i = 0; i < NUM_TEST_THREADS; i++) {
  41                printf_safe("[A] About to create thread %d\n", i);
  42                pthread_create(&my_threads[i], NULL, &block_thread, NULL);
  43        }
  44        for (int i = 0; i < NUM_TEST_THREADS; i++) {
  45                printf_safe("[A] About to join on thread %d\n", i);
  46                pthread_join(my_threads[i], &my_retvals[i]);
  47                printf_safe("[A] Successful join on thread %d (retval: %p)\n",
  48                            i, my_retvals[i]);
  49        }
  50        if (gettimeofday(&tv, 0))
  51                perror("Time error...");
  52        printf("End time  : %dsec %dusec\n", tv.tv_sec, tv.tv_usec);
  53        printf("All done, exiting cleanishly\n");
  54} 
  55