clock_nanosleep: cyclic POSIX thread freezes during time setting

From: Mehlan, Markus
Date: Mon Oct 05 2009 - 09:31:31 EST


Hi All,

I have the following problem on a MPC5200b customer board,
tested with linux-2.6.24.7-rt11 and 2.6.26.8-rt16.

I start the test application. Every 200ms I can see a new
line on the console ("ping"). Then I set the time with "date
100509152009" a few times on
a second console. The effect is that the output on the first console
freezes and if I check
the time with "date", the time doesn't progress during the "freeze
time". After the
"freeze time" the time makes a jump of several seconds.

In the real application the "ping" should reset the watchdog-timer. The
freezing causes
an elapsing of the watchdog-timer and finally a reboot.

Has someone an idea?

With friendly regards,
Markus Mehlan

---------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <time.h>
#include <sys/time.h>
#include <pthread.h>
#include <sys/mman.h>

#define CTRL_TASK_PRIORITY 5
#define USEC_PER_SEC 1000000
#define NSEC_PER_SEC 1000000000


static void tsnorm(struct timespec *ts)
{
while (ts->tv_nsec >= NSEC_PER_SEC)
{
ts->tv_nsec -= NSEC_PER_SEC;
ts->tv_sec++;
}
}

void *PingTask()
{
static unsigned long ulHwWdgTime = 0;
struct itimerspec timer;
int i;
unsigned long ulInterval = 1000; // = 1ms

if(clock_gettime(CLOCK_MONOTONIC, &timer.it_value) < 0)
{
perror("TimerTask: clock_gettime() failed\n");
//return -1;
}

timer.it_interval.tv_nsec = ulInterval % USEC_PER_SEC;
timer.it_interval.tv_nsec *= 1000;
timer.it_interval.tv_sec = ulInterval / USEC_PER_SEC;

while(1) {
// 1ms-Task
timer.it_value.tv_sec += timer.it_interval.tv_sec;
timer.it_value.tv_nsec += timer.it_interval.tv_nsec;
tsnorm(&timer.it_value);

if (ulHwWdgTime == 0)
{
ulHwWdgTime = 200; // 200ms
clock_gettime(CLOCK_MONOTONIC,
&timer2.it_value);
printf("ping\n");
}
ulHwWdgTime--;

clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME,
&timer.it_value, NULL);
}
}

int main(int argc, char **argv)
{
pthread_attr_t attr;
pthread_t threadHandle;
void *thread_result;
struct sched_param sched_param;
int res;
unsigned char byPriority = CTRL_TASK_PRIORITY;

pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);

if (pthread_create(&threadHandle, &attr, PingTask, NULL) != 0)
{
printf("Fehler pthread_create\n");
if (fd_drv_wdg)
close(fd_drv_wdg);
return -3;
}

sched_param.sched_priority =
max(0,sched_get_priority_max(SCHED_FIFO) - (int)byPriority);
if ((res=pthread_setschedparam
((pthread_t)threadHandle,SCHED_FIFO,&sched_param)))
{
printf("SysTaskSetPriority->pthread_setschedparam()
failed:%s\n",strerror(res));
if (fd_drv_wdg)
close(fd_drv_wdg);
return -4;
}
pthread_attr_destroy(&attr);

printf("Ende\n");
getchar();

if (fd_drv_wdg)
close(fd_drv_wdg);
return 0;
}

Sitz der Gesellschaft: Oberhausen
Handelsregister Amtsgericht Duisburg HRB-Nr. 17168 UST ID-Nr. DE 814009849
Geschaftsfuehrer: Manfred A. Wagner und Dr. Uwe Baader

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/