|
| 1 | +/** |
| 2 | + * @file py_callback.c |
| 3 | + * @brief Erlang callback support and asyncio integration |
| 4 | + * @author Benoit Chesneau |
| 5 | + * @copyright 2026 Benoit Chesneau. Licensed under Apache License 2.0. |
| 6 | + * |
| 7 | + * @ingroup cb |
| 8 | + * |
| 9 | + * This module implements bidirectional calling between Python and Erlang, |
| 10 | + * enabling Python code to invoke Erlang functions and await their results. |
| 11 | + * |
| 12 | + * @par Features |
| 13 | + * |
| 14 | + * - **erlang module**: Python module providing `erlang.call()` and `erlang.func()` |
| 15 | + * - **Suspension/Resume**: Reentrant callbacks without blocking dirty schedulers |
| 16 | + * - **Asyncio support**: Background event loop for async Python operations |
| 17 | + * |
| 18 | + * @par Suspension Mechanism |
| 19 | + * |
| 20 | + * When Python calls `erlang.call('func', args)`: |
| 21 | + * |
| 22 | + * ``` |
| 23 | + * ┌────────────┐ ┌─────────────┐ ┌──────────────┐ |
| 24 | + * │ Python │ raises │ Executor │ returns │ Erlang │ |
| 25 | + * │ Code │ ──────> │ Catches │ ──────> │ Callback │ |
| 26 | + * └────────────┘ Suspend │ Exception │ suspend └──────────────┘ |
| 27 | + * └─────────────┘ │ │ |
| 28 | + * │ │ result |
| 29 | + * ┌─────────────┐ │ │ |
| 30 | + * │ Resume │ <──────────────┘ |
| 31 | + * │ Replay │ |
| 32 | + * └─────────────┘ |
| 33 | + * │ |
| 34 | + * v |
| 35 | + * ┌────────────┐ |
| 36 | + * │ Continue │ |
| 37 | + * │ Python │ |
| 38 | + * └────────────┘ |
| 39 | + * ``` |
| 40 | + * |
| 41 | + * @par Why Suspension? |
| 42 | + * |
| 43 | + * Without suspension, Python calling Erlang would block a dirty scheduler |
| 44 | + * while waiting for the Erlang callback to complete. With suspension: |
| 45 | + * |
| 46 | + * 1. Dirty scheduler is released immediately |
| 47 | + * 2. Erlang callback runs on normal scheduler |
| 48 | + * 3. Result is stored, Python is replayed on dirty scheduler |
| 49 | + * |
| 50 | + * @par The 'erlang' Python Module |
| 51 | + * |
| 52 | + * Provides two calling syntaxes: |
| 53 | + * |
| 54 | + * ```python |
| 55 | + * # Explicit call |
| 56 | + * result = erlang.call('my_function', arg1, arg2) |
| 57 | + * |
| 58 | + * # Attribute-style call (via __getattr__) |
| 59 | + * result = erlang.my_function(arg1, arg2) |
| 60 | + * ``` |
| 61 | + * |
| 62 | + * @par Thread Safety |
| 63 | + * |
| 64 | + * - Thread-local storage tracks current worker and suspended state |
| 65 | + * - Async event loop runs in dedicated thread |
| 66 | + * - Pending futures queue protected by mutex |
| 67 | + * |
| 68 | + * @note This file is included from py_nif.c (single compilation unit) |
| 69 | + */ |
| 70 | + |
1 | 71 | /* |
2 | 72 | * Copyright 2026 Benoit Chesneau |
3 | 73 | * |
|
14 | 84 | * limitations under the License. |
15 | 85 | */ |
16 | 86 |
|
17 | | -/** |
18 | | - * py_callback.c - Erlang callback module and asyncio support |
19 | | - * |
20 | | - * This module handles: |
21 | | - * - Erlang callback module for Python (erlang.call()) |
22 | | - * - Suspended state for reentrant callbacks |
23 | | - * - Resume callback NIFs |
24 | | - * - Asyncio event loop and futures |
25 | | - * - Sub-interpreter support |
26 | | - */ |
27 | | - |
28 | | -/* Note: This file is included from py_nif.c after py_nif.h, py_convert.c, py_exec.c */ |
29 | | - |
30 | 87 | /* ============================================================================ |
31 | 88 | * Suspended state management |
32 | 89 | * ============================================================================ */ |
|
0 commit comments