Skip to content

Commit 3135db7

Browse files
committed
acquire gil around hash messages
1 parent da372e2 commit 3135db7

File tree

1 file changed

+75
-21
lines changed
  • graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/dict

1 file changed

+75
-21
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/dict/PDict.java

Lines changed: 75 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,17 @@
4949
import com.oracle.graal.python.nodes.attributes.LookupInheritedAttributeNode;
5050
import com.oracle.graal.python.nodes.object.IsBuiltinClassProfile;
5151
import com.oracle.graal.python.nodes.util.CastToJavaLongLossyNode;
52+
import com.oracle.graal.python.runtime.GilNode;
5253
import com.oracle.graal.python.runtime.exception.PException;
5354
import com.oracle.truffle.api.CompilerAsserts;
5455
import com.oracle.truffle.api.dsl.Cached;
5556
import com.oracle.truffle.api.dsl.Cached.Exclusive;
5657
import com.oracle.truffle.api.dsl.Cached.Shared;
58+
import com.oracle.truffle.api.dsl.Specialization;
5759
import com.oracle.truffle.api.exception.AbstractTruffleException;
5860
import com.oracle.truffle.api.interop.InteropLibrary;
5961
import com.oracle.truffle.api.interop.UnknownKeyException;
6062
import com.oracle.truffle.api.interop.UnsupportedTypeException;
61-
import com.oracle.truffle.api.dsl.Specialization;
6263
import com.oracle.truffle.api.library.CachedLibrary;
6364
import com.oracle.truffle.api.library.ExportLibrary;
6465
import com.oracle.truffle.api.library.ExportMessage;
@@ -166,22 +167,41 @@ static boolean hasHashEntries(@SuppressWarnings("unused") PDict self) {
166167

167168
@ExportMessage(limit = "2")
168169
static long getHashSize(PDict self,
170+
@Exclusive @Cached GilNode gil,
169171
@CachedLibrary("self.getDictStorage()") HashingStorageLibrary lib) {
170-
return lib.length(self.getDictStorage());
172+
boolean mustRelease = gil.acquire();
173+
try {
174+
return lib.length(self.getDictStorage());
175+
} finally {
176+
gil.release(mustRelease);
177+
}
171178
}
172179

173180
@ExportMessage(limit = "2")
174181
@ExportMessage(name = "isHashEntryModifiable", limit = "2")
175182
@ExportMessage(name = "isHashEntryRemovable", limit = "2")
176183
static boolean isHashEntryReadable(PDict self, Object key,
184+
@Exclusive @Cached GilNode gil,
177185
@CachedLibrary("self.getDictStorage()") HashingStorageLibrary lib) {
178-
return lib.hasKey(self.getDictStorage(), key);
186+
boolean mustRelease = gil.acquire();
187+
try {
188+
return lib.hasKey(self.getDictStorage(), key);
189+
} finally {
190+
gil.release(mustRelease);
191+
}
179192
}
180193

181194
@ExportMessage(limit = "2")
182195
static Object readHashValue(PDict self, Object key,
196+
@Exclusive @Cached GilNode gil,
183197
@CachedLibrary("self.getDictStorage()") HashingStorageLibrary lib) throws UnknownKeyException {
184-
Object value = lib.getItem(self.getDictStorage(), key);
198+
Object value = null;
199+
boolean mustRelease = gil.acquire();
200+
try {
201+
value = lib.getItem(self.getDictStorage(), key);
202+
} finally {
203+
gil.release(mustRelease);
204+
}
185205
if (value == null) {
186206
throw UnknownKeyException.create(key);
187207
} else {
@@ -191,60 +211,94 @@ static Object readHashValue(PDict self, Object key,
191211

192212
@ExportMessage(limit = "3")
193213
static boolean isHashEntryInsertable(PDict self, Object key,
214+
@Exclusive @Cached GilNode gil,
194215
@CachedLibrary("key") PythonObjectLibrary keyLib,
195216
@CachedLibrary("self.getDictStorage()") HashingStorageLibrary lib) {
196-
if (lib.hasKey(self.getDictStorage(), key)) {
197-
return false;
198-
} else {
199-
// we can only insert hashable types
200-
try {
201-
keyLib.hash(key);
202-
} catch (AbstractTruffleException e) {
217+
boolean mustRelease = gil.acquire();
218+
try {
219+
if (lib.hasKey(self.getDictStorage(), key)) {
203220
return false;
221+
} else {
222+
// we can only insert hashable types
223+
try {
224+
keyLib.hash(key);
225+
} catch (AbstractTruffleException e) {
226+
return false;
227+
}
228+
return true;
204229
}
205-
return true;
230+
} finally {
231+
gil.release(mustRelease);
206232
}
207233
}
208234

209235
@ExportMessage(limit = "2")
210236
static void writeHashEntry(PDict self, Object key, Object value,
237+
@Exclusive @Cached GilNode gil,
211238
@Cached IsBuiltinClassProfile errorProfile,
212239
@CachedLibrary("self.getDictStorage()") HashingStorageLibrary lib) throws UnsupportedTypeException {
240+
boolean mustRelease = gil.acquire();
213241
try {
214242
lib.setItem(self.getDictStorage(), key, value);
215243
} catch (PException e) {
216244
e.expect(PythonBuiltinClassType.TypeError, errorProfile);
217245
throw UnsupportedTypeException.create(new Object[]{key}, "keys for Python arrays must be hashable");
246+
} finally {
247+
gil.release(mustRelease);
218248
}
219249
}
220250

221251
@ExportMessage(limit = "2")
222252
static void removeHashEntry(PDict self, Object key,
253+
@Exclusive @Cached GilNode gil,
223254
@CachedLibrary("self.getDictStorage()") HashingStorageLibrary lib) throws UnknownKeyException {
224-
if (!isHashEntryReadable(self, key, lib)) {
225-
throw UnknownKeyException.create(key);
255+
boolean mustRelease = gil.acquire();
256+
try {
257+
if (!isHashEntryReadable(self, key, gil, lib)) {
258+
throw UnknownKeyException.create(key);
259+
}
260+
lib.delItem(self.getDictStorage(), key);
261+
} finally {
262+
gil.release(mustRelease);
226263
}
227-
lib.delItem(self.getDictStorage(), key);
228264
}
229265

230266
@ExportMessage
231267
static Object getHashEntriesIterator(PDict self,
268+
@Exclusive @Cached GilNode gil,
232269
@Shared("iterLib") @CachedLibrary(limit = "2") PythonObjectLibrary lib) {
233-
Object dictItems = lib.lookupAndCallSpecialMethod(self, null, ITEMS);
234-
return lib.getIterator(dictItems);
270+
boolean mustRelease = gil.acquire();
271+
try {
272+
Object dictItems = lib.lookupAndCallSpecialMethod(self, null, ITEMS);
273+
return lib.getIterator(dictItems);
274+
} finally {
275+
gil.release(mustRelease);
276+
}
235277
}
236278

237279
@ExportMessage
238280
static Object getHashKeysIterator(PDict self,
281+
@Exclusive @Cached GilNode gil,
239282
@Shared("iterLib") @CachedLibrary(limit = "2") PythonObjectLibrary lib) {
240-
Object dictKeys = lib.lookupAndCallSpecialMethod(self, null, KEYS);
241-
return lib.getIterator(dictKeys);
283+
boolean mustRelease = gil.acquire();
284+
try {
285+
Object dictKeys = lib.lookupAndCallSpecialMethod(self, null, KEYS);
286+
return lib.getIterator(dictKeys);
287+
} finally {
288+
gil.release(mustRelease);
289+
}
242290
}
243291

244292
@ExportMessage
245293
static Object getHashValuesIterator(PDict self,
294+
@Exclusive @Cached GilNode gil,
246295
@Shared("iterLib") @CachedLibrary(limit = "2") PythonObjectLibrary lib) {
247-
Object dictValues = lib.lookupAndCallSpecialMethod(self, null, VALUES);
248-
return lib.getIterator(dictValues);
296+
boolean mustRelease = gil.acquire();
297+
try {
298+
Object dictValues = lib.lookupAndCallSpecialMethod(self, null, VALUES);
299+
return lib.getIterator(dictValues);
300+
} finally {
301+
gil.release(mustRelease);
302+
}
249303
}
250304
}

0 commit comments

Comments
 (0)