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: Created March 27, 2017, 10:26 a.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..095af1bf382707329a1f42d5a023a4deeb3e0f8d
--- /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 becaus we need the smallest time at the top.
hub 2017/03/27 13:42:22 'nit: typo in the comment: "because"
sergei 2017/03/27 14:45:25 Done.
+ 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