Page Speed Optimization Libraries  1.5.27.2
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 
00116   class ThreadId {
00117    public:
00118     ThreadId() {}
00119     virtual ~ThreadId();
00120     virtual bool IsEqual(const ThreadId& that) const = 0;
00121     virtual bool IsCurrentThread() const = 0;
00122 
00123    private:
00124     DISALLOW_COPY_AND_ASSIGN(ThreadId);
00125   };
00126 
00127   enum ThreadFlags {
00128     kDetached = 0,
00129     kJoinable = 1
00130   };
00131 
00132   virtual ~ThreadSystem();
00133   ThreadSystem() {}
00134 
00138   virtual CondvarCapableMutex* NewMutex() = 0;
00139 
00143   virtual RWLock* NewRWLock() = 0;
00144 
00148   virtual Timer* NewTimer() = 0;
00149 
00152   virtual ThreadId* GetThreadId() const = 0;
00153 
00154  private:
00155   friend class Thread;
00156   friend class MockThreadSystem;
00157   friend class CheckingThreadSystem;
00158   virtual ThreadImpl* NewThreadImpl(Thread* wrapper, ThreadFlags flags) = 0;
00159 
00160   DISALLOW_COPY_AND_ASSIGN(ThreadSystem);
00161 };
00162 
00166 class ThreadSystem::ThreadImpl {
00167  public:
00168   virtual bool StartImpl() = 0;
00169   virtual void JoinImpl() = 0;
00170   virtual ~ThreadImpl();
00171 
00172  protected:
00173   ThreadImpl() {}
00174 
00175  private:
00176   DISALLOW_COPY_AND_ASSIGN(ThreadImpl);
00177 };
00178 
00181 #define ScopedReader(x) COMPILE_ASSERT(0, mutex_lock_decl_missing_var_name)
00182 
00183 }  
00184 
00185 #endif  ///< NET_INSTAWEB_UTIL_PUBLIC_THREAD_SYSTEM_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines