OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef PPAPI_CPP_NON_THREAD_SAFE_REF_COUNT_H_ | |
6 #define PPAPI_CPP_NON_THREAD_SAFE_REF_COUNT_H_ | |
7 | |
8 #include "ppapi/cpp/core.h" | |
9 #include "ppapi/cpp/logging.h" | |
10 #include "ppapi/cpp/module.h" | |
11 | |
12 /// @file | |
13 /// This file defines the APIs for maintaining a reference counter. | |
14 namespace pp { | |
15 | |
16 /// A simple reference counter that is not thread-safe. <strong>Note:</strong> | |
17 /// in Debug mode, it checks that it is either called on the main thread, or | |
18 /// always called on another thread. | |
19 class NonThreadSafeRefCount { | |
20 public: | |
21 /// Default constructor. In debug mode, this checks that the object is being | |
22 /// created on the main thread. | |
23 NonThreadSafeRefCount() | |
24 : ref_(0) { | |
25 #ifndef NDEBUG | |
26 is_main_thread_ = Module::Get()->core()->IsMainThread(); | |
27 #endif | |
28 } | |
29 | |
30 /// Destructor. | |
31 ~NonThreadSafeRefCount() { | |
32 PP_DCHECK(is_main_thread_ == Module::Get()->core()->IsMainThread()); | |
33 } | |
34 | |
35 /// AddRef() increments the reference counter. | |
36 /// | |
37 /// @return An int32_t with the incremented reference counter. | |
38 int32_t AddRef() { | |
39 PP_DCHECK(is_main_thread_ == Module::Get()->core()->IsMainThread()); | |
40 return ++ref_; | |
41 } | |
42 | |
43 /// Release() decrements the reference counter. | |
44 /// | |
45 /// @return An int32_t with the decremeneted reference counter. | |
46 int32_t Release() { | |
47 PP_DCHECK(is_main_thread_ == Module::Get()->core()->IsMainThread()); | |
48 return --ref_; | |
49 } | |
50 | |
51 private: | |
52 int32_t ref_; | |
53 #ifndef NDEBUG | |
54 bool is_main_thread_; | |
55 #endif | |
56 }; | |
57 | |
58 } // namespace pp | |
59 | |
60 #endif // PPAPI_CPP_NON_THREAD_SAFE_REF_COUNT_H_ | |
OLD | NEW |