-
Notifications
You must be signed in to change notification settings - Fork 227
/
Copy pathtrampoline.cpp
188 lines (155 loc) · 5.19 KB
/
trampoline.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
src/trampoline.cpp: support for overriding virtual functions in Python
Copyright (c) 2022 Wenzel Jakob
All rights reserved. Use of this source code is governed by a
BSD-style license that can be found in the LICENSE file.
*/
#include <nanobind/trampoline.h>
#include "nb_internals.h"
NAMESPACE_BEGIN(NB_NAMESPACE)
NAMESPACE_BEGIN(detail)
void trampoline_new(void **data, size_t size, void *ptr) noexcept {
// GIL is held when the trampoline constructor runs. Lock the
// associated instance shard in GIL-less Python.
nb_shard &shard = internals->shard(ptr);
lock_shard lock(shard);
nb_ptr_map &inst_c2p = shard.inst_c2p;
nb_ptr_map::iterator it = inst_c2p.find(ptr);
check(it != inst_c2p.end() && (((uintptr_t) it->second) & 1) == 0,
"nanobind::detail::trampoline_new(): unique instance not found!");
data[0] = it->second;
memset(data + 1, 0, sizeof(void *) * 2 * size);
}
void trampoline_release(void **data, size_t size) noexcept {
// GIL is held when the trampoline destructor runs
for (size_t i = 0; i < size; ++i)
Py_XDECREF((PyObject *) data[i*2 + 2]);
}
static void trampoline_enter_internal(void **data, size_t size,
const char *name, bool pure, ticket *t) {
const PyObject *None = Py_None;
PyGILState_STATE state{ };
const char *error = nullptr;
PyObject *key = nullptr, *value = nullptr;
PyObject *self = (PyObject *) data[0];
PyTypeObject *value_tp = nullptr;
size_t offset = 0;
// First, perform a quick sweep without lock
for (size_t i = 0; i < size; i++) {
void *d_name = data[2*i + 1],
*d_value = data[2*i + 2];
if (name == d_name && d_value) {
if (d_value != None) {
t->state = PyGILState_Ensure();
t->key = (PyObject *) d_value;
return;
} else {
if (pure) {
error = "tried to call a pure virtual function";
break;
} else {
return;
}
}
}
}
// Nothing found -- retry, now with lock held
state = PyGILState_Ensure();
ft_object_guard guard(self);
if (error)
goto fail;
for (size_t i = 0; i < size; i++) {
void *d_name = data[2*i + 1],
*d_value = data[2*i + 2];
if (name == d_name && d_value) {
if (d_value != None) {
t->state = state;
t->key = (PyObject *) d_value;
return;
} else {
if (pure) {
error = "tried to call a pure virtual function";
goto fail;
} else {
PyGILState_Release(state);
return;
}
}
}
}
// Sill no luck -- perform a lookup and populate the trampoline
for (; offset < size; offset++) {
if (data[2 * offset + 1] == nullptr &&
data[2 * offset + 2] == nullptr)
break;
}
if (offset == size) {
error = "the trampoline ran out of slots (you will need to increase "
"the value provided to the NB_TRAMPOLINE() macro)";
goto fail;
}
key = PyUnicode_InternFromString(name);
if (!key) {
error = "could not intern string";
goto fail;
}
value = PyObject_GetAttr(self, key);
if (!value) {
error = "lookup failed";
goto fail;
}
value_tp = Py_TYPE(value);
Py_CLEAR(value);
if (value_tp == internals->nb_func || value_tp == internals->nb_method ||
value_tp == internals->nb_bound_method) {
Py_DECREF(key);
if (pure) {
error = "tried to call a pure virtual function";
goto fail;
}
Py_INCREF(Py_None);
key = Py_None;
}
data[2 * offset + 1] = (void *) name;
data[2 * offset + 2] = key;
if (key != None) {
t->state = state;
t->key = key;
return;
} else {
PyGILState_Release(state);
return;
}
fail:
type_data *td = nb_type_data(Py_TYPE(self));
PyGILState_Release(state);
raise("nanobind::detail::get_trampoline('%s::%s()'): %s!",
td->name, name, error);
}
static NB_THREAD_LOCAL ticket *current_ticket = nullptr;
void trampoline_enter(void **data, size_t size, const char *name, bool pure, ticket *t) {
trampoline_enter_internal(data, size, name, pure, t);
if (t->key) {
t->self = (PyObject *) data[0];
t->prev = current_ticket;
if (t->prev && t->prev->self.is(t->self) && t->prev->key.is(t->key)) {
t->self = handle();
t->key = handle();
t->prev = nullptr;
PyGILState_Release(t->state);
if (pure)
raise("nanobind::detail::get_trampoline('%s()'): tried to call "
"a pure virtual function!", name);
return;
}
current_ticket = t;
}
}
void trampoline_leave(ticket *t) noexcept {
if (!t->key)
return;
current_ticket = t->prev;
PyGILState_Release(t->state);
}
NAMESPACE_END(detail)
NAMESPACE_END(NB_NAMESPACE)