Skip to content

Commit

Permalink
Synchronize update of shared genotype likelihood tables. (#5071)
Browse files Browse the repository at this point in the history
* Synchronize update of shared genotype likelihood tables.

* Add comment explaining synchronization.

* Synchronize for reads as well as writes to shared state.
  • Loading branch information
tomwhite authored and droazen committed Oct 3, 2018
1 parent 50e0185 commit fad6eb8
Showing 1 changed file with 7 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
import java.util.Arrays;

/**
* Genotype likelihood calculator utility.
* Genotype likelihood calculator utility. This class is thread-safe since access to shared mutable state is
* synchronized.
*
* <p>
* This class provide genotype likelihood calculators with any number of alleles able given an arbitrary ploidy and allele
Expand Down Expand Up @@ -257,7 +258,7 @@ private static GenotypeAlleleCounts[] buildGenotypeAlleleCountsArray(final int p
*
* @return never {@code null}.
*/
public GenotypeLikelihoodCalculator getInstance(final int ploidy, final int alleleCount) {
public synchronized GenotypeLikelihoodCalculator getInstance(final int ploidy, final int alleleCount) {
checkPloidyAndMaximumAllele(ploidy, alleleCount);

if (calculateGenotypeCountUsingTables(ploidy, alleleCount) == GENOTYPE_COUNT_OVERFLOW) {
Expand All @@ -270,17 +271,16 @@ public GenotypeLikelihoodCalculator getInstance(final int ploidy, final int alle
}

/**
* Update of shared tables
* Update of shared tables.
*
* @param requestedMaximumAllele the new requested maximum allele maximum.
* @param requestedMaximumPloidy the new requested ploidy maximum.
*/
private void ensureCapacity(final int requestedMaximumAllele, final int requestedMaximumPloidy) {
private synchronized void ensureCapacity(final int requestedMaximumAllele, final int requestedMaximumPloidy) {

final boolean needsToExpandAlleleCapacity = requestedMaximumAllele > maximumAllele;
final boolean needsToExpandPloidyCapacity = requestedMaximumPloidy > maximumPloidy;

// Double check with the lock on to avoid double work.
if (!needsToExpandAlleleCapacity && !needsToExpandPloidyCapacity) {
return;
}
Expand Down Expand Up @@ -380,12 +380,9 @@ public static int computeMaxAcceptableAlleleCount(final int ploidy, final int ma
throw new GATKException("Code should never reach here.");
}


private int calculateGenotypeCountUsingTables(int ploidy, int alleleCount) {
private synchronized int calculateGenotypeCountUsingTables(int ploidy, int alleleCount) {
checkPloidyAndMaximumAllele(ploidy, alleleCount);
if (ploidy > maximumPloidy || alleleCount > maximumAllele) {
ensureCapacity(alleleCount, ploidy);
}
ensureCapacity(alleleCount, ploidy);
return alleleFirstGenotypeOffsetByPloidy[ploidy][alleleCount];
}
}

0 comments on commit fad6eb8

Please sign in to comment.