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 USERAUTH_THREADPOOL_H 17 #define USERAUTH_THREADPOOL_H 18 19 #include <cstdint> 20 #include <map> 21 #include <mutex> 22 #include <vector> 23 #include "nocopyable.h" 24 #include "thread_pool.h" 25 26 namespace OHOS { 27 namespace UserIAM { 28 namespace UserAuth { 29 static constexpr char THREADPOOLNAME[] = "userauthThreadPool"; 30 static constexpr uint64_t THREADPOOLMAXSTART = 3; 31 static constexpr uint64_t THREADPOOLMAXTASK = 6; 32 33 class ContextThreadPool : ThreadPool { 34 public: 35 DISALLOW_COPY_AND_MOVE(ContextThreadPool); 36 static ContextThreadPool &GetInstance(); Start(int threadsNum)37 uint32_t Start(int threadsNum) 38 { 39 return ThreadPool::Start(threadsNum); 40 } 41 Stop()42 void Stop() 43 { 44 ThreadPool::Stop(); 45 } 46 SetMaxTaskNum(int maxSize)47 void SetMaxTaskNum(int maxSize) 48 { 49 ThreadPool::SetMaxTaskNum(maxSize); 50 } 51 GetMaxTaskNum()52 size_t GetMaxTaskNum() const 53 { 54 return ThreadPool::GetMaxTaskNum(); 55 } 56 GetCurTaskNum()57 size_t GetCurTaskNum() 58 { 59 return ThreadPool::GetCurTaskNum(); 60 } 61 GetThreadsNum()62 size_t GetThreadsNum() const 63 { 64 return ThreadPool::GetThreadsNum(); 65 } 66 67 bool AddTask(const uint64_t context, const ThreadPool::Task &f); 68 69 void TaskFunction(const uint64_t context, const ThreadPool::Task &f); 70 71 private: 72 class ContextTask { 73 public: 74 explicit ContextTask(); 75 ~ContextTask(); 76 77 ThreadPool::Task GetTask(); 78 79 void AddTask(const ThreadPool::Task &f); 80 81 private: 82 std::vector<ThreadPool::Task> tasks; 83 }; 84 explicit ContextThreadPool(const std::string &name); 85 ~ContextThreadPool() override; 86 ThreadPool::Task CheckTask(const uint64_t context); 87 std::map<uint64_t, ContextTask> ctMap_; 88 std::mutex taskMutex_; 89 }; 90 } // namespace UserAuth 91 } // namespace UserIAM 92 } // namespace OHOS 93 #endif