I'm testing POSIX skin in Xenomai. I'm trying to read and write from some GPIOs on a Raspberry Pi, and when I execute the program, there is an increasing number of context switching (in /proc/xenomai/stat/).
The main of the program maps the GPIOs to memory and starts the pthreads. The pthread that makes trouble is this:
void *productive_thread(void *arg)
{
struct timespec delay, sleep;
unsigned long over;
delay.tv_sec = 0;
delay.tv_nsec = 10000; // 10 usec
sleep.tv_sec = 0;
sleep.tv_nsec = *(long *)arg;
while(1)
{
// This are the read and write macros (gpio is the address of the GPIO mapping):
// #define GPIO_SET *(gpio+7)
// #define GPIO_CLR *(gpio+10)
// #define GPIO_READ(g) (*(gpio + 13)&(1<<(g)))>>4
while(GPIO_READ(4) != 1);
GPIO_SET = 1 << 17;
clock_nanosleep(CLOCK_REALTIME, 0, &delay, NULL);
GPIO_CLR = 1 << 17;
clock_nanosleep(CLOCK_REALTIME, 0, &sleep, NULL);
}
return NULL;
}
The number of context switching increases by every loop. I suspect the problem is clock_nanosleep, because all other operations are arithmetic, but clock_nanosleep is defined in Xenomai documentation. Can it be improved somehow (using POSIX skin)?