blob: 4460436ee91fb7cfa866ee00d81be0144fafe3d5 [file] [log] [blame]
Alex Perrycb7da4b2019-08-28 19:35:56 -07001#ifndef AOS_EVENTS_SHM_EVENT_LOOP_H_
2#define AOS_EVENTS_SHM_EVENT_LOOP_H_
3
Alex Perrycb7da4b2019-08-28 19:35:56 -07004#include <vector>
5
Brian Silverman5120afb2020-01-31 17:44:35 -08006#include "absl/types/span.h"
7
Alex Perrycb7da4b2019-08-28 19:35:56 -07008#include "aos/events/epoll.h"
9#include "aos/events/event_loop.h"
Austin Schuh39788ff2019-12-01 18:22:57 -080010#include "aos/events/event_loop_generated.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070011
Jim Ostrowski2192ddb2020-06-24 19:07:31 -070012DECLARE_string(application_name);
13DECLARE_string(shm_base);
14
Alex Perrycb7da4b2019-08-28 19:35:56 -070015namespace aos {
Brian Silverman148d43d2020-06-07 18:19:22 -050016namespace shm_event_loop_internal {
Alex Perrycb7da4b2019-08-28 19:35:56 -070017
Brian Silverman148d43d2020-06-07 18:19:22 -050018class ShmWatcherState;
19class ShmTimerHandler;
20class ShmPhasedLoopHandler;
Austin Schuh39788ff2019-12-01 18:22:57 -080021class ShmSender;
22class ShmFetcher;
Alex Perrycb7da4b2019-08-28 19:35:56 -070023
Brian Silverman148d43d2020-06-07 18:19:22 -050024} // namespace shm_event_loop_internal
Alex Perrycb7da4b2019-08-28 19:35:56 -070025
26// Specialization of EventLoop that is built from queues running out of shared
Austin Schuh39788ff2019-12-01 18:22:57 -080027// memory.
Alex Perrycb7da4b2019-08-28 19:35:56 -070028//
Austin Schuh39788ff2019-12-01 18:22:57 -080029// TODO(austin): Timing reports break multiple threads. Need to add back in a
30// mutex.
31// This object must be interacted with from one thread, but the Senders
32// and Fetchers may be used from multiple threads afterwords (as long as their
Alex Perrycb7da4b2019-08-28 19:35:56 -070033// destructors are called back in one thread again)
34class ShmEventLoop : public EventLoop {
35 public:
36 ShmEventLoop(const Configuration *configuration);
37 ~ShmEventLoop() override;
38
Austin Schuh39788ff2019-12-01 18:22:57 -080039 // Runs the event loop until Exit is called, or ^C is caught.
40 void Run();
41 // Exits the event loop. Async safe.
42 void Exit();
43
Alex Perrycb7da4b2019-08-28 19:35:56 -070044 aos::monotonic_clock::time_point monotonic_now() override {
45 return aos::monotonic_clock::now();
46 }
47 aos::realtime_clock::time_point realtime_now() override {
48 return aos::realtime_clock::now();
49 }
50
51 std::unique_ptr<RawSender> MakeRawSender(const Channel *channel) override;
52 std::unique_ptr<RawFetcher> MakeRawFetcher(const Channel *channel) override;
53
54 void MakeRawWatcher(
55 const Channel *channel,
56 std::function<void(const Context &context, const void *message)> watcher)
57 override;
Brian Silverman6b8a3c32020-03-06 11:26:14 -080058 void MakeRawNoArgWatcher(
59 const Channel *channel,
60 std::function<void(const Context &context)> watcher) override;
Alex Perrycb7da4b2019-08-28 19:35:56 -070061
62 TimerHandler *AddTimer(std::function<void()> callback) override;
Brian Silverman148d43d2020-06-07 18:19:22 -050063 PhasedLoopHandler *AddPhasedLoop(std::function<void(int)> callback,
64 const monotonic_clock::duration interval,
65 const monotonic_clock::duration offset =
66 std::chrono::seconds(0)) override;
Alex Perrycb7da4b2019-08-28 19:35:56 -070067
68 void OnRun(std::function<void()> on_run) override;
Alex Perrycb7da4b2019-08-28 19:35:56 -070069
70 void SetRuntimeRealtimePriority(int priority) override;
Brian Silverman6a54ff32020-04-28 16:41:39 -070071 void SetRuntimeAffinity(const cpu_set_t &cpuset) override;
Alex Perrycb7da4b2019-08-28 19:35:56 -070072
James Kuszmaul57c2baa2020-01-19 14:52:52 -080073 void set_name(const std::string_view name) override;
James Kuszmaul3ae42262019-11-08 12:33:41 -080074 const std::string_view name() const override { return name_; }
Austin Schuh217a9782019-12-21 23:02:50 -080075 const Node *node() const override { return node_; }
Alex Perrycb7da4b2019-08-28 19:35:56 -070076
Austin Schuh39788ff2019-12-01 18:22:57 -080077 int priority() const override { return priority_; }
Alex Perrycb7da4b2019-08-28 19:35:56 -070078
Brian Silverman5120afb2020-01-31 17:44:35 -080079 // Returns the epoll loop used to run the event loop.
Austin Schuhe84c3ed2019-12-14 15:29:48 -080080 internal::EPoll *epoll() { return &epoll_; }
81
Brian Silverman5120afb2020-01-31 17:44:35 -080082 // Returns the local mapping of the shared memory used by the watcher on the
83 // specified channel. A watcher must be created on this channel before calling
84 // this.
85 absl::Span<char> GetWatcherSharedMemory(const Channel *channel);
86
Brian Silverman6d2b3592020-06-18 14:40:15 -070087 // Returns the local mapping of the shared memory used by the provided Sender.
Brian Silverman5120afb2020-01-31 17:44:35 -080088 template <typename T>
89 absl::Span<char> GetSenderSharedMemory(aos::Sender<T> *sender) const {
90 return GetShmSenderSharedMemory(GetRawSender(sender));
91 }
92
Brian Silverman6d2b3592020-06-18 14:40:15 -070093 // Returns the local mapping of the private memory used by the provided
94 // Fetcher to hold messages.
95 template <typename T>
96 absl::Span<char> GetFetcherPrivateMemory(aos::Fetcher<T> *fetcher) const {
97 return GetShmFetcherPrivateMemory(GetRawFetcher(fetcher));
98 }
99
Alex Perrycb7da4b2019-08-28 19:35:56 -0700100 private:
Brian Silverman148d43d2020-06-07 18:19:22 -0500101 friend class shm_event_loop_internal::ShmWatcherState;
102 friend class shm_event_loop_internal::ShmTimerHandler;
103 friend class shm_event_loop_internal::ShmPhasedLoopHandler;
104 friend class shm_event_loop_internal::ShmSender;
105 friend class shm_event_loop_internal::ShmFetcher;
Austin Schuh39788ff2019-12-01 18:22:57 -0800106
Brian Silverman6a54ff32020-04-28 16:41:39 -0700107 static cpu_set_t DefaultAffinity() {
108 cpu_set_t result;
109 for (int i = 0; i < CPU_SETSIZE; ++i) {
110 CPU_SET(i, &result);
111 }
112 return result;
113 }
114
Austin Schuh7d87b672019-12-01 20:23:49 -0800115 void HandleEvent();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700116
Austin Schuh39788ff2019-12-01 18:22:57 -0800117 // Returns the TID of the event loop.
118 pid_t GetTid() override;
119
Brian Silverman6d2b3592020-06-18 14:40:15 -0700120 // Private method to access the shared memory mapping of a ShmSender.
Brian Silverman5120afb2020-01-31 17:44:35 -0800121 absl::Span<char> GetShmSenderSharedMemory(const aos::RawSender *sender) const;
122
Brian Silverman6d2b3592020-06-18 14:40:15 -0700123 // Private method to access the private memory mapping of a ShmFetcher.
124 absl::Span<char> GetShmFetcherPrivateMemory(
125 const aos::RawFetcher *fetcher) const;
126
Alex Perrycb7da4b2019-08-28 19:35:56 -0700127 std::vector<std::function<void()>> on_run_;
128 int priority_ = 0;
Brian Silverman6a54ff32020-04-28 16:41:39 -0700129 cpu_set_t affinity_ = DefaultAffinity();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700130 std::string name_;
Austin Schuh217a9782019-12-21 23:02:50 -0800131 const Node *const node_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700132
133 internal::EPoll epoll_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700134};
135
Alex Perrycb7da4b2019-08-28 19:35:56 -0700136} // namespace aos
137
138#endif // AOS_EVENTS_SHM_EVENT_LOOP_H_