File: monitor.hpp

package info (click to toggle)
rocm-hipamd 6.4.3-5
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 23,036 kB
  • sloc: cpp: 211,057; ansic: 35,860; sh: 755; python: 623; perl: 275; asm: 166; makefile: 27
file content (283 lines) | stat: -rw-r--r-- 8,145 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/* Copyright (c) 2008 - 2021 Advanced Micro Devices, Inc.

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE. */

#ifndef MONITOR_HPP_
#define MONITOR_HPP_

#include "top.hpp"
#include "utils/flags.hpp"
#include "thread/semaphore.hpp"
#include "thread/thread.hpp"
#include <condition_variable>
#include <mutex>
#include <atomic>
#include <tuple>
#include <utility>

namespace amd {

/*! \addtogroup Threads
 *  @{
 *
 *  \addtogroup Synchronization
 *  @{
 */

namespace details {

template <class T, class AllocClass = HeapObject> struct SimplyLinkedNode : public AllocClass {
  typedef SimplyLinkedNode<T, AllocClass> Node;

 protected:
  std::atomic<Node*> next_; /*!< \brief The next element. */
  T volatile item_;

 public:
  //! \brief Return the next element in the linked-list.
  Node* next() const { return next_; }
  //! \brief Return the item.
  T item() const { return item_; }

  //! \brief Set the next element pointer.
  void setNext(Node* next) { next_ = next; }
  //! \brief Set the item.
  void setItem(T item) { item_ = item; }

  //! \brief Swap the next element pointer.
  Node* swapNext(Node* next) { return next_.swap(next); }

  //! \brief Compare and set the next element pointer.
  bool compareAndSetNext(Node* compare, Node* next) {
    return next_.compare_exchange_strong(compare, next);
  }
};

}  // namespace details

class MonitorBase {
public:
  virtual ~MonitorBase() = 0;
  virtual bool tryLock() = 0;
  virtual void lock() = 0;
  virtual void unlock() = 0;
  virtual void wait() = 0;
  virtual void notify() = 0;
  virtual void notifyAll() = 0;
};

namespace legacy_monitor {
class Monitor final: public HeapObject, public MonitorBase {
  typedef details::SimplyLinkedNode<Semaphore*, StackObject> LinkedNode;

 private:
  static constexpr intptr_t kLockBit = 0x1;

  static constexpr int kMaxSpinIter = 55;      //!< Total number of spin iterations.
  static constexpr int kMaxReadSpinIter = 50;  //!< Read iterations before yielding

  /*! Linked list of semaphores the contending threads are waiting on
   *  and main lock.
   */
  std::atomic_intptr_t contendersList_;

  //! Semaphore of the next thread to contend for the lock.
  std::atomic_intptr_t onDeck_;
  //! Linked list of the suspended threads resume semaphores.
  LinkedNode* volatile waitersList_;

  //! Thread owning this monitor.
  Thread* volatile owner_;
  //! The amount of times this monitor was acquired by the owner.
  uint32_t lockCount_;
  //! True if this is a recursive mutex, false otherwise.
  const bool recursive_;

 private:
  //! Finish locking the mutex (contented case).
  void finishLock();
  //! Finish unlocking the mutex (contented case).
  void finishUnlock();

 protected:
  //! Try to spin-acquire the lock, return true if successful.
  bool trySpinLock();

  /*! \brief Return true if the lock is owned.
   *
   *  \note The user is responsible for the memory ordering.
   */
  bool isLocked() const { return (contendersList_ & kLockBit) != 0; }

  //! Return this monitor's owner thread (NULL if unlocked).
  Thread* owner() const { return owner_; }

  //! Set the owner.
  void setOwner(Thread* thread) { owner_ = thread; }

 public:
  explicit Monitor(bool recursive = false);
  ~Monitor() {}

  //! Try to acquire the lock, return true if successful.
  bool tryLock();

  //! Acquire the lock or suspend the calling thread.
  void lock();

  //! Release the lock and wake a single waiting thread if any.
  void unlock();

  /*! \brief Give up the lock and go to sleep.
   *
   *  Calling wait() causes the current thread to go to sleep until
   *  another thread calls notify()/notifyAll().
   *
   *  \note The monitor must be owned before calling wait().
   */
  void wait();
  /*! \brief Wake up a single thread waiting on this monitor.
   *
   *  \note The monitor must be owned before calling notify().
   */
  void notify();
  /*! \brief Wake up all threads that are waiting on this monitor.
   *
   *  \note The monitor must be owned before calling notifyAll().
   */
  void notifyAll();
};


} // namespace legacy_monitor

namespace mutex_monitor {
class Monitor final: public HeapObject, public MonitorBase {
 public:
  explicit Monitor(bool recursive = false)
      : recursive_(recursive) {
    if (recursive)
      new (&rec_mutex_) std::recursive_mutex();
    else
      new (&mutex_) std::mutex();
  }

  ~Monitor() {
    // Caller must make sure the mutext is unlocked.
    if (recursive_)
      rec_mutex_.~recursive_mutex();
    else
      mutex_.~mutex();
  }

  //! Try to acquire the lock, return true if successful, false if failed.
  bool tryLock() {
    return recursive_ ? rec_mutex_.try_lock() : mutex_.try_lock();
  }

  //! Acquire the lock or suspend the calling thread.
  void lock() {
    recursive_ ? rec_mutex_.lock() : mutex_.lock();
  }

  //! Release the lock and wake a single waiting thread if any.
  void unlock() {
    recursive_ ? rec_mutex_.unlock() : mutex_.unlock();
  }

  /*! \brief Give up the lock and go to sleep.
   *
   *  Calling wait() causes the current thread to go to sleep until
   *  another thread calls notify()/notifyAll().
   *
   *  \note The monitor must be owned before calling wait().
   */
  void wait() {
    assert(recursive_ == false && "wait() doesn't support recursive mode");
    // the mutex must be locked by caller
    std::unique_lock lk(mutex_, std::adopt_lock);
    cv_.wait(lk);
    // the mutex is locked again
    lk.release();  // Release the ownership so that the caller should unlock the mutex
  }

  /*! \brief Wake up a single thread waiting on this monitor.
   *
   *  \note The monitor may or may not be owned before calling notify().
   */
  void notify() { cv_.notify_one(); }

  /*! \brief Wake up all threads that are waiting on this monitor.
   *
   *  \note The monitor may or may not be owned before calling notifyAll().
   */
  void notifyAll() { cv_.notify_all(); }

 private:
  union {
    std::mutex mutex_;
    std::recursive_mutex rec_mutex_;
  };
  std::condition_variable cv_; //!< The condition variable for sync on the mutex
  const bool recursive_; //!< True if this is a recursive mutex, false otherwise.
};
} // namespace mutex_monitor

// Monitor API wrapper to user
class Monitor {
public:
  explicit Monitor(bool recursive = false) {
    if (DEBUG_CLR_USE_STDMUTEX_IN_AMD_MONITOR) {
      monitor_ = new mutex_monitor::Monitor(recursive);
    }
    else {
      monitor_ = new legacy_monitor::Monitor(recursive);
    }
  }
  inline ~Monitor() { delete monitor_; };
  inline bool tryLock() { return monitor_->tryLock(); }
  inline void lock() { monitor_->lock(); }
  inline void unlock() { monitor_->unlock(); }
  inline void wait() { monitor_->wait(); }
  inline void notify() { monitor_->notify(); }
  inline void notifyAll() { monitor_->notifyAll(); }

private:
  MonitorBase* monitor_;
};

class ScopedLock : StackObject {
 public:
  ScopedLock(Monitor& lock) : lock_(&lock) { lock_->lock(); }

  ScopedLock(Monitor* lock) : lock_(lock) {
    if (lock_) lock_->lock();
  }

  ~ScopedLock() {
    if (lock_) lock_->unlock();
  }

 private:
  Monitor* lock_;
};

}  // namespace amd

#endif /*MONITOR_HPP_*/