Permalink
Browse files

use CHM.putIfAbsent instead of explicit synchronization

  • Loading branch information...
1 parent 61d5f02 commit b008115e9068e3e4084b1e83b7d70375f8072010 @charliesome committed Jun 11, 2013
Showing with 14 additions and 5 deletions.
  1. +14 −5 src/org/jruby/Ruby.java
View
@@ -4181,13 +4181,22 @@ public int getConstantGeneration() {
return -1;
}
- public synchronized Invalidator getConstantInvalidator(String constantName) {
+ public Invalidator getConstantInvalidator(String constantName) {
Invalidator invalidator = constantNameInvalidators.get(constantName);
- if (invalidator == null) {
- invalidator = OptoFactory.newConstantInvalidator();
- constantNameInvalidators.put(constantName, invalidator);
+ if (invalidator != null) {
+ return invalidator;
+ } else {
+ return addConstantInvalidator(constantName);
+ }
}
- return invalidator;
+
+ private Invalidator addConstantInvalidator(String constantName) {
+ Invalidator invalidator = OptoFactory.newConstantInvalidator();
+ constantNameInvalidators.putIfAbsent(constantName, invalidator);
+
+ // fetch the invalidator back from the ConcurrentHashMap to ensure that
+ // only one invalidator for a given constant name is ever used:
+ return constantNameInvalidators.get(constantName);
}
public Invalidator getCheckpointInvalidator() {

0 comments on commit b008115

Please sign in to comment.