Left: | ||
Right: |
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 : set(false) | |
39 { | |
40 } | |
41 template<class R = typename std::chrono::seconds::rep, | |
42 class P = std::ratio<1>> | |
43 void Wait(const std::chrono::duration<R, P>& duration = std::chrono::seconds (20)) | |
44 { | |
45 std::unique_lock<std::mutex> lock(mutex); | |
46 while (!set) | |
47 { | |
48 if (duration == std::chrono::duration<R>::zero()) | |
49 cv.wait(lock); | |
50 else | |
51 cv.wait_for(lock, duration); | |
52 } | |
53 } | |
sergei
2017/07/03 09:25:56
Zero seems even less appropriate here but OK, I th
hub
2017/07/04 19:58:27
I wasn't super thrilled about it either.
Maybe we
sergei
2017/07/05 10:03:20
I thought about it too, it actually looks very goo
| |
54 void Set(const std::string& err = std::string()) | |
55 { | |
56 { | |
57 std::unique_lock<std::mutex> lock(mutex); | |
58 set = true; | |
59 error = err; | |
60 } | |
61 cv.notify_all(); | |
62 } | |
63 std::string GetError() | |
64 { | |
65 std::unique_lock<std::mutex> lock(mutex); | |
66 return error; | |
67 } | |
68 private: | |
69 std::mutex mutex; | |
70 std::condition_variable cv; | |
71 bool set; | |
sergei
2017/07/03 09:25:55
would it be better to call it as `isSet`?
hub
2017/07/04 19:58:27
sure
| |
72 std::string error; | |
73 }; | |
74 | |
29 void Sleep(int millis); | 75 void Sleep(int millis); |
30 | 76 |
31 class Mutex | 77 class Mutex |
32 { | 78 { |
33 public: | 79 public: |
34 #ifdef WIN32 | 80 #ifdef WIN32 |
35 CRITICAL_SECTION nativeMutex; | 81 CRITICAL_SECTION nativeMutex; |
36 #else | 82 #else |
37 pthread_mutex_t nativeMutex; | 83 pthread_mutex_t nativeMutex; |
38 #endif | 84 #endif |
(...skipping 28 matching lines...) Expand all Loading... | |
67 #ifdef WIN32 | 113 #ifdef WIN32 |
68 HANDLE nativeThread; | 114 HANDLE nativeThread; |
69 #else | 115 #else |
70 pthread_t nativeThread; | 116 pthread_t nativeThread; |
71 #endif | 117 #endif |
72 bool m_deleteSelfOnFinish; | 118 bool m_deleteSelfOnFinish; |
73 }; | 119 }; |
74 } | 120 } |
75 | 121 |
76 #endif | 122 #endif |
OLD | NEW |