Ceph引用计数对象实现

类成员:

struct RefCountedObject {
  private:
    atomic_t nref;
    CephContext *cct;

原子变量nref用于对cct对象进行计数,每当调用get函数,计数加一:

  RefCountedObject *get() {
    int v = nref.inc();
    if (cct)
    lsubdout(cct, refs, 1) << "RefCountedObject::get " << this << " "<< (v - 1) << " -> " << v<< dendl;
    return this;
  }

调用put函数,计数减一:

  void put() {
    CephContext *local_cct = cct;
    int v = nref.dec();
    if (v == 0) {
      ANNOTATE_HAPPENS_AFTER(&nref);
      ANNOTATE_HAPPENS_BEFORE_FORGET_ALL(&nref);
      delete this;
    } else {
      ANNOTATE_HAPPENS_BEFORE(&nref);
    }
    if (local_cct)
        lsubdout(local_cct, refs, 1) << "RefCountedObject::put " << this << " "<< (v + 1) << " -> " << v<< dendl;
  }

你可能感兴趣的:(Ceph引用计数对象实现)