Right, automatic memory inside functions (ie the stack) gets nixed after
you exit a function :).  Here is a simple change that should work for you:

You can still use automatic memory, but do it as as a GLOBAL so it
doesn't die until your whole module is unloaded... do the following:

unsigned int fifos[2] = {0, 1}

instead of the following which you had in init_module:
> >   unsigned int fifos[2] = {0,1};//write to 0, read from 1

Then, there's no need to really pass fifos to start_routine.. unless you
want to :).

Or, alternatively you can possibly use kmalloc, if you anticipate some
dynamic size for your fifos array (I can't imagine why you would, but
hey.. you might!):

/* GLOBAL: */
unsigned int *fifos;

somewhere in init_module, before calling pthread_create_np:

/* make sure to #include <linux/malloc.h>, <linux/errno.h> */
if ( (fifos = kmalloc ( sizeof(unsigned int) * 2, GFP_KERNEL)) == NULL) {
        return -ENOMEM;
}

and cleanup_module should have the following somewhere:

if (fifos) kfree (fifos);


Personally, I would use the global myself, since you don't have to mess
with kmalloc and the like.  Of course, there always is a certain joy in
kmalloc'ing around..

-Calin

On Sun, 28 Oct 2001, Richard Reeve wrote:

> Hi,
>
> The problem is that fifos in init_modules whose pointer you are sending to
> start_routine is freed when you leave init_modules (around the time the
> start_routine thread is created), so the fifos you are using in
> start_routine is actually an unallocated bit of memory (or more likely it
> is allocated for something else during the pthread_wait_np since it is
> changing).
>
> If you want to pass the same fifos from init_module to start_routine, you
> need to explicitly allocate the memory, and then free in in the cleanup at
> the end, or use a global variable.
>
> Cheers,
>
> Richard Reeve.
>
> On Fri, 26 Oct 2001, Pablo Alvarez wrote:
>
> > Hi,
> >
> > I am starting out with rtLinux, and am having a funny problem in a simple
> > test program: it looks like after I call pthread_wait_np() for the first
> > time, the values of one of my variables change for no reason that I can see.
> > I can work around this if I have to, but I would rather not have unsolved
> > mysteries, so I would appreciate any advice. It seems likely that I just
> > don't understand some feature that I need to learn about.
> >
> > The module is supposed to write data to an rtfifo and will eventually read
> > from a second one, which is why I try to pass an array of two fifo numbers as
> > arguments to start_routine. The pointer to the array appears to remain
> > unchanged, but the values pointed to do change, although only once. Output
> > from dmesg is after the code.
> >
> > Here is the code. There are a lot of debugging rtl_printfs in it, so you can
> > see what happens with dmesg.
> >
> > #include <rtl.h>
> > #include <time.h>
> > #include <pthread.h>
> > #include <rtl_fifo.h>
> >
> > pthread_t thread;
> >
> > void * start_routine(void *arg)
> > {
> >   unsigned int *fifos = (unsigned int *) arg;
> >   unsigned short values[4] = {0,1,2,3};
> >   unsigned int counter = 0;
> >   struct sched_param p;
> >
> >   p . sched_priority = 1;
> >   pthread_setschedparam (pthread_self(), SCHED_FIFO, &p);
> >
> >   pthread_make_periodic_np (pthread_self(), gethrtime(), 500000000);//0.5
> > seconds
> >
> >   rtl_printf("pre-while fifo values are: %u write and %u
> > read\n",fifos[0],fifos[1]);
> >   rtl_printf("pre-while fifo pointer is: %lu\n", (unsigned long) fifos);
> >
> >   while (1)
> >     {
> >       rtl_printf("fifo values before pthread_wait_np() are: %u write and %u
> > read\n",fifos[0],fifos[1]);
> >       rtl_printf("fifo pointer before pthread_wait_np() is: %lu\n", (unsigned
> > long) fifos);
> >       ++counter;
> >       pthread_wait_np ();
> >       rtl_printf("fifo values after pthread_wait_np() are: %u write and %u
> > read\n",fifos[0],fifos[1]);
> >       rtl_printf("fifo pointer after pthread_wait_np() is: %lu\n", (unsigned
> > long) fifos);
> >       rtf_put(fifos[0],values,4*sizeof(unsigned short));
> >       rtf_put(0,&counter,sizeof(counter));
> >     }
> >   return 0;
> > }
> >
> > int init_module(void)
> > {
> >   unsigned int fifos[2] = {0,1};//write to 0, read from 1
> >   int i;
> >   for (i = 0; i < 2; ++i)
> >     {
> >       rtf_destroy(fifos[i]);
> >       rtl_printf("created rtfifo %d: %d\n", fifos[i],
> > rtf_create(fifos[i],2048));
> >     }
> >   return pthread_create (&thread, NULL, start_routine, (void *)fifos);
> > }
> >
> > void cleanup_module(void)
> > {
> >   unsigned int fifos[2] = {0,1};//write to 0, read from 1
> >   int i;
> >   for (i = 0; i < 2; ++i)
> >     {
> >       rtl_printf("destroyed rtfifo %d: %d\n", fifos[i],
> > rtf_destroy(fifos[i]));
> >     }
> >   pthread_delete_np (thread);
> > }
> >
> >
> > ---------------Output from dmesg -------------------------
> >
> > RTLinux Extensions Loaded (http://www.fsmlabs.com/)
> > created rtfifo 0: 0
> > created rtfifo 1: 0
> > pre-while fifo values are: 0 write and 1 read
> > pre-while fifo pointer is: 3372441380
> > fifo values before pthread_wait_np() are: 0 write and 1 read
> > fifo pointer before pthread_wait_np() is: 3372441380
> > fifo values after pthread_wait_np() are: 3550217344 write and 128 read
> > fifo pointer after pthread_wait_np() is: 3372441380
> > fifo values before pthread_wait_np() are: 3550217344 write and 128 read
> > fifo pointer before pthread_wait_np() is: 3372441380
> > fifo values after pthread_wait_np() are: 3550217344 write and 128 read
> > fifo pointer after pthread_wait_np() is: 3372441380
> > fifo values before pthread_wait_np() are: 3550217344 write and 128 read
> > fifo pointer before pthread_wait_np() is: 3372441380
> > destroyed rtfifo 0: 0
> > destroyed rtfifo 1: 0
> >
> > Thanks,
> >
> > Pablo Alvarez ([EMAIL PROTECTED])
> > -- [rtl] ---
> > To unsubscribe:
> > echo "unsubscribe rtl" | mail [EMAIL PROTECTED] OR
> > echo "unsubscribe rtl <Your_email>" | mail [EMAIL PROTECTED]
> > --
> > For more information on Real-Time Linux see:
> > http://www.rtlinux.org/
> >
> >
>
> -- [rtl] ---
> To unsubscribe:
> echo "unsubscribe rtl" | mail [EMAIL PROTECTED] OR
> echo "unsubscribe rtl <Your_email>" | mail [EMAIL PROTECTED]
> --
> For more information on Real-Time Linux see:
> http://www.rtlinux.org/
>

-- [rtl] ---
To unsubscribe:
echo "unsubscribe rtl" | mail [EMAIL PROTECTED] OR
echo "unsubscribe rtl <Your_email>" | mail [EMAIL PROTECTED]
--
For more information on Real-Time Linux see:
http://www.rtlinux.org/

Reply via email to