summaryrefslogtreecommitdiff
path: root/automotive/vehicle/aidl/impl/utils/common/test/RecurrentTimerTest.cpp
diff options
context:
space:
mode:
authorAndy Quan <andyq@google.com>2023-02-16 14:15:10 -0800
committerAndy Quan <andyq@google.com>2023-02-16 14:15:10 -0800
commit9f3a2e40bf3ca1e866f5c6ec774620b05a06439b (patch)
tree1d89d996f386a5e538d216b355210d13c36cf886 /automotive/vehicle/aidl/impl/utils/common/test/RecurrentTimerTest.cpp
parentbda05164842353bb8d2e1a0d125051e6765cebdd (diff)
parent352035b047c679608086b4bb2b79ae20732c16b2 (diff)
DO NOT MERGE - Merge tm-qpr2-release into tm-platform-merge
Bug: 269346300 Change-Id: I948e2d29fc58a2937a203318b812ec0fa77baa9f
Diffstat (limited to 'automotive/vehicle/aidl/impl/utils/common/test/RecurrentTimerTest.cpp')
-rw-r--r--automotive/vehicle/aidl/impl/utils/common/test/RecurrentTimerTest.cpp27
1 files changed, 27 insertions, 0 deletions
diff --git a/automotive/vehicle/aidl/impl/utils/common/test/RecurrentTimerTest.cpp b/automotive/vehicle/aidl/impl/utils/common/test/RecurrentTimerTest.cpp
index a033a248cd..141efc135b 100644
--- a/automotive/vehicle/aidl/impl/utils/common/test/RecurrentTimerTest.cpp
+++ b/automotive/vehicle/aidl/impl/utils/common/test/RecurrentTimerTest.cpp
@@ -186,6 +186,33 @@ TEST_F(RecurrentTimerTest, testRegisterSameCallbackMultipleTimes) {
ASSERT_EQ(countTimerCallbackQueue(&timer), static_cast<size_t>(0));
}
+TEST_F(RecurrentTimerTest, testRegisterCallbackMultipleTimesNoDeadLock) {
+ // We want to avoid the following situation:
+ // Caller holds a lock while calling registerTimerCallback, registerTimerCallback will try
+ // to obtain an internal lock inside timer.
+ // Meanwhile an recurrent action happens with timer holding an internal lock. The action
+ // tries to obtain the lock currently hold by the caller.
+ // The solution is that while calling recurrent actions, timer must not hold the internal lock.
+
+ std::unique_ptr<RecurrentTimer> timer = std::make_unique<RecurrentTimer>();
+ std::mutex lock;
+ for (size_t i = 0; i < 1000; i++) {
+ std::scoped_lock<std::mutex> lockGuard(lock);
+ auto action = std::make_shared<RecurrentTimer::Callback>([&lock] {
+ // While calling this function, the timer must not hold lock in order not to dead
+ // lock.
+ std::scoped_lock<std::mutex> lockGuard(lock);
+ });
+ // 10ms
+ int64_t interval = 10'000'000;
+ timer->registerTimerCallback(interval, action);
+ // Sleep for a little while to let the recurrent actions begin.
+ std::this_thread::sleep_for(std::chrono::milliseconds(1));
+ }
+ // Make sure we stop the timer before we destroy lock.
+ timer.reset();
+}
+
} // namespace vehicle
} // namespace automotive
} // namespace hardware