Stefan Schuermans commited on 2019-08-13 19:35:46
Showing 2 changed files, with 173 additions and 0 deletions.
| ... | ... |
@@ -0,0 +1,100 @@ |
| 1 |
+/* Blinker |
|
| 2 |
+ Copyright 2011-2019 Stefan Schuermans <stefan@blinkenarea.org> |
|
| 3 |
+ Copyleft GNU public license - http://www.gnu.org/copyleft/gpl.html |
|
| 4 |
+ a blinkenarea.org project */ |
|
| 5 |
+ |
|
| 6 |
+#include "Lock.h" |
|
| 7 |
+#include "LockMgr.h" |
|
| 8 |
+#include "LockNameFile.h" |
|
| 9 |
+#include "NameFile.h" |
|
| 10 |
+ |
|
| 11 |
+namespace Blinker {
|
|
| 12 |
+ |
|
| 13 |
+/** |
|
| 14 |
+ * @brief constructor |
|
| 15 |
+ * @param[in] file basic file object to treat as lock name file |
|
| 16 |
+ * @param[in] lockMgr lock manager |
|
| 17 |
+ */ |
|
| 18 |
+LockNameFile::LockNameFile(const File &file, LockMgr &lockMgr): |
|
| 19 |
+ m_nameFile(file), |
|
| 20 |
+ m_lockMgr(lockMgr), |
|
| 21 |
+ m_refLock(nullptr), |
|
| 22 |
+ m_takenLock(nullptr) |
|
| 23 |
+{
|
|
| 24 |
+ update(); |
|
| 25 |
+} |
|
| 26 |
+ |
|
| 27 |
+/// destructor |
|
| 28 |
+LockNameFile::~LockNameFile() |
|
| 29 |
+{
|
|
| 30 |
+ release(); |
|
| 31 |
+} |
|
| 32 |
+ |
|
| 33 |
+/// update lock name if file has been modified |
|
| 34 |
+void LockNameFile::updateIfModified() |
|
| 35 |
+{
|
|
| 36 |
+ if (m_nameFile.checkModified()) {
|
|
| 37 |
+ update(); |
|
| 38 |
+ } |
|
| 39 |
+} |
|
| 40 |
+ |
|
| 41 |
+/** |
|
| 42 |
+ * @brief check if lock is free, |
|
| 43 |
+ * i.e., no lock configured (i.e. no lock file), |
|
| 44 |
+ * lock not locked or taken by this object |
|
| 45 |
+ * @return whether lock is free |
|
| 46 |
+ */ |
|
| 47 |
+bool LockNameFile::isfree() const |
|
| 48 |
+{
|
|
| 49 |
+ return m_takenLock != nullptr || |
|
| 50 |
+ m_refLock == nullptr || |
|
| 51 |
+ ! m_refLock->islocked(); |
|
| 52 |
+} |
|
| 53 |
+ |
|
| 54 |
+/** |
|
| 55 |
+ * @brief take the lock, |
|
| 56 |
+ * success if no lock configured (i.e. no lock file), |
|
| 57 |
+ * lock was not locked and could be taken by this object |
|
| 58 |
+ * or lock was already taken by this object |
|
| 59 |
+ * @return whether lock could be taken |
|
| 60 |
+ */ |
|
| 61 |
+bool LockNameFile::take() |
|
| 62 |
+{
|
|
| 63 |
+ if (m_takenLock != nullptr) {
|
|
| 64 |
+ return true; // already taken |
|
| 65 |
+ } |
|
| 66 |
+ if (m_refLock == nullptr) {
|
|
| 67 |
+ return true; // no lock configured |
|
| 68 |
+ } |
|
| 69 |
+ if (! m_refLock->trylock()) {
|
|
| 70 |
+ return false; // did not get lock |
|
| 71 |
+ } |
|
| 72 |
+ // remember pointer to taken lock |
|
| 73 |
+ // (important if lock name changes while taken) |
|
| 74 |
+ m_takenLock = m_refLock; |
|
| 75 |
+ return true; // lock taken |
|
| 76 |
+} |
|
| 77 |
+ |
|
| 78 |
+/// release taken lock |
|
| 79 |
+void LockNameFile::release() |
|
| 80 |
+{
|
|
| 81 |
+ if (m_takenLock != nullptr) {
|
|
| 82 |
+ m_takenLock->unlock(); |
|
| 83 |
+ m_takenLock = nullptr; |
|
| 84 |
+ } |
|
| 85 |
+} |
|
| 86 |
+ |
|
| 87 |
+/// update pointer to referenced lock based on name file contents |
|
| 88 |
+void LockNameFile::update() |
|
| 89 |
+{
|
|
| 90 |
+ m_nameFile.update(); |
|
| 91 |
+ m_refLock = nullptr; // forget old referenced lock |
|
| 92 |
+ if (! m_nameFile.m_valid) {
|
|
| 93 |
+ return; // no lock referenced |
|
| 94 |
+ } |
|
| 95 |
+ // get new referenced lock |
|
| 96 |
+ m_refLock = &m_lockMgr.getLock(m_nameFile.m_obj.m_str); |
|
| 97 |
+} |
|
| 98 |
+ |
|
| 99 |
+} // namespace Blinker |
|
| 100 |
+ |
| ... | ... |
@@ -0,0 +1,73 @@ |
| 1 |
+/* Blinker |
|
| 2 |
+ Copyright 2011-2019 Stefan Schuermans <stefan@blinkenarea.org> |
|
| 3 |
+ Copyleft GNU public license - http://www.gnu.org/copyleft/gpl.html |
|
| 4 |
+ a blinkenarea.org project */ |
|
| 5 |
+ |
|
| 6 |
+#ifndef BLINKER_LOCKNAMEFILE_H |
|
| 7 |
+#define BLINKER_LOCKNAMEFILE_H |
|
| 8 |
+ |
|
| 9 |
+#include "LockMgr.h" |
|
| 10 |
+#include "NameFile.h" |
|
| 11 |
+ |
|
| 12 |
+namespace Blinker {
|
|
| 13 |
+ |
|
| 14 |
+/// file containting the name of a lock |
|
| 15 |
+class LockNameFile |
|
| 16 |
+{
|
|
| 17 |
+public: |
|
| 18 |
+ /** |
|
| 19 |
+ * @brief constructor |
|
| 20 |
+ * @param[in] file basic file object to treat as lock name file |
|
| 21 |
+ * @param[in] lockMgr lock manager |
|
| 22 |
+ */ |
|
| 23 |
+ LockNameFile(const File &file, LockMgr &lockMgr); |
|
| 24 |
+ |
|
| 25 |
+ /// destructor |
|
| 26 |
+ ~LockNameFile(); |
|
| 27 |
+ |
|
| 28 |
+private: |
|
| 29 |
+ /// copy constructor disabled |
|
| 30 |
+ LockNameFile(const LockNameFile &); |
|
| 31 |
+ |
|
| 32 |
+ /// assignment operator disabled |
|
| 33 |
+ const LockNameFile & operator=(const LockNameFile &); |
|
| 34 |
+ |
|
| 35 |
+public: |
|
| 36 |
+ /// update lock name if file has been modified |
|
| 37 |
+ void updateIfModified(); |
|
| 38 |
+ |
|
| 39 |
+ /** |
|
| 40 |
+ * @brief check if lock is free, |
|
| 41 |
+ * i.e., no lock configured (i.e. no lock file), |
|
| 42 |
+ * lock not locked or taken by this object |
|
| 43 |
+ * @return whether lock is free |
|
| 44 |
+ */ |
|
| 45 |
+ bool isfree() const; |
|
| 46 |
+ |
|
| 47 |
+ /** |
|
| 48 |
+ * @brief take the lock, |
|
| 49 |
+ * success if no lock configured (i.e. no lock file), |
|
| 50 |
+ * lock was not locked and could be taken by this object |
|
| 51 |
+ * or lock was already taken by this object |
|
| 52 |
+ * @return whether lock could be taken |
|
| 53 |
+ */ |
|
| 54 |
+ bool take(); |
|
| 55 |
+ |
|
| 56 |
+ /// release taken lock |
|
| 57 |
+ void release(); |
|
| 58 |
+ |
|
| 59 |
+protected: |
|
| 60 |
+ /// update pointer to referenced lock based on name file contents |
|
| 61 |
+ void update(); |
|
| 62 |
+ |
|
| 63 |
+public: |
|
| 64 |
+ NameFile m_nameFile; ///< file containing lock name |
|
| 65 |
+ LockMgr &m_lockMgr; ///< reference to lock manager |
|
| 66 |
+ class Lock *m_refLock; ///< pointer to re referenced lock (if any) or nullptr |
|
| 67 |
+ class Lock *m_takenLock; ///< pointer to taken lock (if any) or nullptr |
|
| 68 |
+}; // class LockNameFile |
|
| 69 |
+ |
|
| 70 |
+} // namespace Blinker |
|
| 71 |
+ |
|
| 72 |
+#endif // #ifndef BLINKER_LOCKNAMEFILE_H |
|
| 73 |
+ |
|
| 0 | 74 |