1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 * Ceph - scalable distributed file system
6 * Copyright (C) 2004-2006 Sage Weil <sage@newdream.net>
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.
15 #ifndef CEPH_REFCOUNTEDOBJ_H
16 #define CEPH_REFCOUNTEDOBJ_H
18 #include "common/Mutex.h"
19 #include "common/Cond.h"
20 #include "common/ceph_context.h"
21 #include "common/valgrind.h"
23 // re-include our assert to clobber the system one; fix dout:
24 #include "include/assert.h"
26 struct RefCountedObject {
28 mutable std::atomic<uint64_t> nref;
31 RefCountedObject(CephContext *c = NULL, int n=1) : nref(n), cct(c) {}
32 virtual ~RefCountedObject() {
36 const RefCountedObject *get() const {
39 lsubdout(cct, refs, 1) << "RefCountedObject::get " << this << " "
40 << (v - 1) << " -> " << v
44 RefCountedObject *get() {
47 lsubdout(cct, refs, 1) << "RefCountedObject::get " << this << " "
48 << (v - 1) << " -> " << v
53 CephContext *local_cct = cct;
56 ANNOTATE_HAPPENS_AFTER(&nref);
57 ANNOTATE_HAPPENS_BEFORE_FORGET_ALL(&nref);
60 ANNOTATE_HAPPENS_BEFORE(&nref);
63 lsubdout(local_cct, refs, 1) << "RefCountedObject::put " << this << " "
64 << (v + 1) << " -> " << v
67 void set_cct(CephContext *c) {
71 uint64_t get_nref() const {
79 * a refcounted condition, will be removed when all references are dropped
82 struct RefCountedCond : public RefCountedObject {
88 RefCountedCond() : complete(false), lock("RefCountedCond"), rval(0) {}
91 Mutex::Locker l(lock);
99 Mutex::Locker l(lock);
111 * RefCountedWaitObject
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
121 struct RefCountedWaitObject {
122 std::atomic<uint64_t> nref = { 1 };
125 RefCountedWaitObject() {
126 c = new RefCountedCond;
128 virtual ~RefCountedWaitObject() {
132 RefCountedWaitObject *get() {
139 RefCountedCond *cond = c;
151 RefCountedCond *cond = c;
164 void intrusive_ptr_add_ref(const RefCountedObject *p);
165 void intrusive_ptr_release(const RefCountedObject *p);