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
40 changes: 26 additions & 14 deletions src/main/java/net/ladenthin/llama/loader/LlamaLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@
@ToString
public class LlamaLoader {

/**
* Private monitor guarding {@link #initialize()}. Synchronizing on this
* dedicated object instead of {@code LlamaLoader.class} keeps the lock
* private to this class, so untrusted code that can reach the public
* {@code LlamaLoader} type cannot acquire the same intrinsic lock and
* interfere with library initialization (SpotBugs
* {@code USO_UNSAFE_STATIC_METHOD_SYNCHRONIZATION}).
*/
private static final Object INITIALIZE_LOCK = new Object();

private static boolean extracted = false;
private static final LlamaSystemProperties systemProperties = new LlamaSystemProperties();
private static final NativeLibraryPermissionSetter permissionSetter = new NativeLibraryPermissionSetter(System.err);
Expand All @@ -63,22 +73,24 @@ private LlamaLoader() {}
/**
* Loads the llama and jllama shared libraries
*/
public static synchronized void initialize() {
// only cleanup before the first extract
if (!extracted) {
cleanup();
}
if ("Mac".equals(OSInfo.getOSName())) {
String nativeDirName = getNativeResourcePath();
String tempFolder = getTempDir().getAbsolutePath();
System.out.println(nativeDirName);
Path metalFilePath = extractFile(nativeDirName, "ggml-metal.metal", tempFolder);
if (metalFilePath == null) {
System.err.println("'ggml-metal.metal' not found");
public static void initialize() {
synchronized (INITIALIZE_LOCK) {
// only cleanup before the first extract
if (!extracted) {
cleanup();
}
if ("Mac".equals(OSInfo.getOSName())) {
String nativeDirName = getNativeResourcePath();
String tempFolder = getTempDir().getAbsolutePath();
System.out.println(nativeDirName);
Path metalFilePath = extractFile(nativeDirName, "ggml-metal.metal", tempFolder);
if (metalFilePath == null) {
System.err.println("'ggml-metal.metal' not found");
}
}
loadNativeLibrary("jllama");
extracted = true;
}
loadNativeLibrary("jllama");
extracted = true;
}

/**
Expand Down
Loading