Left: | ||
Right: |
OLD | NEW |
---|---|
(Empty) | |
1 /* | |
Oleksandr
2016/02/10 10:58:49
Nit: If I understand correctly this file will have
Eric
2016/05/19 17:23:58
Synchronization isn't fundamental to what these cl
sergei
2016/05/23 13:14:48
I like "BHOContainer.h" but the classes' names sho
Eric
2016/07/17 16:04:44
This file also appears in https://codereview.adblo
| |
2 * This file is part of Adblock Plus <https://adblockplus.org/>, | |
3 * Copyright (C) 2006-2016 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 | |
18 #ifndef _INSTANCES_H_ | |
19 #define _INSTANCES_H_ | |
20 | |
21 #include <mutex> | |
22 #include <map> | |
23 | |
24 /** | |
25 * A base class for a synchronized map from threads to BHO instances. | |
26 * | |
27 * The member functions here not simply forwarded versions of the container func tions. | |
28 * Rather, they are specialized for tracking BHO calls to SetSite(). | |
29 * Their semantics allow for verification that the call pattern is as expected. | |
30 * | |
31 * The key to the map (in the subclass) is the thread ID, thus operations are se rialized on a per-key basis. | |
32 * Calls to SetSite() bracket all other calls, so on a per-key basis | |
33 * the order of operations is always either (insert / find-success / erase) or (find-failure). | |
34 * The library guarantees for std::map seem to indicate that operations on diffe rent keys | |
35 * do not interfer with each other, but there's some ambiguity there. | |
36 * This class is synchronized as a matter of defensive programming. | |
37 */ | |
38 template<class Key, class T, T nullValue> | |
39 class SyncMap | |
40 { | |
41 typedef std::lock_guard<std::mutex> SentryType; | |
42 | |
43 /** | |
44 * Underlying map container | |
45 */ | |
46 std::map<Key, T> idMap; | |
47 | |
48 /** | |
49 * Synchronization primitive | |
50 */ | |
51 mutable std::mutex mutex; | |
52 | |
53 public: | |
54 /** | |
55 * Returns true if (as expected) no key of value 'id' was present. | |
56 * Returns false otherwise. | |
57 */ | |
58 bool AddIfAbsent(Key id, T p) | |
59 { | |
60 SentryType sentry(mutex); | |
61 auto it = idMap.find(id); | |
62 if (it != idMap.end()) | |
63 { | |
64 return false; | |
65 } | |
66 idMap[id] = p; | |
67 return true; | |
68 } | |
69 | |
70 /** | |
71 * Returns true if (as expected) a key of value 'id' was already present. | |
72 * Returns false otherwise. | |
73 */ | |
74 bool RemoveAndCheck(Key id) | |
75 { | |
76 SentryType sentry(mutex); | |
77 auto it = idMap.find(id); | |
78 if (it == idMap.end()) | |
79 { | |
80 return false; | |
81 } | |
82 idMap.erase(it); | |
83 return true; | |
84 } | |
85 | |
86 /** | |
87 * Returns a non-nullValue if a key of value 'id' is present. | |
88 * Returns nullValue otherwise. | |
89 */ | |
90 T Locate(Key id) const | |
91 { | |
92 SentryType sentry(mutex); | |
93 auto it = idMap.find(id); | |
94 return (it != idMap.end()) ? it->second : nullValue; | |
95 } | |
96 }; | |
97 | |
98 #endif | |
OLD | NEW |