Skip to content

Commit e8d9d68

Browse files
Factor out tstate_tss_*().
1 parent 22c6fd0 commit e8d9d68

File tree

1 file changed

+71
-51
lines changed

1 file changed

+71
-51
lines changed

Python/pystate.c

Lines changed: 71 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -88,64 +88,46 @@ current_fast_clear(_PyRuntimeState *runtime)
8888
// the thread state bound to the current OS thread
8989
//------------------------------------------------
9090

91-
/*
92-
The stored thread state is set by bind_tstate() (AKA PyThreadState_Bind().
93-
94-
The GIL does no need to be held for these.
95-
*/
96-
97-
static int
98-
current_tss_initialized(_PyRuntimeState *runtime)
91+
static inline int
92+
tstate_tss_initialized(Py_tss_t *key)
9993
{
100-
return PyThread_tss_is_created(&runtime->autoTSSkey);
94+
return PyThread_tss_is_created(key);
10195
}
10296

103-
static PyStatus
104-
current_tss_init(_PyRuntimeState *runtime)
97+
static inline int
98+
tstate_tss_init(Py_tss_t *key)
10599
{
106-
assert(!current_tss_initialized(runtime));
107-
if (PyThread_tss_create(&runtime->autoTSSkey) != 0) {
108-
return _PyStatus_NO_MEMORY();
109-
}
110-
return _PyStatus_OK();
100+
assert(!tstate_tss_initialized(key));
101+
return PyThread_tss_create(key);
111102
}
112103

113-
static void
114-
current_tss_fini(_PyRuntimeState *runtime)
104+
static inline void
105+
tstate_tss_fini(Py_tss_t *key)
115106
{
116-
assert(current_tss_initialized(runtime));
117-
PyThread_tss_delete(&runtime->autoTSSkey);
107+
assert(tstate_tss_initialized(key));
108+
PyThread_tss_delete(key);
118109
}
119110

120111
static inline PyThreadState *
121-
current_tss_get(_PyRuntimeState *runtime)
112+
tstate_tss_get(Py_tss_t *key)
122113
{
123-
assert(current_tss_initialized(runtime));
124-
return (PyThreadState *)PyThread_tss_get(&runtime->autoTSSkey);
114+
assert(tstate_tss_initialized(key));
115+
return (PyThreadState *)PyThread_tss_get(key);
125116
}
126117

127118
static inline int
128-
_current_tss_set(_PyRuntimeState *runtime, PyThreadState *tstate)
119+
tstate_tss_set(Py_tss_t *key, PyThreadState *tstate)
129120
{
130121
assert(tstate != NULL);
131-
assert(current_tss_initialized(runtime));
132-
return PyThread_tss_set(&runtime->autoTSSkey, (void *)tstate);
133-
}
134-
static inline void
135-
current_tss_set(_PyRuntimeState *runtime, PyThreadState *tstate)
136-
{
137-
if (_current_tss_set(runtime, tstate) != 0) {
138-
Py_FatalError("failed to set current tstate (TSS)");
139-
}
122+
assert(tstate_tss_initialized(key));
123+
return PyThread_tss_set(key, (void *)tstate);
140124
}
141125

142-
static inline void
143-
current_tss_clear(_PyRuntimeState *runtime)
126+
static inline int
127+
tstate_tss_clear(Py_tss_t *key)
144128
{
145-
assert(current_tss_initialized(runtime));
146-
if (PyThread_tss_set(&runtime->autoTSSkey, NULL) != 0) {
147-
Py_FatalError("failed to clear current tstate (TSS)");
148-
}
129+
assert(tstate_tss_initialized(key));
130+
return PyThread_tss_set(key, (void *)NULL);
149131
}
150132

151133
#ifdef HAVE_FORK
@@ -154,28 +136,67 @@ current_tss_clear(_PyRuntimeState *runtime)
154136
* don't reset TSS upon fork(), see issue #10517.
155137
*/
156138
static PyStatus
157-
current_tss_reinit(_PyRuntimeState *runtime)
139+
tstate_tss_reinit(Py_tss_t *key)
158140
{
159-
if (!current_tss_initialized(runtime)) {
141+
if (!tstate_tss_initialized(key)) {
160142
return _PyStatus_OK();
161143
}
162-
PyThreadState *tstate = current_tss_get(runtime);
144+
PyThreadState *tstate = tstate_tss_get(key);
163145

164-
current_tss_fini(runtime);
165-
PyStatus status = current_tss_init(runtime);
166-
if (_PyStatus_EXCEPTION(status)) {
167-
return status;
146+
tstate_tss_fini(key);
147+
if (tstate_tss_init(key) != 0) {
148+
return _PyStatus_NO_MEMORY();
168149
}
169150

170151
/* If the thread had an associated auto thread state, reassociate it with
171152
* the new key. */
172-
if (tstate && _current_tss_set(runtime, tstate) != 0) {
173-
return _PyStatus_ERR("failed to set autoTSSkey");
153+
if (tstate && tstate_tss_set(key, tstate) != 0) {
154+
return _PyStatus_ERR("failed to re-set autoTSSkey");
174155
}
175156
return _PyStatus_OK();
176157
}
177158
#endif
178159

160+
161+
/*
162+
The stored thread state is set by bind_tstate() (AKA PyThreadState_Bind().
163+
164+
The GIL does no need to be held for these.
165+
*/
166+
167+
#define current_tss_initialized(runtime) \
168+
tstate_tss_initialized(&(runtime)->autoTSSkey)
169+
#define current_tss_init(runtime) \
170+
tstate_tss_init(&(runtime)->autoTSSkey)
171+
#define current_tss_fini(runtime) \
172+
tstate_tss_fini(&(runtime)->autoTSSkey)
173+
#define current_tss_get(runtime) \
174+
tstate_tss_get(&(runtime)->autoTSSkey)
175+
#define _current_tss_set(runtime, tstate) \
176+
tstate_tss_set(&(runtime)->autoTSSkey, tstate)
177+
#define _current_tss_clear(runtime) \
178+
tstate_tss_clear(&(runtime)->autoTSSkey)
179+
#define current_tss_reinit(runtime) \
180+
tstate_tss_reinit(&(runtime)->autoTSSkey)
181+
182+
static inline void
183+
current_tss_set(_PyRuntimeState *runtime, PyThreadState *tstate)
184+
{
185+
assert(tstate != NULL && tstate->interp->runtime == runtime);
186+
if (_current_tss_set(runtime, tstate) != 0) {
187+
Py_FatalError("failed to set current tstate (TSS)");
188+
}
189+
}
190+
191+
static inline void
192+
current_tss_clear(_PyRuntimeState *runtime)
193+
{
194+
if (_current_tss_clear(runtime) != 0) {
195+
Py_FatalError("failed to clear current tstate (TSS)");
196+
}
197+
}
198+
199+
179200
static inline int tstate_is_alive(PyThreadState *tstate);
180201

181202
static inline int
@@ -409,10 +430,9 @@ _PyRuntimeState_Init(_PyRuntimeState *runtime)
409430
memcpy(runtime, &initial, sizeof(*runtime));
410431
}
411432

412-
PyStatus status = current_tss_init(runtime);
413-
if (_PyStatus_EXCEPTION(status)) {
433+
if (current_tss_init(runtime) != 0) {
414434
_PyRuntimeState_Fini(runtime);
415-
return status;
435+
return _PyStatus_NO_MEMORY();
416436
}
417437

418438
if (PyThread_tss_create(&runtime->trashTSSkey) != 0) {

0 commit comments

Comments
 (0)