]> git.proxmox.com Git - ceph.git/blob - ceph/src/common/RefCountedObj.h
update sources to v12.1.0
[ceph.git] / ceph / src / common / RefCountedObj.h
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3 /*
4 * Ceph - scalable distributed file system
5 *
6 * Copyright (C) 2004-2006 Sage Weil <sage@newdream.net>
7 *
8 * This is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License version 2.1, as published by the Free Software
11 * Foundation. See file COPYING.
12 *
13 */
14
15 #ifndef CEPH_REFCOUNTEDOBJ_H
16 #define CEPH_REFCOUNTEDOBJ_H
17
18 #include "common/Mutex.h"
19 #include "common/Cond.h"
20 #include "common/ceph_context.h"
21 #include "common/valgrind.h"
22
23 // re-include our assert to clobber the system one; fix dout:
24 #include "include/assert.h"
25
26 struct RefCountedObject {
27 private:
28 mutable std::atomic<uint64_t> nref;
29 CephContext *cct;
30 public:
31 RefCountedObject(CephContext *c = NULL, int n=1) : nref(n), cct(c) {}
32 virtual ~RefCountedObject() {
33 assert(nref == 0);
34 }
35
36 const RefCountedObject *get() const {
37 int v = ++nref;
38 if (cct)
39 lsubdout(cct, refs, 1) << "RefCountedObject::get " << this << " "
40 << (v - 1) << " -> " << v
41 << dendl;
42 return this;
43 }
44 RefCountedObject *get() {
45 int v = ++nref;
46 if (cct)
47 lsubdout(cct, refs, 1) << "RefCountedObject::get " << this << " "
48 << (v - 1) << " -> " << v
49 << dendl;
50 return this;
51 }
52 void put() const {
53 CephContext *local_cct = cct;
54 int v = --nref;
55 if (v == 0) {
56 ANNOTATE_HAPPENS_AFTER(&nref);
57 ANNOTATE_HAPPENS_BEFORE_FORGET_ALL(&nref);
58 delete this;
59 } else {
60 ANNOTATE_HAPPENS_BEFORE(&nref);
61 }
62 if (local_cct)
63 lsubdout(local_cct, refs, 1) << "RefCountedObject::put " << this << " "
64 << (v + 1) << " -> " << v
65 << dendl;
66 }
67 void set_cct(CephContext *c) {
68 cct = c;
69 }
70
71 uint64_t get_nref() const {
72 return nref;
73 }
74 };
75
76 /**
77 * RefCountedCond
78 *
79 * a refcounted condition, will be removed when all references are dropped
80 */
81
82 struct RefCountedCond : public RefCountedObject {
83 bool complete;
84 Mutex lock;
85 Cond cond;
86 int rval;
87
88 RefCountedCond() : complete(false), lock("RefCountedCond"), rval(0) {}
89
90 int wait() {
91 Mutex::Locker l(lock);
92 while (!complete) {
93 cond.Wait(lock);
94 }
95 return rval;
96 }
97
98 void done(int r) {
99 Mutex::Locker l(lock);
100 rval = r;
101 complete = true;
102 cond.SignalAll();
103 }
104
105 void done() {
106 done(0);
107 }
108 };
109
110 /**
111 * RefCountedWaitObject
112 *
113 * refcounted object that allows waiting for the object's last reference.
114 * Any referrer can either put or put_wait(). A simple put() will return
115 * immediately, a put_wait() will return only when the object is destroyed.
116 * e.g., useful when we want to wait for a specific event completion. We
117 * use RefCountedCond, as the condition can be referenced after the object
118 * destruction.
119 *
120 */
121 struct RefCountedWaitObject {
122 std::atomic<uint64_t> nref = { 1 };
123 RefCountedCond *c;
124
125 RefCountedWaitObject() {
126 c = new RefCountedCond;
127 }
128 virtual ~RefCountedWaitObject() {
129 c->put();
130 }
131
132 RefCountedWaitObject *get() {
133 nref++;
134 return this;
135 }
136
137 bool put() {
138 bool ret = false;
139 RefCountedCond *cond = c;
140 cond->get();
141 if (--nref == 0) {
142 cond->done();
143 delete this;
144 ret = true;
145 }
146 cond->put();
147 return ret;
148 }
149
150 void put_wait() {
151 RefCountedCond *cond = c;
152
153 cond->get();
154 if (--nref == 0) {
155 cond->done();
156 delete this;
157 } else {
158 cond->wait();
159 }
160 cond->put();
161 }
162 };
163
164 void intrusive_ptr_add_ref(const RefCountedObject *p);
165 void intrusive_ptr_release(const RefCountedObject *p);
166
167 #endif