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 { namespace klotski::cases {
// ------------------------------------------------------------------------------------- // // ----------------------------------------------------------------------------------------- //
class BasicRanges { class BasicRanges {
public: public:
@ -71,7 +71,7 @@ private:
KLSK_INSTANCE(BasicRanges) KLSK_INSTANCE(BasicRanges)
}; };
// ------------------------------------------------------------------------------------- // // ----------------------------------------------------------------------------------------- //
class AllCases { class AllCases {
public: public:
@ -97,7 +97,7 @@ private:
KLSK_INSTANCE(AllCases) KLSK_INSTANCE(AllCases)
}; };
// ------------------------------------------------------------------------------------- // // ----------------------------------------------------------------------------------------- //
} // namespace klotski::cases } // namespace klotski::cases

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

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

Loading…
Cancel
Save