sclass SerializeComputations {
Map>> calculating = syncMap();
new MultiSetMap waitingFor;
B get(A a, IF1 f) {
ret get(a, () -> f.get(a));
}
B get(A a, IF0 f) {
bool meCalc;
Var> var;
synchronized {
var = calculating.get(a);
meCalc = var == null;
if (meCalc) {
var = new Var;
calculating.put(a, var);
} else
waitingFor.put(a, currentThread());
}
if (meCalc) {
OKOrError b = okOrError(() -> f!);
var.set(b);
ret b.getMandatory();
} else {
try {
waitUntilNotNull(var);
ret var->getMandatory();
} finally {
synchronized {
waitingFor.remove(a, currentThread());
if (!waitingFor.containsKey(a))
calculating.remove(a);
}
}
}
}
}