LWJGL / lwjgl3

LWJGL is a Java library that enables cross-platform access to popular native APIs useful in the development of graphics (OpenGL, Vulkan, bgfx), audio (OpenAL, Opus), parallel computing (OpenCL, CUDA) and XR (OpenVR, LibOVR, OpenXR) applications.
https://www.lwjgl.org
BSD 3-Clause "New" or "Revised" License
4.67k stars 631 forks source link

Project Loom, and fibers #808

Closed ws909 closed 1 year ago

ws909 commented 1 year ago

Question

Java is about to have a number of new features integrated, some of which are:

It has been mentioned in previous issues that some of these features (Panama) will require such extensive rewriting of LWJGL, that LWJGL will be transitioned into version 4. However, as several of these have been available as incubator features for a while, and are now transitioning to preview features, new projects utilizing LWJGL will want to take advantage of them. Many existing projects would also benefit from them.

What issues currently exist, that will cause problems with, or prevent projects from, using these new features with LWJGL?

  1. Which LWJGL APIs are not safe to use with these new features?
  2. Is the MemoryStack API safe to use in virtual threads?
  3. Certain libraries unnecessarily perform blocking calls in addition to data processing, on the calling thread. Would these APIs be problematic from virtual threads? The OpenJDK team may be better fit for answering such a question.
    • STBI loads images from file paths. Loom-safe file loading can be achieved by using stbi_load_from_memory instead.
    • ShaderC processes GLSL files that can link to other GLSL files on the system.

I assume I can add more questions once they pop up in my head.

octylFractal commented 1 year ago
  1. To my knowledge, all of LWJGL should be safe to use with the new features. Obviously Panama can't be used with LWJGL until LWJGL itself is rewritten to be based on it, but the feature existing does not prevent LWJGL from working. The only issue that might occur is with virtual threads -- the threading requirements of some libraries in LWJGL will refer to the platform thread, so as long as all your virtual threads use the same platform thread, they will be thread-safe / using the "same" thread with respect to LWJGL. However, code that runs on a single virtual thread, but switches to different platform threads, will not be thread-safe / [...] in that regard.
  2. I see no reason that MemoryStack would not be safe to use from virtual threads.
  3. These APIs will be problematic from virtual threads, as specified by JEP 425 native calls (i.e. JNI and Panama) will pin the virtual thread to its platform carrier thread. Other than potentially reducing parallelism, there should be no issues.
ws909 commented 1 year ago

@octylFractal

1.

octylFractal commented 1 year ago

Re (3), you should be able to manually wrap these I/O methods in ForkJoinPool.managedBlock(...) in order to avoid starving the virtual thread executor. As the JEP specifies, virtual threads are run using an internal ForkJoinPool.

ws909 commented 1 year ago

@octylFractal Thanks. Apparently, I missed all of it. I read most of the newer JEPs, so I may have come across it a good while ago, but didn't recall that now.