Kea  2.3.5-git
interprocess_sync.h
Go to the documentation of this file.
1 // Copyright (C) 2012-2015 Internet Systems Consortium, Inc. ("ISC")
2 //
3 // This Source Code Form is subject to the terms of the Mozilla Public
4 // License, v. 2.0. If a copy of the MPL was not distributed with this
5 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
6 
7 #ifndef INTERPROCESS_SYNC_H
8 #define INTERPROCESS_SYNC_H
9 
10 #include <string>
11 
12 namespace isc {
13 namespace log {
14 namespace interprocess {
15 
16 class InterprocessSyncLocker; // forward declaration
17 
39  // InterprocessSyncLocker is the only code outside this class that
40  // should be allowed to call the lock(), tryLock() and unlock()
41  // methods.
42  friend class InterprocessSyncLocker;
43 
44 public:
52  InterprocessSync(const std::string& task_name) :
53  task_name_(task_name), is_locked_(false)
54  {}
55 
57  virtual ~InterprocessSync() {}
58 
59 protected:
64  virtual bool lock() = 0;
65 
69  virtual bool tryLock() = 0;
70 
74  virtual bool unlock() = 0;
75 
76  const std::string task_name_;
77  bool is_locked_;
78 };
79 
87 public:
95  sync_(sync)
96  {}
97 
100  if (isLocked())
101  unlock();
102  }
103 
108  bool lock() {
109  return (sync_.lock());
110  }
111 
116  bool tryLock() {
117  return (sync_.tryLock());
118  }
119 
124  bool isLocked() const {
125  return (sync_.is_locked_);
126  }
127 
131  bool unlock() {
132  return (sync_.unlock());
133  }
134 
135 protected:
137 };
138 
139 } // namespace interprocess
140 } // namespace log
141 } // namespace isc
142 
143 #endif // INTERPROCESS_SYNC_H
bool tryLock()
Try to acquire a lock (doesn&#39;t block)
virtual bool tryLock()=0
Try to acquire a lock (doesn&#39;t block)
virtual bool unlock()=0
Release the lock.
Defines the logger used by the top-level component of kea-lfc.
virtual bool lock()=0
Acquire the lock (blocks if something else has acquired a lock on the same task name) ...
InterprocessSyncLocker(InterprocessSync &sync)
Constructor.
const std::string task_name_
The task name.
bool isLocked() const
Check if the lock is taken.
InterprocessSync(const std::string &task_name)
Constructor.
InterprocessSync & sync_
Ref to underlying sync object.
bool lock()
Acquire the lock (blocks if something else has acquired a lock on the same task name) ...