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

Unified Diff: src/plugin/Instances.h

Issue 29330403: Issue #1467, #3397 - Rewrite the map from threads to tabs (Closed)
Patch Set: rebase only Created Dec. 8, 2015, 7:43 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/plugin/Instances.h
===================================================================
new file mode 100644
--- /dev/null
+++ b/src/plugin/Instances.h
@@ -0,0 +1,92 @@
+/*
sergei 2016/02/01 15:50:42 Header guard is missed.
sergei 2016/02/01 15:50:42 It would be better to rename the file to SyncMap.h
Eric 2016/02/03 17:17:04 Acknowledged. FYI, it's already present in https:
Eric 2016/02/03 17:17:04 See https://codereview.adblockplus.org/29333107/.
sergei 2016/02/08 13:35:35 In that case it would be better to have two files
Eric 2016/02/08 18:45:29 We're not writing Java. One header for two related
sergei 2016/05/23 13:14:48 So far there is only one container which is named
Eric 2016/07/17 16:04:43 Both points addressed elsewhere.
+ * This file is part of Adblock Plus <https://adblockplus.org/>,
+ * Copyright (C) 2006-2015 Eyeo GmbH
sergei 2016/02/01 15:50:42 2016
Eric 2016/02/03 17:17:04 Done.
+ *
+ * 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/>.
+ */
+#include <map>
+#include <mutex>
+
+/**
+ * A base class for a synchronized map from threads to BHO instances.
sergei 2016/02/01 15:50:41 Either the comment or class should not be so threa
Eric 2016/02/03 17:17:03 It's a template class to be instantiated with some
sergei 2016/05/23 13:14:48 I think that we should not overcomplicate the code
Eric 2016/07/17 16:04:44 This "complication" is part of the environment. We
+ *
+ * The member functions here not simply forwarded versions of the container functions.
+ * Rather, they are specialized for tracking BHO calls to SetSite().
+ * Their semantics allow for verification that the call pattern is as expected.
+ *
+ * The key to the map (in the subclass) is the thread ID, thus operations are serialized on a per-key basis.
+ * Calls to SetSite() bracket all other calls, so on a per-key basis
+ * the order of operations is always either (insert / find-success / erase) or (find-failure).
+ * The library guarantees for std::map seem to indicate that operations on different keys
+ * do not interfer with each other, but there's some ambiguity there.
+ * This class is synchronized as a matter of defensive programming.
+ */
+template<class Key, class T, T nullValue>
sergei 2016/02/01 15:50:42 We don't need `T nullValue`.
Eric 2016/02/03 17:17:04 See the unit tests. This class is easier to test w
+class SyncMap
Oleksandr 2016/02/01 10:53:28 Isn't this essentially the same as concurrent_unor
Eric 2016/02/01 12:14:59 It's similar in purpose, but that's about it. You
+{
+ typedef std::lock_guard<std::mutex> SentryType;
+
+ /**
+ * Underlying map container
+ */
+ std::map<Key, T> idMap;
sergei 2016/02/01 15:50:43 it would be better to call just "map".
Eric 2016/02/03 17:17:03 Acknowledged.
+
+ /**
+ * Synchronization primitive
+ */
+ std::mutex mutex;
sergei 2016/02/01 15:50:42 it should has `mutable`.
Eric 2016/02/03 17:17:03 Done.
+
+public:
+ /**
+ * Returns true if (as expected) no key of value 'id' was present.
+ * Returns false otherwise.
+ */
+ bool AddIfAbsent(Key id, T p)
sergei 2016/02/01 15:50:41 What about constant references or more concrete in
sergei 2016/02/01 15:50:43 it would be better to call the arguments as "key"
Eric 2016/02/03 17:17:03 I don't want it completely generic. If this were a
Eric 2016/02/03 17:17:05 No need for that. A plain 'class T' is just fine.
sergei 2016/02/08 13:35:35 I disagree here. If it's written as generic and it
Eric 2016/02/08 18:45:29 Please tell that to 'std::basic_string', which ord
Oleksandr 2016/02/10 10:58:47 I think if we are not implementing a proper interf
Eric 2016/05/19 17:23:57 Good.
+ {
+ SentryType sentry(mutex);
+ auto it = idMap.find(id);
+ if (it != idMap.end())
+ {
+ return false;
+ }
+ idMap[id] = p;
sergei 2016/02/01 15:50:43 `T` is not necessarily default constructible, it's
Eric 2016/02/03 17:17:04 How is default construction an issue here? 'p' is
sergei 2016/02/08 13:35:35 `map<K, T>::operator[]` firstly creates T() and th
Eric 2016/02/08 18:45:30 No it doesn't. It constructs the value type in-pla
sergei 2016/05/23 13:14:47 So, it describes exactly what I had told. One can
Eric 2016/07/17 16:04:43 I've rewritten the function to use insert(). The b
sergei 2016/07/19 07:56:09 JIC, insert has the same complexity.
+ return true;
+ }
+
+ /**
+ * Returns true if (as expected) a key of value 'id' was already present.
+ * Returns false otherwise.
+ */
+ bool RemoveAndCheck(Key id)
+ {
+ SentryType sentry(mutex);
+ auto it = idMap.find(id);
sergei 2016/02/01 15:50:42 Can we do instead `return 1 == idMap.erase(id);`?
Eric 2016/02/03 17:17:04 Other than being written as a yoda condition, that
+ if (it == idMap.end())
+ {
+ return false;
+ }
+ idMap.erase(it);
+ return true;
+ }
+
+ /**
+ * Returns a non-null pointer if a key of value 'id' is present.
sergei 2016/02/01 15:50:42 This comment does not fit the template class. We d
Eric 2016/02/03 17:17:04 Acknowledged. I probably wrote this comment befor
+ * Returns nullptr otherwise.
+ */
+ T Locate(Key id)
sergei 2016/02/01 15:50:42 I would rather prefer to pass T by reference to us
sergei 2016/02/01 15:50:42 This method should be constant method.
Eric 2016/02/03 17:17:03 No need. Putting return values in references is ne
Eric 2016/02/03 17:17:03 Done.
sergei 2016/02/08 13:35:36 IMO having default `nullValue` here is a bad desig
Eric 2016/02/08 18:45:30 The alternative is a traits class, which would ove
+ {
+ SentryType sentry(mutex);
+ auto it = idMap.find(id);
+ return (it != idMap.end()) ? it->second : nullValue;
sergei 2016/02/01 15:50:43 As far as I remember () are not needed here.
Eric 2016/02/03 17:17:04 Not necessary, but ternary expressions are always
sergei 2016/02/08 13:35:36 That's very subjective.
Eric 2016/02/08 18:45:29 It should be manifestly obvious by now that your p
+ }
+};
« no previous file with comments | « adblockplus.gyp ('k') | src/plugin/PluginClass.h » ('j') | src/plugin/PluginClass.cpp » ('J')

Powered by Google App Engine
This is Rietveld