• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 #ifndef HKS_USERIDM_API_WRAP_H
17 #define HKS_USERIDM_API_WRAP_H
18 
19 #include "hks_type.h"
20 #include "hks_type_inner.h"
21 #include "hks_log.h"
22 
23 #ifdef __cplusplus
24 #include <atomic>
25 #include <condition_variable>
26 #include <mutex>
27 
28 namespace OHOS::Security::Hks {
29 class HksIpcCounter {
30 public:
31     inline HksIpcCounter() = default;
32 
~HksIpcCounter()33     inline ~HksIpcCounter()
34     {
35         std::unique_lock<std::mutex> lck(mutex_);
36         if (!threadsCountDecreased_) {
37             --threadsCount_;
38         }
39         cv_.notify_one();
40         HKS_LOG_I("after notify_one threadsCount_ %" LOG_PUBLIC "u waitCount_ %" LOG_PUBLIC "u",
41             threadsCount_.load(), waitCount_.load());
42     }
43 
Wait()44     [[nodiscard]] inline int32_t Wait()
45     {
46         std::unique_lock<std::mutex> lck(mutex_);
47         ++threadsCount_;
48         while (threadsCount_ > HUKS_IPC_THREAD_WITH_CALLBACK_ASYNC_TO_SYNC_PARALLEL_NUM_LIMIT) {
49             if (waitCount_ >= HUKS_IPC_THREAD_WITH_CALLBACK_ASYNC_TO_SYNC_WAIT_NUM_LIMIT -
50                 HUKS_IPC_THREAD_WITH_CALLBACK_ASYNC_TO_SYNC_PARALLEL_NUM_LIMIT) {
51                 return LogErrorDecreaseThreadCount();
52             }
53             LogWarningWait(lck);
54         }
55         return HKS_SUCCESS;
56     }
57 
58 private:
LogErrorDecreaseThreadCount()59     inline int32_t LogErrorDecreaseThreadCount()
60     {
61         HKS_LOG_E("wait queue full! %" LOG_PUBLIC
62             "u can not wait! wait here will cause dead lock! return immediatelly!", waitCount_.load());
63         threadsCountDecreased_ = true;
64         --threadsCount_;
65         return HKS_ERROR_SESSION_REACHED_LIMIT;
66     }
67 
LogWarningWait(std::unique_lock<std::mutex> & lck)68     inline static void LogWarningWait(std::unique_lock<std::mutex> &lck)
69     {
70         ++waitCount_;
71         HKS_LOG_W("begin wait waitCount_ %" LOG_PUBLIC "u threadsCount_ %" LOG_PUBLIC "u",
72             waitCount_.load(), threadsCount_.load());
73         cv_.wait(lck);
74         HKS_LOG_W("end wait waitCount_ %" LOG_PUBLIC "u threadsCount_ %" LOG_PUBLIC "u",
75             waitCount_.load(), threadsCount_.load());
76         --waitCount_;
77     }
78 
79     std::atomic_bool threadsCountDecreased_{};
80     static inline std::atomic_uint32_t threadsCount_{};
81     static inline std::atomic_uint32_t waitCount_{};
82     static inline std::mutex mutex_{};
83     static inline std::condition_variable cv_{};
84 };
85 }
86 #endif
87 
88 #ifdef __cplusplus
89 extern "C" {
90 #endif
91 
92 void *HksLockUserIdm(void);
93 
94 void HksUnlockUserIdm(void *ptr);
95 
96 // callback
97 int32_t HksUserIdmGetSecInfo(int32_t userId, struct SecInfoWrap **outSecInfo);
98 
99 // callback
100 int32_t HksUserIdmGetAuthInfoNum(int32_t userId, enum HksUserAuthType hksAuthType, uint32_t *numOfAuthInfo);
101 
102 int32_t HksConvertUserIamTypeToHksType(enum HksUserIamType type, uint32_t userIamValue, uint32_t *hksValue);
103 
104 #ifdef __cplusplus
105 }
106 #endif
107 
108 #endif // HKS_USERIDM_API_WRAP_H