Kea  1.5.0
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
isc::log::interprocess::InterprocessSync
Interprocess Sync Class.
Definition: interprocess_sync.h:38
isc::log::interprocess::InterprocessSync::tryLock
virtual bool tryLock()=0
Try to acquire a lock (doesn't block)
isc::log::interprocess::InterprocessSyncLocker::tryLock
bool tryLock()
Try to acquire a lock (doesn't block)
Definition: interprocess_sync.h:116
isc::log::interprocess::InterprocessSync::unlock
virtual bool unlock()=0
Release the lock.
isc::log::interprocess::InterprocessSync::InterprocessSync
InterprocessSync(const std::string &task_name)
Constructor.
Definition: interprocess_sync.h:52
isc::log::interprocess::InterprocessSync::is_locked_
bool is_locked_
Is the lock taken?
Definition: interprocess_sync.h:77
isc::log::interprocess::InterprocessSyncLocker::~InterprocessSyncLocker
~InterprocessSyncLocker()
Destructor.
Definition: interprocess_sync.h:99
isc::log::interprocess::InterprocessSync::lock
virtual bool lock()=0
Acquire the lock (blocks if something else has acquired a lock on the same task name)
isc
Defines the logger used by the top-level component of kea-dhcp-ddns.
Definition: agent_parser.cc:144
isc::log::interprocess::InterprocessSync::task_name_
const std::string task_name_
The task name.
Definition: interprocess_sync.h:76
isc::log::interprocess::InterprocessSyncLocker
Interprocess Sync Locker Class.
Definition: interprocess_sync.h:86
isc::log::interprocess::InterprocessSyncLocker::lock
bool lock()
Acquire the lock (blocks if something else has acquired a lock on the same task name)
Definition: interprocess_sync.h:108
isc::log::interprocess::InterprocessSyncLocker::isLocked
bool isLocked() const
Check if the lock is taken.
Definition: interprocess_sync.h:124
isc::log::interprocess::InterprocessSyncLocker::sync_
InterprocessSync & sync_
Ref to underlying sync object.
Definition: interprocess_sync.h:136
isc::log::interprocess::InterprocessSyncLocker::InterprocessSyncLocker
InterprocessSyncLocker(InterprocessSync &sync)
Constructor.
Definition: interprocess_sync.h:94
isc::log::interprocess::InterprocessSync::~InterprocessSync
virtual ~InterprocessSync()
Destructor.
Definition: interprocess_sync.h:57
isc::log::interprocess::InterprocessSyncLocker::unlock
bool unlock()
Release the lock.
Definition: interprocess_sync.h:131