Skip to contentSkip to conversation

How to make main sleep ? clock_nanosleep ? rt_alarm_wait ?

Xenomai-help Lionel Perrin 2 participants 4 messages Jun 1, 2006

lore
Share & tools
lore
#0 lore raw

I got some difficulties to use either rt_alarm_wait or clock_nanosleep. It seems i haven't understood what's a "context that can sleep".

First of all, what do we need to use clock_nanosleep ? Is it impossible to use this function in the main thread ?

I tried this :

#include <stdio.h>
#include <time.h>
#include <errno.h>

#define NANOCOEF 0.000000001

double getfloattime(const struct timespec *tp){
return tp->tv_sec + tp->tv_nsec * NANOCOEF;
}

int setfloattime(struct timespec *tp, double val) {
tp->tv_sec = (time_t)val;
tp->tv_nsec = (long) ( ( val - (double)(tp->tv_sec) ) / NANOCOEF ); return 0;
}

int main(int argc, char *argv[])
{
struct timespec tp, rmtp;
int ret;

clock_gettime( CLOCK_REALTIME, &tp); printf("start time : %lf\n", getfloattime( &tp )); setfloattime(&tp, 1.0);
ret = nanosleep(&tp, &rmtp);

   switch (errno) {
       case 0:       printf("clock_nanosleep ok\n");
       break;
       case EPERM:   printf("the caller context is invalid\n");
       return errno;
       case ENOTSUP: printf("the specified clock is unsupported\n");
       return errno;
       case EINVAL:  printf("the specified wakeup time is invalid\n");
       return errno;
       case EINTR:   printf("this service was interrupted by a signal\n");
       return errno;

}
clock_gettime( CLOCK_REALTIME, &tp); printf("stop time : %lf\n", getfloattime( &tp )); return 0;
}

but nanosleep won't work :(

I also tried to put this nanosleep into a thread, to set thread scheduling policy to SCHED_FIFO, but that's not better... I solve my problem puting nanosleep in thread with policy set to 'SCHED_FIFO' and adding those lines in main:
mysched.sched_priority = sched_get_priority_max(SCHED_FIFO) - 1;
if( sched_setscheduler( 0, SCHED_FIFO, &mysched ) == -1 ) {
perror("sched_setscheduler");
}
but I don't know why this is correct...

Is it impossible to make the main thread sleep ?

I had the same problem with native API and rt_alarm_wait.

Any help would be usefull.
Thanks

#1 lore raw
Quoted 5 lines · Lionel Perrin

I got some difficulties to use either rt_alarm_wait or clock_nanosleep. It seems i haven't understood what's a "context that can sleep".

First of all, what do we need to use clock_nanosleep ? Is it impossible to use this function in the main thread ?

Xenomai clock_nanosleep, nanosleep or rt_alarm_wait services are reserved to Xenomai real-time threads, and will return an error of EPERM otherwise.

If you want a non-realtime sleep, you may still access standard services using __real_nanosleep or __real_clock_nanosleep.

If you want to obtain a Xenomai real-time thread using the posix skin you have to make it use the SCHED_FIFO policy, either by creating it with the pthread_create service, specifying non null creation attributes or by changing the scheduling policy of an existing thread with the pthread_setschedparam service.

If you want to obtain a Xenomai real-time thread using the native skin, you may create it with the rt_task_create service or turn an existing thread into a real-time thread with the rt_task_shadow service.

Quoted 4 lines

(...)

    ret = nanosleep(&tp, &rmtp);
    switch (errno) {

(...)

errno value is only meaningful if nanosleep returns -1. In particular it is not garanteed to be zero if nanosleep did not fail.

Signature · 1 line
					    Gilles Chanteperdrix.
#2 lore raw

Thanks for your answer. That's really great to have support so quickly. :)

Quoted 10 lines
Quoted 5 lines

I got some difficulties to use either rt_alarm_wait or clock_nanosleep. It seems i haven't understood what's a "context that can sleep".

First of all, what do we need to use clock_nanosleep ? Is it impossible to use this function in the main thread ?

Xenomai clock_nanosleep, nanosleep or rt_alarm_wait services are reserved to Xenomai real-time threads, and will return an error of EPERM otherwise.

Ok. So the thread "main" is not a Xenomai real-time, right ?

Quoted 6 lines

If you want to obtain a Xenomai real-time thread using the posix skin you have to make it use the SCHED_FIFO policy, either by creating it with the pthread_create service, specifying non null creation attributes or by changing the scheduling policy of an existing thread with the pthread_setschedparam service.

Is it possible to apply those methods to the thread "main" ?

Quoted 4 lines

If you want to obtain a Xenomai real-time thread using the native skin, you may create it with the rt_task_create service or turn an existing thread into a real-time thread with the rt_task_shadow service.

Quoted 8 lines
Quoted 4 lines

(...)

    ret = nanosleep(&tp, &rmtp);
    switch (errno) {

(...)

errno value is only meaningful if nanosleep returns -1. In particular it is not garanteed to be zero if nanosleep did not fail.

Mmm... Copy-Paste mistake.

There's still my sched_setscheduler enigma.
Why the following program need the commented line to run perfectly, and why nanosleep failed without those lines ?

#include <time.h>
#include <stdio.h>
#include <errno.h>
#include <pthread.h>

#define NANOCOEF 0.000000001

double getfloattime(const struct timespec *tp){
return tp->tv_sec + tp->tv_nsec * NANOCOEF;
}

int setfloattime(struct timespec *tp, double val) {
tp->tv_sec = (time_t)val;
tp->tv_nsec = (long) ( ( val - (double)(tp->tv_sec) ) / NANOCOEF ); return 0;
}

void * task(void * p)
{
struct timespec tp, rmtp;
int ret;

clock_gettime( CLOCK_REALTIME, &tp); printf("start time : %lf\n", getfloattime( &tp )); setfloattime(&tp, 1.0);
ret = nanosleep(&tp, &rmtp);
if(ret)

   {
       switch (errno) {
           case 0:       printf("clock_nanosleep ok\n");
           break;
           case EPERM:   printf("the caller context is invalid\n");
           return NULL;
           case ENOTSUP: printf("the specified clock is unsupported\n");
           return NULL;
           case EINVAL:  printf("the specified wakeup time is invalid\n");
           return NULL;
           case EINTR:   printf("this service was interrupted by a 
signal\n");
           return NULL;
       }
   }
   else
   {
       printf("nanosleep OK\n");

}
clock_gettime( CLOCK_REALTIME, &tp); printf("stop time : %lf\n", getfloattime( &tp )); return NULL;
}

int main(int argc, char *argv[])
{
pthread_t th;
pthread_attr_t th_attr;
struct sched_param mysched;
/*
mysched.sched_priority = sched_get_priority_max(SCHED_FIFO) - 1;

   if( sched_setscheduler( 0, SCHED_FIFO, &mysched ) == -1 ) {
       perror("sched_setscheduler");
}*/
   if(pthread_attr_init(&th_attr))
       perror("pthead_attr_init");
  
   if (pthread_attr_setschedpolicy(&th_attr, SCHED_FIFO))
       perror("pthread_attr_setschedpolicy");
  
   if (pthread_create(&th, &th_attr, task, NULL))
       perror("pthread_create");;
  
   if(pthread_join(th, NULL))
       perror("pthread_join");

return 0;
}

#3 lore raw
Quoted 13 lines · Lionel Perrin

Thanks for your answer. That's really great to have support so quickly. :)

Quoted 10 lines
Quoted 5 lines

I got some difficulties to use either rt_alarm_wait or clock_nanosleep. It seems i haven't understood what's a "context that can sleep".

First of all, what do we need to use clock_nanosleep ? Is it impossible to use this function in the main thread ?

Xenomai clock_nanosleep, nanosleep or rt_alarm_wait services are reserved to Xenomai real-time threads, and will return an error of EPERM otherwise.

Ok. So the thread "main" is not a Xenomai real-time, right ?

Quoted 7 lines
Quoted 6 lines

If you want to obtain a Xenomai real-time thread using the posix skin you have to make it use the SCHED_FIFO policy, either by creating it with the pthread_create service, specifying non null creation attributes or by changing the scheduling policy of an existing thread with the pthread_setschedparam service.

Is it possible to apply those methods to the thread "main" ?

You can not create the main thread with pthread_create, but you can make it real-time with pthread_setschedparam.

Quoted 3 lines

There's still my sched_setscheduler enigma.
Why the following program need the commented line to run perfectly, and why nanosleep failed without those lines ?

Maybe because the default value of the inheritsched attribute is not what you expect ? You can check if a thread uses a real-time scheduling policy with pthread_getschedparam.

Quoted 12 lines

(...)

    if(pthread_attr_init(&th_attr))
        perror("pthead_attr_init");
   
    if (pthread_attr_setschedpolicy(&th_attr, SCHED_FIFO))
        perror("pthread_attr_setschedpolicy");
   
    if (pthread_create(&th, &th_attr, task, NULL))
        perror("pthread_create");;
   
    if(pthread_join(th, NULL))
        perror("pthread_join");
Quoted 1 line

(...)

perror will not work with pthread_* services: they do not use errno, they return errors directly.

Signature · 1 line
					    Gilles Chanteperdrix.