implement lock and lock manager
Stefan Schuermans

Stefan Schuermans commited on 2019-08-13 18:30:31
Showing 5 changed files, with 187 additions and 0 deletions.

... ...
@@ -0,0 +1,50 @@
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
+
8
+namespace Blinker {
9
+
10
+/// constructor
11
+Lock::Lock():
12
+  m_locked(false)
13
+{
14
+}
15
+
16
+/// destructor
17
+Lock::~Lock()
18
+{
19
+}
20
+
21
+/**
22
+ * @brief check if locked
23
+ * @return whether lock is taken
24
+ */
25
+bool Lock::islocked() const
26
+{
27
+  return m_locked;
28
+}
29
+
30
+/**
31
+ * @brief lock if not locked
32
+ * @return whether lock could be acquired
33
+ */
34
+bool Lock::trylock()
35
+{
36
+  if (m_locked) {
37
+    return false;
38
+  }
39
+  m_locked = true;
40
+  return true;
41
+}
42
+
43
+/// unlock (if locked)
44
+void Lock::unlock()
45
+{
46
+  m_locked = false;
47
+}
48
+
49
+} // namespace Blinker
50
+
... ...
@@ -0,0 +1,51 @@
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_LOCK_H
7
+#define BLINKER_LOCK_H
8
+
9
+namespace Blinker {
10
+
11
+/// a mutual exclusion lock (easy, because Blinker is single-threaded)
12
+class Lock
13
+{
14
+public:
15
+  /// constructor
16
+  Lock();
17
+
18
+  /// destructor
19
+  ~Lock();
20
+
21
+private:
22
+  /// copy constructor disabled
23
+  Lock(const Lock &that);
24
+
25
+  /// assignment operator disabled
26
+  const Lock & operator=(const Lock &that);
27
+
28
+public:
29
+  /**
30
+   * @brief check if locked
31
+   * @return whether lock is taken
32
+   */
33
+  bool islocked() const;
34
+
35
+  /**
36
+   * @brief lock if not locked
37
+   * @return whether lock could be acquired
38
+   */
39
+  bool trylock();
40
+
41
+  /// unlock (if locked)
42
+  void unlock();
43
+
44
+private:
45
+  bool m_locked; ///< whether lock is locked
46
+}; // class Lock
47
+
48
+} // namespace Blinker
49
+
50
+#endif // #ifndef BLINKER_LOCK_H
51
+
... ...
@@ -0,0 +1,33 @@
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 <map>
7
+
8
+#include "Lock.h"
9
+#include "LockMgr.h"
10
+
11
+namespace Blinker {
12
+
13
+/// constructor
14
+LockMgr::LockMgr()
15
+{
16
+}
17
+
18
+/// destructor
19
+LockMgr::~LockMgr()
20
+{
21
+}
22
+
23
+/**
24
+ * @brief get a lock object
25
+ * @param[in] name name of lock
26
+ */
27
+Lock & LockMgr::getLock(std::string const &name)
28
+{
29
+  return m_lockMap[name];
30
+}
31
+
32
+} // namespace Blinker
33
+
... ...
@@ -0,0 +1,51 @@
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_LOCKMGR_H
7
+#define BLINKER_LOCKMGR_H
8
+
9
+#include <map>
10
+
11
+#include "Lock.h"
12
+
13
+namespace Blinker {
14
+
15
+/// lock manager, provides mutual exclusion among modules
16
+class LockMgr
17
+{
18
+protected:
19
+  /// map of available locks: name -> lock object
20
+  typedef std::map<std::string, Lock> LockMap;
21
+
22
+public:
23
+  /// constructor
24
+  LockMgr();
25
+
26
+  /// destructor
27
+  ~LockMgr();
28
+
29
+private:
30
+  /// copy constructor disabled
31
+  LockMgr(const LockMgr &that);
32
+
33
+  /// assignment operator disabled
34
+  const LockMgr & operator=(const LockMgr &that);
35
+
36
+public:
37
+  /**
38
+   * @brief get a lock object
39
+   * @param[in] name name of lock
40
+   */
41
+  Lock & getLock(std::string const &name);
42
+
43
+protected:
44
+  /// map of available locks: name -> lock object
45
+  LockMap m_lockMap;
46
+}; // class LockMgr
47
+
48
+} // namespace Blinker
49
+
50
+#endif // #ifndef BLINKER_LOCKMGR_H
51
+
... ...
@@ -7,6 +7,7 @@
7 7
 #define BLINKER_MGRS_H
8 8
 
9 9
 #include "CallMgr.h"
10
+#include "LockMgr.h"
10 11
 #include "OpMgr.h"
11 12
 #include "StreamMgr.h"
12 13
 #include "SyncMgr.h"
... ...
@@ -17,6 +18,7 @@ namespace Blinker {
17 18
 struct Mgrs
18 19
 {
19 20
   CallMgr   m_callMgr;   ///< call manager
21
+  LockMgr   m_lockMgr;   ///< lock manager
20 22
   OpMgr     m_opMgr;     ///< operator connection manager
21 23
   StreamMgr m_streamMgr; ///< stream manager
22 24
   SyncMgr   m_syncMgr;   ///< synchronization stream manager
23 25