Victor Stinner | f2c3b68 | 2020-05-14 18:46:24 +0200 | [diff] [blame] | 1 | |
| 2 | /* interpreters module */ |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 3 | /* low-level access to interpreter primitives */ |
| 4 | |
| 5 | #include "Python.h" |
| 6 | #include "frameobject.h" |
Eric Snow | c11183c | 2019-03-15 16:35:46 -0600 | [diff] [blame] | 7 | #include "interpreteridobject.h" |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 8 | |
| 9 | |
Victor Stinner | f2c3b68 | 2020-05-14 18:46:24 +0200 | [diff] [blame] | 10 | static char * |
| 11 | _copy_raw_string(PyObject *strobj) |
| 12 | { |
| 13 | const char *str = PyUnicode_AsUTF8(strobj); |
| 14 | if (str == NULL) { |
| 15 | return NULL; |
| 16 | } |
| 17 | char *copied = PyMem_Malloc(strlen(str)+1); |
| 18 | if (copied == NULL) { |
| 19 | PyErr_NoMemory(); |
| 20 | return NULL; |
| 21 | } |
| 22 | strcpy(copied, str); |
| 23 | return copied; |
| 24 | } |
Eric Snow | 4e9da0d | 2018-02-02 21:49:49 -0700 | [diff] [blame] | 25 | |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 26 | static PyInterpreterState * |
| 27 | _get_current(void) |
| 28 | { |
Victor Stinner | f2c3b68 | 2020-05-14 18:46:24 +0200 | [diff] [blame] | 29 | // PyInterpreterState_Get() aborts if lookup fails, so don't need |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 30 | // to check the result for NULL. |
Victor Stinner | f2c3b68 | 2020-05-14 18:46:24 +0200 | [diff] [blame] | 31 | return PyInterpreterState_Get(); |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 32 | } |
| 33 | |
Eric Snow | 4e9da0d | 2018-02-02 21:49:49 -0700 | [diff] [blame] | 34 | |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 35 | /* data-sharing-specific code ***********************************************/ |
| 36 | |
Eric Snow | 4e9da0d | 2018-02-02 21:49:49 -0700 | [diff] [blame] | 37 | struct _sharednsitem { |
Victor Stinner | f2c3b68 | 2020-05-14 18:46:24 +0200 | [diff] [blame] | 38 | char *name; |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 39 | _PyCrossInterpreterData data; |
Eric Snow | 4e9da0d | 2018-02-02 21:49:49 -0700 | [diff] [blame] | 40 | }; |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 41 | |
Eric Snow | c11183c | 2019-03-15 16:35:46 -0600 | [diff] [blame] | 42 | static void _sharednsitem_clear(struct _sharednsitem *); // forward |
| 43 | |
Eric Snow | 4e9da0d | 2018-02-02 21:49:49 -0700 | [diff] [blame] | 44 | static int |
| 45 | _sharednsitem_init(struct _sharednsitem *item, PyObject *key, PyObject *value) |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 46 | { |
Victor Stinner | f2c3b68 | 2020-05-14 18:46:24 +0200 | [diff] [blame] | 47 | item->name = _copy_raw_string(key); |
| 48 | if (item->name == NULL) { |
Eric Snow | 4e9da0d | 2018-02-02 21:49:49 -0700 | [diff] [blame] | 49 | return -1; |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 50 | } |
Eric Snow | 4e9da0d | 2018-02-02 21:49:49 -0700 | [diff] [blame] | 51 | if (_PyObject_GetCrossInterpreterData(value, &item->data) != 0) { |
Eric Snow | c11183c | 2019-03-15 16:35:46 -0600 | [diff] [blame] | 52 | _sharednsitem_clear(item); |
Eric Snow | 4e9da0d | 2018-02-02 21:49:49 -0700 | [diff] [blame] | 53 | return -1; |
| 54 | } |
| 55 | return 0; |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 56 | } |
| 57 | |
Eric Snow | 4e9da0d | 2018-02-02 21:49:49 -0700 | [diff] [blame] | 58 | static void |
| 59 | _sharednsitem_clear(struct _sharednsitem *item) |
| 60 | { |
Victor Stinner | f2c3b68 | 2020-05-14 18:46:24 +0200 | [diff] [blame] | 61 | if (item->name != NULL) { |
| 62 | PyMem_Free(item->name); |
| 63 | item->name = NULL; |
| 64 | } |
Eric Snow | 4e9da0d | 2018-02-02 21:49:49 -0700 | [diff] [blame] | 65 | _PyCrossInterpreterData_Release(&item->data); |
| 66 | } |
| 67 | |
| 68 | static int |
| 69 | _sharednsitem_apply(struct _sharednsitem *item, PyObject *ns) |
| 70 | { |
Victor Stinner | f2c3b68 | 2020-05-14 18:46:24 +0200 | [diff] [blame] | 71 | PyObject *name = PyUnicode_FromString(item->name); |
Eric Snow | 4e9da0d | 2018-02-02 21:49:49 -0700 | [diff] [blame] | 72 | if (name == NULL) { |
| 73 | return -1; |
| 74 | } |
| 75 | PyObject *value = _PyCrossInterpreterData_NewObject(&item->data); |
| 76 | if (value == NULL) { |
| 77 | Py_DECREF(name); |
| 78 | return -1; |
| 79 | } |
| 80 | int res = PyDict_SetItem(ns, name, value); |
| 81 | Py_DECREF(name); |
| 82 | Py_DECREF(value); |
| 83 | return res; |
| 84 | } |
| 85 | |
| 86 | typedef struct _sharedns { |
| 87 | Py_ssize_t len; |
| 88 | struct _sharednsitem* items; |
| 89 | } _sharedns; |
| 90 | |
| 91 | static _sharedns * |
| 92 | _sharedns_new(Py_ssize_t len) |
| 93 | { |
| 94 | _sharedns *shared = PyMem_NEW(_sharedns, 1); |
| 95 | if (shared == NULL) { |
| 96 | PyErr_NoMemory(); |
| 97 | return NULL; |
| 98 | } |
| 99 | shared->len = len; |
| 100 | shared->items = PyMem_NEW(struct _sharednsitem, len); |
| 101 | if (shared->items == NULL) { |
| 102 | PyErr_NoMemory(); |
| 103 | PyMem_Free(shared); |
| 104 | return NULL; |
| 105 | } |
| 106 | return shared; |
| 107 | } |
| 108 | |
| 109 | static void |
| 110 | _sharedns_free(_sharedns *shared) |
| 111 | { |
| 112 | for (Py_ssize_t i=0; i < shared->len; i++) { |
| 113 | _sharednsitem_clear(&shared->items[i]); |
| 114 | } |
| 115 | PyMem_Free(shared->items); |
| 116 | PyMem_Free(shared); |
| 117 | } |
| 118 | |
| 119 | static _sharedns * |
| 120 | _get_shared_ns(PyObject *shareable) |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 121 | { |
| 122 | if (shareable == NULL || shareable == Py_None) { |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 123 | return NULL; |
| 124 | } |
| 125 | Py_ssize_t len = PyDict_Size(shareable); |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 126 | if (len == 0) { |
| 127 | return NULL; |
| 128 | } |
| 129 | |
Eric Snow | 4e9da0d | 2018-02-02 21:49:49 -0700 | [diff] [blame] | 130 | _sharedns *shared = _sharedns_new(len); |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 131 | if (shared == NULL) { |
| 132 | return NULL; |
| 133 | } |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 134 | Py_ssize_t pos = 0; |
| 135 | for (Py_ssize_t i=0; i < len; i++) { |
| 136 | PyObject *key, *value; |
| 137 | if (PyDict_Next(shareable, &pos, &key, &value) == 0) { |
| 138 | break; |
| 139 | } |
Eric Snow | 4e9da0d | 2018-02-02 21:49:49 -0700 | [diff] [blame] | 140 | if (_sharednsitem_init(&shared->items[i], key, value) != 0) { |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 141 | break; |
| 142 | } |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 143 | } |
| 144 | if (PyErr_Occurred()) { |
Eric Snow | 4e9da0d | 2018-02-02 21:49:49 -0700 | [diff] [blame] | 145 | _sharedns_free(shared); |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 146 | return NULL; |
| 147 | } |
| 148 | return shared; |
| 149 | } |
| 150 | |
| 151 | static int |
Eric Snow | 4e9da0d | 2018-02-02 21:49:49 -0700 | [diff] [blame] | 152 | _sharedns_apply(_sharedns *shared, PyObject *ns) |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 153 | { |
Eric Snow | 4e9da0d | 2018-02-02 21:49:49 -0700 | [diff] [blame] | 154 | for (Py_ssize_t i=0; i < shared->len; i++) { |
| 155 | if (_sharednsitem_apply(&shared->items[i], ns) != 0) { |
| 156 | return -1; |
| 157 | } |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 158 | } |
Eric Snow | 4e9da0d | 2018-02-02 21:49:49 -0700 | [diff] [blame] | 159 | return 0; |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | // Ultimately we'd like to preserve enough information about the |
| 163 | // exception and traceback that we could re-constitute (or at least |
| 164 | // simulate, a la traceback.TracebackException), and even chain, a copy |
| 165 | // of the exception in the calling interpreter. |
| 166 | |
| 167 | typedef struct _sharedexception { |
Victor Stinner | f2c3b68 | 2020-05-14 18:46:24 +0200 | [diff] [blame] | 168 | char *name; |
| 169 | char *msg; |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 170 | } _sharedexception; |
| 171 | |
| 172 | static _sharedexception * |
Eric Snow | 4e9da0d | 2018-02-02 21:49:49 -0700 | [diff] [blame] | 173 | _sharedexception_new(void) |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 174 | { |
Victor Stinner | f2c3b68 | 2020-05-14 18:46:24 +0200 | [diff] [blame] | 175 | _sharedexception *err = PyMem_NEW(_sharedexception, 1); |
| 176 | if (err == NULL) { |
Eric Snow | 4e9da0d | 2018-02-02 21:49:49 -0700 | [diff] [blame] | 177 | PyErr_NoMemory(); |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 178 | return NULL; |
| 179 | } |
Victor Stinner | f2c3b68 | 2020-05-14 18:46:24 +0200 | [diff] [blame] | 180 | err->name = NULL; |
| 181 | err->msg = NULL; |
| 182 | return err; |
Eric Snow | 4e9da0d | 2018-02-02 21:49:49 -0700 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | static void |
Victor Stinner | f2c3b68 | 2020-05-14 18:46:24 +0200 | [diff] [blame] | 186 | _sharedexception_clear(_sharedexception *exc) |
Eric Snow | 4e9da0d | 2018-02-02 21:49:49 -0700 | [diff] [blame] | 187 | { |
Victor Stinner | f2c3b68 | 2020-05-14 18:46:24 +0200 | [diff] [blame] | 188 | if (exc->name != NULL) { |
| 189 | PyMem_Free(exc->name); |
Eric Snow | 4e9da0d | 2018-02-02 21:49:49 -0700 | [diff] [blame] | 190 | } |
Victor Stinner | f2c3b68 | 2020-05-14 18:46:24 +0200 | [diff] [blame] | 191 | if (exc->msg != NULL) { |
| 192 | PyMem_Free(exc->msg); |
| 193 | } |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 194 | } |
| 195 | |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 196 | static void |
Victor Stinner | f2c3b68 | 2020-05-14 18:46:24 +0200 | [diff] [blame] | 197 | _sharedexception_free(_sharedexception *exc) |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 198 | { |
Victor Stinner | f2c3b68 | 2020-05-14 18:46:24 +0200 | [diff] [blame] | 199 | _sharedexception_clear(exc); |
| 200 | PyMem_Free(exc); |
| 201 | } |
Eric Snow | a1d9e0a | 2020-05-07 08:56:01 -0600 | [diff] [blame] | 202 | |
Victor Stinner | f2c3b68 | 2020-05-14 18:46:24 +0200 | [diff] [blame] | 203 | static _sharedexception * |
| 204 | _sharedexception_bind(PyObject *exctype, PyObject *exc, PyObject *tb) |
| 205 | { |
| 206 | assert(exctype != NULL); |
| 207 | char *failure = NULL; |
Eric Snow | a1d9e0a | 2020-05-07 08:56:01 -0600 | [diff] [blame] | 208 | |
Victor Stinner | f2c3b68 | 2020-05-14 18:46:24 +0200 | [diff] [blame] | 209 | _sharedexception *err = _sharedexception_new(); |
| 210 | if (err == NULL) { |
| 211 | goto finally; |
| 212 | } |
| 213 | |
| 214 | PyObject *name = PyUnicode_FromFormat("%S", exctype); |
| 215 | if (name == NULL) { |
| 216 | failure = "unable to format exception type name"; |
| 217 | goto finally; |
| 218 | } |
| 219 | err->name = _copy_raw_string(name); |
| 220 | Py_DECREF(name); |
| 221 | if (err->name == NULL) { |
| 222 | if (PyErr_ExceptionMatches(PyExc_MemoryError)) { |
| 223 | failure = "out of memory copying exception type name"; |
| 224 | } else { |
| 225 | failure = "unable to encode and copy exception type name"; |
| 226 | } |
| 227 | goto finally; |
| 228 | } |
| 229 | |
| 230 | if (exc != NULL) { |
| 231 | PyObject *msg = PyUnicode_FromFormat("%S", exc); |
| 232 | if (msg == NULL) { |
| 233 | failure = "unable to format exception message"; |
| 234 | goto finally; |
| 235 | } |
| 236 | err->msg = _copy_raw_string(msg); |
| 237 | Py_DECREF(msg); |
| 238 | if (err->msg == NULL) { |
| 239 | if (PyErr_ExceptionMatches(PyExc_MemoryError)) { |
| 240 | failure = "out of memory copying exception message"; |
| 241 | } else { |
| 242 | failure = "unable to encode and copy exception message"; |
| 243 | } |
| 244 | goto finally; |
Eric Snow | 4e9da0d | 2018-02-02 21:49:49 -0700 | [diff] [blame] | 245 | } |
| 246 | } |
Victor Stinner | f2c3b68 | 2020-05-14 18:46:24 +0200 | [diff] [blame] | 247 | |
| 248 | finally: |
| 249 | if (failure != NULL) { |
| 250 | PyErr_Clear(); |
| 251 | if (err->name != NULL) { |
| 252 | PyMem_Free(err->name); |
| 253 | err->name = NULL; |
| 254 | } |
| 255 | err->msg = failure; |
| 256 | } |
| 257 | return err; |
Eric Snow | a1d9e0a | 2020-05-07 08:56:01 -0600 | [diff] [blame] | 258 | } |
| 259 | |
Victor Stinner | f2c3b68 | 2020-05-14 18:46:24 +0200 | [diff] [blame] | 260 | static void |
| 261 | _sharedexception_apply(_sharedexception *exc, PyObject *wrapperclass) |
Eric Snow | a1d9e0a | 2020-05-07 08:56:01 -0600 | [diff] [blame] | 262 | { |
Victor Stinner | f2c3b68 | 2020-05-14 18:46:24 +0200 | [diff] [blame] | 263 | if (exc->name != NULL) { |
| 264 | if (exc->msg != NULL) { |
| 265 | PyErr_Format(wrapperclass, "%s: %s", exc->name, exc->msg); |
| 266 | } |
| 267 | else { |
| 268 | PyErr_SetString(wrapperclass, exc->name); |
| 269 | } |
Eric Snow | 4e9da0d | 2018-02-02 21:49:49 -0700 | [diff] [blame] | 270 | } |
Victor Stinner | f2c3b68 | 2020-05-14 18:46:24 +0200 | [diff] [blame] | 271 | else if (exc->msg != NULL) { |
| 272 | PyErr_SetString(wrapperclass, exc->msg); |
| 273 | } |
| 274 | else { |
| 275 | PyErr_SetNone(wrapperclass); |
| 276 | } |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 277 | } |
| 278 | |
Eric Snow | 4e9da0d | 2018-02-02 21:49:49 -0700 | [diff] [blame] | 279 | |
| 280 | /* channel-specific code ****************************************************/ |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 281 | |
Eric Snow | 3ab0136 | 2018-05-17 10:27:09 -0400 | [diff] [blame] | 282 | #define CHANNEL_SEND 1 |
| 283 | #define CHANNEL_BOTH 0 |
| 284 | #define CHANNEL_RECV -1 |
| 285 | |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 286 | static PyObject *ChannelError; |
| 287 | static PyObject *ChannelNotFoundError; |
| 288 | static PyObject *ChannelClosedError; |
| 289 | static PyObject *ChannelEmptyError; |
Eric Snow | 3ab0136 | 2018-05-17 10:27:09 -0400 | [diff] [blame] | 290 | static PyObject *ChannelNotEmptyError; |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 291 | |
| 292 | static int |
| 293 | channel_exceptions_init(PyObject *ns) |
| 294 | { |
| 295 | // XXX Move the exceptions into per-module memory? |
| 296 | |
| 297 | // A channel-related operation failed. |
| 298 | ChannelError = PyErr_NewException("_xxsubinterpreters.ChannelError", |
| 299 | PyExc_RuntimeError, NULL); |
| 300 | if (ChannelError == NULL) { |
| 301 | return -1; |
| 302 | } |
| 303 | if (PyDict_SetItemString(ns, "ChannelError", ChannelError) != 0) { |
| 304 | return -1; |
| 305 | } |
| 306 | |
| 307 | // An operation tried to use a channel that doesn't exist. |
| 308 | ChannelNotFoundError = PyErr_NewException( |
| 309 | "_xxsubinterpreters.ChannelNotFoundError", ChannelError, NULL); |
| 310 | if (ChannelNotFoundError == NULL) { |
| 311 | return -1; |
| 312 | } |
| 313 | if (PyDict_SetItemString(ns, "ChannelNotFoundError", ChannelNotFoundError) != 0) { |
| 314 | return -1; |
| 315 | } |
| 316 | |
| 317 | // An operation tried to use a closed channel. |
| 318 | ChannelClosedError = PyErr_NewException( |
| 319 | "_xxsubinterpreters.ChannelClosedError", ChannelError, NULL); |
| 320 | if (ChannelClosedError == NULL) { |
| 321 | return -1; |
| 322 | } |
| 323 | if (PyDict_SetItemString(ns, "ChannelClosedError", ChannelClosedError) != 0) { |
| 324 | return -1; |
| 325 | } |
| 326 | |
| 327 | // An operation tried to pop from an empty channel. |
| 328 | ChannelEmptyError = PyErr_NewException( |
| 329 | "_xxsubinterpreters.ChannelEmptyError", ChannelError, NULL); |
| 330 | if (ChannelEmptyError == NULL) { |
| 331 | return -1; |
| 332 | } |
| 333 | if (PyDict_SetItemString(ns, "ChannelEmptyError", ChannelEmptyError) != 0) { |
| 334 | return -1; |
| 335 | } |
| 336 | |
Eric Snow | 3ab0136 | 2018-05-17 10:27:09 -0400 | [diff] [blame] | 337 | // An operation tried to close a non-empty channel. |
| 338 | ChannelNotEmptyError = PyErr_NewException( |
| 339 | "_xxsubinterpreters.ChannelNotEmptyError", ChannelError, NULL); |
| 340 | if (ChannelNotEmptyError == NULL) { |
| 341 | return -1; |
| 342 | } |
| 343 | if (PyDict_SetItemString(ns, "ChannelNotEmptyError", ChannelNotEmptyError) != 0) { |
| 344 | return -1; |
| 345 | } |
| 346 | |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 347 | return 0; |
| 348 | } |
| 349 | |
Eric Snow | 4e9da0d | 2018-02-02 21:49:49 -0700 | [diff] [blame] | 350 | /* the channel queue */ |
| 351 | |
| 352 | struct _channelitem; |
| 353 | |
| 354 | typedef struct _channelitem { |
| 355 | _PyCrossInterpreterData *data; |
| 356 | struct _channelitem *next; |
| 357 | } _channelitem; |
| 358 | |
| 359 | static _channelitem * |
| 360 | _channelitem_new(void) |
| 361 | { |
| 362 | _channelitem *item = PyMem_NEW(_channelitem, 1); |
| 363 | if (item == NULL) { |
| 364 | PyErr_NoMemory(); |
| 365 | return NULL; |
| 366 | } |
| 367 | item->data = NULL; |
| 368 | item->next = NULL; |
| 369 | return item; |
| 370 | } |
| 371 | |
| 372 | static void |
| 373 | _channelitem_clear(_channelitem *item) |
| 374 | { |
| 375 | if (item->data != NULL) { |
| 376 | _PyCrossInterpreterData_Release(item->data); |
| 377 | PyMem_Free(item->data); |
| 378 | item->data = NULL; |
| 379 | } |
| 380 | item->next = NULL; |
| 381 | } |
| 382 | |
| 383 | static void |
| 384 | _channelitem_free(_channelitem *item) |
| 385 | { |
| 386 | _channelitem_clear(item); |
| 387 | PyMem_Free(item); |
| 388 | } |
| 389 | |
| 390 | static void |
| 391 | _channelitem_free_all(_channelitem *item) |
| 392 | { |
| 393 | while (item != NULL) { |
| 394 | _channelitem *last = item; |
| 395 | item = item->next; |
| 396 | _channelitem_free(last); |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | static _PyCrossInterpreterData * |
| 401 | _channelitem_popped(_channelitem *item) |
| 402 | { |
| 403 | _PyCrossInterpreterData *data = item->data; |
| 404 | item->data = NULL; |
| 405 | _channelitem_free(item); |
| 406 | return data; |
| 407 | } |
| 408 | |
| 409 | typedef struct _channelqueue { |
| 410 | int64_t count; |
| 411 | _channelitem *first; |
| 412 | _channelitem *last; |
| 413 | } _channelqueue; |
| 414 | |
| 415 | static _channelqueue * |
| 416 | _channelqueue_new(void) |
| 417 | { |
| 418 | _channelqueue *queue = PyMem_NEW(_channelqueue, 1); |
| 419 | if (queue == NULL) { |
| 420 | PyErr_NoMemory(); |
| 421 | return NULL; |
| 422 | } |
| 423 | queue->count = 0; |
| 424 | queue->first = NULL; |
| 425 | queue->last = NULL; |
| 426 | return queue; |
| 427 | } |
| 428 | |
| 429 | static void |
| 430 | _channelqueue_clear(_channelqueue *queue) |
| 431 | { |
| 432 | _channelitem_free_all(queue->first); |
| 433 | queue->count = 0; |
| 434 | queue->first = NULL; |
| 435 | queue->last = NULL; |
| 436 | } |
| 437 | |
| 438 | static void |
| 439 | _channelqueue_free(_channelqueue *queue) |
| 440 | { |
| 441 | _channelqueue_clear(queue); |
| 442 | PyMem_Free(queue); |
| 443 | } |
| 444 | |
| 445 | static int |
| 446 | _channelqueue_put(_channelqueue *queue, _PyCrossInterpreterData *data) |
| 447 | { |
| 448 | _channelitem *item = _channelitem_new(); |
| 449 | if (item == NULL) { |
| 450 | return -1; |
| 451 | } |
| 452 | item->data = data; |
| 453 | |
| 454 | queue->count += 1; |
| 455 | if (queue->first == NULL) { |
| 456 | queue->first = item; |
| 457 | } |
| 458 | else { |
| 459 | queue->last->next = item; |
| 460 | } |
| 461 | queue->last = item; |
| 462 | return 0; |
| 463 | } |
| 464 | |
| 465 | static _PyCrossInterpreterData * |
| 466 | _channelqueue_get(_channelqueue *queue) |
| 467 | { |
| 468 | _channelitem *item = queue->first; |
| 469 | if (item == NULL) { |
| 470 | return NULL; |
| 471 | } |
| 472 | queue->first = item->next; |
| 473 | if (queue->last == item) { |
| 474 | queue->last = NULL; |
| 475 | } |
| 476 | queue->count -= 1; |
| 477 | |
| 478 | return _channelitem_popped(item); |
| 479 | } |
| 480 | |
| 481 | /* channel-interpreter associations */ |
| 482 | |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 483 | struct _channelend; |
| 484 | |
| 485 | typedef struct _channelend { |
| 486 | struct _channelend *next; |
| 487 | int64_t interp; |
| 488 | int open; |
| 489 | } _channelend; |
| 490 | |
| 491 | static _channelend * |
| 492 | _channelend_new(int64_t interp) |
| 493 | { |
| 494 | _channelend *end = PyMem_NEW(_channelend, 1); |
| 495 | if (end == NULL) { |
Eric Snow | 4e9da0d | 2018-02-02 21:49:49 -0700 | [diff] [blame] | 496 | PyErr_NoMemory(); |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 497 | return NULL; |
| 498 | } |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 499 | end->next = NULL; |
| 500 | end->interp = interp; |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 501 | end->open = 1; |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 502 | return end; |
| 503 | } |
| 504 | |
| 505 | static void |
Eric Snow | 4e9da0d | 2018-02-02 21:49:49 -0700 | [diff] [blame] | 506 | _channelend_free(_channelend *end) |
| 507 | { |
| 508 | PyMem_Free(end); |
| 509 | } |
| 510 | |
| 511 | static void |
| 512 | _channelend_free_all(_channelend *end) |
| 513 | { |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 514 | while (end != NULL) { |
| 515 | _channelend *last = end; |
| 516 | end = end->next; |
Eric Snow | 4e9da0d | 2018-02-02 21:49:49 -0700 | [diff] [blame] | 517 | _channelend_free(last); |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 518 | } |
| 519 | } |
| 520 | |
| 521 | static _channelend * |
| 522 | _channelend_find(_channelend *first, int64_t interp, _channelend **pprev) |
| 523 | { |
| 524 | _channelend *prev = NULL; |
| 525 | _channelend *end = first; |
| 526 | while (end != NULL) { |
| 527 | if (end->interp == interp) { |
| 528 | break; |
| 529 | } |
| 530 | prev = end; |
| 531 | end = end->next; |
| 532 | } |
| 533 | if (pprev != NULL) { |
| 534 | *pprev = prev; |
| 535 | } |
| 536 | return end; |
| 537 | } |
| 538 | |
Eric Snow | 4e9da0d | 2018-02-02 21:49:49 -0700 | [diff] [blame] | 539 | typedef struct _channelassociations { |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 540 | // Note that the list entries are never removed for interpreter |
Lewis Gaul | f7bbf58 | 2020-04-29 01:18:42 +0100 | [diff] [blame] | 541 | // for which the channel is closed. This should not be a problem in |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 542 | // practice. Also, a channel isn't automatically closed when an |
| 543 | // interpreter is destroyed. |
| 544 | int64_t numsendopen; |
| 545 | int64_t numrecvopen; |
| 546 | _channelend *send; |
| 547 | _channelend *recv; |
Eric Snow | 4e9da0d | 2018-02-02 21:49:49 -0700 | [diff] [blame] | 548 | } _channelends; |
| 549 | |
| 550 | static _channelends * |
| 551 | _channelends_new(void) |
| 552 | { |
| 553 | _channelends *ends = PyMem_NEW(_channelends, 1); |
| 554 | if (ends== NULL) { |
| 555 | return NULL; |
| 556 | } |
| 557 | ends->numsendopen = 0; |
| 558 | ends->numrecvopen = 0; |
| 559 | ends->send = NULL; |
| 560 | ends->recv = NULL; |
| 561 | return ends; |
| 562 | } |
| 563 | |
| 564 | static void |
| 565 | _channelends_clear(_channelends *ends) |
| 566 | { |
| 567 | _channelend_free_all(ends->send); |
| 568 | ends->send = NULL; |
| 569 | ends->numsendopen = 0; |
| 570 | |
| 571 | _channelend_free_all(ends->recv); |
| 572 | ends->recv = NULL; |
| 573 | ends->numrecvopen = 0; |
| 574 | } |
| 575 | |
| 576 | static void |
| 577 | _channelends_free(_channelends *ends) |
| 578 | { |
| 579 | _channelends_clear(ends); |
| 580 | PyMem_Free(ends); |
| 581 | } |
| 582 | |
| 583 | static _channelend * |
| 584 | _channelends_add(_channelends *ends, _channelend *prev, int64_t interp, |
| 585 | int send) |
| 586 | { |
| 587 | _channelend *end = _channelend_new(interp); |
| 588 | if (end == NULL) { |
| 589 | return NULL; |
| 590 | } |
| 591 | |
| 592 | if (prev == NULL) { |
| 593 | if (send) { |
| 594 | ends->send = end; |
| 595 | } |
| 596 | else { |
| 597 | ends->recv = end; |
| 598 | } |
| 599 | } |
| 600 | else { |
| 601 | prev->next = end; |
| 602 | } |
| 603 | if (send) { |
| 604 | ends->numsendopen += 1; |
| 605 | } |
| 606 | else { |
| 607 | ends->numrecvopen += 1; |
| 608 | } |
| 609 | return end; |
| 610 | } |
| 611 | |
| 612 | static int |
| 613 | _channelends_associate(_channelends *ends, int64_t interp, int send) |
| 614 | { |
| 615 | _channelend *prev; |
| 616 | _channelend *end = _channelend_find(send ? ends->send : ends->recv, |
| 617 | interp, &prev); |
| 618 | if (end != NULL) { |
| 619 | if (!end->open) { |
| 620 | PyErr_SetString(ChannelClosedError, "channel already closed"); |
| 621 | return -1; |
| 622 | } |
| 623 | // already associated |
| 624 | return 0; |
| 625 | } |
| 626 | if (_channelends_add(ends, prev, interp, send) == NULL) { |
| 627 | return -1; |
| 628 | } |
| 629 | return 0; |
| 630 | } |
| 631 | |
| 632 | static int |
| 633 | _channelends_is_open(_channelends *ends) |
| 634 | { |
| 635 | if (ends->numsendopen != 0 || ends->numrecvopen != 0) { |
| 636 | return 1; |
| 637 | } |
| 638 | if (ends->send == NULL && ends->recv == NULL) { |
| 639 | return 1; |
| 640 | } |
| 641 | return 0; |
| 642 | } |
| 643 | |
| 644 | static void |
| 645 | _channelends_close_end(_channelends *ends, _channelend *end, int send) |
| 646 | { |
| 647 | end->open = 0; |
| 648 | if (send) { |
| 649 | ends->numsendopen -= 1; |
| 650 | } |
| 651 | else { |
| 652 | ends->numrecvopen -= 1; |
| 653 | } |
| 654 | } |
| 655 | |
| 656 | static int |
| 657 | _channelends_close_interpreter(_channelends *ends, int64_t interp, int which) |
| 658 | { |
| 659 | _channelend *prev; |
| 660 | _channelend *end; |
| 661 | if (which >= 0) { // send/both |
| 662 | end = _channelend_find(ends->send, interp, &prev); |
| 663 | if (end == NULL) { |
| 664 | // never associated so add it |
| 665 | end = _channelends_add(ends, prev, interp, 1); |
| 666 | if (end == NULL) { |
| 667 | return -1; |
| 668 | } |
| 669 | } |
| 670 | _channelends_close_end(ends, end, 1); |
| 671 | } |
| 672 | if (which <= 0) { // recv/both |
| 673 | end = _channelend_find(ends->recv, interp, &prev); |
| 674 | if (end == NULL) { |
| 675 | // never associated so add it |
| 676 | end = _channelends_add(ends, prev, interp, 0); |
| 677 | if (end == NULL) { |
| 678 | return -1; |
| 679 | } |
| 680 | } |
| 681 | _channelends_close_end(ends, end, 0); |
| 682 | } |
| 683 | return 0; |
| 684 | } |
| 685 | |
| 686 | static void |
Eric Snow | 3ab0136 | 2018-05-17 10:27:09 -0400 | [diff] [blame] | 687 | _channelends_close_all(_channelends *ends, int which, int force) |
Eric Snow | 4e9da0d | 2018-02-02 21:49:49 -0700 | [diff] [blame] | 688 | { |
Eric Snow | 3ab0136 | 2018-05-17 10:27:09 -0400 | [diff] [blame] | 689 | // XXX Handle the ends. |
| 690 | // XXX Handle force is True. |
| 691 | |
Eric Snow | 4e9da0d | 2018-02-02 21:49:49 -0700 | [diff] [blame] | 692 | // Ensure all the "send"-associated interpreters are closed. |
| 693 | _channelend *end; |
| 694 | for (end = ends->send; end != NULL; end = end->next) { |
| 695 | _channelends_close_end(ends, end, 1); |
| 696 | } |
| 697 | |
| 698 | // Ensure all the "recv"-associated interpreters are closed. |
| 699 | for (end = ends->recv; end != NULL; end = end->next) { |
| 700 | _channelends_close_end(ends, end, 0); |
| 701 | } |
| 702 | } |
| 703 | |
| 704 | /* channels */ |
| 705 | |
| 706 | struct _channel; |
Eric Snow | 3ab0136 | 2018-05-17 10:27:09 -0400 | [diff] [blame] | 707 | struct _channel_closing; |
| 708 | static void _channel_clear_closing(struct _channel *); |
| 709 | static void _channel_finish_closing(struct _channel *); |
Eric Snow | 4e9da0d | 2018-02-02 21:49:49 -0700 | [diff] [blame] | 710 | |
| 711 | typedef struct _channel { |
| 712 | PyThread_type_lock mutex; |
| 713 | _channelqueue *queue; |
| 714 | _channelends *ends; |
| 715 | int open; |
Eric Snow | 3ab0136 | 2018-05-17 10:27:09 -0400 | [diff] [blame] | 716 | struct _channel_closing *closing; |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 717 | } _PyChannelState; |
| 718 | |
| 719 | static _PyChannelState * |
| 720 | _channel_new(void) |
| 721 | { |
| 722 | _PyChannelState *chan = PyMem_NEW(_PyChannelState, 1); |
| 723 | if (chan == NULL) { |
| 724 | return NULL; |
| 725 | } |
| 726 | chan->mutex = PyThread_allocate_lock(); |
| 727 | if (chan->mutex == NULL) { |
| 728 | PyMem_Free(chan); |
| 729 | PyErr_SetString(ChannelError, |
| 730 | "can't initialize mutex for new channel"); |
| 731 | return NULL; |
| 732 | } |
Eric Snow | 4e9da0d | 2018-02-02 21:49:49 -0700 | [diff] [blame] | 733 | chan->queue = _channelqueue_new(); |
| 734 | if (chan->queue == NULL) { |
| 735 | PyMem_Free(chan); |
| 736 | return NULL; |
| 737 | } |
| 738 | chan->ends = _channelends_new(); |
| 739 | if (chan->ends == NULL) { |
| 740 | _channelqueue_free(chan->queue); |
| 741 | PyMem_Free(chan); |
| 742 | return NULL; |
| 743 | } |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 744 | chan->open = 1; |
Eric Snow | 3ab0136 | 2018-05-17 10:27:09 -0400 | [diff] [blame] | 745 | chan->closing = NULL; |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 746 | return chan; |
| 747 | } |
| 748 | |
Eric Snow | 4e9da0d | 2018-02-02 21:49:49 -0700 | [diff] [blame] | 749 | static void |
| 750 | _channel_free(_PyChannelState *chan) |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 751 | { |
Eric Snow | 3ab0136 | 2018-05-17 10:27:09 -0400 | [diff] [blame] | 752 | _channel_clear_closing(chan); |
Eric Snow | 4e9da0d | 2018-02-02 21:49:49 -0700 | [diff] [blame] | 753 | PyThread_acquire_lock(chan->mutex, WAIT_LOCK); |
| 754 | _channelqueue_free(chan->queue); |
| 755 | _channelends_free(chan->ends); |
| 756 | PyThread_release_lock(chan->mutex); |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 757 | |
Eric Snow | 4e9da0d | 2018-02-02 21:49:49 -0700 | [diff] [blame] | 758 | PyThread_free_lock(chan->mutex); |
| 759 | PyMem_Free(chan); |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 760 | } |
| 761 | |
Eric Snow | 4e9da0d | 2018-02-02 21:49:49 -0700 | [diff] [blame] | 762 | static int |
| 763 | _channel_add(_PyChannelState *chan, int64_t interp, |
| 764 | _PyCrossInterpreterData *data) |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 765 | { |
Eric Snow | 4e9da0d | 2018-02-02 21:49:49 -0700 | [diff] [blame] | 766 | int res = -1; |
| 767 | PyThread_acquire_lock(chan->mutex, WAIT_LOCK); |
| 768 | |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 769 | if (!chan->open) { |
| 770 | PyErr_SetString(ChannelClosedError, "channel closed"); |
Eric Snow | 4e9da0d | 2018-02-02 21:49:49 -0700 | [diff] [blame] | 771 | goto done; |
| 772 | } |
| 773 | if (_channelends_associate(chan->ends, interp, 1) != 0) { |
| 774 | goto done; |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 775 | } |
| 776 | |
Eric Snow | 4e9da0d | 2018-02-02 21:49:49 -0700 | [diff] [blame] | 777 | if (_channelqueue_put(chan->queue, data) != 0) { |
| 778 | goto done; |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 779 | } |
Eric Snow | 4e9da0d | 2018-02-02 21:49:49 -0700 | [diff] [blame] | 780 | |
| 781 | res = 0; |
| 782 | done: |
| 783 | PyThread_release_lock(chan->mutex); |
| 784 | return res; |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 785 | } |
| 786 | |
Eric Snow | 4e9da0d | 2018-02-02 21:49:49 -0700 | [diff] [blame] | 787 | static _PyCrossInterpreterData * |
| 788 | _channel_next(_PyChannelState *chan, int64_t interp) |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 789 | { |
Eric Snow | 4e9da0d | 2018-02-02 21:49:49 -0700 | [diff] [blame] | 790 | _PyCrossInterpreterData *data = NULL; |
| 791 | PyThread_acquire_lock(chan->mutex, WAIT_LOCK); |
| 792 | |
| 793 | if (!chan->open) { |
| 794 | PyErr_SetString(ChannelClosedError, "channel closed"); |
| 795 | goto done; |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 796 | } |
Eric Snow | 4e9da0d | 2018-02-02 21:49:49 -0700 | [diff] [blame] | 797 | if (_channelends_associate(chan->ends, interp, 0) != 0) { |
| 798 | goto done; |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 799 | } |
Eric Snow | 4e9da0d | 2018-02-02 21:49:49 -0700 | [diff] [blame] | 800 | |
| 801 | data = _channelqueue_get(chan->queue); |
Eric Snow | 3ab0136 | 2018-05-17 10:27:09 -0400 | [diff] [blame] | 802 | if (data == NULL && !PyErr_Occurred() && chan->closing != NULL) { |
| 803 | chan->open = 0; |
| 804 | } |
| 805 | |
Eric Snow | 4e9da0d | 2018-02-02 21:49:49 -0700 | [diff] [blame] | 806 | done: |
| 807 | PyThread_release_lock(chan->mutex); |
Eric Snow | 3ab0136 | 2018-05-17 10:27:09 -0400 | [diff] [blame] | 808 | if (chan->queue->count == 0) { |
| 809 | _channel_finish_closing(chan); |
| 810 | } |
Eric Snow | 4e9da0d | 2018-02-02 21:49:49 -0700 | [diff] [blame] | 811 | return data; |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 812 | } |
| 813 | |
| 814 | static int |
Eric Snow | 3ab0136 | 2018-05-17 10:27:09 -0400 | [diff] [blame] | 815 | _channel_close_interpreter(_PyChannelState *chan, int64_t interp, int end) |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 816 | { |
| 817 | PyThread_acquire_lock(chan->mutex, WAIT_LOCK); |
| 818 | |
| 819 | int res = -1; |
| 820 | if (!chan->open) { |
| 821 | PyErr_SetString(ChannelClosedError, "channel already closed"); |
| 822 | goto done; |
| 823 | } |
| 824 | |
Eric Snow | 3ab0136 | 2018-05-17 10:27:09 -0400 | [diff] [blame] | 825 | if (_channelends_close_interpreter(chan->ends, interp, end) != 0) { |
Eric Snow | 4e9da0d | 2018-02-02 21:49:49 -0700 | [diff] [blame] | 826 | goto done; |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 827 | } |
Eric Snow | 4e9da0d | 2018-02-02 21:49:49 -0700 | [diff] [blame] | 828 | chan->open = _channelends_is_open(chan->ends); |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 829 | |
| 830 | res = 0; |
| 831 | done: |
| 832 | PyThread_release_lock(chan->mutex); |
| 833 | return res; |
| 834 | } |
| 835 | |
| 836 | static int |
Eric Snow | 3ab0136 | 2018-05-17 10:27:09 -0400 | [diff] [blame] | 837 | _channel_close_all(_PyChannelState *chan, int end, int force) |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 838 | { |
| 839 | int res = -1; |
| 840 | PyThread_acquire_lock(chan->mutex, WAIT_LOCK); |
| 841 | |
| 842 | if (!chan->open) { |
| 843 | PyErr_SetString(ChannelClosedError, "channel already closed"); |
| 844 | goto done; |
| 845 | } |
| 846 | |
Eric Snow | 3ab0136 | 2018-05-17 10:27:09 -0400 | [diff] [blame] | 847 | if (!force && chan->queue->count > 0) { |
| 848 | PyErr_SetString(ChannelNotEmptyError, |
| 849 | "may not be closed if not empty (try force=True)"); |
| 850 | goto done; |
| 851 | } |
| 852 | |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 853 | chan->open = 0; |
| 854 | |
| 855 | // We *could* also just leave these in place, since we've marked |
| 856 | // the channel as closed already. |
Eric Snow | 3ab0136 | 2018-05-17 10:27:09 -0400 | [diff] [blame] | 857 | _channelends_close_all(chan->ends, end, force); |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 858 | |
| 859 | res = 0; |
| 860 | done: |
| 861 | PyThread_release_lock(chan->mutex); |
| 862 | return res; |
| 863 | } |
| 864 | |
Eric Snow | 4e9da0d | 2018-02-02 21:49:49 -0700 | [diff] [blame] | 865 | /* the set of channels */ |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 866 | |
| 867 | struct _channelref; |
| 868 | |
| 869 | typedef struct _channelref { |
| 870 | int64_t id; |
| 871 | _PyChannelState *chan; |
| 872 | struct _channelref *next; |
| 873 | Py_ssize_t objcount; |
| 874 | } _channelref; |
| 875 | |
| 876 | static _channelref * |
| 877 | _channelref_new(int64_t id, _PyChannelState *chan) |
| 878 | { |
| 879 | _channelref *ref = PyMem_NEW(_channelref, 1); |
| 880 | if (ref == NULL) { |
| 881 | return NULL; |
| 882 | } |
| 883 | ref->id = id; |
| 884 | ref->chan = chan; |
| 885 | ref->next = NULL; |
| 886 | ref->objcount = 0; |
| 887 | return ref; |
| 888 | } |
| 889 | |
Eric Snow | 4e9da0d | 2018-02-02 21:49:49 -0700 | [diff] [blame] | 890 | //static void |
| 891 | //_channelref_clear(_channelref *ref) |
| 892 | //{ |
| 893 | // ref->id = -1; |
| 894 | // ref->chan = NULL; |
| 895 | // ref->next = NULL; |
| 896 | // ref->objcount = 0; |
| 897 | //} |
| 898 | |
| 899 | static void |
| 900 | _channelref_free(_channelref *ref) |
| 901 | { |
Eric Snow | 3ab0136 | 2018-05-17 10:27:09 -0400 | [diff] [blame] | 902 | if (ref->chan != NULL) { |
| 903 | _channel_clear_closing(ref->chan); |
| 904 | } |
Eric Snow | 4e9da0d | 2018-02-02 21:49:49 -0700 | [diff] [blame] | 905 | //_channelref_clear(ref); |
| 906 | PyMem_Free(ref); |
| 907 | } |
| 908 | |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 909 | static _channelref * |
| 910 | _channelref_find(_channelref *first, int64_t id, _channelref **pprev) |
| 911 | { |
| 912 | _channelref *prev = NULL; |
| 913 | _channelref *ref = first; |
| 914 | while (ref != NULL) { |
| 915 | if (ref->id == id) { |
| 916 | break; |
| 917 | } |
| 918 | prev = ref; |
| 919 | ref = ref->next; |
| 920 | } |
| 921 | if (pprev != NULL) { |
| 922 | *pprev = prev; |
| 923 | } |
| 924 | return ref; |
| 925 | } |
| 926 | |
| 927 | typedef struct _channels { |
| 928 | PyThread_type_lock mutex; |
| 929 | _channelref *head; |
| 930 | int64_t numopen; |
| 931 | int64_t next_id; |
| 932 | } _channels; |
| 933 | |
| 934 | static int |
| 935 | _channels_init(_channels *channels) |
| 936 | { |
| 937 | if (channels->mutex == NULL) { |
| 938 | channels->mutex = PyThread_allocate_lock(); |
| 939 | if (channels->mutex == NULL) { |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 940 | PyErr_SetString(ChannelError, |
| 941 | "can't initialize mutex for channel management"); |
| 942 | return -1; |
| 943 | } |
| 944 | } |
| 945 | channels->head = NULL; |
| 946 | channels->numopen = 0; |
| 947 | channels->next_id = 0; |
| 948 | return 0; |
| 949 | } |
| 950 | |
| 951 | static int64_t |
| 952 | _channels_next_id(_channels *channels) // needs lock |
| 953 | { |
| 954 | int64_t id = channels->next_id; |
| 955 | if (id < 0) { |
| 956 | /* overflow */ |
| 957 | PyErr_SetString(ChannelError, |
| 958 | "failed to get a channel ID"); |
| 959 | return -1; |
| 960 | } |
| 961 | channels->next_id += 1; |
| 962 | return id; |
| 963 | } |
| 964 | |
| 965 | static _PyChannelState * |
| 966 | _channels_lookup(_channels *channels, int64_t id, PyThread_type_lock *pmutex) |
| 967 | { |
| 968 | _PyChannelState *chan = NULL; |
| 969 | PyThread_acquire_lock(channels->mutex, WAIT_LOCK); |
| 970 | if (pmutex != NULL) { |
| 971 | *pmutex = NULL; |
| 972 | } |
| 973 | |
| 974 | _channelref *ref = _channelref_find(channels->head, id, NULL); |
| 975 | if (ref == NULL) { |
Eric Snow | ab4a198 | 2018-06-13 08:02:39 -0600 | [diff] [blame] | 976 | PyErr_Format(ChannelNotFoundError, "channel %" PRId64 " not found", id); |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 977 | goto done; |
| 978 | } |
| 979 | if (ref->chan == NULL || !ref->chan->open) { |
Eric Snow | ab4a198 | 2018-06-13 08:02:39 -0600 | [diff] [blame] | 980 | PyErr_Format(ChannelClosedError, "channel %" PRId64 " closed", id); |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 981 | goto done; |
| 982 | } |
| 983 | |
| 984 | if (pmutex != NULL) { |
| 985 | // The mutex will be closed by the caller. |
| 986 | *pmutex = channels->mutex; |
| 987 | } |
| 988 | |
| 989 | chan = ref->chan; |
| 990 | done: |
| 991 | if (pmutex == NULL || *pmutex == NULL) { |
| 992 | PyThread_release_lock(channels->mutex); |
| 993 | } |
| 994 | return chan; |
| 995 | } |
| 996 | |
| 997 | static int64_t |
| 998 | _channels_add(_channels *channels, _PyChannelState *chan) |
| 999 | { |
| 1000 | int64_t cid = -1; |
| 1001 | PyThread_acquire_lock(channels->mutex, WAIT_LOCK); |
| 1002 | |
| 1003 | // Create a new ref. |
| 1004 | int64_t id = _channels_next_id(channels); |
| 1005 | if (id < 0) { |
| 1006 | goto done; |
| 1007 | } |
| 1008 | _channelref *ref = _channelref_new(id, chan); |
| 1009 | if (ref == NULL) { |
| 1010 | goto done; |
| 1011 | } |
| 1012 | |
| 1013 | // Add it to the list. |
| 1014 | // We assume that the channel is a new one (not already in the list). |
| 1015 | ref->next = channels->head; |
| 1016 | channels->head = ref; |
| 1017 | channels->numopen += 1; |
| 1018 | |
| 1019 | cid = id; |
| 1020 | done: |
| 1021 | PyThread_release_lock(channels->mutex); |
| 1022 | return cid; |
| 1023 | } |
| 1024 | |
Eric Snow | 3ab0136 | 2018-05-17 10:27:09 -0400 | [diff] [blame] | 1025 | /* forward */ |
| 1026 | static int _channel_set_closing(struct _channelref *, PyThread_type_lock); |
| 1027 | |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 1028 | static int |
Eric Snow | 3ab0136 | 2018-05-17 10:27:09 -0400 | [diff] [blame] | 1029 | _channels_close(_channels *channels, int64_t cid, _PyChannelState **pchan, |
| 1030 | int end, int force) |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 1031 | { |
| 1032 | int res = -1; |
| 1033 | PyThread_acquire_lock(channels->mutex, WAIT_LOCK); |
| 1034 | if (pchan != NULL) { |
| 1035 | *pchan = NULL; |
| 1036 | } |
| 1037 | |
| 1038 | _channelref *ref = _channelref_find(channels->head, cid, NULL); |
| 1039 | if (ref == NULL) { |
Eric Snow | ab4a198 | 2018-06-13 08:02:39 -0600 | [diff] [blame] | 1040 | PyErr_Format(ChannelNotFoundError, "channel %" PRId64 " not found", cid); |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 1041 | goto done; |
| 1042 | } |
| 1043 | |
| 1044 | if (ref->chan == NULL) { |
Eric Snow | ab4a198 | 2018-06-13 08:02:39 -0600 | [diff] [blame] | 1045 | PyErr_Format(ChannelClosedError, "channel %" PRId64 " closed", cid); |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 1046 | goto done; |
| 1047 | } |
Eric Snow | 3ab0136 | 2018-05-17 10:27:09 -0400 | [diff] [blame] | 1048 | else if (!force && end == CHANNEL_SEND && ref->chan->closing != NULL) { |
Eric Snow | ab4a198 | 2018-06-13 08:02:39 -0600 | [diff] [blame] | 1049 | PyErr_Format(ChannelClosedError, "channel %" PRId64 " closed", cid); |
Eric Snow | 3ab0136 | 2018-05-17 10:27:09 -0400 | [diff] [blame] | 1050 | goto done; |
| 1051 | } |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 1052 | else { |
Eric Snow | 3ab0136 | 2018-05-17 10:27:09 -0400 | [diff] [blame] | 1053 | if (_channel_close_all(ref->chan, end, force) != 0) { |
| 1054 | if (end == CHANNEL_SEND && |
| 1055 | PyErr_ExceptionMatches(ChannelNotEmptyError)) { |
| 1056 | if (ref->chan->closing != NULL) { |
Eric Snow | 6854e80 | 2018-06-01 16:26:01 -0600 | [diff] [blame] | 1057 | PyErr_Format(ChannelClosedError, |
Eric Snow | ab4a198 | 2018-06-13 08:02:39 -0600 | [diff] [blame] | 1058 | "channel %" PRId64 " closed", cid); |
Eric Snow | 3ab0136 | 2018-05-17 10:27:09 -0400 | [diff] [blame] | 1059 | goto done; |
| 1060 | } |
| 1061 | // Mark the channel as closing and return. The channel |
| 1062 | // will be cleaned up in _channel_next(). |
| 1063 | PyErr_Clear(); |
| 1064 | if (_channel_set_closing(ref, channels->mutex) != 0) { |
| 1065 | goto done; |
| 1066 | } |
| 1067 | if (pchan != NULL) { |
| 1068 | *pchan = ref->chan; |
| 1069 | } |
| 1070 | res = 0; |
| 1071 | } |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 1072 | goto done; |
| 1073 | } |
| 1074 | if (pchan != NULL) { |
| 1075 | *pchan = ref->chan; |
| 1076 | } |
Eric Snow | 3ab0136 | 2018-05-17 10:27:09 -0400 | [diff] [blame] | 1077 | else { |
Eric Snow | 4e9da0d | 2018-02-02 21:49:49 -0700 | [diff] [blame] | 1078 | _channel_free(ref->chan); |
| 1079 | } |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 1080 | ref->chan = NULL; |
| 1081 | } |
| 1082 | |
| 1083 | res = 0; |
| 1084 | done: |
| 1085 | PyThread_release_lock(channels->mutex); |
| 1086 | return res; |
| 1087 | } |
| 1088 | |
| 1089 | static void |
| 1090 | _channels_remove_ref(_channels *channels, _channelref *ref, _channelref *prev, |
| 1091 | _PyChannelState **pchan) |
| 1092 | { |
| 1093 | if (ref == channels->head) { |
| 1094 | channels->head = ref->next; |
| 1095 | } |
| 1096 | else { |
| 1097 | prev->next = ref->next; |
| 1098 | } |
| 1099 | channels->numopen -= 1; |
| 1100 | |
| 1101 | if (pchan != NULL) { |
| 1102 | *pchan = ref->chan; |
| 1103 | } |
Eric Snow | 4e9da0d | 2018-02-02 21:49:49 -0700 | [diff] [blame] | 1104 | _channelref_free(ref); |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 1105 | } |
| 1106 | |
| 1107 | static int |
| 1108 | _channels_remove(_channels *channels, int64_t id, _PyChannelState **pchan) |
| 1109 | { |
| 1110 | int res = -1; |
| 1111 | PyThread_acquire_lock(channels->mutex, WAIT_LOCK); |
| 1112 | |
| 1113 | if (pchan != NULL) { |
| 1114 | *pchan = NULL; |
| 1115 | } |
| 1116 | |
| 1117 | _channelref *prev = NULL; |
| 1118 | _channelref *ref = _channelref_find(channels->head, id, &prev); |
| 1119 | if (ref == NULL) { |
Eric Snow | ab4a198 | 2018-06-13 08:02:39 -0600 | [diff] [blame] | 1120 | PyErr_Format(ChannelNotFoundError, "channel %" PRId64 " not found", id); |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 1121 | goto done; |
| 1122 | } |
| 1123 | |
| 1124 | _channels_remove_ref(channels, ref, prev, pchan); |
| 1125 | |
| 1126 | res = 0; |
| 1127 | done: |
| 1128 | PyThread_release_lock(channels->mutex); |
| 1129 | return res; |
| 1130 | } |
| 1131 | |
| 1132 | static int |
| 1133 | _channels_add_id_object(_channels *channels, int64_t id) |
| 1134 | { |
| 1135 | int res = -1; |
| 1136 | PyThread_acquire_lock(channels->mutex, WAIT_LOCK); |
| 1137 | |
| 1138 | _channelref *ref = _channelref_find(channels->head, id, NULL); |
| 1139 | if (ref == NULL) { |
Eric Snow | ab4a198 | 2018-06-13 08:02:39 -0600 | [diff] [blame] | 1140 | PyErr_Format(ChannelNotFoundError, "channel %" PRId64 " not found", id); |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 1141 | goto done; |
| 1142 | } |
| 1143 | ref->objcount += 1; |
| 1144 | |
| 1145 | res = 0; |
| 1146 | done: |
| 1147 | PyThread_release_lock(channels->mutex); |
| 1148 | return res; |
| 1149 | } |
| 1150 | |
| 1151 | static void |
| 1152 | _channels_drop_id_object(_channels *channels, int64_t id) |
| 1153 | { |
| 1154 | PyThread_acquire_lock(channels->mutex, WAIT_LOCK); |
| 1155 | |
| 1156 | _channelref *prev = NULL; |
| 1157 | _channelref *ref = _channelref_find(channels->head, id, &prev); |
| 1158 | if (ref == NULL) { |
| 1159 | // Already destroyed. |
| 1160 | goto done; |
| 1161 | } |
| 1162 | ref->objcount -= 1; |
| 1163 | |
| 1164 | // Destroy if no longer used. |
| 1165 | if (ref->objcount == 0) { |
| 1166 | _PyChannelState *chan = NULL; |
| 1167 | _channels_remove_ref(channels, ref, prev, &chan); |
| 1168 | if (chan != NULL) { |
| 1169 | _channel_free(chan); |
| 1170 | } |
| 1171 | } |
| 1172 | |
| 1173 | done: |
| 1174 | PyThread_release_lock(channels->mutex); |
| 1175 | } |
| 1176 | |
Benjamin Peterson | 4629c0d | 2018-07-06 23:28:35 -0700 | [diff] [blame] | 1177 | static int64_t * |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 1178 | _channels_list_all(_channels *channels, int64_t *count) |
| 1179 | { |
| 1180 | int64_t *cids = NULL; |
| 1181 | PyThread_acquire_lock(channels->mutex, WAIT_LOCK); |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 1182 | int64_t *ids = PyMem_NEW(int64_t, (Py_ssize_t)(channels->numopen)); |
| 1183 | if (ids == NULL) { |
| 1184 | goto done; |
| 1185 | } |
| 1186 | _channelref *ref = channels->head; |
| 1187 | for (int64_t i=0; ref != NULL; ref = ref->next, i++) { |
| 1188 | ids[i] = ref->id; |
| 1189 | } |
| 1190 | *count = channels->numopen; |
| 1191 | |
| 1192 | cids = ids; |
| 1193 | done: |
| 1194 | PyThread_release_lock(channels->mutex); |
| 1195 | return cids; |
| 1196 | } |
| 1197 | |
Eric Snow | 3ab0136 | 2018-05-17 10:27:09 -0400 | [diff] [blame] | 1198 | /* support for closing non-empty channels */ |
| 1199 | |
| 1200 | struct _channel_closing { |
| 1201 | struct _channelref *ref; |
| 1202 | }; |
| 1203 | |
| 1204 | static int |
| 1205 | _channel_set_closing(struct _channelref *ref, PyThread_type_lock mutex) { |
| 1206 | struct _channel *chan = ref->chan; |
| 1207 | if (chan == NULL) { |
| 1208 | // already closed |
| 1209 | return 0; |
| 1210 | } |
| 1211 | int res = -1; |
| 1212 | PyThread_acquire_lock(chan->mutex, WAIT_LOCK); |
| 1213 | if (chan->closing != NULL) { |
| 1214 | PyErr_SetString(ChannelClosedError, "channel closed"); |
| 1215 | goto done; |
| 1216 | } |
| 1217 | chan->closing = PyMem_NEW(struct _channel_closing, 1); |
| 1218 | if (chan->closing == NULL) { |
| 1219 | goto done; |
| 1220 | } |
| 1221 | chan->closing->ref = ref; |
| 1222 | |
| 1223 | res = 0; |
| 1224 | done: |
| 1225 | PyThread_release_lock(chan->mutex); |
| 1226 | return res; |
| 1227 | } |
| 1228 | |
| 1229 | static void |
| 1230 | _channel_clear_closing(struct _channel *chan) { |
| 1231 | PyThread_acquire_lock(chan->mutex, WAIT_LOCK); |
| 1232 | if (chan->closing != NULL) { |
| 1233 | PyMem_Free(chan->closing); |
| 1234 | chan->closing = NULL; |
| 1235 | } |
| 1236 | PyThread_release_lock(chan->mutex); |
| 1237 | } |
| 1238 | |
| 1239 | static void |
| 1240 | _channel_finish_closing(struct _channel *chan) { |
| 1241 | struct _channel_closing *closing = chan->closing; |
| 1242 | if (closing == NULL) { |
| 1243 | return; |
| 1244 | } |
| 1245 | _channelref *ref = closing->ref; |
| 1246 | _channel_clear_closing(chan); |
| 1247 | // Do the things that would have been done in _channels_close(). |
| 1248 | ref->chan = NULL; |
| 1249 | _channel_free(chan); |
Zackery Spytz | 1a2252e | 2019-05-06 10:56:51 -0600 | [diff] [blame] | 1250 | } |
Eric Snow | 3ab0136 | 2018-05-17 10:27:09 -0400 | [diff] [blame] | 1251 | |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 1252 | /* "high"-level channel-related functions */ |
| 1253 | |
| 1254 | static int64_t |
| 1255 | _channel_create(_channels *channels) |
| 1256 | { |
| 1257 | _PyChannelState *chan = _channel_new(); |
| 1258 | if (chan == NULL) { |
| 1259 | return -1; |
| 1260 | } |
| 1261 | int64_t id = _channels_add(channels, chan); |
| 1262 | if (id < 0) { |
| 1263 | _channel_free(chan); |
| 1264 | return -1; |
| 1265 | } |
| 1266 | return id; |
| 1267 | } |
| 1268 | |
| 1269 | static int |
| 1270 | _channel_destroy(_channels *channels, int64_t id) |
| 1271 | { |
| 1272 | _PyChannelState *chan = NULL; |
| 1273 | if (_channels_remove(channels, id, &chan) != 0) { |
| 1274 | return -1; |
| 1275 | } |
| 1276 | if (chan != NULL) { |
| 1277 | _channel_free(chan); |
| 1278 | } |
| 1279 | return 0; |
| 1280 | } |
| 1281 | |
| 1282 | static int |
| 1283 | _channel_send(_channels *channels, int64_t id, PyObject *obj) |
| 1284 | { |
| 1285 | PyInterpreterState *interp = _get_current(); |
| 1286 | if (interp == NULL) { |
| 1287 | return -1; |
| 1288 | } |
| 1289 | |
| 1290 | // Look up the channel. |
| 1291 | PyThread_type_lock mutex = NULL; |
| 1292 | _PyChannelState *chan = _channels_lookup(channels, id, &mutex); |
| 1293 | if (chan == NULL) { |
| 1294 | return -1; |
| 1295 | } |
| 1296 | // Past this point we are responsible for releasing the mutex. |
| 1297 | |
Eric Snow | 3ab0136 | 2018-05-17 10:27:09 -0400 | [diff] [blame] | 1298 | if (chan->closing != NULL) { |
Eric Snow | ab4a198 | 2018-06-13 08:02:39 -0600 | [diff] [blame] | 1299 | PyErr_Format(ChannelClosedError, "channel %" PRId64 " closed", id); |
Eric Snow | 3ab0136 | 2018-05-17 10:27:09 -0400 | [diff] [blame] | 1300 | PyThread_release_lock(mutex); |
| 1301 | return -1; |
| 1302 | } |
| 1303 | |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 1304 | // Convert the object to cross-interpreter data. |
| 1305 | _PyCrossInterpreterData *data = PyMem_NEW(_PyCrossInterpreterData, 1); |
| 1306 | if (data == NULL) { |
| 1307 | PyThread_release_lock(mutex); |
| 1308 | return -1; |
| 1309 | } |
| 1310 | if (_PyObject_GetCrossInterpreterData(obj, data) != 0) { |
Victor Stinner | 4d61e6e | 2019-03-04 14:21:28 +0100 | [diff] [blame] | 1311 | PyThread_release_lock(mutex); |
Eric Snow | c11183c | 2019-03-15 16:35:46 -0600 | [diff] [blame] | 1312 | PyMem_Free(data); |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 1313 | return -1; |
| 1314 | } |
| 1315 | |
| 1316 | // Add the data to the channel. |
Eric Snow | c11183c | 2019-03-15 16:35:46 -0600 | [diff] [blame] | 1317 | int res = _channel_add(chan, PyInterpreterState_GetID(interp), data); |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 1318 | PyThread_release_lock(mutex); |
| 1319 | if (res != 0) { |
| 1320 | _PyCrossInterpreterData_Release(data); |
| 1321 | PyMem_Free(data); |
| 1322 | return -1; |
| 1323 | } |
| 1324 | |
| 1325 | return 0; |
| 1326 | } |
| 1327 | |
| 1328 | static PyObject * |
| 1329 | _channel_recv(_channels *channels, int64_t id) |
| 1330 | { |
| 1331 | PyInterpreterState *interp = _get_current(); |
| 1332 | if (interp == NULL) { |
| 1333 | return NULL; |
| 1334 | } |
| 1335 | |
| 1336 | // Look up the channel. |
| 1337 | PyThread_type_lock mutex = NULL; |
| 1338 | _PyChannelState *chan = _channels_lookup(channels, id, &mutex); |
| 1339 | if (chan == NULL) { |
| 1340 | return NULL; |
| 1341 | } |
| 1342 | // Past this point we are responsible for releasing the mutex. |
| 1343 | |
| 1344 | // Pop off the next item from the channel. |
Eric Snow | c11183c | 2019-03-15 16:35:46 -0600 | [diff] [blame] | 1345 | _PyCrossInterpreterData *data = _channel_next(chan, PyInterpreterState_GetID(interp)); |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 1346 | PyThread_release_lock(mutex); |
| 1347 | if (data == NULL) { |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 1348 | return NULL; |
| 1349 | } |
| 1350 | |
| 1351 | // Convert the data back to an object. |
| 1352 | PyObject *obj = _PyCrossInterpreterData_NewObject(data); |
Eric Snow | 5e8c691 | 2020-04-28 17:11:32 -0600 | [diff] [blame] | 1353 | _PyCrossInterpreterData_Release(data); |
| 1354 | PyMem_Free(data); |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 1355 | if (obj == NULL) { |
| 1356 | return NULL; |
| 1357 | } |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 1358 | |
| 1359 | return obj; |
| 1360 | } |
| 1361 | |
| 1362 | static int |
| 1363 | _channel_drop(_channels *channels, int64_t id, int send, int recv) |
| 1364 | { |
| 1365 | PyInterpreterState *interp = _get_current(); |
| 1366 | if (interp == NULL) { |
| 1367 | return -1; |
| 1368 | } |
| 1369 | |
| 1370 | // Look up the channel. |
| 1371 | PyThread_type_lock mutex = NULL; |
| 1372 | _PyChannelState *chan = _channels_lookup(channels, id, &mutex); |
| 1373 | if (chan == NULL) { |
| 1374 | return -1; |
| 1375 | } |
| 1376 | // Past this point we are responsible for releasing the mutex. |
| 1377 | |
| 1378 | // Close one or both of the two ends. |
Eric Snow | c11183c | 2019-03-15 16:35:46 -0600 | [diff] [blame] | 1379 | int res = _channel_close_interpreter(chan, PyInterpreterState_GetID(interp), send-recv); |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 1380 | PyThread_release_lock(mutex); |
| 1381 | return res; |
| 1382 | } |
| 1383 | |
| 1384 | static int |
Eric Snow | 3ab0136 | 2018-05-17 10:27:09 -0400 | [diff] [blame] | 1385 | _channel_close(_channels *channels, int64_t id, int end, int force) |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 1386 | { |
Eric Snow | 3ab0136 | 2018-05-17 10:27:09 -0400 | [diff] [blame] | 1387 | return _channels_close(channels, id, NULL, end, force); |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 1388 | } |
| 1389 | |
Lewis Gaul | f7bbf58 | 2020-04-29 01:18:42 +0100 | [diff] [blame] | 1390 | static int |
| 1391 | _channel_is_associated(_channels *channels, int64_t cid, int64_t interp, |
| 1392 | int send) |
| 1393 | { |
| 1394 | _PyChannelState *chan = _channels_lookup(channels, cid, NULL); |
| 1395 | if (chan == NULL) { |
| 1396 | return -1; |
| 1397 | } else if (send && chan->closing != NULL) { |
| 1398 | PyErr_Format(ChannelClosedError, "channel %" PRId64 " closed", cid); |
| 1399 | return -1; |
| 1400 | } |
| 1401 | |
| 1402 | _channelend *end = _channelend_find(send ? chan->ends->send : chan->ends->recv, |
| 1403 | interp, NULL); |
| 1404 | |
| 1405 | return (end != NULL && end->open); |
| 1406 | } |
| 1407 | |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 1408 | /* ChannelID class */ |
| 1409 | |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 1410 | static PyTypeObject ChannelIDtype; |
| 1411 | |
| 1412 | typedef struct channelid { |
| 1413 | PyObject_HEAD |
| 1414 | int64_t id; |
| 1415 | int end; |
Eric Snow | 6d2cd90 | 2018-05-16 15:04:57 -0400 | [diff] [blame] | 1416 | int resolve; |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 1417 | _channels *channels; |
| 1418 | } channelid; |
| 1419 | |
Serhiy Storchaka | bf16991 | 2019-09-13 22:50:27 +0300 | [diff] [blame] | 1420 | static int |
| 1421 | channel_id_converter(PyObject *arg, void *ptr) |
| 1422 | { |
| 1423 | int64_t cid; |
| 1424 | if (PyObject_TypeCheck(arg, &ChannelIDtype)) { |
| 1425 | cid = ((channelid *)arg)->id; |
| 1426 | } |
| 1427 | else if (PyIndex_Check(arg)) { |
| 1428 | cid = PyLong_AsLongLong(arg); |
| 1429 | if (cid == -1 && PyErr_Occurred()) { |
| 1430 | return 0; |
| 1431 | } |
| 1432 | if (cid < 0) { |
| 1433 | PyErr_Format(PyExc_ValueError, |
| 1434 | "channel ID must be a non-negative int, got %R", arg); |
| 1435 | return 0; |
| 1436 | } |
| 1437 | } |
| 1438 | else { |
| 1439 | PyErr_Format(PyExc_TypeError, |
| 1440 | "channel ID must be an int, got %.100s", |
Victor Stinner | daa9756 | 2020-02-07 03:37:06 +0100 | [diff] [blame] | 1441 | Py_TYPE(arg)->tp_name); |
Serhiy Storchaka | bf16991 | 2019-09-13 22:50:27 +0300 | [diff] [blame] | 1442 | return 0; |
| 1443 | } |
| 1444 | *(int64_t *)ptr = cid; |
| 1445 | return 1; |
| 1446 | } |
| 1447 | |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 1448 | static channelid * |
| 1449 | newchannelid(PyTypeObject *cls, int64_t cid, int end, _channels *channels, |
Eric Snow | 6d2cd90 | 2018-05-16 15:04:57 -0400 | [diff] [blame] | 1450 | int force, int resolve) |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 1451 | { |
| 1452 | channelid *self = PyObject_New(channelid, cls); |
| 1453 | if (self == NULL) { |
| 1454 | return NULL; |
| 1455 | } |
| 1456 | self->id = cid; |
| 1457 | self->end = end; |
Eric Snow | 6d2cd90 | 2018-05-16 15:04:57 -0400 | [diff] [blame] | 1458 | self->resolve = resolve; |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 1459 | self->channels = channels; |
| 1460 | |
| 1461 | if (_channels_add_id_object(channels, cid) != 0) { |
| 1462 | if (force && PyErr_ExceptionMatches(ChannelNotFoundError)) { |
| 1463 | PyErr_Clear(); |
| 1464 | } |
| 1465 | else { |
| 1466 | Py_DECREF((PyObject *)self); |
| 1467 | return NULL; |
| 1468 | } |
| 1469 | } |
| 1470 | |
| 1471 | return self; |
| 1472 | } |
| 1473 | |
| 1474 | static _channels * _global_channels(void); |
| 1475 | |
| 1476 | static PyObject * |
| 1477 | channelid_new(PyTypeObject *cls, PyObject *args, PyObject *kwds) |
| 1478 | { |
Eric Snow | 6d2cd90 | 2018-05-16 15:04:57 -0400 | [diff] [blame] | 1479 | static char *kwlist[] = {"id", "send", "recv", "force", "_resolve", NULL}; |
Serhiy Storchaka | bf16991 | 2019-09-13 22:50:27 +0300 | [diff] [blame] | 1480 | int64_t cid; |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 1481 | int send = -1; |
| 1482 | int recv = -1; |
| 1483 | int force = 0; |
Eric Snow | 6d2cd90 | 2018-05-16 15:04:57 -0400 | [diff] [blame] | 1484 | int resolve = 0; |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 1485 | if (!PyArg_ParseTupleAndKeywords(args, kwds, |
Serhiy Storchaka | bf16991 | 2019-09-13 22:50:27 +0300 | [diff] [blame] | 1486 | "O&|$pppp:ChannelID.__new__", kwlist, |
| 1487 | channel_id_converter, &cid, &send, &recv, &force, &resolve)) |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 1488 | return NULL; |
| 1489 | |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 1490 | // Handle "send" and "recv". |
| 1491 | if (send == 0 && recv == 0) { |
| 1492 | PyErr_SetString(PyExc_ValueError, |
| 1493 | "'send' and 'recv' cannot both be False"); |
| 1494 | return NULL; |
| 1495 | } |
Eric Snow | 4e9da0d | 2018-02-02 21:49:49 -0700 | [diff] [blame] | 1496 | |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 1497 | int end = 0; |
| 1498 | if (send == 1) { |
| 1499 | if (recv == 0 || recv == -1) { |
| 1500 | end = CHANNEL_SEND; |
| 1501 | } |
| 1502 | } |
| 1503 | else if (recv == 1) { |
| 1504 | end = CHANNEL_RECV; |
| 1505 | } |
| 1506 | |
Eric Snow | 6d2cd90 | 2018-05-16 15:04:57 -0400 | [diff] [blame] | 1507 | return (PyObject *)newchannelid(cls, cid, end, _global_channels(), |
| 1508 | force, resolve); |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 1509 | } |
| 1510 | |
| 1511 | static void |
| 1512 | channelid_dealloc(PyObject *v) |
| 1513 | { |
| 1514 | int64_t cid = ((channelid *)v)->id; |
| 1515 | _channels *channels = ((channelid *)v)->channels; |
| 1516 | Py_TYPE(v)->tp_free(v); |
| 1517 | |
| 1518 | _channels_drop_id_object(channels, cid); |
| 1519 | } |
| 1520 | |
| 1521 | static PyObject * |
| 1522 | channelid_repr(PyObject *self) |
| 1523 | { |
| 1524 | PyTypeObject *type = Py_TYPE(self); |
| 1525 | const char *name = _PyType_Name(type); |
| 1526 | |
| 1527 | channelid *cid = (channelid *)self; |
| 1528 | const char *fmt; |
| 1529 | if (cid->end == CHANNEL_SEND) { |
Eric Snow | ab4a198 | 2018-06-13 08:02:39 -0600 | [diff] [blame] | 1530 | fmt = "%s(%" PRId64 ", send=True)"; |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 1531 | } |
| 1532 | else if (cid->end == CHANNEL_RECV) { |
Eric Snow | ab4a198 | 2018-06-13 08:02:39 -0600 | [diff] [blame] | 1533 | fmt = "%s(%" PRId64 ", recv=True)"; |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 1534 | } |
| 1535 | else { |
Eric Snow | ab4a198 | 2018-06-13 08:02:39 -0600 | [diff] [blame] | 1536 | fmt = "%s(%" PRId64 ")"; |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 1537 | } |
| 1538 | return PyUnicode_FromFormat(fmt, name, cid->id); |
| 1539 | } |
| 1540 | |
Eric Snow | 6d2cd90 | 2018-05-16 15:04:57 -0400 | [diff] [blame] | 1541 | static PyObject * |
| 1542 | channelid_str(PyObject *self) |
| 1543 | { |
| 1544 | channelid *cid = (channelid *)self; |
Eric Snow | ab4a198 | 2018-06-13 08:02:39 -0600 | [diff] [blame] | 1545 | return PyUnicode_FromFormat("%" PRId64 "", cid->id); |
Eric Snow | 6d2cd90 | 2018-05-16 15:04:57 -0400 | [diff] [blame] | 1546 | } |
| 1547 | |
Benjamin Peterson | 4629c0d | 2018-07-06 23:28:35 -0700 | [diff] [blame] | 1548 | static PyObject * |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 1549 | channelid_int(PyObject *self) |
| 1550 | { |
| 1551 | channelid *cid = (channelid *)self; |
| 1552 | return PyLong_FromLongLong(cid->id); |
| 1553 | } |
| 1554 | |
| 1555 | static PyNumberMethods channelid_as_number = { |
| 1556 | 0, /* nb_add */ |
| 1557 | 0, /* nb_subtract */ |
| 1558 | 0, /* nb_multiply */ |
| 1559 | 0, /* nb_remainder */ |
| 1560 | 0, /* nb_divmod */ |
| 1561 | 0, /* nb_power */ |
| 1562 | 0, /* nb_negative */ |
| 1563 | 0, /* nb_positive */ |
| 1564 | 0, /* nb_absolute */ |
| 1565 | 0, /* nb_bool */ |
| 1566 | 0, /* nb_invert */ |
| 1567 | 0, /* nb_lshift */ |
| 1568 | 0, /* nb_rshift */ |
| 1569 | 0, /* nb_and */ |
| 1570 | 0, /* nb_xor */ |
| 1571 | 0, /* nb_or */ |
| 1572 | (unaryfunc)channelid_int, /* nb_int */ |
| 1573 | 0, /* nb_reserved */ |
| 1574 | 0, /* nb_float */ |
| 1575 | |
| 1576 | 0, /* nb_inplace_add */ |
| 1577 | 0, /* nb_inplace_subtract */ |
| 1578 | 0, /* nb_inplace_multiply */ |
| 1579 | 0, /* nb_inplace_remainder */ |
| 1580 | 0, /* nb_inplace_power */ |
| 1581 | 0, /* nb_inplace_lshift */ |
| 1582 | 0, /* nb_inplace_rshift */ |
| 1583 | 0, /* nb_inplace_and */ |
| 1584 | 0, /* nb_inplace_xor */ |
| 1585 | 0, /* nb_inplace_or */ |
| 1586 | |
| 1587 | 0, /* nb_floor_divide */ |
| 1588 | 0, /* nb_true_divide */ |
| 1589 | 0, /* nb_inplace_floor_divide */ |
| 1590 | 0, /* nb_inplace_true_divide */ |
| 1591 | |
| 1592 | (unaryfunc)channelid_int, /* nb_index */ |
| 1593 | }; |
| 1594 | |
| 1595 | static Py_hash_t |
| 1596 | channelid_hash(PyObject *self) |
| 1597 | { |
| 1598 | channelid *cid = (channelid *)self; |
| 1599 | PyObject *id = PyLong_FromLongLong(cid->id); |
| 1600 | if (id == NULL) { |
| 1601 | return -1; |
| 1602 | } |
Eric Snow | 4e9da0d | 2018-02-02 21:49:49 -0700 | [diff] [blame] | 1603 | Py_hash_t hash = PyObject_Hash(id); |
| 1604 | Py_DECREF(id); |
| 1605 | return hash; |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 1606 | } |
| 1607 | |
| 1608 | static PyObject * |
| 1609 | channelid_richcompare(PyObject *self, PyObject *other, int op) |
| 1610 | { |
| 1611 | if (op != Py_EQ && op != Py_NE) { |
| 1612 | Py_RETURN_NOTIMPLEMENTED; |
| 1613 | } |
| 1614 | |
| 1615 | if (!PyObject_TypeCheck(self, &ChannelIDtype)) { |
| 1616 | Py_RETURN_NOTIMPLEMENTED; |
| 1617 | } |
| 1618 | |
| 1619 | channelid *cid = (channelid *)self; |
| 1620 | int equal; |
| 1621 | if (PyObject_TypeCheck(other, &ChannelIDtype)) { |
| 1622 | channelid *othercid = (channelid *)other; |
Serhiy Storchaka | bf16991 | 2019-09-13 22:50:27 +0300 | [diff] [blame] | 1623 | equal = (cid->end == othercid->end) && (cid->id == othercid->id); |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 1624 | } |
Serhiy Storchaka | bf16991 | 2019-09-13 22:50:27 +0300 | [diff] [blame] | 1625 | else if (PyLong_Check(other)) { |
| 1626 | /* Fast path */ |
| 1627 | int overflow; |
| 1628 | long long othercid = PyLong_AsLongLongAndOverflow(other, &overflow); |
| 1629 | if (othercid == -1 && PyErr_Occurred()) { |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 1630 | return NULL; |
| 1631 | } |
Serhiy Storchaka | bf16991 | 2019-09-13 22:50:27 +0300 | [diff] [blame] | 1632 | equal = !overflow && (othercid >= 0) && (cid->id == othercid); |
| 1633 | } |
| 1634 | else if (PyNumber_Check(other)) { |
| 1635 | PyObject *pyid = PyLong_FromLongLong(cid->id); |
| 1636 | if (pyid == NULL) { |
| 1637 | return NULL; |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 1638 | } |
Serhiy Storchaka | bf16991 | 2019-09-13 22:50:27 +0300 | [diff] [blame] | 1639 | PyObject *res = PyObject_RichCompare(pyid, other, op); |
| 1640 | Py_DECREF(pyid); |
| 1641 | return res; |
| 1642 | } |
| 1643 | else { |
| 1644 | Py_RETURN_NOTIMPLEMENTED; |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 1645 | } |
| 1646 | |
| 1647 | if ((op == Py_EQ && equal) || (op == Py_NE && !equal)) { |
| 1648 | Py_RETURN_TRUE; |
| 1649 | } |
| 1650 | Py_RETURN_FALSE; |
| 1651 | } |
| 1652 | |
Eric Snow | ab4a198 | 2018-06-13 08:02:39 -0600 | [diff] [blame] | 1653 | static PyObject * |
| 1654 | _channel_from_cid(PyObject *cid, int end) |
| 1655 | { |
| 1656 | PyObject *highlevel = PyImport_ImportModule("interpreters"); |
| 1657 | if (highlevel == NULL) { |
| 1658 | PyErr_Clear(); |
| 1659 | highlevel = PyImport_ImportModule("test.support.interpreters"); |
| 1660 | if (highlevel == NULL) { |
| 1661 | return NULL; |
| 1662 | } |
| 1663 | } |
| 1664 | const char *clsname = (end == CHANNEL_RECV) ? "RecvChannel" : |
| 1665 | "SendChannel"; |
| 1666 | PyObject *cls = PyObject_GetAttrString(highlevel, clsname); |
| 1667 | Py_DECREF(highlevel); |
| 1668 | if (cls == NULL) { |
| 1669 | return NULL; |
| 1670 | } |
| 1671 | PyObject *chan = PyObject_CallFunctionObjArgs(cls, cid, NULL); |
| 1672 | Py_DECREF(cls); |
| 1673 | if (chan == NULL) { |
| 1674 | return NULL; |
| 1675 | } |
| 1676 | return chan; |
| 1677 | } |
| 1678 | |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 1679 | struct _channelid_xid { |
| 1680 | int64_t id; |
| 1681 | int end; |
Eric Snow | 6d2cd90 | 2018-05-16 15:04:57 -0400 | [diff] [blame] | 1682 | int resolve; |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 1683 | }; |
| 1684 | |
| 1685 | static PyObject * |
| 1686 | _channelid_from_xid(_PyCrossInterpreterData *data) |
| 1687 | { |
| 1688 | struct _channelid_xid *xid = (struct _channelid_xid *)data->data; |
Eric Snow | 6d2cd90 | 2018-05-16 15:04:57 -0400 | [diff] [blame] | 1689 | // Note that we do not preserve the "resolve" flag. |
| 1690 | PyObject *cid = (PyObject *)newchannelid(&ChannelIDtype, xid->id, xid->end, |
| 1691 | _global_channels(), 0, 0); |
| 1692 | if (xid->end == 0) { |
| 1693 | return cid; |
| 1694 | } |
| 1695 | if (!xid->resolve) { |
| 1696 | return cid; |
| 1697 | } |
| 1698 | |
| 1699 | /* Try returning a high-level channel end but fall back to the ID. */ |
Eric Snow | ab4a198 | 2018-06-13 08:02:39 -0600 | [diff] [blame] | 1700 | PyObject *chan = _channel_from_cid(cid, xid->end); |
Eric Snow | 6d2cd90 | 2018-05-16 15:04:57 -0400 | [diff] [blame] | 1701 | if (chan == NULL) { |
Eric Snow | ab4a198 | 2018-06-13 08:02:39 -0600 | [diff] [blame] | 1702 | PyErr_Clear(); |
| 1703 | return cid; |
Eric Snow | 6d2cd90 | 2018-05-16 15:04:57 -0400 | [diff] [blame] | 1704 | } |
| 1705 | Py_DECREF(cid); |
| 1706 | return chan; |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 1707 | } |
| 1708 | |
| 1709 | static int |
| 1710 | _channelid_shared(PyObject *obj, _PyCrossInterpreterData *data) |
| 1711 | { |
| 1712 | struct _channelid_xid *xid = PyMem_NEW(struct _channelid_xid, 1); |
| 1713 | if (xid == NULL) { |
| 1714 | return -1; |
| 1715 | } |
| 1716 | xid->id = ((channelid *)obj)->id; |
| 1717 | xid->end = ((channelid *)obj)->end; |
Eric Snow | 6d2cd90 | 2018-05-16 15:04:57 -0400 | [diff] [blame] | 1718 | xid->resolve = ((channelid *)obj)->resolve; |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 1719 | |
| 1720 | data->data = xid; |
Eric Snow | 6379913 | 2018-06-01 18:45:20 -0600 | [diff] [blame] | 1721 | Py_INCREF(obj); |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 1722 | data->obj = obj; |
| 1723 | data->new_object = _channelid_from_xid; |
| 1724 | data->free = PyMem_Free; |
| 1725 | return 0; |
| 1726 | } |
| 1727 | |
| 1728 | static PyObject * |
| 1729 | channelid_end(PyObject *self, void *end) |
| 1730 | { |
| 1731 | int force = 1; |
| 1732 | channelid *cid = (channelid *)self; |
| 1733 | if (end != NULL) { |
| 1734 | return (PyObject *)newchannelid(Py_TYPE(self), cid->id, *(int *)end, |
Eric Snow | 6d2cd90 | 2018-05-16 15:04:57 -0400 | [diff] [blame] | 1735 | cid->channels, force, cid->resolve); |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 1736 | } |
| 1737 | |
| 1738 | if (cid->end == CHANNEL_SEND) { |
| 1739 | return PyUnicode_InternFromString("send"); |
| 1740 | } |
| 1741 | if (cid->end == CHANNEL_RECV) { |
| 1742 | return PyUnicode_InternFromString("recv"); |
| 1743 | } |
| 1744 | return PyUnicode_InternFromString("both"); |
| 1745 | } |
| 1746 | |
| 1747 | static int _channelid_end_send = CHANNEL_SEND; |
| 1748 | static int _channelid_end_recv = CHANNEL_RECV; |
| 1749 | |
| 1750 | static PyGetSetDef channelid_getsets[] = { |
| 1751 | {"end", (getter)channelid_end, NULL, |
| 1752 | PyDoc_STR("'send', 'recv', or 'both'")}, |
| 1753 | {"send", (getter)channelid_end, NULL, |
| 1754 | PyDoc_STR("the 'send' end of the channel"), &_channelid_end_send}, |
| 1755 | {"recv", (getter)channelid_end, NULL, |
| 1756 | PyDoc_STR("the 'recv' end of the channel"), &_channelid_end_recv}, |
| 1757 | {NULL} |
| 1758 | }; |
| 1759 | |
| 1760 | PyDoc_STRVAR(channelid_doc, |
| 1761 | "A channel ID identifies a channel and may be used as an int."); |
| 1762 | |
| 1763 | static PyTypeObject ChannelIDtype = { |
| 1764 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 1765 | "_xxsubinterpreters.ChannelID", /* tp_name */ |
Peter Eisentraut | 0e0bc4e | 2018-09-10 18:46:08 +0200 | [diff] [blame] | 1766 | sizeof(channelid), /* tp_basicsize */ |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 1767 | 0, /* tp_itemsize */ |
| 1768 | (destructor)channelid_dealloc, /* tp_dealloc */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 1769 | 0, /* tp_vectorcall_offset */ |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 1770 | 0, /* tp_getattr */ |
| 1771 | 0, /* tp_setattr */ |
| 1772 | 0, /* tp_as_async */ |
| 1773 | (reprfunc)channelid_repr, /* tp_repr */ |
| 1774 | &channelid_as_number, /* tp_as_number */ |
| 1775 | 0, /* tp_as_sequence */ |
| 1776 | 0, /* tp_as_mapping */ |
| 1777 | channelid_hash, /* tp_hash */ |
| 1778 | 0, /* tp_call */ |
Eric Snow | 6d2cd90 | 2018-05-16 15:04:57 -0400 | [diff] [blame] | 1779 | (reprfunc)channelid_str, /* tp_str */ |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 1780 | 0, /* tp_getattro */ |
| 1781 | 0, /* tp_setattro */ |
| 1782 | 0, /* tp_as_buffer */ |
Serhiy Storchaka | bf16991 | 2019-09-13 22:50:27 +0300 | [diff] [blame] | 1783 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 1784 | channelid_doc, /* tp_doc */ |
| 1785 | 0, /* tp_traverse */ |
| 1786 | 0, /* tp_clear */ |
| 1787 | channelid_richcompare, /* tp_richcompare */ |
| 1788 | 0, /* tp_weaklistoffset */ |
| 1789 | 0, /* tp_iter */ |
| 1790 | 0, /* tp_iternext */ |
| 1791 | 0, /* tp_methods */ |
| 1792 | 0, /* tp_members */ |
| 1793 | channelid_getsets, /* tp_getset */ |
| 1794 | 0, /* tp_base */ |
| 1795 | 0, /* tp_dict */ |
| 1796 | 0, /* tp_descr_get */ |
| 1797 | 0, /* tp_descr_set */ |
| 1798 | 0, /* tp_dictoffset */ |
| 1799 | 0, /* tp_init */ |
| 1800 | 0, /* tp_alloc */ |
| 1801 | // Note that we do not set tp_new to channelid_new. Instead we |
| 1802 | // set it to NULL, meaning it cannot be instantiated from Python |
| 1803 | // code. We do this because there is a strong relationship between |
| 1804 | // channel IDs and the channel lifecycle, so this limitation avoids |
| 1805 | // related complications. |
| 1806 | NULL, /* tp_new */ |
| 1807 | }; |
| 1808 | |
Eric Snow | 4e9da0d | 2018-02-02 21:49:49 -0700 | [diff] [blame] | 1809 | |
| 1810 | /* interpreter-specific code ************************************************/ |
| 1811 | |
| 1812 | static PyObject * RunFailedError = NULL; |
| 1813 | |
| 1814 | static int |
| 1815 | interp_exceptions_init(PyObject *ns) |
| 1816 | { |
| 1817 | // XXX Move the exceptions into per-module memory? |
| 1818 | |
| 1819 | if (RunFailedError == NULL) { |
| 1820 | // An uncaught exception came out of interp_run_string(). |
| 1821 | RunFailedError = PyErr_NewException("_xxsubinterpreters.RunFailedError", |
| 1822 | PyExc_RuntimeError, NULL); |
| 1823 | if (RunFailedError == NULL) { |
| 1824 | return -1; |
| 1825 | } |
| 1826 | if (PyDict_SetItemString(ns, "RunFailedError", RunFailedError) != 0) { |
| 1827 | return -1; |
| 1828 | } |
| 1829 | } |
| 1830 | |
| 1831 | return 0; |
| 1832 | } |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 1833 | |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 1834 | static int |
| 1835 | _is_running(PyInterpreterState *interp) |
| 1836 | { |
| 1837 | PyThreadState *tstate = PyInterpreterState_ThreadHead(interp); |
| 1838 | if (PyThreadState_Next(tstate) != NULL) { |
| 1839 | PyErr_SetString(PyExc_RuntimeError, |
| 1840 | "interpreter has more than one thread"); |
| 1841 | return -1; |
| 1842 | } |
Victor Stinner | 4386b90 | 2020-04-29 03:01:43 +0200 | [diff] [blame] | 1843 | |
| 1844 | assert(!PyErr_Occurred()); |
Victor Stinner | 3072338 | 2020-03-25 19:52:02 +0100 | [diff] [blame] | 1845 | PyFrameObject *frame = PyThreadState_GetFrame(tstate); |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 1846 | if (frame == NULL) { |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 1847 | return 0; |
| 1848 | } |
Victor Stinner | 4386b90 | 2020-04-29 03:01:43 +0200 | [diff] [blame] | 1849 | |
Mark Shannon | cb9879b | 2020-07-17 11:44:23 +0100 | [diff] [blame] | 1850 | int executing = _PyFrame_IsExecuting(frame); |
Victor Stinner | 4386b90 | 2020-04-29 03:01:43 +0200 | [diff] [blame] | 1851 | Py_DECREF(frame); |
| 1852 | |
| 1853 | return executing; |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 1854 | } |
| 1855 | |
| 1856 | static int |
| 1857 | _ensure_not_running(PyInterpreterState *interp) |
| 1858 | { |
| 1859 | int is_running = _is_running(interp); |
| 1860 | if (is_running < 0) { |
| 1861 | return -1; |
| 1862 | } |
| 1863 | if (is_running) { |
| 1864 | PyErr_Format(PyExc_RuntimeError, "interpreter already running"); |
| 1865 | return -1; |
| 1866 | } |
| 1867 | return 0; |
| 1868 | } |
| 1869 | |
| 1870 | static int |
| 1871 | _run_script(PyInterpreterState *interp, const char *codestr, |
Victor Stinner | f2c3b68 | 2020-05-14 18:46:24 +0200 | [diff] [blame] | 1872 | _sharedns *shared, _sharedexception **exc) |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 1873 | { |
Victor Stinner | f2c3b68 | 2020-05-14 18:46:24 +0200 | [diff] [blame] | 1874 | PyObject *exctype = NULL; |
| 1875 | PyObject *excval = NULL; |
| 1876 | PyObject *tb = NULL; |
Eric Snow | 4e9da0d | 2018-02-02 21:49:49 -0700 | [diff] [blame] | 1877 | |
Eric Snow | c11183c | 2019-03-15 16:35:46 -0600 | [diff] [blame] | 1878 | PyObject *main_mod = _PyInterpreterState_GetMainModule(interp); |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 1879 | if (main_mod == NULL) { |
| 1880 | goto error; |
| 1881 | } |
| 1882 | PyObject *ns = PyModule_GetDict(main_mod); // borrowed |
| 1883 | Py_DECREF(main_mod); |
| 1884 | if (ns == NULL) { |
| 1885 | goto error; |
| 1886 | } |
| 1887 | Py_INCREF(ns); |
| 1888 | |
| 1889 | // Apply the cross-interpreter data. |
| 1890 | if (shared != NULL) { |
Eric Snow | 4e9da0d | 2018-02-02 21:49:49 -0700 | [diff] [blame] | 1891 | if (_sharedns_apply(shared, ns) != 0) { |
| 1892 | Py_DECREF(ns); |
| 1893 | goto error; |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 1894 | } |
| 1895 | } |
| 1896 | |
| 1897 | // Run the string (see PyRun_SimpleStringFlags). |
| 1898 | PyObject *result = PyRun_StringFlags(codestr, Py_file_input, ns, ns, NULL); |
| 1899 | Py_DECREF(ns); |
| 1900 | if (result == NULL) { |
| 1901 | goto error; |
| 1902 | } |
| 1903 | else { |
| 1904 | Py_DECREF(result); // We throw away the result. |
| 1905 | } |
| 1906 | |
Victor Stinner | f2c3b68 | 2020-05-14 18:46:24 +0200 | [diff] [blame] | 1907 | *exc = NULL; |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 1908 | return 0; |
| 1909 | |
| 1910 | error: |
Victor Stinner | f2c3b68 | 2020-05-14 18:46:24 +0200 | [diff] [blame] | 1911 | PyErr_Fetch(&exctype, &excval, &tb); |
Eric Snow | 4e9da0d | 2018-02-02 21:49:49 -0700 | [diff] [blame] | 1912 | |
Victor Stinner | f2c3b68 | 2020-05-14 18:46:24 +0200 | [diff] [blame] | 1913 | _sharedexception *sharedexc = _sharedexception_bind(exctype, excval, tb); |
| 1914 | Py_XDECREF(exctype); |
| 1915 | Py_XDECREF(excval); |
| 1916 | Py_XDECREF(tb); |
Eric Snow | a1d9e0a | 2020-05-07 08:56:01 -0600 | [diff] [blame] | 1917 | if (sharedexc == NULL) { |
Victor Stinner | f2c3b68 | 2020-05-14 18:46:24 +0200 | [diff] [blame] | 1918 | fprintf(stderr, "RunFailedError: script raised an uncaught exception"); |
| 1919 | PyErr_Clear(); |
| 1920 | sharedexc = NULL; |
| 1921 | } |
| 1922 | else { |
Eric Snow | 4e9da0d | 2018-02-02 21:49:49 -0700 | [diff] [blame] | 1923 | assert(!PyErr_Occurred()); |
| 1924 | } |
Victor Stinner | f2c3b68 | 2020-05-14 18:46:24 +0200 | [diff] [blame] | 1925 | *exc = sharedexc; |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 1926 | return -1; |
| 1927 | } |
| 1928 | |
| 1929 | static int |
| 1930 | _run_script_in_interpreter(PyInterpreterState *interp, const char *codestr, |
| 1931 | PyObject *shareables) |
| 1932 | { |
| 1933 | if (_ensure_not_running(interp) < 0) { |
| 1934 | return -1; |
| 1935 | } |
| 1936 | |
Eric Snow | 4e9da0d | 2018-02-02 21:49:49 -0700 | [diff] [blame] | 1937 | _sharedns *shared = _get_shared_ns(shareables); |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 1938 | if (shared == NULL && PyErr_Occurred()) { |
| 1939 | return -1; |
| 1940 | } |
| 1941 | |
Victor Stinner | fb2c7c4 | 2020-05-05 20:33:06 +0200 | [diff] [blame] | 1942 | #ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS |
| 1943 | // Switch to interpreter. |
| 1944 | PyThreadState *new_tstate = PyInterpreterState_ThreadHead(interp); |
| 1945 | PyThreadState *save1 = PyEval_SaveThread(); |
| 1946 | |
| 1947 | (void)PyThreadState_Swap(new_tstate); |
| 1948 | |
| 1949 | // Run the script. |
| 1950 | _sharedexception *exc = NULL; |
| 1951 | int result = _run_script(interp, codestr, shared, &exc); |
| 1952 | |
| 1953 | // Switch back. |
| 1954 | PyEval_RestoreThread(save1); |
| 1955 | #else |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 1956 | // Switch to interpreter. |
Eric Snow | f53d9f2 | 2018-02-20 16:30:17 -0700 | [diff] [blame] | 1957 | PyThreadState *save_tstate = NULL; |
Victor Stinner | be79373 | 2020-03-13 18:15:33 +0100 | [diff] [blame] | 1958 | if (interp != PyInterpreterState_Get()) { |
Eric Snow | f53d9f2 | 2018-02-20 16:30:17 -0700 | [diff] [blame] | 1959 | // XXX Using the "head" thread isn't strictly correct. |
| 1960 | PyThreadState *tstate = PyInterpreterState_ThreadHead(interp); |
| 1961 | // XXX Possible GILState issues? |
| 1962 | save_tstate = PyThreadState_Swap(tstate); |
| 1963 | } |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 1964 | |
| 1965 | // Run the script. |
Victor Stinner | f2c3b68 | 2020-05-14 18:46:24 +0200 | [diff] [blame] | 1966 | _sharedexception *exc = NULL; |
| 1967 | int result = _run_script(interp, codestr, shared, &exc); |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 1968 | |
| 1969 | // Switch back. |
| 1970 | if (save_tstate != NULL) { |
| 1971 | PyThreadState_Swap(save_tstate); |
| 1972 | } |
Victor Stinner | fb2c7c4 | 2020-05-05 20:33:06 +0200 | [diff] [blame] | 1973 | #endif |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 1974 | |
| 1975 | // Propagate any exception out to the caller. |
Victor Stinner | f2c3b68 | 2020-05-14 18:46:24 +0200 | [diff] [blame] | 1976 | if (exc != NULL) { |
| 1977 | _sharedexception_apply(exc, RunFailedError); |
| 1978 | _sharedexception_free(exc); |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 1979 | } |
| 1980 | else if (result != 0) { |
| 1981 | // We were unable to allocate a shared exception. |
| 1982 | PyErr_NoMemory(); |
| 1983 | } |
| 1984 | |
| 1985 | if (shared != NULL) { |
Eric Snow | 4e9da0d | 2018-02-02 21:49:49 -0700 | [diff] [blame] | 1986 | _sharedns_free(shared); |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 1987 | } |
| 1988 | |
| 1989 | return result; |
| 1990 | } |
| 1991 | |
| 1992 | |
| 1993 | /* module level code ********************************************************/ |
| 1994 | |
| 1995 | /* globals is the process-global state for the module. It holds all |
| 1996 | the data that we need to share between interpreters, so it cannot |
| 1997 | hold PyObject values. */ |
| 1998 | static struct globals { |
| 1999 | _channels channels; |
| 2000 | } _globals = {{0}}; |
| 2001 | |
| 2002 | static int |
| 2003 | _init_globals(void) |
| 2004 | { |
| 2005 | if (_channels_init(&_globals.channels) != 0) { |
| 2006 | return -1; |
| 2007 | } |
| 2008 | return 0; |
| 2009 | } |
| 2010 | |
| 2011 | static _channels * |
| 2012 | _global_channels(void) { |
| 2013 | return &_globals.channels; |
| 2014 | } |
| 2015 | |
| 2016 | static PyObject * |
Victor Stinner | 252346a | 2020-05-01 11:33:44 +0200 | [diff] [blame] | 2017 | interp_create(PyObject *self, PyObject *args, PyObject *kwds) |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 2018 | { |
Victor Stinner | 252346a | 2020-05-01 11:33:44 +0200 | [diff] [blame] | 2019 | |
| 2020 | static char *kwlist[] = {"isolated", NULL}; |
| 2021 | int isolated = 1; |
| 2022 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|$i:create", kwlist, |
| 2023 | &isolated)) { |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 2024 | return NULL; |
| 2025 | } |
| 2026 | |
| 2027 | // Create and initialize the new interpreter. |
Eric Snow | f53d9f2 | 2018-02-20 16:30:17 -0700 | [diff] [blame] | 2028 | PyThreadState *save_tstate = PyThreadState_Swap(NULL); |
| 2029 | // XXX Possible GILState issues? |
Victor Stinner | 252346a | 2020-05-01 11:33:44 +0200 | [diff] [blame] | 2030 | PyThreadState *tstate = _Py_NewInterpreter(isolated); |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 2031 | PyThreadState_Swap(save_tstate); |
| 2032 | if (tstate == NULL) { |
| 2033 | /* Since no new thread state was created, there is no exception to |
| 2034 | propagate; raise a fresh one after swapping in the old thread |
| 2035 | state. */ |
| 2036 | PyErr_SetString(PyExc_RuntimeError, "interpreter creation failed"); |
| 2037 | return NULL; |
| 2038 | } |
Victor Stinner | 3072338 | 2020-03-25 19:52:02 +0100 | [diff] [blame] | 2039 | PyInterpreterState *interp = PyThreadState_GetInterpreter(tstate); |
| 2040 | PyObject *idobj = _PyInterpreterState_GetIDObject(interp); |
Eric Snow | c11183c | 2019-03-15 16:35:46 -0600 | [diff] [blame] | 2041 | if (idobj == NULL) { |
| 2042 | // XXX Possible GILState issues? |
| 2043 | save_tstate = PyThreadState_Swap(tstate); |
| 2044 | Py_EndInterpreter(tstate); |
| 2045 | PyThreadState_Swap(save_tstate); |
| 2046 | return NULL; |
| 2047 | } |
Victor Stinner | 3072338 | 2020-03-25 19:52:02 +0100 | [diff] [blame] | 2048 | _PyInterpreterState_RequireIDRef(interp, 1); |
Eric Snow | c11183c | 2019-03-15 16:35:46 -0600 | [diff] [blame] | 2049 | return idobj; |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 2050 | } |
| 2051 | |
| 2052 | PyDoc_STRVAR(create_doc, |
| 2053 | "create() -> ID\n\ |
| 2054 | \n\ |
| 2055 | Create a new interpreter and return a unique generated ID."); |
| 2056 | |
| 2057 | |
| 2058 | static PyObject * |
Eric Snow | 6d2cd90 | 2018-05-16 15:04:57 -0400 | [diff] [blame] | 2059 | interp_destroy(PyObject *self, PyObject *args, PyObject *kwds) |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 2060 | { |
Eric Snow | 6d2cd90 | 2018-05-16 15:04:57 -0400 | [diff] [blame] | 2061 | static char *kwlist[] = {"id", NULL}; |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 2062 | PyObject *id; |
Eric Snow | 6d2cd90 | 2018-05-16 15:04:57 -0400 | [diff] [blame] | 2063 | // XXX Use "L" for id? |
| 2064 | if (!PyArg_ParseTupleAndKeywords(args, kwds, |
| 2065 | "O:destroy", kwlist, &id)) { |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 2066 | return NULL; |
| 2067 | } |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 2068 | |
| 2069 | // Look up the interpreter. |
Eric Snow | c11183c | 2019-03-15 16:35:46 -0600 | [diff] [blame] | 2070 | PyInterpreterState *interp = _PyInterpreterID_LookUp(id); |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 2071 | if (interp == NULL) { |
| 2072 | return NULL; |
| 2073 | } |
| 2074 | |
| 2075 | // Ensure we don't try to destroy the current interpreter. |
| 2076 | PyInterpreterState *current = _get_current(); |
| 2077 | if (current == NULL) { |
| 2078 | return NULL; |
| 2079 | } |
| 2080 | if (interp == current) { |
| 2081 | PyErr_SetString(PyExc_RuntimeError, |
| 2082 | "cannot destroy the current interpreter"); |
| 2083 | return NULL; |
| 2084 | } |
| 2085 | |
| 2086 | // Ensure the interpreter isn't running. |
| 2087 | /* XXX We *could* support destroying a running interpreter but |
| 2088 | aren't going to worry about it for now. */ |
| 2089 | if (_ensure_not_running(interp) < 0) { |
| 2090 | return NULL; |
| 2091 | } |
| 2092 | |
| 2093 | // Destroy the interpreter. |
Eric Snow | f53d9f2 | 2018-02-20 16:30:17 -0700 | [diff] [blame] | 2094 | PyThreadState *tstate = PyInterpreterState_ThreadHead(interp); |
| 2095 | // XXX Possible GILState issues? |
| 2096 | PyThreadState *save_tstate = PyThreadState_Swap(tstate); |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 2097 | Py_EndInterpreter(tstate); |
| 2098 | PyThreadState_Swap(save_tstate); |
| 2099 | |
| 2100 | Py_RETURN_NONE; |
| 2101 | } |
| 2102 | |
| 2103 | PyDoc_STRVAR(destroy_doc, |
Eric Snow | 6d2cd90 | 2018-05-16 15:04:57 -0400 | [diff] [blame] | 2104 | "destroy(id)\n\ |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 2105 | \n\ |
| 2106 | Destroy the identified interpreter.\n\ |
| 2107 | \n\ |
| 2108 | Attempting to destroy the current interpreter results in a RuntimeError.\n\ |
| 2109 | So does an unrecognized ID."); |
| 2110 | |
| 2111 | |
| 2112 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 2113 | interp_list_all(PyObject *self, PyObject *Py_UNUSED(ignored)) |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 2114 | { |
| 2115 | PyObject *ids, *id; |
| 2116 | PyInterpreterState *interp; |
| 2117 | |
| 2118 | ids = PyList_New(0); |
| 2119 | if (ids == NULL) { |
| 2120 | return NULL; |
| 2121 | } |
| 2122 | |
| 2123 | interp = PyInterpreterState_Head(); |
| 2124 | while (interp != NULL) { |
Eric Snow | c11183c | 2019-03-15 16:35:46 -0600 | [diff] [blame] | 2125 | id = _PyInterpreterState_GetIDObject(interp); |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 2126 | if (id == NULL) { |
| 2127 | Py_DECREF(ids); |
| 2128 | return NULL; |
| 2129 | } |
| 2130 | // insert at front of list |
Eric Snow | 4e9da0d | 2018-02-02 21:49:49 -0700 | [diff] [blame] | 2131 | int res = PyList_Insert(ids, 0, id); |
| 2132 | Py_DECREF(id); |
| 2133 | if (res < 0) { |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 2134 | Py_DECREF(ids); |
| 2135 | return NULL; |
| 2136 | } |
| 2137 | |
| 2138 | interp = PyInterpreterState_Next(interp); |
| 2139 | } |
| 2140 | |
| 2141 | return ids; |
| 2142 | } |
| 2143 | |
| 2144 | PyDoc_STRVAR(list_all_doc, |
| 2145 | "list_all() -> [ID]\n\ |
| 2146 | \n\ |
| 2147 | Return a list containing the ID of every existing interpreter."); |
| 2148 | |
| 2149 | |
| 2150 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 2151 | interp_get_current(PyObject *self, PyObject *Py_UNUSED(ignored)) |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 2152 | { |
| 2153 | PyInterpreterState *interp =_get_current(); |
| 2154 | if (interp == NULL) { |
| 2155 | return NULL; |
| 2156 | } |
Eric Snow | c11183c | 2019-03-15 16:35:46 -0600 | [diff] [blame] | 2157 | return _PyInterpreterState_GetIDObject(interp); |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 2158 | } |
| 2159 | |
| 2160 | PyDoc_STRVAR(get_current_doc, |
| 2161 | "get_current() -> ID\n\ |
| 2162 | \n\ |
| 2163 | Return the ID of current interpreter."); |
| 2164 | |
| 2165 | |
| 2166 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 2167 | interp_get_main(PyObject *self, PyObject *Py_UNUSED(ignored)) |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 2168 | { |
| 2169 | // Currently, 0 is always the main interpreter. |
Victor Stinner | 1a1bd2e | 2020-04-17 19:13:06 +0200 | [diff] [blame] | 2170 | int64_t id = 0; |
Eric Snow | c11183c | 2019-03-15 16:35:46 -0600 | [diff] [blame] | 2171 | return _PyInterpreterID_New(id); |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 2172 | } |
| 2173 | |
| 2174 | PyDoc_STRVAR(get_main_doc, |
| 2175 | "get_main() -> ID\n\ |
| 2176 | \n\ |
| 2177 | Return the ID of main interpreter."); |
| 2178 | |
| 2179 | |
| 2180 | static PyObject * |
Eric Snow | 6d2cd90 | 2018-05-16 15:04:57 -0400 | [diff] [blame] | 2181 | interp_run_string(PyObject *self, PyObject *args, PyObject *kwds) |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 2182 | { |
Eric Snow | 6d2cd90 | 2018-05-16 15:04:57 -0400 | [diff] [blame] | 2183 | static char *kwlist[] = {"id", "script", "shared", NULL}; |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 2184 | PyObject *id, *code; |
| 2185 | PyObject *shared = NULL; |
Eric Snow | 6d2cd90 | 2018-05-16 15:04:57 -0400 | [diff] [blame] | 2186 | if (!PyArg_ParseTupleAndKeywords(args, kwds, |
| 2187 | "OU|O:run_string", kwlist, |
| 2188 | &id, &code, &shared)) { |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 2189 | return NULL; |
| 2190 | } |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 2191 | |
| 2192 | // Look up the interpreter. |
Eric Snow | c11183c | 2019-03-15 16:35:46 -0600 | [diff] [blame] | 2193 | PyInterpreterState *interp = _PyInterpreterID_LookUp(id); |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 2194 | if (interp == NULL) { |
| 2195 | return NULL; |
| 2196 | } |
| 2197 | |
| 2198 | // Extract code. |
| 2199 | Py_ssize_t size; |
| 2200 | const char *codestr = PyUnicode_AsUTF8AndSize(code, &size); |
| 2201 | if (codestr == NULL) { |
| 2202 | return NULL; |
| 2203 | } |
| 2204 | if (strlen(codestr) != (size_t)size) { |
| 2205 | PyErr_SetString(PyExc_ValueError, |
| 2206 | "source code string cannot contain null bytes"); |
| 2207 | return NULL; |
| 2208 | } |
| 2209 | |
| 2210 | // Run the code in the interpreter. |
| 2211 | if (_run_script_in_interpreter(interp, codestr, shared) != 0) { |
| 2212 | return NULL; |
| 2213 | } |
| 2214 | Py_RETURN_NONE; |
| 2215 | } |
| 2216 | |
| 2217 | PyDoc_STRVAR(run_string_doc, |
Eric Snow | 6d2cd90 | 2018-05-16 15:04:57 -0400 | [diff] [blame] | 2218 | "run_string(id, script, shared)\n\ |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 2219 | \n\ |
| 2220 | Execute the provided string in the identified interpreter.\n\ |
| 2221 | \n\ |
| 2222 | See PyRun_SimpleStrings."); |
| 2223 | |
| 2224 | |
| 2225 | static PyObject * |
Eric Snow | 6d2cd90 | 2018-05-16 15:04:57 -0400 | [diff] [blame] | 2226 | object_is_shareable(PyObject *self, PyObject *args, PyObject *kwds) |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 2227 | { |
Eric Snow | 6d2cd90 | 2018-05-16 15:04:57 -0400 | [diff] [blame] | 2228 | static char *kwlist[] = {"obj", NULL}; |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 2229 | PyObject *obj; |
Eric Snow | 6d2cd90 | 2018-05-16 15:04:57 -0400 | [diff] [blame] | 2230 | if (!PyArg_ParseTupleAndKeywords(args, kwds, |
| 2231 | "O:is_shareable", kwlist, &obj)) { |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 2232 | return NULL; |
| 2233 | } |
Eric Snow | 6d2cd90 | 2018-05-16 15:04:57 -0400 | [diff] [blame] | 2234 | |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 2235 | if (_PyObject_CheckCrossInterpreterData(obj) == 0) { |
| 2236 | Py_RETURN_TRUE; |
| 2237 | } |
| 2238 | PyErr_Clear(); |
| 2239 | Py_RETURN_FALSE; |
| 2240 | } |
| 2241 | |
| 2242 | PyDoc_STRVAR(is_shareable_doc, |
| 2243 | "is_shareable(obj) -> bool\n\ |
| 2244 | \n\ |
| 2245 | Return True if the object's data may be shared between interpreters and\n\ |
| 2246 | False otherwise."); |
| 2247 | |
| 2248 | |
| 2249 | static PyObject * |
Eric Snow | 6d2cd90 | 2018-05-16 15:04:57 -0400 | [diff] [blame] | 2250 | interp_is_running(PyObject *self, PyObject *args, PyObject *kwds) |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 2251 | { |
Eric Snow | 6d2cd90 | 2018-05-16 15:04:57 -0400 | [diff] [blame] | 2252 | static char *kwlist[] = {"id", NULL}; |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 2253 | PyObject *id; |
Eric Snow | 6d2cd90 | 2018-05-16 15:04:57 -0400 | [diff] [blame] | 2254 | if (!PyArg_ParseTupleAndKeywords(args, kwds, |
| 2255 | "O:is_running", kwlist, &id)) { |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 2256 | return NULL; |
| 2257 | } |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 2258 | |
Eric Snow | c11183c | 2019-03-15 16:35:46 -0600 | [diff] [blame] | 2259 | PyInterpreterState *interp = _PyInterpreterID_LookUp(id); |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 2260 | if (interp == NULL) { |
| 2261 | return NULL; |
| 2262 | } |
| 2263 | int is_running = _is_running(interp); |
| 2264 | if (is_running < 0) { |
| 2265 | return NULL; |
| 2266 | } |
| 2267 | if (is_running) { |
| 2268 | Py_RETURN_TRUE; |
| 2269 | } |
| 2270 | Py_RETURN_FALSE; |
| 2271 | } |
| 2272 | |
| 2273 | PyDoc_STRVAR(is_running_doc, |
| 2274 | "is_running(id) -> bool\n\ |
| 2275 | \n\ |
| 2276 | Return whether or not the identified interpreter is running."); |
| 2277 | |
| 2278 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 2279 | channel_create(PyObject *self, PyObject *Py_UNUSED(ignored)) |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 2280 | { |
| 2281 | int64_t cid = _channel_create(&_globals.channels); |
| 2282 | if (cid < 0) { |
| 2283 | return NULL; |
| 2284 | } |
| 2285 | PyObject *id = (PyObject *)newchannelid(&ChannelIDtype, cid, 0, |
Eric Snow | 6d2cd90 | 2018-05-16 15:04:57 -0400 | [diff] [blame] | 2286 | &_globals.channels, 0, 0); |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 2287 | if (id == NULL) { |
| 2288 | if (_channel_destroy(&_globals.channels, cid) != 0) { |
| 2289 | // XXX issue a warning? |
| 2290 | } |
| 2291 | return NULL; |
| 2292 | } |
| 2293 | assert(((channelid *)id)->channels != NULL); |
| 2294 | return id; |
| 2295 | } |
| 2296 | |
| 2297 | PyDoc_STRVAR(channel_create_doc, |
Eric Snow | 6d2cd90 | 2018-05-16 15:04:57 -0400 | [diff] [blame] | 2298 | "channel_create() -> cid\n\ |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 2299 | \n\ |
| 2300 | Create a new cross-interpreter channel and return a unique generated ID."); |
| 2301 | |
| 2302 | static PyObject * |
Eric Snow | 6d2cd90 | 2018-05-16 15:04:57 -0400 | [diff] [blame] | 2303 | channel_destroy(PyObject *self, PyObject *args, PyObject *kwds) |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 2304 | { |
Eric Snow | 6d2cd90 | 2018-05-16 15:04:57 -0400 | [diff] [blame] | 2305 | static char *kwlist[] = {"cid", NULL}; |
Serhiy Storchaka | bf16991 | 2019-09-13 22:50:27 +0300 | [diff] [blame] | 2306 | int64_t cid; |
| 2307 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "O&:channel_destroy", kwlist, |
| 2308 | channel_id_converter, &cid)) { |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 2309 | return NULL; |
| 2310 | } |
| 2311 | |
| 2312 | if (_channel_destroy(&_globals.channels, cid) != 0) { |
| 2313 | return NULL; |
| 2314 | } |
| 2315 | Py_RETURN_NONE; |
| 2316 | } |
| 2317 | |
| 2318 | PyDoc_STRVAR(channel_destroy_doc, |
Eric Snow | 6d2cd90 | 2018-05-16 15:04:57 -0400 | [diff] [blame] | 2319 | "channel_destroy(cid)\n\ |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 2320 | \n\ |
| 2321 | Close and finalize the channel. Afterward attempts to use the channel\n\ |
| 2322 | will behave as though it never existed."); |
| 2323 | |
| 2324 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 2325 | channel_list_all(PyObject *self, PyObject *Py_UNUSED(ignored)) |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 2326 | { |
| 2327 | int64_t count = 0; |
| 2328 | int64_t *cids = _channels_list_all(&_globals.channels, &count); |
| 2329 | if (cids == NULL) { |
| 2330 | if (count == 0) { |
| 2331 | return PyList_New(0); |
| 2332 | } |
| 2333 | return NULL; |
| 2334 | } |
| 2335 | PyObject *ids = PyList_New((Py_ssize_t)count); |
| 2336 | if (ids == NULL) { |
Eric Snow | 4e9da0d | 2018-02-02 21:49:49 -0700 | [diff] [blame] | 2337 | goto finally; |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 2338 | } |
Eric Snow | 4e9da0d | 2018-02-02 21:49:49 -0700 | [diff] [blame] | 2339 | int64_t *cur = cids; |
| 2340 | for (int64_t i=0; i < count; cur++, i++) { |
| 2341 | PyObject *id = (PyObject *)newchannelid(&ChannelIDtype, *cur, 0, |
Eric Snow | 6d2cd90 | 2018-05-16 15:04:57 -0400 | [diff] [blame] | 2342 | &_globals.channels, 0, 0); |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 2343 | if (id == NULL) { |
| 2344 | Py_DECREF(ids); |
| 2345 | ids = NULL; |
| 2346 | break; |
| 2347 | } |
| 2348 | PyList_SET_ITEM(ids, i, id); |
| 2349 | } |
Eric Snow | 4e9da0d | 2018-02-02 21:49:49 -0700 | [diff] [blame] | 2350 | |
| 2351 | finally: |
| 2352 | PyMem_Free(cids); |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 2353 | return ids; |
| 2354 | } |
| 2355 | |
| 2356 | PyDoc_STRVAR(channel_list_all_doc, |
Eric Snow | 6d2cd90 | 2018-05-16 15:04:57 -0400 | [diff] [blame] | 2357 | "channel_list_all() -> [cid]\n\ |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 2358 | \n\ |
| 2359 | Return the list of all IDs for active channels."); |
| 2360 | |
| 2361 | static PyObject * |
Lewis Gaul | f7bbf58 | 2020-04-29 01:18:42 +0100 | [diff] [blame] | 2362 | channel_list_interpreters(PyObject *self, PyObject *args, PyObject *kwds) |
| 2363 | { |
| 2364 | static char *kwlist[] = {"cid", "send", NULL}; |
| 2365 | int64_t cid; /* Channel ID */ |
| 2366 | int send = 0; /* Send or receive end? */ |
| 2367 | int64_t id; |
| 2368 | PyObject *ids, *id_obj; |
| 2369 | PyInterpreterState *interp; |
| 2370 | |
| 2371 | if (!PyArg_ParseTupleAndKeywords( |
| 2372 | args, kwds, "O&$p:channel_list_interpreters", |
| 2373 | kwlist, channel_id_converter, &cid, &send)) { |
| 2374 | return NULL; |
| 2375 | } |
| 2376 | |
| 2377 | ids = PyList_New(0); |
| 2378 | if (ids == NULL) { |
| 2379 | goto except; |
| 2380 | } |
| 2381 | |
| 2382 | interp = PyInterpreterState_Head(); |
| 2383 | while (interp != NULL) { |
| 2384 | id = PyInterpreterState_GetID(interp); |
| 2385 | assert(id >= 0); |
| 2386 | int res = _channel_is_associated(&_globals.channels, cid, id, send); |
| 2387 | if (res < 0) { |
| 2388 | goto except; |
| 2389 | } |
| 2390 | if (res) { |
| 2391 | id_obj = _PyInterpreterState_GetIDObject(interp); |
| 2392 | if (id_obj == NULL) { |
| 2393 | goto except; |
| 2394 | } |
| 2395 | res = PyList_Insert(ids, 0, id_obj); |
| 2396 | Py_DECREF(id_obj); |
| 2397 | if (res < 0) { |
| 2398 | goto except; |
| 2399 | } |
| 2400 | } |
| 2401 | interp = PyInterpreterState_Next(interp); |
| 2402 | } |
| 2403 | |
| 2404 | goto finally; |
| 2405 | |
| 2406 | except: |
| 2407 | Py_XDECREF(ids); |
| 2408 | ids = NULL; |
| 2409 | |
| 2410 | finally: |
| 2411 | return ids; |
| 2412 | } |
| 2413 | |
| 2414 | PyDoc_STRVAR(channel_list_interpreters_doc, |
| 2415 | "channel_list_interpreters(cid, *, send) -> [id]\n\ |
| 2416 | \n\ |
| 2417 | Return the list of all interpreter IDs associated with an end of the channel.\n\ |
| 2418 | \n\ |
| 2419 | The 'send' argument should be a boolean indicating whether to use the send or\n\ |
| 2420 | receive end."); |
| 2421 | |
| 2422 | |
| 2423 | static PyObject * |
Eric Snow | 6d2cd90 | 2018-05-16 15:04:57 -0400 | [diff] [blame] | 2424 | channel_send(PyObject *self, PyObject *args, PyObject *kwds) |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 2425 | { |
Eric Snow | 6d2cd90 | 2018-05-16 15:04:57 -0400 | [diff] [blame] | 2426 | static char *kwlist[] = {"cid", "obj", NULL}; |
Serhiy Storchaka | bf16991 | 2019-09-13 22:50:27 +0300 | [diff] [blame] | 2427 | int64_t cid; |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 2428 | PyObject *obj; |
Serhiy Storchaka | bf16991 | 2019-09-13 22:50:27 +0300 | [diff] [blame] | 2429 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "O&O:channel_send", kwlist, |
| 2430 | channel_id_converter, &cid, &obj)) { |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 2431 | return NULL; |
| 2432 | } |
| 2433 | |
| 2434 | if (_channel_send(&_globals.channels, cid, obj) != 0) { |
| 2435 | return NULL; |
| 2436 | } |
| 2437 | Py_RETURN_NONE; |
| 2438 | } |
| 2439 | |
| 2440 | PyDoc_STRVAR(channel_send_doc, |
Eric Snow | 6d2cd90 | 2018-05-16 15:04:57 -0400 | [diff] [blame] | 2441 | "channel_send(cid, obj)\n\ |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 2442 | \n\ |
| 2443 | Add the object's data to the channel's queue."); |
| 2444 | |
| 2445 | static PyObject * |
Eric Snow | 6d2cd90 | 2018-05-16 15:04:57 -0400 | [diff] [blame] | 2446 | channel_recv(PyObject *self, PyObject *args, PyObject *kwds) |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 2447 | { |
Eric Snow | 5e8c691 | 2020-04-28 17:11:32 -0600 | [diff] [blame] | 2448 | static char *kwlist[] = {"cid", "default", NULL}; |
Serhiy Storchaka | bf16991 | 2019-09-13 22:50:27 +0300 | [diff] [blame] | 2449 | int64_t cid; |
Eric Snow | 5e8c691 | 2020-04-28 17:11:32 -0600 | [diff] [blame] | 2450 | PyObject *dflt = NULL; |
| 2451 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "O&|O:channel_recv", kwlist, |
| 2452 | channel_id_converter, &cid, &dflt)) { |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 2453 | return NULL; |
| 2454 | } |
Eric Snow | 5e8c691 | 2020-04-28 17:11:32 -0600 | [diff] [blame] | 2455 | Py_XINCREF(dflt); |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 2456 | |
Eric Snow | 5e8c691 | 2020-04-28 17:11:32 -0600 | [diff] [blame] | 2457 | PyObject *obj = _channel_recv(&_globals.channels, cid); |
| 2458 | if (obj != NULL) { |
| 2459 | Py_XDECREF(dflt); |
| 2460 | return obj; |
| 2461 | } else if (PyErr_Occurred()) { |
| 2462 | Py_XDECREF(dflt); |
| 2463 | return NULL; |
| 2464 | } else if (dflt != NULL) { |
| 2465 | return dflt; |
| 2466 | } else { |
| 2467 | PyErr_Format(ChannelEmptyError, "channel %" PRId64 " is empty", cid); |
| 2468 | return NULL; |
| 2469 | } |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 2470 | } |
| 2471 | |
| 2472 | PyDoc_STRVAR(channel_recv_doc, |
Eric Snow | 5e8c691 | 2020-04-28 17:11:32 -0600 | [diff] [blame] | 2473 | "channel_recv(cid, [default]) -> obj\n\ |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 2474 | \n\ |
Eric Snow | 5e8c691 | 2020-04-28 17:11:32 -0600 | [diff] [blame] | 2475 | Return a new object from the data at the front of the channel's queue.\n\ |
| 2476 | \n\ |
| 2477 | If there is nothing to receive then raise ChannelEmptyError, unless\n\ |
| 2478 | a default value is provided. In that case return it."); |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 2479 | |
| 2480 | static PyObject * |
Eric Snow | 6d2cd90 | 2018-05-16 15:04:57 -0400 | [diff] [blame] | 2481 | channel_close(PyObject *self, PyObject *args, PyObject *kwds) |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 2482 | { |
Eric Snow | 6d2cd90 | 2018-05-16 15:04:57 -0400 | [diff] [blame] | 2483 | static char *kwlist[] = {"cid", "send", "recv", "force", NULL}; |
Serhiy Storchaka | bf16991 | 2019-09-13 22:50:27 +0300 | [diff] [blame] | 2484 | int64_t cid; |
Eric Snow | 6d2cd90 | 2018-05-16 15:04:57 -0400 | [diff] [blame] | 2485 | int send = 0; |
| 2486 | int recv = 0; |
| 2487 | int force = 0; |
| 2488 | if (!PyArg_ParseTupleAndKeywords(args, kwds, |
Serhiy Storchaka | bf16991 | 2019-09-13 22:50:27 +0300 | [diff] [blame] | 2489 | "O&|$ppp:channel_close", kwlist, |
| 2490 | channel_id_converter, &cid, &send, &recv, &force)) { |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 2491 | return NULL; |
| 2492 | } |
Eric Snow | 6d2cd90 | 2018-05-16 15:04:57 -0400 | [diff] [blame] | 2493 | |
Eric Snow | 3ab0136 | 2018-05-17 10:27:09 -0400 | [diff] [blame] | 2494 | if (_channel_close(&_globals.channels, cid, send-recv, force) != 0) { |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 2495 | return NULL; |
| 2496 | } |
| 2497 | Py_RETURN_NONE; |
| 2498 | } |
| 2499 | |
| 2500 | PyDoc_STRVAR(channel_close_doc, |
Eric Snow | 6d2cd90 | 2018-05-16 15:04:57 -0400 | [diff] [blame] | 2501 | "channel_close(cid, *, send=None, recv=None, force=False)\n\ |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 2502 | \n\ |
Eric Snow | 6d2cd90 | 2018-05-16 15:04:57 -0400 | [diff] [blame] | 2503 | Close the channel for all interpreters.\n\ |
| 2504 | \n\ |
| 2505 | If the channel is empty then the keyword args are ignored and both\n\ |
| 2506 | ends are immediately closed. Otherwise, if 'force' is True then\n\ |
| 2507 | all queued items are released and both ends are immediately\n\ |
| 2508 | closed.\n\ |
| 2509 | \n\ |
| 2510 | If the channel is not empty *and* 'force' is False then following\n\ |
| 2511 | happens:\n\ |
| 2512 | \n\ |
| 2513 | * recv is True (regardless of send):\n\ |
| 2514 | - raise ChannelNotEmptyError\n\ |
| 2515 | * recv is None and send is None:\n\ |
| 2516 | - raise ChannelNotEmptyError\n\ |
| 2517 | * send is True and recv is not True:\n\ |
| 2518 | - fully close the 'send' end\n\ |
| 2519 | - close the 'recv' end to interpreters not already receiving\n\ |
| 2520 | - fully close it once empty\n\ |
| 2521 | \n\ |
| 2522 | Closing an already closed channel results in a ChannelClosedError.\n\ |
| 2523 | \n\ |
| 2524 | Once the channel's ID has no more ref counts in any interpreter\n\ |
| 2525 | the channel will be destroyed."); |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 2526 | |
| 2527 | static PyObject * |
Eric Snow | 6d2cd90 | 2018-05-16 15:04:57 -0400 | [diff] [blame] | 2528 | channel_release(PyObject *self, PyObject *args, PyObject *kwds) |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 2529 | { |
| 2530 | // Note that only the current interpreter is affected. |
Eric Snow | 6d2cd90 | 2018-05-16 15:04:57 -0400 | [diff] [blame] | 2531 | static char *kwlist[] = {"cid", "send", "recv", "force", NULL}; |
Serhiy Storchaka | bf16991 | 2019-09-13 22:50:27 +0300 | [diff] [blame] | 2532 | int64_t cid; |
Eric Snow | 6d2cd90 | 2018-05-16 15:04:57 -0400 | [diff] [blame] | 2533 | int send = 0; |
| 2534 | int recv = 0; |
| 2535 | int force = 0; |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 2536 | if (!PyArg_ParseTupleAndKeywords(args, kwds, |
Serhiy Storchaka | bf16991 | 2019-09-13 22:50:27 +0300 | [diff] [blame] | 2537 | "O&|$ppp:channel_release", kwlist, |
| 2538 | channel_id_converter, &cid, &send, &recv, &force)) { |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 2539 | return NULL; |
| 2540 | } |
Eric Snow | 6d2cd90 | 2018-05-16 15:04:57 -0400 | [diff] [blame] | 2541 | if (send == 0 && recv == 0) { |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 2542 | send = 1; |
| 2543 | recv = 1; |
| 2544 | } |
Eric Snow | 6d2cd90 | 2018-05-16 15:04:57 -0400 | [diff] [blame] | 2545 | |
| 2546 | // XXX Handle force is True. |
| 2547 | // XXX Fix implicit release. |
| 2548 | |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 2549 | if (_channel_drop(&_globals.channels, cid, send, recv) != 0) { |
| 2550 | return NULL; |
| 2551 | } |
| 2552 | Py_RETURN_NONE; |
| 2553 | } |
| 2554 | |
Eric Snow | 6d2cd90 | 2018-05-16 15:04:57 -0400 | [diff] [blame] | 2555 | PyDoc_STRVAR(channel_release_doc, |
| 2556 | "channel_release(cid, *, send=None, recv=None, force=True)\n\ |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 2557 | \n\ |
| 2558 | Close the channel for the current interpreter. 'send' and 'recv'\n\ |
| 2559 | (bool) may be used to indicate the ends to close. By default both\n\ |
| 2560 | ends are closed. Closing an already closed end is a noop."); |
| 2561 | |
| 2562 | static PyObject * |
| 2563 | channel__channel_id(PyObject *self, PyObject *args, PyObject *kwds) |
| 2564 | { |
| 2565 | return channelid_new(&ChannelIDtype, args, kwds); |
| 2566 | } |
| 2567 | |
| 2568 | static PyMethodDef module_functions[] = { |
Victor Stinner | 252346a | 2020-05-01 11:33:44 +0200 | [diff] [blame] | 2569 | {"create", (PyCFunction)(void(*)(void))interp_create, |
| 2570 | METH_VARARGS | METH_KEYWORDS, create_doc}, |
Serhiy Storchaka | 62be742 | 2018-11-27 13:27:31 +0200 | [diff] [blame] | 2571 | {"destroy", (PyCFunction)(void(*)(void))interp_destroy, |
Eric Snow | 6d2cd90 | 2018-05-16 15:04:57 -0400 | [diff] [blame] | 2572 | METH_VARARGS | METH_KEYWORDS, destroy_doc}, |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 2573 | {"list_all", interp_list_all, |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 2574 | METH_NOARGS, list_all_doc}, |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 2575 | {"get_current", interp_get_current, |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 2576 | METH_NOARGS, get_current_doc}, |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 2577 | {"get_main", interp_get_main, |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 2578 | METH_NOARGS, get_main_doc}, |
Serhiy Storchaka | 62be742 | 2018-11-27 13:27:31 +0200 | [diff] [blame] | 2579 | {"is_running", (PyCFunction)(void(*)(void))interp_is_running, |
Eric Snow | 6d2cd90 | 2018-05-16 15:04:57 -0400 | [diff] [blame] | 2580 | METH_VARARGS | METH_KEYWORDS, is_running_doc}, |
Serhiy Storchaka | 62be742 | 2018-11-27 13:27:31 +0200 | [diff] [blame] | 2581 | {"run_string", (PyCFunction)(void(*)(void))interp_run_string, |
Eric Snow | 6d2cd90 | 2018-05-16 15:04:57 -0400 | [diff] [blame] | 2582 | METH_VARARGS | METH_KEYWORDS, run_string_doc}, |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 2583 | |
Serhiy Storchaka | 62be742 | 2018-11-27 13:27:31 +0200 | [diff] [blame] | 2584 | {"is_shareable", (PyCFunction)(void(*)(void))object_is_shareable, |
Eric Snow | 6d2cd90 | 2018-05-16 15:04:57 -0400 | [diff] [blame] | 2585 | METH_VARARGS | METH_KEYWORDS, is_shareable_doc}, |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 2586 | |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 2587 | {"channel_create", channel_create, |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 2588 | METH_NOARGS, channel_create_doc}, |
Serhiy Storchaka | 62be742 | 2018-11-27 13:27:31 +0200 | [diff] [blame] | 2589 | {"channel_destroy", (PyCFunction)(void(*)(void))channel_destroy, |
Eric Snow | 6d2cd90 | 2018-05-16 15:04:57 -0400 | [diff] [blame] | 2590 | METH_VARARGS | METH_KEYWORDS, channel_destroy_doc}, |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 2591 | {"channel_list_all", channel_list_all, |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 2592 | METH_NOARGS, channel_list_all_doc}, |
Lewis Gaul | f7bbf58 | 2020-04-29 01:18:42 +0100 | [diff] [blame] | 2593 | {"channel_list_interpreters", (PyCFunction)(void(*)(void))channel_list_interpreters, |
| 2594 | METH_VARARGS | METH_KEYWORDS, channel_list_interpreters_doc}, |
Serhiy Storchaka | 62be742 | 2018-11-27 13:27:31 +0200 | [diff] [blame] | 2595 | {"channel_send", (PyCFunction)(void(*)(void))channel_send, |
Eric Snow | 6d2cd90 | 2018-05-16 15:04:57 -0400 | [diff] [blame] | 2596 | METH_VARARGS | METH_KEYWORDS, channel_send_doc}, |
Serhiy Storchaka | 62be742 | 2018-11-27 13:27:31 +0200 | [diff] [blame] | 2597 | {"channel_recv", (PyCFunction)(void(*)(void))channel_recv, |
Eric Snow | 6d2cd90 | 2018-05-16 15:04:57 -0400 | [diff] [blame] | 2598 | METH_VARARGS | METH_KEYWORDS, channel_recv_doc}, |
Serhiy Storchaka | 62be742 | 2018-11-27 13:27:31 +0200 | [diff] [blame] | 2599 | {"channel_close", (PyCFunction)(void(*)(void))channel_close, |
Eric Snow | 6d2cd90 | 2018-05-16 15:04:57 -0400 | [diff] [blame] | 2600 | METH_VARARGS | METH_KEYWORDS, channel_close_doc}, |
Serhiy Storchaka | 62be742 | 2018-11-27 13:27:31 +0200 | [diff] [blame] | 2601 | {"channel_release", (PyCFunction)(void(*)(void))channel_release, |
Eric Snow | 6d2cd90 | 2018-05-16 15:04:57 -0400 | [diff] [blame] | 2602 | METH_VARARGS | METH_KEYWORDS, channel_release_doc}, |
Serhiy Storchaka | 62be742 | 2018-11-27 13:27:31 +0200 | [diff] [blame] | 2603 | {"_channel_id", (PyCFunction)(void(*)(void))channel__channel_id, |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 2604 | METH_VARARGS | METH_KEYWORDS, NULL}, |
| 2605 | |
| 2606 | {NULL, NULL} /* sentinel */ |
| 2607 | }; |
| 2608 | |
| 2609 | |
| 2610 | /* initialization function */ |
| 2611 | |
| 2612 | PyDoc_STRVAR(module_doc, |
| 2613 | "This module provides primitive operations to manage Python interpreters.\n\ |
| 2614 | The 'interpreters' module provides a more convenient interface."); |
| 2615 | |
| 2616 | static struct PyModuleDef interpretersmodule = { |
| 2617 | PyModuleDef_HEAD_INIT, |
| 2618 | "_xxsubinterpreters", /* m_name */ |
| 2619 | module_doc, /* m_doc */ |
| 2620 | -1, /* m_size */ |
| 2621 | module_functions, /* m_methods */ |
| 2622 | NULL, /* m_slots */ |
| 2623 | NULL, /* m_traverse */ |
| 2624 | NULL, /* m_clear */ |
| 2625 | NULL /* m_free */ |
| 2626 | }; |
| 2627 | |
| 2628 | |
| 2629 | PyMODINIT_FUNC |
| 2630 | PyInit__xxsubinterpreters(void) |
| 2631 | { |
| 2632 | if (_init_globals() != 0) { |
| 2633 | return NULL; |
| 2634 | } |
| 2635 | |
| 2636 | /* Initialize types */ |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 2637 | if (PyType_Ready(&ChannelIDtype) != 0) { |
| 2638 | return NULL; |
| 2639 | } |
| 2640 | |
| 2641 | /* Create the module */ |
| 2642 | PyObject *module = PyModule_Create(&interpretersmodule); |
| 2643 | if (module == NULL) { |
| 2644 | return NULL; |
| 2645 | } |
| 2646 | |
| 2647 | /* Add exception types */ |
| 2648 | PyObject *ns = PyModule_GetDict(module); // borrowed |
| 2649 | if (interp_exceptions_init(ns) != 0) { |
| 2650 | return NULL; |
| 2651 | } |
| 2652 | if (channel_exceptions_init(ns) != 0) { |
| 2653 | return NULL; |
| 2654 | } |
| 2655 | |
| 2656 | /* Add other types */ |
| 2657 | Py_INCREF(&ChannelIDtype); |
| 2658 | if (PyDict_SetItemString(ns, "ChannelID", (PyObject *)&ChannelIDtype) != 0) { |
| 2659 | return NULL; |
| 2660 | } |
Eric Snow | c11183c | 2019-03-15 16:35:46 -0600 | [diff] [blame] | 2661 | Py_INCREF(&_PyInterpreterID_Type); |
| 2662 | if (PyDict_SetItemString(ns, "InterpreterID", (PyObject *)&_PyInterpreterID_Type) != 0) { |
Eric Snow | 4c6955e | 2018-02-16 18:53:40 -0700 | [diff] [blame] | 2663 | return NULL; |
| 2664 | } |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 2665 | |
Eric Snow | c11183c | 2019-03-15 16:35:46 -0600 | [diff] [blame] | 2666 | if (_PyCrossInterpreterData_RegisterClass(&ChannelIDtype, _channelid_shared)) { |
Eric Snow | 7f8bfc9 | 2018-01-29 18:23:44 -0700 | [diff] [blame] | 2667 | return NULL; |
| 2668 | } |
| 2669 | |
| 2670 | return module; |
| 2671 | } |