Skip to content

Commit

Permalink
sys: Add double checked locking into ClassType.java reflect and finish
Browse files Browse the repository at this point in the history
  • Loading branch information
briansfrank committed Jun 25, 2024
1 parent 40d3839 commit 7f2bb88
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
1 change: 1 addition & 0 deletions src/doc/docIntro/doc/ChangeLog.fandoc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
- Add new SqlConnPool API
- Support for static once methods
- StrBuf.addRange
- Add double checked locking into ClassType.java reflect and finish

*Build 1.0.80 (23 Apr 2024)*
- New ECMA class-based JavaScript design
Expand Down
24 changes: 15 additions & 9 deletions src/sys/java/fan/sys/ClassType.java
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,9 @@ public void encode(ObjEncoder out)

protected final ClassType reflect()
{
// short circuit if already reflected using double-checked locking
if (slotsByName != null) return this;

synchronized(lock())
{
// short circuit if already reflected
Expand All @@ -279,14 +282,14 @@ protected final ClassType reflect()
if (Debug) System.out.println("-- reflect: " + qname + " " + slotsByName);

// do it
doReflect();
slotsByName = doReflect();

// return this
return this;
}
}

protected void doReflect()
private HashMap doReflect()
{
// if the ftype is non-null, that means it was passed in non-hollow
// ftype (in-memory compile), otherwise we need to read it from the pod
Expand Down Expand Up @@ -341,11 +344,11 @@ protected void doReflect()
this.slots = slots.trim();
this.fields = fields.trim();
this.methods = methods.trim();
this.slotsByName = nameToSlot;
this.myFacets = Facets.mapFacets(pod, ftype.attrs.facets);
this.lineNum = ftype.attrs.lineNum;
this.sourceFile = ftype.attrs.sourceFile;

this.lineNum = ftype.attrs.lineNum;
this.sourceFile = ftype.attrs.sourceFile;
return nameToSlot;
}

/**
Expand Down Expand Up @@ -537,6 +540,7 @@ public void precompiled(Class cls)
*/
public void finish()
{
if (finished) return;
synchronized (lock())
{
if (finished) return;
Expand All @@ -545,14 +549,15 @@ public void finish()
// ensure reflected and emitted
reflect();
emit();
finished = true;

// map Java members to my slots for reflection; if
// mixin then we do this for both the interface and
// the static methods only of the implementation class
finishSlots(cls, false);
if (isMixin()) finishSlots(auxCls, true);

finished = true;

/*
System.out.println("---- Finish " + qname() + " cls=" + cls);
try
Expand Down Expand Up @@ -721,7 +726,7 @@ boolean checkAllFan(Class[] params)
List fields;
List methods;
List slots;
HashMap slotsByName; // String:Slot
volatile HashMap slotsByName; // String:Slot
Facets myFacets;
Facets inheritedFacets; // handled in loadFacets

Expand All @@ -730,10 +735,11 @@ boolean checkAllFan(Class[] params)
Class auxCls; // implementation Java class if mixin

// flags to ensure we finish only once
boolean finished;
volatile boolean finished;
String finishing;

// misc
boolean javaRepr; // if representation a Java type, such as java.lang.Long

}
}

0 comments on commit 7f2bb88

Please sign in to comment.