Page Speed Optimization Libraries  1.13.35.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
context_registry.h
Go to the documentation of this file.
1 // Copyright 2016 Google Inc.
16 
17 #ifndef PAGESPEED_CONTROLLER_CONTEXT_REGISTRY_H_
18 #define PAGESPEED_CONTROLLER_CONTEXT_REGISTRY_H_
19 
20 #include <unistd.h>
21 #include <memory>
22 #include <unordered_set>
23 
24 #include "base/logging.h"
28 #include "pagespeed/kernel/base/thread_annotations.h"
30 
31 namespace net_instaweb {
32 
38 
39 template <typename ContextT>
41  public:
42  ContextRegistry(ThreadSystem* thread_system);
43  virtual ~ContextRegistry();
44 
47  bool TryRegisterContext(ContextT* ctx)
48  LOCKS_EXCLUDED(mutex_) WARN_UNUSED_RESULT;
49  void RemoveContext(ContextT* ctx) LOCKS_EXCLUDED(mutex_);
50 
57  void CancelAllActiveAndWait() LOCKS_EXCLUDED(mutex_);
58 
62  void CancelAllActive() LOCKS_EXCLUDED(mutex_);
63 
69  bool IsShutdown() const LOCKS_EXCLUDED(mutex_);
70 
72  int Size() const LOCKS_EXCLUDED(mutex_);
73 
75  bool Empty() const LOCKS_EXCLUDED(mutex_);
76 
77  private:
78  typedef std::unordered_set<ContextT*> ContextSet;
79 
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_);
84 };
85 
86 template <typename ContextT>
88  : mutex_(thread_system->NewMutex()),
89  condvar_(mutex_->NewCondvar()),
90  shutdown_(false) {
91 }
92 
93 template <typename ContextT>
94 ContextRegistry<ContextT>::~ContextRegistry() {
95  DCHECK_EQ(contexts_.size(), 0);
96 }
97 
98 template <typename ContextT>
100  ScopedMutex lock(mutex_.get());
101  return contexts_.empty();
102 }
103 
104 template <typename ContextT>
106  ScopedMutex lock(mutex_.get());
107  return static_cast<int>(contexts_.size());
108 }
109 
110 template <typename ContextT>
112  ScopedMutex lock(mutex_.get());
113  return shutdown_;
114 }
115 
116 template <typename ContextT>
118  CHECK(context != nullptr);
119 
120  ScopedMutex lock(mutex_.get());
121  bool inserted = false;
122  if (!shutdown_) {
123  inserted = contexts_.insert(context).second;
124  DCHECK(inserted);
125  }
126  return inserted;
127 }
128 
129 template <typename ContextT>
130 void ContextRegistry<ContextT>::RemoveContext(ContextT* context) {
131  ScopedMutex lock(mutex_.get());
132  int num_erased = contexts_.erase(context);
133  DCHECK_EQ(num_erased, 1);
134  if (num_erased > 0 && contexts_.empty() && shutdown_) {
135  condvar_->Broadcast();
136  }
137 }
138 
139 template <typename ContextT>
141  ContextSet old_contexts;
142  {
143  ScopedMutex lock(mutex_.get());
144  shutdown_ = true;
149  old_contexts = contexts_;
150  }
151 
154  if (old_contexts.empty()) {
155  return;
156  }
157 
158  for (ContextT* ctx : old_contexts) {
165  mutex_->Lock();
166  if (contexts_.find(ctx) != contexts_.end()) {
169  ctx->TryCancel();
170  }
171  mutex_->Unlock();
172  usleep(1);
173  }
174 }
175 
176 template <typename ContextT>
178  CancelAllActive();
179 
181  {
182  ScopedMutex lock(mutex_.get());
183  while (!contexts_.empty()) {
184  condvar_->Wait();
185  }
186  }
187 }
188 
189 }
190 
191 #endif
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