comparison truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/NodeUtil.java @ 22080:65e9fbb40e51

avoid publishing unadopted children in node replacement to fix potential race
author Andreas Woess <andreas.woess@oracle.com>
date Thu, 13 Aug 2015 18:22:22 +0200
parents b2d1c8ff592a
children dc83cc1f94f2
comparison
equal deleted inserted replaced
22079:14ee5048c76e 22080:65e9fbb40e51
197 oldNode.replaceHelper(newNode, reason); 197 oldNode.replaceHelper(newNode, reason);
198 return newNode; 198 return newNode;
199 } 199 }
200 200
201 public static boolean replaceChild(Node parent, Node oldChild, Node newChild) { 201 public static boolean replaceChild(Node parent, Node oldChild, Node newChild) {
202 return replaceChild(parent, oldChild, newChild, false);
203 }
204
205 static boolean replaceChild(Node parent, Node oldChild, Node newChild, boolean adopt) {
202 CompilerAsserts.neverPartOfCompilation(); 206 CompilerAsserts.neverPartOfCompilation();
203 NodeClass nodeClass = parent.getNodeClass(); 207 NodeClass nodeClass = parent.getNodeClass();
204 208
205 for (NodeFieldAccessor nodeField : nodeClass.getChildFields()) { 209 for (NodeFieldAccessor nodeField : nodeClass.getChildFields()) {
206 if (nodeField.getObject(parent) == oldChild) { 210 if (nodeField.getObject(parent) == oldChild) {
207 assert assertAssignable(nodeField, newChild); 211 assert assertAssignable(nodeField, newChild);
212 if (adopt) {
213 parent.adoptHelper(newChild);
214 }
208 nodeField.putObject(parent, newChild); 215 nodeField.putObject(parent, newChild);
209 return true; 216 return true;
210 } 217 }
211 } 218 }
212 219
215 if (arrayObject != null) { 222 if (arrayObject != null) {
216 Object[] array = (Object[]) arrayObject; 223 Object[] array = (Object[]) arrayObject;
217 for (int i = 0; i < array.length; i++) { 224 for (int i = 0; i < array.length; i++) {
218 if (array[i] == oldChild) { 225 if (array[i] == oldChild) {
219 assert assertAssignable(nodeField, newChild); 226 assert assertAssignable(nodeField, newChild);
227 if (adopt) {
228 parent.adoptHelper(newChild);
229 }
220 array[i] = newChild; 230 array[i] = newChild;
221 return true; 231 return true;
222 } 232 }
223 } 233 }
224 } 234 }