blob: 55fc85f741fbecfe69af81c2bb9934bcf997f98e [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
12namespace aos {
Brian Silverman148d43d2020-06-07 18:19:22 -050013namespace shm_event_loop_internal {
Alex Perrycb7da4b2019-08-28 19:35:56 -070014
Brian Silverman148d43d2020-06-07 18:19:22 -050015class ShmWatcherState;
16class ShmTimerHandler;
17class ShmPhasedLoopHandler;
Austin Schuh39788ff2019-12-01 18:22:57 -080018class ShmSender;
19class ShmFetcher;
Alex Perrycb7da4b2019-08-28 19:35:56 -070020
Brian Silverman148d43d2020-06-07 18:19:22 -050021} // namespace shm_event_loop_internal
Alex Perrycb7da4b2019-08-28 19:35:56 -070022
23// Specialization of EventLoop that is built from queues running out of shared
Austin Schuh39788ff2019-12-01 18:22:57 -080024// memory.
Alex Perrycb7da4b2019-08-28 19:35:56 -070025//
Austin Schuh39788ff2019-12-01 18:22:57 -080026// TODO(austin): Timing reports break multiple threads. Need to add back in a
27// mutex.
28// This object must be interacted with from one thread, but the Senders
29// and Fetchers may be used from multiple threads afterwords (as long as their
Alex Perrycb7da4b2019-08-28 19:35:56 -070030// destructors are called back in one thread again)
31class ShmEventLoop : public EventLoop {
32 public:
33 ShmEventLoop(const Configuration *configuration);
34 ~ShmEventLoop() override;
35
Austin Schuh39788ff2019-12-01 18:22:57 -080036 // Runs the event loop until Exit is called, or ^C is caught.
37 void Run();
38 // Exits the event loop. Async safe.
39 void Exit();
40
Alex Perrycb7da4b2019-08-28 19:35:56 -070041 aos::monotonic_clock::time_point monotonic_now() override {
42 return aos::monotonic_clock::now();
43 }
44 aos::realtime_clock::time_point realtime_now() override {
45 return aos::realtime_clock::now();
46 }
47
48 std::unique_ptr<RawSender> MakeRawSender(const Channel *channel) override;
49 std::unique_ptr<RawFetcher> MakeRawFetcher(const Channel *channel) override;
50
51 void MakeRawWatcher(
52 const Channel *channel,
53 std::function<void(const Context &context, const void *message)> watcher)
54 override;
Brian Silverman6b8a3c32020-03-06 11:26:14 -080055 void MakeRawNoArgWatcher(
56 const Channel *channel,
57 std::function<void(const Context &context)> watcher) override;
Alex Perrycb7da4b2019-08-28 19:35:56 -070058
59 TimerHandler *AddTimer(std::function<void()> callback) override;
Brian Silverman148d43d2020-06-07 18:19:22 -050060 PhasedLoopHandler *AddPhasedLoop(std::function<void(int)> callback,
61 const monotonic_clock::duration interval,
62 const monotonic_clock::duration offset =
63 std::chrono::seconds(0)) override;
Alex Perrycb7da4b2019-08-28 19:35:56 -070064
65 void OnRun(std::function<void()> on_run) override;
Alex Perrycb7da4b2019-08-28 19:35:56 -070066
67 void SetRuntimeRealtimePriority(int priority) override;
Brian Silverman6a54ff32020-04-28 16:41:39 -070068 void SetRuntimeAffinity(const cpu_set_t &cpuset) override;
Alex Perrycb7da4b2019-08-28 19:35:56 -070069
James Kuszmaul57c2baa2020-01-19 14:52:52 -080070 void set_name(const std::string_view name) override;
James Kuszmaul3ae42262019-11-08 12:33:41 -080071 const std::string_view name() const override { return name_; }
Austin Schuh217a9782019-12-21 23:02:50 -080072 const Node *node() const override { return node_; }
Alex Perrycb7da4b2019-08-28 19:35:56 -070073
Austin Schuh39788ff2019-12-01 18:22:57 -080074 int priority() const override { return priority_; }
Alex Perrycb7da4b2019-08-28 19:35:56 -070075
Brian Silverman5120afb2020-01-31 17:44:35 -080076 // Returns the epoll loop used to run the event loop.
Austin Schuhe84c3ed2019-12-14 15:29:48 -080077 internal::EPoll *epoll() { return &epoll_; }
78
Brian Silverman5120afb2020-01-31 17:44:35 -080079 // Returns the local mapping of the shared memory used by the watcher on the
80 // specified channel. A watcher must be created on this channel before calling
81 // this.
82 absl::Span<char> GetWatcherSharedMemory(const Channel *channel);
83
Brian Silverman6d2b3592020-06-18 14:40:15 -070084 // Returns the local mapping of the shared memory used by the provided Sender.
Brian Silverman5120afb2020-01-31 17:44:35 -080085 template <typename T>
86 absl::Span<char> GetSenderSharedMemory(aos::Sender<T> *sender) const {
87 return GetShmSenderSharedMemory(GetRawSender(sender));
88 }
89
Brian Silverman6d2b3592020-06-18 14:40:15 -070090 // Returns the local mapping of the private memory used by the provided
91 // Fetcher to hold messages.
92 template <typename T>
93 absl::Span<char> GetFetcherPrivateMemory(aos::Fetcher<T> *fetcher) const {
94 return GetShmFetcherPrivateMemory(GetRawFetcher(fetcher));
95 }
96
Alex Perrycb7da4b2019-08-28 19:35:56 -070097 private:
Brian Silverman148d43d2020-06-07 18:19:22 -050098 friend class shm_event_loop_internal::ShmWatcherState;
99 friend class shm_event_loop_internal::ShmTimerHandler;
100 friend class shm_event_loop_internal::ShmPhasedLoopHandler;
101 friend class shm_event_loop_internal::ShmSender;
102 friend class shm_event_loop_internal::ShmFetcher;
Austin Schuh39788ff2019-12-01 18:22:57 -0800103
Brian Silverman6a54ff32020-04-28 16:41:39 -0700104 static cpu_set_t DefaultAffinity() {
105 cpu_set_t result;
106 for (int i = 0; i < CPU_SETSIZE; ++i) {
107 CPU_SET(i, &result);
108 }
109 return result;
110 }
111
Austin Schuh7d87b672019-12-01 20:23:49 -0800112 void HandleEvent();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700113
Austin Schuh39788ff2019-12-01 18:22:57 -0800114 // Returns the TID of the event loop.
115 pid_t GetTid() override;
116
Brian Silverman6d2b3592020-06-18 14:40:15 -0700117 // Private method to access the shared memory mapping of a ShmSender.
Brian Silverman5120afb2020-01-31 17:44:35 -0800118 absl::Span<char> GetShmSenderSharedMemory(const aos::RawSender *sender) const;
119
Brian Silverman6d2b3592020-06-18 14:40:15 -0700120 // Private method to access the private memory mapping of a ShmFetcher.
121 absl::Span<char> GetShmFetcherPrivateMemory(
122 const aos::RawFetcher *fetcher) const;
123
Alex Perrycb7da4b2019-08-28 19:35:56 -0700124 std::vector<std::function<void()>> on_run_;
125 int priority_ = 0;
Brian Silverman6a54ff32020-04-28 16:41:39 -0700126 cpu_set_t affinity_ = DefaultAffinity();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700127 std::string name_;
Austin Schuh217a9782019-12-21 23:02:50 -0800128 const Node *const node_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700129
130 internal::EPoll epoll_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700131};
132
Alex Perrycb7da4b2019-08-28 19:35:56 -0700133} // namespace aos
134
135#endif // AOS_EVENTS_SHM_EVENT_LOOP_H_