Skip to content

Commit 30209e0

Browse files
Resolve data races found by ThreadSanitizer
Signed-off-by: Eryk Szpotanski <[email protected]> Co-authored-by: Krzysztof Bieganski <[email protected]>
1 parent d066711 commit 30209e0

File tree

4 files changed

+36
-16
lines changed

4 files changed

+36
-16
lines changed

graph/Graph.cc

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,7 @@ Graph::makeArrivals(Vertex *vertex,
586586
Arrival *
587587
Graph::arrivals(Vertex *vertex)
588588
{
589+
LockGuard lock(arrivals_lock_);
589590
return arrivals_.pointer(vertex->arrivals());
590591
}
591592

@@ -619,6 +620,7 @@ Graph::makeRequireds(Vertex *vertex,
619620
Required *
620621
Graph::requireds(Vertex *vertex)
621622
{
623+
LockGuard lock(requireds_lock_);
622624
return requireds_.pointer(vertex->requireds());
623625
}
624626

@@ -650,6 +652,7 @@ Graph::makePrevPaths(Vertex *vertex,
650652
PathVertexRep *
651653
Graph::prevPaths(Vertex *vertex) const
652654
{
655+
LockGuard lock(prev_paths_lock_);
653656
return prev_paths_.pointer(vertex->prevPaths());
654657
}
655658

@@ -1352,20 +1355,31 @@ Vertex::setHasDownstreamClkPin(bool has_clk_pin)
13521355
has_downstream_clk_pin_ = has_clk_pin;
13531356
}
13541357

1358+
#define IN_QUEUE(mask, index) (mask & (1 << unsigned(index)))
1359+
#define SET_IN_QUEUE(mask, index) ((mask) |= (1 << unsigned(index)))
1360+
#define CLEAR_IN_QUEUE(mask, index) ((mask) &= ~(1 << unsigned(index)))
1361+
13551362
bool
13561363
Vertex::bfsInQueue(BfsIndex index) const
13571364
{
1358-
return (bfs_in_queue_ >> unsigned(index)) & 1;
1365+
return IN_QUEUE(bfs_in_queue_, index);
13591366
}
13601367

1361-
void
1368+
bool
13621369
Vertex::setBfsInQueue(BfsIndex index,
13631370
bool value)
13641371
{
1365-
if (value)
1366-
bfs_in_queue_ |= 1 << int(index);
1367-
else
1368-
bfs_in_queue_ &= ~(1 << int(index));
1372+
unsigned char expected = bfs_in_queue_;
1373+
unsigned char desired;
1374+
do {
1375+
if ((value && IN_QUEUE(expected, index)) || (!value && !IN_QUEUE(expected, index))) {
1376+
return false;
1377+
}
1378+
desired = expected;
1379+
SET_IN_QUEUE(value ? desired : expected, index);
1380+
CLEAR_IN_QUEUE(value ? expected : desired, index);
1381+
} while (!bfs_in_queue_.compare_exchange_weak(expected, desired));
1382+
return true;
13691383
}
13701384

13711385
////////////////////////////////////////////////////////////////

include/sta/Graph.hh

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#pragma once
1818

19+
#include <atomic>
1920
#include <mutex>
2021

2122
#include "Iterator.hh"
@@ -253,7 +254,7 @@ protected:
253254
RequiredsTable requireds_;
254255
std::mutex requireds_lock_;
255256
PrevPathsTable prev_paths_;
256-
std::mutex prev_paths_lock_;
257+
mutable std::mutex prev_paths_lock_;
257258
Vector<bool> arc_delay_annotated_;
258259
int slew_rf_count_;
259260
bool have_arc_delays_;
@@ -329,7 +330,7 @@ public:
329330
bool isConstrained() const { return is_constrained_; }
330331
void setIsConstrained(bool constrained);
331332
bool bfsInQueue(BfsIndex index) const;
332-
void setBfsInQueue(BfsIndex index, bool value);
333+
bool setBfsInQueue(BfsIndex index, bool value);
333334
bool isRegClk() const { return is_reg_clk_; }
334335
bool crprPathPruningDisabled() const { return crpr_path_pruning_disabled_;}
335336
void setCrprPathPruningDisabled(bool disabled);
@@ -357,10 +358,10 @@ protected:
357358
EdgeId out_edges_; // Edges from this vertex.
358359

359360
// 32 bits
360-
unsigned int tag_group_index_:tag_group_index_bits; // 24
361+
unsigned int tag_group_index_; // >= tag_group_index_bits = 24
361362
// Each bit corresponds to a different BFS queue.
362-
unsigned int bfs_in_queue_:int(BfsIndex::bits); // 4
363-
unsigned int slew_annotated_:slew_annotated_bits;
363+
std::atomic<unsigned char> bfs_in_queue_; // >= int(BfsIndex::bits) = 4
364+
unsigned char object_idx_; // >= VertexTable::idx_bits = 7
364365

365366
// 32 bits
366367
unsigned int level_:Graph::vertex_level_bits;
@@ -385,7 +386,8 @@ protected:
385386
bool has_downstream_clk_pin_:1;
386387
bool crpr_path_pruning_disabled_:1;
387388
bool requireds_pruned_:1;
388-
unsigned object_idx_:VertexTable::idx_bits;
389+
390+
unsigned int slew_annotated_:slew_annotated_bits;
389391

390392
private:
391393
friend class Graph;

include/sta/Search.hh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -587,7 +587,7 @@ protected:
587587
TagIndex tag_next_;
588588
// Holes in tags_ left by deleting filter tags.
589589
std::vector<TagIndex> tag_free_indices_;
590-
std::mutex tag_lock_;
590+
mutable std::mutex tag_lock_;
591591
TagGroupSet *tag_group_set_;
592592
TagGroup **tag_groups_;
593593
TagGroup **tag_groups_prev_;
@@ -596,7 +596,7 @@ protected:
596596
std::vector<TagIndex> tag_group_free_indices_;
597597
// Capacity of tag_groups_.
598598
TagGroupIndex tag_group_capacity_;
599-
std::mutex tag_group_lock_;
599+
mutable std::mutex tag_group_lock_;
600600
// Latches data outputs to queue on the next search pass.
601601
VertexSet *pending_latch_outputs_;
602602
std::mutex pending_latch_outputs_lock_;

search/Search.cc

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2775,6 +2775,7 @@ Search::reportArrivals(Vertex *vertex) const
27752775
TagGroup *
27762776
Search::tagGroup(TagGroupIndex index) const
27772777
{
2778+
LockGuard lock(tag_group_lock_);
27782779
return tag_groups_[index];
27792780
}
27802781

@@ -2784,13 +2785,14 @@ Search::tagGroup(const Vertex *vertex) const
27842785
TagGroupIndex index = vertex->tagGroupIndex();
27852786
if (index == tag_group_index_max)
27862787
return nullptr;
2787-
else
2788-
return tag_groups_[index];
2788+
LockGuard lock(tag_group_lock_);
2789+
return tag_groups_[index];
27892790
}
27902791

27912792
TagGroupIndex
27922793
Search::tagGroupCount() const
27932794
{
2795+
LockGuard lock(tag_group_lock_);
27942796
return tag_group_set_->size();
27952797
}
27962798

@@ -2845,12 +2847,14 @@ Search::reportArrivalCountHistogram() const
28452847
Tag *
28462848
Search::tag(TagIndex index) const
28472849
{
2850+
LockGuard lock(tag_lock_);
28482851
return tags_[index];
28492852
}
28502853

28512854
TagIndex
28522855
Search::tagCount() const
28532856
{
2857+
LockGuard lock(tag_lock_);
28542858
return tag_set_->size();
28552859
}
28562860

0 commit comments

Comments
 (0)