Native: fix losing Worker.executeAfter jobs on scheduled time clash

The implementation was keeping delayed jobs in std::set sorted
only by the scheduled execution time (in microseconds since epoch).
So two jobs submitted to a worker and having the same scheduled time
were considered equivalent by the set, and one of them got lost.

Fix this by using std::multiset instead of std::set.
This commit is contained in:
Svyatoslav Scherbina
2021-11-26 11:59:40 +00:00
committed by Space
parent 95648f1a9e
commit 24e7a11abc
3 changed files with 65 additions and 2 deletions
+1 -1
View File
@@ -77,7 +77,7 @@ using KStdUnorderedSet = std::unordered_set<Value,
std::hash<Value>, std::equal_to<Value>,
KonanAllocator<Value>>;
template<class Value, class Compare = std::less<Value>>
using KStdOrderedSet = std::set<Value, Compare, KonanAllocator<Value>>;
using KStdOrderedMultiset = std::multiset<Value, Compare, KonanAllocator<Value>>;
template<class Key, class Value, class Compare = std::less<Key>>
using KStdOrderedMap = std::map<Key, Value, Compare, KonanAllocator<std::pair<const Key, Value>>>;
template<class Value>
@@ -130,7 +130,10 @@ struct JobCompare {
}
};
typedef KStdOrderedSet<Job, JobCompare> DelayedJobSet;
// Using multiset instead of regular set, because we compare the jobs only by `whenExecute`.
// So if `whenExecute` of two different jobs is the same, the jobs are considered equivalent,
// and set would simply drop one of them.
typedef KStdOrderedMultiset<Job, JobCompare> DelayedJobSet;
} // namespace
@@ -966,6 +969,9 @@ KLong Worker::checkDelayedLocked() {
RuntimeAssert(job.kind == JOB_EXECUTE_AFTER, "Must be delayed job");
auto now = konan::getTimeMicros();
if (job.executeAfter.whenExecute <= now) {
// Note: `delayed_` is multiset sorted only by `whenExecute`.
// So using erase(it) instead of erase(job) is crucial,
// because the latter would remove all the jobs with the same `whenExecute`.
delayed_.erase(it);
queue_.push_back(job);
return 0;