summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSage Weil <sage@inktank.com>2013-09-11 18:00:09 -0700
committerSage Weil <sage@inktank.com>2013-10-16 09:28:13 -0700
commit87577753f4bc1043016bfdb548c43214a7d58f4e (patch)
treeb7f4c4484ffb3f154037ef49298a3e58b2f28188
parenta49d66895d19d50d9b761dee5adcb62f49dbd2d7 (diff)
downloadceph-87577753f4bc1043016bfdb548c43214a7d58f4e.tar.gz
include: add Spinlock
Signed-off-by: Sage Weil <sage@inktank.com>
-rw-r--r--src/include/Makefile.am1
-rw-r--r--src/include/Spinlock.h57
2 files changed, 58 insertions, 0 deletions
diff --git a/src/include/Makefile.am b/src/include/Makefile.am
index c8823ce523d..34976a6cc29 100644
--- a/src/include/Makefile.am
+++ b/src/include/Makefile.am
@@ -21,6 +21,7 @@ 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
new file mode 100644
index 00000000000..6154ae1124b
--- /dev/null
+++ b/src/include/Spinlock.h
@@ -0,0 +1,57 @@
+// -*- 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) 2013 Inktank
+ *
+ * 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.
+ *
+ * @author Sage Weil <sage@inktank.com>
+ */
+
+#ifndef CEPH_SPINLOCK_H
+#define CEPH_SPINLOCK_H
+
+#include <pthread.h>
+
+class Spinlock {
+ mutable pthread_spinlock_t _lock;
+
+public:
+ Spinlock() {
+ pthread_spin_init(&_lock, PTHREAD_PROCESS_PRIVATE);
+ }
+ ~Spinlock() {
+ pthread_spin_destroy(&_lock);
+ }
+
+ // don't allow copying.
+ void operator=(Spinlock& s);
+ Spinlock(const Spinlock& s);
+
+ /// acquire spinlock
+ void lock() const {
+ pthread_spin_lock(&_lock);
+ }
+ /// release spinlock
+ void unlock() const {
+ pthread_spin_unlock(&_lock);
+ }
+
+ class Locker {
+ const Spinlock& spinlock;
+ public:
+ Locker(const Spinlock& s) : spinlock(s) {
+ spinlock.lock();
+ }
+ ~Locker() {
+ spinlock.unlock();
+ }
+ };
+};
+
+#endif