Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1050)

Side by Side Diff: native_client_sdk/src/libraries/sdk_util/ref_object.h

Issue 18644009: [NaCl SDK] Upate atomic ops in nacl_io (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Add declartions for newval and oldval Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 5
6 #ifndef LIBRARIES_SDK_UTIL_REF_OBJECT 6 #ifndef LIBRARIES_SDK_UTIL_REF_OBJECT
7 #define LIBRARIES_SDK_UTIL_REF_OBJECT 7 #define LIBRARIES_SDK_UTIL_REF_OBJECT
8 8
9 #include <stdlib.h> 9 #include <stdlib.h>
10 #include "pthread.h" 10 #include "pthread.h"
11 11
12 #include "sdk_util/atomicops.h" 12 #include "sdk_util/atomicops.h"
13 13
14 class ScopedRefBase; 14 class ScopedRefBase;
15 15
16 /* 16 /*
17 * RefObject 17 * RefObject
18 * 18 *
19 * A reference counted object with a lock. The lock protects the data within 19 * A reference counted object. RefObjects should only be handled by ScopedRef
20 * the object, but not the reference counting which happens through atomic 20 * objects.
21 * operations. RefObjects should only be handled by ScopedRef objects.
22 * 21 *
23 * When the object is first created, it has a reference count of zero. It's 22 * When the object is first created, it has a reference count of zero. It's
24 * first incremented when it gets assigned to a ScopedRef. When the last 23 * first incremented when it gets assigned to a ScopedRef. When the last
25 * ScopedRef is reset or destroyed the object will get released. 24 * ScopedRef is reset or destroyed the object will get released.
26 */ 25 */
27 26
28 class RefObject { 27 class RefObject {
29 public: 28 public:
30 RefObject() { 29 RefObject() {
31 ref_count_ = 0; 30 ref_count_ = 0;
32 pthread_mutex_init(&lock_, NULL);
33 } 31 }
34 32
35 /* 33 /*
36 * RefCount 34 * RefCount
37 * 35 *
38 * RefCount returns an instantaneous snapshot of the RefCount, which may 36 * RefCount returns an instantaneous snapshot of the RefCount, which may
39 * change immediately after it is fetched. This is only stable when all 37 * change immediately after it is fetched. This is only stable when all
40 * pointers to the object are scoped pointers (ScopedRef), and the value 38 * pointers to the object are scoped pointers (ScopedRef), and the value
41 * is one implying the current thread is the only one, with visibility to 39 * is one implying the current thread is the only one, with visibility to
42 * the object. 40 * the object.
43 */ 41 */
44 int RefCount() const { return ref_count_; } 42 int RefCount() const { return ref_count_; }
45 43
46 protected: 44 protected:
45 virtual ~RefObject() {}
46
47 // Override to clean up object when last reference is released.
48 virtual void Destroy() {}
49
47 void Acquire() { 50 void Acquire() {
48 AtomicAddFetch(&ref_count_, 1); 51 AtomicAddFetch(&ref_count_, 1);
49 } 52 }
50 53
51 bool Release() { 54 bool Release() {
52 Atomic32 val = AtomicAddFetch(&ref_count_, -1); 55 Atomic32 val = AtomicAddFetch(&ref_count_, -1);
53 if (val == 0) { 56 if (val == 0) {
54 Destroy(); 57 Destroy();
55 delete this; 58 delete this;
56 return false; 59 return false;
57 } 60 }
58 return true; 61 return true;
59 } 62 }
60 63
61 virtual ~RefObject() {
62 pthread_mutex_destroy(&lock_);
63 }
64
65 // Override to clean up object when last reference is released.
66 virtual void Destroy() {}
67 pthread_mutex_t lock_;
68
69 private: 64 private:
70 Atomic32 ref_count_; 65 Atomic32 ref_count_;
71 66
72 friend class ScopedRefBase; 67 friend class ScopedRefBase;
73 }; 68 };
74 69
75 #endif // LIBRARIES_SDK_UTIL_REF_OBJECT 70 #endif // LIBRARIES_SDK_UTIL_REF_OBJECT
76 71
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698