Skip to content

Commit 86d1388

Browse files
committed
parallel compilation wip - use lock
1 parent 8240e1d commit 86d1388

File tree

2 files changed

+15
-26
lines changed

2 files changed

+15
-26
lines changed

src/main/java/org/perlonjava/codegen/EmitterMethodCreator.java

+12-4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import java.io.StringWriter;
1111
import java.lang.annotation.Annotation;
1212
import java.lang.reflect.*;
13+
import java.util.concurrent.locks.ReentrantLock;
1314

1415
/**
1516
* EmitterMethodCreator is a utility class that uses the ASM library to dynamically generate Java
@@ -77,6 +78,8 @@ public static String getVariableClassName(String varName) {
7778
};
7879
}
7980

81+
private static final ReentrantLock lock = new ReentrantLock();
82+
8083
/**
8184
* Creates a new class with a method based on the provided context, environment, and abstract
8285
* syntax tree (AST).
@@ -87,10 +90,15 @@ public static String getVariableClassName(String varName) {
8790
* @return The generated class.
8891
*/
8992
public static Class<?> createClassWithMethod(EmitterContext ctx, Node ast, boolean useTryCatch) {
90-
91-
byte[] classData = getBytecode(ctx, ast, useTryCatch);
92-
93-
return loadBytecode(ctx, classData);
93+
lock.lock();
94+
Class<?> aClass;
95+
try {
96+
byte[] classData = getBytecode(ctx, ast, useTryCatch);
97+
aClass = loadBytecode(ctx, classData);
98+
} finally {
99+
lock.unlock();
100+
}
101+
return aClass;
94102
}
95103

96104
public static byte[] getBytecode(EmitterContext ctx, Node ast, boolean useTryCatch) {

src/main/java/org/perlonjava/parser/SubroutineParser.java

+3-22
Original file line numberDiff line numberDiff line change
@@ -259,32 +259,13 @@ private static ListNode handleNamedSub(Parser parser, String subName, String pro
259259
new RuntimeArray()
260260
);
261261

262-
// Generate bytecode for the subroutine block
263-
byte[] classData = EmitterMethodCreator.getBytecode(newCtx, block, false);
264-
// System.out.println("Creating subroutine " + fullName);
265-
// Load the generated bytecode into a Class object
266-
Class<?> generatedClass = EmitterMethodCreator.loadBytecode(newCtx, classData);
267-
268262
// Create a Runnable to execute the subroutine creation
269263
Runnable subroutineCreationTask = () -> {
270-
271-
// The following commented-out code is a work in progress for handling concurrency
272-
// Class<?> generatedClass = null;
273-
// try {
274-
// semaphore.acquire();
275-
// byte[] classData = EmitterMethodCreator.getBytecode(newCtx, block, false);
276-
// // System.out.println("Creating subroutine " + fullName);
277-
// generatedClass = EmitterMethodCreator.loadBytecode(newCtx, classData);
278-
//
279-
// } catch (InterruptedException e) {
280-
// throw new RuntimeException(e);
281-
// } finally {
282-
// // Release the semaphore
283-
// semaphore.release();
284-
// }
285-
264+
// Generate bytecode and load into a Class object
265+
Class<?> generatedClass = EmitterMethodCreator.createClassWithMethod(newCtx, block, false);
286266
// System.out.println("Class " + generatedClass);
287267
// EmitterMethodCreator.debugInspectClass(generatedClass);
268+
288269
try {
289270
// Prepare constructor with the captured variable types
290271
Class<?>[] parameterTypes = classList.toArray(new Class<?>[0]);

0 commit comments

Comments
 (0)