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

Delta Between Two Patch Sets: src/plugin/Instances.h

Issue 29330403: Issue #1467, #3397 - Rewrite the map from threads to tabs (Closed)
Left Patch Set: rebase only Created Dec. 8, 2015, 7:43 p.m.
Right Patch Set: add comment; new function body Created July 17, 2016, 4:01 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « adblockplus.gyp ('k') | src/plugin/PluginClass.h » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 /* 1 /*
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.
2 * This file is part of Adblock Plus <https://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-2015 Eyeo GmbH 3 * Copyright (C) 2006-2016 Eyeo GmbH
sergei 2016/02/01 15:50:42 2016
Eric 2016/02/03 17:17:04 Done.
4 * 4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify 5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as 6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * Adblock Plus is distributed in the hope that it will be useful, 9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. 12 * GNU General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU General Public License 14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */ 16 */
17
18 #ifndef _INSTANCES_H_
19 #define _INSTANCES_H_
20
21 #include <mutex>
17 #include <map> 22 #include <map>
18 #include <mutex>
19 23
20 /** 24 /**
21 * A base class for a synchronized map from threads to BHO instances. 25 * 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
26 *
27 * This is a base class for 'CurrentThreadMap', defined in PluginClass.cpp.
28 * It's separated out here for testability.
29 * This class should not be used as polymorphic base class, as it has no virtual base class.
22 * 30 *
23 * The member functions here not simply forwarded versions of the container func tions. 31 * The member functions here not simply forwarded versions of the container func tions.
24 * Rather, they are specialized for tracking BHO calls to SetSite(). 32 * Rather, they are specialized for tracking BHO calls to SetSite().
25 * Their semantics allow for verification that the call pattern is as expected. 33 * Their semantics allow for verification that the call pattern is as expected.
26 * 34 *
27 * The key to the map (in the subclass) is the thread ID, thus operations are se rialized on a per-key basis. 35 * The key to the map (in the subclass) is the thread ID, thus operations are se rialized on a per-key basis.
28 * Calls to SetSite() bracket all other calls, so on a per-key basis 36 * Calls to SetSite() bracket all other calls, so on a per-key basis
29 * the order of operations is always either (insert / find-success / erase) or (find-failure). 37 * the order of operations is always either (insert / find-success / erase) or (find-failure).
30 * The library guarantees for std::map seem to indicate that operations on diffe rent keys 38 * The library guarantees for std::map seem to indicate that operations on diffe rent keys
31 * do not interfer with each other, but there's some ambiguity there. 39 * do not interfer with each other, but there's some ambiguity there.
32 * This class is synchronized as a matter of defensive programming. 40 * This class is synchronized as a matter of defensive programming.
33 */ 41 */
34 template<class Key, class T, T nullValue> 42 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
35 class SyncMap 43 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
36 { 44 {
37 typedef std::lock_guard<std::mutex> SentryType; 45 typedef std::lock_guard<std::mutex> SentryType;
38 46
39 /** 47 /**
40 * Underlying map container 48 * Underlying map container
41 */ 49 */
42 std::map<Key, T> idMap; 50 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.
43 51
44 /** 52 /**
45 * Synchronization primitive 53 * Synchronization primitive
46 */ 54 */
47 std::mutex mutex; 55 mutable std::mutex mutex;
sergei 2016/02/01 15:50:42 it should has `mutable`.
Eric 2016/02/03 17:17:03 Done.
48 56
49 public: 57 public:
50 /** 58 /**
51 * Returns true if (as expected) no key of value 'id' was present. 59 * Returns true if (as expected) no key of value 'id' was present.
52 * Returns false otherwise. 60 * Returns false otherwise.
53 */ 61 */
54 bool AddIfAbsent(Key id, T p) 62 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.
55 { 63 {
56 SentryType sentry(mutex); 64 SentryType sentry(mutex);
57 auto it = idMap.find(id); 65 auto it = idMap.insert(std::make_pair(id,p));
sergei 2016/07/19 07:56:09 missing space after comma
Eric 2016/07/27 20:14:22 Fixed.
58 if (it != idMap.end()) 66 return it.second;
59 { 67 // Assert it.second==true implies the insertion took place,
60 return false; 68 // which means there was no key of value 'id' already present.
sergei 2016/07/19 07:56:10 I think we don't need this comment.
61 }
62 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.
63 return true;
64 } 69 }
65 70
66 /** 71 /**
67 * Returns true if (as expected) a key of value 'id' was already present. 72 * Returns true if (as expected) a key of value 'id' was already present.
68 * Returns false otherwise. 73 * Returns false otherwise.
69 */ 74 */
70 bool RemoveAndCheck(Key id) 75 bool RemoveIfPresent(Key id)
71 { 76 {
72 SentryType sentry(mutex); 77 SentryType sentry(mutex);
73 auto it = idMap.find(id); 78 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
74 if (it == idMap.end()) 79 if (it == idMap.end())
75 { 80 {
76 return false; 81 return false;
77 } 82 }
78 idMap.erase(it); 83 idMap.erase(it);
79 return true; 84 return true;
80 } 85 }
81 86
82 /** 87 /**
83 * Returns a non-null pointer if a key of value 'id' is present. 88 * Returns a non-nullValue 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
84 * Returns nullptr otherwise. 89 * Returns nullValue otherwise.
85 */ 90 */
86 T Locate(Key id) 91 T Locate(Key id) const
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
87 { 92 {
88 SentryType sentry(mutex); 93 SentryType sentry(mutex);
89 auto it = idMap.find(id); 94 auto it = idMap.find(id);
90 return (it != idMap.end()) ? it->second : nullValue; 95 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
91 } 96 }
92 }; 97 };
98
99 #endif
LEFTRIGHT

Powered by Google App Engine
This is Rietveld