[ECOS] Re: Per Thread Destructors Question

Ivan Horvat ihorvat@xylon.hr
Sat Jan 31 14:27:00 GMT 2004


Hi.

> I am writing an application that dynamically creates threads. After
> consulting the documentation, it is not clear if per thread destructors
> [created via cyg_thread_add_destructor()] can be used to do a
> cyg_thread_delete() on one's own exited thread and follow that up with a
> free() of the thread stack, etc?


The destructors are called from Cyg_Thread::exit(), which looks like this:

void Cyg_Thread::exit(){

    Cyg_Thread *self = Cyg_Thread::self();

    cyg_ucount16 i;
    for (i=0; i<CYGNUM_KERNEL_THREADS_DESTRUCTORS; i++) {
       if (NULL != self->destructors[i].fn) {
          destructor_fn fn = self->destructors[i].fn;
          CYG_ADDRWORD data = self->destructors[i].data;
          fn(data);
       }
    }
    .....

So, there are two problems:

   1. when thread is trying to delete itself
   2. cleaning the stack

When You attempt 1., cyg_thread_delete() calls thread->kill() method, 
which will
return upon detecting that it is "killing itself":

void Cyg_Thread::kill() {

    // If this is called by the current thread on itself,
    // just call exit(), which is what he should have done
    // in the first place.
    if( this == Cyg_Scheduler::get_current_thread() )
        exit();
    .....

The exit() method is running on the thread stack, so if You try 2. to 
free it in per-thread
destructors, there is chance to crash the system (destroying the ground 
under Your feet).

> In other words, does the destructor run in the context of the thread itself
> or as part of kernel after the thread itself has exited - such that all
> thread context can be free'd.

The destructor is running in the context of the thread that called 
exit(), so it can't be used
to free the thread "context".

> If this procedure is not legal what is the recommended way to allow all such
> resources to be reclaimed on thread exit.

The question is what is the most elegant way to do it. :-) There are 
some proposals in the
"Threads on ecos" confirmation needed... message and follow-ups...


Regards,
Ivan.




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



More information about the Ecos-discuss mailing list