SF1603631:

concurrent modification of ROT (adding or removing thread tables) causes VM crash.  The simple fix is to sychronize all accessors to the ROT.  Performance impact has not been analyzed
This commit is contained in:
clay_shooter
2006-12-08 12:42:37 +00:00
parent 5ac71b76b4
commit 3b524074a6

View File

@@ -41,188 +41,192 @@ import java.util.WeakHashMap;
* Is [ 1116101 ] jacob-msg 0284 relevant??? * Is [ 1116101 ] jacob-msg 0284 relevant???
*/ */
public abstract class ROT { public abstract class ROT {
/** /**
* Manual garbage collection was the only option pre 1.9 * Manual garbage collection was the only option pre 1.9
*/ */
protected static final boolean USE_AUTOMATIC_GARBAGE_COLLECTION = protected static final boolean USE_AUTOMATIC_GARBAGE_COLLECTION =
"true".equalsIgnoreCase(System.getProperty("com.jacob.autogc")); "true".equalsIgnoreCase( System.getProperty( "com.jacob.autogc" ) );
/** /**
* A hash table where each element is another * A hash table where each element is another
* HashMap that represents a thread. * HashMap that represents a thread.
* Each thread HashMap contains the com objects created * Each thread HashMap contains the com objects created
* in that thread * in that thread
*/ */
private static HashMap rot = new HashMap(); private static HashMap rot = new HashMap();
/** /**
* adds a new thread storage area to rot * adds a new thread storage area to rot
* @return Map corresponding to the thread that this call was made in * @return Map corresponding to the thread that this call was made in
*/ */
protected static Map addThread() { protected synchronized static Map addThread() {
String t_name = Thread.currentThread().getName(); // should use the id here instead of the name because the name can be changed
if (rot.containsKey(t_name)){ String t_name = Thread.currentThread().getName();
// nothing to do if ( rot.containsKey( t_name ) ) {
} else { // nothing to do
Map tab = null; } else {
if (JacobObject.isDebugEnabled()){ Map tab = null;
JacobObject.debug("ROT: Automatic GC flag == "+ USE_AUTOMATIC_GARBAGE_COLLECTION if ( JacobObject.isDebugEnabled() ) {
);} JacobObject.debug( "ROT: Automatic GC flag == " + USE_AUTOMATIC_GARBAGE_COLLECTION );
if (!USE_AUTOMATIC_GARBAGE_COLLECTION){ }
tab = new HashMap(); if ( !USE_AUTOMATIC_GARBAGE_COLLECTION ) {
} else { tab = new HashMap();
tab = new WeakHashMap(); } else {
} tab = new WeakHashMap();
rot.put(t_name,tab); }
} rot.put( t_name, tab );
return getThreadObjects(false); }
} return getThreadObjects( false );
}
/**
/** * returns the pool for this thread if it exists. can create a new
* returns the pool for this thread if it exists. can create a new * one if you wish by passing in TRUE
* one if you wish by passing in TRUE * @param createIfDoesNotExist
* @param createIfDoesNotExist * @return Map the collection that holds the objects created in the current thread
* @return Map the collection that holds the objects created in the current thread */
*/ protected synchronized static Map getThreadObjects( boolean createIfDoesNotExist ) {
protected static Map getThreadObjects(boolean createIfDoesNotExist) { String t_name = Thread.currentThread().getName();
String t_name = Thread.currentThread().getName(); if ( !rot.containsKey( t_name ) && createIfDoesNotExist ) {
if (!rot.containsKey(t_name) && createIfDoesNotExist){ addThread();
addThread(); }
} return (Map) rot.get( t_name );
return (Map)rot.get(t_name); }
}
/**
* Iterates across all of the entries in the Hashmap in the rot
* that corresponds to this thread.
* This calls safeRelease() on each entry and then
* clears the map when done and removes it from the rot.
* All traces of this thread's objects will disapear.
* This is called by COMThread in the tear down and provides a
* synchronous way of releasing memory
*/
protected static void clearObjects() {
Map tab = getThreadObjects(false);
if (JacobObject.isDebugEnabled()){
JacobObject.debug("ROT: "+rot.keySet().size()+" thread tables exist");
}
if (tab != null){
if (JacobObject.isDebugEnabled()){
JacobObject.debug("ROT: "+tab.keySet().size()+ " objects to clear in this thread " );
}
// walk the values
Iterator it;
if (USE_AUTOMATIC_GARBAGE_COLLECTION){
it = tab.keySet().iterator();
} else {
it = tab.values().iterator();
}
while (it.hasNext()) {
JacobObject o = (JacobObject) it.next();
if (o != null
// can't use this cause creates a Variant if calling SafeAray
// and we get an exceptin modifying the collection while iterating
// && o.toString() != null
){
if (JacobObject.isDebugEnabled()){
if (o instanceof SafeArray){
// SafeArray create more objects when calling toString()
// which causes a concurrent modification exception in HashMap
JacobObject.debug("ROT: removing "+o.getClass().getName());
} else {
// Variant toString() is probably always bad in here
JacobObject.debug("ROT: removing "+o.hashCode()+"->"+o.getClass().getName());
}
}
o.safeRelease();
}
// used to be an iterator.remove() but why bother when we're nuking them all anyway?
}
// empty the collection
tab.clear();
// remove the collection from rot
rot.remove(Thread.currentThread().getName());
if (JacobObject.isDebugEnabled()){
JacobObject.debug("ROT: thread table cleared and removed");
}
} else {
if (JacobObject.isDebugEnabled()){ JacobObject.debug("ROT: nothing to clear!");}
}
}
/** /**
* generates the key used to insert object into the thread's list of objects. * Iterates across all of the entries in the Hashmap in the rot
* @param targetMap the map we need the key for. Used to make sure we create a compatabile key * that corresponds to this thread.
* @param o JacobObject we need key for * This calls safeRelease() on each entry and then
* @return some key compatabile with hashmaps * clears the map when done and removes it from the rot.
*/ * All traces of this thread's objects will disapear.
protected static Object getMapKey(Map targetMap, JacobObject o){ * This is called by COMThread in the tear down and provides a
if (targetMap instanceof WeakHashMap){ * synchronous way of releasing memory
return o; */
} else { protected synchronized static void clearObjects() {
return new Integer(o.hashCode()); Map tab = getThreadObjects( false );
} if ( JacobObject.isDebugEnabled() ) {
} JacobObject.debug( "ROT: " + rot.keySet().size() + " thread tables exist" );
}
/** if ( tab != null ) {
* generates the object used to insert object into the thread's list of objects. if ( JacobObject.isDebugEnabled() ) {
* @param targetMap the map we need the key for. Used to make sure we create a compatabile key JacobObject.debug( "ROT: " + tab.keySet().size() + " objects to clear in this thread " );
* @param o JacobObject we need key for }
* @return some compatabile with hashmaps (null for weak has map) // walk the values
*/ Iterator it;
protected static Object getMapValue(Map targetMap, JacobObject o){ if ( USE_AUTOMATIC_GARBAGE_COLLECTION ) {
if (targetMap instanceof WeakHashMap){ it = tab.keySet().iterator();
return null; } else {
} else { it = tab.values().iterator();
return o; }
} while ( it.hasNext() ) {
} JacobObject o = (JacobObject) it.next();
if ( o != null
/** // can't use this cause creates a Variant if calling SafeAray
* @deprecated the java model leave the responsibility of clearing up objects // and we get an exceptin modifying the collection while iterating
* to the Garbage Collector. Our programming model should not require that the // && o.toString() != null
* user specifically remove object from the thread. ) {
* if ( JacobObject.isDebugEnabled() ) {
* This will remove an object from the ROT if ( o instanceof SafeArray ) {
* @param o // SafeArray create more objects when calling toString()
*/ // which causes a concurrent modification exception in HashMap
protected static void removeObject(JacobObject o) { JacobObject.debug( "ROT: removing " + o.getClass().getName() );
String t_name = Thread.currentThread().getName(); } else {
Map tab = (Map) rot.get(t_name); // Variant toString() is probably always bad in here
if (tab != null) { JacobObject.debug( "ROT: removing " + o.hashCode() + "->" + o.getClass().getName() );
tab.remove(getMapKey(tab,o)); }
} }
o.safeRelease(); o.safeRelease();
} }
// used to be an iterator.remove() but why bother when we're nuking them all anyway?
/** }
* adds an object to the HashMap for the current thread // empty the collection
* @param o tab.clear();
*/ // remove the collection from rot
protected static void addObject(JacobObject o) { rot.remove( Thread.currentThread().getName() );
Map tab = getThreadObjects(false); if ( JacobObject.isDebugEnabled() ) {
if (tab == null) { JacobObject.debug( "ROT: thread table cleared and removed" );
// this thread has not been initialized as a COM thread }
// so make it part of MTA for backwards compatibility } else {
ComThread.InitMTA(false); if ( JacobObject.isDebugEnabled() ) {
tab = getThreadObjects(true); JacobObject.debug( "ROT: nothing to clear!" );
} }
if (JacobObject.isDebugEnabled()){ }
JacobObject.debug( }
"ROT: adding "+o+"->"+o.getClass().getName() +
" table size prior to addition:" +tab.size());}
if (tab != null) {
tab.put(getMapKey(tab,o),getMapValue(tab,o));
}
}
/** /**
* ROT can't be a subclass of JacobObject because of the way ROT pools are managed * generates the key used to insert object into the thread's list of objects.
* so we force a DLL load here by referncing JacobObject * @param targetMap the map we need the key for. Used to make sure we create a compatabile key
*/ * @param o JacobObject we need key for
static { * @return some key compatabile with hashmaps
LibraryLoader.loadJacobLibrary(); */
} protected static Object getMapKey( Map targetMap, JacobObject o ) {
if ( targetMap instanceof WeakHashMap ) {
} return o;
} else {
return new Integer( o.hashCode() );
}
}
/**
* generates the object used to insert object into the thread's list of objects.
* @param targetMap the map we need the key for. Used to make sure we create a compatabile key
* @param o JacobObject we need key for
* @return some compatabile with hashmaps (null for weak has map)
*/
protected static Object getMapValue( Map targetMap, JacobObject o ) {
if ( targetMap instanceof WeakHashMap ) {
return null;
} else {
return o;
}
}
/**
* @deprecated the java model leave the responsibility of clearing up objects
* to the Garbage Collector. Our programming model should not require that the
* user specifically remove object from the thread.
*
* This will remove an object from the ROT
* @param o
*/
protected synchronized static void removeObject( JacobObject o ) {
String t_name = Thread.currentThread().getName();
Map tab = (Map) rot.get( t_name );
if ( tab != null ) {
tab.remove( getMapKey( tab, o ) );
}
o.safeRelease();
}
/**
* adds an object to the HashMap for the current thread
* @param o
*/
protected synchronized static void addObject( JacobObject o ) {
Map tab = getThreadObjects( false );
if ( tab == null ) {
// this thread has not been initialized as a COM thread
// so make it part of MTA for backwards compatibility
ComThread.InitMTA( false );
tab = getThreadObjects( true );
}
if ( JacobObject.isDebugEnabled() ) {
JacobObject.debug(
"ROT: adding " + o + "->" + o.getClass().getName() +
" table size prior to addition:" + tab.size() );
}
if ( tab != null ) {
tab.put( getMapKey( tab, o ), getMapValue( tab, o ) );
}
}
/**
* ROT can't be a subclass of JacobObject because of the way ROT pools are managed
* so we force a DLL load here by referncing JacobObject
*/
static {
LibraryLoader.loadJacobLibrary();
}
}