zhiyong
(Zhiyong Li)
January 20, 2022, 6:51am
1
Hi,
Assume a function is called by EventQueue every second, the function can typically finish execution in less than 100 milliseconds, but occasionally the execution can take up to several seconds due IO congestion or other factors. Can EventQueue automatically handle such scenario?
Or user has to ensure there is no overlap between function calls or cancel previous calls while function is being executed and then call it again once execution is completed ?
Please advise. Thanks.
1 Like
ladislas
(Ladislas de Toldi)
January 20, 2022, 9:08am
2
That’s a good question! I’m curious about the answer
This may answer your question:
#include "events/EventQueue.h"
#include "mbed_shared_queues.h"
#include "mbed_thread.h"
events::EventQueue *queue = mbed::mbed_event_queue();
uint8_t counter = 0;
void print_fc_slow_in_second_call() {
printf("%s - time: %lu\r\n", __FUNCTION__, time(NULL));
counter++;
if (counter == 2) {
thread_sleep_for(30*1000); // 30 seconds
}
}
void print_fc() {
printf("%s - time: %lu \r\n", __FUNCTION__, time(NULL));
}
int main(void)
{
printf("Starting: %lu\r\n", time(NULL));
queue->call_every(1*1000, mbed::callback(print_fc_slow_in_second_call));
queue->call_every(1*1000, mbed::callback(print_fc));
queue->dispatch_forever();
return 0;
}
Output:
Starting: 0
print_fc_slow_in_second_call - time: 1
print_fc - time: 1
print_fc_slow_in_second_call - time: 2
print_fc - time: 32
print_fc_slow_in_second_call - time: 32
print_fc - time: 32
print_fc_slow_in_second_call - time: 33
print_fc - time: 33
print_fc_slow_in_second_call - time: 34
print_fc - time: 34
zhiyong
(Zhiyong Li)
January 20, 2022, 3:00pm
4
@davidAlonso This is actually a good idea, just test it out. My interpretation is that Overlapping function calls are silently discarded. Thanks.
hudakz
(Zoltan Hudak)
January 20, 2022, 6:46pm
5
I don’t think there are overlapping function calls when an EventQueue is starving. Hence, there is nothing to be discarded.
The following link may help
https://os.mbed.com/questions/81400/How-to-tell-if-an-EventQueue-is-starved/
zhiyong
(Zhiyong Li)
January 20, 2022, 8:32pm
6
But how do you explain the 31 seconds gap between the first two prinf_fc lines? The execution of prinf_fc is just being postponed?
print_fc - time: 1 print_fc_slow_in_second_call - time: 2 print_fc - time: 32
From the 1 second gap between 2 ~ 4th call of print_fc_slow_in_second_call I get they are not discarded.
I think it is just delayed until the queue is free, but I don’t really know if it is delayed or discarded.
New example:
#include "events/EventQueue.h"
#include "mbed_shared_queues.h"
#include "mbed_thread.h"
events::EventQueue *queue = mbed::mbed_event_queue();
void print_fc_slow_in_second_call(uint32_t *counter) {
printf("%s - time: %lu\r\n", __FUNCTION__, time(NULL));
printf("%s - counter: %lu\r\n", __FUNCTION__, (*counter));
(*counter) = (*counter) + 1;
if ((*counter) == 2) {
thread_sleep_for(30*1000); // 30 seconds
}
}
void print_fc(uint32_t *counter) {
printf("%s - time: %lu\r\n", __FUNCTION__, time(NULL));
printf("%s - counter: %lu\r\n", __FUNCTION__, (*counter));
(*counter) = (*counter) + 1;
}
int main(void)
{
uint32_t print_fc_slow_in_second_call_cnt = 0;
uint32_t print_fc_cnt = 0;
printf("Starting: %lu\r\n", time(NULL));
queue->call_every(1*1000, mbed::callback(print_fc_slow_in_second_call, &print_fc_slow_in_second_call_cnt));
queue->call_every(1*1000, mbed::callback(print_fc, &print_fc_cnt));
queue->dispatch_forever();
return 0;
}
Output:
Starting: 0
print_fc_slow_in_second_call - time: 1
print_fc_slow_in_second_call - counter: 0
print_fc - time: 1
print_fc - counter: 0
print_fc_slow_in_second_call - time: 2
print_fc_slow_in_second_call - counter: 1
print_fc - time: 32
print_fc - counter: 1
print_fc_slow_in_second_call - time: 32
print_fc_slow_in_second_call - counter: 2
print_fc - time: 32
print_fc - counter: 2
print_fc_slow_in_second_call - time: 33
print_fc_slow_in_second_call - counter: 3
print_fc - time: 33
print_fc - counter: 3
print_fc_slow_in_second_call - time: 34
print_fc_slow_in_second_call - counter: 4
print_fc - time: 34
print_fc - counter: 4
1 Like
zhiyong
(Zhiyong Li)
January 21, 2022, 4:43pm
9
I think if we run this test code long enough, at some point either EventQueue gets full and no new event can be added or system may just crash.