Main worker thread loop.
| 382 | |
| 383 | // Main worker thread loop. |
| 384 | void RunHandlerThreadPool::WorkerLoop(int thread_id, |
| 385 | bool may_steal_blocking_work) { |
| 386 | PerThread* pt = GetPerThread(); |
| 387 | pt->pool = this; |
| 388 | pt->thread_id = thread_id; |
| 389 | static constexpr int32 kMaxBlockingInflight = 10; |
| 390 | |
| 391 | while (!cancelled_) { |
| 392 | Task t; |
| 393 | ThreadWorkSource* tws = nullptr; |
| 394 | bool task_from_blocking_queue = true; |
| 395 | Eigen::MaxSizeVector<ThreadWorkSource*>* thread_work_sources = |
| 396 | &thread_data_[thread_id].thread_work_sources; |
| 397 | { |
| 398 | // The mutex is not hot since its per thread and can only be held |
| 399 | // by some other thread when a session run starts/finishes. |
| 400 | mutex_lock l(thread_data_[thread_id].mu); |
| 401 | |
| 402 | for (int i = 0; i < thread_work_sources->size(); ++i) { |
| 403 | tws = (*thread_work_sources)[i]; |
| 404 | // We want a smallish numbers of inter threads since |
| 405 | // otherwise there will be contention in PropagateOutputs. |
| 406 | // This is best effort policy. |
| 407 | if (may_steal_blocking_work && |
| 408 | tws->GetInflightTaskCount(true) < kMaxBlockingInflight) { |
| 409 | t = tws->PopBlockingTask(); |
| 410 | if (t.f) { |
| 411 | break; |
| 412 | } |
| 413 | } |
| 414 | if (i == 0) { |
| 415 | // Always look for any work from the "primary" work source. |
| 416 | // This way when we wake up a thread for a new closure we are |
| 417 | // guaranteed it can be worked on. |
| 418 | for (int j = 0; j < tws->NonBlockingWorkShardingFactor(); ++j) { |
| 419 | t = tws->PopNonBlockingTask((j + thread_id) % |
| 420 | tws->NonBlockingWorkShardingFactor()); |
| 421 | if (t.f) { |
| 422 | task_from_blocking_queue = false; |
| 423 | break; |
| 424 | } |
| 425 | } |
| 426 | if (t.f) { |
| 427 | break; |
| 428 | } |
| 429 | } else { |
| 430 | t = tws->PopNonBlockingTask(thread_id % |
| 431 | tws->NonBlockingWorkShardingFactor()); |
| 432 | if (t.f) { |
| 433 | task_from_blocking_queue = false; |
| 434 | break; |
| 435 | } |
| 436 | } |
| 437 | } |
| 438 | } |
| 439 | if (t.f) { |
| 440 | profiler::TraceMe activity( |
| 441 | [=] { |
nothing calls this directly
no test coverage detected