static void readerthread(void *junk, unsigned long num) { (void)junk; if(num < 80) { //kprintf_n("Thread %lu trying to acquire read lock\n", num); random_yielder(4); rwlock_acquire_read(testrwlock); random_yielder(4); kprintf_n("Thread %lu: Reader val:%lu\n", num, testval1); for(int i=0; i<40000; i++); random_yielder(4); rwlock_release_read(testrwlock); random_yielder(4); //kprintf_n("Thread %lu released read lock\n", num); V(donesem); } else { //kprintf_n("Thread %lu trying to acquire write lock\n", num); random_yielder(4); rwlock_acquire_write(testrwlock); random_yielder(4); testval1++; kprintf_n("Thread %lu: Writer val:%lu\n", num, testval1); for(int i=0; i<40000; i++); random_yielder(4); rwlock_release_write(testrwlock); random_yielder(4); //kprintf_n("Thread %lu released write lock\n", num); V(donesem); } return; }
static void fail_writer(unsigned long num, const char *msg) { kprintf("Writer thread %lu: Mismatch on %s\n", num, msg); kprintf("Test failed\n"); rwlock_release_write(testrwlock); V(donesem); thread_exit(); }
static void rwlocktestthread_writer(void *junk, unsigned long num) { int i; (void)junk; for (i=0; i<NLOCKLOOPS; i++) { rwlock_acquire_write(testrwlock); testval1 = num; testval2 = num*num; testval3 = num%3; if (testval2 != testval1*testval1) { fail_writer(num, "testval2/testval1"); } if (testval2%3 != (testval3*testval3)%3) { fail_writer(num, "testval2/testval3"); } if (testval3 != testval1%3) { fail_writer(num, "testval3/testval1"); } if (testval1 != num) { fail_writer(num, "testval1/num"); } if (testval2 != num*num) { fail_writer(num, "testval2/num"); } if (testval3 != num%3) { fail_writer(num, "testval3/num"); } rwlock_release_write(testrwlock); } V(donesem); }
static void writetestthread(void *junk, unsigned long num) { int i; (void)junk; int j ; unsigned long localtestval ; for (i=0; i<NRWLOOPS; i++) { rwlock_acquire_write(testrwlock); kprintf("Lock Acquired for Write Thread %lu \n", num); testval4++ ; localtestval = testval4; //Empty loop to pass time. No other thread should modify the value in this time. for(j=0 ; j<3000 ;j++) ; //assert operation to verify whether the value has been changed after this thread has modified it. KASSERT(localtestval == testval4); kprintf("Lock Released for Write Thread %lu \n", num); rwlock_release_write(testrwlock); } V(donesem); }