OLD | NEW |
(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 "PausePoint.h" |
| 19 #include <thread> |
| 20 |
| 21 PausePoint::PausePoint() |
| 22 : state(Idle) |
| 23 {} |
| 24 |
| 25 void PausePoint::Pause() |
| 26 { |
| 27 UniqueLockType ul(m); |
| 28 if (state == Waiting) |
| 29 { |
| 30 cv.notify_one(); |
| 31 } |
| 32 state = Paused; |
| 33 cv.wait(ul, |
| 34 [this]() -> bool |
| 35 { |
| 36 return state != Paused; |
| 37 }); |
| 38 } |
| 39 |
| 40 void PausePoint::WaitUntilPaused() |
| 41 { |
| 42 UniqueLockType ul(m); |
| 43 if (state == Paused) |
| 44 { |
| 45 return; |
| 46 } |
| 47 state = Waiting; |
| 48 cv.wait(ul, |
| 49 [this]() -> bool |
| 50 { |
| 51 return state == Paused; |
| 52 }); |
| 53 } |
| 54 |
| 55 void PausePoint::Resume() |
| 56 { |
| 57 UniqueLockType ul(m); |
| 58 if (state != Paused) |
| 59 { |
| 60 throw std::runtime_error("May not call Resume() when the puppet is not block
ed."); |
| 61 } |
| 62 state = Idle; |
| 63 cv.notify_one(); |
| 64 } |
| 65 |
| 66 void PausePoint::ResumeAndSleep(unsigned int ms) |
| 67 { |
| 68 Resume(); |
| 69 std::this_thread::sleep_for(std::chrono::milliseconds(ms)); |
| 70 } |
OLD | NEW |