* 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>
include/Context.h\
include/CompatSet.h\
include/Distribution.h\
- include/Spinlock.h\
include/addr_parsing.h\
include/assert.h\
include/atomic.h\
+++ /dev/null
-// -*- 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
# include "acconfig.h"
#endif
-
#ifndef NO_ATOMIC_OPS
//libatomic_ops implementation
#include <atomic_ops.h>
+
namespace ceph {
class atomic_t {
AO_t val;
// 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
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
{
// 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;
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;
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"
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;
}
int get_queue_len() {
- qlen_lock.lock();
- int l = qlen;
- qlen_lock.unlock();
- return l;
+ return qlen.read();
}
void queue_connect(Connection *con) {
lock("SimpleMessenger::DispatchQeueu::lock"),
stop(false),
qlen(0),
- qlen_lock("SimpleMessenger::DispatchQueue::qlen_lock"),
local_pipe(NULL)
{}
~DispatchQueue() {