Page Speed Optimization Libraries  1.4.26.1
net/instaweb/util/public/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 
00026 
00027 #ifndef NET_INSTAWEB_UTIL_PUBLIC_THREAD_SYSTEM_H_
00028 #define NET_INSTAWEB_UTIL_PUBLIC_THREAD_SYSTEM_H_
00029 
00030 #include "net/instaweb/util/public/abstract_mutex.h"
00031 #include "net/instaweb/util/public/basictypes.h"
00032 
00033 namespace net_instaweb {
00034 
00035 class Timer;
00036 
00039 class ThreadSystem {
00040  public:
00041   class Condvar;
00042   class Thread;
00043   class ThreadImpl;
00044 
00045   class CondvarCapableMutex : public AbstractMutex {
00046    public:
00047     CondvarCapableMutex() { }
00048     virtual ~CondvarCapableMutex();
00049 
00051     virtual Condvar* NewCondvar() = 0;
00052 
00053    private:
00054     DISALLOW_COPY_AND_ASSIGN(CondvarCapableMutex);
00055   };
00056 
00060   class RWLock : public AbstractMutex {
00061    public:
00062     RWLock() {}
00063     virtual ~RWLock();
00064 
00069     virtual bool ReaderTryLock() = 0;
00071     virtual void ReaderLock() = 0;
00073     virtual void ReaderUnlock() = 0;
00074 
00077     virtual void DCheckReaderLocked();
00078 
00079    private:
00080     DISALLOW_COPY_AND_ASSIGN(RWLock);
00081   };
00082 
00088   class ScopedReader {
00089    public:
00090     explicit ScopedReader(RWLock* lock) : lock_(lock) {
00091       lock_->ReaderLock();
00092     }
00093 
00094     void Release() {
00097       if (lock_ != NULL) {
00098         lock_->ReaderUnlock();
00099         lock_ = NULL;
00100       }
00101     }
00102 
00103     ~ScopedReader() {
00104       Release();
00105     }
00106 
00107    private:
00108     RWLock* lock_;
00109 
00110     DISALLOW_COPY_AND_ASSIGN(ScopedReader);
00111   };
00112 
00113   enum ThreadFlags {
00114     kDetached = 0,
00115     kJoinable = 1
00116   };
00117 
00118   virtual ~ThreadSystem();
00119   ThreadSystem() {}
00120 
00124   virtual CondvarCapableMutex* NewMutex() = 0;
00125 
00129   virtual RWLock* NewRWLock() = 0;
00130 
00132   static ThreadSystem* CreateThreadSystem();
00133 
00135   virtual Timer* NewTimer() = 0;
00136 
00137  private:
00138   friend class Thread;
00139   friend class MockThreadSystem;
00140   friend class CheckingThreadSystem;
00141   virtual ThreadImpl* NewThreadImpl(Thread* wrapper, ThreadFlags flags) = 0;
00142 
00143   DISALLOW_COPY_AND_ASSIGN(ThreadSystem);
00144 };
00145 
00149 class ThreadSystem::ThreadImpl {
00150  public:
00151   virtual bool StartImpl() = 0;
00152   virtual void JoinImpl() = 0;
00153   virtual ~ThreadImpl();
00154 
00155  protected:
00156   ThreadImpl() {}
00157 
00158  private:
00159   DISALLOW_COPY_AND_ASSIGN(ThreadImpl);
00160 };
00161 
00164 #define ScopedReader(x) COMPILE_ASSERT(0, mutex_lock_decl_missing_var_name)
00165 
00166 }  
00167 
00168 #endif  ///< NET_INSTAWEB_UTIL_PUBLIC_THREAD_SYSTEM_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines