Commit 9419609
committed
POC: Add compat mutex for free-threaded Python builds
In free-threaded Python (Py_GIL_DISABLED), the GIL no longer provides
mutual exclusion. Existing code that assumes gil_scoped_acquire provides
mutual exclusion would have data races.
This adds a global compatibility mutex that restores the safety guarantee:
- Acquired when gil_scoped_acquire is constructed (if not already held)
- Released when gil_scoped_release is constructed (if held)
- Ownership is tracked per-thread to handle the main thread case
KNOWN LIMITATION: The global mutex conflicts with per-interpreter GILs
(Py_MOD_PER_INTERPRETER_GIL_SUPPORTED). The "Per-Subinterpreter GIL" test
deadlocks with this change. Future work could use per-interpreter mutexes.
This is a proof-of-concept for discussion.1 parent 78381e5 commit 9419609
File tree
3 files changed
+123
-3
lines changed- include/pybind11
- tests/test_with_catch
3 files changed
+123
-3
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
97 | 97 | | |
98 | 98 | | |
99 | 99 | | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
100 | 107 | | |
101 | 108 | | |
102 | 109 | | |
| |||
141 | 148 | | |
142 | 149 | | |
143 | 150 | | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
144 | 157 | | |
145 | 158 | | |
146 | 159 | | |
| |||
151 | 164 | | |
152 | 165 | | |
153 | 166 | | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
154 | 170 | | |
155 | 171 | | |
156 | 172 | | |
| |||
162 | 178 | | |
163 | 179 | | |
164 | 180 | | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
165 | 188 | | |
166 | 189 | | |
167 | 190 | | |
| |||
186 | 209 | | |
187 | 210 | | |
188 | 211 | | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
189 | 218 | | |
190 | 219 | | |
191 | 220 | | |
| |||
196 | 225 | | |
197 | 226 | | |
198 | 227 | | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
199 | 231 | | |
200 | 232 | | |
201 | 233 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
10 | 13 | | |
11 | 14 | | |
12 | 15 | | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
13 | 58 | | |
14 | 59 | | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
15 | 63 | | |
16 | 64 | | |
17 | | - | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
18 | 74 | | |
19 | 75 | | |
20 | | - | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
21 | 85 | | |
22 | 86 | | |
23 | 87 | | |
24 | 88 | | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
25 | 92 | | |
26 | 93 | | |
27 | 94 | | |
28 | 95 | | |
29 | 96 | | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
30 | 104 | | |
31 | 105 | | |
32 | 106 | | |
33 | 107 | | |
34 | | - | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
35 | 117 | | |
36 | 118 | | |
37 | 119 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
310 | 310 | | |
311 | 311 | | |
312 | 312 | | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
313 | 319 | | |
314 | 320 | | |
315 | 321 | | |
| |||
0 commit comments