title: Periodic thread fails real-time in Xenomai
body: <p>I'm creating a periodic thread which outputs a square signal on an analogic output. I'm using Posix Skin and Analogy from the Xenomai API.</p>
  
  <p>I tested the real-time performance of my code using an oscilloscope and looking at the latency on the square signal (whose frequency is 1kHz). I am supposed to achieve &lt;100us latency. However, the signal is strongly (>250us latency) perturbated by common interruption signals, like moving the mouse, starting a new program, etc.</p>
  
  <p>The flags in my makefile are set up as such:</p>
  
  <pre><code>gcc  -I/usr/xenomai/include -D_GNU_SOURCE -D_REENTRANT -D__XENO__ -I/usr/xenomai/include/posix    
  main_posix.c -Xlinker -rpath -Xlinker /usr/xenomai/lib -Wl,@/usr/xenomai/lib/posix.wrappers 
  -L/usr/xenomai/lib -lpthread_rt -lxenomai -lpthread -lrt  -lanalogy -lrtdm -o main_posix
  </code></pre>
  
  <p>and this is the code:</p>
  
  <pre><code>#define PERIOD 1e6 
  #define FILENAME "analogy0"
  #define ANALOG_SUBD 1
  #define CHANNEL 0
  #define SCAN_SIZE 2
  #define DELAI 5
  
  static char *filename = FILENAME;
  static int idx_subd = ANALOG_SUBD;
  static int idx_chan = CHANNEL;
  static int valueUp = 450000;
  static int valueDown = 98500;
  
  void *TaskCode(void *arg)
  {
     unsigned char sgnl = 0;
     unsigned long overruns_r = 0;
  
     a4l_desc_t dsc = { .sbdata = NULL };
     a4l_chinfo_t *chinfo;
     int err = 0;
     unsigned int scan_size = SCAN_SIZE;
  
     err = a4l_open(&amp;dsc, filename);
     if (err &lt; 0) {
          fprintf(stderr,
              "insn_write: a4l_open %s failed (err=%d)\n",
              filename, err);
          return NULL;
     }    
  
     while(1) {
          pthread_wait_np( &amp;overruns_r );
      if(sgnl)
          err = a4l_sync_write(&amp;dsc,
                   idx_subd, CHAN(idx_chan), 0, &amp;valueUp, scan_size);
      else
          err = a4l_sync_write(&amp;dsc,
                   idx_subd, CHAN(idx_chan), 0, &amp;valueDown, scan_size);
      if (err &lt; 0) {
          fprintf(stderr,
              "insn_write: a4l_sync_write failed (err=%d)\n", err);
          goto out_insn_write;
      }
  
      sgnl = (sgnl + 1) % 2;
     }
  
     out_insn_write:
  
      if (dsc.sbdata != NULL)
          free(dsc.sbdata);
  
      a4l_close(&amp;dsc);
  
      return NULL;
  
  }
  
  int main(void)
  {
     mlockall( MCL_CURRENT | MCL_FUTURE );
  
     pthread_t thread;
     int rc, i;
     int prio = 99;
     struct timespec rqtp, rmtp;
     rqtp.tv_sec = 0;
     rqtp.tv_nsec = PERIOD;
  
     struct sched_param sparam;
     sparam.sched_priority = 99;
  
     rc = pthread_create(&amp;thread, NULL, TaskCode, NULL);
     assert(0 == rc);
  
     rc = pthread_setschedparam(&amp;thread, SCHED_FIFO, &amp;sparam);
     assert(0 == rc);
  
     rc = clock_gettime( CLOCK_REALTIME, &amp;rmtp );
     assert(0 == rc);
     rmtp.tv_sec = rmtp.tv_sec + DELAI;   
  
     rc = pthread_make_periodic_np(thread, &amp;rmtp, &amp;rqtp);
     if(rc == ETIMEDOUT) printf("Début dépassé \n");
     else if(rc == ESRCH) printf("Thread invalide \n");
     assert(0 == rc);
  
     rc = pthread_join(thread, NULL);
  
     exit(EXIT_SUCCESS);
  }
  </code></pre>
  
  <p>I am strongly suspecting (by looking at the Xenomai scheduler) that my program somehow enters secondary mode. I tried to remove the "assert" statements as well as the relevant printf's, but this was not successful. Any idea how to fix it?</p>