17 #ifndef PAGESPEED_CONTROLLER_CONTEXT_REGISTRY_H_
18 #define PAGESPEED_CONTROLLER_CONTEXT_REGISTRY_H_
22 #include <unordered_set>
24 #include "base/logging.h"
28 #include "pagespeed/kernel/base/thread_annotations.h"
31 namespace net_instaweb {
39 template <
typename ContextT>
48 LOCKS_EXCLUDED(mutex_) WARN_UNUSED_RESULT;
49 void RemoveContext(ContextT* ctx) LOCKS_EXCLUDED(mutex_);
69 bool IsShutdown()
const LOCKS_EXCLUDED(mutex_);
72 int Size()
const LOCKS_EXCLUDED(mutex_);
75 bool Empty()
const LOCKS_EXCLUDED(mutex_);
78 typedef std::unordered_set<ContextT*> ContextSet;
80 std::unique_ptr<ThreadSystem::CondvarCapableMutex> mutex_;
81 std::unique_ptr<ThreadSystem::Condvar> condvar_ GUARDED_BY(mutex_);
82 bool shutdown_ GUARDED_BY(mutex_);
83 ContextSet contexts_ GUARDED_BY(mutex_);
86 template <
typename ContextT>
88 : mutex_(thread_system->NewMutex()),
89 condvar_(mutex_->NewCondvar()),
93 template <
typename ContextT>
94 ContextRegistry<ContextT>::~ContextRegistry() {
95 DCHECK_EQ(contexts_.size(), 0);
98 template <
typename ContextT>
101 return contexts_.empty();
104 template <
typename ContextT>
107 return static_cast<int>(contexts_.size());
110 template <
typename ContextT>
116 template <
typename ContextT>
118 CHECK(context !=
nullptr);
121 bool inserted =
false;
123 inserted = contexts_.insert(context).second;
129 template <
typename ContextT>
132 int num_erased = contexts_.erase(context);
133 DCHECK_EQ(num_erased, 1);
134 if (num_erased > 0 && contexts_.empty() && shutdown_) {
135 condvar_->Broadcast();
139 template <
typename ContextT>
141 ContextSet old_contexts;
149 old_contexts = contexts_;
154 if (old_contexts.empty()) {
158 for (ContextT* ctx : old_contexts) {
166 if (contexts_.find(ctx) != contexts_.end()) {
176 template <
typename ContextT>
183 while (!contexts_.empty()) {
int Size() const LOCKS_EXCLUDED(mutex_)
Number of contained contexts.
Definition: context_registry.h:105
void CancelAllActive() LOCKS_EXCLUDED(mutex_)
Definition: context_registry.h:140
bool TryRegisterContext(ContextT *ctx) LOCKS_EXCLUDED(mutex_) WARN_UNUSED_RESULT
Definition: context_registry.h:117
bool IsShutdown() const LOCKS_EXCLUDED(mutex_)
Definition: context_registry.h:111
Helper class for lexically scoped mutexing.
Definition: abstract_mutex.h:46
void CancelAllActiveAndWait() LOCKS_EXCLUDED(mutex_)
Definition: context_registry.h:177
Definition: thread_system.h:40
bool Empty() const LOCKS_EXCLUDED(mutex_)
For use in tests.
Definition: context_registry.h:99
Definition: context_registry.h:40