00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00018
00020
00021 #ifndef NET_INSTAWEB_UTIL_PUBLIC_THREAD_SYNCHRONIZER_H__
00022 #define NET_INSTAWEB_UTIL_PUBLIC_THREAD_SYNCHRONIZER_H__
00023
00024 #include <map>
00025
00026 #include "base/scoped_ptr.h"
00027 #include "net/instaweb/util/public/basictypes.h"
00028 #include "net/instaweb/util/public/string.h"
00029 #include "net/instaweb/util/public/string_util.h"
00030
00031 namespace net_instaweb {
00032
00033 class AbstractMutex;
00034 class ThreadSystem;
00035 class Timer;
00036
00055 class ThreadSynchronizer {
00056 public:
00057 explicit ThreadSynchronizer(ThreadSystem* thread_system);
00058 ~ThreadSynchronizer();
00059
00066 void EnableForPrefix(StringPiece prefix) {
00067 enabled_ = true;
00068 prefix.CopyToString(StringVectorAdd(&prefixes_));
00069 }
00070
00072 void Wait(const char* key) {
00073 if (enabled_) {
00074 DoWait(key);
00075 }
00076 }
00077
00082 void TimedWait(const char* key, int64 timeout_ms) {
00083 if (enabled_) {
00084 DoTimedWait(key, timeout_ms);
00085 }
00086 }
00087
00092 void Signal(const char* key) {
00093 if (enabled_) {
00094 DoSignal(key);
00095 }
00096 }
00097
00103 void AllowSloppyTermination(const char* key);
00104
00105 private:
00106 class SyncPoint;
00107 typedef std::map<GoogleString, SyncPoint*> SyncMap;
00108
00109 SyncPoint* GetSyncPoint(const GoogleString& key);
00110 void DoWait(const char* key);
00111 void DoTimedWait(const char* key, int64 timeout_ms);
00112 void DoSignal(const char* key);
00113 bool MatchesPrefix(const char* key) const;
00114
00115 bool enabled_;
00116 ThreadSystem* thread_system_;
00117 SyncMap sync_map_;
00118 scoped_ptr<AbstractMutex> map_mutex_;
00119 scoped_ptr<Timer> timer_;
00120 StringVector prefixes_;
00121
00122 DISALLOW_COPY_AND_ASSIGN(ThreadSynchronizer);
00123 };
00124
00125 }
00126
00127 #endif ///< NET_INSTAWEB_UTIL_PUBLIC_THREAD_SYNCHRONIZER_H__