]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
include/atomic cleanup
authorColin Patrick McCabe <cmccabe@alumni.cmu.edu>
Fri, 10 Jun 2011 21:43:28 +0000 (14:43 -0700)
committerColin Patrick McCabe <cmccabe@alumni.cmu.edu>
Mon, 20 Jun 2011 23:22:26 +0000 (16:22 -0700)
* Don't allow copying of class atomic_t.
* Remove common/Spinlock.h because it's unecessary
* SimpleMessenger: use atomic var for qlen

Signed-off-by: Colin McCabe <colin.mccabe@dreamhost.com>
src/Makefile.am
src/include/Spinlock.h [deleted file]
src/include/atomic.h
src/msg/SimpleMessenger.cc
src/msg/SimpleMessenger.h

index 0455dbfb635abb567653302e2388e2fa13ab17f9..32f616492bd047dccc84ebbfd0e82b76e6af894d 100644 (file)
@@ -844,7 +844,6 @@ noinst_HEADERS = \
         include/Context.h\
        include/CompatSet.h\
         include/Distribution.h\
-       include/Spinlock.h\
        include/addr_parsing.h\
        include/assert.h\
         include/atomic.h\
diff --git a/src/include/Spinlock.h b/src/include/Spinlock.h
deleted file mode 100644 (file)
index 690c87c..0000000
+++ /dev/null
@@ -1,127 +0,0 @@
-// -*- 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 <sage@newdream.net>
- *
- * 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_SPINLOCK_H
-#define CEPH_SPINLOCK_H
-
-#include <pthread.h>
-#include "assert.h"
-
-namespace ceph {
-
-//#define SPINLOCK_LOCKDEP
-
-#ifdef SPINLOCK_LOCKDEP
-# include "lockdep.h"
-#endif
-
-class Spinlock {
-private:
-  pthread_spinlock_t _s;
-  int nlock;
-
-  // don't allow copying.
-  void operator=(Spinlock &M) {}
-  Spinlock( const Spinlock &M ) {}
-
-#ifdef SPINLOCK_LOCKDEP
-  const char *name;
-  int id;
-  bool lockdep;
-  bool backtrace;  // gather backtrace on lock acquisition
-
-  void _register() {
-    if (lockdep && g_lockdep)
-      id = lockdep_register(name);
-  }
-  void _will_lock() { // about to lock
-    if (lockdep && g_lockdep)
-      id = lockdep_will_lock(name, id);
-  }
-  void _locked() {    // just locked
-    if (lockdep && g_lockdep)
-      id = lockdep_locked(name, id, backtrace);
-  }
-  void _will_unlock() {  // about to unlock
-    if (lockdep && g_lockdep)
-      id = lockdep_will_unlock(name, id);
-  }
-#else
-  void _register() {}
-  void _will_lock() {} // about to lock
-  void _locked() {}    // just locked
-  void _will_unlock() {}  // about to unlock
-#endif
-
-public:
-  Spinlock(const char *n, bool ld=true, bool bt=false) :
-    nlock(0)
-#ifdef SPINLOCK_LOCKDEP
-    , name(n), id(-1), lockdep(ld), backtrace(bt)
-#endif
-  {
-    pthread_spin_init(&_s, 0);
-    _register();
-  }
-  ~Spinlock() {
-    assert(nlock == 0);
-    pthread_spin_destroy(&_s); 
-  }
-
-  bool is_locked() {
-    return (nlock > 0);
-  }
-
-  bool try_lock() {
-    int r = pthread_spin_trylock(&_s);
-    if (r == 0) {
-      _locked();
-      nlock++;
-    }
-    return r == 0;
-  }
-
-  void lock() {
-    _will_lock();
-    int r = pthread_spin_lock(&_s);
-    _locked();
-    assert(r == 0);
-    nlock++;
-  }
-
-  void unlock() {
-    assert(nlock > 0);
-    --nlock;
-    _will_unlock();
-    int r = pthread_spin_unlock(&_s);
-    assert(r == 0);
-  }
-
-public:
-  class Locker {
-    Spinlock &spinlock;
-
-  public:
-    Locker(Spinlock& m) : spinlock(m) {
-      spinlock.lock();
-    }
-    ~Locker() {
-      spinlock.unlock();
-    }
-  };
-};
-
-}
-
-#endif
index 28cc7df095183064a8c9da47cc06a7834efe3e97..6fe43b9bf88e71af506d610b49fff57b7d3682b2 100644 (file)
 # include "acconfig.h"
 #endif
 
-
 #ifndef NO_ATOMIC_OPS
 //libatomic_ops implementation
 #include <atomic_ops.h>
+
 namespace ceph {
   class atomic_t {
     AO_t val;
@@ -48,49 +48,64 @@ namespace ceph {
       // at some point.  this hack can go away someday...
       return AO_load_full((AO_t *)&val);
     }
+  private:
+    // forbid copying
+    atomic_t(const atomic_t &other);
+    atomic_t &operator=(const atomic_t &rhs);
   };
 }
 #else
 /*
  * crappy slow implementation that uses a pthreads spinlock.
  */
-#include "include/Spinlock.h"
+#include <pthread.h>
 #include "include/assert.h"
 
 namespace ceph {
   class atomic_t {
-    Spinlock lock;
-    long nref;
+    pthread_spin_lock lock;
+    signed long val;
   public:
-    atomic_t(int i=0) : lock("atomic_t::lock", false /* no lockdep */), nref(i) {}
-    atomic_t(const atomic_t& other);
+    atomic_t(int i=0)
+      : val(i) {
+      pthread_spin_init(&lock);
+    }
+    ~atomic_t() {
+      pthread_spin_destroy(&lock);
+    }
     int inc() {
-      lock.lock();
-      int r = ++nref;
-      lock.unlock();
+      pthread_spin_lock(&lock);
+      int r = ++val;
+      pthread_spin_unlock(&lock);
       return r;
     }
     int dec() {
-      lock.lock();
-      assert(nref > 0);
-      int r = --nref;
-      lock.unlock();
+      pthread_spin_lock(&lock);
+      int r = --val;
+      pthread_spin_unlock(&lock);
       return r;
     }
     void add(int d) {
-      lock.lock();
-      nref += d;
-      lock.unlock();
+      pthread_spin_lock(&lock);
+      val += d;
+      pthread_spin_unlock(&lock);
     }
     void sub(int d) {
-      lock.lock();
-      assert(nref >= d);
-      nref -= d;
-      lock.unlock();
+      pthread_spin_lock(&lock);
+      val -= d;
+      pthread_spin_unlock(&lock);
     }
     int read() const {
-      return nref;
+      signed long ret;
+      pthread_spin_lock(&lock);
+      ret = val;
+      pthread_spin_unlock(&lock);
+      return ret;
     }
+  private:
+    // forbid copying
+    atomic_t(const atomic_t &other);
+    atomic_t &operator=(const atomic_t &rhs);
   };
 }
 #endif
index 99f300a642bda87464bfd71cf1044affba9f4960..046a92c80127d7cf03a723c844494bce9ec387bf 100644 (file)
@@ -310,9 +310,7 @@ void SimpleMessenger::dispatch_entry()
       dispatch_queue.lock.Unlock(); //done with the pipe queue for a while
 
       pipe->in_qlen--;
-      dispatch_queue.qlen_lock.lock();
-      dispatch_queue.qlen--;
-      dispatch_queue.qlen_lock.unlock();
+      dispatch_queue.qlen.dec();
 
       pipe->pipe_lock.Unlock(); // done with the pipe's message queue now
        {
@@ -566,9 +564,7 @@ void SimpleMessenger::Pipe::queue_received(Message *m, int priority)
   
   // increment queue length counters
   in_qlen++;
-  messenger->dispatch_queue.qlen_lock.lock();
-  ++messenger->dispatch_queue.qlen;
-  messenger->dispatch_queue.qlen_lock.unlock();
+  messenger->dispatch_queue.qlen.inc();
   
   return;
   
@@ -1378,9 +1374,7 @@ void SimpleMessenger::Pipe::discard_queue()
   dout(20) << " dequeued pipe " << dendl;
 
   // adjust qlen
-  q.qlen_lock.lock();
-  q.qlen -= in_qlen;
-  q.qlen_lock.unlock();
+  q.qlen.sub(in_qlen);
 
   for (list<Message*>::iterator p = sent.begin(); p != sent.end(); p++) {
     dout(20) << "  discard " << *p << dendl;
index e5f4fdfbb83cd1b8a5afddf9ada77f1ca61805cb..4bd8c7afbbc9abf27ce491e502e19bcb62d81152 100644 (file)
@@ -26,7 +26,7 @@ using namespace std;
 using namespace __gnu_cxx;
 
 #include "common/Mutex.h"
-#include "include/Spinlock.h"
+#include "include/atomic.h"
 #include "common/Cond.h"
 #include "common/Thread.h"
 #include "common/Throttle.h"
@@ -368,8 +368,7 @@ private:
 
     map<int, xlist<Pipe *> > queued_pipes;
     map<int, xlist<Pipe *>::iterator> queued_pipe_iters;
-    int qlen;
-    Spinlock qlen_lock;
+    atomic_t qlen;
     
     enum { D_CONNECT, D_BAD_REMOTE_RESET, D_BAD_RESET };
     list<Connection*> connect_q;
@@ -386,10 +385,7 @@ private:
     }
 
     int get_queue_len() {
-      qlen_lock.lock();
-      int l = qlen;
-      qlen_lock.unlock();
-      return l;
+      return qlen.read();
     }
     
     void queue_connect(Connection *con) {
@@ -415,7 +411,6 @@ private:
       lock("SimpleMessenger::DispatchQeueu::lock"), 
       stop(false),
       qlen(0),
-      qlen_lock("SimpleMessenger::DispatchQueue::qlen_lock"),
       local_pipe(NULL)
     {}
     ~DispatchQueue() {