diff options
| author | android-build-prod (mdb) <android-build-team-robot@google.com> | 2018-10-12 02:11:39 +0000 |
|---|---|---|
| committer | android-build-prod (mdb) <android-build-team-robot@google.com> | 2018-10-12 02:11:39 +0000 |
| commit | f71a682e1c2306e3501f66d2008fdf96e460c87f (patch) | |
| tree | 97a24dfa7144bc16f9131fe19f9ba12976e5bc85 | |
| parent | 5d524a1d619fe61db253bf6d37f2498661dc72c5 (diff) | |
| parent | c0578b912871d97250fb8f6cbf6a68cc046d7014 (diff) | |
| download | platform_system_hardware_interfaces-master-cuttlefish-testing-release.tar.gz platform_system_hardware_interfaces-master-cuttlefish-testing-release.tar.bz2 platform_system_hardware_interfaces-master-cuttlefish-testing-release.zip | |
Snap for 5061196 from c0578b912871d97250fb8f6cbf6a68cc046d7014 to master-cuttlefish-testing-releasemaster-cuttlefish-testing-release
Change-Id: I9f71dd1bf81105c0b67ba7a6229bd15ace43e793
| -rw-r--r-- | suspend/1.0/default/SystemSuspend.cpp | 11 | ||||
| -rw-r--r-- | suspend/1.0/default/SystemSuspendUnitTest.cpp | 23 |
2 files changed, 32 insertions, 2 deletions
diff --git a/suspend/1.0/default/SystemSuspend.cpp b/suspend/1.0/default/SystemSuspend.cpp index 5f706717..eae240af 100644 --- a/suspend/1.0/default/SystemSuspend.cpp +++ b/suspend/1.0/default/SystemSuspend.cpp @@ -194,8 +194,15 @@ void SystemSuspend::initAutosuspend() { if (!success) { PLOG(VERBOSE) << "error writing to /sys/power/state"; } - auto callbackLock = std::lock_guard(mCallbackLock); - for (const auto& callback : mCallbacks) { + + // A callback could potentially modify mCallbacks (e.g. via registerCallback). That must + // not result in a deadlock. To that end, we make a copy of mCallbacks and release + // mCallbackLock before calling the copied callbacks. + auto callbackLock = std::unique_lock(mCallbackLock); + auto callbacksCopy = mCallbacks; + callbackLock.unlock(); + + for (const auto& callback : callbacksCopy) { callback->notifyWakeup(success).isOk(); // ignore errors } updateSleepTime(success); diff --git a/suspend/1.0/default/SystemSuspendUnitTest.cpp b/suspend/1.0/default/SystemSuspendUnitTest.cpp index 968e52fa..6d08ec4e 100644 --- a/suspend/1.0/default/SystemSuspendUnitTest.cpp +++ b/suspend/1.0/default/SystemSuspendUnitTest.cpp @@ -335,6 +335,29 @@ TEST_F(SystemSuspendTest, DeadCallback) { // or checking isOk() on every call. checkLoop(3); } + +// Callback that registers another callback. +class CbRegisteringCb : public ISystemSuspendCallback { + public: + CbRegisteringCb(sp<ISystemSuspend> suspendService) : mSuspendService(suspendService) {} + Return<void> notifyWakeup(bool x) { + sp<MockCallback> cb = new MockCallback(nullptr); + cb->disable(); + mSuspendService->registerCallback(cb); + return Void(); + } + + private: + sp<ISystemSuspend> mSuspendService; +}; + +// Tests that callback registering another callback doesn't result in a deadlock. +TEST_F(SystemSuspendTest, CallbackRegisterCallbackNoDeadlock) { + sp<CbRegisteringCb> cb = new CbRegisteringCb(suspendService); + ASSERT_TRUE(suspendService->registerCallback(cb)); + checkLoop(3); +} + } // namespace android int main(int argc, char** argv) { |
