This is the mail archive of the ecos-discuss@sources.redhat.com mailing list for the eCos project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Seeking Thread join / waitpid equivalent.


On Fri, Aug 27, 2004 at 12:02:00PM +1200, John Carter wrote:
> We have just been bitten by the "Don't free the thread into in the 
> destructor" problem as discussed so comprehensively by Ivan Horvat in 
> April.
>  http://sources.redhat.com/ml/ecos-discuss/2004-04/msg00122.html
> 
> Thanks Ivan!
> 
> Unfortunately the thread exiting is explicitly our lowest priority 
> thread, so Reaper in a mbox trick won't work.

I think the cyg_thread destruction question above is different from the
thread.join question below.

> However, in languages such as Ruby, you have the nifty thread.join 
> primitive. With processes under posix systems you have waitpid.

> Isn't there something like that in ecos, where an arbitrary thread may wait 
> for another thread to exit?

I dealt with this by hand, by applying a thin veneer of plastic wrap to
the eCos thread API.  What it did was associate a death_semaphore with
each my thread objects.  The cyg_thread_entry function I gave to eCos
looked something like:

	void
	thread_entry_function(cyg_addrword_t data)
	{
	    struct myThreadType *thread = (struct myThreadType*)data;
	    (*thread->entry_function)(thread->data);
	    cyg_semaphore_post(&thread->death_semaphore);
	}

So thread_join() was implemented as a wait() on that thread's
death_semaphore:

	void
	thread_join(struct myThreadType *thread)
	{
	    cyg_semaphore_wait(&thread->death_semaphore);
	}

Obviously nobody's trying to join() your lowest priority thread, so
the post would go unnoticed.

Semaphores aren't the best sync primative for this, but that's what I
did.  If I redo it, I'll probably use condition variables or event flags.

So I have intercepted the death of the thread by wrapping the
entry_function.  If you wanted to dig into Cyg_Thread::exit() and
put the post() in there, I guess it would be more seamless, but
then you're changing eCos code, which I try to avoid.

Just my 0.02


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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]