1 /*
2 * Copyright (c) 2024-2024 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 "hks_upgrade_lock.h"
17
18 #include "hks_condition.h"
19 #include "hks_mutex.h"
20 #include "hks_template.h"
21 #include "hks_type_enum.h"
22
23 #include <rwlock.h>
24 #include <atomic>
25
26 static __thread bool g_readLocked = false;
27
28 namespace OHOS {
29 namespace Security {
30 namespace Hks {
31 static HksCondition *g_powerOnUpgradeCondition = NULL;
32 OHOS::Utils::RWLock g_upgradeOrRequestLock(true);
33
HksProcessConditionCreate(void)34 int32_t HksProcessConditionCreate(void)
35 {
36 g_powerOnUpgradeCondition = HksConditionCreate();
37 HKS_IF_NULL_LOGE_RETURN(g_powerOnUpgradeCondition, HKS_ERROR_BAD_STATE, "create process condition failed.")
38 return HKS_SUCCESS;
39 }
40
HksWaitIfPowerOnUpgrading(void)41 int32_t HksWaitIfPowerOnUpgrading(void)
42 {
43 int32_t ret = HksConditionWait(g_powerOnUpgradeCondition);
44 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, HKS_ERROR_BAD_STATE,
45 "HksWaitIfPowerOnUpgrading HksConditionWait fail %" LOG_PUBLIC "d", ret)
46 return HKS_SUCCESS;
47 }
48
HksUpgradeOnPowerOnDoneNotifyAll(void)49 void HksUpgradeOnPowerOnDoneNotifyAll(void)
50 {
51 HKS_LOG_I("HksUpgradeOnPowerOnDoneNotifyAll HksConditionNotifyAll");
52 int32_t ret = HksConditionNotifyAll(g_powerOnUpgradeCondition);
53 HKS_IF_NOT_SUCC_LOGE_RETURN_VOID(ret,
54 "HksUpgradeOnPowerOnDoneNotifyAll HksConditionNotifyAll fail %" LOG_PUBLIC "d", ret)
55 HKS_LOG_I("HksUpgradeOnPowerOnDoneNotifyAll HksConditionNotifyAll ok!");
56 }
57 }
58 }
59 }
60
HksUpgradeOrRequestLockRead(void)61 void HksUpgradeOrRequestLockRead(void)
62 {
63 HKS_IF_TRUE_RETURN_VOID(g_readLocked)
64 OHOS::Security::Hks::g_upgradeOrRequestLock.LockRead();
65 g_readLocked = true;
66 }
67
HksUpgradeOrRequestUnlockRead(void)68 void HksUpgradeOrRequestUnlockRead(void)
69 {
70 HKS_IF_NOT_TRUE_RETURN_VOID(g_readLocked)
71 OHOS::Security::Hks::g_upgradeOrRequestLock.UnLockRead();
72 g_readLocked = false;
73 }
74