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

Delta Between Two Patch Sets: src/DefaultTimer.h

Issue 29395640: Issue 3595 - Get rid of detached threads for setTimeout (Closed)
Left Patch Set: Created March 27, 2017, 10:26 a.m.
Right Patch Set: address comments Created March 28, 2017, 11:06 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « libadblockplus.gyp ('k') | src/DefaultTimer.cpp » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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
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 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.
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
LEFTRIGHT

Powered by Google App Engine
This is Rietveld