Skip to content
This repository was archived by the owner on Mar 22, 2023. It is now read-only.

Commit a003549

Browse files
Merge pull request #719 from igchor/fix_concurrent_hash_map_free_data
concurrent_hash_map: fix free_data()
2 parents 9ed63df + 4d6222d commit a003549

File tree

5 files changed

+44
-4
lines changed

5 files changed

+44
-4
lines changed

examples/doc_snippets/make_persistent.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2018, Intel Corporation
2+
* Copyright 2016-2020, Intel Corporation
33
*
44
* Redistribution and use in source and binary forms, with or without
55
* modification, are permitted provided that the following conditions
@@ -81,6 +81,10 @@ make_persistent_example()
8181

8282
// transactionally delete the object, ~compound_type() is called
8383
delete_persistent<compound_type>(proot->comp);
84+
85+
// set pointer to null so that after restart it's known whether
86+
// compound_type is still allocated or not
87+
proot->comp = nullptr;
8488
});
8589

8690
// throws an transaction_scope_error exception
@@ -139,6 +143,10 @@ make_persistent_array_example()
139143
// transactionally delete arrays , ~compound_type() is called
140144
delete_persistent<compound_type[]>(proot->comp, 20);
141145
delete_persistent<compound_type[3]>(arr1);
146+
147+
// set pointer to null so that after restart it's known whether
148+
// compound_type is still allocated or not
149+
proot->comp = nullptr;
142150
});
143151

144152
// throws an transaction_scope_error exception

include/libpmemobj++/container/concurrent_hash_map.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2316,6 +2316,9 @@ class concurrent_hash_map
23162316
void
23172317
free_data()
23182318
{
2319+
if (!this->tls_ptr)
2320+
return;
2321+
23192322
auto pop = get_pool_base();
23202323

23212324
transaction::run(pop, [&] {

include/libpmemobj++/make_persistent.hpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2019, Intel Corporation
2+
* Copyright 2016-2020, Intel Corporation
33
*
44
* Redistribution and use in source and binary forms, with or without
55
* modification, are permitted provided that the following conditions
@@ -133,6 +133,9 @@ make_persistent(Args &&... args)
133133
* object's destructor before freeing memory. Cannot be used for array
134134
* types.
135135
*
136+
* To ensure that proper recovery is possible, ptr should be set to null after
137+
* delete_persistent call and within the same transaction.
138+
*
136139
* @param[in,out] ptr persistent pointer to an object that is not an
137140
* array.
138141
*

include/libpmemobj++/make_persistent_array.hpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2019, Intel Corporation
2+
* Copyright 2016-2020, Intel Corporation
33
*
44
* Redistribution and use in source and binary forms, with or without
55
* modification, are permitted provided that the following conditions
@@ -194,6 +194,9 @@ make_persistent(allocation_flag flag = allocation_flag::none())
194194
* objects. Calls the objects' destructors before freeing memory.
195195
* This overload only participates in overload resolution if T is an array.
196196
*
197+
* To ensure that proper recovery is possible, ptr should be set to null after
198+
* delete_persistent call and within the same transaction.
199+
*
197200
* @param[in,out] ptr persistent pointer to an array of objects.
198201
* @param[in] N the size of the array.
199202
*
@@ -238,6 +241,9 @@ delete_persistent(typename detail::pp_if_array<T>::type ptr, std::size_t N)
238241
* objects. Calls the objects' destructors before freeing memory.
239242
* This overload only participates in overload resolution if T is an array.
240243
*
244+
* To ensure that proper recovery is possible, ptr should be set to null after
245+
* delete_persistent call and within the same transaction.
246+
*
241247
* @param[in,out] ptr persistent pointer to an array of objects.
242248
*
243249
* @throw transaction_scope_error if called outside of an active

tests/concurrent_hash_map_tx/concurrent_hash_map_tx.cpp

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,8 +358,28 @@ test_tx_singlethread(nvobj::pool<root> &pop)
358358

359359
UT_ASSERT(static_cast<int>(map->size()) == number_of_inserts);
360360

361+
try {
362+
pmem::obj::transaction::run(pop, [&] {
363+
map->free_data();
364+
pmem::obj::transaction::abort(0);
365+
});
366+
} catch (pmem::manual_tx_abort &) {
367+
} catch (...) {
368+
UT_ASSERT(0);
369+
}
370+
371+
verify_elements(pop, number_of_inserts);
372+
373+
try {
374+
pmem::obj::transaction::run(pop, [&] {
375+
map->free_data();
376+
pmem::obj::delete_persistent<persistent_map_type>(map);
377+
});
378+
} catch (...) {
379+
UT_ASSERT(0);
380+
}
381+
361382
pmem::obj::transaction::run(pop, [&] {
362-
pmem::obj::delete_persistent<persistent_map_type>(map);
363383
pmem::obj::delete_persistent<persistent_map_type>(map2);
364384
});
365385
}

0 commit comments

Comments
 (0)