void ThreadPoolTaskExecutor::cancel(const CallbackHandle& cbHandle) { invariant(cbHandle.isValid()); auto cbState = checked_cast<CallbackState*>(getCallbackFromHandle(cbHandle)); stdx::unique_lock<stdx::mutex> lk(_mutex); cbState->canceled.store(1); if (cbState->isNetworkOperation) { lk.unlock(); _net->cancelCommand(cbHandle); return; } if (cbState->readyDate != Date_t{}) { // This callback might still be in the sleeper queue; if it is, schedule it now // rather than when the alarm fires. auto iter = std::find_if(_sleepersQueue.begin(), _sleepersQueue.end(), [cbState](const std::shared_ptr<CallbackState>& other) { return cbState == other.get(); }); if (iter != _sleepersQueue.end()) { invariant(iter == cbState->iter); scheduleIntoPool_inlock(&_sleepersQueue, cbState->iter); } } }
StatusWith<TaskExecutor::CallbackHandle> ThreadPoolTaskExecutor::scheduleWorkAt( Date_t when, const CallbackFn& work) { if (when <= now()) { return scheduleWork(work); } stdx::lock_guard<stdx::mutex> lk(_mutex); auto cbHandle = enqueueCallbackState_inlock(&_sleepersQueue, work, when); if (!cbHandle.isOK()) { return cbHandle; } _net->setAlarm(when, [this, when, cbHandle] { auto cbState = checked_cast<CallbackState*>(getCallbackFromHandle(cbHandle.getValue())); if (cbState->canceled.load()) { return; } invariant(now() >= when); stdx::lock_guard<stdx::mutex> lk(_mutex); scheduleIntoPool_inlock(&_sleepersQueue, cbState->iter); }); return cbHandle; }
void ThreadPoolTaskExecutor::wait(const CallbackHandle& cbHandle) { invariant(cbHandle.isValid()); auto cbState = checked_cast<CallbackState*>(getCallbackFromHandle(cbHandle)); waitForEvent(cbState->finishedEvent); }
void TaskExecutor::wait(const CallbackHandle& cbHandle) { getCallbackFromHandle(cbHandle)->waitForCompletion(); };
void TaskExecutor::cancel(const CallbackHandle& cbHandle) { getCallbackFromHandle(cbHandle)->cancel(); };