OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef PPAPI_UTILITY_THREAD_SAFE_THREAD_TRAITS_H_ | 5 #ifndef PPAPI_UTILITY_THREAD_SAFE_THREAD_TRAITS_H_ |
6 #define PPAPI_UTILITY_THREAD_SAFE_THREAD_TRAITS_H_ | 6 #define PPAPI_UTILITY_THREAD_SAFE_THREAD_TRAITS_H_ |
7 | 7 |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include "ppapi/cpp/logging.h" | 10 #include "ppapi/cpp/logging.h" |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
78 public: | 78 public: |
79 /// A simple reference counter that is not thread-safe. | 79 /// A simple reference counter that is not thread-safe. |
80 /// | 80 /// |
81 /// <strong>Note:</strong> in Debug mode, it checks that it is either called | 81 /// <strong>Note:</strong> in Debug mode, it checks that it is either called |
82 /// on the main thread, or always called on another thread. | 82 /// on the main thread, or always called on another thread. |
83 class RefCount { | 83 class RefCount { |
84 public: | 84 public: |
85 /// Default constructor. In debug mode, this checks that the object is being | 85 /// Default constructor. In debug mode, this checks that the object is being |
86 /// created on the main thread. | 86 /// created on the main thread. |
87 RefCount() : ref_(0) { | 87 RefCount() : ref_(0) { |
88 #ifndef NDEBUG | 88 #if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON) |
89 is_main_thread_ = Module::Get()->core()->IsMainThread(); | 89 is_main_thread_ = Module::Get()->core()->IsMainThread(); |
90 #endif | 90 #endif |
91 } | 91 } |
92 | 92 |
93 /// Destructor. | 93 /// Destructor. |
94 ~RefCount() { | 94 ~RefCount() { |
95 PP_DCHECK(is_main_thread_ == Module::Get()->core()->IsMainThread()); | 95 PP_DCHECK(is_main_thread_ == Module::Get()->core()->IsMainThread()); |
96 } | 96 } |
97 | 97 |
98 /// AddRef() increments the reference counter. | 98 /// AddRef() increments the reference counter. |
99 /// | 99 /// |
100 /// @return An int32_t with the incremented reference counter. | 100 /// @return An int32_t with the incremented reference counter. |
101 int32_t AddRef() { | 101 int32_t AddRef() { |
102 PP_DCHECK(is_main_thread_ == Module::Get()->core()->IsMainThread()); | 102 PP_DCHECK(is_main_thread_ == Module::Get()->core()->IsMainThread()); |
103 return ++ref_; | 103 return ++ref_; |
104 } | 104 } |
105 | 105 |
106 /// Release() decrements the reference counter. | 106 /// Release() decrements the reference counter. |
107 /// | 107 /// |
108 /// @return An int32_t with the decremeneted reference counter. | 108 /// @return An int32_t with the decremeneted reference counter. |
109 int32_t Release() { | 109 int32_t Release() { |
110 PP_DCHECK(is_main_thread_ == Module::Get()->core()->IsMainThread()); | 110 PP_DCHECK(is_main_thread_ == Module::Get()->core()->IsMainThread()); |
111 return --ref_; | 111 return --ref_; |
112 } | 112 } |
113 | 113 |
114 private: | 114 private: |
115 int32_t ref_; | 115 int32_t ref_; |
116 #ifndef NDEBUG | 116 #if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON) |
117 bool is_main_thread_; | 117 bool is_main_thread_; |
118 #endif | 118 #endif |
119 }; | 119 }; |
120 | 120 |
121 /// A simple object that acts like a lock but does nothing. | 121 /// A simple object that acts like a lock but does nothing. |
122 /// | 122 /// |
123 /// <strong>Note:</strong> in Debug mode, it checks that it is either | 123 /// <strong>Note:</strong> in Debug mode, it checks that it is either |
124 /// called on the main thread, or always called on another thread. It also | 124 /// called on the main thread, or always called on another thread. It also |
125 /// asserts that the caller does not recursively lock. | 125 /// asserts that the caller does not recursively lock. |
126 class Lock { | 126 class Lock { |
127 public: | 127 public: |
128 Lock() { | 128 Lock() { |
129 #ifndef NDEBUG | 129 #if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON) |
130 is_main_thread_ = Module::Get()->core()->IsMainThread(); | 130 is_main_thread_ = Module::Get()->core()->IsMainThread(); |
131 lock_held_ = false; | 131 lock_held_ = false; |
132 #endif | 132 #endif |
133 } | 133 } |
134 | 134 |
135 ~Lock() { | 135 ~Lock() { |
136 PP_DCHECK(is_main_thread_ == Module::Get()->core()->IsMainThread()); | 136 PP_DCHECK(is_main_thread_ == Module::Get()->core()->IsMainThread()); |
137 } | 137 } |
138 | 138 |
139 /// Acquires the fake "lock". This does nothing except perform checks in | 139 /// Acquires the fake "lock". This does nothing except perform checks in |
140 /// debug mode. | 140 /// debug mode. |
141 void Acquire() { | 141 void Acquire() { |
142 #ifndef NDEBUG | 142 #if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON) |
143 PP_DCHECK(!lock_held_); | 143 PP_DCHECK(!lock_held_); |
144 lock_held_ = true; | 144 lock_held_ = true; |
145 #endif | 145 #endif |
146 } | 146 } |
147 | 147 |
148 /// Releases the fake "lock". This does nothing except perform checks in | 148 /// Releases the fake "lock". This does nothing except perform checks in |
149 /// debug mode. | 149 /// debug mode. |
150 void Release() { | 150 void Release() { |
151 #ifndef NDEBUG | 151 #if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON) |
152 PP_DCHECK(lock_held_); | 152 PP_DCHECK(lock_held_); |
153 lock_held_ = false; | 153 lock_held_ = false; |
154 #endif | 154 #endif |
155 } | 155 } |
156 | 156 |
157 private: | 157 private: |
158 #ifndef NDEBUG | 158 #if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON) |
159 bool is_main_thread_; | 159 bool is_main_thread_; |
160 bool lock_held_; | 160 bool lock_held_; |
161 #endif | 161 #endif |
162 }; | 162 }; |
163 | 163 |
164 class AutoLock { | 164 class AutoLock { |
165 public: | 165 public: |
166 explicit AutoLock(Lock& lock) : lock_(lock) { | 166 explicit AutoLock(Lock& lock) : lock_(lock) { |
167 lock_.Acquire(); | 167 lock_.Acquire(); |
168 } | 168 } |
169 ~AutoLock() { | 169 ~AutoLock() { |
170 lock_.Release(); | 170 lock_.Release(); |
171 } | 171 } |
172 | 172 |
173 private: | 173 private: |
174 Lock& lock_; | 174 Lock& lock_; |
175 }; | 175 }; |
176 }; | 176 }; |
177 | 177 |
178 } // namespace pp | 178 } // namespace pp |
179 | 179 |
180 #endif // PPAPI_UTILITY_THREAD_SAFE_THREAD_TRAITS_H_ | 180 #endif // PPAPI_UTILITY_THREAD_SAFE_THREAD_TRAITS_H_ |
OLD | NEW |