summaryrefslogtreecommitdiff
path: root/src/mbgl/actor
diff options
context:
space:
mode:
authorMikhail Pozdnyakov <mikhail.pozdnyakov@mapbox.com>2019-11-27 23:41:30 +0200
committerMikhail Pozdnyakov <mikhail.pozdnyakov@mapbox.com>2019-11-28 14:34:52 +0200
commitb5794730b5cfba58be0aa63070718cde662bb538 (patch)
treeed56f988fda080dfff63040ebee10391468a9708 /src/mbgl/actor
parent316f1d0ad19459c9494b874a05707b45cb14db72 (diff)
downloadqtlocation-mapboxgl-b5794730b5cfba58be0aa63070718cde662bb538.tar.gz
[core] Introduce Scheduler::GetSequenced() API
The newly introduced `Scheduler::GetSequenced()` returns sequenced schedulers from the cache limited to 10 instances, preventing from spawning too many threads.
Diffstat (limited to 'src/mbgl/actor')
-rw-r--r--src/mbgl/actor/scheduler.cpp27
1 files changed, 27 insertions, 0 deletions
diff --git a/src/mbgl/actor/scheduler.cpp b/src/mbgl/actor/scheduler.cpp
index 81e259fe1f..0e051d0273 100644
--- a/src/mbgl/actor/scheduler.cpp
+++ b/src/mbgl/actor/scheduler.cpp
@@ -41,4 +41,31 @@ std::shared_ptr<Scheduler> Scheduler::GetBackground() {
return scheduler;
}
+// static
+std::shared_ptr<Scheduler> Scheduler::GetSequenced() {
+ const std::size_t kSchedulersCount = 10;
+ static std::vector<std::weak_ptr<Scheduler>> weaks(kSchedulersCount);
+ static std::mutex mtx;
+ static std::size_t lastUsedIndex = 0u;
+
+ std::lock_guard<std::mutex> lock(mtx);
+
+ if (++lastUsedIndex == kSchedulersCount) lastUsedIndex = 0u;
+
+ std::shared_ptr<Scheduler> result;
+ for (std::size_t i = 0; i < kSchedulersCount; ++i) {
+ auto& weak = weaks[i];
+ if (auto scheduler = weak.lock()) {
+ if (lastUsedIndex == i) result = scheduler;
+ continue;
+ }
+ result = std::make_shared<SequencedScheduler>();
+ weak = result;
+ lastUsedIndex = i;
+ break;
+ }
+
+ return result;
+}
+
} //namespace mbgl