Skip to content

Commit

Permalink
#12685: Make CoreRangeSet thread safe (#12679)
Browse files Browse the repository at this point in the history
  • Loading branch information
tt-asaigal authored Sep 14, 2024
1 parent 2d0d38a commit c82f308
Showing 1 changed file with 26 additions and 6 deletions.
32 changes: 26 additions & 6 deletions tt_metal/common/core_coord.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include <algorithm>
#include <limits>
#include <mutex>
#include <optional>
#include <set>
#include <string>
Expand Down Expand Up @@ -264,11 +265,29 @@ class CoreRangeSet {
}
}

CoreRangeSet(const CoreRangeSet &other) = default;
CoreRangeSet &operator=(const CoreRangeSet &other) = default;
friend void swap(CoreRangeSet& first, CoreRangeSet& second) {
std::scoped_lock lock(first.ranges_guard, second.ranges_guard);
std::swap(first.ranges_, second.ranges_);
}

CoreRangeSet(const CoreRangeSet &other) {
std::scoped_lock lock(other.ranges_guard);
this->ranges_ = other.ranges_;
}
CoreRangeSet &operator=(const CoreRangeSet &other) {
std::scoped_lock lock(other.ranges_guard);
this->ranges_ = other.ranges_;
return *this;
}

CoreRangeSet(CoreRangeSet &&other) = default;
CoreRangeSet &operator=(CoreRangeSet &&other) = default;
CoreRangeSet(CoreRangeSet &&other) {
swap(*this, other);
}

CoreRangeSet &operator=(CoreRangeSet &&other) {;
swap(*this, other);
return *this;
}

auto size() const { return ranges_.size(); }

Expand Down Expand Up @@ -391,8 +410,9 @@ class CoreRangeSet {
return {{min_x, min_y}, {max_x, max_y}};
}

private:
std::set<CoreRange> ranges_;
private:
mutable std::mutex ranges_guard;
std::set<CoreRange> ranges_;
};

const inline bool operator==(const CoreRangeSet &a, const CoreRangeSet &b) {
Expand Down

0 comments on commit c82f308

Please sign in to comment.