libfoedus-core
FOEDUS Core Library
dumb_spinlock.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014-2015, Hewlett-Packard Development Company, LP.
3  * This program is free software; you can redistribute it and/or modify it
4  * under the terms of the GNU General Public License as published by the Free
5  * Software Foundation; either version 2 of the License, or (at your option)
6  * any later version.
7  *
8  * This program is distributed in the hope that it will be useful, but WITHOUT
9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11  * more details. You should have received a copy of the GNU General Public
12  * License along with this program; if not, write to the Free Software
13  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
14  *
15  * HP designates this particular file as subject to the "Classpath" exception
16  * as provided by HP in the LICENSE.txt file that accompanied this code.
17  */
18 #ifndef FOEDUS_ASSORTED_DUMB_SPINLOCK_HPP_
19 #define FOEDUS_ASSORTED_DUMB_SPINLOCK_HPP_
20 
21 #include "foedus/assert_nd.hpp"
25 
26 namespace foedus {
27 namespace assorted {
28 
40 class DumbSpinlock {
41  public:
42  DumbSpinlock(bool* locked, bool lock_initially = true) : locked_by_me_(false), locked_(locked) {
43  if (lock_initially) {
44  lock();
45  }
46  }
49 
50  bool is_locked_by_me() const { return locked_by_me_; }
51 
53  void lock() {
54  SPINLOCK_WHILE(true) {
55  if (try_lock()) {
56  break;
57  }
58  }
59  }
61  bool try_lock() {
62  if (locked_by_me_) {
63  return true; // already locked
64  }
65 
66  bool expected = false;
67  if (raw_atomic_compare_exchange_weak<bool>(locked_, &expected, true)) {
68  ASSERT_ND(*locked_);
69  locked_by_me_ = true;
70  return true;
71  } else {
72  ASSERT_ND(expected);
73  return false;
74  }
75  }
76 
78  void unlock() {
79  if (locked_by_me_) {
80  ASSERT_ND(*locked_);
81  locked_by_me_ = false;
83  *locked_ = false;
85  }
86  }
87 
88  private:
89  bool locked_by_me_;
90  bool* const locked_;
91 };
92 
93 } // namespace assorted
94 } // namespace foedus
95 
96 #endif // FOEDUS_ASSORTED_DUMB_SPINLOCK_HPP_
Root package of FOEDUS (Fast Optimistic Engine for Data Unification Services).
Definition: assert_nd.hpp:44
A simple spinlock using a boolean field.
DumbSpinlock(bool *locked, bool lock_initially=true)
bool try_lock()
try-version of the lock.
#define SPINLOCK_WHILE(x)
A macro to busy-wait (spinlock) with occasional pause.
~DumbSpinlock()
automatically unlocks when out of scope.
void lock()
Locks it if I haven't locked it yet.
void unlock()
Unlocks it if I locked it.
Atomic fence methods and load/store with fences that work for both C++11/non-C++11 code...
#define ASSERT_ND(x)
A warning-free wrapper macro of assert() that has no performance effect in release mode even when 'x'...
Definition: assert_nd.hpp:72
Raw atomic operations that work for both C++11 and non-C++11 code.
void memory_fence_acq_rel()
Equivalent to std::atomic_thread_fence(std::memory_order_acq_rel).