• 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 #include "context_thread_pool.h"
17 #include "userauth_hilog_wrapper.h"
18 
19 namespace OHOS {
20 namespace UserIAM {
21 namespace UserAuth {
ContextThreadPool(const std::string & name)22 ContextThreadPool::ContextThreadPool(const std::string &name) : ThreadPool(name) {
23 }
24 
~ContextThreadPool()25 ContextThreadPool::~ContextThreadPool() {
26 }
27 
GetInstance()28 ContextThreadPool &ContextThreadPool::GetInstance()
29 {
30     static ContextThreadPool instance(THREADPOOLNAME);
31     return instance;
32 }
33 
AddTask(const uint64_t context,const ThreadPool::Task & f)34 bool ContextThreadPool::AddTask(const uint64_t context, const ThreadPool::Task& f)
35 {
36     std::lock_guard<std::mutex> taskMutexGuard(taskMutex_);
37     if (ThreadPool::GetCurTaskNum() >= ThreadPool::GetMaxTaskNum()) {
38         return false;
39     }
40     ContextTask contextTask;
41     bool hasContextTask = false;
42     if (ctMap_.count(context) != 0) {
43         hasContextTask = true;
44         contextTask = ctMap_[context];
45     }
46     auto task = std::bind(&ContextThreadPool::TaskFunction, this, context, f);
47     if (!hasContextTask) {
48         ThreadPool::AddTask(task);
49     } else {
50         contextTask.AddTask(task);
51     }
52     ctMap_[context] = contextTask;
53     return true;
54 }
55 
TaskFunction(const uint64_t context,const ThreadPool::Task & f)56 void ContextThreadPool::TaskFunction(const uint64_t context, const ThreadPool::Task& f)
57 {
58     f();
59     ThreadPool::Task next = CheckTask(context);
60     if (next != nullptr) {
61         next();
62     }
63 }
64 
CheckTask(const uint64_t context)65 ThreadPool::Task ContextThreadPool::CheckTask(const uint64_t context)
66 {
67     std::lock_guard<std::mutex> taskMutexGuard(taskMutex_);
68     if (ctMap_.count(context) != 0) {
69         ContextTask contextTask = ctMap_[context];
70         ThreadPool::Task next = contextTask.GetTask();
71         ctMap_[context] = contextTask;
72         if (next == nullptr) {
73             ctMap_.erase(context);
74         }
75         return next;
76     }
77     return nullptr;
78 }
79 
ContextTask()80 ContextThreadPool::ContextTask::ContextTask() {
81 }
82 
~ContextTask()83 ContextThreadPool::ContextTask::~ContextTask() {
84 }
85 
GetTask()86 ThreadPool::Task ContextThreadPool::ContextTask::GetTask()
87 {
88     if (!tasks.empty()) {
89         Task f = tasks.front();
90         tasks.erase(tasks.begin());
91         return f;
92     }
93     return nullptr;
94 }
95 
AddTask(const ThreadPool::Task & f)96 void ContextThreadPool::ContextTask::AddTask(const ThreadPool::Task& f)
97 {
98     tasks.push_back(f);
99 }
100 } // namespace UserAuth
101 } // namespace UserIam
102 } // namespace OHOS
103