Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: src/DefaultTimer.h

Issue 29395640: Issue 3595 - Get rid of detached threads for setTimeout (Closed)
Patch Set: address comment Created March 27, 2017, 2:42 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « libadblockplus.gyp ('k') | src/DefaultTimer.cpp » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/DefaultTimer.h
diff --git a/src/DefaultTimer.h b/src/DefaultTimer.h
new file mode 100644
index 0000000000000000000000000000000000000000..91ec09cb967cbd75980930c5778fc6e158b30b5c
--- /dev/null
+++ b/src/DefaultTimer.h
@@ -0,0 +1,61 @@
+/*
+* This file is part of Adblock Plus <https://adblockplus.org/>,
+* Copyright (C) 2006-2017 eyeo GmbH
+*
+* Adblock Plus is free software: you can redistribute it and/or modify
+* it under the terms of the GNU General Public License version 3 as
+* published by the Free Software Foundation.
+*
+* Adblock Plus is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+* GNU General Public License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#ifndef ADBLOCK_PLUS_DEFAULT_TIMER_H
+#define ADBLOCK_PLUS_DEFAULT_TIMER_H
+
+#include <AdblockPlus/ITimer.h>
+#include <mutex>
+#include <condition_variable>
+#include <queue>
+#include <atomic>
+#include <thread>
+
+namespace AdblockPlus
+{
+ class DefaultTimer : public ITimer
+ {
+ struct TimerUnit {
+ std::chrono::steady_clock::time_point fireAt;
+ TimerCallback callback;
+ };
+ struct TimerUnitComparator {
+ typedef bool result_type;
+ typedef TimerUnit first_argument_type;
+ typedef TimerUnit second_argument_type;
+ bool operator()(const first_argument_type& t1, const second_argument_type& t2) const {
+ // pay attention 2 < 1 because we need the smallest time at the top.
+ return t2.fireAt < t1.fireAt;
+ }
+ };
+ typedef std::priority_queue<TimerUnit, std::vector<TimerUnit>, TimerUnitComparator> TimerUnits;
+ public:
+ DefaultTimer();
+ ~DefaultTimer();
+ void SetTimer(const std::chrono::milliseconds& timeout, const TimerCallback& timerCallback) override;
+ private:
+ void ThreadFunc();
+ private:
+ std::mutex mutex;
+ std::condition_variable conditionVariable;
+ TimerUnits timers;
+ std::atomic<bool> shouldThreadStop;
+ std::thread m_thread;
+ };
+}
+
+#endif
« no previous file with comments | « libadblockplus.gyp ('k') | src/DefaultTimer.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld