void WorkQueue_Impl::process() { MutexSection mutex_lock(&mutex); std::vector<WorkItem *> items; items.swap(finished_items); mutex_lock.unlock(); for (size_t i = 0; i < items.size(); i++) { try { items[i]->work_completed(); } catch (...) { mutex_lock.lock(); finished_items.insert(finished_items.begin(), items.begin() + i, items.end()); throw; } delete items[i]; items_queued.decrement(); } }
void InterlockCheck::worker_main(int core) { while (true) { int wakeup_reason = Event::wait(reader_events[core], event_stop); if (wakeup_reason != 0) break; run_count.increment(); reader_events[core].reset(); while(true) { write_check.decrement(); iteration_count[core].decrement(); if (iteration_count[core].get() == 0) break; } writer_event.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(); }