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

Unified Diff: include/AdblockPlus/SynchronizedCollection.h

Issue 29706560: Issue 5179 - Implement asynchronous executor with a controllable lifetime (Closed) Base URL: https://github.com/adblockplus/libadblockplus.git
Patch Set: Created Feb. 23, 2018, 10:29 a.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: include/AdblockPlus/SynchronizedCollection.h
diff --git a/include/AdblockPlus/SynchronizedCollection.h b/include/AdblockPlus/SynchronizedCollection.h
new file mode 100644
index 0000000000000000000000000000000000000000..85f6c9cefb55060b652905ac3083c975911c4a9b
--- /dev/null
+++ b/include/AdblockPlus/SynchronizedCollection.h
@@ -0,0 +1,81 @@
+/*
+ * This file is part of Adblock Plus <https://adblockplus.org/>,
+ * Copyright (C) 2006-present eyeo GmbH
+ *
+ * Adblock Plus is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 3 as
+ * published by the Free Software Foundation.
+ *
+ * Adblock Plus is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
+ */
+#pragma once
+#include <mutex>
+#include <condition_variable>
+
+namespace AdblockPlus
+{
+ /**
+ * Wrapper around `Container` providing few generic methods which ensure
+ * that the underlying container is accessed only by one thread at the same
+ * time.
+ */
+ template<typename TContainer>
+ class SynchronizedCollection
+ {
+ protected:
+ typedef TContainer Container;
+ public:
+ /**
+ * The `value_type` represents the type of stored values.
+ */
+ typedef typename Container::value_type value_type;
+
+ /**
+ * Adds `value` normally to the end.
+ * @param value which is stored.
+ */
+ void push_back(const value_type& value)
+ {
+ {
+ std::lock_guard<std::mutex> lock(mutex);
+ collection.push_back(value);
+ }
+ conditionVar.notify_one();
+ }
+ void push_back(value_type&& value)
+ {
+ {
+ std::lock_guard<std::mutex> lock(mutex);
+ collection.push_back(std::move(value));
+ }
+ conditionVar.notify_one();
+ }
+
+ /**
+ * Extracts normally a firtst of currently stored elements and returns it.
hub 2018/02/23 22:59:35 I think the comment should read "Extracts the firs
sergei 2018/03/01 11:18:37 Yeah, perhaps it's better, done.
+ * Pay attention that the call of this method blocks the execution until
+ * there is at least one element added to the collection.
+ */
+ value_type pop_front()
+ {
+ std::unique_lock<std::mutex> lock(mutex);
+ conditionVar.wait(lock, [this]()->bool
+ {
+ return !collection.empty();
+ });
+ value_type retValue = collection.front();
+ collection.pop_front();
+ return retValue;
+ }
+ protected:
+ Container collection;
+ std::mutex mutex;
+ std::condition_variable conditionVar;
+ };
+}

Powered by Google App Engine
This is Rietveld