• 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 #ifndef _DELAYED_WORKER_H_
17 #define _DELAYED_WORKER_H_
18 
19 #include <map>
20 #include <functional>
21 #include "cpp/sleep.h"
22 #include "sched/execute_ctx.h"
23 namespace ffrt {
24 using time_point_t = std::chrono::steady_clock::time_point;
25 
26 struct DelayedWork {
27     WaitEntry* we;
28     const std::function<void(WaitEntry*)>* cb;
29 };
30 
31 class DelayedWorker {
32     std::multimap<time_point_t, DelayedWork> map;
33     std::mutex lock;
34     std::atomic_int futex;
35     bool exited = false;
36 
37     void HandleWork(struct timespec** p);
38 
39 public:
40     DelayedWorker(DelayedWorker const&) = delete;
41     void operator=(DelayedWorker const&) = delete;
42 
43     DelayedWorker();
44 
45     ~DelayedWorker();
46 
47     bool dispatch(const time_point_t& to, WaitEntry* we, const std::function<void(WaitEntry*)>& wakeup);
48 };
49 } // namespace ffrt
50 #endif
51