Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: src/Thread.h

Issue 29449592: Issue 5183 - Provide async interface for FileSystem (Closed) Base URL: https://hg.adblockplus.org/libadblockplus/
Patch Set: Rebase on master. Last changes. Created July 7, 2017, 1:36 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « src/JsValue.cpp ('k') | src/Utils.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/Thread.h
===================================================================
--- a/src/Thread.h
+++ b/src/Thread.h
@@ -13,24 +13,73 @@
*
* 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()
+ : isSet(false)
+ {
+ }
+ void Wait()
+ {
+ std::unique_lock<std::mutex> lock(mutex);
+ if (!isSet)
+ cv.wait(lock);
+ }
+
+ template<class R = typename std::chrono::seconds::rep,
+ class P = std::ratio<1>>
+ bool WaitFor(const std::chrono::duration<R, P>& duration = std::chrono::seconds(20))
+ {
+ std::unique_lock<std::mutex> lock(mutex);
+ if (!isSet)
+ return cv.wait_for(lock, duration) == std::cv_status::no_timeout;
+ return true;
+ }
+ void Set(const std::string& err = std::string())
+ {
+ {
+ std::unique_lock<std::mutex> lock(mutex);
+ isSet = 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 isSet;
+ std::string error;
+ };
+
void Sleep(int millis);
class Mutex
{
public:
#ifdef WIN32
CRITICAL_SECTION nativeMutex;
#else
« no previous file with comments | « src/JsValue.cpp ('k') | src/Utils.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld