void WorkQueue_Impl::work_completed(WorkItem *item) // transfers ownership { MutexSection mutex_lock(&mutex); finished_items.push_back(item); items_queued.increment(); mutex_lock.unlock(); set_wakeup_event(); }
void WorkQueue_Impl::queue(WorkItem *item) // transfers ownership { if (threads.empty()) { int num_cores = serial_queue ? 1 : std::max(System::get_num_cores() - 1, 1); for (int i = 0; i < num_cores; i++) { Thread thread; thread.start(this, &WorkQueue_Impl::worker_main); threads.push_back(thread); } } MutexSection mutex_lock(&mutex); queued_items.push_back(item); items_queued.increment(); mutex_lock.unlock(); work_available_event.set(); }
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(); }