Kea  2.1.7-git
resource_handler.cc
Go to the documentation of this file.
1 // Copyright (C) 2020 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 #include <config.h>
8 
10 #include <exceptions/exceptions.h>
11 
12 using namespace std;
13 using namespace isc::asiolink;
14 using namespace isc::util;
15 
16 namespace isc {
17 namespace dhcp {
18 
19 mutex ResourceHandler::mutex_;
20 
21 ResourceHandler::ResourceContainer ResourceHandler::resources_;
22 
23 ResourceHandler::ResourceHandler() : owned_() {
24 }
25 
27  lock_guard<mutex> lock_(mutex_);
28  for (auto res : owned_) {
29  unLockInternal(res->type_, res->addr_);
30  }
31  owned_.clear();
32 }
33 
34 ResourceHandler::ResourcePtr
35 ResourceHandler::lookup(Lease::Type type, const asiolink::IOAddress& addr) {
36  auto key = boost::make_tuple(type, addr.toBytes());
37  auto it = resources_.find(key);
38  if (it == resources_.end()) {
39  return (ResourcePtr());
40  }
41  return (*it);
42 }
43 
44 void
45 ResourceHandler::lock(Lease::Type type, const asiolink::IOAddress& addr) {
46  ResourcePtr res(new Resource(type, addr));
47  // Assume insert will never fail so not checking its result.
48  resources_.insert(res);
49  owned_.insert(res);
50 }
51 
52 void
53 ResourceHandler::unLockInternal(Lease::Type type,
54  const asiolink::IOAddress& addr) {
55  auto key = boost::make_tuple(type, addr.toBytes());
56  auto it = resources_.find(key);
57  if (it == resources_.end()) {
58  return;
59  }
60  resources_.erase(it);
61 }
62 
63 bool
65  ResourcePtr holder;
66  // Try to acquire the lock and return the holder when it failed.
67  lock_guard<mutex> lock_(mutex_);
68  holder = lookup(type, addr);
69  if (holder) {
70  return (false);
71  }
72  lock(type, addr);
73  return (true);
74 }
75 
76 bool
78  auto key = boost::make_tuple(type, addr.toBytes());
79  lock_guard<mutex> lock_(mutex_);
80  auto it = owned_.find(key);
81  return (it != owned_.end());
82 }
83 
84 void
86  auto key = boost::make_tuple(type, addr.toBytes());
87  lock_guard<mutex> lock_(mutex_);
88  auto it = owned_.find(key);
89  if (it == owned_.end()) {
90  isc_throw(NotFound, "does not own " << Lease::typeToText(type)
91  << " " << addr.toText());
92  }
93  unLockInternal(type, addr);
94  owned_.erase(it);
95 }
96 
97 } // namespace dhcp
98 } // namespace isc
static std::string typeToText(Type type)
returns text representation of a lease type
Definition: lease.cc:52
virtual ~ResourceHandler()
Destructor.
STL namespace.
bool tryLock(Lease::Type type, const asiolink::IOAddress &addr)
Tries to acquires a resource.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Definition: edns.h:19
void unLock(Lease::Type type, const asiolink::IOAddress &addr)
Releases a resource.
Defines the logger used by the top-level component of kea-lfc.
Type
Type of lease or pool.
Definition: lease.h:45
A generic exception that is thrown when an object can not be found.
bool isLocked(Lease::Type type, const asiolink::IOAddress &addr)
Checks if a resource is owned by this handler.