]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
include: add Spinlock
authorSage Weil <sage@inktank.com>
Thu, 12 Sep 2013 01:00:09 +0000 (18:00 -0700)
committerSage Weil <sage@inktank.com>
Wed, 16 Oct 2013 16:28:13 +0000 (09:28 -0700)
Signed-off-by: Sage Weil <sage@inktank.com>
src/include/Makefile.am
src/include/Spinlock.h [new file with mode: 0644]

index c8823ce523dd0df8f278b4cc7030dd068503f904..34976a6cc29909d428913cf36ee18291febecc66 100644 (file)
@@ -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 (file)
index 0000000..6154ae1
--- /dev/null
@@ -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