[PATCH]: natThread.cc: Destroy join sync objects in finalizer

Bryce McKinlay mckinlay@redhat.com
Wed Sep 15 15:56:00 GMT 2004


Mohan Embar wrote:

>>Yes, this would be cleaner, but we'd need to be careful that 
>>Thread.join() etc don't try to access the native state after it has been 
>>freed. To avoid race problems here we could perhaps register the 
>>finializer on the native state (natThread) itself rather than the 
>>java.lang.Thread, and atomically release it (set "thread->nt" to null) 
>>when the thread dies.
>>    
>>
>
>Believe it or not, my initial iteration attempted to clean up the native
>state when the thread died, but:
>
>- I didn't know what to synchronize on.
>- I figured it would require synchronizing on something every time we
>  needed to access the native state, which would unnecessary penalize
>  everyone because of the fringe case of troublemakers trying to
>  resurrect a thread.
>  
>

Right, with my scheme above, no extra synchronization would be needed. 
It would still be safe to synchronize on the native state (join_mutex, 
etc), if thread->nt is non-null. The native state would not be destroyed 
until its finializer is run, so if a thread sees it as non-null, there 
is no danger of a race.

And, by registering the finalizier on the native state rather than the 
Thread object, we avoid conflicts with user finalizers.

Bryce



More information about the Java-patches mailing list