I am writing a timer-based scheduler to accomplish the task of informing a certain process of what to do and when.
The Idea:
Inform an X process
of certain requests to perform at timed-intervals. So if the X process
needs to perform an action every Y seconds, the scheduler should send the desired request to the X process
to perform the action every Y seconds. There could be multiple requests of different time intervals to be sent out by the scheduler, and obviously equal amount of actions to be done by X process
.
In addition, it's priority-based: if multiple requests have the same timeout expiry, the request with a highest priority is sent out first.
My thoughts:
- Create a posix periodic timer
(timer_create())
and set them up(timer_settime())
with the specified expiry time for each request - Create a priority queue, into which a request is written within the timer callback (triggers every expiry time).
- Have a main thread that probably reads from a priority queue every certain number of seconds, and writes back the read value to a message queue which wakes up the
X process
. Now,X process
knows to service the request however it wants (not our concern).
Concerns:
- If there are multiple requests with the same timeout, how do we ensure the requests are sent in the order of priorities?
My idea is: In the main thread, block it on priority queue and as soon as it's populated from the timer callback, signal the main thread to wake up (via semaphore or conditional variable perhaps) and wait for 1 second before reading from a priority queue. 1 second assuming is long enough to ensure requests of the same priority are already written to a priority queue and the main thread first reads the request of the highest priority, which is then sent out to a message queue.
Could this be a feasible solution?
A scenario:
TimerA
and TimerB
expire at the same time, but TimerA
has a higher priority. The posix timer could invoke the callback for either TimerA
or TimerB
first. The idea is to ensure the request from TimerA
is sent out first followed by that from TimerB
Edit:
I made this basic flowchart to help visualize.