Skip to content

Commit 3e5a5c4

Browse files
committed
[#3298] Made MemHostDataSource MT safe
1 parent 4dae2a3 commit 3e5a5c4

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

Diff for: src/lib/dhcpsrv/testutils/memory_host_data_source.cc

+25
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77
#include <config.h>
88

99
#include <dhcpsrv/testutils/memory_host_data_source.h>
10+
#include <util/multi_threading_mgr.h>
1011
#include <boost/foreach.hpp>
1112

1213
using namespace isc::db;
14+
using namespace isc::util;
1315
using namespace std;
1416

1517
namespace isc {
@@ -22,6 +24,7 @@ MemHostDataSource::getAll(const Host::IdentifierType& identifier_type,
2224
const size_t identifier_len) const {
2325
vector<uint8_t> ident(identifier_begin, identifier_begin + identifier_len);
2426
ConstHostCollection hosts;
27+
MultiThreadingLock lock(mutex_);
2528
for (auto const& h : store_) {
2629
// If identifier type do not match, it's not for us
2730
if (h->getIdentifierType() != identifier_type) {
@@ -38,6 +41,7 @@ MemHostDataSource::getAll(const Host::IdentifierType& identifier_type,
3841
ConstHostCollection
3942
MemHostDataSource::getAll4(const SubnetID& subnet_id) const {
4043
ConstHostCollection hosts;
44+
MultiThreadingLock lock(mutex_);
4145
for (auto const& h : store_) {
4246
// Keep it when subnet_id matches.
4347
if (h->getIPv4SubnetID() == subnet_id) {
@@ -50,6 +54,7 @@ MemHostDataSource::getAll4(const SubnetID& subnet_id) const {
5054
ConstHostCollection
5155
MemHostDataSource::getAll6(const SubnetID& subnet_id) const {
5256
ConstHostCollection hosts;
57+
MultiThreadingLock lock(mutex_);
5358
for (auto const& h : store_) {
5459
// Keep it when subnet_id matches.
5560
if (h->getIPv6SubnetID() == subnet_id) {
@@ -62,6 +67,7 @@ MemHostDataSource::getAll6(const SubnetID& subnet_id) const {
6267
ConstHostCollection
6368
MemHostDataSource::getAllbyHostname(const std::string& hostname) const {
6469
ConstHostCollection hosts;
70+
MultiThreadingLock lock(mutex_);
6571
for (auto const& h : store_) {
6672
// Keep it when hostname matches.
6773
if (h->getLowerHostname() == hostname) {
@@ -75,6 +81,7 @@ ConstHostCollection
7581
MemHostDataSource::getAllbyHostname4(const std::string& hostname,
7682
const SubnetID& subnet_id) const {
7783
ConstHostCollection hosts;
84+
MultiThreadingLock lock(mutex_);
7885
for (auto const& h : store_) {
7986
// Keep it when hostname and subnet_id match.
8087
if ((h->getLowerHostname() == hostname) &&
@@ -89,6 +96,7 @@ ConstHostCollection
8996
MemHostDataSource::getAllbyHostname6(const std::string& hostname,
9097
const SubnetID& subnet_id) const {
9198
ConstHostCollection hosts;
99+
MultiThreadingLock lock(mutex_);
92100
for (auto const& h : store_) {
93101
// Keep it when hostname and subnet_id match.
94102
if ((h->getLowerHostname() == hostname) &&
@@ -105,6 +113,7 @@ MemHostDataSource::getPage4(const SubnetID& subnet_id,
105113
uint64_t lower_host_id,
106114
const HostPageSize& page_size) const {
107115
ConstHostCollection hosts;
116+
MultiThreadingLock lock(mutex_);
108117
for (auto const& h : store_) {
109118
// Skip it when subnet_id does not match.
110119
if (h->getIPv4SubnetID() != subnet_id) {
@@ -127,6 +136,7 @@ MemHostDataSource::getPage6(const SubnetID& subnet_id,
127136
uint64_t lower_host_id,
128137
const HostPageSize& page_size) const {
129138
ConstHostCollection hosts;
139+
MultiThreadingLock lock(mutex_);
130140
for (auto const& h : store_) {
131141
// Skip it when subnet_id does not match.
132142
if (h->getIPv6SubnetID() != subnet_id) {
@@ -148,6 +158,7 @@ MemHostDataSource::getPage4(size_t& /*source_index*/,
148158
uint64_t lower_host_id,
149159
const HostPageSize& page_size) const {
150160
ConstHostCollection hosts;
161+
MultiThreadingLock lock(mutex_);
151162
for (auto const& h : store_) {
152163
if (lower_host_id && (h->getHostId() <= lower_host_id)) {
153164
continue;
@@ -165,6 +176,7 @@ MemHostDataSource::getPage6(size_t& /*source_index*/,
165176
uint64_t lower_host_id,
166177
const HostPageSize& page_size) const {
167178
ConstHostCollection hosts;
179+
MultiThreadingLock lock(mutex_);
168180
for (auto const& h : store_) {
169181
if (lower_host_id && (h->getHostId() <= lower_host_id)) {
170182
continue;
@@ -180,6 +192,7 @@ MemHostDataSource::getPage6(size_t& /*source_index*/,
180192
ConstHostCollection
181193
MemHostDataSource::getAll4(const asiolink::IOAddress& address) const {
182194
ConstHostCollection hosts;
195+
MultiThreadingLock lock(mutex_);
183196
for (auto const& h : store_) {
184197
if (h->getIPv4Reservation() == address) {
185198
hosts.push_back(h);
@@ -195,6 +208,7 @@ MemHostDataSource::get4(const SubnetID& subnet_id,
195208
const uint8_t* identifier_begin,
196209
const size_t identifier_len) const {
197210
vector<uint8_t> ident(identifier_begin, identifier_begin + identifier_len);
211+
MultiThreadingLock lock(mutex_);
198212
for (auto const& h : store_) {
199213
// If either subnet-id or identifier type do not match,
200214
// it's not our host
@@ -218,6 +232,7 @@ MemHostDataSource::get6(const SubnetID& subnet_id,
218232
const uint8_t* identifier_begin,
219233
const size_t identifier_len) const {
220234
vector<uint8_t> ident(identifier_begin, identifier_begin + identifier_len);
235+
MultiThreadingLock lock(mutex_);
221236
for (auto const& h : store_) {
222237
// If either subnet-id or identifier type do not match,
223238
// it's not our host
@@ -237,6 +252,7 @@ MemHostDataSource::get6(const SubnetID& subnet_id,
237252
ConstHostPtr
238253
MemHostDataSource::get4(const SubnetID& subnet_id,
239254
const asiolink::IOAddress& address) const {
255+
MultiThreadingLock lock(mutex_);
240256
for (auto const& h : store_) {
241257
if (h->getIPv4SubnetID() == subnet_id &&
242258
h->getIPv4Reservation() == address) {
@@ -251,6 +267,7 @@ ConstHostCollection
251267
MemHostDataSource::getAll4(const SubnetID& subnet_id,
252268
const asiolink::IOAddress& address) const {
253269
ConstHostCollection hosts;
270+
MultiThreadingLock lock(mutex_);
254271
for (auto const& h : store_) {
255272
if (h->getIPv4SubnetID() == subnet_id &&
256273
h->getIPv4Reservation() == address) {
@@ -270,6 +287,7 @@ MemHostDataSource::get6(const asiolink::IOAddress& /*prefix*/,
270287
ConstHostPtr
271288
MemHostDataSource::get6(const SubnetID& subnet_id,
272289
const asiolink::IOAddress& address) const {
290+
MultiThreadingLock lock(mutex_);
273291
for (auto const& h : store_) {
274292

275293
// Naive approach: check hosts one by one
@@ -297,6 +315,7 @@ ConstHostCollection
297315
MemHostDataSource::getAll6(const SubnetID& subnet_id,
298316
const asiolink::IOAddress& address) const {
299317
ConstHostCollection hosts;
318+
MultiThreadingLock lock(mutex_);
300319
for (auto const& h : store_) {
301320
if (h->getIPv6SubnetID() != subnet_id) {
302321
continue;
@@ -316,6 +335,7 @@ MemHostDataSource::getAll6(const SubnetID& subnet_id,
316335
ConstHostCollection
317336
MemHostDataSource::getAll6(const asiolink::IOAddress& address) const {
318337
ConstHostCollection hosts;
338+
MultiThreadingLock lock(mutex_);
319339
for (auto const& h : store_) {
320340
auto const& resrvs = h->getIPv6Reservations();
321341
BOOST_FOREACH(auto const& r, resrvs) {
@@ -330,13 +350,15 @@ MemHostDataSource::getAll6(const asiolink::IOAddress& address) const {
330350

331351
void
332352
MemHostDataSource::add(const HostPtr& host) {
353+
MultiThreadingLock lock(mutex_);
333354
host->setHostId(++next_host_id_);
334355
store_.push_back(host);
335356
}
336357

337358
bool
338359
MemHostDataSource::del(const SubnetID& subnet_id,
339360
const asiolink::IOAddress& addr) {
361+
MultiThreadingLock lock(mutex_);
340362
for (auto h = store_.begin(); h != store_.end(); ++h) {
341363
if (addr.isV4()) {
342364
if ((*h)->getIPv4SubnetID() == subnet_id &&
@@ -371,6 +393,7 @@ MemHostDataSource::del4(const SubnetID& subnet_id,
371393
const uint8_t* identifier_begin,
372394
const size_t identifier_len) {
373395
vector<uint8_t> ident(identifier_begin, identifier_begin + identifier_len);
396+
MultiThreadingLock lock(mutex_);
374397
for (auto h = store_.begin(); h != store_.end(); ++h) {
375398
// If either subnet-id or identifier type do not match,
376399
// it's not our host
@@ -394,6 +417,7 @@ MemHostDataSource::del6(const SubnetID& subnet_id,
394417
const uint8_t* identifier_begin,
395418
const size_t identifier_len) {
396419
vector<uint8_t> ident(identifier_begin, identifier_begin + identifier_len);
420+
MultiThreadingLock lock(mutex_);
397421
for (auto h = store_.begin(); h != store_.end(); ++h) {
398422
// If either subnet-id or identifier type do not match,
399423
// it's not our host
@@ -412,6 +436,7 @@ MemHostDataSource::del6(const SubnetID& subnet_id,
412436

413437
size_t
414438
MemHostDataSource::size() const {
439+
MultiThreadingLock lock(mutex_);
415440
return (store_.size());
416441
}
417442

Diff for: src/lib/dhcpsrv/testutils/memory_host_data_source.h

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (C) 2018-2023 Internet Systems Consortium, Inc. ("ISC")
1+
// Copyright (C) 2018-2024 Internet Systems Consortium, Inc. ("ISC")
22
//
33
// This Source Code Form is subject to the terms of the Mozilla Public
44
// License, v. 2.0. If a copy of the MPL was not distributed with this
@@ -9,6 +9,7 @@
99

1010
#include <dhcpsrv/host_data_source_factory.h>
1111
#include <boost/shared_ptr.hpp>
12+
#include <mutex>
1213
#include <string>
1314
#include <vector>
1415

@@ -333,6 +334,9 @@ class MemHostDataSource : public virtual BaseHostDataSource {
333334

334335
/// @brief Next host id
335336
uint64_t next_host_id_;
337+
338+
/// @brief Mutex to protect the store.
339+
std::mutex mutable mutex_;
336340
};
337341

338342
/// Pointer to the Mem host data source.

0 commit comments

Comments
 (0)