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

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: 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:
47 void Acquire() { 45 void Acquire() {
48 AtomicAddFetch(&ref_count_, 1); 46 AtomicAddFetch(&ref_count_, 1);
49 } 47 }
50 48
51 bool Release() { 49 bool Release() {
52 Atomic32 val = AtomicAddFetch(&ref_count_, -1); 50 Atomic32 val = AtomicAddFetch(&ref_count_, -1);
53 if (val == 0) { 51 if (val == 0) {
54 Destroy(); 52 Destroy();
55 delete this; 53 delete this;
56 return false; 54 return false;
57 } 55 }
58 return true; 56 return true;
59 } 57 }
60 58
61 virtual ~RefObject() {
62 pthread_mutex_destroy(&lock_);
63 }
64
65 // Override to clean up object when last reference is released. 59 // Override to clean up object when last reference is released.
66 virtual void Destroy() {} 60 virtual void Destroy() {}
67 pthread_mutex_t lock_;
68 61
69 private: 62 private:
70 Atomic32 ref_count_; 63 Atomic32 ref_count_;
71 64
72 friend class ScopedRefBase; 65 friend class ScopedRefBase;
73 }; 66 };
74 67
75 #endif // LIBRARIES_SDK_UTIL_REF_OBJECT 68 #endif // LIBRARIES_SDK_UTIL_REF_OBJECT
76 69
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698