1e0e9324cSopenharmony_ci/*
2e0e9324cSopenharmony_ci * Copyright (c) 2023 Shenzhen Kaihong Digital Industry Development Co., Ltd.
3e0e9324cSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4e0e9324cSopenharmony_ci * you may not use this file except in compliance with the License.
5e0e9324cSopenharmony_ci * You may obtain a copy of the License at
6e0e9324cSopenharmony_ci *
7e0e9324cSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8e0e9324cSopenharmony_ci *
9e0e9324cSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10e0e9324cSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11e0e9324cSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12e0e9324cSopenharmony_ci * See the License for the specific language governing permissions and
13e0e9324cSopenharmony_ci * limitations under the License.
14e0e9324cSopenharmony_ci */
15e0e9324cSopenharmony_ci
16e0e9324cSopenharmony_ci#ifndef OHOS_SHARING_THREAD_POOL_H
17e0e9324cSopenharmony_ci#define OHOS_SHARING_THREAD_POOL_H
18e0e9324cSopenharmony_ci
19e0e9324cSopenharmony_ci#include <atomic>
20e0e9324cSopenharmony_ci#include <chrono>
21e0e9324cSopenharmony_ci#include <functional>
22e0e9324cSopenharmony_ci#include <future>
23e0e9324cSopenharmony_ci#include <list>
24e0e9324cSopenharmony_ci#include <memory>
25e0e9324cSopenharmony_ci#include <mutex>
26e0e9324cSopenharmony_ci#include <queue>
27e0e9324cSopenharmony_ci#include <thread>
28e0e9324cSopenharmony_ci#include <unordered_map>
29e0e9324cSopenharmony_ci#include "event_base.h"
30e0e9324cSopenharmony_ci#include "nocopyable.h"
31e0e9324cSopenharmony_ci
32e0e9324cSopenharmony_cinamespace OHOS {
33e0e9324cSopenharmony_cinamespace Sharing {
34e0e9324cSopenharmony_ci
35e0e9324cSopenharmony_ciclass TaskPool : public NoCopyable {
36e0e9324cSopenharmony_cipublic:
37e0e9324cSopenharmony_ci    using Task = int32_t(const SharingEvent &);
38e0e9324cSopenharmony_ci    using BindedTask = int32_t();
39e0e9324cSopenharmony_ci
40e0e9324cSopenharmony_ci    TaskPool();
41e0e9324cSopenharmony_ci    ~TaskPool();
42e0e9324cSopenharmony_ci
43e0e9324cSopenharmony_ci    virtual inline void SetMaxTaskNum(const size_t maxSize)
44e0e9324cSopenharmony_ci    {
45e0e9324cSopenharmony_ci        maxTaskNum_ = maxSize;
46e0e9324cSopenharmony_ci    }
47e0e9324cSopenharmony_ci
48e0e9324cSopenharmony_ci    virtual inline size_t GetMaxTaskNum() const
49e0e9324cSopenharmony_ci    {
50e0e9324cSopenharmony_ci        return maxTaskNum_;
51e0e9324cSopenharmony_ci    }
52e0e9324cSopenharmony_ci
53e0e9324cSopenharmony_ci    virtual inline size_t GetTaskNum() const
54e0e9324cSopenharmony_ci    {
55e0e9324cSopenharmony_ci        return tasks_.size();
56e0e9324cSopenharmony_ci    };
57e0e9324cSopenharmony_ci
58e0e9324cSopenharmony_cipublic:
59e0e9324cSopenharmony_ci    virtual void Stop();
60e0e9324cSopenharmony_ci    virtual void SetTimeoutInterval(uint32_t ms);
61e0e9324cSopenharmony_ci    virtual void PushTask(std::packaged_task<BindedTask> &task);
62e0e9324cSopenharmony_ci
63e0e9324cSopenharmony_ci    virtual int32_t Start(int32_t threadsNum);
64e0e9324cSopenharmony_ci
65e0e9324cSopenharmony_ciprotected:
66e0e9324cSopenharmony_ci    virtual void TaskMainWorker();
67e0e9324cSopenharmony_ci    virtual bool IsOverload() const;
68e0e9324cSopenharmony_ci
69e0e9324cSopenharmony_ciprotected:
70e0e9324cSopenharmony_ci    bool isRunning_ = false;
71e0e9324cSopenharmony_ci    size_t maxTaskNum_ = 0;
72e0e9324cSopenharmony_ci    std::mutex taskMutex_;
73e0e9324cSopenharmony_ci    std::condition_variable hasTask_;
74e0e9324cSopenharmony_ci    std::condition_variable acceptNewTask_;
75e0e9324cSopenharmony_ci    std::chrono::milliseconds timeoutInterval_;
76e0e9324cSopenharmony_ci    std::vector<std::thread> threads_;
77e0e9324cSopenharmony_ci    std::deque<std::packaged_task<BindedTask>> tasks_;
78e0e9324cSopenharmony_ci};
79e0e9324cSopenharmony_ci
80e0e9324cSopenharmony_ci} // namespace Sharing
81e0e9324cSopenharmony_ci} // namespace OHOS
82e0e9324cSopenharmony_ci#endif