Issue with the timer

I have initialized a timer with its expiry handler in Component_init. It was working properly. but then I added a while(1) loop after starting the timer in component_init.
Now, my timer is never going in the timer expiry handler. can you explain this behaviour?

how about using timer with threads like below?

#include “legato.h”

static int set_up = 0;
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t condition = PTHREAD_COND_INITIALIZER;

static void *thread(void *aArgument)
{
pthread_mutex_init(&mutex, NULL);
pthread_cond_init(&condition, NULL);
set_up = 1;

int total_cycles = 10;

while (total_cycles--)
{
pthread_mutex_lock(&mutex);
pthread_cond_wait(&condition, &mutex);
pthread_mutex_unlock(&mutex);

printf("execute %i\n", total_cycles);
}

set_up = 0;
pthread_mutex_destroy(&mutex);
pthread_cond_destroy(&condition);

return NULL;

}

void alarm_handler(int sSignal)
{ if (set_up) pthread_cond_broadcast(&condition); }

COMPONENT_INIT
{
LE_INFO(“Hello, world.”);

pthread_t timed_thread = (pthread_t) NULL;

pthread_create(&timed_thread, NULL, &thread, NULL);

signal(SIGALRM, &alarm_handler);

struct itimerval timer_setup = { .it_interval = { 1, 500 * 1000 },
                                    .it_value = { 2, 0 } };

setitimer(ITIMER_REAL, &timer_setup, NULL);

//pthread_join(timed_thread, NULL);

while (1)
{
LE_INFO(“Hello, world.2”);
sleep(2);

}

}

Hi jyijyi,

Thanks for the work around solution.

After working on it, a little bit more. I understood that timer won’t go to the expiry handler till entire component_init is executed.

case 1:
So basically if timer is started in component_init with 2 sec of expiry count and if we put sleep(10) just after that, expiry handler will be called after 10 secs.

COMPONENT_INIT
{
LE_INFO(" component started");

block_read_timer = InitializationMngSetAndStartTimer(2,NULL); 

LE_INFO("---------------------------------Starting the thread--------------------------------");
sleep(10); 

}

case 2:
Also, I found the work around by creating a thread for the execution of sleep(10), expiry handler was called correctly after 2 secs.

COMPONENT_INIT
{

LE_INFO("InitializationMng component started");
le_thread_Ref_t init_thread;

init_thread = le_thread_Create("MngInit",MngInit,&block_read_timer);
block_read_timer = InitializationMngSetAndStartTimer(2,NULL); 

LE_INFO("---------------------------------Starting the thread--------------------------------");
/* sleep(10); is added in the thread now */
le_thread_Start(init_thread);

}

As per explained in the case 1, is this the expected behavior of component_init? If yes, can you explain it a little bit?

regards,
Kamlesh

I found this is not only related to COMPONENT_INIT().
I tried to run the following code , but timer is no longer called every 3 seconds, but it is called every 5 seconds.


#include “legato.h”

#define TIMER_INTERVAL_SEC 3
#define TIMER_INTERVAL_uSEC 100000

le_timer_Ref_t adxlPollingTimer;
static void tmrHandler(le_timer_Ref_t timerRef) {
LE_INFO(“INSIDE TIMER”);

sleep(5);

}

COMPONENT_INIT
{
LE_INFO("Hello, world. ");

le_clk_Time_t clk = { .sec = TIMER_INTERVAL_SEC, .usec = TIMER_INTERVAL_uSEC };
	 adxlPollingTimer = le_timer_Create("ADXL_TIMER");
	le_timer_SetRepeat(adxlPollingTimer, 500);
	le_timer_SetInterval(adxlPollingTimer, clk);
	le_timer_SetHandler(adxlPollingTimer, tmrHandler);
	le_timer_Start(adxlPollingTimer);

}


I guess it is related to the description in here:

“The call to the timer expiry handler may not occur immediately after the timer expires, depending on which other functions are called from the event loop. The amount of delay is entirely dependent on other work done in the event loop. For a repeating timer, if this delay is longer than the timer period, one or more timer expiries may be dropped. To reduce the likelihood of dropped expiries, the combined execution time of all handlers called from the event loop should ideally be less than the timer period.”

Hi jyijyi,

I guess that’s the expected behavior of timer and can be explained in detail with this.

“The reason for that is that while a signal is being inside its handler, it is blocked. Blocked signals are set to pending, but not queued. The term “pending” means that the operating system remembers that there is a signal waiting to be delivered at the next opportunity, and “not queued” means that it does this by setting a flag somewhere, but not by keeping an exact record of how many signals have arrived.”

That explains dropping as well as the delaying part.

But with the case 1 (that i have stated in my previous reply), it is different. It is not in the timer expiry handler yet and still it is getting delayed. For which I haven’t found the satisfactory explanation yet.

As we already have the work around solution, it’s not a dire problem anymore. I wanted to know just because I’m curious.

Regards,
Kamlesh

Well, I guess this part explains this behavior of component_init from the link,

“Every component implements a special initialization function (COMPONENT_INIT in C). That function does whatever initialization is required for that component (e.g., initializes data structures, registers event handlers, starts threads, etc.) and returns. The process’s main thread (auto-generated by the Build Tools) runs all the component initializers for all components in the executable and then enters the Legato event loop. The event loop then reacts to events by and calling registered handlers.