Skip to content

Commit 4911d41

Browse files
committed
Allow reassigning native pointer to wrapper for shared cores.
1 parent 19e34dd commit 4911d41

File tree

3 files changed

+21
-12
lines changed

3 files changed

+21
-12
lines changed

graalpython/com.oracle.graal.python.cext/src/capi.c

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,23 @@
3838
*/
3939
#include "capi.h"
4040

41-
#define FORCE_TO_NATIVE(__obj__) (polyglot_invoke(PY_TRUFFLE_CEXT, "PyTruffle_Set_Ptr", (__obj__), truffle_is_handle_to_managed((__obj__)) ? (__obj__) : truffle_deref_handle_for_managed(__obj__)))
41+
42+
MUST_INLINE static void force_to_native(void* obj) {
43+
if (polyglot_is_value(obj)) {
44+
void* handle = truffle_deref_handle_for_managed(obj);
45+
if(!polyglot_invoke(PY_TRUFFLE_CEXT, "PyTruffle_Set_Ptr", obj, handle)) {
46+
truffle_release_handle(handle);
47+
}
48+
}
49+
}
4250

4351
static void initialize_type_structure(PyTypeObject* structure, const char* typname) {
4452
// explicit type cast is required because the type flags are not yet initialized !
4553
PyTypeObject* ptype = polyglot_as__typeobject(UPCALL_CEXT_O("PyTruffle_Type", polyglot_from_string(typname, SRC_CS)));
4654

4755
// We eagerly create a native pointer for all builtin types. This is necessary for pointer comparisons to work correctly.
4856
// TODO Remove this as soon as this is properly supported.
49-
FORCE_TO_NATIVE(ptype);
57+
force_to_native(ptype);
5058

5159
unsigned long original_flags = structure->tp_flags;
5260
Py_ssize_t basicsize = structure->tp_basicsize;
@@ -59,30 +67,30 @@ static void initialize_type_structure(PyTypeObject* structure, const char* typna
5967
static void initialize_globals() {
6068
// None
6169
PyObject* jnone = UPCALL_CEXT_O("Py_None");
62-
FORCE_TO_NATIVE(jnone);
70+
force_to_native(jnone);
6371
truffle_assign_managed(&_Py_NoneStruct, jnone);
6472

6573
// NotImplemented
6674
void *jnotimpl = UPCALL_CEXT_O("Py_NotImplemented");
67-
FORCE_TO_NATIVE(jnotimpl);
75+
force_to_native(jnotimpl);
6876
truffle_assign_managed(&_Py_NotImplementedStruct, jnotimpl);
6977

7078
// Ellipsis
7179
void *jellipsis = UPCALL_CEXT_O("Py_Ellipsis");
72-
FORCE_TO_NATIVE(jellipsis);
80+
force_to_native(jellipsis);
7381
truffle_assign_managed(&_Py_EllipsisObject, jellipsis);
7482

7583
// True, False
7684
void *jtrue = UPCALL_CEXT_O("Py_True");
77-
FORCE_TO_NATIVE(jtrue);
85+
force_to_native(jtrue);
7886
truffle_assign_managed(&_Py_TrueStruct, polyglot_as__longobject(jtrue));
7987
void *jfalse = UPCALL_CEXT_O("Py_False");
80-
FORCE_TO_NATIVE(jfalse);
88+
force_to_native(jfalse);
8189
truffle_assign_managed(&_Py_FalseStruct, polyglot_as__longobject(jfalse));
8290

8391
// error marker
8492
void *jerrormarker = UPCALL_CEXT_O("Py_ErrorHandler");
85-
FORCE_TO_NATIVE(jerrormarker);
93+
force_to_native(jerrormarker);
8694
truffle_assign_managed(&marker_struct, jerrormarker);
8795
}
8896

@@ -205,7 +213,7 @@ PyObject* to_sulong(void *o) {
205213

206214
/** to be used from Java code only; reads native 'ob_type' field */
207215
void* get_ob_type(PyObject* obj) {
208-
return native_to_java(obj->ob_type);
216+
return native_to_java((PyObject*)obj->ob_type);
209217
}
210218

211219
typedef struct PyObjectHandle {

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1199,9 +1199,10 @@ private GetClassNode getClassNode() {
11991199
abstract static class PyTruffle_Set_Ptr extends NativeBuiltin {
12001200

12011201
@Specialization
1202-
PythonObjectNativeWrapper doPythonObject(PythonObjectNativeWrapper nativeWrapper, TruffleObject ptr) {
1202+
int doPythonObject(PythonObjectNativeWrapper nativeWrapper, TruffleObject ptr) {
1203+
nativeWrapper.setNativePointer(null);
12031204
nativeWrapper.setNativePointer(ptr);
1204-
return nativeWrapper;
1205+
return 0;
12051206
}
12061207
}
12071208

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/NativeWrappers.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public Object getNativePointer() {
6262

6363
public void setNativePointer(Object nativePointer) {
6464
// we should set the pointer just once
65-
assert this.nativePointer == null || this.nativePointer.equals(nativePointer);
65+
assert this.nativePointer == null || this.nativePointer.equals(nativePointer) || nativePointer == null;
6666
this.nativePointer = nativePointer;
6767
}
6868

0 commit comments

Comments
 (0)