Honeycomb  0.1
Component-Model Framework
SharedMutex.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 
4 #include "Honey/Thread/Thread.h"
6 
7 namespace honey
8 {
9 
10 template<class Lockable> class SharedLock;
11 
13 
19 {
20 public:
23 
24  SharedMutex() { init(); }
26  SharedMutex(const SharedMutex&) { init(); }
27 
29 
31  SharedMutex& operator=(const SharedMutex&) { return *this; }
32 
34  void lock();
36  void unlock();
37 
39  bool tryLock();
41  bool tryLock(MonoClock::Duration time) { return tryLock(time == time.max() ? MonoClock::TimePoint::max() : MonoClock::now() + time); }
43  bool tryLock(MonoClock::TimePoint time);
44 
46  void lockShared();
48  void unlockShared();
49 
51  bool tryLockShared();
53  bool tryLockShared(MonoClock::Duration time) { return tryLockShared(time == time.max() ? MonoClock::TimePoint::max() : MonoClock::now() + time); }
56 
58  void unlockAndLockShared();
59 
60 private:
61  void init();
62 
64  struct State { enum t {
65  unlock = 0,
66  unique = 1,
67  shared = 2
68  }; };
69 
70  bool tryLock_priv(Thread::ThreadId threadId);
71  bool tryLockShared_priv();
72 
73  bool isShared() const { return (_state & ~State::unique) != 0; }
74 
75  Atomic<int> _state;
76  ConditionLock _cond;
77  Atomic<Thread::ThreadId> _owner;
78  int _holdCount;
79  Atomic<int> _waitCount;
80 };
81 
82 
83 }
A scoped lock that references any lockable. Locks on construction and unlocks on destruction.
Definition: Mutex.h:10
bool tryLockShared(MonoClock::Duration time)
Attempt to acquire the shared reader lock for an amount of time. Returns true if the lock was acquire...
Definition: SharedMutex.h:53
static TimePoint now()
Get current time.
Definition: Clock.h:48
UniqueLock< SharedMutex > Scoped
Definition: SharedMutex.h:21
Super::TimePoint TimePoint
Definition: Clock.h:40
SharedMutex()
Definition: SharedMutex.h:24
bool tryLock()
Attempt to acquire the unique writer lock, returns immediately. Returns true if the lock was acquired...
Definition: SharedMutex.cpp:48
void unlock()
Release the unique writer lock.
Definition: SharedMutex.cpp:28
void lock()
Acquire the unique writer lock. Thread suspends until lock is available and all readers release the s...
Definition: SharedMutex.cpp:17
Super::ThreadId ThreadId
Definition: Thread.h:140
SharedLock< SharedMutex > SharedScoped
Definition: SharedMutex.h:22
bool tryLock(MonoClock::Duration time)
Attempt to acquire the unique writer lock for an amount of time. Returns true if the lock was acquire...
Definition: SharedMutex.h:41
void unlockAndLockShared()
Atomically unlock unique writer lock and acquire shared reader lock without blocking.
Definition: SharedMutex.cpp:115
~SharedMutex()
Definition: SharedMutex.h:28
bool tryLockShared()
Attempt to acquire the shared reader lock, returns immediately. Returns true if the lock was acquired...
Definition: SharedMutex.cpp:98
SharedMutex(const SharedMutex &)
Can't copy, silently inits to default.
Definition: SharedMutex.h:26
SharedMutex & operator=(const SharedMutex &)
Can't copy, silently does nothing.
Definition: SharedMutex.h:31
TimePoint::Duration Duration
Definition: Clock.h:41
A scoped lock that references a shared mutex. Does a shared read lock on construction and unlocks on ...
Definition: Shared.h:15
void lockShared()
Acquire the shared reader lock. Thread suspends until writer lock has been released.
Definition: SharedMutex.cpp:66
Global Honeycomb namespace.
A thread lock for shared data where there may be many readers and one writer.
Definition: SharedMutex.h:18
void unlockShared()
Release the shared reader lock.
Definition: SharedMutex.cpp:76