Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
openlcb::BulkAliasAllocator Class Reference

Implementation of the BulkAliasAllocatorInterface to allocate many aliases at the same time. More...

Inheritance diagram for openlcb::BulkAliasAllocator:
CallableFlow< BulkAliasRequest > StateFlow< MessageType, QueueType > TypedStateFlow< MessageType, UntypedStateFlow< QueueType > > UntypedStateFlow< QueueType > FlowInterface< MessageType > StateFlowWithQueue StateFlowBase Atomic LinkedObject< StateFlowWithQueue > Executable Notifiable QMember Destructable

Classes

struct  AliasLookupInfo
 We store this type in the sorted map lookup structure. More...
 
struct  LookupCompare
 Comparator function on AliasLookupInfo objects. More...
 
struct  PendingAliasInfo
 We store this type in the time-ordered aliases structure. More...
 

Public Member Functions

 BulkAliasAllocator (IfCan *iface)
 Constructor.
 
Action entry () override
 Start of flow when a request arrives to allocate many aliases.
 
Action send_cid_frames ()
 Picks a bunch of random aliases, sends CID frames for them to the bus.
 
Action stamp_time ()
 Adds the timestamps when the CID requests were sent out.
 
Action wait_for_results ()
 Sends out the RID frames for any alias that the 200 msec has already elapsed, then waits a bit and tries again.
 
Action complete ()
 Called when all RID frames are sent out.
 
- Public Member Functions inherited from CallableFlow< BulkAliasRequest >
 CallableFlow (Service *s)
 Creates a callable flow.
 
- Public Member Functions inherited from StateFlow< MessageType, QueueType >
 StateFlow (Service *service)
 Constructor.
 
- Public Member Functions inherited from TypedStateFlow< MessageType, UntypedStateFlow< QueueType > >
 TypedStateFlow (Service *service)
 Constructor.
 
virtual ~TypedStateFlow ()
 Destructor.
 
void send (MessageType *msg, unsigned priority=UINT_MAX) OVERRIDE
 Sends a message to the state flow for processing.
 
- Public Member Functions inherited from UntypedStateFlow< QueueType >
 UntypedStateFlow (Service *service)
 Constructor.
 
- Public Member Functions inherited from StateFlowWithQueue
void notify () override
 Wakeup call arrived. Schedules *this on the executor.
 
bool is_waiting ()
 
- Public Member Functions inherited from StateFlowBase
void run () override
 Callback from the executor.
 
Serviceservice ()
 Return a pointer to the service I am bound to.
 
- Public Member Functions inherited from Executable
void test_deletion ()
 
- Public Member Functions inherited from QMember
void init ()
 Initiailize a QMember, in place of a public placement construction.
 
- Public Member Functions inherited from LinkedObject< StateFlowWithQueue >
StateFlowWithQueuelink_next ()
 
- Public Member Functions inherited from FlowInterface< MessageType >
virtual Poolpool ()
 
virtual MessageType * type_helper ()
 This function is never user in the code, but GDB can use it to infer the correct message types.
 
MessageType * alloc ()
 Synchronously allocates a message buffer from the pool of this flow.
 
void alloc_async (Executable *target)
 Asynchronously allocates a message buffer from the pool of this flow.
 

Private Member Functions

void handle_conflict (Buffer< CanMessageData > *message)
 Callback from the stack for all incoming frames while we are operating.
 
void send_can_frame (NodeAlias src, uint16_t control_field, int sequence)
 Sends a CAN control frame to the bus.
 
IfCanif_can ()
 
unsigned relative_time ()
 

Private Attributes

IncomingFrameHandler::GenericHandler conflictHandler_
 Listens to incoming CAN frames and handles alias conflicts.
 
StateFlowTimer timer_ {this}
 Helper object for sleeping.
 
BarrierNotifiable bn_
 Helper object to determine when the CAN frames have flushed from the system.
 
long long startTime_
 We measure time elapsed relative to this point.
 
std::vector< PendingAliasInfopendingAliasesByTime_
 Stores the aliases we are trying to allocate in time order of picking them.
 
SortedListSet< AliasLookupInfo, LookupComparependingAliasesByKey_
 Stores the aliases we are trying to allocate in the alias order.
 
uint16_t nextToStampTime_
 Index into the pendingAliasesByTime_ vector where we need to stmap time.
 
uint16_t nextToClaim_
 Index into the pendingAliasesByTime_ vector where we need to send out the reserve frame.
 

Static Private Attributes

static constexpr unsigned ALLOCATE_DELAY = 20
 How many count to wait before sending out the RID frames.
 

Additional Inherited Members

- Public Types inherited from TypedStateFlow< MessageType, UntypedStateFlow< QueueType > >
typedef Base::Action Action
 Allows using Action without having StateFlowBase:: prefix in front of it.
 
- Public Types inherited from FlowInterface< MessageType >
typedef MessageType message_type
 Stores the message template type for external reference.
 
- Static Public Member Functions inherited from StateFlowBase
template<class T , typename... Args>
static void invoke_subflow_and_ignore_result (FlowInterface< Buffer< T > > *target_flow, Args &&... args)
 Calls a helper flow to perform some actions.
 
- Static Public Member Functions inherited from LinkedObject< StateFlowWithQueue >
static StateFlowWithQueuelink_head ()
 
static Atomichead_mu ()
 Locks the list for modification (at any entry!).
 
- Static Public Member Functions inherited from FlowInterface< MessageType >
static MessageType * cast_alloc (QMember *entry)
 Down casts and initializes an asynchronous allocation result to the appropriate flow's buffer type.
 
- Protected Types inherited from CallableFlow< BulkAliasRequest >
using Action = StateFlowBase::Action
 
- Protected Types inherited from UntypedStateFlow< QueueType >
typedef Action(StateFlowBase::* Callback) ()
 State Flow callback prototype.
 
- Protected Types inherited from StateFlowBase
typedef Action(StateFlowBase::* Callback) ()
 State Flow callback prototype.
 
- Protected Member Functions inherited from CallableFlow< BulkAliasRequest >
BulkAliasRequest * request ()
 
bool has_request ()
 
Action return_ok ()
 Terminates the flow and returns the request buffer to the caller with an error code of OK (zero).
 
Action wait_and_return_ok ()
 Waits to be notified before moving onto the next state for termination.
 
Action wait_done ()
 Terminates the flow and returns the request buffer to the caller with an error code of OK (zero).
 
Action return_with_error (int error)
 Terminates the flow and returns the request buffer to the caller with an specific error code.
 
- Protected Member Functions inherited from TypedStateFlow< MessageType, UntypedStateFlow< QueueType > >
void release () OVERRIDE
 Unrefs the current buffer.
 
void return_buffer ()
 For state flows that are operated using invoke_subflow_and_wait this is a way to hand back the buffer to the caller.
 
MessageType * message ()
 
MessageType * transfer_message ()
 Releases ownership of the current message.
 
- Protected Member Functions inherited from UntypedStateFlow< QueueType >
void send (BufferBase *msg, unsigned priority=UINT_MAX)
 Sends a message to the state flow for processing.
 
QMemberqueue_next (unsigned *priority) OVERRIDE
 Takes the front entry in the queue.
 
bool queue_empty () OVERRIDE
 
Action call_immediately (Callback c)
 Imediately call the next state upon return.
 
- Protected Member Functions inherited from StateFlowWithQueue
 StateFlowWithQueue (Service *service)
 Constructor.
 
Action exit ()
 Terminates the processing of this flow.
 
Action release_and_exit ()
 Terminates the processing of the current message.
 
BufferBasemessage ()
 
BufferBasetransfer_message ()
 Releases ownership of the current message.
 
void reset_message (BufferBase *message, unsigned priority)
 Sets the current message being processed.
 
unsigned priority ()
 
void set_priority (unsigned priority)
 Overrides the current priority.
 
void start_flow_at_init (Callback c)
 Call this from the constructor of the child class to do some work before the main queue processing loop begins.
 
- Protected Member Functions inherited from StateFlowBase
 StateFlowBase (Service *service)
 Constructor.
 
 ~StateFlowBase ()
 Destructor.
 
void reset_flow (Callback c)
 Resets the flow to the specified state.
 
bool is_state (Callback c)
 
bool is_terminated ()
 
void start_flow (Callback c)
 Resets the flow to the specified state and starts it.
 
Action again ()
 Call the current state again via call_immediately.
 
Action exit ()
 Terminate current StateFlow activity.
 
Action delete_this ()
 Terminates the flow and deletes *this.
 
Action set_terminated ()
 Sets the flow to terminated state.
 
Action call_immediately (Callback c)
 Imediately call the next state upon return.
 
Action wait ()
 Wait for an asynchronous call.
 
Action wait_and_call (Callback c)
 Wait for resource to become available before proceeding to next state.
 
template<class T >
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 allocate_and_call (Callback c, QAsync *queue)
 Allocates an entry from an asynchronous queue, and transitions to a state once the allocation is complete.
 
template<class T >
Buffer< T > * full_allocation_result (FlowInterface< Buffer< T > > *target_flow)
 Takes the result of the asynchronous allocation without resetting the object.
 
template<class T >
T * full_allocation_result (TypedQAsync< T > *queue)
 Takes the result of the asynchronous allocation without resetting the object.
 
template<class T >
void cast_allocation_result (T **member)
 Takes the result of the asynchronous allocation without resetting the object.
 
template<class T >
Buffer< T > * get_allocation_result (FlowInterface< Buffer< T > > *target_flow)
 Takes the result of the asynchronous allocation.
 
Action yield_and_call (Callback c)
 Place the current flow to the back of the executor, and transition to a new state after we get the CPU again.
 
Action yield ()
 Place the current flow to the back of the executor, and re-try the current state after we get the CPU again.
 
Action sleep_and_call (::Timer *timer, long long timeout_nsec, Callback c)
 Suspends execution of this control flow for a specified time.
 
template<class T , typename... Args>
Action invoke_subflow_and_wait (FlowInterface< Buffer< T > > *target_flow, Callback c, Args &&... args)
 Calls a helper flow to perform some actions.
 
Action read_repeated (StateFlowSelectHelper *helper, int fd, void *buf, size_t size, Callback c, unsigned priority=Selectable::MAX_PRIO)
 Blocks until size bytes are read and then invokes the next state.
 
Action read_single (StateFlowSelectHelper *helper, int fd, void *buf, size_t size, Callback c, unsigned priority=Selectable::MAX_PRIO)
 Attempts to read at most size_t bytes, and blocks the caller until at least one byte is read.
 
Action read_nonblocking (StateFlowSelectHelper *helper, int fd, void *buf, size_t size, Callback c, unsigned priority=Selectable::MAX_PRIO)
 Attempts to read at most size bytes, and then invokes the next state, even if only zero bytes are available right now.
 
Action read_repeated_with_timeout (StateFlowTimedSelectHelper *helper, long long timeout_nsec, int fd, void *buf, size_t size, Callback c, unsigned priority=Selectable::MAX_PRIO)
 Blocks until size bytes are read, or a timeout expires.
 
Action internal_try_read ()
 Implementation state that gets repeatedly called upon every wakeup and tries to make progress on reading.
 
Action write_repeated (StateFlowSelectHelper *helper, int fd, const void *buf, size_t size, Callback c, unsigned priority=Selectable::MAX_PRIO)
 Writes some data into a file descriptor, repeating the operation as necessary until all bytes are written.
 
Action internal_try_write ()
 Implementation state that gets repeatedly called upon every wakeup and tries to make progress on writing.
 
- Protected Member Functions inherited from QMember
 QMember ()
 Constructor.
 
 ~QMember ()
 Destructor.
 
- Protected Member Functions inherited from Atomic
void lock ()
 
void unlock ()
 
- Protected Member Functions inherited from LinkedObject< StateFlowWithQueue >
StateFlowWithQueuelink_this ()
 
 LinkedObject ()
 Constructor. Puts *this on the linked list.
 
 ~LinkedObject ()
 Constructor. Removes *this from the linked list.
 
StateFlowWithQueuelink_next ()
 
- Static Protected Member Functions inherited from LinkedObject< StateFlowWithQueue >
static StateFlowWithQueuelink_head ()
 
static Atomichead_mu ()
 Locks the list for modification (at any entry!).
 
- Protected Attributes inherited from QMember
QMembernext
 pointer to the next member in the queue
 
- Protected Attributes inherited from LinkedObject< StateFlowWithQueue >
StateFlowWithQueuelink_
 Linked list pointer.
 
- Static Protected Attributes inherited from LinkedObject< StateFlowWithQueue >
static StateFlowWithQueuehead_
 Beginning of the list.
 

Detailed Description

Implementation of the BulkAliasAllocatorInterface to allocate many aliases at the same time.

Definition at line 44 of file BulkAliasAllocator.cxx.

Constructor & Destructor Documentation

◆ BulkAliasAllocator()

openlcb::BulkAliasAllocator::BulkAliasAllocator ( IfCan iface)
inline

Constructor.

Parameters
ifacethe openlcb CAN interface

Definition at line 49 of file BulkAliasAllocator.cxx.

Member Function Documentation

◆ complete()

Action openlcb::BulkAliasAllocator::complete ( )
inline

Called when all RID frames are sent out.

Definition at line 156 of file BulkAliasAllocator.cxx.

◆ entry()

Action openlcb::BulkAliasAllocator::entry ( )
inlineoverridevirtual

Start of flow when a request arrives to allocate many aliases.

Resets the internal state and goes on to start the allocation process.

Implements TypedStateFlow< MessageType, UntypedStateFlow< QueueType > >.

Definition at line 56 of file BulkAliasAllocator.cxx.

◆ handle_conflict()

void openlcb::BulkAliasAllocator::handle_conflict ( Buffer< CanMessageData > *  message)
inlineprivate

Callback from the stack for all incoming frames while we are operating.

We sniff the alias uot of it and record any conflicts we see.

Parameters
messagean incoming CAN frame.

Definition at line 169 of file BulkAliasAllocator.cxx.

◆ if_can()

IfCan * openlcb::BulkAliasAllocator::if_can ( )
inlineprivate
Returns
the openlcb CAN interface

Definition at line 204 of file BulkAliasAllocator.cxx.

◆ relative_time()

unsigned openlcb::BulkAliasAllocator::relative_time ( )
inlineprivate
Returns
the time elapsed from start time in 10 msec units.

Definition at line 210 of file BulkAliasAllocator.cxx.

◆ send_can_frame()

void openlcb::BulkAliasAllocator::send_can_frame ( NodeAlias  src,
uint16_t  control_field,
int  sequence 
)
inlineprivate

Sends a CAN control frame to the bus.

Take a share of the barrier bn_ to send with the frame.

Parameters
srcsource alias to use on the frame.
control_field16-bit control value (e.g. RID_FRAME, or 0 top nibble and a chunk of the unique node ID in the middle).
sequenceused for CID messages.

Definition at line 195 of file BulkAliasAllocator.cxx.

◆ send_cid_frames()

Action openlcb::BulkAliasAllocator::send_cid_frames ( )
inline

Picks a bunch of random aliases, sends CID frames for them to the bus.

Definition at line 68 of file BulkAliasAllocator.cxx.

◆ stamp_time()

Action openlcb::BulkAliasAllocator::stamp_time ( )
inline

Adds the timestamps when the CID requests were sent out.

Definition at line 94 of file BulkAliasAllocator.cxx.

◆ wait_for_results()

Action openlcb::BulkAliasAllocator::wait_for_results ( )
inline

Sends out the RID frames for any alias that the 200 msec has already elapsed, then waits a bit and tries again.

Definition at line 109 of file BulkAliasAllocator.cxx.

Member Data Documentation

◆ ALLOCATE_DELAY

constexpr unsigned openlcb::BulkAliasAllocator::ALLOCATE_DELAY = 20
staticconstexprprivate

How many count to wait before sending out the RID frames.

One count is 10 msec (see { } ).

Definition at line 187 of file BulkAliasAllocator.cxx.

◆ bn_

BarrierNotifiable openlcb::BulkAliasAllocator::bn_
private

Helper object to determine when the CAN frames have flushed from the system.

Definition at line 264 of file BulkAliasAllocator.cxx.

◆ conflictHandler_

IncomingFrameHandler::GenericHandler openlcb::BulkAliasAllocator::conflictHandler_
private
Initial value:
{
void handle_conflict(Buffer< CanMessageData > *message)
Callback from the stack for all incoming frames while we are operating.

Listens to incoming CAN frames and handles alias conflicts.

Definition at line 182 of file BulkAliasAllocator.cxx.

◆ nextToClaim_

uint16_t openlcb::BulkAliasAllocator::nextToClaim_
private

Index into the pendingAliasesByTime_ vector where we need to send out the reserve frame.

Definition at line 277 of file BulkAliasAllocator.cxx.

◆ nextToStampTime_

uint16_t openlcb::BulkAliasAllocator::nextToStampTime_
private

Index into the pendingAliasesByTime_ vector where we need to stmap time.

Definition at line 274 of file BulkAliasAllocator.cxx.

◆ pendingAliasesByKey_

SortedListSet<AliasLookupInfo, LookupCompare> openlcb::BulkAliasAllocator::pendingAliasesByKey_
private

Stores the aliases we are trying to allocate in the alias order.

Definition at line 271 of file BulkAliasAllocator.cxx.

◆ pendingAliasesByTime_

std::vector<PendingAliasInfo> openlcb::BulkAliasAllocator::pendingAliasesByTime_
private

Stores the aliases we are trying to allocate in time order of picking them.

Definition at line 269 of file BulkAliasAllocator.cxx.

◆ startTime_

long long openlcb::BulkAliasAllocator::startTime_
private

We measure time elapsed relative to this point.

Definition at line 266 of file BulkAliasAllocator.cxx.

◆ timer_

StateFlowTimer openlcb::BulkAliasAllocator::timer_ {this}
private

Helper object for sleeping.

Definition at line 261 of file BulkAliasAllocator.cxx.


The documentation for this class was generated from the following file: