OLD | NEW |
| (Empty) |
1 /* | |
2 * This file is part of Adblock Plus <https://adblockplus.org/>, | |
3 * Copyright (C) 2006-present 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 ADBLOCKPLUS_THREAD_H | |
19 #define ADBLOCKPLUS_THREAD_H | |
20 | |
21 #include <chrono> | |
22 #include <condition_variable> | |
23 #include <mutex> | |
24 #include <string> | |
25 | |
26 #ifdef WIN32 | |
27 #include <windows.h> | |
28 #else | |
29 #include <pthread.h> | |
30 #endif | |
31 | |
32 namespace AdblockPlus | |
33 { | |
34 class Sync | |
35 { | |
36 public: | |
37 Sync() | |
38 : isSet(false) | |
39 { | |
40 } | |
41 void Wait() | |
42 { | |
43 std::unique_lock<std::mutex> lock(mutex); | |
44 if (!isSet) | |
45 cv.wait(lock); | |
46 } | |
47 | |
48 template<class R = typename std::chrono::seconds::rep, | |
49 class P = std::ratio<1>> | |
50 bool WaitFor(const std::chrono::duration<R, P>& duration = std::chrono::seco
nds(20)) | |
51 { | |
52 std::unique_lock<std::mutex> lock(mutex); | |
53 if (!isSet) | |
54 return cv.wait_for(lock, duration) == std::cv_status::no_timeout; | |
55 return true; | |
56 } | |
57 void Set(const std::string& err = std::string()) | |
58 { | |
59 { | |
60 std::unique_lock<std::mutex> lock(mutex); | |
61 isSet = true; | |
62 error = err; | |
63 } | |
64 cv.notify_all(); | |
65 } | |
66 std::string GetError() | |
67 { | |
68 std::unique_lock<std::mutex> lock(mutex); | |
69 return error; | |
70 } | |
71 private: | |
72 std::mutex mutex; | |
73 std::condition_variable cv; | |
74 bool isSet; | |
75 std::string error; | |
76 }; | |
77 | |
78 void Sleep(int millis); | |
79 } | |
80 | |
81 #endif | |
OLD | NEW |