# How to create timer inside a thread

**URL:** <https://forum.legato.io/t/how-to-create-timer-inside-a-thread/104>\
**Category:** Legato Application Framework\
**Created:** [February 25, 2015, 5:13pm UTC](https://forum.legato.io/t/how-to-create-timer-inside-a-thread/104 "2015-02-25T17:13:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dfrancois](https://avatars.discourse-cdn.com/v4/letter/d/9e8a1a/32.png) [@dfrancois](https://forum.legato.io/u/dfrancois)\
**Post date:** [February 25, 2015, 5:13pm UTC](https://forum.legato.io/t/how-to-create-timer-inside-a-thread/104/1 "2015-02-25T17:13:59Z")

</div>

Hi,

I have a thread that poll events on a GPIO.  
Depending the value of the GPIO I want to start or stop a timer.

Unfortunately my timer is never started / stopped even if the le\_timer\_Start/Stop returns 0.

Here is the code of my thread:

```auto
void* adxl_intHandler(void* context) {
	unsigned char value;
	struct pollfd fdset[1];
	int nfds = 1;
	int gpio_fd, timeout, rc;
	unsigned int gpio = 9;

	le_clk_Time_t clk = { .sec = 0, .usec = 100000 };
	le_timer_Ref_t adxlPollingTimer = le_timer_Create("ADXL_TIMER");
	le_timer_SetRepeat(adxlPollingTimer, 0);
	le_timer_SetInterval(adxlPollingTimer, clk);
	le_timer_SetHandler(adxlPollingTimer, tmrHandler);

	gpio_export(gpio);
	gpio_set_dir(gpio, GPIO_IN);
	gpio_set_edge(gpio, GPIO_EDGE_RISING);
	gpio_fd = gpio_fd_open(gpio);

	memset((void*) fdset, 0, sizeof(fdset));

	fdset[0].fd = gpio_fd;
	fdset[0].events = POLLPRI;
	timeout = 100;

	while (1) {
		rc = poll(fdset, nfds, timeout);
		if (rc < 0) {
			printf("\npoll() failed!\n");
		}
		if (fdset[0].revents & POLLPRI) {
			char buf[64];
			int res =-1 ;
			read(fdset[0].fd, buf, 64);
			adxl_read_register(0x30, &value);
			if (value & 0x08) {
				printf("**stop detected\n");
				res = le_timer_Stop(adxlPollingTimer);
			}
			else if (value & 0x10) {
				printf("**start detected\n");
				res = le_timer_Start(adxlPollingTimer);
			}
			printf("timer start/stop status: %d\n", res);
		}
	}
	return NULL;
}
```

and the (very complex ) code of my timer’s handler:

```auto
static void tmrHandler(le_timer_Ref_t timerRef) {
	printf("INSIDE TIMER\n");
}
```

And here the result of the execution of my app:

> [@](#):
>
> root@swi-mdm9x15:~# \*\*start detected  
> timer start/stop status: 0  
> \*\*stop detected  
> timer start/stop status: 0

As you can see, the “INSIDE TIMER” log is never printed

Note: I deliberately use printf instead of LE\_DEBUG for this kind of debug in order to not miss any logs.

Thanks for your help

David

---

<div class="post-metadata">

**Author:** ![dfrancois](https://avatars.discourse-cdn.com/v4/letter/d/9e8a1a/32.png) [@dfrancois](https://forum.legato.io/u/dfrancois)\
**Post date:** [February 26, 2015, 9:00am UTC](https://forum.legato.io/t/how-to-create-timer-inside-a-thread/104/2 "2015-02-26T09:00:02Z")

</div>

Here is legato traces with timers traces enables

[quote]  
08:58:22 timers | AccelerometerEXE[22709]/framework T=ADXL\_INT\_POLLER | timer.c le\_timer\_Start() 730 | Starting timer ‘ADXL\_TIMER’  
08:58:22 DBUG | AccelerometerEXE[22709]/framework T=ADXL\_INT\_POLLER | timer.c le\_timer\_Start() 742 | threadRecPtr-\>timerFD=-1  
08:58:22 DBUG | AccelerometerEXE[22709]/framework T=ADXL\_INT\_POLLER | timer.c le\_timer\_Start() 750 | threadRecPtr-\>timerFD=12  
08:58:22 timers | AccelerometerEXE[22709]/framework T=ADXL\_INT\_POLLER | timer.c RestartTimerFD() 309 | timer ‘ADXL\_TIMER’ started  
08:58:27 timers | AccelerometerEXE[22709]/framework T=ADXL\_INT\_POLLER | timer.c le\_timer\_Stop() 821 | Stopping the first active timer  
08:58:27 timers | AccelerometerEXE[22709]/framework T=ADXL\_INT\_POLLER | timer.c le\_timer\_Stop() 842 | timerFD=12 stopped[/quote]

---

<div class="post-metadata">

**Author:** ![jchitty](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.legato.io/jchitty/32/72_2.png) [@jchitty](https://forum.legato.io/u/jchitty)\
**Post date:** [February 27, 2015, 2:40am UTC](https://forum.legato.io/t/how-to-create-timer-inside-a-thread/104/3 "2015-02-27T02:40:39Z")

</div>

Hi, David,

The Legato Timer API is integrated with the Legato Event Loop. Your thread is not running the Legato Event Loop, so the timer expiry never gets handled.

You can fix this in either of two ways:  
[ul]  
[li] Instead of using your own loop, with poll(), use the Legato event loop by calling le\_event\_RunLoop() in your thread.[/li]  
[li] Use le\_event\_GetFd() to fetch an event loop file descriptor that will become “ready to read” (POLLIN) when there’s something that the Legato event loop needs to process, and then call le\_event\_ServiceLoop() when the event loop fd becomes ready to read.[/li][/ul]

Cheers,

–Jen
