#include #include "locks.hfa" #include #include #include #include const unsigned int num_times = 50; simple_owner_lock l; pthread_cond_var( simple_owner_lock ) c; owner_lock l2; condition_variable( owner_lock ) c2; volatile int counter = 0; thread Wait_Signal_1 {}; void main( Wait_Signal_1 & this ) { for (unsigned int i = 0; i < num_times; i++) { lock(l); if(empty(c) && i != num_times - 1) { wait(c,l); }else{ notify_one(c); } unlock(l); } } thread Wait_3_Signal_3 {}; void main( Wait_3_Signal_3 & this ) { for (unsigned int i = 0; i < num_times; i++) { lock(l); counter++; if(counter == 4 || i == num_times - 1) { counter = 0; notify_all(c); }else{ wait(c,l); } unlock(l); } } thread Rec_Lock_Wait_Signal_1 {}; void main( Rec_Lock_Wait_Signal_1 & this ) { for (unsigned int i = 0; i < num_times; i++) { lock(l); lock(l); lock(l); if(empty(c) && i != num_times - 1) { wait(c,l); }else{ notify_one(c); } unlock(l); unlock(l); unlock(l); } } thread Wait_Time_Signal_1 {}; void main( Wait_Time_Signal_1 & this ) { for (unsigned int i = 0; i < num_times; i++) { lock(l); if(empty(c) || random(10) >= 9 ) { timespec t; clock_gettime(CLOCK_REALTIME, &t); timespec waitTime{0,1}; bool woken = wait(c,l, t + waitTime); }else{ notify_one(c); } unlock(l); } } int main() { processor p[1]; printf("Start Test 1: lock and condition variable single wait/notify\n"); { Wait_Signal_1 t1[2]; } printf("Done Test 1\n"); printf("Start Test 2: lock and condition variable 3 wait/notify all\n"); { Wait_3_Signal_3 t1[4]; } printf("Done Test 2\n"); printf("Start Test 3: lock and condition variable multiple acquire and wait/notify\n"); { Rec_Lock_Wait_Signal_1 t1[2]; } printf("Done Test 3\n"); printf("Start Test 4: lock and condition variable single timed wait/notify\n"); { Wait_Time_Signal_1 t1[2]; } printf("Done Test 4\n"); }