| 154 | }; |
| 155 | |
| 156 | void ThreadPool::ExecuteImpl(int task_count, int stride, Task* tasks) { |
| 157 | RUY_DCHECK_GE(task_count, 1); |
| 158 | |
| 159 | // Case of 1 thread: just run the single task on the current thread. |
| 160 | if (task_count == 1) { |
| 161 | (tasks + 0)->Run(); |
| 162 | return; |
| 163 | } |
| 164 | |
| 165 | // Task #0 will be run on the current thread. |
| 166 | CreateThreads(task_count - 1); |
| 167 | counter_to_decrement_when_ready_.Reset(task_count - 1); |
| 168 | for (int i = 1; i < task_count; i++) { |
| 169 | auto task_address = reinterpret_cast<std::uintptr_t>(tasks) + i * stride; |
| 170 | threads_[i - 1]->StartWork(reinterpret_cast<Task*>(task_address)); |
| 171 | } |
| 172 | |
| 173 | // Execute task #0 immediately on the current thread. |
| 174 | (tasks + 0)->Run(); |
| 175 | |
| 176 | // Wait for the threads submitted above to finish. |
| 177 | counter_to_decrement_when_ready_.Wait(); |
| 178 | } |
| 179 | |
| 180 | // Ensures that the pool has at least the given count of threads. |
| 181 | // If any new thread has to be created, this function waits for it to |