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

Side by Side Diff: src/plugin/Instances.h

Issue 29330403: Issue #1467, #3397 - Rewrite the map from threads to tabs (Closed)
Patch Set: Created Nov. 22, 2015, 8:17 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « adblockplus.gyp ('k') | src/plugin/PluginClass.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-2015 Eyeo GmbH
4 *
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
7 * published by the Free Software Foundation.
8 *
9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
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/>.
16 */
17 #include <map>
18 #include <mutex>
19
20 /**
21 * A base class for a synchronized map from threads to BHO instances.
22 *
23 * The member functions here not simply forwarded versions of the container func tions.
24 * Rather, they are specialized for tracking BHO calls to SetSite().
25 * Their semantics allow for verification that the call pattern is as expected.
26 *
27 * 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
29 * 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
31 * do not interfer with each other, but there's some ambiguity there.
32 * This class is synchronized as a matter of defensive programming.
33 */
34 template<class Key, class T, T nullValue>
35 class SyncMap
36 {
37 typedef std::lock_guard<std::mutex> SentryType;
38
39 /**
40 * Underlying map container
41 */
42 std::map<Key, T> idMap;
43
44 /**
45 * Synchronization primitive
46 */
47 std::mutex mutex;
48
49 public:
50 /**
51 * Returns true if (as expected) no key of value 'id' was present.
52 * Returns false otherwise.
53 */
54 bool AddIfAbsent(Key id, T p)
55 {
56 SentryType sentry(mutex);
57 auto it = idMap.find(id);
58 if (it != idMap.end())
59 {
60 return false;
61 }
62 idMap[id] = p;
63 return true;
64 }
65
66 /**
67 * Returns true if (as expected) a key of value 'id' was already present.
68 * Returns false otherwise.
69 */
70 bool RemoveAndCheck(Key id)
71 {
72 SentryType sentry(mutex);
73 auto it = idMap.find(id);
74 if (it == idMap.end())
75 {
76 return false;
77 }
78 idMap.erase(it);
79 return true;
80 }
81
82 /**
83 * Returns a non-null pointer if a key of value 'id' is present.
84 * Returns nullptr otherwise.
85 */
86 T Locate(Key id)
87 {
88 SentryType sentry(mutex);
89 auto it = idMap.find(id);
90 return (it != idMap.end()) ? it->second : nullValue;
91 }
92 };
OLDNEW
« no previous file with comments | « adblockplus.gyp ('k') | src/plugin/PluginClass.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld