Kea 2.5.8
resource_handler.cc
Go to the documentation of this file.
1// Copyright (C) 2020-2024 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
11
12using namespace std;
13using namespace isc::asiolink;
14using namespace isc::util;
15
16namespace isc {
17namespace dhcp {
18
19mutex ResourceHandler::mutex_;
20
21ResourceHandler::ResourceContainer ResourceHandler::resources_;
22
24}
25
27 lock_guard<mutex> lock_(mutex_);
28 for (auto const& res : owned_) {
29 unLockInternal(res->type_, res->addr_);
30 }
31 owned_.clear();
32}
33
34ResourceHandler::ResourcePtr
35ResourceHandler::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
44void
45ResourceHandler::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
52void
53ResourceHandler::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
63bool
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
76bool
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
84void
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
A generic exception that is thrown when an object can not be found.
virtual ~ResourceHandler()
Destructor.
bool tryLock(Lease::Type type, const asiolink::IOAddress &addr)
Tries to acquires a resource.
void unLock(Lease::Type type, const asiolink::IOAddress &addr)
Releases a resource.
bool isLocked(Lease::Type type, const asiolink::IOAddress &addr)
Checks if a resource is owned by this handler.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Defines the logger used by the top-level component of kea-lfc.
Type
Type of lease or pool.
Definition: lease.h:46
static std::string typeToText(Type type)
returns text representation of a lease type
Definition: lease.cc:54