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

Side by Side Diff: src/DefaultTimer.cpp

Issue 29395640: Issue 3595 - Get rid of detached threads for setTimeout (Closed)
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:
View unified diff | Download patch
« no previous file with comments | « src/DefaultTimer.h ('k') | src/GlobalJsObject.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 #include "DefaultTimer.h"
19
20 using AdblockPlus::DefaultTimer;
21
22 DefaultTimer::DefaultTimer()
23 : shouldThreadStop(false)
24 {
25 m_thread = std::thread([this]
26 {
27 ThreadFunc();
28 });
29 }
30
31 DefaultTimer::~DefaultTimer()
32 {
33 shouldThreadStop = true;
34 conditionVariable.notify_one();
35 if (m_thread.joinable())
36 m_thread.join();
37 }
38
39 void DefaultTimer::SetTimer(const std::chrono::milliseconds& timeout, const Time rCallback& timerCallback)
40 {
41 if (!timerCallback)
42 return;
43 std::lock_guard<std::mutex> lock(mutex);
44 TimerUnit timer = { std::chrono::steady_clock::now() + timeout, timerCallback };
45 timers.push(timer);
46 conditionVariable.notify_one();
47 }
48
49 void DefaultTimer::ThreadFunc()
50 {
51 while (!shouldThreadStop)
52 {
53 std::unique_lock<std::mutex> lock(mutex);
54 if (timers.empty())
55 {
56 conditionVariable.wait(lock, [this]()->bool
57 {
58 return shouldThreadStop || !timers.empty();
59 });
60 }
61 else
62 {
63 conditionVariable.wait_until(lock, timers.top().fireAt);
64 }
65 // execute all expired timers and remove them
66 while (!shouldThreadStop && !timers.empty() && timers.top().fireAt <= std::c hrono::steady_clock::now())
67 {
68 auto callback = timers.top().callback;
69 timers.pop();
70 // allow to put new timers while this timer is being processed
71 lock.unlock();
72 try
73 {
74 callback();
75 }
76 catch (...)
77 {
78 // do nothing, but the thread will be alive.
79 }
80 lock.lock();
81 }
82 }
83 }
OLDNEW
« no previous file with comments | « src/DefaultTimer.h ('k') | src/GlobalJsObject.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld