Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
BusMaster.hxx
Go to the documentation of this file.
1
35#ifndef _UTILS_BUSMASTER_HXX_
36#define _UTILS_BUSMASTER_HXX_
37
39#include "utils/Buffer.hxx"
40#include "utils/LimitedPool.hxx"
42
45template <class MessageType> class Bus
46{
47public:
50
53
57
60 class Activity : public QMember
61 {
62 public:
63 using Packet = Bus::Packet;
64 using PacketPtr = Bus::PacketPtr;
65
69 virtual void fill_packet(Packet *packet) = 0;
70 };
71
74 class Master : public StateFlowBase
75 {
76 public:
89 Service *s, PacketSink *sink, Activity *idle, unsigned num_enqueued)
90 : StateFlowBase(s)
91 , idle_(idle)
92 , sink_(sink)
93 , pool_(sizeof(Packet), num_enqueued)
94 , numPacketsInPool_(num_enqueued)
95 , needShutdown_(false)
96 {
97 }
98
99#ifdef GTEST
101 void request_shutdown()
102 {
103 needShutdown_ = true;
104 }
105
107 void shutdown()
108 {
109 needShutdown_ = true;
111 {
112 usleep(200);
113 }
114 }
115#endif
116
119 void set_policy(unsigned num_prio, const Fixed16 *strides)
120 {
121 queue_.emplace(num_prio, strides);
123 }
124
129 void schedule_activity(Activity *a, unsigned prio)
130 {
131 queue_->insert(a, prio);
132 }
133
134 private:
140
144 {
146 if (needShutdown_)
147 {
148 return exit();
149 }
150 // Picks the next activity to do on the bus.
151 Activity *a = (Activity *)queue_->next().item;
152 if (!a)
153 {
154 a = idle_;
155 }
156 a->fill_packet(buf.get());
157 sink_->send(buf.release());
159 }
160
173 uint16_t needShutdown_ : 1;
174 }; // class Master
175
176private:
179};
180
181#endif // _UTILS_BUSMASTER_HXX_
BufferPtr< T > get_buffer_deleter(Buffer< T > *b)
Helper function to create a BufferPtr of an appropriate type without having to explicitly specify the...
Definition Buffer.hxx:272
AutoReleaseBuffer< T > BufferPtr
Smart pointer for buffers.
Definition Buffer.hxx:259
#define STATE(_fn)
Turns a function name into an argument to be supplied to functions expecting a state.
Definition StateFlow.hxx:61
Base class for all QMember types that hold data in an expandable format.
Definition Buffer.hxx:195
Abstract class that gets scheduled for polling and when the master decides to take it,...
Definition BusMaster.hxx:61
virtual void fill_packet(Packet *packet)=0
Complete a packet to send to the bus.
The Bus Master class.
Definition BusMaster.hxx:75
void set_policy(unsigned num_prio, const Fixed16 *strides)
Sets the scheduling policy.
uint16_t needShutdown_
True if shutdown was requested.
Action get_buffer()
Start of scheduling flow.
PacketSink * sink_
Where to send the generated packets.
Master(Service *s, PacketSink *sink, Activity *idle, unsigned num_enqueued)
Constructor.
Definition BusMaster.hxx:88
void schedule_activity(Activity *a, unsigned prio)
Adds an activity to the bus master's scheduler.
Activity * idle_
If we have no activity to do, this activity gets invoked.
LimitedPool pool_
Source of the buffers that we fill and hand out.
Action fill_pkt()
Executes the scheduling decision, fills in the packet by the selected activity and sends it to the bu...
uninitialized< ScheduledQueue > queue_
Handles the policy of polling.
uint16_t numPacketsInPool_
Total number of packets that the pool can generate.
This is a namespace class that contains a variety of related classes for handling a bus that needs a ...
Definition BusMaster.hxx:46
BufferPtr< MessageType > PacketPtr
Self-owned buffer type.
Definition BusMaster.hxx:52
Buffer< MessageType > Packet
The buffer type that is handed around to the different participants.
Definition BusMaster.hxx:49
Bus()
This class cannot be instantiated.
Abstract class for message recipients.
virtual void send(MessageType *message, unsigned priority=UINT_MAX)=0
Entry point to the flow.
Implementation of a Pool interface that takes memory from mainBufferPool (configurable) but limits th...
size_t free_items() override
Number of free items in the pool.
Essentially a "next" pointer container.
Definition QMember.hxx:42
void insert(QMember *item, unsigned prio)
Adds an entry to the queue.
Result next()
Get an item from the queue.
Collection of related state machines that pend on incoming messages.
Return type for a state flow callback.
Base class for state machines.
bool is_terminated()
Action allocate_and_call(FlowInterface< Buffer< T > > *target_flow, Callback c, Pool *pool=nullptr)
Allocates a buffer from a pool and proceed to the next state when allocation is successful.
Action exit()
Terminate current StateFlow activity.
void start_flow(Callback c)
Resets the flow to the specified state and starts it.
Buffer< T > * get_allocation_result(FlowInterface< Buffer< T > > *target_flow)
Takes the result of the asynchronous allocation.
Action call_immediately(Callback c)
Imediately call the next state upon return.
Template class that allows allocating storage for an object but not calling its constructor.
T & emplace(Args &&... args)
Constructs the embedded object.
QMember * item
item pulled from queue
Definition Queue.hxx:84