• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2022 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef BASE_MESSAGE_LOOP_MESSAGE_PUMP_EPOLL_H_
6 #define BASE_MESSAGE_LOOP_MESSAGE_PUMP_EPOLL_H_
7 
8 #include <sys/epoll.h>
9 
10 #include <cstdint>
11 #include <map>
12 
13 #include "base/base_export.h"
14 #include "base/containers/stack_container.h"
15 #include "base/files/scoped_file.h"
16 #include "base/memory/raw_ptr.h"
17 #include "base/memory/raw_ptr_exclusion.h"
18 #include "base/memory/ref_counted.h"
19 #include "base/memory/weak_ptr.h"
20 #include "base/message_loop/message_pump.h"
21 #include "base/message_loop/message_pump_libevent.h"
22 #include "base/message_loop/watchable_io_message_pump_posix.h"
23 #include "base/threading/thread_checker.h"
24 #include "base/time/time.h"
25 
26 namespace base {
27 
28 // A MessagePump implementation suitable for I/O message loops on Linux-based
29 // systems with epoll API support.
30 class BASE_EXPORT MessagePumpEpoll : public MessagePump,
31                                      public WatchableIOMessagePumpPosix {
32   using InterestParams = MessagePumpLibevent::EpollInterestParams;
33   using Interest = MessagePumpLibevent::EpollInterest;
34 
35  public:
36   using FdWatchController = MessagePumpLibevent::FdWatchController;
37 
38   MessagePumpEpoll();
39   MessagePumpEpoll(const MessagePumpEpoll&) = delete;
40   MessagePumpEpoll& operator=(const MessagePumpEpoll&) = delete;
41   ~MessagePumpEpoll() override;
42 
43   bool WatchFileDescriptor(int fd,
44                            bool persistent,
45                            int mode,
46                            FdWatchController* controller,
47                            FdWatcher* watcher);
48 
49   // MessagePump methods:
50   void Run(Delegate* delegate) override;
51   void Quit() override;
52   void ScheduleWork() override;
53   void ScheduleDelayedWork(
54       const Delegate::NextWorkInfo& next_work_info) override;
55 
56  private:
57   friend class MessagePumpLibevent;
58   friend class MessagePumpLibeventTest;
59 
60   // The WatchFileDescriptor API supports multiple FdWatchControllers watching
61   // the same file descriptor, potentially for different events; but the epoll
62   // API only supports a single interest list entry per unique file descriptor.
63   //
64   // EpollEventEntry tracks all epoll state relevant to a single file
65   // descriptor, including references to all active and inactive Interests
66   // concerned with that descriptor. This is used to derive a single aggregate
67   // interest entry for the descriptor when manipulating epoll.
68   struct EpollEventEntry {
69     explicit EpollEventEntry(int fd);
70     EpollEventEntry(const EpollEventEntry&) = delete;
71     EpollEventEntry& operator=(const EpollEventEntry&) = delete;
72     ~EpollEventEntry();
73 
FromEpollEventEpollEventEntry74     static EpollEventEntry& FromEpollEvent(epoll_event& e) {
75       return *static_cast<EpollEventEntry*>(e.data.ptr);
76     }
77 
78     // Returns the combined set of epoll event flags which should be monitored
79     // by the epoll instance for `fd`. This is based on a combination of the
80     // parameters of all currently active elements in `interests`. Namely:
81     //   - EPOLLIN is set if any active Interest wants to `read`.
82     //   - EPOLLOUT is set if any active Interest wants to `write`.
83     //   - EPOLLONESHOT is set if all active Interests are one-shot.
84     uint32_t ComputeActiveEvents();
85 
86     // The file descriptor to which this entry pertains.
87     const int fd;
88 
89     // A cached copy of the last known epoll event bits registered for this
90     // descriptor on the epoll instance.
91     uint32_t registered_events = 0;
92 
93     // A collection of all the interests regarding `fd` on this message pump.
94     // The small amount of inline storage avoids heap allocation in virtually
95     // all real scenarios, since there's little practical value in having more
96     // than two controllers (e.g. one reader and one writer) watch the same
97     // descriptor on the same thread.
98     StackVector<scoped_refptr<Interest>, 2> interests;
99 
100     // Temporary pointer to an active epoll_event structure which refers to
101     // this entry. This is set immediately upon returning from epoll_wait() and
102     // cleared again immediately before dispatching to any registered interests,
103     // so long as this entry isn't destroyed in the interim.
104     raw_ptr<epoll_event> active_event = nullptr;
105   };
106 
107   // State which lives on the stack within Run(), to support nested run loops.
108   struct RunState {
RunStateRunState109     explicit RunState(Delegate* delegate) : delegate(delegate) {}
110 
111     // `delegate` is not a raw_ptr<...> for performance reasons (based on
112     // analysis of sampling profiler data and tab_search:top100:2020).
113     RAW_PTR_EXCLUSION Delegate* const delegate;
114 
115     // Used to flag that the current Run() invocation should return ASAP.
116     bool should_quit = false;
117   };
118 
119   void AddEpollEvent(EpollEventEntry& entry);
120   void UpdateEpollEvent(EpollEventEntry& entry);
121   void UnregisterInterest(const scoped_refptr<Interest>& interest);
122   bool WaitForEpollEvents(TimeDelta timeout);
123   void OnEpollEvent(EpollEventEntry& entry, uint32_t events);
124   void HandleEvent(int fd,
125                    bool can_read,
126                    bool can_write,
127                    FdWatchController* controller);
128   void HandleWakeUp();
129 
130   // Null if Run() is not currently executing. Otherwise it's a pointer into the
131   // stack of the innermost nested Run() invocation.
132   // This field is not a raw_ptr<> because it was filtered by the rewriter for:
133   // #addr-of
134   RAW_PTR_EXCLUSION RunState* run_state_ = nullptr;
135 
136   // Mapping of all file descriptors currently watched by this message pump.
137   // std::map was chosen because (1) the number of elements can vary widely,
138   // (2) we don't do frequent lookups, and (3) values need stable addresses
139   // across insertion or removal of other elements.
140   std::map<int, EpollEventEntry> entries_;
141 
142   // The epoll instance used by this message pump to monitor file descriptors.
143   ScopedFD epoll_;
144 
145   // An eventfd object used to wake the pump's thread when scheduling new work.
146   ScopedFD wake_event_;
147 
148   // WatchFileDescriptor() must be called from this thread, and so must
149   // FdWatchController::StopWatchingFileDescriptor().
150   THREAD_CHECKER(thread_checker_);
151 
152   WeakPtrFactory<MessagePumpEpoll> weak_ptr_factory_{this};
153 };
154 
155 }  // namespace base
156 
157 #endif  // BASE_MESSAGE_LOOP_MESSAGE_PUMP_EPOLL_H_
158