Hi Dirk,

Pointers should be passed the other way around: from RT-threads to the
Linux task. As it is, ptr_b points to a virtual address in some random
user-space task that happened to be current. One way to solve this
problem is to allocate a block of memory in your RT-module, then pass 
a pointer (e.g., via an RT-FIFO) to your Linux task, and allocate
the object in this area by redefining operator::new.

Michael.

Dirk Pohl [EIT] ([EMAIL PROTECTED]) wrote:
> Hello,
> I created an object in my linux-application. Now I want to call
> memberfunctions of this object from RT-Threads. I included the files from
> the examples/cpp directory, defined a pointer on the object an did this
> __do_global_ctors thing. From the linux application I start the
> RT-Thread like in the examples/frank example and hand the pointer to
> the object down through a FIFO. The RT-Thread then calls
> the function (ptr_b -> calculate()), incrementing the object variable. 
> Meanwhile, my application just counts to 10^9 doing nothing else,  and I
> can watch the thread counting (it sends the actual value to console). 
> THIS ALL WORKS FINE!
> Now I tried to also print the actual value in my linux-application, while
> the thread runs. AND THEN: the thread suddenly stops running (at
> different points: once it counts to 7, to 15, to 8 ...). The variable
> is not incremented any more.
> 
> Here are some cutouts (which I hope are important ...) from my programs:
> [CObjekt.cpp] 
> CObjekt::CObjekt() { a=1; b=1; }
> CObjekt::~CObjekt() {}
> void CObjekt::calculate(void) { a += b; }
> int CObjekt::show(void) { return a; }
> 
> [rt_app.cpp] RTLinux module
>  
> CObjekt* ptr_b; 
> pthread_t thread;
>  
> void * huelle (void* arg) {
>   struct sched_param p;
>   int a;
>  
>   p.sched_priority = 1;
>   pthread_setschedparam (pthread_self(), SCHED_FIFO, &p);
>   while (1) {
>     pthread_wait_np (); 
>     ptr_b -> calculate();
>     a = ptr_b -> show();
>     rtl_printf("Result: %d\n", a);
>   }
>   return 0; 
> }
>  
> int command_handler(unsigned int fifo) {
>   struct my_msg_struct msg;
>   int err;
>  
>   while ((err = rtf_get(1, &msg, sizeof(msg))) == sizeof(msg)) {
>     switch (msg.command) {
>     case START_TASK:
>       ptr_b = msg.data;
>       pthread_make_periodic_np(thread, gethrtime(),100000000);
>       break;
>     case STOP_TASK:
>       pthread_suspend_np(thread);
>       break;
>     }
>   }
>   return 0;
> } 
> [init_module omitted]
> [cleanup omitted]
> 
> main_app.cpp //linux application 
> int main(void) {
>   CObjekt * ptr_a = new CObjekt; // Objekt instanciated
>   int fd0;
>   struct my_msg_struct msg;
>   int i;
>  
>   if ((fd0 = open("/dev/rtf1", O_WRONLY)) < 0 ) {
>     exit(1);
>   }
>   msg.command = START_TASK;
>   msg.data = ptr_a;
>   if (write(fd0, &msg, sizeof(msg)) < 0) {
>   }
>   for (i=0; i<=1000000000; i++) {     // if I leave this block empty 
>     if ( (i%1000000) == 0) {          // all is ok
>       cout << ptr_a -> show() << "\n"; // if I access the variable
>     }                                 // thread hangs up
>   }
>   msg.command = STOP_TASK;
>   if (write(fd0, &msg, sizeof(msg)) < 0) {
>   }
>   fprintf(stdout,"Value: %d\n", ptr_a->show());
>   delete ptr_a;
>   return close(fd0);
> }    
> 
> I'd appreciate any help, comments, etc. since I'm new to all of this.
> ---
> Dirk Pohl
> University of Kaiserslautern
> 
> 
> 
> -- [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/rtlinux/
-- [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/rtlinux/

Reply via email to