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 rwlocktestthread_reader(void *junk, unsigned long num) { int i; (void)junk; unsigned long tv_1; unsigned long tv_2; unsigned long tv_3; for (i=0; i<NLOCKLOOPS; i++) { rwlock_acquire_read(testrwlock); tv_1 = testval1; tv_2 = testval2; tv_3 = testval3; if(tv_1 != testval1){ fail_reader(num, "testval1 changed"); } if(tv_2 != testval2){ fail_reader(num, "testval2 changed"); } if(tv_3 != testval3){ fail_reader(num, "testval3 changed"); } // if (testval2 != testval1*testval1) { // fail_reader(num, "testval2/testval1"); // } // // if (testval2%3 != (testval3*testval3)%3) { // fail_reader(num, "testval2/testval3"); // } // // if (testval3 != testval1%3) { // fail_reader(num, "testval3/testval1"); // } // // if (testval1 != num) { // fail_reader(num, "testval1/num"); // } // // if (testval2 != num*num) { // fail_reader(num, "testval2/num"); // } // // if (testval3 != num%3) { // fail_reader(num, "testval3/num"); // } rwlock_release_read(testrwlock); } V(donesem); }
static void fail_reader(unsigned long num, const char *msg) { kprintf("Reader thread %lu: Mismatch on %s\n", num, msg); kprintf("Test failed\n"); rwlock_release_read(testrwlock); V(donesem); thread_exit(); }
static void readtestthread(void *junk, unsigned long num) { int i; (void)junk; unsigned long localtestval ; int j ; for (i=0; i<NRWLOOPS; i++) { rwlock_acquire_read(testrwlock); kprintf("Lock Acquired for Read Thread %lu \n", num); 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 the lock is acquired. KASSERT(localtestval == testval4); kprintf("Read Thread %lu : Read Value %lu \n", num, testval4); kprintf("Lock Released for Read Thread %lu \n", num); rwlock_release_read(testrwlock); } V(donesem); }