Page Speed Optimization Libraries  1.2.24.1
net/instaweb/util/public/thread_synchronizer.h
Go to the documentation of this file.
00001 /*
00002  * Copyright 2012 Google Inc.
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *      http:///www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
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 "net/instaweb/util/public/basictypes.h"
00027 #include "net/instaweb/util/public/scoped_ptr.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__
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines