Honeycomb  0.1
Component-Model Framework
Shared.h
Go to the documentation of this file.
1 // Honeycomb, Copyright (C) 2015 NewGamePlus Inc. Distributed under the Boost Software License v1.0.
2 #pragma once
3 
5 
6 namespace honey
7 {
8 
10 
14 template<class Lockable_>
16 {
17 public:
18  typedef Lockable_ Lockable;
19 
20  SharedLock() : _lock(nullptr), _owns(false) {}
21 
22  SharedLock(Lockable& lock, lock::Op op = lock::Op::lock) : _lock(&lock), _owns(false)
23  {
24  switch (op)
25  {
26  case lock::Op::lock:
27  this->lock();
28  break;
29  case lock::Op::tryLock:
30  tryLock();
31  break;
32  case lock::Op::adopt:
33  _owns = true;
34  break;
35  case lock::Op::defer:
36  break;
37  }
38  }
39 
40  template<class Rep, class Period>
41  SharedLock(Lockable& lock, Duration<Rep,Period> time) : _lock(&lock), _owns(false) { tryLock(time); }
42  template<class Clock, class Dur>
43  SharedLock(Lockable& lock, TimePoint<Clock,Dur> time) : _lock(&lock), _owns(false) { tryLock(time); }
44 
45  SharedLock(SharedLock&& rhs) noexcept { operator=(move(rhs)); }
47  SharedLock(UniqueLock<Lockable>&& rhs) { operator=(move(rhs)); }
48 
49  ~SharedLock() { if (_owns) unlock(); }
50 
52  {
53  _lock = rhs._lock; _owns = rhs._owns;
54  rhs._lock = nullptr; rhs._owns = false;
55  return *this;
56  }
57 
59  {
60  _lock = &rhs.mutex(); _owns = true;
61  _lock->unlockAndLockShared();
62  rhs.release();
63  return *this;
64  }
65 
66  void lock()
67  {
68  assert(_lock, "Lock has been released");
69  assert(!_owns, "Lock already held");
70  _lock->lockShared();
71  _owns = true;
72  }
73 
74  void unlock()
75  {
76  assert(_lock, "Lock has been released");
77  assert(_owns, "Lock not held");
78  _lock->unlockShared();
79  _owns = false;
80  }
81 
82  bool tryLock()
83  {
84  assert(_lock, "Lock has been released");
85  assert(!_owns, "Lock already held");
86  return _owns = _lock->tryLockShared();
87  }
88 
89  template<class Rep, class Period>
91  {
92  assert(_lock, "Lock has been released");
93  assert(!_owns, "Lock already held");
94  return _owns = _lock->tryLockShared(time);
95  }
96 
97  template<class Clock, class Dur>
99  {
100  assert(_lock, "Lock has been released");
101  assert(!_owns, "Lock already held");
102  return _owns = _lock->tryLockShared(time);
103  }
104 
105  bool owns() const { return _owns; }
106  explicit operator bool() const { return owns(); }
107 
108  Lockable& mutex()
109  {
110  assert(_lock, "Lock has been released");
111  return *_lock;
112  }
113 
114  Lockable& release()
115  {
116  Lockable& ret = mutex();
117  _lock = nullptr;
118  _owns = false;
119  return ret;
120  }
121 
122 private:
123  Lockable* _lock;
124  bool _owns;
125 };
126 
127 
128 }
A scoped lock that references any lockable. Locks on construction and unlocks on destruction.
Definition: Mutex.h:10
TimePoint represented by a duration since a clock's epoch time.
Definition: TimePoint.h:11
Lock (blocking)
bool tryLock(TimePoint< Clock, Dur > time)
Definition: Shared.h:98
bool tryLock(Duration< Rep, Period > time)
Definition: Shared.h:90
bool owns() const
Definition: Shared.h:105
Try to lock (non-blocking)
Lockable & mutex()
Definition: Shared.h:108
SharedLock(UniqueLock< Lockable > &&rhs)
Atomically unlock unique writer lock and acquire shared reader lock without blocking. The unique lock is released.
Definition: Shared.h:47
void unlock()
Definition: Shared.h:74
SharedLock & operator=(UniqueLock< Lockable > &&rhs)
Definition: Shared.h:58
Inherit to declare that class is not copyable.
Definition: Meta.h:286
SharedLock(Lockable &lock, Duration< Rep, Period > time)
Definition: Shared.h:41
SharedLock(Lockable &lock, TimePoint< Clock, Dur > time)
Definition: Shared.h:43
SharedLock(Lockable &lock, lock::Op op=lock::Op::lock)
Definition: Shared.h:22
SharedLock(SharedLock &&rhs) noexcept
Definition: Shared.h:45
void lock()
Definition: Shared.h:66
#define assert(...)
Forwards to assert_#args. See assert_1(), assert_2().
Definition: Debug.h:24
Lockable & release()
Definition: Shared.h:114
Not yet locked, will lock manually.
bool tryLock()
Definition: Shared.h:82
~SharedLock()
Definition: Shared.h:49
Duration represented by repetitions of a period. The period must be a ratio.
Definition: Duration.h:7
SharedLock & operator=(SharedLock &&rhs)
Definition: Shared.h:51
Op
Definition: Unique.h:16
SharedLock()
Definition: Shared.h:20
A scoped lock that references a shared mutex. Does a shared read lock on construction and unlocks on ...
Definition: Shared.h:15
Global Honeycomb namespace.
Lockable_ Lockable
Definition: Shared.h:18
Already locked.