All of lore.kernel.org
 help / color / mirror / Atom feed
* [Xenomai-help] Xenomai 2.0.1 - Posix Skin - implementing a periodic thread
@ 2005-12-13 17:57 Paolo Gai
  2005-12-13 18:22 ` Jan Kiszka
  2005-12-13 20:27 ` Gilles Chanteperdrix
  0 siblings, 2 replies; 3+ messages in thread
From: Paolo Gai @ 2005-12-13 17:57 UTC (permalink / raw)
  To: xenomai

[-- Attachment #1: Type: text/plain, Size: 645 bytes --]

Dear all,

here is another small demo... this time of a periodic thread in POSIX.

It works under Xenomai, but it fails under Posix Real-Time.

The reason of the failure is the SIGUSR1 not being blocked by the main 
thread. If in main() the comments below the note "// REMOVE THE COMMENTS 
BELOW" are removed, then the signal is masked also in the main(), and as 
a result the POSIX version works.

The question I have is if the behavior in Xenomai (that is, not failing 
like the POSIX counterpart) is correct or not...

bye

Paolo

-- 
Evidence S.R.L. - Paolo Gai, CEO
http://www.evidence.eu.com   Tel: +39 0587 274823   Fax: +39 0587 291904


[-- Attachment #2: ex_periodic.c --]
[-- Type: text/x-csrc, Size: 2214 bytes --]

#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <errno.h>
#include <signal.h>
#include <unistd.h>
#include <pthread.h>

void abort_perror(const char *msg)
{
  perror(msg);
  exit(1);
}


void *my_periodic_thread (void *cookie)
{
  struct itimerspec its;
  struct sigevent sig;
  sigset_t blocked;
  siginfo_t si;
  timer_t mytimer;
  int i;

  /* create a timer that will be used to periodically send a signal */
  sig.sigev_notify = SIGEV_SIGNAL;
  sig.sigev_signo = SIGUSR1;
  sig.sigev_value.sival_ptr = &sig;
  if(timer_create(CLOCK_REALTIME, &sig, &mytimer))
    abort_perror("timer_create");

  /* block the signal used by the timer */
  sigemptyset(&blocked);
  sigaddset(&blocked, SIGUSR1);
  pthread_sigmask(SIG_BLOCK, &blocked, NULL);

  /* set the periodic timer */
  its.it_value.tv_sec = 0;
  its.it_value.tv_nsec = 40000000; /* 40 ms */
  its.it_interval.tv_sec = 0;
  its.it_interval.tv_nsec = 20000000; /* 20 ms */;
  if(timer_settime(mytimer, 0, &its, NULL))
    abort_perror("timer_settime");

  printf("periodic thread started...\n");

  for (i=0; i<40; i++) {
    while (sigwaitinfo(&blocked, &si) == -1 && errno == EINTR);

    /* The thread body */
    printf("%d\n",i);
  }

  printf("...periodic thread end!\n");
  
  timer_delete(mytimer);
  return NULL;
}

int main()
{
  pthread_t mythread;
  pthread_attr_t myattr;
  struct sched_param myparam;
  void *returnvalue;
  sigset_t blocked;

  /* block the signal used by the timer */
  // REMOVE THE COMMENTS BELOW
  //  sigemptyset(&blocked);
  //  sigaddset(&blocked, SIGUSR1);
  //  pthread_sigmask(SIG_BLOCK, &blocked, NULL);

  /* initializes the thread attribute */
  pthread_attr_init(&myattr);
  pthread_attr_setinheritsched(&myattr, PTHREAD_EXPLICIT_SCHED);
  pthread_attr_setschedpolicy(&myattr, SCHED_FIFO);
  myparam.sched_priority = 3;
  pthread_attr_setschedparam(&myattr, &myparam);

  printf("starting a periodic thread...\n");

  if (pthread_create(&mythread, &myattr, my_periodic_thread, NULL))
    abort_perror("pthread_create");

  pthread_attr_destroy(&myattr);

  /* wait the end of the thread we just created */
  pthread_join(mythread, &returnvalue);

  printf("ending main...\n");

  return 0;
}


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Xenomai-help] Xenomai 2.0.1 - Posix Skin - implementing a periodic thread
  2005-12-13 17:57 [Xenomai-help] Xenomai 2.0.1 - Posix Skin - implementing a periodic thread Paolo Gai
@ 2005-12-13 18:22 ` Jan Kiszka
  2005-12-13 20:27 ` Gilles Chanteperdrix
  1 sibling, 0 replies; 3+ messages in thread
From: Jan Kiszka @ 2005-12-13 18:22 UTC (permalink / raw)
  To: Paolo Gai; +Cc: xenomai

[-- Attachment #1: Type: text/plain, Size: 1037 bytes --]

Paolo Gai wrote:
> Dear all,
> 
> here is another small demo... this time of a periodic thread in POSIX.
> 
> It works under Xenomai, but it fails under Posix Real-Time.
> 
> The reason of the failure is the SIGUSR1 not being blocked by the main
> thread. If in main() the comments below the note "// REMOVE THE COMMENTS
> BELOW" are removed, then the signal is masked also in the main(), and as
> a result the POSIX version works.
> 
> The question I have is if the behavior in Xenomai (that is, not failing
> like the POSIX counterpart) is correct or not...
> 

Without having read your program in details and thought about the signal
behaviour as well, just two general remarks:

1. Signals are not yet supported by Xenomai in primary mode. All you
receive are Linux signals so that the real-time thread is effectively
running only in secondary mode (also due to the printf).

2. Periodic threads can be set up more easily and efficiently under
POSIX by using clock_nanosleep with absolute timeouts.

Jan


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 250 bytes --]

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Xenomai-help] Xenomai 2.0.1 - Posix Skin - implementing a periodic thread
  2005-12-13 17:57 [Xenomai-help] Xenomai 2.0.1 - Posix Skin - implementing a periodic thread Paolo Gai
  2005-12-13 18:22 ` Jan Kiszka
@ 2005-12-13 20:27 ` Gilles Chanteperdrix
  1 sibling, 0 replies; 3+ messages in thread
From: Gilles Chanteperdrix @ 2005-12-13 20:27 UTC (permalink / raw)
  To: xenomai

Paolo Gai wrote:
 > Dear all,
 > 
 > here is another small demo... this time of a periodic thread in POSIX.
 > 
 > It works under Xenomai, but it fails under Posix Real-Time.
 > 
 > The reason of the failure is the SIGUSR1 not being blocked by the main 
 > thread. If in main() the comments below the note "// REMOVE THE COMMENTS 
 > BELOW" are removed, then the signal is masked also in the main(), and as 
 > a result the POSIX version works.
 > 
 > The question I have is if the behavior in Xenomai (that is, not failing 
 > like the POSIX counterpart) is correct or not...

The answer is in section 2.4.2 of the standard, delivery of real-time
signals.

http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html#tag_02_04_02

real-time signals are marked pending to the process, not a particular
thread. So, Linux implementation is correct, Xenomai is incorrect.

When creating a timer with timer_create, Xenomai assume that signals
will be delivered to the thread which created the thread.

Linux supports an extension which allows timer notifications to be
delivered to a particular thread; set the sigev_notify member of the
sigevent structure to 
SIGEV_SIGNAL | SIGEV_THREAD_ID
and set the member _sigev_un._tid to the thread ID of the target thread.

-- 


					    Gilles Chanteperdrix.


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2005-12-13 20:27 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-12-13 17:57 [Xenomai-help] Xenomai 2.0.1 - Posix Skin - implementing a periodic thread Paolo Gai
2005-12-13 18:22 ` Jan Kiszka
2005-12-13 20:27 ` Gilles Chanteperdrix

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.