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 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
|
// SPDX-License-Identifier: BSD-3-Clause
/* Copyright 2019-2020, Intel Corporation */
#include "unittest.hpp"
#include <libpmemobj++/detail/enumerable_thread_specific.hpp>
#include <libpmemobj++/make_persistent.hpp>
#include <set>
namespace nvobj = pmem::obj;
using test_t = nvobj::p<size_t>;
using container_type = pmem::detail::enumerable_thread_specific<test_t>;
struct root {
nvobj::persistent_ptr<container_type> pptr;
};
void
create_and_fill(nvobj::pool<struct root> &pop, size_t concurrency)
{
auto &tls = pop.root()->pptr;
UT_ASSERT(tls == nullptr);
nvobj::transaction::run(
pop, [&] { tls = nvobj::make_persistent<container_type>(); });
parallel_exec_with_sync(concurrency, [&](size_t thread_index) {
tls->local() = thread_index;
pop.persist(tls->local());
});
UT_ASSERT(tls->size() <= concurrency);
}
void
check_and_delete(nvobj::pool<struct root> &pop, size_t concurrency)
{
auto &tls = pop.root()->pptr;
std::set<size_t> checker;
tls->initialize([&checker](test_t &e) {
UT_ASSERT(checker.emplace(e).second);
});
UT_ASSERT(checker.size() <= concurrency);
UT_ASSERT(tls->empty());
nvobj::transaction::run(pop, [&] {
nvobj::delete_persistent<container_type>(tls);
tls = nullptr;
});
}
void
check_with_tx_and_delete(nvobj::pool<struct root> &pop, size_t concurrency)
{
auto &tls = pop.root()->pptr;
std::set<size_t> checker;
nvobj::transaction::run(pop, [&] {
tls->initialize([&checker](test_t &e) {
UT_ASSERT(checker.emplace(e).second);
});
});
UT_ASSERT(checker.size() <= concurrency);
UT_ASSERT(tls->empty());
nvobj::transaction::run(pop, [&] {
nvobj::delete_persistent<container_type>(tls);
tls = nullptr;
});
}
void
check_with_tx_abort_and_delete(nvobj::pool<struct root> &pop,
size_t concurrency)
{
auto &tls = pop.root()->pptr;
std::set<size_t> checker;
try {
nvobj::transaction::run(pop, [&] {
tls->initialize([&checker](test_t &e) {
UT_ASSERT(checker.emplace(e).second);
});
nvobj::transaction::abort(0);
});
} catch (pmem::manual_tx_abort &) {
} catch (...) {
UT_ASSERT(0);
}
UT_ASSERT(checker.size() <= concurrency);
UT_ASSERT(!tls->empty());
UT_ASSERT(tls->size() <= concurrency);
for (auto &e : *tls)
e = 0;
parallel_exec_with_sync(concurrency, [&](size_t thread_index) {
tls->local()++;
pop.persist(&tls->local(), sizeof(tls->local()));
});
for (auto &e : *tls) {
UT_ASSERTeq(e, 1);
}
nvobj::transaction::run(pop, [&] {
nvobj::delete_persistent<container_type>(tls);
tls = nullptr;
});
}
static void
test(int argc, char *argv[])
{
if (argc < 2) {
UT_FATAL("usage: %s file-name", argv[0]);
}
auto path = argv[1];
auto layout = "TLSTest: enumerable_thread_specific_initialize";
auto pop = nvobj::pool<root>::create(path, layout, PMEMOBJ_MIN_POOL,
S_IWUSR | S_IRUSR);
// Adding more concurrency will increase DRD test time
size_t concurrency = 16;
{
create_and_fill(pop, concurrency);
pop.close();
pop = nvobj::pool<root>::open(path, layout);
check_and_delete(pop, concurrency);
}
{
create_and_fill(pop, concurrency);
pop.close();
pop = nvobj::pool<root>::open(path, layout);
check_with_tx_and_delete(pop, concurrency);
}
{
create_and_fill(pop, concurrency);
pop.close();
pop = nvobj::pool<root>::open(path, layout);
check_with_tx_abort_and_delete(pop, concurrency);
}
pop.close();
}
int
main(int argc, char *argv[])
{
return run_test([&] { test(argc, argv); });
}
|