Index: src/Thread.h |
=================================================================== |
--- a/src/Thread.h |
+++ b/src/Thread.h |
@@ -13,24 +13,70 @@ |
* |
* You should have received a copy of the GNU General Public License |
* along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
*/ |
#ifndef ADBLOCKPLUS_THREAD_H |
#define ADBLOCKPLUS_THREAD_H |
+#include <chrono> |
+#include <condition_variable> |
+#include <mutex> |
+#include <string> |
+ |
#ifdef WIN32 |
#include <windows.h> |
#else |
#include <pthread.h> |
#endif |
namespace AdblockPlus |
{ |
+ class Sync |
+ { |
+ public: |
+ Sync() |
+ : set(false) |
+ { |
+ } |
+ template<class R = typename std::chrono::seconds::rep, |
+ class P = std::ratio<1>> |
+ void Wait(const std::chrono::duration<R, P>& duration = std::chrono::seconds(20)) |
+ { |
+ std::unique_lock<std::mutex> lock(mutex); |
+ while (!set) |
+ { |
+ if (duration == std::chrono::duration<R>::zero()) |
+ cv.wait(lock); |
+ else |
+ cv.wait_for(lock, duration); |
+ } |
+ } |
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
|
+ void Set(const std::string& err = std::string()) |
+ { |
+ { |
+ std::unique_lock<std::mutex> lock(mutex); |
+ set = true; |
+ error = err; |
+ } |
+ cv.notify_all(); |
+ } |
+ std::string GetError() |
+ { |
+ std::unique_lock<std::mutex> lock(mutex); |
+ return error; |
+ } |
+ private: |
+ std::mutex mutex; |
+ std::condition_variable cv; |
+ 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
|
+ std::string error; |
+ }; |
+ |
void Sleep(int millis); |
class Mutex |
{ |
public: |
#ifdef WIN32 |
CRITICAL_SECTION nativeMutex; |
#else |