ash: Update app list layout:
[chromium-blink-merge.git] / ppapi / utility / non_thread_safe_ref_count.h
blobdb00b7df7db7ecb15822199c6e905a47a7d490b2
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.
5 #ifndef PPAPI_UTILITY_NON_THREAD_SAFE_REF_COUNT_H_
6 #define PPAPI_UTILITY_NON_THREAD_SAFE_REF_COUNT_H_
8 #include "ppapi/cpp/core.h"
9 #include "ppapi/cpp/logging.h"
10 #include "ppapi/cpp/module.h"
12 /// @file
13 /// This file defines the APIs for maintaining a reference counter.
14 namespace pp {
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
30 /// Destructor.
31 ~NonThreadSafeRefCount() {
32 PP_DCHECK(is_main_thread_ == Module::Get()->core()->IsMainThread());
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_;
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_;
51 private:
52 int32_t ref_;
53 #ifndef NDEBUG
54 bool is_main_thread_;
55 #endif
58 } // namespace pp
60 #endif // PPAPI_UTILITY_NON_THREAD_SAFE_REF_COUNT_H_