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 |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 * GNU General Public License for more details. | 12 * GNU General Public License for more details. |
13 * | 13 * |
14 * You should have received a copy of the GNU General Public License | 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/>. | 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
16 */ | 16 */ |
17 | 17 |
18 #ifndef ADBLOCKPLUS_THREAD_H | 18 #ifndef ADBLOCKPLUS_THREAD_H |
19 #define ADBLOCKPLUS_THREAD_H | 19 #define ADBLOCKPLUS_THREAD_H |
20 | 20 |
| 21 #include <chrono> |
| 22 #include <condition_variable> |
| 23 #include <mutex> |
| 24 #include <string> |
| 25 |
21 #ifdef WIN32 | 26 #ifdef WIN32 |
22 #include <windows.h> | 27 #include <windows.h> |
23 #else | 28 #else |
24 #include <pthread.h> | 29 #include <pthread.h> |
25 #endif | 30 #endif |
26 | 31 |
27 namespace AdblockPlus | 32 namespace AdblockPlus |
28 { | 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 |
29 void Sleep(int millis); | 78 void Sleep(int millis); |
30 | 79 |
31 class Mutex | 80 class Mutex |
32 { | 81 { |
33 public: | 82 public: |
34 #ifdef WIN32 | 83 #ifdef WIN32 |
35 CRITICAL_SECTION nativeMutex; | 84 CRITICAL_SECTION nativeMutex; |
36 #else | 85 #else |
37 pthread_mutex_t nativeMutex; | 86 pthread_mutex_t nativeMutex; |
38 #endif | 87 #endif |
(...skipping 28 matching lines...) Expand all Loading... |
67 #ifdef WIN32 | 116 #ifdef WIN32 |
68 HANDLE nativeThread; | 117 HANDLE nativeThread; |
69 #else | 118 #else |
70 pthread_t nativeThread; | 119 pthread_t nativeThread; |
71 #endif | 120 #endif |
72 bool m_deleteSelfOnFinish; | 121 bool m_deleteSelfOnFinish; |
73 }; | 122 }; |
74 } | 123 } |
75 | 124 |
76 #endif | 125 #endif |
OLD | NEW |