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#ifndef FFRT_WORKER_MANAGER_HPP
17#define FFRT_WORKER_MANAGER_HPP
18
19#include <thread>
20#include <list>
21#include <mutex>
22#include <shared_mutex>
23#include <condition_variable>
24#include <unordered_map>
25
26#include "eu/worker_thread.h"
27#include "eu/thread_group.h"
28#include "eu/cpu_monitor.h"
29#include "sync/sync.h"
30#include "dfx/log/ffrt_log_api.h"
31
32namespace ffrt {
33struct WorkerGroupCtl {
34    std::unique_ptr<ThreadGroup> tg;
35    uint64_t tgRefCount = 0;
36    mutable std::shared_mutex tgMutex;
37    size_t workerStackSize = 0;
38    std::unordered_map<WorkerThread*, std::unique_ptr<WorkerThread>> threads;
39};
40
41class WorkerManager {
42public:
43    virtual ~WorkerManager()
44    {
45    };
46
47    ThreadGroup* JoinTG(QoS& qos);
48    void LeaveTG(QoS& qos);
49    void JoinRtg(QoS& qos);
50
51    virtual bool IncWorker(const QoS& qos) = 0;
52    virtual bool DecWorker() = 0;
53    virtual void NotifyTaskAdded(const QoS& qos) = 0;
54    virtual void NotifyLocalTaskAdded(const QoS& qos) = 0;
55    virtual void NotifyWorkers(const QoS& qos, int number) = 0;
56    virtual std::mutex* GetSleepCtl(int qos) = 0;
57    virtual CPUMonitor* GetCPUMonitor() = 0;
58
59    WorkerGroupCtl* GetGroupCtl()
60    {
61        return groupCtl;
62    }
63protected:
64    ThreadGroup tg;
65    WorkerGroupCtl groupCtl[QoS::MaxNum()];
66};
67} // namespace ffrt
68#endif
69