Page Speed Optimization Libraries  1.13.35.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
thread_system.h
Go to the documentation of this file.
1 /*
2  * Copyright 2011 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http:///www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
26 
27 #ifndef PAGESPEED_KERNEL_BASE_THREAD_SYSTEM_H_
28 #define PAGESPEED_KERNEL_BASE_THREAD_SYSTEM_H_
29 
32 #include "pagespeed/kernel/base/thread_annotations.h"
33 
34 namespace net_instaweb {
35 
36 class Timer;
37 
40 class ThreadSystem {
41  public:
42  class Condvar;
43  class Thread;
44  class ThreadImpl;
45 
46  class LOCKABLE CondvarCapableMutex : public AbstractMutex {
47  public:
49  virtual ~CondvarCapableMutex();
50 
52  virtual Condvar* NewCondvar() = 0;
53 
54  private:
55 
56  };
57 
61  class LOCKABLE RWLock : public AbstractMutex {
62  public:
63  RWLock() {}
64  virtual ~RWLock();
65 
70  virtual bool ReaderTryLock() SHARED_TRYLOCK_FUNCTION(true) = 0;
72  virtual void ReaderLock() SHARED_LOCK_FUNCTION() = 0;
74  virtual void ReaderUnlock() UNLOCK_FUNCTION() = 0;
75 
78  virtual void DCheckReaderLocked();
79 
80  private:
81 
82  };
83 
89  class SCOPED_LOCKABLE ScopedReader {
90  public:
91  explicit ScopedReader(RWLock* lock) SHARED_LOCK_FUNCTION(lock)
92  : lock_(lock) {
93  lock_->ReaderLock();
94  }
95 
96  void Release() UNLOCK_FUNCTION() {
99  if (lock_ != NULL) {
100  lock_->ReaderUnlock();
101  lock_ = NULL;
102  }
103  }
104 
105  ~ScopedReader() UNLOCK_FUNCTION() { Release(); }
106 
107  private:
108  RWLock* lock_;
109 
110 
111  };
112 
116  class ThreadId {
117  public:
118  ThreadId() {}
119  virtual ~ThreadId();
120  virtual bool IsEqual(const ThreadId& that) const = 0;
121  virtual bool IsCurrentThread() const = 0;
122 
123  private:
124 
125  };
126 
127  enum ThreadFlags {
128  kDetached = 0,
129  kJoinable = 1
130  };
131 
132  virtual ~ThreadSystem();
133  ThreadSystem() {}
134 
138  virtual CondvarCapableMutex* NewMutex() = 0;
139 
143  virtual RWLock* NewRWLock() = 0;
144 
148  virtual Timer* NewTimer() = 0;
149 
152  virtual ThreadId* GetThreadId() const = 0;
153 
154  private:
155  friend class Thread;
156  friend class MockThreadSystem;
157  friend class CheckingThreadSystem;
158  virtual ThreadImpl* NewThreadImpl(Thread* wrapper, ThreadFlags flags) = 0;
159 
160 
161 };
162 
167  public:
168  virtual bool StartImpl() = 0;
169  virtual void JoinImpl() = 0;
170  virtual ~ThreadImpl();
171 
172  protected:
173  ThreadImpl() {}
174 
175  private:
176 
177 };
178 
181 #define ScopedReader(x) COMPILE_ASSERT(0, mutex_lock_decl_missing_var_name)
182 
183 }
184 
185 #endif
Definition: condvar.h:27
Abstract interface for implementing a mutex.
Definition: abstract_mutex.h:28
virtual RWLock * NewRWLock()=0
void Release() UNLOCK_FUNCTION()
Definition: thread_system.h:96
Definition: thread_system.h:61
virtual Timer * NewTimer()=0
virtual ThreadId * GetThreadId() const =0
#define ScopedReader(x)
Definition: thread_system.h:181
Definition: thread_system.h:40
Definition: thread_system.h:116
Definition: thread_system.h:89
Definition: thread_system.h:166
virtual CondvarCapableMutex * NewMutex()=0
Base class for client thread code.
Definition: thread.h:34