-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Explorations in adding atomic compare and swap
VarHandles were introduced in openjdk9, so the code needs to be conditionalized somehow. It is currently being developed on openjdk17. <#92>
- Loading branch information
Showing
3 changed files
with
48 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package org.armedbear.lisp; | ||
|
||
import static org.armedbear.lisp.Lisp.*; | ||
|
||
import java.lang.invoke.MethodHandles; | ||
import java.lang.invoke.VarHandle; | ||
|
||
public class Atomic | ||
{ | ||
static MethodHandles.Lookup Lookup | ||
= MethodHandles.lookup(); | ||
|
||
static boolean compareAndSwap(LispObject place, LispObject expectedValue, LispObject newValue) { | ||
VarHandle vh = null; | ||
if (place instanceof Fixnum) { | ||
try { | ||
vh = Lookup.findVarHandle(Fixnum.class, "value", int.class); | ||
} catch (ReflectiveOperationException e) { | ||
java_error(e); | ||
return false; // unreached | ||
} | ||
} | ||
if (vh == null) { | ||
simple_error("Unable to find VarHandle for place ~a", place); | ||
return false; // unreached | ||
} | ||
return vh.compareAndSet(expectedValue, newValue); | ||
} | ||
|
||
public static final Primitive _CAS = new pf_cas(); | ||
@DocString(name="%cas", | ||
args="place expected-value new-value", | ||
returns="generalized boolean") | ||
private static final class pf_cas extends Primitive { // Maybe a special operator? Or do that Lisp-side? | ||
pf_cas() { | ||
super(Symbol._CAS); | ||
} | ||
@Override | ||
public LispObject execute(LispObject place, LispObject expectedValue, LispObject newValue) { | ||
boolean result = compareAndSwap (place, expectedValue, newValue); | ||
return (result == false) ? NIL : T; // TODO this has to exist somewhere as a static method | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters