Skip to content

Commit 28c9afe

Browse files
committed
Fixed OpenCL bug. Bumped to Version 1.0.3
1 parent 07675e3 commit 28c9afe

3 files changed

Lines changed: 32 additions & 3 deletions

File tree

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ mapping_version=1.20.1
1212
mod_id=quantified
1313
mod_name=Quantified API
1414
mod_license=BRSSLA V1.3
15-
mod_version=1.0.2
15+
mod_version=1.0.3
1616
mod_group_id=org.admany.quantified
1717
mod_authors=Admany
1818
mod_description=An advanced API for asynchronous tasks with OpenCL, intelligent caching, and mod networking for Minecraft. License available at: https://github.com/Admany/Quantified-API/blob/master/LICENSE

src/main/java/org/admany/quantified/core/common/opencl/core/OpenCLContext.java

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,12 @@ public void releaseKernel(long kernel) {
186186
public void close() {
187187
if (!initialized.get()) return;
188188

189+
if (!OpenCLRuntime.isInitialised()) {
190+
resetHandles();
191+
LOGGER.fine("OpenCL context closed after runtime shutdown");
192+
return;
193+
}
194+
189195
try {
190196
if (programHandle != 0) {
191197
releaseProgram(programHandle);
@@ -202,15 +208,34 @@ public void close() {
202208
contextHandle = 0;
203209
}
204210

205-
deviceId = 0;
206-
initialized.set(false);
211+
resetHandles();
207212
LOGGER.info("OpenCL context cleaned up");
208213

214+
} catch (IllegalStateException e) {
215+
if (isLibraryNotLoaded(e)) {
216+
resetHandles();
217+
LOGGER.fine("OpenCL context cleanup skipped (library already unloaded)");
218+
return;
219+
}
220+
LOGGER.log(Level.WARNING, "Error during OpenCL context cleanup", e);
209221
} catch (Exception e) {
210222
LOGGER.log(Level.WARNING, "Error during OpenCL context cleanup", e);
211223
}
212224
}
213225

226+
private void resetHandles() {
227+
programHandle = 0;
228+
commandQueueHandle = 0;
229+
contextHandle = 0;
230+
deviceId = 0;
231+
initialized.set(false);
232+
}
233+
234+
private boolean isLibraryNotLoaded(IllegalStateException e) {
235+
String msg = e.getMessage();
236+
return msg != null && msg.toLowerCase(java.util.Locale.ROOT).contains("opencl library has not been loaded");
237+
}
238+
214239
private long createContext(GPUDetector.OpenCLDevice device) {
215240
try (MemoryStack stack = MemoryStack.stackPush()) {
216241
PointerBuffer properties = stack.mallocPointer(3);

src/main/java/org/admany/quantified/core/common/opencl/core/OpenCLRuntime.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,10 @@ public static String lastError() {
105105
return LAST_ERROR.get();
106106
}
107107

108+
public static boolean isInitialised() {
109+
return INITIALISED.get();
110+
}
111+
108112
public static String getBindingName() {
109113
Binding b = BINDING.get();
110114
return b == null ? "UNKNOWN" : b.name();

0 commit comments

Comments
 (0)