public inbox for ecos-discuss@sourceware.org
 help / color / mirror / Atom feed
* [ECOS] Two threads -two messages - one mbox
@ 2004-06-20 16:51 Matthias Gorjup
  2004-06-20 17:32 ` Bart Veer
  0 siblings, 1 reply; 3+ messages in thread
From: Matthias Gorjup @ 2004-06-20 16:51 UTC (permalink / raw)
  To: ecos-discuss

Hello,

I am having problems with sending more than on mbox-meassage in one 
loop-iteration from one thread to another. Below is the code we are using:

cyg_handle_t mbox_handle;
void *sock_msb;
#define THREAD1_NAME "Thread1"
#define THREAD2_NAME "Thread2"
unsigned char THREAD1_STACK[0x2000];
unsigned char THREAD2_STACK[0x2000];
static cyg_handle_t THREAD1_Handle;
static cyg_handle_t THREAD2_Handle;
static cyg_thread   THREAD1_Thread;
static cyg_thread   THREAD2_Thread;

int message1;
int message2;

void main()
{

    cyg_thread_create(20,
                      Thread1,
                      (cyg_addrword_t)0,
                      RPC_THREAD1_NAME,
                      (void *)THREAD1_STACK,
                      0x2000,
                      &THREAD1_Handle,
                      &THREAD1_Thread);

    cyg_thread_resume(THREAD1_Handle); // start thread 1

    cyg_thread_create(22,
                      Thread2, // CBFTask
                      (cyg_addrword_t)0,
                      RPC_THREAD2_NAME,
                      (void *)RPC_THREAD2_STACK,
                      0x2000,
                      &THREAD2_Handle,
                      &THREAD2_Thread);

    cyg_thread_resume(RPC_CB_TASK_Handle); // start thread 2

     cyg_mbox_create(&mbox_handle,sock_msb);

}

void Thread1(void)
{
	while (1)
	{
		cyg_thread_delay(1000);
		cyg_mbox_put(mbox_handle, message1);
		cyg_mbox_put(mbox_handle, message2);
	}
}

void Thread2(void)
{
	void *msg;
	while(1)
	{
		cyg_mbox_get(mbox_handle, );
		cyg_mbox_get(mbox_handle, );
	}
}

If I only send one message and read one mesage, the code works fine. But if I 
send two messages in one iteration, the system reboots after the first 
iteration of the sending thread.

I must say that I need to send two integer values from one thread to another 
during one iteration and the other thread needs to read both of them in one 
iteration as well.

What could be wrong?
Should I use a global structure with two integer elements and then send only a 
pointer to that structure (this would make it possible to send only one 
message in one iteration)?
Should I use mutex-es or some other synchronizaiton mechanism?

Thank you all for your time

Matthias Gorjup
 
-- 
Matthias Gorjup
Norik Systems
www.norik.com
Tel: + 386 41 540 545
Tel: + 386 3 759 3077
Fax: + 386 3 759 3078


-- 
Before posting, please read the FAQ: http://ecos.sourceware.org/fom/ecos
and search the list archive: http://ecos.sourceware.org/ml/ecos-discuss

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

* Re: [ECOS] Two threads -two messages - one mbox
  2004-06-20 16:51 [ECOS] Two threads -two messages - one mbox Matthias Gorjup
@ 2004-06-20 17:32 ` Bart Veer
  2004-06-21 17:46   ` Matthias Gorjup
  0 siblings, 1 reply; 3+ messages in thread
From: Bart Veer @ 2004-06-20 17:32 UTC (permalink / raw)
  To: gorjup; +Cc: ecos-discuss

>>>>> "Matthias" == Matthias Gorjup <gorjup@norik.com> writes:

    Matthias> Hello,
    Matthias> I am having problems with sending more than on
    Matthias> mbox-meassage in one loop-iteration from one thread to
    Matthias> another. Below is the code we are using:

    <snip>

    Matthias> If I only send one message and read one mesage, the code
    Matthias> works fine. But if I send two messages in one iteration,
    Matthias> the system reboots after the first iteration of the
    Matthias> sending thread.

    Matthias> I must say that I need to send two integer values from
    Matthias> one thread to another during one iteration and the other
    Matthias> thread needs to read both of them in one iteration as
    Matthias> well.

    Matthias> What could be wrong?
    Matthias> Should I use a global structure with two integer
    Matthias> elements and then send only a pointer to that structure
    Matthias> (this would make it possible to send only one message in
    Matthias> one iteration)? Should I use mutex-es or some other
    Matthias> synchronizaiton mechanism?

I can see two obvious problems with your code.

1) you are creating and starting the threads before the
   synchronization data structures. In particular mbox_handle could
   get used by thread1() or thread2() before main() has created it. In
   your simple example code main() is likely to run at a higher
   priority then the other threads so there is not actually a problem,
   but that is likely to change as the code gets more complicated. It
   is a good idea to initialize all synchronization data structures
   before starting any threads.

2) you are not allocating any space for the mbox itself. Your sock_msb
   argument to cyg_mbox_create() is an uninitialized pointer so it
   will have a value of 0. Hence you are placing the mbox at location
   0, where the interrupt vectors are likely to be depending on the
   architecture you are using.

   I suspect that when you do the second mbox send you are overwriting
   a critical interrupt vector, and the next time an interrupt
   triggers the system will crash and burn.

   Instead you need to allocate space for the mbox, something like:

     cyg_handle_t  mbox_handle;
     cyg_mbox	   mbox_data;

     ...

     int
     main(int argc, char** argv)
     {
         cyg_mbox_create(&mbox_handle, &mbox_data);

         /* now create and start the threads */
     }

Bart

-- 
Bart Veer                       eCos Configuration Architect
http://www.ecoscentric.com/     The eCos and RedBoot experts

-- 
Before posting, please read the FAQ: http://ecos.sourceware.org/fom/ecos
and search the list archive: http://ecos.sourceware.org/ml/ecos-discuss

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

* Re: [ECOS] Two threads -two messages - one mbox
  2004-06-20 17:32 ` Bart Veer
@ 2004-06-21 17:46   ` Matthias Gorjup
  0 siblings, 0 replies; 3+ messages in thread
From: Matthias Gorjup @ 2004-06-21 17:46 UTC (permalink / raw)
  To: Bart Veer; +Cc: ecos-discuss


On Sunday 20 June 2004 19:32, Bart Veer wrote:
> >>>>> "Matthias" == Matthias Gorjup <gorjup@norik.com> writes:
>
>     Matthias> Hello,
>     Matthias> I am having problems with sending more than on
>     Matthias> mbox-meassage in one loop-iteration from one thread to
>     Matthias> another. Below is the code we are using:
>
>     <snip>
>
>     Matthias> If I only send one message and read one mesage, the code
>     Matthias> works fine. But if I send two messages in one iteration,
>     Matthias> the system reboots after the first iteration of the
>     Matthias> sending thread.
>
>     Matthias> I must say that I need to send two integer values from
>     Matthias> one thread to another during one iteration and the other
>     Matthias> thread needs to read both of them in one iteration as
>     Matthias> well.
>
>     Matthias> What could be wrong?
>     Matthias> Should I use a global structure with two integer
>     Matthias> elements and then send only a pointer to that structure
>     Matthias> (this would make it possible to send only one message in
>     Matthias> one iteration)? Should I use mutex-es or some other
>     Matthias> synchronizaiton mechanism?
>
> I can see two obvious problems with your code.
>
> 1) you are creating and starting the threads before the
>    synchronization data structures. In particular mbox_handle could
>    get used by thread1() or thread2() before main() has created it. In
>    your simple example code main() is likely to run at a higher
>    priority then the other threads so there is not actually a problem,
>    but that is likely to change as the code gets more complicated. It
>    is a good idea to initialize all synchronization data structures
>    before starting any threads.
>
> 2) you are not allocating any space for the mbox itself. Your sock_msb
>    argument to cyg_mbox_create() is an uninitialized pointer so it
>    will have a value of 0. Hence you are placing the mbox at location
>    0, where the interrupt vectors are likely to be depending on the
>    architecture you are using.
>
Thanks!! Now it works :)

Matthias Gorjup

>    I suspect that when you do the second mbox send you are overwriting
>    a critical interrupt vector, and the next time an interrupt
>    triggers the system will crash and burn.
>
>    Instead you need to allocate space for the mbox, something like:
>
>      cyg_handle_t  mbox_handle;
>      cyg_mbox	   mbox_data;
>
>      ...
>
>      int
>      main(int argc, char** argv)
>      {
>          cyg_mbox_create(&mbox_handle, &mbox_data);
>
>          /* now create and start the threads */
>      }
>
> Bart
>
> --
> Bart Veer                       eCos Configuration Architect
> http://www.ecoscentric.com/     The eCos and RedBoot experts

-- 
Matthias Gorjup
Norik Systems
www.norik.com
Tel: + 386 41 540 545
Tel: + 386 3 759 3077
Fax: + 386 3 759 3078


-- 
Before posting, please read the FAQ: http://ecos.sourceware.org/fom/ecos
and search the list archive: http://ecos.sourceware.org/ml/ecos-discuss

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

end of thread, other threads:[~2004-06-21 17:46 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-06-20 16:51 [ECOS] Two threads -two messages - one mbox Matthias Gorjup
2004-06-20 17:32 ` Bart Veer
2004-06-21 17:46   ` Matthias Gorjup

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).