On Thu, Mar 22, 2001 at 08:58:58AM +0530, Kalyanaraman, Sivakumar (CTS) wrote:
> Hi all,
>        Is it possible to dynamically spawn threads from RTLinux/RTAI ? The
> documentation for pthread_create in RTlinux states that the function can be
> called only in kernel mode. Is there any other way ?

It's very simple to do this. If there is a request, I will post a working
example.
The basic idea is

typedef   *(*start_routine)(void *) assign_func_t; // function to run.
assign_func_t myfunc[MAX_DYNAMIC_THREADS];
pthread_t t[MAX_DYNAMIC_THREADS];
#define EXITVALUE (assign_func_t)-1
#define IDLEVALUE (assign_func_t)1  // this is never a usable pointer value
#define BUSY (assign_func_t)0  // start busy


module_init:
        for(i=0; i < MAX_DYNAMIC_THREADS; i++)
                pthread_create(&t[i],waitaround,i,0);


and then

void waitaround(void *i){
        int x; //for saving irq state.
        int j = (int)i;
       while(myfunc[j] != EXITVALUE){
                if(myfunc[j] == BUSY){ // we are BUSY going to sleep
                        rtl_disable_interrupts(x);
                        myfunc[j] = IDLEVALUE;
                        pthread_suspend_np(pthread_self());
                        rtl_restore_interrupts(x);
                }
                else {
                     myfunc[j]();
                     }
                myfunc[j] = BUSY;

      }

int dynamic_create(assign_funct_t f){
      for(i=0 ; i < MAX_DYNAMIC_THREADS; i++)
                if(myfunc[i] == IDLEVALUE){
                        myfunc[j] = f;
                        pthread_wakeup_np(t[j]);
                        return j;
                   }

        return -1;
}


This is not smp safe, but it should work.



                
> 
> regards,
> siva
> 
> "Believe that life is worth living and your belief will help create the
> fact."
> 

> This e-mail and any files transmitted with it are for the sole use of the intended 
>recipient(s) and may contain confidential and privileged information.
> If you are not the intended recipient, please contact the sender by reply e-mail and 
>destroy all copies of the original message. 
> Any unauthorised review, use, disclosure, dissemination, forwarding, printing or 
>copying of this email or any action taken in reliance on this e-mail is strictly 
> prohibited and may be unlawful.
> 
>               Visit us at http://www.cognizant.com


-- 
---------------------------------------------------------
Victor Yodaiken 
Finite State Machine Labs: The RTLinux Company.
 www.fsmlabs.com  www.rtlinux.com

-- [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