blob: 9290255c66c3fa56ab6b259616dabddccb6eb0d7 [file] [log] [blame]
Victor Stinnerf2c3b682020-05-14 18:46:24 +02001
2/* interpreters module */
Eric Snow7f8bfc92018-01-29 18:23:44 -07003/* low-level access to interpreter primitives */
4
5#include "Python.h"
6#include "frameobject.h"
Eric Snowc11183c2019-03-15 16:35:46 -06007#include "interpreteridobject.h"
Eric Snow7f8bfc92018-01-29 18:23:44 -07008
9
Victor Stinnerf2c3b682020-05-14 18:46:24 +020010static 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 Snow4e9da0d2018-02-02 21:49:49 -070025
Eric Snow7f8bfc92018-01-29 18:23:44 -070026static PyInterpreterState *
27_get_current(void)
28{
Victor Stinnerf2c3b682020-05-14 18:46:24 +020029 // PyInterpreterState_Get() aborts if lookup fails, so don't need
Eric Snow7f8bfc92018-01-29 18:23:44 -070030 // to check the result for NULL.
Victor Stinnerf2c3b682020-05-14 18:46:24 +020031 return PyInterpreterState_Get();
Eric Snow7f8bfc92018-01-29 18:23:44 -070032}
33
Eric Snow4e9da0d2018-02-02 21:49:49 -070034
Eric Snow7f8bfc92018-01-29 18:23:44 -070035/* data-sharing-specific code ***********************************************/
36
Eric Snow4e9da0d2018-02-02 21:49:49 -070037struct _sharednsitem {
Victor Stinnerf2c3b682020-05-14 18:46:24 +020038 char *name;
Eric Snow7f8bfc92018-01-29 18:23:44 -070039 _PyCrossInterpreterData data;
Eric Snow4e9da0d2018-02-02 21:49:49 -070040};
Eric Snow7f8bfc92018-01-29 18:23:44 -070041
Eric Snowc11183c2019-03-15 16:35:46 -060042static void _sharednsitem_clear(struct _sharednsitem *); // forward
43
Eric Snow4e9da0d2018-02-02 21:49:49 -070044static int
45_sharednsitem_init(struct _sharednsitem *item, PyObject *key, PyObject *value)
Eric Snow7f8bfc92018-01-29 18:23:44 -070046{
Victor Stinnerf2c3b682020-05-14 18:46:24 +020047 item->name = _copy_raw_string(key);
48 if (item->name == NULL) {
Eric Snow4e9da0d2018-02-02 21:49:49 -070049 return -1;
Eric Snow7f8bfc92018-01-29 18:23:44 -070050 }
Eric Snow4e9da0d2018-02-02 21:49:49 -070051 if (_PyObject_GetCrossInterpreterData(value, &item->data) != 0) {
Eric Snowc11183c2019-03-15 16:35:46 -060052 _sharednsitem_clear(item);
Eric Snow4e9da0d2018-02-02 21:49:49 -070053 return -1;
54 }
55 return 0;
Eric Snow7f8bfc92018-01-29 18:23:44 -070056}
57
Eric Snow4e9da0d2018-02-02 21:49:49 -070058static void
59_sharednsitem_clear(struct _sharednsitem *item)
60{
Victor Stinnerf2c3b682020-05-14 18:46:24 +020061 if (item->name != NULL) {
62 PyMem_Free(item->name);
63 item->name = NULL;
64 }
Eric Snow4e9da0d2018-02-02 21:49:49 -070065 _PyCrossInterpreterData_Release(&item->data);
66}
67
68static int
69_sharednsitem_apply(struct _sharednsitem *item, PyObject *ns)
70{
Victor Stinnerf2c3b682020-05-14 18:46:24 +020071 PyObject *name = PyUnicode_FromString(item->name);
Eric Snow4e9da0d2018-02-02 21:49:49 -070072 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
86typedef struct _sharedns {
87 Py_ssize_t len;
88 struct _sharednsitem* items;
89} _sharedns;
90
91static _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
109static 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
119static _sharedns *
120_get_shared_ns(PyObject *shareable)
Eric Snow7f8bfc92018-01-29 18:23:44 -0700121{
122 if (shareable == NULL || shareable == Py_None) {
Eric Snow7f8bfc92018-01-29 18:23:44 -0700123 return NULL;
124 }
125 Py_ssize_t len = PyDict_Size(shareable);
Eric Snow7f8bfc92018-01-29 18:23:44 -0700126 if (len == 0) {
127 return NULL;
128 }
129
Eric Snow4e9da0d2018-02-02 21:49:49 -0700130 _sharedns *shared = _sharedns_new(len);
Eric Snow7f8bfc92018-01-29 18:23:44 -0700131 if (shared == NULL) {
132 return NULL;
133 }
Eric Snow7f8bfc92018-01-29 18:23:44 -0700134 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 Snow4e9da0d2018-02-02 21:49:49 -0700140 if (_sharednsitem_init(&shared->items[i], key, value) != 0) {
Eric Snow7f8bfc92018-01-29 18:23:44 -0700141 break;
142 }
Eric Snow7f8bfc92018-01-29 18:23:44 -0700143 }
144 if (PyErr_Occurred()) {
Eric Snow4e9da0d2018-02-02 21:49:49 -0700145 _sharedns_free(shared);
Eric Snow7f8bfc92018-01-29 18:23:44 -0700146 return NULL;
147 }
148 return shared;
149}
150
151static int
Eric Snow4e9da0d2018-02-02 21:49:49 -0700152_sharedns_apply(_sharedns *shared, PyObject *ns)
Eric Snow7f8bfc92018-01-29 18:23:44 -0700153{
Eric Snow4e9da0d2018-02-02 21:49:49 -0700154 for (Py_ssize_t i=0; i < shared->len; i++) {
155 if (_sharednsitem_apply(&shared->items[i], ns) != 0) {
156 return -1;
157 }
Eric Snow7f8bfc92018-01-29 18:23:44 -0700158 }
Eric Snow4e9da0d2018-02-02 21:49:49 -0700159 return 0;
Eric Snow7f8bfc92018-01-29 18:23:44 -0700160}
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
167typedef struct _sharedexception {
Victor Stinnerf2c3b682020-05-14 18:46:24 +0200168 char *name;
169 char *msg;
Eric Snow7f8bfc92018-01-29 18:23:44 -0700170} _sharedexception;
171
172static _sharedexception *
Eric Snow4e9da0d2018-02-02 21:49:49 -0700173_sharedexception_new(void)
Eric Snow7f8bfc92018-01-29 18:23:44 -0700174{
Victor Stinnerf2c3b682020-05-14 18:46:24 +0200175 _sharedexception *err = PyMem_NEW(_sharedexception, 1);
176 if (err == NULL) {
Eric Snow4e9da0d2018-02-02 21:49:49 -0700177 PyErr_NoMemory();
Eric Snow7f8bfc92018-01-29 18:23:44 -0700178 return NULL;
179 }
Victor Stinnerf2c3b682020-05-14 18:46:24 +0200180 err->name = NULL;
181 err->msg = NULL;
182 return err;
Eric Snow4e9da0d2018-02-02 21:49:49 -0700183}
184
185static void
Victor Stinnerf2c3b682020-05-14 18:46:24 +0200186_sharedexception_clear(_sharedexception *exc)
Eric Snow4e9da0d2018-02-02 21:49:49 -0700187{
Victor Stinnerf2c3b682020-05-14 18:46:24 +0200188 if (exc->name != NULL) {
189 PyMem_Free(exc->name);
Eric Snow4e9da0d2018-02-02 21:49:49 -0700190 }
Victor Stinnerf2c3b682020-05-14 18:46:24 +0200191 if (exc->msg != NULL) {
192 PyMem_Free(exc->msg);
193 }
Eric Snow7f8bfc92018-01-29 18:23:44 -0700194}
195
Eric Snow7f8bfc92018-01-29 18:23:44 -0700196static void
Victor Stinnerf2c3b682020-05-14 18:46:24 +0200197_sharedexception_free(_sharedexception *exc)
Eric Snow7f8bfc92018-01-29 18:23:44 -0700198{
Victor Stinnerf2c3b682020-05-14 18:46:24 +0200199 _sharedexception_clear(exc);
200 PyMem_Free(exc);
201}
Eric Snowa1d9e0a2020-05-07 08:56:01 -0600202
Victor Stinnerf2c3b682020-05-14 18:46:24 +0200203static _sharedexception *
204_sharedexception_bind(PyObject *exctype, PyObject *exc, PyObject *tb)
205{
206 assert(exctype != NULL);
207 char *failure = NULL;
Eric Snowa1d9e0a2020-05-07 08:56:01 -0600208
Victor Stinnerf2c3b682020-05-14 18:46:24 +0200209 _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 Snow4e9da0d2018-02-02 21:49:49 -0700245 }
246 }
Victor Stinnerf2c3b682020-05-14 18:46:24 +0200247
248finally:
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 Snowa1d9e0a2020-05-07 08:56:01 -0600258}
259
Victor Stinnerf2c3b682020-05-14 18:46:24 +0200260static void
261_sharedexception_apply(_sharedexception *exc, PyObject *wrapperclass)
Eric Snowa1d9e0a2020-05-07 08:56:01 -0600262{
Victor Stinnerf2c3b682020-05-14 18:46:24 +0200263 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 Snow4e9da0d2018-02-02 21:49:49 -0700270 }
Victor Stinnerf2c3b682020-05-14 18:46:24 +0200271 else if (exc->msg != NULL) {
272 PyErr_SetString(wrapperclass, exc->msg);
273 }
274 else {
275 PyErr_SetNone(wrapperclass);
276 }
Eric Snow7f8bfc92018-01-29 18:23:44 -0700277}
278
Eric Snow4e9da0d2018-02-02 21:49:49 -0700279
280/* channel-specific code ****************************************************/
Eric Snow7f8bfc92018-01-29 18:23:44 -0700281
Eric Snow3ab01362018-05-17 10:27:09 -0400282#define CHANNEL_SEND 1
283#define CHANNEL_BOTH 0
284#define CHANNEL_RECV -1
285
Eric Snow7f8bfc92018-01-29 18:23:44 -0700286static PyObject *ChannelError;
287static PyObject *ChannelNotFoundError;
288static PyObject *ChannelClosedError;
289static PyObject *ChannelEmptyError;
Eric Snow3ab01362018-05-17 10:27:09 -0400290static PyObject *ChannelNotEmptyError;
Eric Snow7f8bfc92018-01-29 18:23:44 -0700291
292static int
293channel_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 Snow3ab01362018-05-17 10:27:09 -0400337 // 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 Snow7f8bfc92018-01-29 18:23:44 -0700347 return 0;
348}
349
Eric Snow4e9da0d2018-02-02 21:49:49 -0700350/* the channel queue */
351
352struct _channelitem;
353
354typedef struct _channelitem {
355 _PyCrossInterpreterData *data;
356 struct _channelitem *next;
357} _channelitem;
358
359static _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
372static 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
383static void
384_channelitem_free(_channelitem *item)
385{
386 _channelitem_clear(item);
387 PyMem_Free(item);
388}
389
390static 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
400static _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
409typedef struct _channelqueue {
410 int64_t count;
411 _channelitem *first;
412 _channelitem *last;
413} _channelqueue;
414
415static _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
429static 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
438static void
439_channelqueue_free(_channelqueue *queue)
440{
441 _channelqueue_clear(queue);
442 PyMem_Free(queue);
443}
444
445static 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
465static _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 Snow7f8bfc92018-01-29 18:23:44 -0700483struct _channelend;
484
485typedef struct _channelend {
486 struct _channelend *next;
487 int64_t interp;
488 int open;
489} _channelend;
490
491static _channelend *
492_channelend_new(int64_t interp)
493{
494 _channelend *end = PyMem_NEW(_channelend, 1);
495 if (end == NULL) {
Eric Snow4e9da0d2018-02-02 21:49:49 -0700496 PyErr_NoMemory();
Eric Snow7f8bfc92018-01-29 18:23:44 -0700497 return NULL;
498 }
Eric Snow7f8bfc92018-01-29 18:23:44 -0700499 end->next = NULL;
500 end->interp = interp;
Eric Snow7f8bfc92018-01-29 18:23:44 -0700501 end->open = 1;
Eric Snow7f8bfc92018-01-29 18:23:44 -0700502 return end;
503}
504
505static void
Eric Snow4e9da0d2018-02-02 21:49:49 -0700506_channelend_free(_channelend *end)
507{
508 PyMem_Free(end);
509}
510
511static void
512_channelend_free_all(_channelend *end)
513{
Eric Snow7f8bfc92018-01-29 18:23:44 -0700514 while (end != NULL) {
515 _channelend *last = end;
516 end = end->next;
Eric Snow4e9da0d2018-02-02 21:49:49 -0700517 _channelend_free(last);
Eric Snow7f8bfc92018-01-29 18:23:44 -0700518 }
519}
520
521static _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 Snow4e9da0d2018-02-02 21:49:49 -0700539typedef struct _channelassociations {
Eric Snow7f8bfc92018-01-29 18:23:44 -0700540 // Note that the list entries are never removed for interpreter
Lewis Gaulf7bbf582020-04-29 01:18:42 +0100541 // for which the channel is closed. This should not be a problem in
Eric Snow7f8bfc92018-01-29 18:23:44 -0700542 // 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 Snow4e9da0d2018-02-02 21:49:49 -0700548} _channelends;
549
550static _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
564static 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
576static void
577_channelends_free(_channelends *ends)
578{
579 _channelends_clear(ends);
580 PyMem_Free(ends);
581}
582
583static _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
612static 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
632static 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
644static 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
656static 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
686static void
Eric Snow3ab01362018-05-17 10:27:09 -0400687_channelends_close_all(_channelends *ends, int which, int force)
Eric Snow4e9da0d2018-02-02 21:49:49 -0700688{
Eric Snow3ab01362018-05-17 10:27:09 -0400689 // XXX Handle the ends.
690 // XXX Handle force is True.
691
Eric Snow4e9da0d2018-02-02 21:49:49 -0700692 // 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
706struct _channel;
Eric Snow3ab01362018-05-17 10:27:09 -0400707struct _channel_closing;
708static void _channel_clear_closing(struct _channel *);
709static void _channel_finish_closing(struct _channel *);
Eric Snow4e9da0d2018-02-02 21:49:49 -0700710
711typedef struct _channel {
712 PyThread_type_lock mutex;
713 _channelqueue *queue;
714 _channelends *ends;
715 int open;
Eric Snow3ab01362018-05-17 10:27:09 -0400716 struct _channel_closing *closing;
Eric Snow7f8bfc92018-01-29 18:23:44 -0700717} _PyChannelState;
718
719static _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 Snow4e9da0d2018-02-02 21:49:49 -0700733 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 Snow7f8bfc92018-01-29 18:23:44 -0700744 chan->open = 1;
Eric Snow3ab01362018-05-17 10:27:09 -0400745 chan->closing = NULL;
Eric Snow7f8bfc92018-01-29 18:23:44 -0700746 return chan;
747}
748
Eric Snow4e9da0d2018-02-02 21:49:49 -0700749static void
750_channel_free(_PyChannelState *chan)
Eric Snow7f8bfc92018-01-29 18:23:44 -0700751{
Eric Snow3ab01362018-05-17 10:27:09 -0400752 _channel_clear_closing(chan);
Eric Snow4e9da0d2018-02-02 21:49:49 -0700753 PyThread_acquire_lock(chan->mutex, WAIT_LOCK);
754 _channelqueue_free(chan->queue);
755 _channelends_free(chan->ends);
756 PyThread_release_lock(chan->mutex);
Eric Snow7f8bfc92018-01-29 18:23:44 -0700757
Eric Snow4e9da0d2018-02-02 21:49:49 -0700758 PyThread_free_lock(chan->mutex);
759 PyMem_Free(chan);
Eric Snow7f8bfc92018-01-29 18:23:44 -0700760}
761
Eric Snow4e9da0d2018-02-02 21:49:49 -0700762static int
763_channel_add(_PyChannelState *chan, int64_t interp,
764 _PyCrossInterpreterData *data)
Eric Snow7f8bfc92018-01-29 18:23:44 -0700765{
Eric Snow4e9da0d2018-02-02 21:49:49 -0700766 int res = -1;
767 PyThread_acquire_lock(chan->mutex, WAIT_LOCK);
768
Eric Snow7f8bfc92018-01-29 18:23:44 -0700769 if (!chan->open) {
770 PyErr_SetString(ChannelClosedError, "channel closed");
Eric Snow4e9da0d2018-02-02 21:49:49 -0700771 goto done;
772 }
773 if (_channelends_associate(chan->ends, interp, 1) != 0) {
774 goto done;
Eric Snow7f8bfc92018-01-29 18:23:44 -0700775 }
776
Eric Snow4e9da0d2018-02-02 21:49:49 -0700777 if (_channelqueue_put(chan->queue, data) != 0) {
778 goto done;
Eric Snow7f8bfc92018-01-29 18:23:44 -0700779 }
Eric Snow4e9da0d2018-02-02 21:49:49 -0700780
781 res = 0;
782done:
783 PyThread_release_lock(chan->mutex);
784 return res;
Eric Snow7f8bfc92018-01-29 18:23:44 -0700785}
786
Eric Snow4e9da0d2018-02-02 21:49:49 -0700787static _PyCrossInterpreterData *
788_channel_next(_PyChannelState *chan, int64_t interp)
Eric Snow7f8bfc92018-01-29 18:23:44 -0700789{
Eric Snow4e9da0d2018-02-02 21:49:49 -0700790 _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 Snow7f8bfc92018-01-29 18:23:44 -0700796 }
Eric Snow4e9da0d2018-02-02 21:49:49 -0700797 if (_channelends_associate(chan->ends, interp, 0) != 0) {
798 goto done;
Eric Snow7f8bfc92018-01-29 18:23:44 -0700799 }
Eric Snow4e9da0d2018-02-02 21:49:49 -0700800
801 data = _channelqueue_get(chan->queue);
Eric Snow3ab01362018-05-17 10:27:09 -0400802 if (data == NULL && !PyErr_Occurred() && chan->closing != NULL) {
803 chan->open = 0;
804 }
805
Eric Snow4e9da0d2018-02-02 21:49:49 -0700806done:
807 PyThread_release_lock(chan->mutex);
Eric Snow3ab01362018-05-17 10:27:09 -0400808 if (chan->queue->count == 0) {
809 _channel_finish_closing(chan);
810 }
Eric Snow4e9da0d2018-02-02 21:49:49 -0700811 return data;
Eric Snow7f8bfc92018-01-29 18:23:44 -0700812}
813
814static int
Eric Snow3ab01362018-05-17 10:27:09 -0400815_channel_close_interpreter(_PyChannelState *chan, int64_t interp, int end)
Eric Snow7f8bfc92018-01-29 18:23:44 -0700816{
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 Snow3ab01362018-05-17 10:27:09 -0400825 if (_channelends_close_interpreter(chan->ends, interp, end) != 0) {
Eric Snow4e9da0d2018-02-02 21:49:49 -0700826 goto done;
Eric Snow7f8bfc92018-01-29 18:23:44 -0700827 }
Eric Snow4e9da0d2018-02-02 21:49:49 -0700828 chan->open = _channelends_is_open(chan->ends);
Eric Snow7f8bfc92018-01-29 18:23:44 -0700829
830 res = 0;
831done:
832 PyThread_release_lock(chan->mutex);
833 return res;
834}
835
836static int
Eric Snow3ab01362018-05-17 10:27:09 -0400837_channel_close_all(_PyChannelState *chan, int end, int force)
Eric Snow7f8bfc92018-01-29 18:23:44 -0700838{
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 Snow3ab01362018-05-17 10:27:09 -0400847 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 Snow7f8bfc92018-01-29 18:23:44 -0700853 chan->open = 0;
854
855 // We *could* also just leave these in place, since we've marked
856 // the channel as closed already.
Eric Snow3ab01362018-05-17 10:27:09 -0400857 _channelends_close_all(chan->ends, end, force);
Eric Snow7f8bfc92018-01-29 18:23:44 -0700858
859 res = 0;
860done:
861 PyThread_release_lock(chan->mutex);
862 return res;
863}
864
Eric Snow4e9da0d2018-02-02 21:49:49 -0700865/* the set of channels */
Eric Snow7f8bfc92018-01-29 18:23:44 -0700866
867struct _channelref;
868
869typedef struct _channelref {
870 int64_t id;
871 _PyChannelState *chan;
872 struct _channelref *next;
873 Py_ssize_t objcount;
874} _channelref;
875
876static _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 Snow4e9da0d2018-02-02 21:49:49 -0700890//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
899static void
900_channelref_free(_channelref *ref)
901{
Eric Snow3ab01362018-05-17 10:27:09 -0400902 if (ref->chan != NULL) {
903 _channel_clear_closing(ref->chan);
904 }
Eric Snow4e9da0d2018-02-02 21:49:49 -0700905 //_channelref_clear(ref);
906 PyMem_Free(ref);
907}
908
Eric Snow7f8bfc92018-01-29 18:23:44 -0700909static _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
927typedef struct _channels {
928 PyThread_type_lock mutex;
929 _channelref *head;
930 int64_t numopen;
931 int64_t next_id;
932} _channels;
933
934static int
935_channels_init(_channels *channels)
936{
937 if (channels->mutex == NULL) {
938 channels->mutex = PyThread_allocate_lock();
939 if (channels->mutex == NULL) {
Eric Snow7f8bfc92018-01-29 18:23:44 -0700940 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
951static 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
965static _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 Snowab4a1982018-06-13 08:02:39 -0600976 PyErr_Format(ChannelNotFoundError, "channel %" PRId64 " not found", id);
Eric Snow7f8bfc92018-01-29 18:23:44 -0700977 goto done;
978 }
979 if (ref->chan == NULL || !ref->chan->open) {
Eric Snowab4a1982018-06-13 08:02:39 -0600980 PyErr_Format(ChannelClosedError, "channel %" PRId64 " closed", id);
Eric Snow7f8bfc92018-01-29 18:23:44 -0700981 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;
990done:
991 if (pmutex == NULL || *pmutex == NULL) {
992 PyThread_release_lock(channels->mutex);
993 }
994 return chan;
995}
996
997static 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;
1020done:
1021 PyThread_release_lock(channels->mutex);
1022 return cid;
1023}
1024
Eric Snow3ab01362018-05-17 10:27:09 -04001025/* forward */
1026static int _channel_set_closing(struct _channelref *, PyThread_type_lock);
1027
Eric Snow7f8bfc92018-01-29 18:23:44 -07001028static int
Eric Snow3ab01362018-05-17 10:27:09 -04001029_channels_close(_channels *channels, int64_t cid, _PyChannelState **pchan,
1030 int end, int force)
Eric Snow7f8bfc92018-01-29 18:23:44 -07001031{
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 Snowab4a1982018-06-13 08:02:39 -06001040 PyErr_Format(ChannelNotFoundError, "channel %" PRId64 " not found", cid);
Eric Snow7f8bfc92018-01-29 18:23:44 -07001041 goto done;
1042 }
1043
1044 if (ref->chan == NULL) {
Eric Snowab4a1982018-06-13 08:02:39 -06001045 PyErr_Format(ChannelClosedError, "channel %" PRId64 " closed", cid);
Eric Snow7f8bfc92018-01-29 18:23:44 -07001046 goto done;
1047 }
Eric Snow3ab01362018-05-17 10:27:09 -04001048 else if (!force && end == CHANNEL_SEND && ref->chan->closing != NULL) {
Eric Snowab4a1982018-06-13 08:02:39 -06001049 PyErr_Format(ChannelClosedError, "channel %" PRId64 " closed", cid);
Eric Snow3ab01362018-05-17 10:27:09 -04001050 goto done;
1051 }
Eric Snow7f8bfc92018-01-29 18:23:44 -07001052 else {
Eric Snow3ab01362018-05-17 10:27:09 -04001053 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 Snow6854e802018-06-01 16:26:01 -06001057 PyErr_Format(ChannelClosedError,
Eric Snowab4a1982018-06-13 08:02:39 -06001058 "channel %" PRId64 " closed", cid);
Eric Snow3ab01362018-05-17 10:27:09 -04001059 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 Snow7f8bfc92018-01-29 18:23:44 -07001072 goto done;
1073 }
1074 if (pchan != NULL) {
1075 *pchan = ref->chan;
1076 }
Eric Snow3ab01362018-05-17 10:27:09 -04001077 else {
Eric Snow4e9da0d2018-02-02 21:49:49 -07001078 _channel_free(ref->chan);
1079 }
Eric Snow7f8bfc92018-01-29 18:23:44 -07001080 ref->chan = NULL;
1081 }
1082
1083 res = 0;
1084done:
1085 PyThread_release_lock(channels->mutex);
1086 return res;
1087}
1088
1089static 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 Snow4e9da0d2018-02-02 21:49:49 -07001104 _channelref_free(ref);
Eric Snow7f8bfc92018-01-29 18:23:44 -07001105}
1106
1107static 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 Snowab4a1982018-06-13 08:02:39 -06001120 PyErr_Format(ChannelNotFoundError, "channel %" PRId64 " not found", id);
Eric Snow7f8bfc92018-01-29 18:23:44 -07001121 goto done;
1122 }
1123
1124 _channels_remove_ref(channels, ref, prev, pchan);
1125
1126 res = 0;
1127done:
1128 PyThread_release_lock(channels->mutex);
1129 return res;
1130}
1131
1132static 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 Snowab4a1982018-06-13 08:02:39 -06001140 PyErr_Format(ChannelNotFoundError, "channel %" PRId64 " not found", id);
Eric Snow7f8bfc92018-01-29 18:23:44 -07001141 goto done;
1142 }
1143 ref->objcount += 1;
1144
1145 res = 0;
1146done:
1147 PyThread_release_lock(channels->mutex);
1148 return res;
1149}
1150
1151static 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
1173done:
1174 PyThread_release_lock(channels->mutex);
1175}
1176
Benjamin Peterson4629c0d2018-07-06 23:28:35 -07001177static int64_t *
Eric Snow7f8bfc92018-01-29 18:23:44 -07001178_channels_list_all(_channels *channels, int64_t *count)
1179{
1180 int64_t *cids = NULL;
1181 PyThread_acquire_lock(channels->mutex, WAIT_LOCK);
Eric Snow7f8bfc92018-01-29 18:23:44 -07001182 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;
1193done:
1194 PyThread_release_lock(channels->mutex);
1195 return cids;
1196}
1197
Eric Snow3ab01362018-05-17 10:27:09 -04001198/* support for closing non-empty channels */
1199
1200struct _channel_closing {
1201 struct _channelref *ref;
1202};
1203
1204static 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;
1224done:
1225 PyThread_release_lock(chan->mutex);
1226 return res;
1227}
1228
1229static 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
1239static 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 Spytz1a2252e2019-05-06 10:56:51 -06001250}
Eric Snow3ab01362018-05-17 10:27:09 -04001251
Eric Snow7f8bfc92018-01-29 18:23:44 -07001252/* "high"-level channel-related functions */
1253
1254static 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
1269static 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
1282static 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 Snow3ab01362018-05-17 10:27:09 -04001298 if (chan->closing != NULL) {
Eric Snowab4a1982018-06-13 08:02:39 -06001299 PyErr_Format(ChannelClosedError, "channel %" PRId64 " closed", id);
Eric Snow3ab01362018-05-17 10:27:09 -04001300 PyThread_release_lock(mutex);
1301 return -1;
1302 }
1303
Eric Snow7f8bfc92018-01-29 18:23:44 -07001304 // 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 Stinner4d61e6e2019-03-04 14:21:28 +01001311 PyThread_release_lock(mutex);
Eric Snowc11183c2019-03-15 16:35:46 -06001312 PyMem_Free(data);
Eric Snow7f8bfc92018-01-29 18:23:44 -07001313 return -1;
1314 }
1315
1316 // Add the data to the channel.
Eric Snowc11183c2019-03-15 16:35:46 -06001317 int res = _channel_add(chan, PyInterpreterState_GetID(interp), data);
Eric Snow7f8bfc92018-01-29 18:23:44 -07001318 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
1328static 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 Snowc11183c2019-03-15 16:35:46 -06001345 _PyCrossInterpreterData *data = _channel_next(chan, PyInterpreterState_GetID(interp));
Eric Snow7f8bfc92018-01-29 18:23:44 -07001346 PyThread_release_lock(mutex);
1347 if (data == NULL) {
Eric Snow7f8bfc92018-01-29 18:23:44 -07001348 return NULL;
1349 }
1350
1351 // Convert the data back to an object.
1352 PyObject *obj = _PyCrossInterpreterData_NewObject(data);
Eric Snow5e8c6912020-04-28 17:11:32 -06001353 _PyCrossInterpreterData_Release(data);
1354 PyMem_Free(data);
Eric Snow7f8bfc92018-01-29 18:23:44 -07001355 if (obj == NULL) {
1356 return NULL;
1357 }
Eric Snow7f8bfc92018-01-29 18:23:44 -07001358
1359 return obj;
1360}
1361
1362static 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 Snowc11183c2019-03-15 16:35:46 -06001379 int res = _channel_close_interpreter(chan, PyInterpreterState_GetID(interp), send-recv);
Eric Snow7f8bfc92018-01-29 18:23:44 -07001380 PyThread_release_lock(mutex);
1381 return res;
1382}
1383
1384static int
Eric Snow3ab01362018-05-17 10:27:09 -04001385_channel_close(_channels *channels, int64_t id, int end, int force)
Eric Snow7f8bfc92018-01-29 18:23:44 -07001386{
Eric Snow3ab01362018-05-17 10:27:09 -04001387 return _channels_close(channels, id, NULL, end, force);
Eric Snow7f8bfc92018-01-29 18:23:44 -07001388}
1389
Lewis Gaulf7bbf582020-04-29 01:18:42 +01001390static 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 Snow7f8bfc92018-01-29 18:23:44 -07001408/* ChannelID class */
1409
Eric Snow7f8bfc92018-01-29 18:23:44 -07001410static PyTypeObject ChannelIDtype;
1411
1412typedef struct channelid {
1413 PyObject_HEAD
1414 int64_t id;
1415 int end;
Eric Snow6d2cd902018-05-16 15:04:57 -04001416 int resolve;
Eric Snow7f8bfc92018-01-29 18:23:44 -07001417 _channels *channels;
1418} channelid;
1419
Serhiy Storchakabf169912019-09-13 22:50:27 +03001420static int
1421channel_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 Stinnerdaa97562020-02-07 03:37:06 +01001441 Py_TYPE(arg)->tp_name);
Serhiy Storchakabf169912019-09-13 22:50:27 +03001442 return 0;
1443 }
1444 *(int64_t *)ptr = cid;
1445 return 1;
1446}
1447
Eric Snow7f8bfc92018-01-29 18:23:44 -07001448static channelid *
1449newchannelid(PyTypeObject *cls, int64_t cid, int end, _channels *channels,
Eric Snow6d2cd902018-05-16 15:04:57 -04001450 int force, int resolve)
Eric Snow7f8bfc92018-01-29 18:23:44 -07001451{
1452 channelid *self = PyObject_New(channelid, cls);
1453 if (self == NULL) {
1454 return NULL;
1455 }
1456 self->id = cid;
1457 self->end = end;
Eric Snow6d2cd902018-05-16 15:04:57 -04001458 self->resolve = resolve;
Eric Snow7f8bfc92018-01-29 18:23:44 -07001459 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
1474static _channels * _global_channels(void);
1475
1476static PyObject *
1477channelid_new(PyTypeObject *cls, PyObject *args, PyObject *kwds)
1478{
Eric Snow6d2cd902018-05-16 15:04:57 -04001479 static char *kwlist[] = {"id", "send", "recv", "force", "_resolve", NULL};
Serhiy Storchakabf169912019-09-13 22:50:27 +03001480 int64_t cid;
Eric Snow7f8bfc92018-01-29 18:23:44 -07001481 int send = -1;
1482 int recv = -1;
1483 int force = 0;
Eric Snow6d2cd902018-05-16 15:04:57 -04001484 int resolve = 0;
Eric Snow7f8bfc92018-01-29 18:23:44 -07001485 if (!PyArg_ParseTupleAndKeywords(args, kwds,
Serhiy Storchakabf169912019-09-13 22:50:27 +03001486 "O&|$pppp:ChannelID.__new__", kwlist,
1487 channel_id_converter, &cid, &send, &recv, &force, &resolve))
Eric Snow7f8bfc92018-01-29 18:23:44 -07001488 return NULL;
1489
Eric Snow7f8bfc92018-01-29 18:23:44 -07001490 // 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 Snow4e9da0d2018-02-02 21:49:49 -07001496
Eric Snow7f8bfc92018-01-29 18:23:44 -07001497 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 Snow6d2cd902018-05-16 15:04:57 -04001507 return (PyObject *)newchannelid(cls, cid, end, _global_channels(),
1508 force, resolve);
Eric Snow7f8bfc92018-01-29 18:23:44 -07001509}
1510
1511static void
1512channelid_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
1521static PyObject *
1522channelid_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 Snowab4a1982018-06-13 08:02:39 -06001530 fmt = "%s(%" PRId64 ", send=True)";
Eric Snow7f8bfc92018-01-29 18:23:44 -07001531 }
1532 else if (cid->end == CHANNEL_RECV) {
Eric Snowab4a1982018-06-13 08:02:39 -06001533 fmt = "%s(%" PRId64 ", recv=True)";
Eric Snow7f8bfc92018-01-29 18:23:44 -07001534 }
1535 else {
Eric Snowab4a1982018-06-13 08:02:39 -06001536 fmt = "%s(%" PRId64 ")";
Eric Snow7f8bfc92018-01-29 18:23:44 -07001537 }
1538 return PyUnicode_FromFormat(fmt, name, cid->id);
1539}
1540
Eric Snow6d2cd902018-05-16 15:04:57 -04001541static PyObject *
1542channelid_str(PyObject *self)
1543{
1544 channelid *cid = (channelid *)self;
Eric Snowab4a1982018-06-13 08:02:39 -06001545 return PyUnicode_FromFormat("%" PRId64 "", cid->id);
Eric Snow6d2cd902018-05-16 15:04:57 -04001546}
1547
Benjamin Peterson4629c0d2018-07-06 23:28:35 -07001548static PyObject *
Eric Snow7f8bfc92018-01-29 18:23:44 -07001549channelid_int(PyObject *self)
1550{
1551 channelid *cid = (channelid *)self;
1552 return PyLong_FromLongLong(cid->id);
1553}
1554
1555static 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
1595static Py_hash_t
1596channelid_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 Snow4e9da0d2018-02-02 21:49:49 -07001603 Py_hash_t hash = PyObject_Hash(id);
1604 Py_DECREF(id);
1605 return hash;
Eric Snow7f8bfc92018-01-29 18:23:44 -07001606}
1607
1608static PyObject *
1609channelid_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 Storchakabf169912019-09-13 22:50:27 +03001623 equal = (cid->end == othercid->end) && (cid->id == othercid->id);
Eric Snow7f8bfc92018-01-29 18:23:44 -07001624 }
Serhiy Storchakabf169912019-09-13 22:50:27 +03001625 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 Snow7f8bfc92018-01-29 18:23:44 -07001630 return NULL;
1631 }
Serhiy Storchakabf169912019-09-13 22:50:27 +03001632 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 Snow7f8bfc92018-01-29 18:23:44 -07001638 }
Serhiy Storchakabf169912019-09-13 22:50:27 +03001639 PyObject *res = PyObject_RichCompare(pyid, other, op);
1640 Py_DECREF(pyid);
1641 return res;
1642 }
1643 else {
1644 Py_RETURN_NOTIMPLEMENTED;
Eric Snow7f8bfc92018-01-29 18:23:44 -07001645 }
1646
1647 if ((op == Py_EQ && equal) || (op == Py_NE && !equal)) {
1648 Py_RETURN_TRUE;
1649 }
1650 Py_RETURN_FALSE;
1651}
1652
Eric Snowab4a1982018-06-13 08:02:39 -06001653static 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 Snow7f8bfc92018-01-29 18:23:44 -07001679struct _channelid_xid {
1680 int64_t id;
1681 int end;
Eric Snow6d2cd902018-05-16 15:04:57 -04001682 int resolve;
Eric Snow7f8bfc92018-01-29 18:23:44 -07001683};
1684
1685static PyObject *
1686_channelid_from_xid(_PyCrossInterpreterData *data)
1687{
1688 struct _channelid_xid *xid = (struct _channelid_xid *)data->data;
Eric Snow6d2cd902018-05-16 15:04:57 -04001689 // 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 Snowab4a1982018-06-13 08:02:39 -06001700 PyObject *chan = _channel_from_cid(cid, xid->end);
Eric Snow6d2cd902018-05-16 15:04:57 -04001701 if (chan == NULL) {
Eric Snowab4a1982018-06-13 08:02:39 -06001702 PyErr_Clear();
1703 return cid;
Eric Snow6d2cd902018-05-16 15:04:57 -04001704 }
1705 Py_DECREF(cid);
1706 return chan;
Eric Snow7f8bfc92018-01-29 18:23:44 -07001707}
1708
1709static 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 Snow6d2cd902018-05-16 15:04:57 -04001718 xid->resolve = ((channelid *)obj)->resolve;
Eric Snow7f8bfc92018-01-29 18:23:44 -07001719
1720 data->data = xid;
Eric Snow63799132018-06-01 18:45:20 -06001721 Py_INCREF(obj);
Eric Snow7f8bfc92018-01-29 18:23:44 -07001722 data->obj = obj;
1723 data->new_object = _channelid_from_xid;
1724 data->free = PyMem_Free;
1725 return 0;
1726}
1727
1728static PyObject *
1729channelid_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 Snow6d2cd902018-05-16 15:04:57 -04001735 cid->channels, force, cid->resolve);
Eric Snow7f8bfc92018-01-29 18:23:44 -07001736 }
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
1747static int _channelid_end_send = CHANNEL_SEND;
1748static int _channelid_end_recv = CHANNEL_RECV;
1749
1750static 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
1760PyDoc_STRVAR(channelid_doc,
1761"A channel ID identifies a channel and may be used as an int.");
1762
1763static PyTypeObject ChannelIDtype = {
1764 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1765 "_xxsubinterpreters.ChannelID", /* tp_name */
Peter Eisentraut0e0bc4e2018-09-10 18:46:08 +02001766 sizeof(channelid), /* tp_basicsize */
Eric Snow7f8bfc92018-01-29 18:23:44 -07001767 0, /* tp_itemsize */
1768 (destructor)channelid_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02001769 0, /* tp_vectorcall_offset */
Eric Snow7f8bfc92018-01-29 18:23:44 -07001770 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 Snow6d2cd902018-05-16 15:04:57 -04001779 (reprfunc)channelid_str, /* tp_str */
Eric Snow7f8bfc92018-01-29 18:23:44 -07001780 0, /* tp_getattro */
1781 0, /* tp_setattro */
1782 0, /* tp_as_buffer */
Victor Stinner3bb09942021-04-30 12:46:15 +02001783 // Use Py_TPFLAGS_DISALLOW_INSTANTIATION so the type cannot be instantiated
1784 // from Python code. We do this because there is a strong relationship
1785 // between channel IDs and the channel lifecycle, so this limitation avoids
1786 // related complications. Use the _channel_id() function instead.
1787 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE
1788 | Py_TPFLAGS_DISALLOW_INSTANTIATION, /* tp_flags */
Eric Snow7f8bfc92018-01-29 18:23:44 -07001789 channelid_doc, /* tp_doc */
1790 0, /* tp_traverse */
1791 0, /* tp_clear */
1792 channelid_richcompare, /* tp_richcompare */
1793 0, /* tp_weaklistoffset */
1794 0, /* tp_iter */
1795 0, /* tp_iternext */
1796 0, /* tp_methods */
1797 0, /* tp_members */
1798 channelid_getsets, /* tp_getset */
Eric Snow7f8bfc92018-01-29 18:23:44 -07001799};
1800
Eric Snow4e9da0d2018-02-02 21:49:49 -07001801
1802/* interpreter-specific code ************************************************/
1803
1804static PyObject * RunFailedError = NULL;
1805
1806static int
1807interp_exceptions_init(PyObject *ns)
1808{
1809 // XXX Move the exceptions into per-module memory?
1810
1811 if (RunFailedError == NULL) {
1812 // An uncaught exception came out of interp_run_string().
1813 RunFailedError = PyErr_NewException("_xxsubinterpreters.RunFailedError",
1814 PyExc_RuntimeError, NULL);
1815 if (RunFailedError == NULL) {
1816 return -1;
1817 }
1818 if (PyDict_SetItemString(ns, "RunFailedError", RunFailedError) != 0) {
1819 return -1;
1820 }
1821 }
1822
1823 return 0;
1824}
Eric Snow7f8bfc92018-01-29 18:23:44 -07001825
Eric Snow7f8bfc92018-01-29 18:23:44 -07001826static int
1827_is_running(PyInterpreterState *interp)
1828{
1829 PyThreadState *tstate = PyInterpreterState_ThreadHead(interp);
1830 if (PyThreadState_Next(tstate) != NULL) {
1831 PyErr_SetString(PyExc_RuntimeError,
1832 "interpreter has more than one thread");
1833 return -1;
1834 }
Victor Stinner4386b902020-04-29 03:01:43 +02001835
1836 assert(!PyErr_Occurred());
Victor Stinner30723382020-03-25 19:52:02 +01001837 PyFrameObject *frame = PyThreadState_GetFrame(tstate);
Eric Snow7f8bfc92018-01-29 18:23:44 -07001838 if (frame == NULL) {
Eric Snow7f8bfc92018-01-29 18:23:44 -07001839 return 0;
1840 }
Victor Stinner4386b902020-04-29 03:01:43 +02001841
Mark Shannoncb9879b2020-07-17 11:44:23 +01001842 int executing = _PyFrame_IsExecuting(frame);
Victor Stinner4386b902020-04-29 03:01:43 +02001843 Py_DECREF(frame);
1844
1845 return executing;
Eric Snow7f8bfc92018-01-29 18:23:44 -07001846}
1847
1848static int
1849_ensure_not_running(PyInterpreterState *interp)
1850{
1851 int is_running = _is_running(interp);
1852 if (is_running < 0) {
1853 return -1;
1854 }
1855 if (is_running) {
1856 PyErr_Format(PyExc_RuntimeError, "interpreter already running");
1857 return -1;
1858 }
1859 return 0;
1860}
1861
1862static int
1863_run_script(PyInterpreterState *interp, const char *codestr,
Victor Stinnerf2c3b682020-05-14 18:46:24 +02001864 _sharedns *shared, _sharedexception **exc)
Eric Snow7f8bfc92018-01-29 18:23:44 -07001865{
Victor Stinnerf2c3b682020-05-14 18:46:24 +02001866 PyObject *exctype = NULL;
1867 PyObject *excval = NULL;
1868 PyObject *tb = NULL;
Eric Snow4e9da0d2018-02-02 21:49:49 -07001869
Eric Snowc11183c2019-03-15 16:35:46 -06001870 PyObject *main_mod = _PyInterpreterState_GetMainModule(interp);
Eric Snow7f8bfc92018-01-29 18:23:44 -07001871 if (main_mod == NULL) {
1872 goto error;
1873 }
1874 PyObject *ns = PyModule_GetDict(main_mod); // borrowed
1875 Py_DECREF(main_mod);
1876 if (ns == NULL) {
1877 goto error;
1878 }
1879 Py_INCREF(ns);
1880
1881 // Apply the cross-interpreter data.
1882 if (shared != NULL) {
Eric Snow4e9da0d2018-02-02 21:49:49 -07001883 if (_sharedns_apply(shared, ns) != 0) {
1884 Py_DECREF(ns);
1885 goto error;
Eric Snow7f8bfc92018-01-29 18:23:44 -07001886 }
1887 }
1888
1889 // Run the string (see PyRun_SimpleStringFlags).
1890 PyObject *result = PyRun_StringFlags(codestr, Py_file_input, ns, ns, NULL);
1891 Py_DECREF(ns);
1892 if (result == NULL) {
1893 goto error;
1894 }
1895 else {
1896 Py_DECREF(result); // We throw away the result.
1897 }
1898
Victor Stinnerf2c3b682020-05-14 18:46:24 +02001899 *exc = NULL;
Eric Snow7f8bfc92018-01-29 18:23:44 -07001900 return 0;
1901
1902error:
Victor Stinnerf2c3b682020-05-14 18:46:24 +02001903 PyErr_Fetch(&exctype, &excval, &tb);
Eric Snow4e9da0d2018-02-02 21:49:49 -07001904
Victor Stinnerf2c3b682020-05-14 18:46:24 +02001905 _sharedexception *sharedexc = _sharedexception_bind(exctype, excval, tb);
1906 Py_XDECREF(exctype);
1907 Py_XDECREF(excval);
1908 Py_XDECREF(tb);
Eric Snowa1d9e0a2020-05-07 08:56:01 -06001909 if (sharedexc == NULL) {
Victor Stinnerf2c3b682020-05-14 18:46:24 +02001910 fprintf(stderr, "RunFailedError: script raised an uncaught exception");
1911 PyErr_Clear();
1912 sharedexc = NULL;
1913 }
1914 else {
Eric Snow4e9da0d2018-02-02 21:49:49 -07001915 assert(!PyErr_Occurred());
1916 }
Victor Stinnerf2c3b682020-05-14 18:46:24 +02001917 *exc = sharedexc;
Eric Snow7f8bfc92018-01-29 18:23:44 -07001918 return -1;
1919}
1920
1921static int
1922_run_script_in_interpreter(PyInterpreterState *interp, const char *codestr,
1923 PyObject *shareables)
1924{
1925 if (_ensure_not_running(interp) < 0) {
1926 return -1;
1927 }
1928
Eric Snow4e9da0d2018-02-02 21:49:49 -07001929 _sharedns *shared = _get_shared_ns(shareables);
Eric Snow7f8bfc92018-01-29 18:23:44 -07001930 if (shared == NULL && PyErr_Occurred()) {
1931 return -1;
1932 }
1933
Victor Stinnerfb2c7c42020-05-05 20:33:06 +02001934#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
1935 // Switch to interpreter.
1936 PyThreadState *new_tstate = PyInterpreterState_ThreadHead(interp);
1937 PyThreadState *save1 = PyEval_SaveThread();
1938
1939 (void)PyThreadState_Swap(new_tstate);
1940
1941 // Run the script.
1942 _sharedexception *exc = NULL;
1943 int result = _run_script(interp, codestr, shared, &exc);
1944
1945 // Switch back.
1946 PyEval_RestoreThread(save1);
1947#else
Eric Snow7f8bfc92018-01-29 18:23:44 -07001948 // Switch to interpreter.
Eric Snowf53d9f22018-02-20 16:30:17 -07001949 PyThreadState *save_tstate = NULL;
Victor Stinnerbe793732020-03-13 18:15:33 +01001950 if (interp != PyInterpreterState_Get()) {
Eric Snowf53d9f22018-02-20 16:30:17 -07001951 // XXX Using the "head" thread isn't strictly correct.
1952 PyThreadState *tstate = PyInterpreterState_ThreadHead(interp);
1953 // XXX Possible GILState issues?
1954 save_tstate = PyThreadState_Swap(tstate);
1955 }
Eric Snow7f8bfc92018-01-29 18:23:44 -07001956
1957 // Run the script.
Victor Stinnerf2c3b682020-05-14 18:46:24 +02001958 _sharedexception *exc = NULL;
1959 int result = _run_script(interp, codestr, shared, &exc);
Eric Snow7f8bfc92018-01-29 18:23:44 -07001960
1961 // Switch back.
1962 if (save_tstate != NULL) {
1963 PyThreadState_Swap(save_tstate);
1964 }
Victor Stinnerfb2c7c42020-05-05 20:33:06 +02001965#endif
Eric Snow7f8bfc92018-01-29 18:23:44 -07001966
1967 // Propagate any exception out to the caller.
Victor Stinnerf2c3b682020-05-14 18:46:24 +02001968 if (exc != NULL) {
1969 _sharedexception_apply(exc, RunFailedError);
1970 _sharedexception_free(exc);
Eric Snow7f8bfc92018-01-29 18:23:44 -07001971 }
1972 else if (result != 0) {
1973 // We were unable to allocate a shared exception.
1974 PyErr_NoMemory();
1975 }
1976
1977 if (shared != NULL) {
Eric Snow4e9da0d2018-02-02 21:49:49 -07001978 _sharedns_free(shared);
Eric Snow7f8bfc92018-01-29 18:23:44 -07001979 }
1980
1981 return result;
1982}
1983
1984
1985/* module level code ********************************************************/
1986
1987/* globals is the process-global state for the module. It holds all
1988 the data that we need to share between interpreters, so it cannot
1989 hold PyObject values. */
1990static struct globals {
1991 _channels channels;
1992} _globals = {{0}};
1993
1994static int
1995_init_globals(void)
1996{
1997 if (_channels_init(&_globals.channels) != 0) {
1998 return -1;
1999 }
2000 return 0;
2001}
2002
2003static _channels *
2004_global_channels(void) {
2005 return &_globals.channels;
2006}
2007
2008static PyObject *
Victor Stinner252346a2020-05-01 11:33:44 +02002009interp_create(PyObject *self, PyObject *args, PyObject *kwds)
Eric Snow7f8bfc92018-01-29 18:23:44 -07002010{
Victor Stinner252346a2020-05-01 11:33:44 +02002011
2012 static char *kwlist[] = {"isolated", NULL};
2013 int isolated = 1;
2014 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|$i:create", kwlist,
2015 &isolated)) {
Eric Snow7f8bfc92018-01-29 18:23:44 -07002016 return NULL;
2017 }
2018
2019 // Create and initialize the new interpreter.
Steve Dower7b86e472021-04-21 23:34:29 +01002020 PyThreadState *save_tstate = PyThreadState_Get();
Eric Snowf53d9f22018-02-20 16:30:17 -07002021 // XXX Possible GILState issues?
Victor Stinner252346a2020-05-01 11:33:44 +02002022 PyThreadState *tstate = _Py_NewInterpreter(isolated);
Eric Snow7f8bfc92018-01-29 18:23:44 -07002023 PyThreadState_Swap(save_tstate);
2024 if (tstate == NULL) {
2025 /* Since no new thread state was created, there is no exception to
2026 propagate; raise a fresh one after swapping in the old thread
2027 state. */
2028 PyErr_SetString(PyExc_RuntimeError, "interpreter creation failed");
2029 return NULL;
2030 }
Victor Stinner30723382020-03-25 19:52:02 +01002031 PyInterpreterState *interp = PyThreadState_GetInterpreter(tstate);
2032 PyObject *idobj = _PyInterpreterState_GetIDObject(interp);
Eric Snowc11183c2019-03-15 16:35:46 -06002033 if (idobj == NULL) {
2034 // XXX Possible GILState issues?
2035 save_tstate = PyThreadState_Swap(tstate);
2036 Py_EndInterpreter(tstate);
2037 PyThreadState_Swap(save_tstate);
2038 return NULL;
2039 }
Victor Stinner30723382020-03-25 19:52:02 +01002040 _PyInterpreterState_RequireIDRef(interp, 1);
Eric Snowc11183c2019-03-15 16:35:46 -06002041 return idobj;
Eric Snow7f8bfc92018-01-29 18:23:44 -07002042}
2043
2044PyDoc_STRVAR(create_doc,
2045"create() -> ID\n\
2046\n\
2047Create a new interpreter and return a unique generated ID.");
2048
2049
2050static PyObject *
Eric Snow6d2cd902018-05-16 15:04:57 -04002051interp_destroy(PyObject *self, PyObject *args, PyObject *kwds)
Eric Snow7f8bfc92018-01-29 18:23:44 -07002052{
Eric Snow6d2cd902018-05-16 15:04:57 -04002053 static char *kwlist[] = {"id", NULL};
Eric Snow7f8bfc92018-01-29 18:23:44 -07002054 PyObject *id;
Eric Snow6d2cd902018-05-16 15:04:57 -04002055 // XXX Use "L" for id?
2056 if (!PyArg_ParseTupleAndKeywords(args, kwds,
2057 "O:destroy", kwlist, &id)) {
Eric Snow7f8bfc92018-01-29 18:23:44 -07002058 return NULL;
2059 }
Eric Snow7f8bfc92018-01-29 18:23:44 -07002060
2061 // Look up the interpreter.
Eric Snowc11183c2019-03-15 16:35:46 -06002062 PyInterpreterState *interp = _PyInterpreterID_LookUp(id);
Eric Snow7f8bfc92018-01-29 18:23:44 -07002063 if (interp == NULL) {
2064 return NULL;
2065 }
2066
2067 // Ensure we don't try to destroy the current interpreter.
2068 PyInterpreterState *current = _get_current();
2069 if (current == NULL) {
2070 return NULL;
2071 }
2072 if (interp == current) {
2073 PyErr_SetString(PyExc_RuntimeError,
2074 "cannot destroy the current interpreter");
2075 return NULL;
2076 }
2077
2078 // Ensure the interpreter isn't running.
2079 /* XXX We *could* support destroying a running interpreter but
2080 aren't going to worry about it for now. */
2081 if (_ensure_not_running(interp) < 0) {
2082 return NULL;
2083 }
2084
2085 // Destroy the interpreter.
Eric Snowf53d9f22018-02-20 16:30:17 -07002086 PyThreadState *tstate = PyInterpreterState_ThreadHead(interp);
2087 // XXX Possible GILState issues?
2088 PyThreadState *save_tstate = PyThreadState_Swap(tstate);
Eric Snow7f8bfc92018-01-29 18:23:44 -07002089 Py_EndInterpreter(tstate);
2090 PyThreadState_Swap(save_tstate);
2091
2092 Py_RETURN_NONE;
2093}
2094
2095PyDoc_STRVAR(destroy_doc,
Eric Snow6d2cd902018-05-16 15:04:57 -04002096"destroy(id)\n\
Eric Snow7f8bfc92018-01-29 18:23:44 -07002097\n\
2098Destroy the identified interpreter.\n\
2099\n\
2100Attempting to destroy the current interpreter results in a RuntimeError.\n\
2101So does an unrecognized ID.");
2102
2103
2104static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302105interp_list_all(PyObject *self, PyObject *Py_UNUSED(ignored))
Eric Snow7f8bfc92018-01-29 18:23:44 -07002106{
2107 PyObject *ids, *id;
2108 PyInterpreterState *interp;
2109
2110 ids = PyList_New(0);
2111 if (ids == NULL) {
2112 return NULL;
2113 }
2114
2115 interp = PyInterpreterState_Head();
2116 while (interp != NULL) {
Eric Snowc11183c2019-03-15 16:35:46 -06002117 id = _PyInterpreterState_GetIDObject(interp);
Eric Snow7f8bfc92018-01-29 18:23:44 -07002118 if (id == NULL) {
2119 Py_DECREF(ids);
2120 return NULL;
2121 }
2122 // insert at front of list
Eric Snow4e9da0d2018-02-02 21:49:49 -07002123 int res = PyList_Insert(ids, 0, id);
2124 Py_DECREF(id);
2125 if (res < 0) {
Eric Snow7f8bfc92018-01-29 18:23:44 -07002126 Py_DECREF(ids);
2127 return NULL;
2128 }
2129
2130 interp = PyInterpreterState_Next(interp);
2131 }
2132
2133 return ids;
2134}
2135
2136PyDoc_STRVAR(list_all_doc,
2137"list_all() -> [ID]\n\
2138\n\
2139Return a list containing the ID of every existing interpreter.");
2140
2141
2142static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302143interp_get_current(PyObject *self, PyObject *Py_UNUSED(ignored))
Eric Snow7f8bfc92018-01-29 18:23:44 -07002144{
2145 PyInterpreterState *interp =_get_current();
2146 if (interp == NULL) {
2147 return NULL;
2148 }
Eric Snowc11183c2019-03-15 16:35:46 -06002149 return _PyInterpreterState_GetIDObject(interp);
Eric Snow7f8bfc92018-01-29 18:23:44 -07002150}
2151
2152PyDoc_STRVAR(get_current_doc,
2153"get_current() -> ID\n\
2154\n\
2155Return the ID of current interpreter.");
2156
2157
2158static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302159interp_get_main(PyObject *self, PyObject *Py_UNUSED(ignored))
Eric Snow7f8bfc92018-01-29 18:23:44 -07002160{
2161 // Currently, 0 is always the main interpreter.
Victor Stinner1a1bd2e2020-04-17 19:13:06 +02002162 int64_t id = 0;
Eric Snowc11183c2019-03-15 16:35:46 -06002163 return _PyInterpreterID_New(id);
Eric Snow7f8bfc92018-01-29 18:23:44 -07002164}
2165
2166PyDoc_STRVAR(get_main_doc,
2167"get_main() -> ID\n\
2168\n\
2169Return the ID of main interpreter.");
2170
2171
2172static PyObject *
Eric Snow6d2cd902018-05-16 15:04:57 -04002173interp_run_string(PyObject *self, PyObject *args, PyObject *kwds)
Eric Snow7f8bfc92018-01-29 18:23:44 -07002174{
Eric Snow6d2cd902018-05-16 15:04:57 -04002175 static char *kwlist[] = {"id", "script", "shared", NULL};
Eric Snow7f8bfc92018-01-29 18:23:44 -07002176 PyObject *id, *code;
2177 PyObject *shared = NULL;
Eric Snow6d2cd902018-05-16 15:04:57 -04002178 if (!PyArg_ParseTupleAndKeywords(args, kwds,
2179 "OU|O:run_string", kwlist,
2180 &id, &code, &shared)) {
Eric Snow7f8bfc92018-01-29 18:23:44 -07002181 return NULL;
2182 }
Eric Snow7f8bfc92018-01-29 18:23:44 -07002183
2184 // Look up the interpreter.
Eric Snowc11183c2019-03-15 16:35:46 -06002185 PyInterpreterState *interp = _PyInterpreterID_LookUp(id);
Eric Snow7f8bfc92018-01-29 18:23:44 -07002186 if (interp == NULL) {
2187 return NULL;
2188 }
2189
2190 // Extract code.
2191 Py_ssize_t size;
2192 const char *codestr = PyUnicode_AsUTF8AndSize(code, &size);
2193 if (codestr == NULL) {
2194 return NULL;
2195 }
2196 if (strlen(codestr) != (size_t)size) {
2197 PyErr_SetString(PyExc_ValueError,
2198 "source code string cannot contain null bytes");
2199 return NULL;
2200 }
2201
2202 // Run the code in the interpreter.
2203 if (_run_script_in_interpreter(interp, codestr, shared) != 0) {
2204 return NULL;
2205 }
2206 Py_RETURN_NONE;
2207}
2208
2209PyDoc_STRVAR(run_string_doc,
Eric Snow6d2cd902018-05-16 15:04:57 -04002210"run_string(id, script, shared)\n\
Eric Snow7f8bfc92018-01-29 18:23:44 -07002211\n\
2212Execute the provided string in the identified interpreter.\n\
2213\n\
2214See PyRun_SimpleStrings.");
2215
2216
2217static PyObject *
Eric Snow6d2cd902018-05-16 15:04:57 -04002218object_is_shareable(PyObject *self, PyObject *args, PyObject *kwds)
Eric Snow7f8bfc92018-01-29 18:23:44 -07002219{
Eric Snow6d2cd902018-05-16 15:04:57 -04002220 static char *kwlist[] = {"obj", NULL};
Eric Snow7f8bfc92018-01-29 18:23:44 -07002221 PyObject *obj;
Eric Snow6d2cd902018-05-16 15:04:57 -04002222 if (!PyArg_ParseTupleAndKeywords(args, kwds,
2223 "O:is_shareable", kwlist, &obj)) {
Eric Snow7f8bfc92018-01-29 18:23:44 -07002224 return NULL;
2225 }
Eric Snow6d2cd902018-05-16 15:04:57 -04002226
Eric Snow7f8bfc92018-01-29 18:23:44 -07002227 if (_PyObject_CheckCrossInterpreterData(obj) == 0) {
2228 Py_RETURN_TRUE;
2229 }
2230 PyErr_Clear();
2231 Py_RETURN_FALSE;
2232}
2233
2234PyDoc_STRVAR(is_shareable_doc,
2235"is_shareable(obj) -> bool\n\
2236\n\
2237Return True if the object's data may be shared between interpreters and\n\
2238False otherwise.");
2239
2240
2241static PyObject *
Eric Snow6d2cd902018-05-16 15:04:57 -04002242interp_is_running(PyObject *self, PyObject *args, PyObject *kwds)
Eric Snow7f8bfc92018-01-29 18:23:44 -07002243{
Eric Snow6d2cd902018-05-16 15:04:57 -04002244 static char *kwlist[] = {"id", NULL};
Eric Snow7f8bfc92018-01-29 18:23:44 -07002245 PyObject *id;
Eric Snow6d2cd902018-05-16 15:04:57 -04002246 if (!PyArg_ParseTupleAndKeywords(args, kwds,
2247 "O:is_running", kwlist, &id)) {
Eric Snow7f8bfc92018-01-29 18:23:44 -07002248 return NULL;
2249 }
Eric Snow7f8bfc92018-01-29 18:23:44 -07002250
Eric Snowc11183c2019-03-15 16:35:46 -06002251 PyInterpreterState *interp = _PyInterpreterID_LookUp(id);
Eric Snow7f8bfc92018-01-29 18:23:44 -07002252 if (interp == NULL) {
2253 return NULL;
2254 }
2255 int is_running = _is_running(interp);
2256 if (is_running < 0) {
2257 return NULL;
2258 }
2259 if (is_running) {
2260 Py_RETURN_TRUE;
2261 }
2262 Py_RETURN_FALSE;
2263}
2264
2265PyDoc_STRVAR(is_running_doc,
2266"is_running(id) -> bool\n\
2267\n\
2268Return whether or not the identified interpreter is running.");
2269
2270static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302271channel_create(PyObject *self, PyObject *Py_UNUSED(ignored))
Eric Snow7f8bfc92018-01-29 18:23:44 -07002272{
2273 int64_t cid = _channel_create(&_globals.channels);
2274 if (cid < 0) {
2275 return NULL;
2276 }
2277 PyObject *id = (PyObject *)newchannelid(&ChannelIDtype, cid, 0,
Eric Snow6d2cd902018-05-16 15:04:57 -04002278 &_globals.channels, 0, 0);
Eric Snow7f8bfc92018-01-29 18:23:44 -07002279 if (id == NULL) {
2280 if (_channel_destroy(&_globals.channels, cid) != 0) {
2281 // XXX issue a warning?
2282 }
2283 return NULL;
2284 }
2285 assert(((channelid *)id)->channels != NULL);
2286 return id;
2287}
2288
2289PyDoc_STRVAR(channel_create_doc,
Eric Snow6d2cd902018-05-16 15:04:57 -04002290"channel_create() -> cid\n\
Eric Snow7f8bfc92018-01-29 18:23:44 -07002291\n\
2292Create a new cross-interpreter channel and return a unique generated ID.");
2293
2294static PyObject *
Eric Snow6d2cd902018-05-16 15:04:57 -04002295channel_destroy(PyObject *self, PyObject *args, PyObject *kwds)
Eric Snow7f8bfc92018-01-29 18:23:44 -07002296{
Eric Snow6d2cd902018-05-16 15:04:57 -04002297 static char *kwlist[] = {"cid", NULL};
Serhiy Storchakabf169912019-09-13 22:50:27 +03002298 int64_t cid;
2299 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O&:channel_destroy", kwlist,
2300 channel_id_converter, &cid)) {
Eric Snow7f8bfc92018-01-29 18:23:44 -07002301 return NULL;
2302 }
2303
2304 if (_channel_destroy(&_globals.channels, cid) != 0) {
2305 return NULL;
2306 }
2307 Py_RETURN_NONE;
2308}
2309
2310PyDoc_STRVAR(channel_destroy_doc,
Eric Snow6d2cd902018-05-16 15:04:57 -04002311"channel_destroy(cid)\n\
Eric Snow7f8bfc92018-01-29 18:23:44 -07002312\n\
2313Close and finalize the channel. Afterward attempts to use the channel\n\
2314will behave as though it never existed.");
2315
2316static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302317channel_list_all(PyObject *self, PyObject *Py_UNUSED(ignored))
Eric Snow7f8bfc92018-01-29 18:23:44 -07002318{
2319 int64_t count = 0;
2320 int64_t *cids = _channels_list_all(&_globals.channels, &count);
2321 if (cids == NULL) {
2322 if (count == 0) {
2323 return PyList_New(0);
2324 }
2325 return NULL;
2326 }
2327 PyObject *ids = PyList_New((Py_ssize_t)count);
2328 if (ids == NULL) {
Eric Snow4e9da0d2018-02-02 21:49:49 -07002329 goto finally;
Eric Snow7f8bfc92018-01-29 18:23:44 -07002330 }
Eric Snow4e9da0d2018-02-02 21:49:49 -07002331 int64_t *cur = cids;
2332 for (int64_t i=0; i < count; cur++, i++) {
2333 PyObject *id = (PyObject *)newchannelid(&ChannelIDtype, *cur, 0,
Eric Snow6d2cd902018-05-16 15:04:57 -04002334 &_globals.channels, 0, 0);
Eric Snow7f8bfc92018-01-29 18:23:44 -07002335 if (id == NULL) {
2336 Py_DECREF(ids);
2337 ids = NULL;
2338 break;
2339 }
2340 PyList_SET_ITEM(ids, i, id);
2341 }
Eric Snow4e9da0d2018-02-02 21:49:49 -07002342
2343finally:
2344 PyMem_Free(cids);
Eric Snow7f8bfc92018-01-29 18:23:44 -07002345 return ids;
2346}
2347
2348PyDoc_STRVAR(channel_list_all_doc,
Eric Snow6d2cd902018-05-16 15:04:57 -04002349"channel_list_all() -> [cid]\n\
Eric Snow7f8bfc92018-01-29 18:23:44 -07002350\n\
2351Return the list of all IDs for active channels.");
2352
2353static PyObject *
Lewis Gaulf7bbf582020-04-29 01:18:42 +01002354channel_list_interpreters(PyObject *self, PyObject *args, PyObject *kwds)
2355{
2356 static char *kwlist[] = {"cid", "send", NULL};
2357 int64_t cid; /* Channel ID */
2358 int send = 0; /* Send or receive end? */
2359 int64_t id;
2360 PyObject *ids, *id_obj;
2361 PyInterpreterState *interp;
2362
2363 if (!PyArg_ParseTupleAndKeywords(
2364 args, kwds, "O&$p:channel_list_interpreters",
2365 kwlist, channel_id_converter, &cid, &send)) {
2366 return NULL;
2367 }
2368
2369 ids = PyList_New(0);
2370 if (ids == NULL) {
2371 goto except;
2372 }
2373
2374 interp = PyInterpreterState_Head();
2375 while (interp != NULL) {
2376 id = PyInterpreterState_GetID(interp);
2377 assert(id >= 0);
2378 int res = _channel_is_associated(&_globals.channels, cid, id, send);
2379 if (res < 0) {
2380 goto except;
2381 }
2382 if (res) {
2383 id_obj = _PyInterpreterState_GetIDObject(interp);
2384 if (id_obj == NULL) {
2385 goto except;
2386 }
2387 res = PyList_Insert(ids, 0, id_obj);
2388 Py_DECREF(id_obj);
2389 if (res < 0) {
2390 goto except;
2391 }
2392 }
2393 interp = PyInterpreterState_Next(interp);
2394 }
2395
2396 goto finally;
2397
2398except:
2399 Py_XDECREF(ids);
2400 ids = NULL;
2401
2402finally:
2403 return ids;
2404}
2405
2406PyDoc_STRVAR(channel_list_interpreters_doc,
2407"channel_list_interpreters(cid, *, send) -> [id]\n\
2408\n\
2409Return the list of all interpreter IDs associated with an end of the channel.\n\
2410\n\
2411The 'send' argument should be a boolean indicating whether to use the send or\n\
2412receive end.");
2413
2414
2415static PyObject *
Eric Snow6d2cd902018-05-16 15:04:57 -04002416channel_send(PyObject *self, PyObject *args, PyObject *kwds)
Eric Snow7f8bfc92018-01-29 18:23:44 -07002417{
Eric Snow6d2cd902018-05-16 15:04:57 -04002418 static char *kwlist[] = {"cid", "obj", NULL};
Serhiy Storchakabf169912019-09-13 22:50:27 +03002419 int64_t cid;
Eric Snow7f8bfc92018-01-29 18:23:44 -07002420 PyObject *obj;
Serhiy Storchakabf169912019-09-13 22:50:27 +03002421 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O&O:channel_send", kwlist,
2422 channel_id_converter, &cid, &obj)) {
Eric Snow7f8bfc92018-01-29 18:23:44 -07002423 return NULL;
2424 }
2425
2426 if (_channel_send(&_globals.channels, cid, obj) != 0) {
2427 return NULL;
2428 }
2429 Py_RETURN_NONE;
2430}
2431
2432PyDoc_STRVAR(channel_send_doc,
Eric Snow6d2cd902018-05-16 15:04:57 -04002433"channel_send(cid, obj)\n\
Eric Snow7f8bfc92018-01-29 18:23:44 -07002434\n\
2435Add the object's data to the channel's queue.");
2436
2437static PyObject *
Eric Snow6d2cd902018-05-16 15:04:57 -04002438channel_recv(PyObject *self, PyObject *args, PyObject *kwds)
Eric Snow7f8bfc92018-01-29 18:23:44 -07002439{
Eric Snow5e8c6912020-04-28 17:11:32 -06002440 static char *kwlist[] = {"cid", "default", NULL};
Serhiy Storchakabf169912019-09-13 22:50:27 +03002441 int64_t cid;
Eric Snow5e8c6912020-04-28 17:11:32 -06002442 PyObject *dflt = NULL;
2443 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O&|O:channel_recv", kwlist,
2444 channel_id_converter, &cid, &dflt)) {
Eric Snow7f8bfc92018-01-29 18:23:44 -07002445 return NULL;
2446 }
Eric Snow5e8c6912020-04-28 17:11:32 -06002447 Py_XINCREF(dflt);
Eric Snow7f8bfc92018-01-29 18:23:44 -07002448
Eric Snow5e8c6912020-04-28 17:11:32 -06002449 PyObject *obj = _channel_recv(&_globals.channels, cid);
2450 if (obj != NULL) {
2451 Py_XDECREF(dflt);
2452 return obj;
2453 } else if (PyErr_Occurred()) {
2454 Py_XDECREF(dflt);
2455 return NULL;
2456 } else if (dflt != NULL) {
2457 return dflt;
2458 } else {
2459 PyErr_Format(ChannelEmptyError, "channel %" PRId64 " is empty", cid);
2460 return NULL;
2461 }
Eric Snow7f8bfc92018-01-29 18:23:44 -07002462}
2463
2464PyDoc_STRVAR(channel_recv_doc,
Eric Snow5e8c6912020-04-28 17:11:32 -06002465"channel_recv(cid, [default]) -> obj\n\
Eric Snow7f8bfc92018-01-29 18:23:44 -07002466\n\
Eric Snow5e8c6912020-04-28 17:11:32 -06002467Return a new object from the data at the front of the channel's queue.\n\
2468\n\
2469If there is nothing to receive then raise ChannelEmptyError, unless\n\
2470a default value is provided. In that case return it.");
Eric Snow7f8bfc92018-01-29 18:23:44 -07002471
2472static PyObject *
Eric Snow6d2cd902018-05-16 15:04:57 -04002473channel_close(PyObject *self, PyObject *args, PyObject *kwds)
Eric Snow7f8bfc92018-01-29 18:23:44 -07002474{
Eric Snow6d2cd902018-05-16 15:04:57 -04002475 static char *kwlist[] = {"cid", "send", "recv", "force", NULL};
Serhiy Storchakabf169912019-09-13 22:50:27 +03002476 int64_t cid;
Eric Snow6d2cd902018-05-16 15:04:57 -04002477 int send = 0;
2478 int recv = 0;
2479 int force = 0;
2480 if (!PyArg_ParseTupleAndKeywords(args, kwds,
Serhiy Storchakabf169912019-09-13 22:50:27 +03002481 "O&|$ppp:channel_close", kwlist,
2482 channel_id_converter, &cid, &send, &recv, &force)) {
Eric Snow7f8bfc92018-01-29 18:23:44 -07002483 return NULL;
2484 }
Eric Snow6d2cd902018-05-16 15:04:57 -04002485
Eric Snow3ab01362018-05-17 10:27:09 -04002486 if (_channel_close(&_globals.channels, cid, send-recv, force) != 0) {
Eric Snow7f8bfc92018-01-29 18:23:44 -07002487 return NULL;
2488 }
2489 Py_RETURN_NONE;
2490}
2491
2492PyDoc_STRVAR(channel_close_doc,
Eric Snow6d2cd902018-05-16 15:04:57 -04002493"channel_close(cid, *, send=None, recv=None, force=False)\n\
Eric Snow7f8bfc92018-01-29 18:23:44 -07002494\n\
Eric Snow6d2cd902018-05-16 15:04:57 -04002495Close the channel for all interpreters.\n\
2496\n\
2497If the channel is empty then the keyword args are ignored and both\n\
2498ends are immediately closed. Otherwise, if 'force' is True then\n\
2499all queued items are released and both ends are immediately\n\
2500closed.\n\
2501\n\
2502If the channel is not empty *and* 'force' is False then following\n\
2503happens:\n\
2504\n\
2505 * recv is True (regardless of send):\n\
2506 - raise ChannelNotEmptyError\n\
2507 * recv is None and send is None:\n\
2508 - raise ChannelNotEmptyError\n\
2509 * send is True and recv is not True:\n\
2510 - fully close the 'send' end\n\
2511 - close the 'recv' end to interpreters not already receiving\n\
2512 - fully close it once empty\n\
2513\n\
2514Closing an already closed channel results in a ChannelClosedError.\n\
2515\n\
2516Once the channel's ID has no more ref counts in any interpreter\n\
2517the channel will be destroyed.");
Eric Snow7f8bfc92018-01-29 18:23:44 -07002518
2519static PyObject *
Eric Snow6d2cd902018-05-16 15:04:57 -04002520channel_release(PyObject *self, PyObject *args, PyObject *kwds)
Eric Snow7f8bfc92018-01-29 18:23:44 -07002521{
2522 // Note that only the current interpreter is affected.
Eric Snow6d2cd902018-05-16 15:04:57 -04002523 static char *kwlist[] = {"cid", "send", "recv", "force", NULL};
Serhiy Storchakabf169912019-09-13 22:50:27 +03002524 int64_t cid;
Eric Snow6d2cd902018-05-16 15:04:57 -04002525 int send = 0;
2526 int recv = 0;
2527 int force = 0;
Eric Snow7f8bfc92018-01-29 18:23:44 -07002528 if (!PyArg_ParseTupleAndKeywords(args, kwds,
Serhiy Storchakabf169912019-09-13 22:50:27 +03002529 "O&|$ppp:channel_release", kwlist,
2530 channel_id_converter, &cid, &send, &recv, &force)) {
Eric Snow7f8bfc92018-01-29 18:23:44 -07002531 return NULL;
2532 }
Eric Snow6d2cd902018-05-16 15:04:57 -04002533 if (send == 0 && recv == 0) {
Eric Snow7f8bfc92018-01-29 18:23:44 -07002534 send = 1;
2535 recv = 1;
2536 }
Eric Snow6d2cd902018-05-16 15:04:57 -04002537
2538 // XXX Handle force is True.
2539 // XXX Fix implicit release.
2540
Eric Snow7f8bfc92018-01-29 18:23:44 -07002541 if (_channel_drop(&_globals.channels, cid, send, recv) != 0) {
2542 return NULL;
2543 }
2544 Py_RETURN_NONE;
2545}
2546
Eric Snow6d2cd902018-05-16 15:04:57 -04002547PyDoc_STRVAR(channel_release_doc,
2548"channel_release(cid, *, send=None, recv=None, force=True)\n\
Eric Snow7f8bfc92018-01-29 18:23:44 -07002549\n\
2550Close the channel for the current interpreter. 'send' and 'recv'\n\
2551(bool) may be used to indicate the ends to close. By default both\n\
2552ends are closed. Closing an already closed end is a noop.");
2553
2554static PyObject *
2555channel__channel_id(PyObject *self, PyObject *args, PyObject *kwds)
2556{
2557 return channelid_new(&ChannelIDtype, args, kwds);
2558}
2559
2560static PyMethodDef module_functions[] = {
Victor Stinner252346a2020-05-01 11:33:44 +02002561 {"create", (PyCFunction)(void(*)(void))interp_create,
2562 METH_VARARGS | METH_KEYWORDS, create_doc},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02002563 {"destroy", (PyCFunction)(void(*)(void))interp_destroy,
Eric Snow6d2cd902018-05-16 15:04:57 -04002564 METH_VARARGS | METH_KEYWORDS, destroy_doc},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302565 {"list_all", interp_list_all,
Eric Snow7f8bfc92018-01-29 18:23:44 -07002566 METH_NOARGS, list_all_doc},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302567 {"get_current", interp_get_current,
Eric Snow7f8bfc92018-01-29 18:23:44 -07002568 METH_NOARGS, get_current_doc},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302569 {"get_main", interp_get_main,
Eric Snow7f8bfc92018-01-29 18:23:44 -07002570 METH_NOARGS, get_main_doc},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02002571 {"is_running", (PyCFunction)(void(*)(void))interp_is_running,
Eric Snow6d2cd902018-05-16 15:04:57 -04002572 METH_VARARGS | METH_KEYWORDS, is_running_doc},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02002573 {"run_string", (PyCFunction)(void(*)(void))interp_run_string,
Eric Snow6d2cd902018-05-16 15:04:57 -04002574 METH_VARARGS | METH_KEYWORDS, run_string_doc},
Eric Snow7f8bfc92018-01-29 18:23:44 -07002575
Serhiy Storchaka62be7422018-11-27 13:27:31 +02002576 {"is_shareable", (PyCFunction)(void(*)(void))object_is_shareable,
Eric Snow6d2cd902018-05-16 15:04:57 -04002577 METH_VARARGS | METH_KEYWORDS, is_shareable_doc},
Eric Snow7f8bfc92018-01-29 18:23:44 -07002578
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302579 {"channel_create", channel_create,
Eric Snow7f8bfc92018-01-29 18:23:44 -07002580 METH_NOARGS, channel_create_doc},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02002581 {"channel_destroy", (PyCFunction)(void(*)(void))channel_destroy,
Eric Snow6d2cd902018-05-16 15:04:57 -04002582 METH_VARARGS | METH_KEYWORDS, channel_destroy_doc},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302583 {"channel_list_all", channel_list_all,
Eric Snow7f8bfc92018-01-29 18:23:44 -07002584 METH_NOARGS, channel_list_all_doc},
Lewis Gaulf7bbf582020-04-29 01:18:42 +01002585 {"channel_list_interpreters", (PyCFunction)(void(*)(void))channel_list_interpreters,
2586 METH_VARARGS | METH_KEYWORDS, channel_list_interpreters_doc},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02002587 {"channel_send", (PyCFunction)(void(*)(void))channel_send,
Eric Snow6d2cd902018-05-16 15:04:57 -04002588 METH_VARARGS | METH_KEYWORDS, channel_send_doc},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02002589 {"channel_recv", (PyCFunction)(void(*)(void))channel_recv,
Eric Snow6d2cd902018-05-16 15:04:57 -04002590 METH_VARARGS | METH_KEYWORDS, channel_recv_doc},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02002591 {"channel_close", (PyCFunction)(void(*)(void))channel_close,
Eric Snow6d2cd902018-05-16 15:04:57 -04002592 METH_VARARGS | METH_KEYWORDS, channel_close_doc},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02002593 {"channel_release", (PyCFunction)(void(*)(void))channel_release,
Eric Snow6d2cd902018-05-16 15:04:57 -04002594 METH_VARARGS | METH_KEYWORDS, channel_release_doc},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02002595 {"_channel_id", (PyCFunction)(void(*)(void))channel__channel_id,
Eric Snow7f8bfc92018-01-29 18:23:44 -07002596 METH_VARARGS | METH_KEYWORDS, NULL},
2597
2598 {NULL, NULL} /* sentinel */
2599};
2600
2601
2602/* initialization function */
2603
2604PyDoc_STRVAR(module_doc,
2605"This module provides primitive operations to manage Python interpreters.\n\
2606The 'interpreters' module provides a more convenient interface.");
2607
2608static struct PyModuleDef interpretersmodule = {
2609 PyModuleDef_HEAD_INIT,
2610 "_xxsubinterpreters", /* m_name */
2611 module_doc, /* m_doc */
2612 -1, /* m_size */
2613 module_functions, /* m_methods */
2614 NULL, /* m_slots */
2615 NULL, /* m_traverse */
2616 NULL, /* m_clear */
2617 NULL /* m_free */
2618};
2619
2620
2621PyMODINIT_FUNC
2622PyInit__xxsubinterpreters(void)
2623{
2624 if (_init_globals() != 0) {
2625 return NULL;
2626 }
2627
2628 /* Initialize types */
Eric Snow7f8bfc92018-01-29 18:23:44 -07002629 if (PyType_Ready(&ChannelIDtype) != 0) {
2630 return NULL;
2631 }
2632
2633 /* Create the module */
2634 PyObject *module = PyModule_Create(&interpretersmodule);
2635 if (module == NULL) {
2636 return NULL;
2637 }
2638
2639 /* Add exception types */
2640 PyObject *ns = PyModule_GetDict(module); // borrowed
2641 if (interp_exceptions_init(ns) != 0) {
2642 return NULL;
2643 }
2644 if (channel_exceptions_init(ns) != 0) {
2645 return NULL;
2646 }
2647
2648 /* Add other types */
2649 Py_INCREF(&ChannelIDtype);
2650 if (PyDict_SetItemString(ns, "ChannelID", (PyObject *)&ChannelIDtype) != 0) {
2651 return NULL;
2652 }
Eric Snowc11183c2019-03-15 16:35:46 -06002653 Py_INCREF(&_PyInterpreterID_Type);
2654 if (PyDict_SetItemString(ns, "InterpreterID", (PyObject *)&_PyInterpreterID_Type) != 0) {
Eric Snow4c6955e2018-02-16 18:53:40 -07002655 return NULL;
2656 }
Eric Snow7f8bfc92018-01-29 18:23:44 -07002657
Eric Snowc11183c2019-03-15 16:35:46 -06002658 if (_PyCrossInterpreterData_RegisterClass(&ChannelIDtype, _channelid_shared)) {
Eric Snow7f8bfc92018-01-29 18:23:44 -07002659 return NULL;
2660 }
2661
2662 return module;
2663}