11 #include <boost/any.hpp>
12 #include <boost/make_shared.hpp>
13 #include <boost/shared_ptr.hpp>
19 #include <unordered_map>
83 void park(T parked_object, std::function<
void()> unpark_callback) {
84 std::lock_guard<std::mutex> lock(mutex_);
85 auto it = find(parked_object);
86 if (it != parking_.end()) {
92 parking_[makeKey(parked_object)] = pinfo;
106 std::lock_guard<std::mutex> lock(mutex_);
107 auto it = find(parked_object);
108 if (it == parking_.end()) {
110 " that has not been parked.");
114 return (++it->second.refcount_);
127 std::lock_guard<std::mutex> lock(mutex_);
128 auto it = find(parked_object);
129 if (it == parking_.end()) {
131 " that has not been parked.");
135 return (--it->second.refcount_);
152 bool unpark(T parked_object,
bool force =
false) {
154 std::function<void()> cb;
156 std::lock_guard<std::mutex> lock(mutex_);
157 auto it = find(parked_object);
158 if (it == parking_.end()) {
164 it->second.refcount_ = 0;
166 --it->second.refcount_;
169 if (it->second.refcount_ <= 0) {
171 cb = it->second.unpark_callback_;
196 std::lock_guard<std::mutex> lock(mutex_);
197 auto it = find(parked_object);
198 if (it != parking_.end()) {
231 std::function<
void()> callback = 0)
232 : parked_object_(parked_object), unpark_callback_(callback),
239 void update(
const boost::any& parked_object,
240 std::function<
void()> callback) {
241 parked_object_ = parked_object;
242 unpark_callback_ = callback;
249 typedef std::unordered_map<std::string, ParkingInfo> ParkingInfoList;
252 typedef ParkingInfoList::iterator ParkingInfoListIterator;
255 ParkingInfoList parking_;
263 std::string makeKey(T parked_object) {
264 std::stringstream ss;
265 ss << boost::any_cast<T>(parked_object);
276 ParkingInfoListIterator find(T parked_object) {
277 return (parking_.find(makeKey(parked_object)));
308 : parking_lot_(parking_lot) {
322 return (parking_lot_->reference(parked_object));
335 return (parking_lot_->dereference(parked_object));
351 return (parking_lot_->unpark(parked_object));
364 return (parking_lot_->drop(parked_object));
370 ParkingLotPtr parking_lot_;
385 std::lock_guard<std::mutex> lock(mutex_);
386 parking_lots_.clear();
398 std::lock_guard<std::mutex> lock(mutex_);
399 if (parking_lots_.count(hook_index) == 0) {
400 parking_lots_[hook_index] = boost::make_shared<ParkingLot>();
402 return (parking_lots_[hook_index]);
408 std::unordered_map<int, ParkingLotPtr> parking_lots_;
bool drop(T parked_object)
Removes parked object without calling a callback.
Provides a limited view to the ParkingLot.
int reference(T parked_object)
Increases reference counter for the parked object.
boost::shared_ptr< ParkingLot > ParkingLotPtr
Type of the pointer to the parking lot.
Holds information about parked object.
void park(T parked_object, std::function< void()> unpark_callback)
Parks an object.
void clear()
Removes all parked objects.
void update(const boost::any &parked_object, std::function< void()> callback)
Update parking information.
int dereference(T parked_object)
Decreases the reference counter for the parked object.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
ParkingLotHandle(const ParkingLotPtr &parking_lot)
Constructor.
int reference(T parked_object)
Increases reference counter for the parked object.
Collection of parking lots for various hook points.
Parking lot for objects, e.g.
boost::shared_ptr< ParkingLots > ParkingLotsPtr
Type of the pointer to the parking lots.
Defines the logger used by the top-level component of kea-dhcp-ddns.
int dereference(T parked_object)
Decreases the reference counter for the parked object.
bool drop(T parked_object)
Removes parked object without calling a callback.
A generic exception that is thrown if a function is called in a prohibited way.
std::function< void()> unpark_callback_
The pointer to callback.
boost::shared_ptr< ParkingLotHandle > ParkingLotHandlePtr
Pointer to the parking lot handle.
ParkingInfo()
Constructor.
ParkingLotPtr getParkingLotPtr(const int hook_index)
Returns pointer to the parking lot for a hook points.
bool unpark(T parked_object, bool force=false)
Signals that the object should be unparked.
ParkingInfo(const boost::any &parked_object, std::function< void()> callback=0)
Constructor.
boost::any parked_object_
The parked object.
bool unpark(T parked_object)
Signals that the object should be unparked.
int refcount_
The current reference count.