void InterlockCheck::start(int num_iterations) { iteration_count[0].set(num_iterations/2); iteration_count[1].set(num_iterations - iteration_count[0].get()); write_check.set(num_iterations); for (int i = 0; i < 2; i++) { reader_events[i].set(); } }
void TestApp::test_interlock() { Console::write_line(" Header: interlocked_variable.h"); Console::write_line(" Class: InterlockedVariable"); Console::write_line(" Function: InterlockedVariable()"); { InterlockedVariable var; var.set(123); if (var.get() != 123) fail(); } Console::write_line(" Function: InterlockedVariable(const InterlockedVariable &src)"); { InterlockedVariable var; var.set(123); InterlockedVariable var2(var); var.set(333); if (var2.get() != 123) fail(); } Console::write_line(" Function: InterlockedVariable &operator =(const InterlockedVariable &src)"); { InterlockedVariable var; var.set(123); InterlockedVariable var2 = var; var.set(333); if (var2.get() != 123) fail(); } Console::write_line(" Function: LONG get() const"); { InterlockedVariable var; var.set(13); var.set(123); if (var.get() != 123) fail(); if (var.get() != 123) fail(); var.set(-555); if (var.get() != -555) fail(); if (var.get() != -555) fail(); } Console::write_line(" Function: void set(LONG new_value)"); { InterlockedVariable var; var.set(13); if (var.get() != 13) fail(); var.set(0); if (var.get() != 0) fail(); var.set(-13); if (var.get() != -13) fail(); } Console::write_line(" Function: LONG increment()"); { InterlockedVariable var; var.set(123); var.increment(); if (var.get() != 124) fail(); var.set(-123); var.increment(); if (var.get() != -122) fail(); } Console::write_line(" Function: LONG decrement()"); { InterlockedVariable var; var.set(123); var.decrement(); if (var.get() != 122) fail(); var.set(-123); var.decrement(); if (var.get() != -124) fail(); } Console::write_line(" Function: bool compare_and_swap(LONG expected_value, LONG new_value)"); { InterlockedVariable var; var.set(123); if (!var.compare_and_swap(123, 456)) fail(); if (var.get() != 456) fail(); if (var.compare_and_swap(123, 789)) fail(); if (var.get() != 456) fail(); } Console::write_line(" Thread Checking"); InterlockCheck check; for (int check_cnt = 0; check_cnt < 1024; check_cnt++) { check.start(20000); check.wait_for_workers(); if (check.write_check.get() != 0) fail(); } if (check.run_count.get() != (1024*2)) fail(); }