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: Updated patch after review. Created June 16, 2017, 9:52 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
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
« src/FileSystemJsObject.cpp ('K') | « src/JsEngine.cpp ('k') | test/BaseJsTest.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld