Skip to content

Commit b3e243f

Browse files
committed
Use TruffleSafepoint.setBlockedThreadInterruptible for blocking operations in LRUCache
1 parent da8f93f commit b3e243f

File tree

3 files changed

+25
-87
lines changed

3 files changed

+25
-87
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/StructModuleBuiltins.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,12 @@ public void postInitialize(Python3Core core) {
7878
structModule.setModuleState(cache);
7979
}
8080

81-
protected static PStruct getStruct(PythonModule structModule, Object format, StructBuiltins.ConstructStructNode constructStructNode) {
81+
protected static PStruct getStruct(Node location, PythonModule structModule, Object format, StructBuiltins.ConstructStructNode constructStructNode) {
8282
LRUStructCache cache = structModule.getModuleState(LRUStructCache.class);
83-
PStruct pStruct = cache.get(format);
83+
PStruct pStruct = cache.get(location, format);
8484
if (pStruct == null) {
8585
pStruct = constructStructNode.execute(format);
86-
cache.put(format, pStruct);
86+
cache.put(location, format, pStruct);
8787
}
8888
return pStruct;
8989
}
@@ -94,8 +94,8 @@ protected static PStruct getStruct(PythonModule structModule, Object format, Str
9494
abstract static class GetStructNode extends PNodeWithContext {
9595
abstract PStruct execute(Node inliningTarget, PythonModule module, Object format, StructBuiltins.ConstructStructNode constructStructNode);
9696

97-
protected PStruct getStructInternal(PythonModule module, Object format, StructBuiltins.ConstructStructNode constructStructNode) {
98-
return getStruct(module, format, constructStructNode);
97+
protected PStruct getStructInternal(Node location, PythonModule module, Object format, StructBuiltins.ConstructStructNode constructStructNode) {
98+
return getStruct(location, module, format, constructStructNode);
9999
}
100100

101101
protected boolean eq(TruffleString s1, TruffleString s2, TruffleString.EqualNode eqNode) {
@@ -107,7 +107,7 @@ protected boolean eq(TruffleString s1, TruffleString s2, TruffleString.EqualNode
107107
static PStruct doCachedString(PythonModule module, TruffleString format, StructBuiltins.ConstructStructNode constructStructNode,
108108
@Cached("format") TruffleString cachedFormat,
109109
@Cached TruffleString.EqualNode eqNode,
110-
@Cached(value = "getStructInternal(module, format, constructStructNode)", weak = true) PStruct cachedStruct) {
110+
@Cached(value = "getStructInternal($node, module, format, constructStructNode)", weak = true) PStruct cachedStruct) {
111111
return cachedStruct;
112112
}
113113

@@ -116,13 +116,14 @@ static PStruct doCachedString(PythonModule module, TruffleString format, StructB
116116
static PStruct doCachedBytes(PythonModule module, PBytes format, StructBuiltins.ConstructStructNode constructStructNode,
117117
@CachedLibrary("format") PythonBufferAccessLibrary bufferLib,
118118
@Cached(value = "bufferLib.getCopiedByteArray(format)", dimensions = 1) byte[] cachedFormat,
119-
@Cached(value = "getStructInternal(module, format, constructStructNode)", weak = true) PStruct cachedStruct) {
119+
@Cached(value = "getStructInternal($node, module, format, constructStructNode)", weak = true) PStruct cachedStruct) {
120120
return cachedStruct;
121121
}
122122

123123
@Specialization(replaces = {"doCachedString", "doCachedBytes"})
124-
static PStruct doGeneric(PythonModule module, Object format, StructBuiltins.ConstructStructNode constructStructNode) {
125-
return getStruct(module, format, constructStructNode);
124+
static PStruct doGeneric(PythonModule module, Object format, StructBuiltins.ConstructStructNode constructStructNode,
125+
@Bind Node location) {
126+
return getStruct(location, module, format, constructStructNode);
126127
}
127128
}
128129

@@ -241,7 +242,7 @@ abstract static class ClearCacheNode extends PythonUnaryBuiltinNode {
241242
@Specialization
242243
Object clearCache(PythonModule self) {
243244
LRUStructCache cache = self.getModuleState(LRUStructCache.class);
244-
cache.clear();
245+
cache.clear(this);
245246
return PNone.NONE;
246247
}
247248
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/util/Cache.java

Lines changed: 0 additions & 51 deletions
This file was deleted.

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/util/LRUCache.java

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2024, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* The Universal Permissive License (UPL), Version 1.0
@@ -46,9 +46,11 @@
4646
import java.util.concurrent.locks.ReadWriteLock;
4747
import java.util.concurrent.locks.ReentrantReadWriteLock;
4848

49-
import com.oracle.truffle.api.CompilerDirectives;
49+
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
50+
import com.oracle.truffle.api.TruffleSafepoint;
51+
import com.oracle.truffle.api.nodes.Node;
5052

51-
public class LRUCache<K, V> implements Cache<K, V> {
53+
public class LRUCache<K, V> {
5254
private final LRUHashMap<K, V> lruHashMap;
5355
private final ReadWriteLock readWriteLock = new ReentrantReadWriteLock();
5456
private final Lock read = readWriteLock.readLock();
@@ -73,47 +75,33 @@ protected boolean removeEldestEntry(Map.Entry<K, V> eldest) {
7375
}
7476
}
7577

76-
@Override
77-
@CompilerDirectives.TruffleBoundary
78-
public V get(K key) {
78+
@TruffleBoundary
79+
public V get(Node location, K key) {
7980
try {
80-
read.lock();
81+
TruffleSafepoint.setBlockedThreadInterruptible(location, Lock::lockInterruptibly, read);
8182
return lruHashMap.get(key);
8283
} finally {
8384
read.unlock();
8485
}
8586
}
8687

87-
@Override
88-
@CompilerDirectives.TruffleBoundary
89-
public V put(K key, V value) {
88+
@TruffleBoundary
89+
public V put(Node location, K key, V value) {
9090
try {
91-
write.lock();
91+
TruffleSafepoint.setBlockedThreadInterruptible(location, Lock::lockInterruptibly, write);
9292
return lruHashMap.putIfAbsent(key, value);
9393
} finally {
9494
write.unlock();
9595
}
9696
}
9797

98-
@Override
99-
@CompilerDirectives.TruffleBoundary
100-
public void clear() {
98+
@TruffleBoundary
99+
public void clear(Node location) {
101100
try {
102-
write.lock();
101+
TruffleSafepoint.setBlockedThreadInterruptible(location, Lock::lockInterruptibly, write);
103102
lruHashMap.clear();
104103
} finally {
105104
write.unlock();
106105
}
107106
}
108-
109-
@Override
110-
@CompilerDirectives.TruffleBoundary
111-
public int size() {
112-
try {
113-
read.lock();
114-
return lruHashMap.size();
115-
} finally {
116-
read.unlock();
117-
}
118-
}
119107
}

0 commit comments

Comments
 (0)