Galois
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
SimpleLock.h
Go to the documentation of this file.
1 /*
2  * This file belongs to the Galois project, a C++ library for exploiting
3  * parallelism. The code is being released under the terms of the 3-Clause BSD
4  * License (a copy is located in LICENSE.txt at the top-level directory).
5  *
6  * Copyright (C) 2018, The University of Texas at Austin. All rights reserved.
7  * UNIVERSITY EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES CONCERNING THIS
8  * SOFTWARE AND DOCUMENTATION, INCLUDING ANY WARRANTIES OF MERCHANTABILITY,
9  * FITNESS FOR ANY PARTICULAR PURPOSE, NON-INFRINGEMENT AND WARRANTIES OF
10  * PERFORMANCE, AND ANY WARRANTY THAT MIGHT OTHERWISE ARISE FROM COURSE OF
11  * DEALING OR USAGE OF TRADE. NO WARRANTY IS EITHER EXPRESS OR IMPLIED WITH
12  * RESPECT TO THE USE OF THE SOFTWARE OR DOCUMENTATION. Under no circumstances
13  * shall University be liable for incidental, special, indirect, direct or
14  * consequential damages or loss of profits, interruption of business, or
15  * related expenses which may arise from use of Software or Documentation,
16  * including but not limited to those resulting from defects in Software and/or
17  * Documentation, or loss or inaccuracy of data of any kind.
18  */
19 
20 #ifndef GALOIS_SUBSTRATE_SIMPLELOCK_H
21 #define GALOIS_SUBSTRATE_SIMPLELOCK_H
22 
23 #include <atomic>
24 #include <cassert>
25 #include <mutex>
26 
27 #include "galois/config.h"
29 
30 namespace galois {
31 namespace substrate {
32 
35 
36 class SimpleLock {
37  mutable std::atomic<int> _lock;
38  void slow_lock() const;
39 
40 public:
41  constexpr SimpleLock() : _lock(0) {}
42  // relaxed order for copy
44  : _lock(p._lock.load(std::memory_order_relaxed)) {}
45 
47  if (&p == this)
48  return *this;
49  // relaxed order for initialization
50  _lock.store(p._lock.load(std::memory_order_relaxed),
51  std::memory_order_relaxed);
52  return *this;
53  }
54 
55  inline void lock() const {
56  int oldval = 0;
57  if (_lock.load(std::memory_order_relaxed))
58  goto slow_path;
59  if (!_lock.compare_exchange_weak(oldval, 1, std::memory_order_acq_rel,
60  std::memory_order_relaxed))
61  goto slow_path;
62  assert(is_locked());
63  return;
64  slow_path:
65  slow_lock();
66  }
67 
68  inline void unlock() const {
69  assert(is_locked());
70  // HMMMM
71  _lock.store(0, std::memory_order_release);
72  //_lock = 0;
73  }
74 
75  inline bool try_lock() const {
76  int oldval = 0;
77  if (_lock.load(std::memory_order_relaxed))
78  return false;
79  if (!_lock.compare_exchange_weak(oldval, 1, std::memory_order_acq_rel))
80  return false;
81  assert(is_locked());
82  return true;
83  }
84 
85  inline bool is_locked() const {
86  return _lock.load(std::memory_order_acquire) & 1;
87  }
88 };
89 
91 
92 class DummyLock {
93 public:
94  inline void lock() const {}
95  inline void unlock() const {}
96  inline bool try_lock() const { return true; }
97  inline bool is_locked() const { return false; }
98 };
99 
100 template <bool Enabled>
101 using CondLock =
102  typename std::conditional<Enabled, SimpleLock, DummyLock>::type;
103 
104 using lock_guard_galois = std::lock_guard<SimpleLock>;
105 
106 #define MAKE_LOCK_GUARD(__x) \
107  galois::substrate::lock_guard_galois locker##___COUNTER__(__x)
108 
109 } // end namespace substrate
110 } // end namespace galois
111 
112 #endif
SimpleLock & operator=(const SimpleLock &p)
Definition: SimpleLock.h:46
bool try_lock() const
Definition: SimpleLock.h:96
void lock() const
Definition: SimpleLock.h:94
constexpr SimpleLock()
Definition: SimpleLock.h:41
std::lock_guard< SimpleLock > lock_guard_galois
Definition: SimpleLock.h:104
bool is_locked() const
Definition: SimpleLock.h:85
typename std::conditional< Enabled, SimpleLock, DummyLock >::type CondLock
Definition: SimpleLock.h:102
bool is_locked() const
Definition: SimpleLock.h:97
void unlock() const
Definition: SimpleLock.h:68
bool try_lock() const
Definition: SimpleLock.h:75
SimpleLock(const SimpleLock &p)
Definition: SimpleLock.h:43
void unlock() const
Definition: SimpleLock.h:95
void lock() const
Definition: SimpleLock.h:55
SimpleLock is a spinlock.
Definition: SimpleLock.h:36
Dummy Lock implements the lock interface without a lock for serial code.
Definition: SimpleLock.h:92