1484543d1Sopenharmony_ci/*
2484543d1Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
3484543d1Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4484543d1Sopenharmony_ci * you may not use this file except in compliance with the License.
5484543d1Sopenharmony_ci * You may obtain a copy of the License at
6484543d1Sopenharmony_ci *
7484543d1Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8484543d1Sopenharmony_ci *
9484543d1Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10484543d1Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11484543d1Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12484543d1Sopenharmony_ci * See the License for the specific language governing permissions and
13484543d1Sopenharmony_ci * limitations under the License.
14484543d1Sopenharmony_ci */
15484543d1Sopenharmony_ci
16484543d1Sopenharmony_ci#ifndef _DELAYED_WORKER_H_
17484543d1Sopenharmony_ci#define _DELAYED_WORKER_H_
18484543d1Sopenharmony_ci
19484543d1Sopenharmony_ci#include <map>
20484543d1Sopenharmony_ci#include <functional>
21484543d1Sopenharmony_ci#include <thread>
22484543d1Sopenharmony_ci#include "cpp/sleep.h"
23484543d1Sopenharmony_ci#include "eu/cpu_monitor.h"
24484543d1Sopenharmony_ci#include "sched/execute_ctx.h"
25484543d1Sopenharmony_cinamespace ffrt {
26484543d1Sopenharmony_ciusing TimePoint = std::chrono::steady_clock::time_point;
27484543d1Sopenharmony_ci
28484543d1Sopenharmony_cistruct DelayedWork {
29484543d1Sopenharmony_ci    WaitEntry* we;
30484543d1Sopenharmony_ci    const std::function<void(WaitEntry*)>* cb;
31484543d1Sopenharmony_ci};
32484543d1Sopenharmony_ci
33484543d1Sopenharmony_ciclass DelayedWorker {
34484543d1Sopenharmony_ci    std::multimap<TimePoint, DelayedWork> map;
35484543d1Sopenharmony_ci    std::mutex lock;
36484543d1Sopenharmony_ci    std::atomic_bool toExit = false;
37484543d1Sopenharmony_ci    std::unique_ptr<std::thread> delayWorker = nullptr;
38484543d1Sopenharmony_ci    int noTaskDelayCount_{0};
39484543d1Sopenharmony_ci    bool exited_ = false;
40484543d1Sopenharmony_ci    int epollfd_{-1};
41484543d1Sopenharmony_ci    int timerfd_{-1};
42484543d1Sopenharmony_ci#ifdef FFRT_WORKERS_DYNAMIC_SCALING
43484543d1Sopenharmony_ci    int monitorfd_{-1};
44484543d1Sopenharmony_ci    CPUMonitor* monitor;
45484543d1Sopenharmony_ci#endif
46484543d1Sopenharmony_ci    int HandleWork(void);
47484543d1Sopenharmony_ci    void ThreadInit();
48484543d1Sopenharmony_ci
49484543d1Sopenharmony_cipublic:
50484543d1Sopenharmony_ci    static DelayedWorker &GetInstance();
51484543d1Sopenharmony_ci
52484543d1Sopenharmony_ci    DelayedWorker(DelayedWorker const&) = delete;
53484543d1Sopenharmony_ci    void operator=(DelayedWorker const&) = delete;
54484543d1Sopenharmony_ci
55484543d1Sopenharmony_ci    bool dispatch(const TimePoint& to, WaitEntry* we, const std::function<void(WaitEntry*)>& wakeup);
56484543d1Sopenharmony_ci    bool remove(const TimePoint& to, WaitEntry* we);
57484543d1Sopenharmony_ci
58484543d1Sopenharmony_ciprivate:
59484543d1Sopenharmony_ci    DelayedWorker();
60484543d1Sopenharmony_ci
61484543d1Sopenharmony_ci    ~DelayedWorker();
62484543d1Sopenharmony_ci};
63484543d1Sopenharmony_ci} // namespace ffrt
64484543d1Sopenharmony_ci#endif
65