From e3d4a00d1b3317fb2514e69c11c308c1fb4b9d57 Mon Sep 17 00:00:00 2001 From: sageweil Date: Mon, 29 Oct 2007 21:01:36 +0000 Subject: [PATCH] atomic_t type; now used by buffer.h git-svn-id: https://ceph.svn.sf.net/svnroot/ceph@2003 29311d96-e01e-0410-9327-a35deaab8ce9 --- branches/sage/ebofs2/include/atomic.h | 56 +++++++++++++++++++++ branches/sage/ebofs2/include/buffer.h | 70 +++++---------------------- 2 files changed, 69 insertions(+), 57 deletions(-) create mode 100644 branches/sage/ebofs2/include/atomic.h diff --git a/branches/sage/ebofs2/include/atomic.h b/branches/sage/ebofs2/include/atomic.h new file mode 100644 index 0000000000000..17d9584db3a39 --- /dev/null +++ b/branches/sage/ebofs2/include/atomic.h @@ -0,0 +1,56 @@ +// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- +// vim: ts=8 sw=2 smarttab +/* + * Ceph - scalable distributed file system + * + * Copyright (C) 2004-2006 Sage Weil + * + * This is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1, as published by the Free Software + * Foundation. See file COPYING. + * + */ + +#ifndef __CEPH_ATOMIC_H +#define __CEPH_ATOMIC_H + +#ifdef BUFFER_USE_CCPP +# include "cc++/thread.h" + +class atomic_t { + mutable ost::AtomicCounter nref; // mutable for const-ness of operator<< +public: + atomic_t(int i=0) : nref(i) {} + void get() { ++nref; } + int put() { --nref; } + int test() const { return nref; } +}; + +#else +# include "common/Mutex.h" + +class atomic_t { + Mutex lock; + int nref; +public: + atomic_t(int i=0) : lock(false), nref(i) {} + void get() { + lock.Lock(); + ++nref; + lock.Unlock(); + } + int put() { + lock.Lock(); + int r = --nref; + lock.Unlock(); + return r; + } + int test() const { + return nref; + } +}; + +#endif + +#endif diff --git a/branches/sage/ebofs2/include/buffer.h b/branches/sage/ebofs2/include/buffer.h index 5e48b6ce91bf6..12be95a7a51c8 100644 --- a/branches/sage/ebofs2/include/buffer.h +++ b/branches/sage/ebofs2/include/buffer.h @@ -19,12 +19,7 @@ #include "common/Mutex.h" - -//#define BUFFER_USE_CCPP - -#ifdef BUFFER_USE_CCPP -# include "cc++/thread.h" -#endif +#include "atomic.h" #include #include @@ -68,22 +63,11 @@ private: public: char *data; unsigned len; -#ifdef BUFFER_USE_CCPP - mutable ost::AtomicCounter nref; // mutable for const-ness of operator<< -#else - int nref; - Mutex lock; // we'll make it non-recursive. -#endif + atomic_t nref; raw(unsigned l) : len(l), nref(0) -#ifndef BUFFER_USE_CCPP - , lock(false) -#endif { } raw(char *c, unsigned l) : data(c), len(l), nref(0) -#ifndef BUFFER_USE_CCPP - , lock(false) -#endif { } virtual ~raw() {}; @@ -230,48 +214,30 @@ public: public: ptr() : _raw(0), _off(0), _len(0) {} ptr(raw *r) : _raw(r), _off(0), _len(r->len) { // no lock needed; this is an unref raw. - ++r->nref; + r->nref.get(); } ptr(unsigned l) : _off(0), _len(l) { _raw = create(l); - ++_raw->nref; + _raw->nref.get(); } ptr(char *d, unsigned l) : _off(0), _len(l) { // ditto. _raw = copy(d, l); - ++_raw->nref; + _raw->nref.get(); } ptr(const ptr& p) : _raw(p._raw), _off(p._off), _len(p._len) { if (_raw) { -#ifdef BUFFER_USE_CCPP - ++_raw->nref; -#else - _raw->lock.Lock(); - ++_raw->nref; - _raw->lock.Unlock(); -#endif + _raw->nref.get(); } } ptr(const ptr& p, unsigned o, unsigned l) : _raw(p._raw), _off(p._off + o), _len(l) { assert(o+l <= p._len); assert(_raw); -#ifdef BUFFER_USE_CCPP - ++_raw->nref; -#else - _raw->lock.Lock(); - ++_raw->nref; - _raw->lock.Unlock(); -#endif + _raw->nref.get(); } ptr& operator= (const ptr& p) { // be careful -- we need to properly handle self-assignment. if (p._raw) { -#ifdef BUFFER_USE_CCPP - ++p._raw->nref; // inc new -#else - p._raw->lock.Lock(); - ++p._raw->nref; // inc new - p._raw->lock.Unlock(); -#endif + p._raw->nref.get(); // inc new } release(); // dec (+ dealloc) old (if any) _raw = p._raw; // change my ref @@ -288,11 +254,11 @@ public: } bool do_cow() { - if (_raw->nref > 1) { + if (_raw->nref.test() > 1) { //std::cout << "doing cow on " << _raw << " len " << _len << std::endl; raw *newraw = _raw->clone(); release(); - newraw->nref++; + newraw->nref.get(); _raw = newraw; return true; } else @@ -313,19 +279,9 @@ public: void release() { if (_raw) { -#ifndef BUFFER_USE_CCPP - _raw->lock.Lock(); -#endif - if (--_raw->nref == 0) { + if (_raw->nref.put() == 0) { //cout << "hosing raw " << (void*)_raw << " len " << _raw->len << std::endl; -#ifndef BUFFER_USE_CCPP - _raw->lock.Unlock(); -#endif delete _raw; // dealloc old (if any) - } else { -#ifndef BUFFER_USE_CCPP - _raw->lock.Unlock(); -#endif } _raw = 0; } @@ -364,7 +320,7 @@ public: const char *raw_c_str() const { assert(_raw); return _raw->data; } unsigned raw_length() const { assert(_raw); return _raw->len; } - int raw_nref() const { assert(_raw); return _raw->nref; } + int raw_nref() const { assert(_raw); return _raw->nref.test(); } void copy_out(unsigned o, unsigned l, char *dest) const { assert(_raw); @@ -922,7 +878,7 @@ inline bool operator<=(bufferlist& l, bufferlist& r) { inline std::ostream& operator<<(std::ostream& out, const buffer::raw &r) { - return out << "buffer::raw(" << (void*)r.data << " len " << r.len << " nref " << r.nref << ")"; + return out << "buffer::raw(" << (void*)r.data << " len " << r.len << " nref " << r.nref.test() << ")"; } inline std::ostream& operator<<(std::ostream& out, const buffer::ptr& bp) { -- 2.39.5