Event Loop

gerhardkreuzer wrote on Tuesday, July 10, 2012:

I have to inform some tasks about significant changes very async, like somebody has entered some command using a console task.
So I want to have a task (high priority) which takes up some thing lets call event and than check out all tasks which have subscribed to this event during initialization.
Now the tasks loops throogh all the callBacks and execute them.
Works fine, as long tzhere is no need to pass parameters, even if this parameters were of different datatypes, like int and string.
Has anybody some idea or is something like this almost around?

Thanks for helping.

With best regards


richard_damon wrote on Wednesday, July 11, 2012:

I am a bit confused on which of two structures you are using.

One method is to have the high priority task accept the notification of the event, and then walk a list of subscribers queues and relay that message to all those event queues. Then the various tasks take the events off their lists and process the notifications.  This does require that all the tasks involved use similarly formatted messages. In this case, you just need to make sure that your message format has room for the various options/parameters, perhaps in a union in the event structure. Alternatively, it might have a pointer to a structure which holds the various parameters.

A second method is that the various tasks register a callback function that is called in the context of the notifying task, and that callback function does what is needed to notify the task of the event. All of these functions for a given notification list should have the same signature. The notification task will need to pass all the information that any task might need to this function. If you really need to have differing signatures, then in addition to saving the address of the function to call, you need to also save information about what parameter list this function needs. This can either be a number that you put into a switch statement, or a pointer to a second function that is what the notification task actually calls, and in addition to the event info, it is also passed the real callback function, and the converter function extracts the information, and makes the needed call.

Note that in C, it is allowed to cast a void (*)(int) to void(*)() and then back to void (*)(int) again, so the registration table can use a generic function signature, and then the conversion function converts it back to the original signature to make the call.