| 209 | } |
| 210 | |
| 211 | void LegacyThreadPool::pushTask(const std::function<void(int)> &runnable, |
| 212 | TaskType type /* = DEFAULT*/) { |
| 213 | if (!_isFixedSize) { |
| 214 | _idleThreadNumMutex.lock(); |
| 215 | int idleNum = _idleThreadNum; |
| 216 | _idleThreadNumMutex.unlock(); |
| 217 | |
| 218 | if (idleNum > _minThreadNum) { |
| 219 | if (_taskQueue.empty()) { |
| 220 | auto now = std::chrono::high_resolution_clock::now(); |
| 221 | float seconds = TIME_MINUS(now, _lastShrinkTime); |
| 222 | if (seconds > _shrinkInterval) { |
| 223 | tryShrinkPool(); |
| 224 | _lastShrinkTime = now; |
| 225 | } |
| 226 | } |
| 227 | } else if (idleNum == 0) { |
| 228 | stretchPool(_stretchStep); |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | auto callback = ccnew std::function<void(int)>([runnable](int tid) { |
| 233 | runnable(tid); |
| 234 | }); |
| 235 | |
| 236 | Task task; |
| 237 | task.type = type; |
| 238 | task.callback = callback; |
| 239 | _taskQueue.push(task); |
| 240 | |
| 241 | { |
| 242 | std::unique_lock<std::mutex> lock(_mutex); |
| 243 | _cv.notify_one(); |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | void LegacyThreadPool::stopAllTasks() { |
| 248 | Task task; |
no test coverage detected