Page Speed Optimization Libraries  1.13.35.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
thread_synchronizer.h
Go to the documentation of this file.
1 /*
2  * Copyright 2012 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http:///www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
18 
20 
21 #ifndef PAGESPEED_KERNEL_THREAD_THREAD_SYNCHRONIZER_H__
22 #define PAGESPEED_KERNEL_THREAD_THREAD_SYNCHRONIZER_H__
23 
24 #include <map>
25 
30 
31 namespace net_instaweb {
32 
33 class AbstractMutex;
34 class ThreadSystem;
35 class Timer;
36 
56  public:
57  explicit ThreadSynchronizer(ThreadSystem* thread_system);
59 
66  void EnableForPrefix(StringPiece prefix) {
67  enabled_ = true;
68  prefix.CopyToString(StringVectorAdd(&prefixes_));
69  }
70 
72  void Wait(const char* key) {
73  if (enabled_) {
74  DoWait(key);
75  }
76  }
77 
82  void TimedWait(const char* key, int64 timeout_ms) {
83  if (enabled_) {
84  DoTimedWait(key, timeout_ms);
85  }
86  }
87 
92  void Signal(const char* key) {
93  if (enabled_) {
94  DoSignal(key);
95  }
96  }
97 
103  void AllowSloppyTermination(const char* key);
104 
105  private:
106  class SyncPoint;
107  typedef std::map<GoogleString, SyncPoint*> SyncMap;
108 
109  SyncPoint* GetSyncPoint(const GoogleString& key);
110  void DoWait(const char* key);
111  void DoTimedWait(const char* key, int64 timeout_ms);
112  void DoSignal(const char* key);
113  bool MatchesPrefix(const char* key) const;
114 
115  bool enabled_;
116  ThreadSystem* thread_system_;
117  SyncMap sync_map_;
118  scoped_ptr<AbstractMutex> map_mutex_;
119  scoped_ptr<Timer> timer_;
120  StringVector prefixes_;
121 
122 
123 };
124 
125 }
126 
127 #endif
Definition: thread_synchronizer.h:55
void Signal(const char *key)
Definition: thread_synchronizer.h:92
void AllowSloppyTermination(const char *key)
Definition: scoped_ptr.h:30
std::string GoogleString
PAGESPEED_KERNEL_BASE_STRING_H_.
Definition: string.h:24
void EnableForPrefix(StringPiece prefix)
Definition: thread_synchronizer.h:66
GoogleString * StringVectorAdd(StringVector *v)
Appends new empty string to a StringVector and returns a pointer to it.
Definition: string_util.h:696
Definition: thread_system.h:40
void TimedWait(const char *key, int64 timeout_ms)
Definition: thread_synchronizer.h:82
void Wait(const char *key)
Waits for a thread to signal the specified key.
Definition: thread_synchronizer.h:72