1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <stdio.h>
17 #include <stdint.h>
18 #include <unistd.h>
19 #include <pthread.h>
20 #include "hilog_wrapper.h"
21 #include "power/suspend_ops.h"
22
23 #define SUSPEND_CHECK_INTERVAL_US 500000
24
25 static pthread_mutex_t g_mutex = PTHREAD_MUTEX_INITIALIZER;
26 static pthread_cond_t g_cond = PTHREAD_COND_INITIALIZER;
27 static pthread_t g_suspendThread;
28 static AutoSuspendLoop g_suspendLoop = NULL;
29 static uint32_t g_suspendBlockCounter = 0;
30
31 int usleep(useconds_t usec);
SuspendConditionSatisfied()32 static BOOL SuspendConditionSatisfied()
33 {
34 return (g_suspendBlockCounter == 0) ? TRUE : FALSE;
35 }
36
WaitingSuspendCondition()37 static void WaitingSuspendCondition()
38 {
39 pthread_mutex_lock(&g_mutex);
40 while (SuspendConditionSatisfied() == FALSE) {
41 pthread_cond_wait(&g_cond, &g_mutex);
42 }
43 pthread_mutex_unlock(&g_mutex);
44 }
45
SuspendThreadLoop(void * arg)46 static void *SuspendThreadLoop(void *arg)
47 {
48 if (!g_suspendLoop) {
49 return NULL;
50 }
51
52 POWER_HILOGI("Suspend thread enter loop");
53 while (1) {
54 usleep(SUSPEND_CHECK_INTERVAL_US);
55 if (g_suspendLoop(WaitingSuspendCondition) == FALSE) {
56 break;
57 }
58 }
59 return NULL;
60 }
61
Enable()62 static void Enable()
63 {
64 static BOOL started = FALSE;
65 if (started == TRUE) {
66 return;
67 }
68
69 int32_t ret = pthread_create(&g_suspendThread, NULL, SuspendThreadLoop, NULL);
70 if (ret != 0) {
71 POWER_HILOGE("Failed to create suspend thread");
72 return;
73 }
74 pthread_detach(g_suspendThread);
75 started = TRUE;
76 }
77
IncSuspendBlockCounter()78 static void IncSuspendBlockCounter()
79 {
80 pthread_mutex_lock(&g_mutex);
81 g_suspendBlockCounter++;
82 POWER_HILOGD("Suspend block counter: %{public}d", g_suspendBlockCounter);
83 pthread_mutex_unlock(&g_mutex);
84 }
85
DecSuspendBlockCounter()86 static void DecSuspendBlockCounter()
87 {
88 pthread_mutex_lock(&g_mutex);
89 g_suspendBlockCounter--;
90 POWER_HILOGD("Suspend block counter: %{public}d", g_suspendBlockCounter);
91 if (SuspendConditionSatisfied() == TRUE) {
92 pthread_cond_signal(&g_cond);
93 }
94 pthread_mutex_unlock(&g_mutex);
95 }
96
97 static struct AutoSuspendOps g_ops = {
98 .Enable = Enable,
99 .IncSuspendBlockCounter = IncSuspendBlockCounter,
100 .DecSuspendBlockCounter = DecSuspendBlockCounter,
101 };
102
AutoSuspendOpsInit()103 struct AutoSuspendOps* AutoSuspendOpsInit()
104 {
105 g_suspendLoop = AutoSuspendLoopInit();
106 if (!g_suspendLoop) {
107 POWER_HILOGE("Failed to init auto suspend loop");
108 return NULL;
109 }
110 return &g_ops;
111 }
112