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

Side by Side Diff: src/DefaultTimer.h

Issue 29395640: Issue 3595 - Get rid of detached threads for setTimeout (Closed)
Patch Set: only rebase Created March 27, 2017, 2:44 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
(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 std::chrono::steady_clock::time_point fireAt;
34 TimerCallback callback;
35 };
36 struct TimerUnitComparator {
37 typedef bool result_type;
38 typedef TimerUnit first_argument_type;
39 typedef TimerUnit second_argument_type;
40 bool operator()(const first_argument_type& t1, const second_argument_type& t2) const {
Oleksandr 2017/03/28 10:39:55 I don't see why we need to use typedef'd type here
sergei 2017/03/28 11:06:59 It seems we can, it simply was not working before.
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
OLDNEW

Powered by Google App Engine
This is Rietveld