bool wait_queue_t::wait(duration_t* timeout) { if(FIBER_IMPL) { fiber_waiter_t waiter(FIBER_IMPL, SCHEDULER_IMPL); waiters_.push_back(waiter); auto wait_res = SCHEDULER_IMPL->wait_queue(lock_, timeout); waiters_.erase(waiters_.iterator_to(waiter)); if(waiter.wakeup_next) notify_one(); return wait_res == scheduler_impl_t::READY; } else { native_waiter_t waiter; waiter.queue_lock = lock_; waiters_.push_back(waiter); bool wait_successfull = waiter.wait(timeout); waiters_.erase(waiters_.iterator_to(waiter)); if(waiter.wakeup_next) notify_one(); return wait_successfull; } }
template<typename... Args> distributor& operator()(Args&&... args) { unique_lock lock { *this }; while (queue.size() == threads.size()) wait(lock); queue.emplace(std::forward<Args>(args)...); notify_one(); }
void ConqurrentQueue::push(value_type const& data) { auto aQueue = static_cast<std::queue<value_type>*>(_queue); auto aMutex = static_cast<std::mutex*>(_mutex); auto aConditionVariable = static_cast<std::condition_variable*>(_conditionVariable); std::unique_lock<std::mutex> lock(*aMutex); aQueue->push(data); lock.unlock(); aConditionVariable->notify_one(); }
void consume(Function process) { unique_lock lock { *this }; while (true) { if (not queue.empty()) { Type item { std::move(queue.front()) }; queue.pop(); notify_one(); lock.unlock(); process(item); lock.lock(); } else if (done) { break; } else { wait(lock); } } }
void SetUp() override { StorageManager::get().reset(); _table_a = load_table("resources/test_data/tbl/int_float.tbl", 2); StorageManager::get().add_table("table_a", _table_a); // Set scheduler so that the server can execute the tasks on separate threads. CurrentScheduler::set(std::make_shared<NodeQueueScheduler>()); uint16_t server_port = 0; std::mutex mutex{}; auto cv = std::make_shared<std::condition_variable>(); auto server_runner = [&, cv](boost::asio::io_service& io_service) { Server server{io_service, /* port = */ 0}; // run on port 0 so the server can pick a free one { std::unique_lock<std::mutex> lock{mutex}; server_port = server.get_port_number(); } cv->notify_one(); io_service.run(); }; _io_service = std::make_unique<boost::asio::io_service>(); _server_thread = std::make_unique<std::thread>(server_runner, std::ref(*_io_service)); // We need to wait here for the server to have started so we can get its port, which must be set != 0 { std::unique_lock<std::mutex> lock{mutex}; cv->wait(lock, [&] { return server_port != 0; }); } // Get randomly assigned port number for client connection _connection_string = "hostaddr=127.0.0.1 port=" + std::to_string(server_port); }
void notify() { notify_one(); }