I have a timer app that fires off every expiry time, and are arranged in the order of their expiry time and priority (similar to how FreeRTOS implements I think apart from the priority part).
So if the following indicates the timers along with their expiry and priorities, the list would look like:
T2 -> T1 -> T3
Nodes | Expiry Times | Priorities |
---|---|---|
T1 | 5 | 1 |
T2 | 5 | 10 |
T3 | 10 | 1 |
In my app, once the node expires, a relevant message stored in the Node
struct is sent over to the client, in response to which it shall receive an ACK within X seconds, and if it’s not received, the same node is added to the front of the list so it’s retransmitted next.
have the timer seems to be running fine but needed some design suggestions on integrating the ACK logic.
Sample pseudocode looks something like this:
static Queue *queue; // gets populated in a different function in the same file
void callback(const char *timerMsg)
{
// writes msg to a mqueue to be read by the client
// in response to which an ACK should be sent...
}
void *TimerThread(void *arg)
{
Queue *head = queue->head;
while(1)
{
long now = getCurrentTime();
long timeElapsed = now - start;
char *msg;
if (timeElapsed >= head-> refExpiryTime) // if node expired
{
msg = head->msg;
callback(msg);
PopAndPush(head); // delete the current head from the list and append with an updated time
}
}
}
// thread that handles messages received from the client
void *ReadThread(void *arg)
{
uint8_t buffer[100];
while(1)
{
// block the thread to wait on a message queue to get populated from a client that may contain an ACK message...
// have a state machine
switch(buffer[0])
{
case ACK_MSG:
// write to AckQueue?
break;
}
}
}
- Would it make sense to:
- have two queues:
SentQueue
andAckQueue
where the former stores the nodes that expired and the latter the ACK messages coming toReadThread()
- fire off a separate timer for every node that expired to keep track of whether an ACK was received within the X timeout. Once the timeout happens, scan through
AckQueue
and see whether an ACK for this particular node is there or not. If not, append to the front of the queue.
- have two queues:
Also I’m using Linux and not FreeRTOS but i’d appreciate hearing some thoughts