OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
| 3 * Copyright (C) 2006-2017 eyeo GmbH |
| 4 * |
| 5 * Adblock Plus is free software: you can redistribute it and/or modify |
| 6 * it under the terms of the GNU General Public License version 3 as |
| 7 * published by the Free Software Foundation. |
| 8 * |
| 9 * Adblock Plus is distributed in the hope that it will be useful, |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 * GNU General Public License for more details. |
| 13 * |
| 14 * You should have received a copy of the GNU General Public License |
| 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
| 16 */ |
| 17 |
| 18 #ifndef ADBLOCK_PLUS_DEFAULT_TIMER_H |
| 19 #define ADBLOCK_PLUS_DEFAULT_TIMER_H |
| 20 |
| 21 #include <AdblockPlus/ITimer.h> |
| 22 #include <mutex> |
| 23 #include <condition_variable> |
| 24 #include <queue> |
| 25 #include <atomic> |
| 26 #include <thread> |
| 27 |
| 28 namespace AdblockPlus |
| 29 { |
| 30 class DefaultTimer : public ITimer |
| 31 { |
| 32 struct TimerUnit |
| 33 { |
| 34 std::chrono::steady_clock::time_point fireAt; |
| 35 TimerCallback callback; |
| 36 }; |
| 37 struct TimerUnitComparator |
| 38 { |
| 39 bool operator()(const TimerUnit& t1, const TimerUnit& t2) const |
| 40 { |
| 41 // pay attention 2 < 1 because we need the smallest time at the top. |
| 42 return t2.fireAt < t1.fireAt; |
| 43 } |
| 44 }; |
| 45 typedef std::priority_queue<TimerUnit, std::vector<TimerUnit>, TimerUnitComp
arator> TimerUnits; |
| 46 public: |
| 47 DefaultTimer(); |
| 48 ~DefaultTimer(); |
| 49 void SetTimer(const std::chrono::milliseconds& timeout, const TimerCallback&
timerCallback) override; |
| 50 private: |
| 51 void ThreadFunc(); |
| 52 private: |
| 53 std::mutex mutex; |
| 54 std::condition_variable conditionVariable; |
| 55 TimerUnits timers; |
| 56 std::atomic<bool> shouldThreadStop; |
| 57 std::thread m_thread; |
| 58 }; |
| 59 } |
| 60 |
| 61 #endif |
OLD | NEW |