Message-ID: Date: Wed, 18 Jan 2006 21:04:25 +0100 From: Jeroen Van den Keybus MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_Part_3468_15604445.1137614665749" Subject: [Xenomai-core] Scheduling while atomic List-Id: "Xenomai life and development \(bug reports, patches, discussions\)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: xenomai@xenomai.org ------=_Part_3468_15604445.1137614665749 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Hello, Apparently, the code I shared with Gilles never made it to this forum. Anyway, the issue I'm having here is really a problem and it might be usefu= l if some of you could try it out or comment on it. I might be making a silly programming error here, but the result is invariably erroneous operation or kernel crashes. The program creates a file dump.txt and has two independent threads trying to access it and write a one or a zero there. Inside the writing routine, which is accessed by both threads, a check is made to see if the access is really locked. In my setup, I have tons of ALERTS popping up with this program, meaning that something is wrong with my use of mutex. Could anyone please check and see if a) it is correctly written and b) it fails as well on their machine. It would allow me to focus my actions on the Xenomai setu= p (which I keep frozen this instant, in order to keep a possible bug predictable) or on my own programming. A second example is also included, which tries to achieve the same goal wit= h a semaphore (initialized to 1). That seems to work, but under heavy load (tmax =3D 1.0e7), the kernel crashes. Kernel: 2.6.15 Adeos: 1.1-03 gcc: 4.0.2 Ipipe tracing enabled TIA Jeroen. /* TEST_MUTEX.C */ #include #include #include #include #include #include #include #include #include #include #include int fd, err; RT_MUTEX m; RT_SEM s; float tmax =3D 1.0e7; #define CHECK(arg) check(arg, __LINE__) int check(int r, int n) { if (r !=3D 0) fprintf(stderr, "L%d: %s.\n", n, strerror(-r)); return(r); } void output(char c) { static int cnt =3D 0; int n; char buf[2]; RT_MUTEX_INFO mutexinfo; buf[0] =3D c; if (cnt =3D=3D 80) { buf[1] =3D '\n'; n =3D 2; cnt =3D 0; } else { n =3D 1; cnt++; } CHECK(rt_mutex_inquire(&m, &mutexinfo)); if (mutexinfo.lockcnt <=3D 0) { RT_TASK_INFO taskinfo; CHECK(rt_task_inquire(NULL, &taskinfo)); fprintf(stderr, "ALERT: No lock! (lockcnt=3D%d) Offending task: %s\= n", mutexinfo.lockcnt, taskinfo.name); } if (write(fd, buf, n) !=3D n) { fprintf(stderr, "File write error.\n"); CHECK(rt_sem_v(&s)); } } void task0(void *arg) { CHECK(rt_task_set_mode(T_PRIMARY, 0, NULL)); while (1) { CHECK(rt_task_sleep((float)rand()*tmax/(float)RAND_MAX)); CHECK(rt_mutex_lock(&m, TM_INFINITE)); output('0'); CHECK(rt_mutex_unlock(&m)); } } void task1(void *arg) { CHECK(rt_task_set_mode(T_PRIMARY, 0, NULL)); while (1) { CHECK(rt_task_sleep((float)rand()*tmax/(float)RAND_MAX)); CHECK(rt_mutex_lock(&m, TM_INFINITE)); output('1'); CHECK(rt_mutex_unlock(&m)); } } void sighandler(int arg) { CHECK(rt_sem_v(&s)); } int main(int argc, char *argv[]) { RT_TASK t, t0, t1; if ((fd =3D open("dump.txt", O_CREAT | O_TRUNC | O_WRONLY)) < 0) fprintf(stderr, "File open error.\n"); else { if (argc =3D=3D 2) { tmax =3D atof(argv[1]); if (tmax =3D=3D 0.0) tmax =3D 1.0e7; } if (mlockall(MCL_CURRENT | MCL_FUTURE) !=3D 0) printf("mlockall() error.\n"); CHECK(rt_task_shadow(&t, "main", 1, T_FPU)); CHECK(rt_timer_start(TM_ONESHOT)); CHECK(rt_mutex_create(&m, "mutex")); CHECK(rt_sem_create(&s, "sem", 0, S_PRIO)); signal(SIGINT, sighandler); CHECK(rt_task_create(&t0, "task0", 0, 30, T_FPU)); CHECK(rt_task_start(&t0, task0, NULL)); CHECK(rt_task_create(&t1, "task1", 0, 29, T_FPU)); CHECK(rt_task_start(&t1, task1, NULL)); printf("Running for %.2f seconds.\n", (float)MAXLONG/1.0e9); CHECK(rt_sem_p(&s, MAXLONG)); signal(SIGINT, SIG_IGN); CHECK(rt_task_delete(&t1)); CHECK(rt_task_delete(&t1)); CHECK(rt_task_delete(&t0)); CHECK(rt_sem_delete(&s)); CHECK(rt_mutex_delete(&m)); rt_timer_stop(); close(fd); } return 0; } /**************************************************************************= ***********/ /* TEST_SEM.C */ #include #include #include #include #include #include #include #include #include #include int fd, err; RT_SEM s, m; float tmax =3D 1.0e9; #define CHECK(arg) check(arg, __LINE__) int check(int r, int n) { if (r !=3D 0) fprintf(stderr, "L%d: %s.\n", n, strerror(-r)); return(r); } void output(char c) { static int cnt =3D 0; int n; char buf[2]; RT_SEM_INFO seminfo; buf[0] =3D c; if (cnt =3D=3D 80) { buf[1] =3D '\n'; n =3D 2; cnt =3D 0; } else { n =3D 1; cnt++; } CHECK(rt_sem_inquire(&m, &seminfo)); if (seminfo.count !=3D 0) { RT_TASK_INFO taskinfo; CHECK(rt_task_inquire(NULL, &taskinfo)); fprintf(stderr, "ALERT: No lock! (count=3D%ld) Offending task: %s\n= ", seminfo.count, taskinfo.name); } if (write(fd, buf, n) !=3D n) { fprintf(stderr, "File write error.\n"); CHECK(rt_sem_v(&s)); } } void task0(void *arg) { CHECK(rt_task_set_mode(T_PRIMARY, 0, NULL)); while (1) { CHECK(rt_task_sleep((float)rand()*tmax/(float)RAND_MAX)); CHECK(rt_sem_p(&m, TM_INFINITE)); output('0'); CHECK(rt_sem_v(&m)); } } void task1(void *arg) { CHECK(rt_task_set_mode(T_PRIMARY, 0, NULL)); while (1) { CHECK(rt_task_sleep((float)rand()*tmax/(float)RAND_MAX)); CHECK(rt_sem_p(&m, TM_INFINITE)); output('1'); CHECK(rt_sem_v(&m)); } } void sighandler(int arg) { CHECK(rt_sem_v(&s)); } int main(int argc, char *argv[]) { RT_TASK t, t0, t1; if ((fd =3D open("dump.txt", O_CREAT | O_TRUNC | O_WRONLY)) < 0) fprintf(stderr, "File open error.\n"); else { if (argc =3D=3D 2) { tmax =3D atof(argv[1]); if (tmax =3D=3D 0.0) tmax =3D 1.0e7; } if (mlockall(MCL_CURRENT | MCL_FUTURE) !=3D 0) printf("mlockall() error.\n"); CHECK(rt_task_shadow(&t, "main", 1, T_FPU)); CHECK(rt_timer_start(TM_ONESHOT)); CHECK(rt_sem_create(&m, "mutex", 1, S_PRIO)); CHECK(rt_sem_create(&s, "sem", 0, S_PRIO)); signal(SIGINT, sighandler); CHECK(rt_task_create(&t0, "task0", 0, 30, T_FPU)); CHECK(rt_task_start(&t0, task0, NULL)); CHECK(rt_task_create(&t1, "task1", 0, 29, T_FPU)); CHECK(rt_task_start(&t1, task1, NULL)); printf("Running for %.2f seconds.\n", (float)MAXLONG/1.0e9); CHECK(rt_sem_p(&s, MAXLONG)); signal(SIGINT, SIG_IGN); CHECK(rt_task_delete(&t1)); CHECK(rt_task_delete(&t1)); CHECK(rt_task_delete(&t0)); CHECK(rt_sem_delete(&s)); CHECK(rt_sem_delete(&m)); rt_timer_stop(); close(fd); } return 0; } ------=_Part_3468_15604445.1137614665749 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline
Hello,
 
 
Apparently, the code I shared with Gilles never made it to this forum.= Anyway, the issue I'm having here is really a problem and it might be usef= ul if some of you could try it out or comment on it. I might be making a si= lly programming error here, but the result is invariably erroneous operatio= n or kernel crashes.
 
The program creates a file dump.txt and has two independent threads tr= ying to access it and write a one or a zero there. Inside the writing routi= ne, which is accessed by both threads, a check is made to see if the access= is really locked. In my setup, I have tons of ALERTS popping up with this = program, meaning that something is wrong with my use of mutex. Could anyone= please check and see if a) it is correctly written and b) it fails as well= on their machine. It would allow me to focus my actions on the Xenomai set= up (which I keep frozen this instant, in order to keep a possible bug predi= ctable) or on my own programming.
 
A second example is also included, which tries to achieve the same goa= l with a semaphore (initialized to 1). That seems to work, but under heavy = load (tmax =3D 1.0e7), the kernel crashes.
 
Kernel: 2.6.15 Adeos: 1.1-03 gcc: 4.0.2 Ipipe tracing enabled
 
TIA
 
Jeroen.
 
 

/* TEST_MUTEX.C */

#include <stdlib.h>
#include <stdio.h>
#include <un= istd.h>
#include <fcntl.h>
#include <signal.h>
#inc= lude <math.h>
#include <values.h>

#include <sys/mman.h>

#include <native/task.h>
#include <native/mutex.h>
#in= clude <native/sem.h>

int fd, err;
RT_MUTEX m;
RT_SEM s;
float tmax =3D 1.0e7;

#define CHECK(arg) check(arg, __LINE__)

int check(int r, int n)
{
    if (r !=3D 0)
&nbs= p;       fprintf(stderr, "L%d: %s.\n&quo= t;, n, strerror(-r));
    return(r);
}

void output(char c) {
    static int cnt =3D 0;
&nb= sp;   int n;
    char buf[2];
  &n= bsp; RT_MUTEX_INFO mutexinfo;
   
    = buf[0] =3D c;
   
    if (cnt =3D=3D 8= 0) {
        buf[1] =3D '\n';
&nbs= p;       n =3D 2;
        cnt =3D 0;
  &n= bsp; }
    else {
      =   n =3D 1;
        cnt++;
&nb= sp;   }
   
    CHECK(rt_mut= ex_inquire(&m, &mutexinfo));
    if (mutexinfo.lo= ckcnt <=3D 0) {
        RT_TASK_IN= FO taskinfo;
        CHECK(rt_task_inquire(NULL, = &taskinfo));
        fprintf(stde= rr, "ALERT: No lock! (lockcnt=3D%d) Offending task: %s\n",
&nb= sp;            =    mutexinfo.lockcnt, taskinfo.n= ame );
    }
  
    if (w= rite(fd, buf, n) !=3D n) {
        fp= rintf(stderr, "File write error.\n");
    =     CHECK(rt_sem_v(&s));
    }
&nbs= p;  
}

void task0(void *arg)
{
    CHECK(rt_task_set_mode(= T_PRIMARY, 0, NULL));
    while (1) {
  &nbs= p;     CHECK(rt_task_sleep((float)rand()*tmax/(float)RA= ND_MAX));
        CHECK(rt_mutex_lock= (&m, TM_INFINITE));
        output('0');
  =       CHECK(rt_mutex_unlock(&m));
 &nb= sp;  }
}

void task1(void *arg)
{
    CHECK(rt_task_set_mode(= T_PRIMARY, 0, NULL));
    while (1) {
  &nbs= p;     CHECK(rt_task_sleep((float)rand()*tmax/(float)RA= ND_MAX));
        CHECK(rt_mutex_lock= (&m, TM_INFINITE));
        output('1');
  =       CHECK(rt_mutex_unlock(&m));
 &nb= sp;  }
}

void sighandler(int arg)
{
    CHECK(rt_sem_v(&= s));
}

int main(int argc, char *argv[])
{
    RT_TASK t, t= 0, t1;
   
    if ((fd =3D open("= dump.txt", O_CREAT | O_TRUNC | O_WRONLY)) < 0)
   = ;     fprintf(stderr, "File open error.\n");<= br>     else {
        if = (argc =3D=3D 2) {
         =    tmax =3D atof(argv[1]);
      = ;      if (tmax =3D=3D 0.0)
   &= nbsp;            tma= x =3D 1.0e7;
        }
  = ;      if (mlockall(MCL_CURRENT | MCL_FUTURE) !=3D= 0)
            p= rintf("mlockall() error.\n");
       
    =     CHECK(rt_task_shadow(&t, "main", 1, T_FPU)= );

        CHECK(rt_timer_start(TM_ONESH= OT));
       
   &= nbsp;    CHECK(rt_mutex_create(&m, "mutex"));<= br>        CHECK(rt_sem_create(&s, &= quot;sem", 0, S_PRIO));

        signal(SIGINT, sighandler);       
    &n= bsp;   CHECK(rt_task_create(&t0, "task0", 0, 30, T_= FPU));
        CHECK(rt_task_start(&a= mp;t0, task0, NULL));
        CHECK(r= t_task_create(&t1, "task1", 0, 29, T_FPU));
        CHECK(rt_task_start(&t1,= task1, NULL));

        printf("Running for %.2f= seconds.\n", (float)MAXLONG/1.0e9);
     =    CHECK(rt_sem_p(&s, MAXLONG));
    &= nbsp;   signal(SIGINT, SIG_IGN);
     = ;  
        CHECK(rt_task_= delete(&t1));
        CHECK(rt_task_delete(&t1));<= br>        CHECK(rt_task_delete(&t0)= );
       
   &nbs= p;    CHECK(rt_sem_delete(&s));
   &nb= sp;    CHECK(rt_mutex_delete(&m));
   =     
        rt_= timer_stop();
       
    &nbs= p;   close(fd);
    }
    ret= urn 0;
}

/***********************************************************************= **************/ 

/* TEST_SEM.C */

#include <stdlib.h>
#include <stdio.h>
#include <un= istd.h>
#include <fcntl.h>
#include <signal.h>
#inc= lude <math.h>
#include <values.h>

#include <sys/mman.h>

#include <native/task.h>
#include <native/sem.h>

int fd, err;
RT_SEM s, m;
float tmax =3D 1.0e9;

#define CHECK(arg) check(arg, __LINE__)

int check(int r, int n)
{
    if (r !=3D 0)
&nbs= p;       fprintf(stderr, "L%d: %s.\n&quo= t;, n, strerror(-r));
    return(r);
}

void output(char c) {
    static int cnt =3D 0;
&nb= sp;   int n;
    char buf[2];
  &n= bsp; RT_SEM_INFO seminfo;
   
    buf[= 0] =3D c;
   
    if (cnt =3D=3D 80) {=
        buf[1] =3D '\n';
 &n= bsp;      n =3D 2;
        cnt =3D 0;
   = }
    else {
      &nbs= p; n =3D 1;
        cnt++;
 &= nbsp;  }
   
    CHECK(rt_sem_inq= uire(&m, &seminfo));
    if (seminfo.count !=3D 0= ) {
        RT_TASK_INFO taskinfo;        CHECK(rt_task_inquire(NULL, &am= p;taskinfo));
        fprintf(stderr, "ALERT:= No lock! (count=3D%ld) Offending task: %s\n",
   &n= bsp;            semi= nfo.count, taskinfo.name);
 &n= bsp;  }
  
    if (write(fd, buf, n) != =3D n) {
        fprintf(stderr, "File w= rite error.\n");
        CHECK(r= t_sem_v(&s));
    }
   
}

void task0(void *arg)
{
    CHECK(rt_task_set_mode(= T_PRIMARY, 0, NULL));
    while (1) {
  &nbs= p;     CHECK(rt_task_sleep((float)rand()*tmax/(float)RA= ND_MAX));
        CHECK(rt_sem_p(&= ;m, TM_INFINITE));
        output('0');
  &nbs= p;     CHECK(rt_sem_v(&m));
    }=
}

void task1(void *arg)
{
    CHECK(rt_task_set_mode(= T_PRIMARY, 0, NULL));
    while (1) {
  &nbs= p;     CHECK(rt_task_sleep((float)rand()*tmax/(float)RA= ND_MAX));
        CHECK(rt_sem_p(&= ;m, TM_INFINITE));
        output('1');
  &nbs= p;     CHECK(rt_sem_v(&m));
    }=
}

void sighandler(int arg)
{
    CHECK(rt_sem_v(&= s));
}

int main(int argc, char *argv[])
{
    RT_TASK t, t= 0, t1;
   
    if ((fd =3D open("= dump.txt", O_CREAT | O_TRUNC | O_WRONLY)) < 0)
   = ;     fprintf(stderr, "File open error.\n");<= br>     else {
        if = (argc =3D=3D 2) {
         =    tmax =3D atof(argv[1]);
      = ;      if (tmax =3D=3D 0.0)
   &= nbsp;            tma= x =3D 1.0e7;
        }
  = ;      if (mlockall(MCL_CURRENT | MCL_FUTURE) !=3D= 0)
            p= rintf("mlockall() error.\n");
       
    =     CHECK(rt_task_shadow(&t, "main", 1, T_FPU)= );

        CHECK(rt_timer_start(TM_ONESH= OT));
       
   &= nbsp;    CHECK(rt_sem_create(&m, "mutex", 1, S= _PRIO));
        CHECK(rt_sem_create(= &s, "sem", 0, S_PRIO));

        signal(SIGINT, sighandler);       
    &n= bsp;   CHECK(rt_task_create(&t0, "task0", 0, 30, T_= FPU));
        CHECK(rt_task_start(&a= mp;t0, task0, NULL));
        CHECK(r= t_task_create(&t1, "task1", 0, 29, T_FPU));
        CHECK(rt_task_start(&t1,= task1, NULL));

        printf("Running for %.2f= seconds.\n", (float)MAXLONG/1.0e9);
     =    CHECK(rt_sem_p(&s, MAXLONG));
    &= nbsp;   signal(SIGINT, SIG_IGN);
     = ;  
        CHECK(rt_task_= delete(&t1));
        CHECK(rt_task_delete(&t1));<= br>        CHECK(rt_task_delete(&t0)= );
       
   &nbs= p;    CHECK(rt_sem_delete(&s));
   &nb= sp;    CHECK(rt_sem_delete(&m));
   &n= bsp;   
        rt_ti= mer_stop();
       =20
        close(fd);
  &n= bsp; }
    return 0;
}

------=_Part_3468_15604445.1137614665749--