Skip to content

Commit

Permalink
pw_clock_tree: Introduce Element may_block()
Browse files Browse the repository at this point in the history
This change introduces the `may_block()` function for the
`Element` class. The `ElementBlocking` class sets the constructor
argument `may_block` to true, all other derived classes set
`may_block` to false.

In a subsequent change the `ClockTree` class will use the
`may_block()` function call to determine whether to acquire
the `interrupt_spin_lock_` or the `mutex_` to synchronize acceess
to the respective clock tree elements.

Bug: 331672574
Change-Id: I5196ff418f59bd8261f843981d62ab2d250ef71c
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/211148
Lint: Lint 🤖 <[email protected]>
Commit-Queue: Austin Foxley <[email protected]>
Reviewed-by: Austin Foxley <[email protected]>
  • Loading branch information
Christoph Klee authored and CQ Bot Account committed May 24, 2024
1 parent b658add commit e27b083
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
12 changes: 12 additions & 0 deletions pw_clock_tree/clock_tree_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1563,5 +1563,17 @@ TEST(ClockTree, ClockFailureRelease2Blocking) {
TEST(ClockTree, ClockFailureRelease2NonBlocking) {
TestFailureRelease2<ElementNonBlockingMightFail>();
}

TEST(ClockTree, ElementMayBlock) {
ClockSourceTest<ElementNonBlockingCannotFail> clock_non_blocking_cannot_fail;
EXPECT_FALSE(clock_non_blocking_cannot_fail.may_block());

ClockSourceTest<ElementNonBlockingMightFail> clock_non_blocking_might_fail;
EXPECT_FALSE(clock_non_blocking_might_fail.may_block());

ClockSourceTest<ElementBlocking> clock_blocking;
EXPECT_TRUE(clock_blocking.may_block());
}

} // namespace
} // namespace pw::clock_tree
15 changes: 12 additions & 3 deletions pw_clock_tree/public/pw_clock_tree/clock_tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,15 @@ namespace pw::clock_tree {
/// `ElementNonBlockingCannotFail` or `ElementNonBlockingMightFail` class.
class Element {
public:
constexpr Element() {}
constexpr Element(bool may_block = false) : may_block_(may_block) {}
virtual ~Element() = default;

/// Get reference count for this clock tree element.
uint32_t ref_count() const { return ref_count_; }

/// Check whether acquiring or releasing the element may block.
bool may_block() const { return may_block_; }

// Not copyable or movable
Element(const Element&) = delete;
Element(const Element&&) = delete;
Expand Down Expand Up @@ -97,9 +100,12 @@ class Element {
virtual Status DoDisable() { return OkStatus(); }

private:
/// Reference count for this child tree element.
/// Reference count for this tree element.
uint32_t ref_count_ = 0;

/// Whether acquiring or releasing the element may block.
const bool may_block_;

friend class ClockTree;
template <typename ElementType>
friend class DependentElement;
Expand All @@ -109,7 +115,10 @@ class Element {

/// Abstract class of a clock tree element that might need to block to perform
/// element updates.
class ElementBlocking : public Element {};
class ElementBlocking : public Element {
public:
constexpr ElementBlocking() : Element(/*may_block=*/true) {}
};

/// Abstract class of a clock tree element that will not block to perform
/// element updates and will not fail when performing clock updates.
Expand Down

0 comments on commit e27b083

Please sign in to comment.