8000 gh-106078: Move static objects related to `CONTEXTVAR` to the decimal module global state by CharlieZhao95 · Pull Request #106395 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-106078: Move static objects related to CONTEXTVAR to the decimal module global state #106395

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Jul 8, 2023
Prev Previous commit
Next Next commit
Fix build error
Fix build error with flag `--without-decimal-contextvar`
  • Loading branch information
CharlieZhao95 committed Jul 4, 2023
commit 4e98c029360c29035039948cac6266b2861f1fa4
59 changes: 29 additions & 30 deletions Modules/_decimal/_decimal.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,31 +39,6 @@

#include "docstrings.h"

typedef struct {
PyTypeObject *PyDecContextManager_Type;
PyTypeObject *PyDecContext_Type;
PyTypeObject *PyDecSignalDictMixin_Type;
PyTypeObject *PyDec_Type;
PyTypeObject *PyDecSignalDict_Type;
PyTypeObject *DecimalTuple;

/* Top level Exception; inherits from ArithmeticError */
PyObject *DecimalException;

#ifndef WITH_DECIMAL_CONTEXTVAR
/* Key for thread state dictionary */
PyObject *tls_context_key;
/* Invariant: NULL or the most recently accessed thread local context */
PyDecContextObject *cached_context;
#else
PyObject *current_context_var;
#endif
} decimal_state;

static decimal_state global_state;

#define GLOBAL_STATE() (&global_state)

#if !defined(MPD_VERSION_HEX) || MPD_VERSION_HEX < 0x02050000
#error "libmpdec version >= 2.5.0 required"
#endif
Expand Down Expand Up @@ -120,6 +95,30 @@ typedef struct {
PyObject *global;
} PyDecContextManagerObject;

typedef struct {
PyTypeObject *PyDecContextManager_Type;
PyTypeObject *PyDecContext_Type;
PyTypeObject *PyDecSignalDictMixin_Type;
PyTypeObject *PyDec_Type;
PyTypeObject *PyDecSignalDict_Type;
PyTypeObject *DecimalTuple;

/* Top level Exception; inherits from ArithmeticError */
PyObject *DecimalException;

#ifndef WITH_DECIMAL_CONTEXTVAR
/* Key for thread state dictionary */
PyObject *tls_context_key;
/* Invariant: NULL or the most recently accessed thread local context */
PyDecContextObject *cached_context;
#else
PyObject *current_context_var;
#endif
} decimal_state;

static decimal_state global_state;

#define GLOBAL_STATE() (&global_state)

#undef MPD
#undef CTX
Expand Down Expand Up @@ -1564,8 +1563,8 @@ current_context_from_dict(void)
return NULL;
}

PyObject *tl_context = PyDict_GetItemWithError(dict,
state->tls_context_key);
PyObject *tl_context;
tl_context = PyDict_GetItemWithError(dict, modstate->tls_context_key);
if (tl_context != NULL) {
/* We already have a thread local context. */
CONTEXT_CHECK(modstate, tl_context);
Expand All @@ -1582,7 +1581,7 @@ current_context_from_dict(void)
}
CTX(tl_context)->status = 0;

if (PyDict_SetItem(dict, state->tls_context_key, tl_context) < 0) {
if (PyDict_SetItem(dict, modstate->tls_context_key, tl_context) < 0) {
Py_DECREF(tl_context);
return NULL;
}
Expand All @@ -1591,8 +1590,8 @@ current_context_from_dict(void)

/* Cache the context of the current thread, assuming that it
* will be accessed several times before a thread switch. */
state->cached_context = (PyDecContextObject *)tl_context;
state->cached_context->tstate = tstate;
modstate->cached_context = (PyDecContextObject *)tl_context;
modstate->cached_context->tstate = tstate;

/* Borrowed reference with refcount==1 */
return tl_context;
Expand Down
0