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

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

Issue 29333107: Issue #1652, #3456 - Replace 's_asyncWebBrowser2' with thread-safe facilities
Left Patch Set: address comments Created Jan. 4, 2016, 5:51 p.m.
Right Patch Set: rebase only Created July 27, 2016, 10:26 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 | « no previous file | 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 /*
Oleksandr 2016/02/01 18:03:14 Nit: This file is also being added in https://code
Eric 2016/02/03 14:57:28 No. I made sure they were not interdependent. The
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-2016 Eyeo GmbH 3 * Copyright (C) 2006-2016 Eyeo GmbH
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 17
18 #ifndef _INSTANCES_H_ 18 #ifndef _INSTANCES_H_
19 #define _INSTANCES_H_ 19 #define _INSTANCES_H_
20 20
21 #include <map>
21 #include <mutex> 22 #include <mutex>
22 #include <set> 23 #include <set>
24
25 /**
26 * A base class for a synchronized map from threads to BHO instances.
27 *
28 * This is a base class for 'CurrentThreadMap', defined in PluginClass.cpp.
29 * It's separated out here for testability.
30 * This class should not be used as polymorphic base class, as it has no virtual base class.
31 *
32 * The member functions here not simply forwarded versions of the container func tions.
33 * Rather, they are specialized for tracking BHO calls to SetSite().
34 * Their semantics allow for verification that the call pattern is as expected.
35 *
36 * The key to the map (in the subclass) is the thread ID, thus operations are se rialized on a per-key basis.
37 * Calls to SetSite() bracket all other calls, so on a per-key basis
38 * the order of operations is always either (insert / find-success / erase) or (find-failure).
39 * The library guarantees for std::map seem to indicate that operations on diffe rent keys
40 * do not interfer with each other, but there's some ambiguity there.
41 * This class is synchronized as a matter of defensive programming.
42 */
43 template<class Key, class T, T nullValue>
44 class SyncMap
45 {
46 typedef std::lock_guard<std::mutex> SentryType;
47
48 /**
49 * Underlying map container
50 */
51 std::map<Key, T> idMap;
52
53 /**
54 * Synchronization primitive
55 */
56 mutable std::mutex mutex;
57
58 public:
59 /**
60 * Returns true if (as expected) no key of value 'id' was present.
61 * Returns false otherwise.
62 */
63 bool AddIfAbsent(Key id, T p)
64 {
65 SentryType sentry(mutex);
66 auto it = idMap.insert(std::make_pair(id, p));
67 return it.second;
68 // Assert it.second==true implies the insertion took place,
69 // which means there was no key of value 'id' already present.
70 }
71
72 /**
73 * Returns true if (as expected) a key of value 'id' was already present.
74 * Returns false otherwise.
75 */
76 bool RemoveIfPresent(Key id)
77 {
78 SentryType sentry(mutex);
79 auto it = idMap.find(id);
80 if (it == idMap.end())
81 {
82 return false;
83 }
84 idMap.erase(it);
85 return true;
86 }
87
88 /**
89 * Returns a non-nullValue if a key of value 'id' is present.
90 * Returns nullValue otherwise.
91 */
92 T Locate(Key id) const
93 {
94 SentryType sentry(mutex);
95 auto it = idMap.find(id);
96 return (it != idMap.end()) ? it->second : nullValue;
97 }
98 };
23 99
24 /** 100 /**
25 * Template class for a synchronized set of BHO instances 101 * Template class for a synchronized set of BHO instances
26 */ 102 */
27 template<class T, T nullValue> 103 template<class T, T nullValue>
28 class SyncSet 104 class SyncSet
29 { 105 {
30 typedef std::lock_guard<std::mutex> SentryType; 106 typedef std::lock_guard<std::mutex> SentryType;
31 107
32 /** 108 /**
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
102 return nullValue; 178 return nullValue;
103 } 179 }
104 180
105 bool Empty() const 181 bool Empty() const
106 { 182 {
107 SentryType sentry(mutex); 183 SentryType sentry(mutex);
108 return set.empty(); 184 return set.empty();
109 } 185 }
110 }; 186 };
111 187
112 #endif 188 #endif
LEFTRIGHT

Powered by Google App Engine
This is Rietveld