• 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 HICORO_IOPOLLER_H
17 #define HICORO_IOPOLLER_H
18 #ifndef _MSC_VER
19 #include <sys/epoll.h>
20 #include <sys/eventfd.h>
21 #endif
22 #include <thread>
23 #include <vector>
24 #include <map>
25 #include <atomic>
26 #include "ffrt_inner.h"
27 #include "internal_inc/non_copyable.h"
28 
29 namespace ffrt {
30 struct WakeData {
31     int fd;
32     void* data;
33 };
34 
35 struct IOPoller: private NonCopyable {
36     IOPoller() noexcept;
37 
38     virtual ~IOPoller() noexcept;
39 
40     void WakeUp() noexcept;
41     bool CasStrong(std::atomic<int> &a, int cmp, int exc);
42     void WaitFdEvent(int fd) noexcept;
43     void PollOnce(int timeout = -1) noexcept;
44 
45 private:
46     int m_epFd;
47     struct WakeData m_wakeData;
48 #ifndef _MSC_VER
49     std::vector<epoll_event> m_events;
50 #endif
51 };
52 
53 IOPoller& GetIOPoller() noexcept;
54 }
55 
ffrt_wait_fd(int fd)56 static inline void ffrt_wait_fd(int fd)
57 {
58 #ifndef _MSC_VER
59     ffrt::GetIOPoller().WaitFdEvent(fd);
60 #endif
61 }
62 #endif
63