Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions core/src/main/java/fj/P1.java
Original file line number Diff line number Diff line change
Expand Up @@ -268,19 +268,18 @@ public static <A> P1<A> memo(F0<A> f) {
}

static class Memo<A> extends P1<A> {
private final P1<A> self;
private volatile boolean initialized;
private volatile P1<A> self;
Copy link
Member

@jbgi jbgi May 26, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self need not be volatile because the memory barrier is already done via initialized.
But thinking of it, you could let self be volatile and remove initialized altogether, replacing the !initialized check by self != null. WDYT?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good to me.

private A value;

Memo(P1<A> self) { this.self = self; }

@Override public final A _1() {
if (!initialized) {
if (self != null) {
synchronized (this) {
if (!initialized) {
if (self != null) {
A a = self._1();
value = a;
initialized = true;
self = null;
return a;
}
}
Expand Down