Skip to content
Snippets Groups Projects
Commit c4d0547a authored by Adrien Béraud's avatar Adrien Béraud
Browse files

scheduled_executor: make cancel/isCancelled non-blocking

Change-Id: Icddfa964f7980af5915b110745cb922adc10dba4
parent ff5ed9d3
Branches
No related tags found
No related merge requests found
......@@ -304,7 +304,7 @@ JamiAccount::~JamiAccount()
{
shutdownConnections();
if (eventHandler) {
eventHandler->cancel();
eventHandler->destroy();
eventHandler.reset();
}
if(peerDiscovery_){
......@@ -3091,17 +3091,17 @@ JamiAccount::getLastMessages(const uint64_t& base_timestamp)
void
JamiAccount::checkPendingCallsTask()
{
bool hasHandler = eventHandler and not eventHandler->isCancelled();
if (not pendingCalls_.empty() and not hasHandler) {
eventHandler = Manager::instance().scheduler().scheduleAtFixedRate([w = weak()] {
decltype(eventHandler) handler;
if (not pendingCalls_.empty()) {
handler = Manager::instance().scheduler().scheduleAtFixedRate([w = weak()] {
if (auto this_ = w.lock())
return this_->handlePendingCallList();
return false;
}, std::chrono::milliseconds(10));
} else if (pendingCalls_.empty() and hasHandler) {
eventHandler->cancel();
eventHandler.reset();
}
std::swap(handler, eventHandler);
if (handler)
handler->cancel();
}
void
......
......@@ -68,22 +68,28 @@ public:
RepeatedTask(RepeatedJob&& j) : job_(std::move(j)) {}
bool run() {
std::lock_guard<std::mutex> l(lock_);
if (job_ and not job_())
if (cancel_.load() or (job_ and not job_())) {
cancel_.store(true);
job_ = {};
}
return (bool)job_;
}
void cancel() {
cancel_.store(true);
}
void destroy() {
cancel();
std::lock_guard<std::mutex> l(lock_);
job_ = {};
}
bool isCancelled() const {
std::lock_guard<std::mutex> l(lock_);
return !job_;
return cancel_.load();
}
private:
NON_COPYABLE(RepeatedTask);
RepeatedJob job_;
mutable std::mutex lock_;
RepeatedJob job_;
std::atomic_bool cancel_ {false};
};
class ScheduledExecutor {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment