• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 * Copyright (c) 2023 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 "task_executor.h"
17 
18 namespace OHOS::NativeRdb {
TaskExecutor()19 TaskExecutor::TaskExecutor()
20 {
21 }
22 
~TaskExecutor()23 TaskExecutor::~TaskExecutor()
24 {
25     pool_ = nullptr;
26 }
27 
GetInstance()28 TaskExecutor &TaskExecutor::GetInstance()
29 {
30     static TaskExecutor instance;
31     return instance;
32 }
33 
Init()34 void TaskExecutor::Init()
35 {
36     std::unique_lock<decltype(mutex_)> lock(mutex_);
37     if (pool_ != nullptr) {
38         return;
39     }
40     pool_ = std::make_shared<ExecutorPool>(MAX_THREADS, MIN_THREADS, "TaskExecutorRDB");
41 };
42 
GetExecutor()43 std::shared_ptr<ExecutorPool> TaskExecutor::GetExecutor()
44 {
45     std::unique_lock<decltype(mutex_)> lock(mutex_);
46     if (pool_ == nullptr) {
47         pool_ = std::make_shared<ExecutorPool>(MAX_THREADS, MIN_THREADS, "TaskExecutorRDB");
48     }
49     return pool_;
50 }
51 
SetExecutor(std::shared_ptr<ExecutorPool> executor)52 void TaskExecutor::SetExecutor(std::shared_ptr<ExecutorPool> executor)
53 {
54     std::unique_lock<decltype(mutex_)> lock(mutex_);
55     pool_ = executor;
56 };
57 
Stop()58 bool TaskExecutor::Stop()
59 {
60     std::unique_lock<decltype(mutex_)> lock(mutex_);
61     pool_ = nullptr;
62     return true;
63 }
64 } // namespace OHOS::NativeRdb
65