Browse Source

update: add memory barrier of AllCases

master
Dnomd343 4 months ago
parent
commit
f8c0726efc
  1. 6
      src/core/all_cases/all_cases.h
  2. 4
      src/core/all_cases/internal/all_cases.cc
  3. 13
      src/core/all_cases/internal/basic_ranges.cc

6
src/core/all_cases/all_cases.h

@ -45,7 +45,7 @@
namespace klotski::cases {
// ------------------------------------------------------------------------------------- //
// ----------------------------------------------------------------------------------------- //
class BasicRanges {
public:
@ -71,7 +71,7 @@ private:
KLSK_INSTANCE(BasicRanges)
};
// ------------------------------------------------------------------------------------- //
// ----------------------------------------------------------------------------------------- //
class AllCases {
public:
@ -97,7 +97,7 @@ private:
KLSK_INSTANCE(AllCases)
};
// ------------------------------------------------------------------------------------- //
// ----------------------------------------------------------------------------------------- //
} // namespace klotski::cases

4
src/core/all_cases/internal/all_cases.cc

@ -54,7 +54,7 @@ void AllCases::build() {
if (available_) {
return; // reduce consumption of mutex
}
std::lock_guard guard {building_};
const std::lock_guard guard {building_};
if (available_) {
return; // data is already available
}
@ -68,6 +68,7 @@ void AllCases::build() {
build_cases(ranges, reversed, get_cases()[head], head);
}
available_ = true;
KLSK_MEM_BARRIER;
}
void AllCases::build_async(Executor &&executor, Notifier &&callback) {
@ -97,6 +98,7 @@ void AllCases::build_async(Executor &&executor, Notifier &&callback) {
worker.then([this, callback = std::move(callback)] {
available_ = true;
KLSK_MEM_BARRIER;
building_.unlock();
callback();
});

13
src/core/all_cases/internal/basic_ranges.cc

@ -8,9 +8,9 @@ using klotski::cases::Ranges;
using klotski::cases::BasicRanges;
using klotski::cases::TYPE_ID_LIMIT;
typedef Ranges::iterator RangesIter;
typedef std::tuple<int, int, int> RangeType;
typedef std::array<RangeType, TYPE_ID_LIMIT> RangeTypeUnion;
using RangesIter = Ranges::iterator ;
using RangeType = std::tuple<int, int, int> ;
using RangeTypeUnion = std::array<RangeType, TYPE_ID_LIMIT>;
/// Generate all possible basic-ranges permutations.
consteval static RangeTypeUnion range_types() {
@ -34,8 +34,9 @@ static void inplace_merge(RangesIter begin, RangesIter mid, const RangesIter end
for (auto p = tmp.begin();;) {
if (*p <= *mid) {
*(begin++) = *(p++); // stored in original span
if (p == tmp.end()) // left array is consumed
if (p == tmp.end()) { // left array is consumed
return;
}
continue;
}
*(begin++) = *(mid++); // stored in original span
@ -50,7 +51,7 @@ void BasicRanges::build() {
if (available_) {
return; // reduce consumption of mutex
}
std::lock_guard guard {building_};
const std::lock_guard guard {building_};
if (available_) {
return; // data is already available
}
@ -78,6 +79,7 @@ void BasicRanges::build() {
} while (points.size() > 2); // merge until only one interval remains
available_ = true;
KLSK_MEM_BARRIER;
}
void BasicRanges::build_async(Executor &&executor, Notifier &&callback) {
@ -94,6 +96,7 @@ void BasicRanges::build_async(Executor &&executor, Notifier &&callback) {
auto all_done = [this, callback = std::move(callback)] {
available_ = true;
KLSK_MEM_BARRIER;
building_.unlock();
callback();
};

Loading…
Cancel
Save