Page Speed Optimization Libraries  1.4.26.1
net/instaweb/util/public/checking_thread_system.h
Go to the documentation of this file.
00001 /*
00002  * Copyright 2011 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 
00019 #ifndef NET_INSTAWEB_UTIL_PUBLIC_CHECKING_THREAD_SYSTEM_H_
00020 #define NET_INSTAWEB_UTIL_PUBLIC_CHECKING_THREAD_SYSTEM_H_
00021 
00022 #include "net/instaweb/util/public/thread_system.h"
00023 
00024 #include "net/instaweb/util/public/atomic_bool.h"
00025 #include "net/instaweb/util/public/atomic_int32.h"
00026 #include "net/instaweb/util/public/basictypes.h"
00027 #include "net/instaweb/util/public/scoped_ptr.h"
00028 
00029 namespace net_instaweb {
00030 
00031 class Timer;
00032 
00041 class CheckingThreadSystem : public ThreadSystem {
00042  private:
00046   class CheckingCondvar;
00047  public:
00054   class Mutex : public ThreadSystem::CondvarCapableMutex {
00055    public:
00056     explicit Mutex(ThreadSystem::CondvarCapableMutex* mutex) : mutex_(mutex) { }
00057     virtual ~Mutex();
00058 
00059     virtual bool TryLock();
00060     virtual void Lock();
00061     virtual void Unlock();
00063     virtual void DCheckLocked();
00064 
00066     virtual void DCheckUnlocked();
00067 
00069     virtual ThreadSystem::Condvar* NewCondvar();
00070 
00071    private:
00072     friend class CheckingCondvar;
00073     void TakeLockControl();
00074     void DropLockControl();
00075 
00076     scoped_ptr<ThreadSystem::CondvarCapableMutex> mutex_;
00077     AtomicBool locked_;
00078     DISALLOW_COPY_AND_ASSIGN(Mutex);
00079   };
00080 
00085   class RWLock : public ThreadSystem::RWLock {
00086    public:
00087     explicit RWLock(ThreadSystem::RWLock* lock) : lock_(lock) { }
00088     virtual ~RWLock();
00089 
00090     virtual bool TryLock();
00091     virtual void Lock();
00092     virtual void Unlock();
00093     virtual bool ReaderTryLock();
00094     virtual void ReaderLock();
00095     virtual void ReaderUnlock();
00096 
00098     virtual void DCheckLocked();
00099     virtual void DCheckReaderLocked();
00100 
00101    private:
00102     void TakeLockControl();
00103     void DropLockControl();
00104     void TakeReaderLockControl();
00105     void DropReaderLockControl();
00106 
00107     scoped_ptr<ThreadSystem::RWLock> lock_;
00108     AtomicInt32 locked_;
00109     DISALLOW_COPY_AND_ASSIGN(RWLock);
00110   };
00111 
00112   explicit CheckingThreadSystem(ThreadSystem* thread_system)
00113       : thread_system_(thread_system) { }
00114   virtual ~CheckingThreadSystem();
00115 
00116   virtual Mutex* NewMutex();
00117   virtual RWLock* NewRWLock();
00118   virtual Timer* NewTimer();
00119 
00120  private:
00121   friend class Mutex;
00122 
00123   virtual ThreadImpl* NewThreadImpl(Thread* wrapper, ThreadFlags flags);
00124 
00125   scoped_ptr<ThreadSystem> thread_system_;
00126   DISALLOW_COPY_AND_ASSIGN(CheckingThreadSystem);
00127 };
00128 
00129 }  
00130 
00131 #endif  ///< NET_INSTAWEB_UTIL_PUBLIC_CHECKING_THREAD_SYSTEM_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines