@@ -88,64 +88,46 @@ current_fast_clear(_PyRuntimeState *runtime)
88
88
// the thread state bound to the current OS thread
89
89
//------------------------------------------------
90
90
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 )
99
93
{
100
- return PyThread_tss_is_created (& runtime -> autoTSSkey );
94
+ return PyThread_tss_is_created (key );
101
95
}
102
96
103
- static PyStatus
104
- current_tss_init ( _PyRuntimeState * runtime )
97
+ static inline int
98
+ tstate_tss_init ( Py_tss_t * key )
105
99
{
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 );
111
102
}
112
103
113
- static void
114
- current_tss_fini ( _PyRuntimeState * runtime )
104
+ static inline void
105
+ tstate_tss_fini ( Py_tss_t * key )
115
106
{
116
- assert (current_tss_initialized ( runtime ));
117
- PyThread_tss_delete (& runtime -> autoTSSkey );
107
+ assert (tstate_tss_initialized ( key ));
108
+ PyThread_tss_delete (key );
118
109
}
119
110
120
111
static inline PyThreadState *
121
- current_tss_get ( _PyRuntimeState * runtime )
112
+ tstate_tss_get ( Py_tss_t * key )
122
113
{
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 );
125
116
}
126
117
127
118
static inline int
128
- _current_tss_set ( _PyRuntimeState * runtime , PyThreadState * tstate )
119
+ tstate_tss_set ( Py_tss_t * key , PyThreadState * tstate )
129
120
{
130
121
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 );
140
124
}
141
125
142
- static inline void
143
- current_tss_clear ( _PyRuntimeState * runtime )
126
+ static inline int
127
+ tstate_tss_clear ( Py_tss_t * key )
144
128
{
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 );
149
131
}
150
132
151
133
#ifdef HAVE_FORK
@@ -154,28 +136,67 @@ current_tss_clear(_PyRuntimeState *runtime)
154
136
* don't reset TSS upon fork(), see issue #10517.
155
137
*/
156
138
static PyStatus
157
- current_tss_reinit ( _PyRuntimeState * runtime )
139
+ tstate_tss_reinit ( Py_tss_t * key )
158
140
{
159
- if (!current_tss_initialized ( runtime )) {
141
+ if (!tstate_tss_initialized ( key )) {
160
142
return _PyStatus_OK ();
161
143
}
162
- PyThreadState * tstate = current_tss_get ( runtime );
144
+ PyThreadState * tstate = tstate_tss_get ( key );
163
145
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 ();
168
149
}
169
150
170
151
/* If the thread had an associated auto thread state, reassociate it with
171
152
* 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" );
174
155
}
175
156
return _PyStatus_OK ();
176
157
}
177
158
#endif
178
159
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
+
179
200
static inline int tstate_is_alive (PyThreadState * tstate );
180
201
181
202
static inline int
@@ -409,10 +430,9 @@ _PyRuntimeState_Init(_PyRuntimeState *runtime)
409
430
memcpy (runtime , & initial , sizeof (* runtime ));
410
431
}
411
432
412
- PyStatus status = current_tss_init (runtime );
413
- if (_PyStatus_EXCEPTION (status )) {
433
+ if (current_tss_init (runtime ) != 0 ) {
414
434
_PyRuntimeState_Fini (runtime );
415
- return status ;
435
+ return _PyStatus_NO_MEMORY () ;
416
436
}
417
437
418
438
if (PyThread_tss_create (& runtime -> trashTSSkey ) != 0 ) {
0 commit comments