Add force_suspend function
This CL implements a simple force_suspend(). This function takes a
timeoutMs argument that is currently ignored. It also uses a two-stage
init process that spawns the suspend_thread when autosuspend is in use.
Follow-up CLs will:
- Implement file locking to prevent multiple libsuspend instances from
starting multiple suspend threads in the system.
- Fully implement timout for force_suspend.
- Integrate force_suspend with suspend_thred.
Bug: 70669809
Test: autosuspend still works
Change-Id: I6b2c72d0c61d1bec6db0f0b28481ffe0e20ec615
diff --git a/libsuspend/autosuspend.c b/libsuspend/autosuspend.c
index 09fc061..b87f59c 100644
--- a/libsuspend/autosuspend.c
+++ b/libsuspend/autosuspend.c
@@ -24,28 +24,20 @@
#include "autosuspend_ops.h"
-static struct autosuspend_ops *autosuspend_ops;
+static struct autosuspend_ops* autosuspend_ops = NULL;
static bool autosuspend_enabled;
-static bool autosuspend_inited;
static int autosuspend_init(void) {
- if (autosuspend_inited) {
+ if (autosuspend_ops != NULL) {
return 0;
}
autosuspend_ops = autosuspend_wakeup_count_init();
- if (autosuspend_ops) {
- goto out;
- }
-
- if (!autosuspend_ops) {
+ if (autosuspend_ops == NULL) {
ALOGE("failed to initialize autosuspend");
return -1;
}
-out:
- autosuspend_inited = true;
-
ALOGV("autosuspend initialized");
return 0;
}
@@ -96,6 +88,19 @@
return 0;
}
+int autosuspend_force_suspend(int timeout_ms) {
+ int ret;
+
+ ret = autosuspend_init();
+ if (ret) {
+ return ret;
+ }
+
+ ALOGV("autosuspend_force_suspend");
+
+ return autosuspend_ops->force_suspend(timeout_ms);
+}
+
void autosuspend_set_wakeup_callback(void (*func)(bool success)) {
int ret;