4 #include <parlib/parlib.h>
8 pthread_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);
15 #define NUM_TEST_THREADS 500
16 #define NUM_TEST_LOOPS 1000
18 pthread_t my_threads[NUM_TEST_THREADS];
19 void *my_retvals[NUM_TEST_THREADS];
22 void *block_thread(void* arg)
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, vcore_id());
27 sys_block(5000 + pthread_self()->id);
29 return (void*)(long)pthread_self()->id;
32 int main(int argc, char** argv)
34 struct timeval tv = {0};
35 if (gettimeofday(&tv, 0))
36 perror("Time error...");
37 printf("Start time: %dsec %dusec\n", tv.tv_sec, tv.tv_usec);
38 for (int i = 0; i < NUM_TEST_THREADS; i++) {
39 printf_safe("[A] About to create thread %d\n", i);
40 pthread_create(&my_threads[i], NULL, &block_thread, NULL);
42 for (int i = 0; i < NUM_TEST_THREADS; i++) {
43 printf_safe("[A] About to join on thread %d\n", i);
44 pthread_join(my_threads[i], &my_retvals[i]);
45 printf_safe("[A] Successfully joined on thread %d (retval: %p)\n", i,
48 if (gettimeofday(&tv, 0))
49 perror("Time error...");
50 printf("End time : %dsec %dusec\n", tv.tv_sec, tv.tv_usec);
51 printf("All done, exiting cleanishly\n");