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 #include "thread_pool_test_stub.h"
16 
17 #include "db_errno.h"
18 #include "log_print.h"
19 namespace DistributedDB {
20 const TaskId INVALID_ID = 0u;
21 
~ThreadPoolTestStub()22 ThreadPoolTestStub::~ThreadPoolTestStub()
23 {
24     LOGI("ThreadPoolTestStub Exist");
25     std::lock_guard<std::mutex> autoLock(taskPoolLock_);
26     if (taskPool_ != nullptr) {
27         taskPool_->Stop();
28         TaskPool::Release(taskPool_);
29     }
30 }
31 
Execute(const Task & task)32 TaskId ThreadPoolTestStub::Execute(const Task &task)
33 {
34     StartThreadIfNeed();
35     std::lock_guard<std::mutex> autoLock(taskPoolLock_);
36     if (taskPool_ == nullptr) {
37         return INVALID_ID;
38     }
39     static std::atomic<uint64_t> taskActionID = 0;
40     auto id = taskActionID++;
41     LOGD("Schedule task succeed, ID:%" PRIu64, id);
42     (void)taskPool_->Schedule([task, id]() {
43         LOGD("Execute task, ID:%" PRIu64, id);
44         task();
45     });
46     return id;
47 }
48 
StartThreadIfNeed()49 void ThreadPoolTestStub::StartThreadIfNeed()
50 {
51     std::lock_guard<std::mutex> autoLock(taskPoolLock_);
52     if (taskPool_ != nullptr) {
53         return;
54     }
55     int errCode = E_OK;
56     taskPool_ = TaskPool::Create(10, 1, errCode); // 10 is max thread, 1 is min thread
57     if (errCode != E_OK) {
58         LOGW("task pool create failed %d", errCode);
59         return;
60     }
61     errCode = taskPool_->Start();
62     if (errCode != E_OK) {
63         LOGW("task pool start failed %d", errCode);
64         TaskPool::Release(taskPool_);
65     }
66 }
67 
Execute(const Task & task,Duration delay)68 TaskId ThreadPoolTestStub::Execute(const Task &task, Duration delay)
69 {
70     return Execute([task, delay]() {
71         std::this_thread::sleep_for(delay);
72         task();
73     });
74 }
75 
Schedule(const Task & task,Duration interval)76 TaskId ThreadPoolTestStub::Schedule(const Task &task, Duration interval)
77 {
78     return INVALID_ID;
79 }
80 
Schedule(const Task & task,Duration delay,Duration interval)81 TaskId ThreadPoolTestStub::Schedule(const Task &task, Duration delay, Duration interval)
82 {
83     return INVALID_ID;
84 }
85 
Schedule(const Task & task,Duration delay,Duration interval,uint64_t times)86 TaskId ThreadPoolTestStub::Schedule(const Task &task, Duration delay, Duration interval, uint64_t times)
87 {
88     return INVALID_ID;
89 }
90 
Remove(const TaskId & taskId,bool wait)91 bool ThreadPoolTestStub::Remove(const TaskId &taskId, bool wait)
92 {
93     return true;
94 }
95 
Reset(const TaskId & taskId,Duration interval)96 TaskId ThreadPoolTestStub::Reset(const TaskId &taskId, Duration interval)
97 {
98     return INVALID_ID;
99 }
100 }
101