|
10 | 10 |
|
11 | 11 |
|
12 | 12 | #define CONTEXT_FREELIST_MAXLEN 255 |
13 | | -static PyContext *ctx_freelist = NULL; |
14 | | -static int ctx_freelist_len = 0; |
15 | 13 |
|
16 | 14 |
|
17 | 15 | #include "clinic/context.c.h" |
@@ -334,11 +332,13 @@ class _contextvars.Context "PyContext *" "&PyContext_Type" |
334 | 332 | static inline PyContext * |
335 | 333 | _context_alloc(void) |
336 | 334 | { |
| 335 | + PyInterpreterState *interp = _PyInterpreterState_GET(); |
| 336 | + struct _Py_context_state *state = &interp->context; |
337 | 337 | PyContext *ctx; |
338 | | - if (ctx_freelist_len) { |
339 | | - ctx_freelist_len--; |
340 | | - ctx = ctx_freelist; |
341 | | - ctx_freelist = (PyContext *)ctx->ctx_weakreflist; |
| 338 | + if (state->numfree) { |
| 339 | + state->numfree--; |
| 340 | + ctx = state->freelist; |
| 341 | + state->freelist = (PyContext *)ctx->ctx_weakreflist; |
342 | 342 | ctx->ctx_weakreflist = NULL; |
343 | 343 | _Py_NewReference((PyObject *)ctx); |
344 | 344 | } |
@@ -458,10 +458,12 @@ context_tp_dealloc(PyContext *self) |
458 | 458 | } |
459 | 459 | (void)context_tp_clear(self); |
460 | 460 |
|
461 | | - if (ctx_freelist_len < CONTEXT_FREELIST_MAXLEN) { |
462 | | - ctx_freelist_len++; |
463 | | - self->ctx_weakreflist = (PyObject *)ctx_freelist; |
464 | | - ctx_freelist = self; |
| 461 | + PyInterpreterState *interp = _PyInterpreterState_GET(); |
| 462 | + struct _Py_context_state *state = &interp->context; |
| 463 | + if (state->numfree < CONTEXT_FREELIST_MAXLEN) { |
| 464 | + state->numfree++; |
| 465 | + self->ctx_weakreflist = (PyObject *)state->freelist; |
| 466 | + state->freelist = self; |
465 | 467 | } |
466 | 468 | else { |
467 | 469 | Py_TYPE(self)->tp_free(self); |
@@ -1271,22 +1273,23 @@ get_token_missing(void) |
1271 | 1273 |
|
1272 | 1274 |
|
1273 | 1275 | void |
1274 | | -_PyContext_ClearFreeList(void) |
| 1276 | +_PyContext_ClearFreeList(PyThreadState *tstate) |
1275 | 1277 | { |
1276 | | - for (; ctx_freelist_len; ctx_freelist_len--) { |
1277 | | - PyContext *ctx = ctx_freelist; |
1278 | | - ctx_freelist = (PyContext *)ctx->ctx_weakreflist; |
| 1278 | + struct _Py_context_state *state = &tstate->interp->context; |
| 1279 | + for (; state->numfree; state->numfree--) { |
| 1280 | + PyContext *ctx = state->freelist; |
| 1281 | + state->freelist = (PyContext *)ctx->ctx_weakreflist; |
1279 | 1282 | ctx->ctx_weakreflist = NULL; |
1280 | 1283 | PyObject_GC_Del(ctx); |
1281 | 1284 | } |
1282 | 1285 | } |
1283 | 1286 |
|
1284 | 1287 |
|
1285 | 1288 | void |
1286 | | -_PyContext_Fini(void) |
| 1289 | +_PyContext_Fini(PyThreadState *tstate) |
1287 | 1290 | { |
1288 | 1291 | Py_CLEAR(_token_missing); |
1289 | | - _PyContext_ClearFreeList(); |
| 1292 | + _PyContext_ClearFreeList(tstate); |
1290 | 1293 | _PyHamt_Fini(); |
1291 | 1294 | } |
1292 | 1295 |
|
|
0 commit comments