Left: | ||
Right: |
LEFT | RIGHT |
---|---|
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 17 matching lines...) Expand all Loading... | |
28 #else | 28 #else |
29 #include <pthread.h> | 29 #include <pthread.h> |
30 #endif | 30 #endif |
31 | 31 |
32 namespace AdblockPlus | 32 namespace AdblockPlus |
33 { | 33 { |
34 class Sync | 34 class Sync |
35 { | 35 { |
36 public: | 36 public: |
37 Sync() | 37 Sync() |
38 : set(false) | 38 : isSet(false) |
39 { | 39 { |
40 } | 40 } |
41 void Wait() | |
42 { | |
43 std::unique_lock<std::mutex> lock(mutex); | |
44 if (!isSet) | |
45 cv.wait(lock); | |
46 } | |
47 | |
41 template<class R = typename std::chrono::seconds::rep, | 48 template<class R = typename std::chrono::seconds::rep, |
42 class P = std::ratio<1>> | 49 class P = std::ratio<1>> |
43 void Wait(const std::chrono::duration<R, P>& duration = std::chrono::seconds (20)) | 50 bool WaitFor(const std::chrono::duration<R, P>& duration = std::chrono::seco nds(20)) |
44 { | 51 { |
45 std::unique_lock<std::mutex> lock(mutex); | 52 std::unique_lock<std::mutex> lock(mutex); |
46 while (!set) | 53 if (!isSet) |
47 { | 54 return cv.wait_for(lock, duration) == std::cv_status::no_timeout; |
48 if (duration == std::chrono::duration<R>::zero()) | 55 return true; |
49 cv.wait(lock); | |
50 else | |
51 cv.wait_for(lock, duration); | |
52 } | |
53 } | 56 } |
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()) | 57 void Set(const std::string& err = std::string()) |
55 { | 58 { |
56 { | 59 { |
57 std::unique_lock<std::mutex> lock(mutex); | 60 std::unique_lock<std::mutex> lock(mutex); |
58 set = true; | 61 isSet = true; |
59 error = err; | 62 error = err; |
60 } | 63 } |
61 cv.notify_all(); | 64 cv.notify_all(); |
62 } | 65 } |
63 std::string GetError() | 66 std::string GetError() |
64 { | 67 { |
65 std::unique_lock<std::mutex> lock(mutex); | 68 std::unique_lock<std::mutex> lock(mutex); |
66 return error; | 69 return error; |
67 } | 70 } |
68 private: | 71 private: |
69 std::mutex mutex; | 72 std::mutex mutex; |
70 std::condition_variable cv; | 73 std::condition_variable cv; |
71 bool set; | 74 bool isSet; |
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; | 75 std::string error; |
73 }; | 76 }; |
74 | 77 |
75 void Sleep(int millis); | 78 void Sleep(int millis); |
76 | 79 |
77 class Mutex | 80 class Mutex |
78 { | 81 { |
79 public: | 82 public: |
80 #ifdef WIN32 | 83 #ifdef WIN32 |
81 CRITICAL_SECTION nativeMutex; | 84 CRITICAL_SECTION nativeMutex; |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
113 #ifdef WIN32 | 116 #ifdef WIN32 |
114 HANDLE nativeThread; | 117 HANDLE nativeThread; |
115 #else | 118 #else |
116 pthread_t nativeThread; | 119 pthread_t nativeThread; |
117 #endif | 120 #endif |
118 bool m_deleteSelfOnFinish; | 121 bool m_deleteSelfOnFinish; |
119 }; | 122 }; |
120 } | 123 } |
121 | 124 |
122 #endif | 125 #endif |
LEFT | RIGHT |