MCPcopy Create free account
hub / github.com/apache/arrow / WorkerLoop

Function WorkerLoop

cpp/src/arrow/util/thread_pool.cc:449–521  ·  view source on GitHub ↗

The worker loop is an independent function so that it can keep running after the ThreadPool is destroyed.

Source from the content-addressed store, hash-verified

447// The worker loop is an independent function so that it can keep running
448// after the ThreadPool is destroyed.
449static void WorkerLoop(std::shared_ptr<ThreadPool::State> state,
450 std::list<std::thread>::iterator it) {
451 std::unique_lock<std::mutex> lock(state->mutex_);
452
453 // Since we hold the lock, `it` now points to the correct thread object
454 // (LaunchWorkersUnlocked has exited)
455 DCHECK_EQ(std::this_thread::get_id(), it->get_id());
456
457 // If too many threads, we should secede from the pool
458 const auto should_secede = [&]() -> bool {
459 return state->workers_.size() > static_cast<size_t>(state->desired_capacity_);
460 };
461
462 while (true) {
463 // By the time this thread is started, some tasks may have been pushed
464 // or shutdown could even have been requested. So we only wait on the
465 // condition variable at the end of the loop.
466
467 // Execute pending tasks if any
468 while (!state->pending_tasks_.empty() && !state->quick_shutdown_) {
469 // We check this opportunistically at each loop iteration since
470 // it releases the lock below.
471 if (should_secede()) {
472 break;
473 }
474
475 DCHECK_GE(state->tasks_queued_or_running_, 0);
476 {
477 Task task = std::move(const_cast<Task&>(state->pending_tasks_.top().task));
478 state->pending_tasks_.pop();
479 StopToken* stop_token = &task.stop_token;
480 lock.unlock();
481 if (!stop_token->IsStopRequested()) {
482 std::move(task.callable)();
483 } else {
484 if (task.stop_callback) {
485 std::move(task.stop_callback)(stop_token->Poll());
486 }
487 }
488 {
489 auto tmp_task = std::move(task); // release resources before waiting for lock
490 ARROW_UNUSED(tmp_task);
491 }
492 lock.lock();
493 }
494 if (ARROW_PREDICT_FALSE(--state->tasks_queued_or_running_ == 0)) {
495 state->cv_idle_.notify_all();
496 }
497 }
498 // Now either the queue is empty *or* a quick shutdown was requested
499 if (state->please_shutdown_ || should_secede()) {
500 break;
501 }
502 // Wait for next wakeup
503 state->cv_.wait(lock);
504 }
505 DCHECK_GE(state->tasks_queued_or_running_, 0);
506

Callers 1

LaunchWorkersUnlockedMethod · 0.85

Calls 5

IsStopRequestedMethod · 0.80
PollMethod · 0.80
push_backMethod · 0.80
sizeMethod · 0.45
emptyMethod · 0.45

Tested by

no test coverage detected