Skip to contentSkip to conversation

RT_QUEUE problem in Sender/Receiver order

Xenomai-help Michel He 4 participants 7 messages Apr 19, 2010 – Apr 20, 2010

lore
Share & tools
lore

Michel He

author
#0 lore raw

hello everyone,

I find out a problem (it seems already mentionned before but I
can't find the link to the forum and the post). It deals with the message passing, eg. when we change the order of task execution, something can get wrong. Notice that codes below, if we launch the exection of Recep2,Recep3,Sender, Recep2 won't get the right message. But if we change the order to : Sender,Recep2,Recep3, everything is back to OK. So any explanation ?

err = rt_task_spawn(&test_task2_ptr, "Recep2", STACK_SIZE, STD_PRIO,

0, &testtask2, NULL);
	if (err) {
		rt_printf("error rt_task_spawn 2\n");
		return 0;
	}
	recvTask[nrecvhost]=&test_task2_ptr;

	err =  

rt_queue_create(&(recvCanal[nrecvhost]),"Recep2CANAL",MAX_MESSAGE_LENGTH, QUEUE_LIM, Q_FIFO);

	if (err) {
		rt_printf("error RT_canal_create 2 \n");
		return 0;
	}
	nrecvhost++;


	err = rt_task_spawn(&test_task3_ptr, "Recep3", STACK_SIZE, STD_PRIO,  
0, &testtask3, NULL);
	if (err) {
		rt_printf("error rt_task_spawn 3\n");
		return 0;
	}
	recvTask[nrecvhost]=&test_task3_ptr;

	err =  

rt_queue_create(&(recvCanal[nrecvhost]),"Recep3CANAL",MAX_MESSAGE_LENGTH, QUEUE_LIM, Q_FIFO);

	if (err) {
		rt_printf("error RT_canal_create 2 \n");
		return 0;

	}
	nrecvhost++;


	err = rt_task_spawn(&test_task_ptr, "Sender", STACK_SIZE, STD_PRIO,  
0, &testtask, NULL);
	if (err) {
		rt_printf("error rt_task_spawn\n");
		return 0;
	}
	recvTask[nrecvhost]=&test_task_ptr;
	nrecvhost++;

testtask2()
{
...
rcbytes=RT_queue_read(&(recvCanal[0]),&rcvmsg,sizeof(rcvmsg),TM_INFINITE); }

testtask3()
{
...
rcbytes=RT_queue_read(&(recvCanal[1]),&rcvmsg,sizeof(rcvmsg),TM_INFINITE); }

thank you

#1 lore raw
Quoted 9 lines · Michel He

hello everyone,

I find out a problem (it seems already mentionned before but I
can't find the link to the forum and the post). It deals with the message passing, eg. when we change the order of task execution, something can get wrong. Notice that codes below, if we launch the exection of Recep2,Recep3,Sender, Recep2 won't get the right message. But if we change the order to : Sender,Recep2,Recep3, everything is back to OK. So any explanation ?

A piece of self-contained code, compiling and demonstrating a bug when running is worth a thousand words.

Signature · 1 line
					    Gilles.
author
#2 lore raw

Re,

In order to describe it (by thousand words as the proverbe said),
I attach the following example.
You notice that in the line 85 :
rv = rt_task_send(&test_task3_ptr,&mcb_send,&mcb_reply,task_period_ns);

whenever you change &test_task3_ptr to &test_task2_ptr, the message is NOT delivered at all. Only task3 can receive good msg. (not task2)

thank you

/*
* tasksend.c
*
* Created on: 7 avr. 2010
* Author: hemichel
*/

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <sys/time.h>
#include <sys/io.h>
#include <sys/mman.h>
#include <native/task.h>
#include <native/queue.h>
#include <native/intr.h>

#define STACK_SIZE 8192
#define STD_PRIO 1

#define MAXRECVTASK 0
#define ANYSRC 0
#define MAX_MESSAGE_LENGTH 4
#define QUEUE_LIM 512
#define PROBECODE 0xa

RT_TASK test_task_ptr,test_task2_ptr,test_task3_ptr;

//tache maintenant la liste recvhostlist
RT_TASK *recvTask[MAXRECVTASK];
unsigned int nrecvhost=0;

int int_count = 0;
int end = 0;

//delay reception 0.1 s
#define PEER_RATE_NS 100000000

//cycle de la tache d'emission
// --s-ms-us-ns
RTIME task_period_ns = 1000000000llu;

void testtask(void *cookie) {
	RT_TASK_MCB mcb_send, mcb_reply;
	int flowid, i, rv;
	unsigned char datasend[16];
	unsigned char datareply[16];

	int count = 0;
	int ret;
	unsigned long overrun;
	ret = rt_task_set_periodic(NULL, TM_NOW, rt_timer_ns2ticks(task_period_ns));
	if (ret) {
		rt_printf("error while set periodic, code %d\n",ret);
		return;
	}

	mcb_send.opcode = 0x03;
	datasend[0]='a';
	mcb_send.data = datasend;
	mcb_send.size = sizeof(datasend);

	mcb_reply.size = sizeof(datareply);
	mcb_reply.data = datareply;

	while(!end){

		if (ret) {
			rt_printf("error while rt_task_set_mode, code %d\n",ret);
			return;
		}
		ret = rt_task_wait_period(&overrun);
		if (ret) {
			rt_printf("error while rt_task_wait_period, code %d\n",ret);
			return;
		}
		count++;
		rt_printf("message from testtask: count=%d\n", count);

		rv = rt_task_send(&test_task3_ptr,&mcb_send,&mcb_reply,task_period_ns);
		if (rv < 0) rt_printf("rt_task_send error\n");
		else rt_printf("get mcb_reply=%d\n",mcb_reply.data[0]);
		fflush(NULL);
	}
}
void testtask2(void *cookie) {
	RT_TASK_MCB mcb_rcv, mcb_reply;
	int flowid, i, rv;
	unsigned char datareply[16];
	unsigned int taskmsg;
	int count = 0;
	int ret;
	unsigned long overrun;
	ret = rt_task_set_periodic(NULL, TM_NOW, rt_timer_ns2ticks(task_period_ns));
	if (ret) {
		rt_printf("error while set periodic, code %d\n",ret);
		return;
	}

	while(!end){

		ret = rt_task_wait_period(&overrun);
		if (ret) {
			rt_printf("error while rt_task_wait_period, code %d\n",ret);
			return;
		}

		mcb_rcv.data = (caddr_t)&datareply;
		mcb_rcv.size = sizeof(datareply);

		flowid = rt_task_receive(&mcb_rcv,PEER_RATE_NS);
		rt_printf("%s flowid=%d opcode=%d\n",\
				__FUNCTION__, flowid, mcb_rcv.opcode);
		if(flowid >= 0)
		{
				//this is my answer
				mcb_reply.opcode = 1;
				mcb_reply.size = 1;
				mcb_reply.data = datareply;
				datareply[0]=100;
				rt_task_reply(flowid, &mcb_reply);
				rt_printf("task2 replied to Sender\n");
		}
		else
		{
			rt_printf("err task2 : flowid neg\n");
		}

		fflush(NULL);
	}
}
void testtask3(void *cookie) {
	RT_TASK_MCB mcb_rcv, mcb_reply;
	int flowid, i, rv;
	unsigned char datareply[16];
	unsigned int taskmsg;

	int count = 0;
	int ret;
	unsigned long overrun;
	ret = rt_task_set_periodic(NULL, TM_NOW, rt_timer_ns2ticks(task_period_ns));
	if (ret) {
		rt_printf("error while set periodic, code %d\n",ret);
		return;
	}

	while(!end){

		ret = rt_task_wait_period(&overrun);
		if (ret) {
			rt_printf("error while rt_task_wait_period, code %d\n",ret);
			return;
		}

		mcb_rcv.data = (caddr_t)&datareply;
		mcb_rcv.size = sizeof(datareply);

		flowid = rt_task_receive(&mcb_rcv,PEER_RATE_NS);
		rt_printf("%s flowid=%d opcode=%d\n",\
				__FUNCTION__, flowid, mcb_rcv.opcode);
		if(flowid >= 0)
		{
				//this is my answer
				mcb_reply.opcode = 1;
				mcb_reply.size = 1;
				mcb_reply.data = datareply;
				datareply[0]=100;
				rt_task_reply(flowid, &mcb_reply);
				rt_printf("task3 replied to Sender\n");
		}
		else
		{
			rt_printf("err task3 : flowid neg\n");
		}

		fflush(NULL);
	}
}

// signal-handler, to ensure clean exit on Ctrl-C

void clean_exit(int dummy) {
	rt_printf("cleanup\n");
	end = 1;
	rt_task_delete(&test_task_ptr);
	rt_printf("end\n");
}
int main(int argc, char *argv[]) {
	int err, ret;
	int itask;


	// install signal handler
	signal(SIGTERM, clean_exit);
	signal(SIGINT, clean_exit);

	mlockall(MCL_CURRENT | MCL_FUTURE);

	/* Perform auto-init of rt_print buffers if the task doesn't do so */
	rt_print_auto_init(1);

	//Initialisation de Dispatch Sender
	for (itask=0; itask < nrecvhost; itask++)
	{
		recvTask[itask]=NULL;
	}

	err = rt_task_spawn(&test_task_ptr, "Timer", STACK_SIZE, STD_PRIO, 0,  
&testtask, NULL);
	recvTask[nrecvhost]=&test_task_ptr;
	nrecvhost++;
	if (err) {
		rt_printf("error rt_task_spawn\n");
		return 0;
	}

	nrecvhost=0;
	rt_printf("Fin Initialisation de la tache Emetteur\n");

	err = rt_task_spawn(&test_task2_ptr, "Timer2", STACK_SIZE, STD_PRIO,  
0, &testtask2, NULL);
	recvTask[nrecvhost]=&test_task2_ptr;
	nrecvhost++;
	if (err) {
		rt_printf("error rt_task_spawn\n");
		return 0;
	}


	err = rt_task_spawn(&test_task3_ptr, "Timer3", STACK_SIZE, STD_PRIO,  
0, &testtask3, NULL);
	recvTask[nrecvhost]=&test_task3_ptr;
	nrecvhost++;
	if (err) {
		rt_printf("error rt_task_spawn\n");
		return 0;
	}



	// wait for signal & return of signal handler
	pause();
	fflush(NULL);
	return 0;
}

Gilles Chanteperdrix <[email protected]> a écrit :

Quoted 18 lines
Quoted 9 lines · Michel He

hello everyone,

I find out a problem (it seems already mentionned before but I
can't find the link to the forum and the post). It deals with the message passing, eg. when we change the order of task execution, something can get wrong. Notice that codes below, if we launch the exection of Recep2,Recep3,Sender, Recep2 won't get the right message. But if we change the order to : Sender,Recep2,Recep3, everything is back to OK. So any explanation ?

A piece of self-contained code, compiling and demonstrating a bug when running is worth a thousand words.

--
Gilles.

#3 lore raw
In reply to Michel He
Quoted 4 lines · Michel He

hello everyone,

I find out a problem (it seems already mentionned before but I
can't find the link to the forum and the post).

Ok. In the same vein, it turns out that you might be right, but I could not find the link to the post that says so.

Quoted 6 lines

It deals with the
message passing, eg. when we change the order of task execution, something can get wrong. Notice that codes below, if we launch the exection of Recep2,Recep3,Sender, Recep2 won't get the right message. But if we change the order to : Sender,Recep2,Recep3, everything is back to OK. So any explanation ?

Any explanation from your end about what you would consider as a correct result from that code, or should we resort to our well-known psychic power to find it?

Quoted 74 lines

err = rt_task_spawn(&test_task2_ptr, "Recep2", STACK_SIZE, STD_PRIO,

0, &testtask2, NULL);
	if (err) {
		rt_printf("error rt_task_spawn 2\n");
		return 0;
	}
	recvTask[nrecvhost]=&test_task2_ptr;

	err =  

rt_queue_create(&(recvCanal[nrecvhost]),"Recep2CANAL",MAX_MESSAGE_LENGTH, QUEUE_LIM, Q_FIFO);

	if (err) {
		rt_printf("error RT_canal_create 2 \n");
		return 0;
	}
	nrecvhost++;


	err = rt_task_spawn(&test_task3_ptr, "Recep3", STACK_SIZE, STD_PRIO,  
0, &testtask3, NULL);
	if (err) {
		rt_printf("error rt_task_spawn 3\n");
		return 0;
	}
	recvTask[nrecvhost]=&test_task3_ptr;

	err =  

rt_queue_create(&(recvCanal[nrecvhost]),"Recep3CANAL",MAX_MESSAGE_LENGTH, QUEUE_LIM, Q_FIFO);

	if (err) {
		rt_printf("error RT_canal_create 2 \n");
		return 0;

	}
	nrecvhost++;


	err = rt_task_spawn(&test_task_ptr, "Sender", STACK_SIZE, STD_PRIO,  
0, &testtask, NULL);
	if (err) {
		rt_printf("error rt_task_spawn\n");
		return 0;
	}
	recvTask[nrecvhost]=&test_task_ptr;
	nrecvhost++;

testtask2()
{
...
rcbytes=RT_queue_read(&(recvCanal[0]),&rcvmsg,sizeof(rcvmsg),TM_INFINITE); }

testtask3()
{
...
rcbytes=RT_queue_read(&(recvCanal[1]),&rcvmsg,sizeof(rcvmsg),TM_INFINITE); }

thank you

_______________________________________________
Xenomai-help mailing list
[email protected]
https://mail.gna.org/listinfo/xenomai-help

Signature · 1 line
Philippe.
#4 lore raw

Hi,

I'm reluctant to jump in here with my own notably limited psychic powers, but I did notice that the code given spawns the receiving tasks before it creates the queues that they receive from. This would create a race condition (if a STD_PRIO task can preempt the task that's doing the initialization.)

-Josh

Quoted 70 lines · Philippe Gerum
Quoted 2 lines · Michel He
      err = rt_task_spawn(&test_task2_ptr, "Recep2", STACK_SIZE,

STD_PRIO,

Quoted 11 lines
0, &testtask2, NULL);
      if (err) {
              rt_printf("error rt_task_spawn 2\n");
              return 0;
      }
      recvTask[nrecvhost]=&test_task2_ptr;

      err =

rt_queue_create(&(recvCanal[nrecvhost]),"Recep2CANAL",MAX_MESSAGE_LENGTH, QUEUE_LIM, Q_FIFO);

Quoted 8 lines
      if (err) {
              rt_printf("error RT_canal_create 2 \n");
              return 0;
      }
      nrecvhost++;


      err = rt_task_spawn(&test_task3_ptr, "Recep3", STACK_SIZE,

STD_PRIO,

Quoted 30 lines
0, &testtask3, NULL);
      if (err) {
              rt_printf("error rt_task_spawn 3\n");
              return 0;
      }
      recvTask[nrecvhost]=&test_task3_ptr;

      err =

rt_queue_create(&(recvCanal[nrecvhost]),"Recep3CANAL",MAX_MESSAGE_LENGTH, QUEUE_LIM, Q_FIFO);

      if (err) {
              rt_printf("error RT_canal_create 2 \n");
              return 0;

      }
      nrecvhost++;


      err = rt_task_spawn(&test_task_ptr, "Sender", STACK_SIZE, STD_PRIO,
0, &testtask, NULL);
      if (err) {
              rt_printf("error rt_task_spawn\n");
              return 0;
      }
      recvTask[nrecvhost]=&test_task_ptr;
      nrecvhost++;
Quoted 4 lines

testtask2()
{
...

rcbytes=RT_queue_read(&(recvCanal[0]),&rcvmsg,sizeof(rcvmsg),TM_INFINITE);

Quoted 7 lines

}

testtask3()
{
...

rcbytes=RT_queue_read(&(recvCanal[1]),&rcvmsg,sizeof(rcvmsg),TM_INFINITE);

Quoted 2 lines

}

#5 lore raw
Quoted 8 lines · Josh Bowman

Hi,

I'm reluctant to jump in here with my own notably limited psychic powers, but I did notice that the code given spawns the receiving tasks before it creates the queues that they receive from. This would create a race condition (if a STD_PRIO task can preempt the task that's doing the initialization.)

You are likely right, priority issues are usually involved in that scenario, particularly since the initiator of all those creation calls is the low-priority root thread.

Quoted 79 lines

-Josh

On Mon, Apr 19, 2010 at 1:16 PM, Philippe Gerum <[email protected]> wrote:

        On Mon, 2010-04-19 at 19:01 +0200, Michel He wrote:
        
        >
        >       err = rt_task_spawn(&test_task2_ptr, "Recep2",
        STACK_SIZE, STD_PRIO,
        > 0, &testtask2, NULL);
        >       if (err) {
        >               rt_printf("error rt_task_spawn 2\n");
        >               return 0;
        >       }
        >       recvTask[nrecvhost]=&test_task2_ptr;
        >
        >       err =
        >
        rt_queue_create(&(recvCanal[nrecvhost]),"Recep2CANAL",MAX_MESSAGE_LENGTH,
        > QUEUE_LIM, Q_FIFO);
        >
        >       if (err) {
        >               rt_printf("error RT_canal_create 2 \n");
        >               return 0;
        >       }
        >       nrecvhost++;
        >
        >
        >       err = rt_task_spawn(&test_task3_ptr, "Recep3",
        STACK_SIZE, STD_PRIO,
        > 0, &testtask3, NULL);
        >       if (err) {
        >               rt_printf("error rt_task_spawn 3\n");
        >               return 0;
        >       }
        >       recvTask[nrecvhost]=&test_task3_ptr;
        >
        >       err =
        >
        rt_queue_create(&(recvCanal[nrecvhost]),"Recep3CANAL",MAX_MESSAGE_LENGTH,
        > QUEUE_LIM, Q_FIFO);
        >
        >       if (err) {
        >               rt_printf("error RT_canal_create 2 \n");
        >               return 0;
        >
        >       }
        >       nrecvhost++;
        >
        >
        >       err = rt_task_spawn(&test_task_ptr, "Sender",
        STACK_SIZE, STD_PRIO,
        > 0, &testtask, NULL);
        >       if (err) {
        >               rt_printf("error rt_task_spawn\n");
        >               return 0;
        >       }
        >       recvTask[nrecvhost]=&test_task_ptr;
        >       nrecvhost++;
        >
        >
        >
        > testtask2()
        > {
        > ...
        >
        rcbytes=RT_queue_read(&(recvCanal[0]),&rcvmsg,sizeof(rcvmsg),TM_INFINITE);
        > }
        >
        >
        > testtask3()
        > {
        > ...
        >
        rcbytes=RT_queue_read(&(recvCanal[1]),&rcvmsg,sizeof(rcvmsg),TM_INFINITE);
        > }
        >
Signature · 1 line
Philippe.
author
#6 lore raw

I firstly thought in xenomai the rt_queue couldn't be simulatenously written and read by 2 process, because of the reservation is cancelled during the occupancy of the queue. but actually not sure about it, so I changed slightly my codes (without adding semaphore though), and everything is right now.

thank you anyways

Philippe Gerum <[email protected]> a écrit :

Quoted 104 lines · Josh Bowman
Quoted 8 lines

Hi,

I'm reluctant to jump in here with my own notably limited psychic powers, but I did notice that the code given spawns the receiving tasks before it creates the queues that they receive from. This would create a race condition (if a STD_PRIO task can preempt the task that's doing the initialization.)

You are likely right, priority issues are usually involved in that scenario, particularly since the initiator of all those creation calls is the low-priority root thread.

Quoted 78 lines

-Josh

On Mon, Apr 19, 2010 at 1:16 PM, Philippe Gerum <[email protected]> wrote:

        On Mon, 2010-04-19 at 19:01 +0200, Michel He wrote:

        >
        >       err = rt_task_spawn(&test_task2_ptr, "Recep2",
        STACK_SIZE, STD_PRIO,
        > 0, &testtask2, NULL);
        >       if (err) {
        >               rt_printf("error rt_task_spawn 2\n");
        >               return 0;
        >       }
        >       recvTask[nrecvhost]=&test_task2_ptr;
        >
        >       err =
        >

rt_queue_create(&(recvCanal[nrecvhost]),"Recep2CANAL",MAX_MESSAGE_LENGTH,

        > QUEUE_LIM, Q_FIFO);
        >
        >       if (err) {
        >               rt_printf("error RT_canal_create 2 \n");
        >               return 0;
        >       }
        >       nrecvhost++;
        >
        >
        >       err = rt_task_spawn(&test_task3_ptr, "Recep3",
        STACK_SIZE, STD_PRIO,
        > 0, &testtask3, NULL);
        >       if (err) {
        >               rt_printf("error rt_task_spawn 3\n");
        >               return 0;
        >       }
        >       recvTask[nrecvhost]=&test_task3_ptr;
        >
        >       err =
        >

rt_queue_create(&(recvCanal[nrecvhost]),"Recep3CANAL",MAX_MESSAGE_LENGTH,

        > QUEUE_LIM, Q_FIFO);
        >
        >       if (err) {
        >               rt_printf("error RT_canal_create 2 \n");
        >               return 0;
        >
        >       }
        >       nrecvhost++;
        >
        >
        >       err = rt_task_spawn(&test_task_ptr, "Sender",
        STACK_SIZE, STD_PRIO,
        > 0, &testtask, NULL);
        >       if (err) {
        >               rt_printf("error rt_task_spawn\n");
        >               return 0;
        >       }
        >       recvTask[nrecvhost]=&test_task_ptr;
        >       nrecvhost++;
        >
        >
        >
        > testtask2()
        > {
        > ...
        >
rcbytes=RT_queue_read(&(recvCanal[0]),&rcvmsg,sizeof(rcvmsg),TM_INFINITE);
        > }
        >
        >
        > testtask3()
        > {
        > ...
        >
Quoted 5 lines
rcbytes=RT_queue_read(&(recvCanal[1]),&rcvmsg,sizeof(rcvmsg),TM_INFINITE);
        > }
        >

--
Philippe.