-
Notifications
You must be signed in to change notification settings - Fork 3
Description
While trying to understand the callback mechanism, I noticed that there is a shared static state between multiple callbacks if they use the callback_value.h template and have the same type. I'm not sure how problematic this is.
Here is the static variable is_buffered_:
bricklib2/utility/callback_value.h
Line 277 in 9e2f400
| static bool is_buffered_ = false; |
For example the voltage current bricklet is using this function template 3 times for different callbacks, but as it is 3 times the same type they are using the exact same function and therefore the same static variable.
https://github.com/Tinkerforge/voltage-current-v2-bricklet/blob/5014a1eeda48e745d6122213a45b560e59aec1ed/software/src/communication.c#L106-L116
So if i.e. the voltage callback fills the buffer, but cannot send the message, the buffered voltage message may is send during calling the power callback handler. As the message content is also buffered in a static variable at least no wrong message is sent, but the wrong callback handler returns true, which seems to be used for sorting the callback functions here:
bricklib2/utility/communication_callback.c
Lines 39 to 49 in 9e2f400
| for(uint32_t _ = 0; _ < COMMUNICATION_CALLBACK_HANDLER_NUM; _++) { | |
| if(communication_callbacks[cb_index]()) { | |
| communication_callback_handler_t current = communication_callbacks[cb_index]; | |
| for(uint32_t i = cb_index; i < COMMUNICATION_CALLBACK_HANDLER_NUM-1; i++) { | |
| communication_callbacks[i] = communication_callbacks[i+1]; | |
| } | |
| communication_callbacks[COMMUNICATION_CALLBACK_HANDLER_NUM-1] = current; | |
| } else { | |
| cb_index++; | |
| } | |
| } |