summaryrefslogtreecommitdiffstats
path: root/src/lib/dhcpsrv/resource_handler.cc
blob: 9ae2ee55b747875ff3de27c67b0e16d315d6e200 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Copyright (C) 2020 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

#include <config.h>

#include <dhcpsrv/resource_handler.h>
#include <exceptions/exceptions.h>

using namespace std;
using namespace isc::asiolink;
using namespace isc::util;

namespace isc {
namespace dhcp {

mutex ResourceHandler::mutex_;

ResourceHandler::ResourceContainer ResourceHandler::resources_;

ResourceHandler::ResourceHandler() : owned_() {
}

ResourceHandler::~ResourceHandler() {
    lock_guard<mutex> lock_(mutex_);
    for (auto res : owned_) {
        unLockInternal(res->type_, res->addr_);
    }
    owned_.clear();
}

ResourceHandler::ResourcePtr
ResourceHandler::lookup(Lease::Type type, const asiolink::IOAddress& addr) {
    auto key = boost::make_tuple(type, addr.toBytes());
    auto it = resources_.find(key);
    if (it == resources_.end()) {
        return (ResourcePtr());
    }
    return (*it);
}

void
ResourceHandler::lock(Lease::Type type, const asiolink::IOAddress& addr) {
    ResourcePtr res(new Resource(type, addr));
    // Assume insert will never fail so not checking its result.
    resources_.insert(res);
    owned_.insert(res);
}

void
ResourceHandler::unLockInternal(Lease::Type type,
                                const asiolink::IOAddress& addr) {
    auto key = boost::make_tuple(type, addr.toBytes());
    auto it = resources_.find(key);
    if (it == resources_.end()) {
        return;
    }
    resources_.erase(it);
}

bool
ResourceHandler::tryLock(Lease::Type type, const asiolink::IOAddress& addr) {
    ResourcePtr holder;
    // Try to acquire the lock and return the holder when it failed.
    lock_guard<mutex> lock_(mutex_);
    holder = lookup(type, addr);
    if (holder) {
        return (false);
    }
    lock(type, addr);
    return (true);
}

bool
ResourceHandler::isLocked(Lease::Type type, const asiolink::IOAddress& addr) {
    auto key = boost::make_tuple(type, addr.toBytes());
    lock_guard<mutex> lock_(mutex_);
    auto it = owned_.find(key);
    return (it != owned_.end());
}

void
ResourceHandler::unLock(Lease::Type type, const asiolink::IOAddress& addr) {
    auto key = boost::make_tuple(type, addr.toBytes());
    lock_guard<mutex> lock_(mutex_);
    auto it = owned_.find(key);
    if (it == owned_.end()) {
        isc_throw(NotFound, "does not own " << Lease::typeToText(type)
                  << " " << addr.toText());
    }
    unLockInternal(type, addr);
    owned_.erase(it);
}

}  // namespace dhcp
}  // namespace isc