Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Support for Atomic Operators (like CAS, etc.) #92

Open
Shinmera opened this issue Jan 24, 2019 · 18 comments
Open

Add Support for Atomic Operators (like CAS, etc.) #92

Shinmera opened this issue Jan 24, 2019 · 18 comments
Assignees

Comments

@Shinmera
Copy link

It would be great if ABCL offered support for CAS and other atomic operators in order to allow the implementation of lock-free datastructures and algorithms.

A variety of implementations already offer support for operations like cas, atomic-incf, atomic-push, and atomic-pop. For instance, both ECL and SBCL follow the protocol described in the SBCL manual.

I've accumulated the information for atomic operator support in a new portability library called Atomics. For ABCL to be supported, it would need to offer CAS on at least svref, car, cdr, and structure accessor places. Naturally it would be great if it offered more than that still, but those are the most fundamental.

As for how to achieve this: since I don't know anything about ABCL internals I can't say too much, though Java itself offers support for CAS through sun.misc.Unsafe and java.util.concurrent.atomic.*.

@Shinmera Shinmera changed the title Add Support for Compare-And-Swap Add Support for Atomic Operators (like CAS, etc.) Jan 24, 2019
@easye
Copy link
Collaborator

easye commented Jan 24, 2019

@Shinmera I'll take a look at what we need to do. I have some notes from a couple years back that I need to dig up…

@Slids
Copy link

Slids commented Jan 1, 2020

So I did a little looking.
The obvious thing to do is use varhandles (and not use unsafe as it may go way):
https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/invoke/VarHandle.html

Sadly this requires targeting 1.7 and not 1.6

@easye
Copy link
Collaborator

easye commented Jan 2, 2020

ABCL 2 will probably targets Java 8 (neé 1.8) as the lowest JVM supported, so we can add "true CAS operations" as part of its feature list.

Thanks for looking into this.

@Slids
Copy link

Slids commented Jan 2, 2020

I'd be happy to work on this bug but I've no idea when abcl 2 is being release, so if you have some method of notification please drop me a message.

@easye
Copy link
Collaborator

easye commented Jun 6, 2020

ABCL 2 is scheduled for September 2020, when I intend to release a version targeting openjdk8

@Slids I guess I should fork ABCL now to give ya a place to add patches. Stay tuned…

https://abcl.org/trac/milestone/2.0.0

@easye
Copy link
Collaborator

easye commented Jun 7, 2020

boolean compareAndSet(expectedValue, updateValue)

is the basic operation on Java Atomics. It was introduced in openjdk8, but I wonder if it is possible to synthesize a version in pure Java that would work on openjdk6? Then we could add a compatibility layer to back-port.

@Slids
Copy link

Slids commented Jun 7, 2020 via email

@easye
Copy link
Collaborator

easye commented Jun 8, 2020

The real problem is with calling compare and set on vectors, you have to look at the type possibilities.

Having my paws bloody overhauling our byte vectors with java.nio.ByteBuffer makes this a problem that I would be ready to tackle. @Slids Do you have some sort of reference to or implementation of the efficient algorithms for compare and set on sequence types that I could perhaps study a bit? I have naive ideas of how to approach this, so it would probably be best if I educated myself a bit more on the prior art here.

@Slids
Copy link

Slids commented Jun 12, 2020

I would avoid any self implementation and just sue the java concurrent libraries.
Have to find the ones I was using though...

@easye
Copy link
Collaborator

easye commented Oct 6, 2020

https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/invoke/VarHandle.html

Actually this requires openjdk9, which we don't support, so the current target of minimum openjdk8 for the next ABCL is not sufficient if we wish to efficiently implement the necessary semantics on any memory that a cons can reference.

So, the question becomes: it is ok if ABCL2 drops openjdk8 support, and only supports openjdk11?

Discussion https://mailman.common-lisp.net/pipermail/armedbear-devel/2020-October/004143.html

@alessiostalla
Copy link
Collaborator

alessiostalla commented Oct 6, 2020 via email

@easye
Copy link
Collaborator

easye commented Oct 6, 2020 via email

@alessiostalla
Copy link
Collaborator

alessiostalla commented Oct 7, 2020 via email

@easye
Copy link
Collaborator

easye commented Oct 7, 2020

I mean, to use VarHandles, doesn't the classfile version have to be quite recent?

Hmm. Not sure, as it depends if we would have to change our compiler. The Java code of the ABCL gets compiled with javac against the JDK platform libraries, so if we were able to replace LispObject purely Java-side, we wouldn't necessarily need to change our compiler.

But modernizing the compiler output is certainly one of the high-priority tasks with the putative ABCL2 codebase.

@alessiostalla
Copy link
Collaborator

alessiostalla commented Oct 7, 2020 via email

@davidsun0
Copy link

Would using AtomicReferenceFieldUpdater work? It wouldn't require updating to a newer Java version (available since Java 5) and it wouldn't require changing the compiler.

The downside is that fields like car and cdr would have to be declared volatile, which might degrade performance when not using CAS.

@mdbergmann
Copy link

@easye easye self-assigned this Jun 22, 2023
easye added a commit to easye/abcl that referenced this issue Jun 28, 2023
VarHandles were introduced in openjdk9, so the code needs to be
conditionalized somehow.  It is currently being developed on
openjdk17.

<armedbear#92>
easye added a commit to easye/abcl that referenced this issue Jun 29, 2023
VarHandles were introduced in openjdk9, so the code needs to be
conditionalized somehow.  It is currently being developed on
openjdk17.

<armedbear#92>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants