1cb69b360Sopenharmony_ci/*
2cb69b360Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd.
3cb69b360Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4cb69b360Sopenharmony_ci * you may not use this file except in compliance with the License.
5cb69b360Sopenharmony_ci * You may obtain a copy of the License at
6cb69b360Sopenharmony_ci *
7cb69b360Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8cb69b360Sopenharmony_ci *
9cb69b360Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10cb69b360Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11cb69b360Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12cb69b360Sopenharmony_ci * See the License for the specific language governing permissions and
13cb69b360Sopenharmony_ci * limitations under the License.
14cb69b360Sopenharmony_ci */
15cb69b360Sopenharmony_ci
16cb69b360Sopenharmony_ci#include "hilog_wrapper.h"
17cb69b360Sopenharmony_ci#include "power/suspend_ops.h"
18cb69b360Sopenharmony_ci
19cb69b360Sopenharmony_cistatic struct AutoSuspendOps* g_suspendOps = NULL;
20cb69b360Sopenharmony_cistatic struct RunningLockOps* g_runningLockOps = NULL;
21cb69b360Sopenharmony_ci
22cb69b360Sopenharmony_civoid RunningLockHubLock(const char* name)
23cb69b360Sopenharmony_ci{
24cb69b360Sopenharmony_ci    if (!g_runningLockOps || !g_suspendOps) {
25cb69b360Sopenharmony_ci        POWER_HILOGE("Invalid running lock or suspend ops");
26cb69b360Sopenharmony_ci        return;
27cb69b360Sopenharmony_ci    }
28cb69b360Sopenharmony_ci    g_runningLockOps->Acquire(name);
29cb69b360Sopenharmony_ci    /*
30cb69b360Sopenharmony_ci     * Because the upper layer call can ensure the acquirment of locks, it is simple to implement here.
31cb69b360Sopenharmony_ci     * It's better to maintain a running lock map by name.
32cb69b360Sopenharmony_ci     */
33cb69b360Sopenharmony_ci    g_suspendOps->IncSuspendBlockCounter();
34cb69b360Sopenharmony_ci}
35cb69b360Sopenharmony_ci
36cb69b360Sopenharmony_civoid RunningLockHubUnlock(const char* name)
37cb69b360Sopenharmony_ci{
38cb69b360Sopenharmony_ci    if (!g_runningLockOps || !g_suspendOps) {
39cb69b360Sopenharmony_ci        POWER_HILOGE("Invalid running lock or suspend ops");
40cb69b360Sopenharmony_ci        return;
41cb69b360Sopenharmony_ci    }
42cb69b360Sopenharmony_ci    g_runningLockOps->Release(name);
43cb69b360Sopenharmony_ci    g_suspendOps->DecSuspendBlockCounter();
44cb69b360Sopenharmony_ci}
45cb69b360Sopenharmony_ci
46cb69b360Sopenharmony_ciBOOL RunningLockHubInit(struct AutoSuspendOps* suspendOps)
47cb69b360Sopenharmony_ci{
48cb69b360Sopenharmony_ci    g_runningLockOps = RunningLockOpsInit();
49cb69b360Sopenharmony_ci    if (!g_runningLockOps) {
50cb69b360Sopenharmony_ci        POWER_HILOGE("Failed to init runninglock ops");
51cb69b360Sopenharmony_ci        return FALSE;
52cb69b360Sopenharmony_ci    }
53cb69b360Sopenharmony_ci    g_suspendOps = suspendOps;
54cb69b360Sopenharmony_ci    return TRUE;
55cb69b360Sopenharmony_ci}
56