|
47 | 47 |
|
48 | 48 | import java.io.IOException;
|
49 | 49 | import java.nio.file.StandardOpenOption;
|
50 |
| -import java.util.concurrent.atomic.AtomicLong; |
| 50 | +import java.util.concurrent.atomic.AtomicInteger; |
51 | 51 |
|
52 | 52 | import com.oracle.graal.python.builtins.objects.cext.common.LoadCExtException.ApiInitException;
|
53 | 53 | import com.oracle.graal.python.builtins.objects.cext.common.LoadCExtException.ImportException;
|
|
67 | 67 | * guaranteed to work with the matching GraalPy version.
|
68 | 68 | */
|
69 | 69 | public final class NativeLibraryLocator {
|
| 70 | + private static final int MAX_CEXT_COPIES = 64; |
| 71 | + |
70 | 72 | /**
|
71 | 73 | * Bitset for which copied C extension to use when {@link PythonOptions#IsolateNativeModules} is
|
72 | 74 | * enabled.
|
73 | 75 | */
|
74 |
| - private static AtomicLong CEXT_COPY_INDICES = new AtomicLong(-1); |
| 76 | + private static final AtomicInteger CEXT_COPY_INDICES = new AtomicInteger(MAX_CEXT_COPIES); |
| 77 | + |
75 | 78 |
|
76 | 79 | /**
|
77 | 80 | * The suffix to add to C extensions when loading. This allows us to support native module
|
@@ -101,9 +104,10 @@ public final class NativeLibraryLocator {
|
101 | 104 | public NativeLibraryLocator(PythonContext context, TruffleFile capiLibrary, boolean isolateNative) throws ApiInitException {
|
102 | 105 | this.capiOriginal = capiLibrary.getName();
|
103 | 106 | if (isolateNative) {
|
104 |
| - this.capiSlot = Long.numberOfTrailingZeros(Long.lowestOneBit(CEXT_COPY_INDICES.getAndUpdate((oldValue) -> oldValue ^ Long.lowestOneBit(oldValue)))); |
105 |
| - if (this.capiSlot == Long.SIZE) { |
106 |
| - throw new ApiInitException(ErrorMessages.CAPI_ISOLATION_CAPPED_AT_D, Long.SIZE); |
| 107 | + this.capiSlot = MAX_CEXT_COPIES - CEXT_COPY_INDICES.getAndDecrement(); |
| 108 | + if (this.capiSlot < 0) { |
| 109 | + CEXT_COPY_INDICES.set(0); |
| 110 | + throw new ApiInitException(ErrorMessages.CAPI_ISOLATION_CAPPED_AT_D, MAX_CEXT_COPIES); |
107 | 111 | }
|
108 | 112 | this.capiCopy = resolve(context, capiLibrary, capiSlot, null);
|
109 | 113 | } else {
|
@@ -132,8 +136,6 @@ public String getCapiLibrary() {
|
132 | 136 | }
|
133 | 137 |
|
134 | 138 | public void close() {
|
135 |
| - // Return the C API slot for subsequent contexts to use. |
136 |
| - CEXT_COPY_INDICES.updateAndGet((oldValue) -> oldValue | (1L << capiSlot)); |
137 | 139 | }
|
138 | 140 |
|
139 | 141 | /**
|
|
0 commit comments