OLD | NEW |
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 12 matching lines...) Expand all Loading... |
23 : shouldThreadStop(false) | 23 : shouldThreadStop(false) |
24 { | 24 { |
25 m_thread = std::thread([this] | 25 m_thread = std::thread([this] |
26 { | 26 { |
27 ThreadFunc(); | 27 ThreadFunc(); |
28 }); | 28 }); |
29 } | 29 } |
30 | 30 |
31 DefaultTimer::~DefaultTimer() | 31 DefaultTimer::~DefaultTimer() |
32 { | 32 { |
33 shouldThreadStop = true; | 33 { |
| 34 std::lock_guard<std::mutex> lock(mutex); |
| 35 shouldThreadStop = true; |
| 36 } |
34 conditionVariable.notify_one(); | 37 conditionVariable.notify_one(); |
35 if (m_thread.joinable()) | 38 if (m_thread.joinable()) |
36 m_thread.join(); | 39 m_thread.join(); |
37 } | 40 } |
38 | 41 |
39 void DefaultTimer::SetTimer(const std::chrono::milliseconds& timeout, const Time
rCallback& timerCallback) | 42 void DefaultTimer::SetTimer(const std::chrono::milliseconds& timeout, const Time
rCallback& timerCallback) |
40 { | 43 { |
41 if (!timerCallback) | 44 if (!timerCallback) |
42 return; | 45 return; |
43 std::lock_guard<std::mutex> lock(mutex); | 46 std::lock_guard<std::mutex> lock(mutex); |
44 TimerUnit timer = { std::chrono::steady_clock::now() + timeout, timerCallback
}; | 47 TimerUnit timer = { std::chrono::steady_clock::now() + timeout, timerCallback
}; |
45 timers.push(timer); | 48 timers.push(timer); |
46 conditionVariable.notify_one(); | 49 conditionVariable.notify_one(); |
47 } | 50 } |
48 | 51 |
49 void DefaultTimer::ThreadFunc() | 52 void DefaultTimer::ThreadFunc() |
50 { | 53 { |
51 while (!shouldThreadStop) | 54 while (true) |
52 { | 55 { |
53 std::unique_lock<std::mutex> lock(mutex); | 56 std::unique_lock<std::mutex> lock(mutex); |
54 if (timers.empty()) | 57 if (timers.empty()) |
55 { | 58 { |
56 conditionVariable.wait(lock, [this]()->bool | 59 conditionVariable.wait(lock, [this]()->bool |
57 { | 60 { |
58 return shouldThreadStop || !timers.empty(); | 61 return shouldThreadStop || !timers.empty(); |
59 }); | 62 }); |
60 } | 63 } |
61 else | 64 else |
(...skipping 10 matching lines...) Expand all Loading... |
72 try | 75 try |
73 { | 76 { |
74 callback(); | 77 callback(); |
75 } | 78 } |
76 catch (...) | 79 catch (...) |
77 { | 80 { |
78 // do nothing, but the thread will be alive. | 81 // do nothing, but the thread will be alive. |
79 } | 82 } |
80 lock.lock(); | 83 lock.lock(); |
81 } | 84 } |
| 85 if (shouldThreadStop) |
| 86 return; |
82 } | 87 } |
83 } | 88 } |
OLD | NEW |