| LEFT | RIGHT |
| 1 /* | 1 /* |
| 2 * This file is part of Adblock Plus <https://adblockplus.org/>, | 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
| 3 * Copyright (C) 2006-2017 eyeo GmbH | 3 * Copyright (C) 2006-2017 eyeo GmbH |
| 4 * | 4 * |
| 5 * Adblock Plus is free software: you can redistribute it and/or modify | 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 | 6 * it under the terms of the GNU General Public License version 3 as |
| 7 * published by the Free Software Foundation. | 7 * published by the Free Software Foundation. |
| 8 * | 8 * |
| 9 * Adblock Plus is distributed in the hope that it will be useful, | 9 * Adblock Plus is distributed in the hope that it will be useful, |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 #include <mutex> | 22 #include <mutex> |
| 23 #include <condition_variable> | 23 #include <condition_variable> |
| 24 #include <queue> | 24 #include <queue> |
| 25 #include <atomic> | 25 #include <atomic> |
| 26 #include <thread> | 26 #include <thread> |
| 27 | 27 |
| 28 namespace AdblockPlus | 28 namespace AdblockPlus |
| 29 { | 29 { |
| 30 class DefaultTimer : public ITimer | 30 class DefaultTimer : public ITimer |
| 31 { | 31 { |
| 32 struct TimerUnit { | 32 struct TimerUnit |
| 33 { |
| 33 std::chrono::steady_clock::time_point fireAt; | 34 std::chrono::steady_clock::time_point fireAt; |
| 34 TimerCallback callback; | 35 TimerCallback callback; |
| 35 }; | 36 }; |
| 36 struct TimerUnitComparator { | 37 struct TimerUnitComparator |
| 37 typedef bool result_type; | 38 { |
| 38 typedef TimerUnit first_argument_type; | 39 bool operator()(const TimerUnit& t1, const TimerUnit& t2) const |
| 39 typedef TimerUnit second_argument_type; | 40 { |
| 40 bool operator()(const first_argument_type& t1, const second_argument_type&
t2) const { | |
| 41 // pay attention 2 < 1 because we need the smallest time at the top. | 41 // pay attention 2 < 1 because we need the smallest time at the top. |
| 42 return t2.fireAt < t1.fireAt; | 42 return t2.fireAt < t1.fireAt; |
| 43 } | 43 } |
| 44 }; | 44 }; |
| 45 typedef std::priority_queue<TimerUnit, std::vector<TimerUnit>, TimerUnitComp
arator> TimerUnits; | 45 typedef std::priority_queue<TimerUnit, std::vector<TimerUnit>, TimerUnitComp
arator> TimerUnits; |
| 46 public: | 46 public: |
| 47 DefaultTimer(); | 47 DefaultTimer(); |
| 48 ~DefaultTimer(); | 48 ~DefaultTimer(); |
| 49 void SetTimer(const std::chrono::milliseconds& timeout, const TimerCallback&
timerCallback) override; | 49 void SetTimer(const std::chrono::milliseconds& timeout, const TimerCallback&
timerCallback) override; |
| 50 private: | 50 private: |
| 51 void ThreadFunc(); | 51 void ThreadFunc(); |
| 52 private: | 52 private: |
| 53 std::mutex mutex; | 53 std::mutex mutex; |
| 54 std::condition_variable conditionVariable; | 54 std::condition_variable conditionVariable; |
| 55 TimerUnits timers; | 55 TimerUnits timers; |
| 56 std::atomic<bool> shouldThreadStop; | 56 std::atomic<bool> shouldThreadStop; |
| 57 std::thread m_thread; | 57 std::thread m_thread; |
| 58 }; | 58 }; |
| 59 } | 59 } |
| 60 | 60 |
| 61 #endif | 61 #endif |
| LEFT | RIGHT |