| 76 | } |
| 77 | |
| 78 | void ThreadPool::AddTaskInternal( |
| 79 | Closure<void()>* callback, |
| 80 | const std::function<void()>& function, |
| 81 | int dispatch_key) |
| 82 | { |
| 83 | DCHECK(!m_exit); |
| 84 | ThreadContext& context = m_thread_contexts[dispatch_key % m_num_threads]; |
| 85 | { |
| 86 | Task* task; |
| 87 | MutexLocker locker(&context.mutex); |
| 88 | if (!context.free_tasks.empty()) { |
| 89 | task = &context.free_tasks.front(); |
| 90 | context.free_tasks.pop_front(); |
| 91 | task->callback = callback; |
| 92 | task->function = function; |
| 93 | } else { |
| 94 | task = new Task(callback, function); |
| 95 | } |
| 96 | |
| 97 | context.pending_tasks.push_back(task); |
| 98 | context.cond.Signal(); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | void ThreadPool::AddTask(Closure<void()>* callback) { |
| 103 | // The memory address is random enough for load balance, but need |