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

Unified Diff: src/plugin/Instances.h

Issue 29333107: Issue #1652, #3456 - Replace 's_asyncWebBrowser2' with thread-safe facilities
Patch Set: rebase only Created July 27, 2016, 10:26 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 | « no previous file | src/plugin/PluginClass.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/plugin/Instances.h
===================================================================
--- a/src/plugin/Instances.h
+++ b/src/plugin/Instances.h
@@ -15,11 +15,12 @@
* along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
*/
-#ifndef _INSTANCES_H_
+#ifndef _INSTANCES_H_
#define _INSTANCES_H_
+#include <map>
#include <mutex>
-#include <map>
+#include <set>
/**
* A base class for a synchronized map from threads to BHO instances.
@@ -96,4 +97,92 @@
}
};
+/**
+ * Template class for a synchronized set of BHO instances
+ */
+template<class T, T nullValue>
+class SyncSet
+{
+ typedef std::lock_guard<std::mutex> SentryType;
+
+ /**
+ * Underlying set container
+ */
+ std::set<T> set;
+
+ /**
+ * Synchronization primitive
+ */
+ mutable std::mutex mutex;
+
+public:
+ /**
+ * \return
+ * true if (as expected) the argument was not already a member of the set
+ * false otherwise
+ *
+ * \par Postcondition
+ * - argument is a member of the set
+ */
+ bool InsertIfAbsent(T p)
+ {
+ SentryType sentry(mutex);
+ auto it = set.find(p);
+ if (it != set.end())
+ {
+ return false;
+ }
+ set.insert(p);
+ return true;
+ }
+
+ /**
+ * \return
+ * true if (as expected) the argument was already a member of the set
+ * false otherwise
+ *
+ * \par Postcondition
+ * - argument is not a member of the set
+ */
+ bool EraseWithCheck(T p)
+ {
+ SentryType sentry(mutex);
+ auto it = set.find(p);
+ if (it == set.end())
+ {
+ return false;
+ }
+ set.erase(it);
+ return true;
+ }
+
+ /**
+ * \param f
+ * Search criterion is a simple predicate function
+ *
+ * \return
+ * - If no member of the set meets the search criterion, the null value
+ * - Otherwise some member of the set that meets the search criterion
+ */
+ T LinearSearch(std::function<bool(T)> f)
+ {
+ SentryType sentry(mutex);
+ for (auto member : set)
+ {
+ bool b = f(member);
+ if (b)
+ {
+ return member;
+ }
+ }
+ return nullValue;
+ }
+
+ bool Empty() const
+ {
+ SentryType sentry(mutex);
+ return set.empty();
+ }
+};
+
#endif
« no previous file with comments | « no previous file | src/plugin/PluginClass.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld