comparison Python/pystate.c @ 61465:e38335d4df91 2.6

Recorded merge of revisions 81029 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r81029 | antoine.pitrou | 2010-05-09 16:46:46 +0200 (dim., 09 mai 2010) | 3 lines Untabify C files. Will watch buildbots. ........
author Antoine Pitrou <solipsis@pitrou.net>
date Sun, 09 May 2010 15:15:40 +0000
parents 8e41f1794d44
children 787b969d37f0 f8f66ebcd5b1
comparison
equal deleted inserted replaced
61461:b6e03ca8a206 61465:e38335d4df91
56 56
57 57
58 PyInterpreterState * 58 PyInterpreterState *
59 PyInterpreterState_New(void) 59 PyInterpreterState_New(void)
60 { 60 {
61 PyInterpreterState *interp = (PyInterpreterState *) 61 PyInterpreterState *interp = (PyInterpreterState *)
62 malloc(sizeof(PyInterpreterState)); 62 malloc(sizeof(PyInterpreterState));
63 63
64 if (interp != NULL) { 64 if (interp != NULL) {
65 HEAD_INIT(); 65 HEAD_INIT();
66 #ifdef WITH_THREAD 66 #ifdef WITH_THREAD
67 if (head_mutex == NULL) 67 if (head_mutex == NULL)
68 Py_FatalError("Can't initialize threads for interpreter"); 68 Py_FatalError("Can't initialize threads for interpreter");
69 #endif 69 #endif
70 interp->modules = NULL; 70 interp->modules = NULL;
71 interp->modules_reloading = NULL; 71 interp->modules_reloading = NULL;
72 interp->sysdict = NULL; 72 interp->sysdict = NULL;
73 interp->builtins = NULL; 73 interp->builtins = NULL;
74 interp->tstate_head = NULL; 74 interp->tstate_head = NULL;
75 interp->codec_search_path = NULL; 75 interp->codec_search_path = NULL;
76 interp->codec_search_cache = NULL; 76 interp->codec_search_cache = NULL;
77 interp->codec_error_registry = NULL; 77 interp->codec_error_registry = NULL;
78 #ifdef HAVE_DLOPEN 78 #ifdef HAVE_DLOPEN
79 #ifdef RTLD_NOW 79 #ifdef RTLD_NOW
80 interp->dlopenflags = RTLD_NOW; 80 interp->dlopenflags = RTLD_NOW;
81 #else 81 #else
82 interp->dlopenflags = RTLD_LAZY; 82 interp->dlopenflags = RTLD_LAZY;
83 #endif 83 #endif
84 #endif 84 #endif
85 #ifdef WITH_TSC 85 #ifdef WITH_TSC
86 interp->tscdump = 0; 86 interp->tscdump = 0;
87 #endif 87 #endif
88 88
89 HEAD_LOCK(); 89 HEAD_LOCK();
90 interp->next = interp_head; 90 interp->next = interp_head;
91 interp_head = interp; 91 interp_head = interp;
92 HEAD_UNLOCK(); 92 HEAD_UNLOCK();
93 } 93 }
94 94
95 return interp; 95 return interp;
96 } 96 }
97 97
98 98
99 void 99 void
100 PyInterpreterState_Clear(PyInterpreterState *interp) 100 PyInterpreterState_Clear(PyInterpreterState *interp)
101 { 101 {
102 PyThreadState *p; 102 PyThreadState *p;
103 HEAD_LOCK(); 103 HEAD_LOCK();
104 for (p = interp->tstate_head; p != NULL; p = p->next) 104 for (p = interp->tstate_head; p != NULL; p = p->next)
105 PyThreadState_Clear(p); 105 PyThreadState_Clear(p);
106 HEAD_UNLOCK(); 106 HEAD_UNLOCK();
107 Py_CLEAR(interp->codec_search_path); 107 Py_CLEAR(interp->codec_search_path);
108 Py_CLEAR(interp->codec_search_cache); 108 Py_CLEAR(interp->codec_search_cache);
109 Py_CLEAR(interp->codec_error_registry); 109 Py_CLEAR(interp->codec_error_registry);
110 Py_CLEAR(interp->modules); 110 Py_CLEAR(interp->modules);
111 Py_CLEAR(interp->modules_reloading); 111 Py_CLEAR(interp->modules_reloading);
112 Py_CLEAR(interp->sysdict); 112 Py_CLEAR(interp->sysdict);
113 Py_CLEAR(interp->builtins); 113 Py_CLEAR(interp->builtins);
114 } 114 }
115 115
116 116
117 static void 117 static void
118 zapthreads(PyInterpreterState *interp) 118 zapthreads(PyInterpreterState *interp)
119 { 119 {
120 PyThreadState *p; 120 PyThreadState *p;
121 /* No need to lock the mutex here because this should only happen 121 /* No need to lock the mutex here because this should only happen
122 when the threads are all really dead (XXX famous last words). */ 122 when the threads are all really dead (XXX famous last words). */
123 while ((p = interp->tstate_head) != NULL) { 123 while ((p = interp->tstate_head) != NULL) {
124 PyThreadState_Delete(p); 124 PyThreadState_Delete(p);
125 } 125 }
126 } 126 }
127 127
128 128
129 void 129 void
130 PyInterpreterState_Delete(PyInterpreterState *interp) 130 PyInterpreterState_Delete(PyInterpreterState *interp)
131 { 131 {
132 PyInterpreterState **p; 132 PyInterpreterState **p;
133 zapthreads(interp); 133 zapthreads(interp);
134 HEAD_LOCK(); 134 HEAD_LOCK();
135 for (p = &interp_head; ; p = &(*p)->next) { 135 for (p = &interp_head; ; p = &(*p)->next) {
136 if (*p == NULL) 136 if (*p == NULL)
137 Py_FatalError( 137 Py_FatalError(
138 "PyInterpreterState_Delete: invalid interp"); 138 "PyInterpreterState_Delete: invalid interp");
139 if (*p == interp) 139 if (*p == interp)
140 break; 140 break;
141 } 141 }
142 if (interp->tstate_head != NULL) 142 if (interp->tstate_head != NULL)
143 Py_FatalError("PyInterpreterState_Delete: remaining threads"); 143 Py_FatalError("PyInterpreterState_Delete: remaining threads");
144 *p = interp->next; 144 *p = interp->next;
145 HEAD_UNLOCK(); 145 HEAD_UNLOCK();
146 free(interp); 146 free(interp);
147 } 147 }
148 148
149 149
150 /* Default implementation for _PyThreadState_GetFrame */ 150 /* Default implementation for _PyThreadState_GetFrame */
151 static struct _frame * 151 static struct _frame *
152 threadstate_getframe(PyThreadState *self) 152 threadstate_getframe(PyThreadState *self)
153 { 153 {
154 return self->frame; 154 return self->frame;
155 } 155 }
156 156
157 static PyThreadState * 157 static PyThreadState *
158 new_threadstate(PyInterpreterState *interp, int init) 158 new_threadstate(PyInterpreterState *interp, int init)
159 { 159 {
160 PyThreadState *tstate = (PyThreadState *)malloc(sizeof(PyThreadState)); 160 PyThreadState *tstate = (PyThreadState *)malloc(sizeof(PyThreadState));
161 161
162 if (_PyThreadState_GetFrame == NULL) 162 if (_PyThreadState_GetFrame == NULL)
163 _PyThreadState_GetFrame = threadstate_getframe; 163 _PyThreadState_GetFrame = threadstate_getframe;
164 164
165 if (tstate != NULL) { 165 if (tstate != NULL) {
166 tstate->interp = interp; 166 tstate->interp = interp;
167 167
168 tstate->frame = NULL; 168 tstate->frame = NULL;
169 tstate->recursion_depth = 0; 169 tstate->recursion_depth = 0;
170 tstate->tracing = 0; 170 tstate->tracing = 0;
171 tstate->use_tracing = 0; 171 tstate->use_tracing = 0;
172 tstate->tick_counter = 0; 172 tstate->tick_counter = 0;
173 tstate->gilstate_counter = 0; 173 tstate->gilstate_counter = 0;
174 tstate->async_exc = NULL; 174 tstate->async_exc = NULL;
175 #ifdef WITH_THREAD 175 #ifdef WITH_THREAD
176 tstate->thread_id = PyThread_get_thread_ident(); 176 tstate->thread_id = PyThread_get_thread_ident();
177 #else 177 #else
178 tstate->thread_id = 0; 178 tstate->thread_id = 0;
179 #endif 179 #endif
180 180
181 tstate->dict = NULL; 181 tstate->dict = NULL;
182 182
183 tstate->curexc_type = NULL; 183 tstate->curexc_type = NULL;
184 tstate->curexc_value = NULL; 184 tstate->curexc_value = NULL;
185 tstate->curexc_traceback = NULL; 185 tstate->curexc_traceback = NULL;
186 186
187 tstate->exc_type = NULL; 187 tstate->exc_type = NULL;
188 tstate->exc_value = NULL; 188 tstate->exc_value = NULL;
189 tstate->exc_traceback = NULL; 189 tstate->exc_traceback = NULL;
190 190
191 tstate->c_profilefunc = NULL; 191 tstate->c_profilefunc = NULL;
192 tstate->c_tracefunc = NULL; 192 tstate->c_tracefunc = NULL;
193 tstate->c_profileobj = NULL; 193 tstate->c_profileobj = NULL;
194 tstate->c_traceobj = NULL; 194 tstate->c_traceobj = NULL;
195 195
196 if (init) 196 if (init)
197 _PyThreadState_Init(tstate); 197 _PyThreadState_Init(tstate);
198 198
199 HEAD_LOCK(); 199 HEAD_LOCK();
200 tstate->next = interp->tstate_head; 200 tstate->next = interp->tstate_head;
201 interp->tstate_head = tstate; 201 interp->tstate_head = tstate;
202 HEAD_UNLOCK(); 202 HEAD_UNLOCK();
203 } 203 }
204 204
205 return tstate; 205 return tstate;
206 } 206 }
207 207
208 PyThreadState * 208 PyThreadState *
209 PyThreadState_New(PyInterpreterState *interp) 209 PyThreadState_New(PyInterpreterState *interp)
210 { 210 {
211 return new_threadstate(interp, 1); 211 return new_threadstate(interp, 1);
212 } 212 }
213 213
214 PyThreadState * 214 PyThreadState *
215 _PyThreadState_Prealloc(PyInterpreterState *interp) 215 _PyThreadState_Prealloc(PyInterpreterState *interp)
216 { 216 {
217 return new_threadstate(interp, 0); 217 return new_threadstate(interp, 0);
218 } 218 }
219 219
220 void 220 void
221 _PyThreadState_Init(PyThreadState *tstate) 221 _PyThreadState_Init(PyThreadState *tstate)
222 { 222 {
223 #ifdef WITH_THREAD 223 #ifdef WITH_THREAD
224 _PyGILState_NoteThreadState(tstate); 224 _PyGILState_NoteThreadState(tstate);
225 #endif 225 #endif
226 } 226 }
227 227
228 void 228 void
229 PyThreadState_Clear(PyThreadState *tstate) 229 PyThreadState_Clear(PyThreadState *tstate)
230 { 230 {
231 if (Py_VerboseFlag && tstate->frame != NULL) 231 if (Py_VerboseFlag && tstate->frame != NULL)
232 fprintf(stderr, 232 fprintf(stderr,
233 "PyThreadState_Clear: warning: thread still has a frame\n"); 233 "PyThreadState_Clear: warning: thread still has a frame\n");
234 234
235 Py_CLEAR(tstate->frame); 235 Py_CLEAR(tstate->frame);
236 236
237 Py_CLEAR(tstate->dict); 237 Py_CLEAR(tstate->dict);
238 Py_CLEAR(tstate->async_exc); 238 Py_CLEAR(tstate->async_exc);
239 239
240 Py_CLEAR(tstate->curexc_type); 240 Py_CLEAR(tstate->curexc_type);
241 Py_CLEAR(tstate->curexc_value); 241 Py_CLEAR(tstate->curexc_value);
242 Py_CLEAR(tstate->curexc_traceback); 242 Py_CLEAR(tstate->curexc_traceback);
243 243
244 Py_CLEAR(tstate->exc_type); 244 Py_CLEAR(tstate->exc_type);
245 Py_CLEAR(tstate->exc_value); 245 Py_CLEAR(tstate->exc_value);
246 Py_CLEAR(tstate->exc_traceback); 246 Py_CLEAR(tstate->exc_traceback);
247 247
248 tstate->c_profilefunc = NULL; 248 tstate->c_profilefunc = NULL;
249 tstate->c_tracefunc = NULL; 249 tstate->c_tracefunc = NULL;
250 Py_CLEAR(tstate->c_profileobj); 250 Py_CLEAR(tstate->c_profileobj);
251 Py_CLEAR(tstate->c_traceobj); 251 Py_CLEAR(tstate->c_traceobj);
252 } 252 }
253 253
254 254
255 /* Common code for PyThreadState_Delete() and PyThreadState_DeleteCurrent() */ 255 /* Common code for PyThreadState_Delete() and PyThreadState_DeleteCurrent() */
256 static void 256 static void
257 tstate_delete_common(PyThreadState *tstate) 257 tstate_delete_common(PyThreadState *tstate)
258 { 258 {
259 PyInterpreterState *interp; 259 PyInterpreterState *interp;
260 PyThreadState **p; 260 PyThreadState **p;
261 PyThreadState *prev_p = NULL; 261 PyThreadState *prev_p = NULL;
262 if (tstate == NULL) 262 if (tstate == NULL)
263 Py_FatalError("PyThreadState_Delete: NULL tstate"); 263 Py_FatalError("PyThreadState_Delete: NULL tstate");
264 interp = tstate->interp; 264 interp = tstate->interp;
265 if (interp == NULL) 265 if (interp == NULL)
266 Py_FatalError("PyThreadState_Delete: NULL interp"); 266 Py_FatalError("PyThreadState_Delete: NULL interp");
267 HEAD_LOCK(); 267 HEAD_LOCK();
268 for (p = &interp->tstate_head; ; p = &(*p)->next) { 268 for (p = &interp->tstate_head; ; p = &(*p)->next) {
269 if (*p == NULL) 269 if (*p == NULL)
270 Py_FatalError( 270 Py_FatalError(
271 "PyThreadState_Delete: invalid tstate"); 271 "PyThreadState_Delete: invalid tstate");
272 if (*p == tstate) 272 if (*p == tstate)
273 break; 273 break;
274 /* Sanity check. These states should never happen but if 274 /* Sanity check. These states should never happen but if
275 * they do we must abort. Otherwise we'll end up spinning in 275 * they do we must abort. Otherwise we'll end up spinning in
276 * in a tight loop with the lock held. A similar check is done 276 * in a tight loop with the lock held. A similar check is done
277 * in thread.c find_key(). */ 277 * in thread.c find_key(). */
278 if (*p == prev_p) 278 if (*p == prev_p)
279 Py_FatalError( 279 Py_FatalError(
280 "PyThreadState_Delete: small circular list(!)" 280 "PyThreadState_Delete: small circular list(!)"
281 " and tstate not found."); 281 " and tstate not found.");
282 prev_p = *p; 282 prev_p = *p;
283 if ((*p)->next == interp->tstate_head) 283 if ((*p)->next == interp->tstate_head)
284 Py_FatalError( 284 Py_FatalError(
285 "PyThreadState_Delete: circular list(!) and" 285 "PyThreadState_Delete: circular list(!) and"
286 " tstate not found."); 286 " tstate not found.");
287 } 287 }
288 *p = tstate->next; 288 *p = tstate->next;
289 HEAD_UNLOCK(); 289 HEAD_UNLOCK();
290 free(tstate); 290 free(tstate);
291 } 291 }
292 292
293 293
294 void 294 void
295 PyThreadState_Delete(PyThreadState *tstate) 295 PyThreadState_Delete(PyThreadState *tstate)
296 { 296 {
297 if (tstate == _PyThreadState_Current) 297 if (tstate == _PyThreadState_Current)
298 Py_FatalError("PyThreadState_Delete: tstate is still current"); 298 Py_FatalError("PyThreadState_Delete: tstate is still current");
299 tstate_delete_common(tstate); 299 tstate_delete_common(tstate);
300 #ifdef WITH_THREAD 300 #ifdef WITH_THREAD
301 if (autoTLSkey && PyThread_get_key_value(autoTLSkey) == tstate) 301 if (autoTLSkey && PyThread_get_key_value(autoTLSkey) == tstate)
302 PyThread_delete_key_value(autoTLSkey); 302 PyThread_delete_key_value(autoTLSkey);
303 #endif /* WITH_THREAD */ 303 #endif /* WITH_THREAD */
304 } 304 }
305 305
306 306
307 #ifdef WITH_THREAD 307 #ifdef WITH_THREAD
308 void 308 void
309 PyThreadState_DeleteCurrent() 309 PyThreadState_DeleteCurrent()
310 { 310 {
311 PyThreadState *tstate = _PyThreadState_Current; 311 PyThreadState *tstate = _PyThreadState_Current;
312 if (tstate == NULL) 312 if (tstate == NULL)
313 Py_FatalError( 313 Py_FatalError(
314 "PyThreadState_DeleteCurrent: no current tstate"); 314 "PyThreadState_DeleteCurrent: no current tstate");
315 _PyThreadState_Current = NULL; 315 _PyThreadState_Current = NULL;
316 tstate_delete_common(tstate); 316 tstate_delete_common(tstate);
317 if (autoTLSkey && PyThread_get_key_value(autoTLSkey) == tstate) 317 if (autoTLSkey && PyThread_get_key_value(autoTLSkey) == tstate)
318 PyThread_delete_key_value(autoTLSkey); 318 PyThread_delete_key_value(autoTLSkey);
319 PyEval_ReleaseLock(); 319 PyEval_ReleaseLock();
320 } 320 }
321 #endif /* WITH_THREAD */ 321 #endif /* WITH_THREAD */
322 322
323 323
324 PyThreadState * 324 PyThreadState *
325 PyThreadState_Get(void) 325 PyThreadState_Get(void)
326 { 326 {
327 if (_PyThreadState_Current == NULL) 327 if (_PyThreadState_Current == NULL)
328 Py_FatalError("PyThreadState_Get: no current thread"); 328 Py_FatalError("PyThreadState_Get: no current thread");
329 329
330 return _PyThreadState_Current; 330 return _PyThreadState_Current;
331 } 331 }
332 332
333 333
334 PyThreadState * 334 PyThreadState *
335 PyThreadState_Swap(PyThreadState *newts) 335 PyThreadState_Swap(PyThreadState *newts)
336 { 336 {
337 PyThreadState *oldts = _PyThreadState_Current; 337 PyThreadState *oldts = _PyThreadState_Current;
338 338
339 _PyThreadState_Current = newts; 339 _PyThreadState_Current = newts;
340 /* It should not be possible for more than one thread state 340 /* It should not be possible for more than one thread state
341 to be used for a thread. Check this the best we can in debug 341 to be used for a thread. Check this the best we can in debug
342 builds. 342 builds.
343 */ 343 */
344 #if defined(Py_DEBUG) && defined(WITH_THREAD) 344 #if defined(Py_DEBUG) && defined(WITH_THREAD)
345 if (newts) { 345 if (newts) {
346 /* This can be called from PyEval_RestoreThread(). Similar 346 /* This can be called from PyEval_RestoreThread(). Similar
347 to it, we need to ensure errno doesn't change. 347 to it, we need to ensure errno doesn't change.
348 */ 348 */
349 int err = errno; 349 int err = errno;
350 PyThreadState *check = PyGILState_GetThisThreadState(); 350 PyThreadState *check = PyGILState_GetThisThreadState();
351 if (check && check->interp == newts->interp && check != newts) 351 if (check && check->interp == newts->interp && check != newts)
352 Py_FatalError("Invalid thread state for this thread"); 352 Py_FatalError("Invalid thread state for this thread");
353 errno = err; 353 errno = err;
354 } 354 }
355 #endif 355 #endif
356 return oldts; 356 return oldts;
357 } 357 }
358 358
359 /* An extension mechanism to store arbitrary additional per-thread state. 359 /* An extension mechanism to store arbitrary additional per-thread state.
360 PyThreadState_GetDict() returns a dictionary that can be used to hold such 360 PyThreadState_GetDict() returns a dictionary that can be used to hold such
361 state; the caller should pick a unique key and store its state there. If 361 state; the caller should pick a unique key and store its state there. If
363 and the caller should assume no per-thread state is available. */ 363 and the caller should assume no per-thread state is available. */
364 364
365 PyObject * 365 PyObject *
366 PyThreadState_GetDict(void) 366 PyThreadState_GetDict(void)
367 { 367 {
368 if (_PyThreadState_Current == NULL) 368 if (_PyThreadState_Current == NULL)
369 return NULL; 369 return NULL;
370 370
371 if (_PyThreadState_Current->dict == NULL) { 371 if (_PyThreadState_Current->dict == NULL) {
372 PyObject *d; 372 PyObject *d;
373 _PyThreadState_Current->dict = d = PyDict_New(); 373 _PyThreadState_Current->dict = d = PyDict_New();
374 if (d == NULL) 374 if (d == NULL)
375 PyErr_Clear(); 375 PyErr_Clear();
376 } 376 }
377 return _PyThreadState_Current->dict; 377 return _PyThreadState_Current->dict;
378 } 378 }
379 379
380 380
381 /* Asynchronously raise an exception in a thread. 381 /* Asynchronously raise an exception in a thread.
382 Requested by Just van Rossum and Alex Martelli. 382 Requested by Just van Rossum and Alex Martelli.
386 match any known thread id). Can be called with exc=NULL to clear an 386 match any known thread id). Can be called with exc=NULL to clear an
387 existing async exception. This raises no exceptions. */ 387 existing async exception. This raises no exceptions. */
388 388
389 int 389 int
390 PyThreadState_SetAsyncExc(long id, PyObject *exc) { 390 PyThreadState_SetAsyncExc(long id, PyObject *exc) {
391 PyThreadState *tstate = PyThreadState_GET(); 391 PyThreadState *tstate = PyThreadState_GET();
392 PyInterpreterState *interp = tstate->interp; 392 PyInterpreterState *interp = tstate->interp;
393 PyThreadState *p; 393 PyThreadState *p;
394 394
395 /* Although the GIL is held, a few C API functions can be called 395 /* Although the GIL is held, a few C API functions can be called
396 * without the GIL held, and in particular some that create and 396 * without the GIL held, and in particular some that create and
397 * destroy thread and interpreter states. Those can mutate the 397 * destroy thread and interpreter states. Those can mutate the
398 * list of thread states we're traversing, so to prevent that we lock 398 * list of thread states we're traversing, so to prevent that we lock
399 * head_mutex for the duration. 399 * head_mutex for the duration.
400 */ 400 */
401 HEAD_LOCK(); 401 HEAD_LOCK();
402 for (p = interp->tstate_head; p != NULL; p = p->next) { 402 for (p = interp->tstate_head; p != NULL; p = p->next) {
403 if (p->thread_id == id) { 403 if (p->thread_id == id) {
404 /* Tricky: we need to decref the current value 404 /* Tricky: we need to decref the current value
405 * (if any) in p->async_exc, but that can in turn 405 * (if any) in p->async_exc, but that can in turn
406 * allow arbitrary Python code to run, including 406 * allow arbitrary Python code to run, including
407 * perhaps calls to this function. To prevent 407 * perhaps calls to this function. To prevent
408 * deadlock, we need to release head_mutex before 408 * deadlock, we need to release head_mutex before
409 * the decref. 409 * the decref.
410 */ 410 */
411 PyObject *old_exc = p->async_exc; 411 PyObject *old_exc = p->async_exc;
412 Py_XINCREF(exc); 412 Py_XINCREF(exc);
413 p->async_exc = exc; 413 p->async_exc = exc;
414 HEAD_UNLOCK(); 414 HEAD_UNLOCK();
415 Py_XDECREF(old_exc); 415 Py_XDECREF(old_exc);
416 return 1; 416 return 1;
417 } 417 }
418 } 418 }
419 HEAD_UNLOCK(); 419 HEAD_UNLOCK();
420 return 0; 420 return 0;
421 } 421 }
422 422
423 423
424 /* Routines for advanced debuggers, requested by David Beazley. 424 /* Routines for advanced debuggers, requested by David Beazley.
425 Don't use unless you know what you are doing! */ 425 Don't use unless you know what you are doing! */
426 426
427 PyInterpreterState * 427 PyInterpreterState *
428 PyInterpreterState_Head(void) 428 PyInterpreterState_Head(void)
429 { 429 {
430 return interp_head; 430 return interp_head;
431 } 431 }
432 432
433 PyInterpreterState * 433 PyInterpreterState *
434 PyInterpreterState_Next(PyInterpreterState *interp) { 434 PyInterpreterState_Next(PyInterpreterState *interp) {
435 return interp->next; 435 return interp->next;
436 } 436 }
437 437
438 PyThreadState * 438 PyThreadState *
439 PyInterpreterState_ThreadHead(PyInterpreterState *interp) { 439 PyInterpreterState_ThreadHead(PyInterpreterState *interp) {
440 return interp->tstate_head; 440 return interp->tstate_head;
441 } 441 }
442 442
443 PyThreadState * 443 PyThreadState *
444 PyThreadState_Next(PyThreadState *tstate) { 444 PyThreadState_Next(PyThreadState *tstate) {
445 return tstate->next; 445 return tstate->next;
446 } 446 }
447 447
448 /* The implementation of sys._current_frames(). This is intended to be 448 /* The implementation of sys._current_frames(). This is intended to be
449 called with the GIL held, as it will be when called via 449 called with the GIL held, as it will be when called via
450 sys._current_frames(). It's possible it would work fine even without 450 sys._current_frames(). It's possible it would work fine even without
451 the GIL held, but haven't thought enough about that. 451 the GIL held, but haven't thought enough about that.
452 */ 452 */
453 PyObject * 453 PyObject *
454 _PyThread_CurrentFrames(void) 454 _PyThread_CurrentFrames(void)
455 { 455 {
456 PyObject *result; 456 PyObject *result;
457 PyInterpreterState *i; 457 PyInterpreterState *i;
458 458
459 result = PyDict_New(); 459 result = PyDict_New();
460 if (result == NULL) 460 if (result == NULL)
461 return NULL; 461 return NULL;
462 462
463 /* for i in all interpreters: 463 /* for i in all interpreters:
464 * for t in all of i's thread states: 464 * for t in all of i's thread states:
465 * if t's frame isn't NULL, map t's id to its frame 465 * if t's frame isn't NULL, map t's id to its frame
466 * Because these lists can mutute even when the GIL is held, we 466 * Because these lists can mutute even when the GIL is held, we
467 * need to grab head_mutex for the duration. 467 * need to grab head_mutex for the duration.
468 */ 468 */
469 HEAD_LOCK(); 469 HEAD_LOCK();
470 for (i = interp_head; i != NULL; i = i->next) { 470 for (i = interp_head; i != NULL; i = i->next) {
471 PyThreadState *t; 471 PyThreadState *t;
472 for (t = i->tstate_head; t != NULL; t = t->next) { 472 for (t = i->tstate_head; t != NULL; t = t->next) {
473 PyObject *id; 473 PyObject *id;
474 int stat; 474 int stat;
475 struct _frame *frame = t->frame; 475 struct _frame *frame = t->frame;
476 if (frame == NULL) 476 if (frame == NULL)
477 continue; 477 continue;
478 id = PyInt_FromLong(t->thread_id); 478 id = PyInt_FromLong(t->thread_id);
479 if (id == NULL) 479 if (id == NULL)
480 goto Fail; 480 goto Fail;
481 stat = PyDict_SetItem(result, id, (PyObject *)frame); 481 stat = PyDict_SetItem(result, id, (PyObject *)frame);
482 Py_DECREF(id); 482 Py_DECREF(id);
483 if (stat < 0) 483 if (stat < 0)
484 goto Fail; 484 goto Fail;
485 } 485 }
486 } 486 }
487 HEAD_UNLOCK(); 487 HEAD_UNLOCK();
488 return result; 488 return result;
489 489
490 Fail: 490 Fail:
491 HEAD_UNLOCK(); 491 HEAD_UNLOCK();
492 Py_DECREF(result); 492 Py_DECREF(result);
493 return NULL; 493 return NULL;
494 } 494 }
495 495
496 /* Python "auto thread state" API. */ 496 /* Python "auto thread state" API. */
497 #ifdef WITH_THREAD 497 #ifdef WITH_THREAD
498 498
505 change until we yield the lock. 505 change until we yield the lock.
506 */ 506 */
507 static int 507 static int
508 PyThreadState_IsCurrent(PyThreadState *tstate) 508 PyThreadState_IsCurrent(PyThreadState *tstate)
509 { 509 {
510 /* Must be the tstate for this thread */ 510 /* Must be the tstate for this thread */
511 assert(PyGILState_GetThisThreadState()==tstate); 511 assert(PyGILState_GetThisThreadState()==tstate);
512 /* On Windows at least, simple reads and writes to 32 bit values 512 /* On Windows at least, simple reads and writes to 32 bit values
513 are atomic. 513 are atomic.
514 */ 514 */
515 return tstate == _PyThreadState_Current; 515 return tstate == _PyThreadState_Current;
516 } 516 }
517 517
518 /* Internal initialization/finalization functions called by 518 /* Internal initialization/finalization functions called by
519 Py_Initialize/Py_Finalize 519 Py_Initialize/Py_Finalize
520 */ 520 */
521 void 521 void
522 _PyGILState_Init(PyInterpreterState *i, PyThreadState *t) 522 _PyGILState_Init(PyInterpreterState *i, PyThreadState *t)
523 { 523 {
524 assert(i && t); /* must init with valid states */ 524 assert(i && t); /* must init with valid states */
525 autoTLSkey = PyThread_create_key(); 525 autoTLSkey = PyThread_create_key();
526 autoInterpreterState = i; 526 autoInterpreterState = i;
527 assert(PyThread_get_key_value(autoTLSkey) == NULL); 527 assert(PyThread_get_key_value(autoTLSkey) == NULL);
528 assert(t->gilstate_counter == 0); 528 assert(t->gilstate_counter == 0);
529 529
530 _PyGILState_NoteThreadState(t); 530 _PyGILState_NoteThreadState(t);
531 } 531 }
532 532
533 void 533 void
534 _PyGILState_Fini(void) 534 _PyGILState_Fini(void)
535 { 535 {
536 PyThread_delete_key(autoTLSkey); 536 PyThread_delete_key(autoTLSkey);
537 autoTLSkey = 0; 537 autoTLSkey = 0;
538 autoInterpreterState = NULL; 538 autoInterpreterState = NULL;
539 } 539 }
540 540
541 /* When a thread state is created for a thread by some mechanism other than 541 /* When a thread state is created for a thread by some mechanism other than
542 PyGILState_Ensure, it's important that the GILState machinery knows about 542 PyGILState_Ensure, it's important that the GILState machinery knows about
543 it so it doesn't try to create another thread state for the thread (this is 543 it so it doesn't try to create another thread state for the thread (this is
544 a better fix for SF bug #1010677 than the first one attempted). 544 a better fix for SF bug #1010677 than the first one attempted).
545 */ 545 */
546 static void 546 static void
547 _PyGILState_NoteThreadState(PyThreadState* tstate) 547 _PyGILState_NoteThreadState(PyThreadState* tstate)
548 { 548 {
549 /* If autoTLSkey is 0, this must be the very first threadstate created 549 /* If autoTLSkey is 0, this must be the very first threadstate created
550 in Py_Initialize(). Don't do anything for now (we'll be back here 550 in Py_Initialize(). Don't do anything for now (we'll be back here
551 when _PyGILState_Init is called). */ 551 when _PyGILState_Init is called). */
552 if (!autoTLSkey) 552 if (!autoTLSkey)
553 return; 553 return;
554 554
555 /* Stick the thread state for this thread in thread local storage. 555 /* Stick the thread state for this thread in thread local storage.
556 556
557 The only situation where you can legitimately have more than one 557 The only situation where you can legitimately have more than one
558 thread state for an OS level thread is when there are multiple 558 thread state for an OS level thread is when there are multiple
559 interpreters, when: 559 interpreters, when:
560 560
561 a) You shouldn't really be using the PyGILState_ APIs anyway, 561 a) You shouldn't really be using the PyGILState_ APIs anyway,
562 and: 562 and:
563 563
564 b) The slightly odd way PyThread_set_key_value works (see 564 b) The slightly odd way PyThread_set_key_value works (see
565 comments by its implementation) means that the first thread 565 comments by its implementation) means that the first thread
566 state created for that given OS level thread will "win", 566 state created for that given OS level thread will "win",
567 which seems reasonable behaviour. 567 which seems reasonable behaviour.
568 */ 568 */
569 if (PyThread_set_key_value(autoTLSkey, (void *)tstate) < 0) 569 if (PyThread_set_key_value(autoTLSkey, (void *)tstate) < 0)
570 Py_FatalError("Couldn't create autoTLSkey mapping"); 570 Py_FatalError("Couldn't create autoTLSkey mapping");
571 571
572 /* PyGILState_Release must not try to delete this thread state. */ 572 /* PyGILState_Release must not try to delete this thread state. */
573 tstate->gilstate_counter = 1; 573 tstate->gilstate_counter = 1;
574 } 574 }
575 575
576 /* The public functions */ 576 /* The public functions */
577 PyThreadState * 577 PyThreadState *
578 PyGILState_GetThisThreadState(void) 578 PyGILState_GetThisThreadState(void)
579 { 579 {
580 if (autoInterpreterState == NULL || autoTLSkey == 0) 580 if (autoInterpreterState == NULL || autoTLSkey == 0)
581 return NULL; 581 return NULL;
582 return (PyThreadState *)PyThread_get_key_value(autoTLSkey); 582 return (PyThreadState *)PyThread_get_key_value(autoTLSkey);
583 } 583 }
584 584
585 PyGILState_STATE 585 PyGILState_STATE
586 PyGILState_Ensure(void) 586 PyGILState_Ensure(void)
587 { 587 {
588 int current; 588 int current;
589 PyThreadState *tcur; 589 PyThreadState *tcur;
590 /* Note that we do not auto-init Python here - apart from 590 /* Note that we do not auto-init Python here - apart from
591 potential races with 2 threads auto-initializing, pep-311 591 potential races with 2 threads auto-initializing, pep-311
592 spells out other issues. Embedders are expected to have 592 spells out other issues. Embedders are expected to have
593 called Py_Initialize() and usually PyEval_InitThreads(). 593 called Py_Initialize() and usually PyEval_InitThreads().
594 */ 594 */
595 assert(autoInterpreterState); /* Py_Initialize() hasn't been called! */ 595 assert(autoInterpreterState); /* Py_Initialize() hasn't been called! */
596 tcur = (PyThreadState *)PyThread_get_key_value(autoTLSkey); 596 tcur = (PyThreadState *)PyThread_get_key_value(autoTLSkey);
597 if (tcur == NULL) { 597 if (tcur == NULL) {
598 /* Create a new thread state for this thread */ 598 /* Create a new thread state for this thread */
599 tcur = PyThreadState_New(autoInterpreterState); 599 tcur = PyThreadState_New(autoInterpreterState);
600 if (tcur == NULL) 600 if (tcur == NULL)
601 Py_FatalError("Couldn't create thread-state for new thread"); 601 Py_FatalError("Couldn't create thread-state for new thread");
602 /* This is our thread state! We'll need to delete it in the 602 /* This is our thread state! We'll need to delete it in the
603 matching call to PyGILState_Release(). */ 603 matching call to PyGILState_Release(). */
604 tcur->gilstate_counter = 0; 604 tcur->gilstate_counter = 0;
605 current = 0; /* new thread state is never current */ 605 current = 0; /* new thread state is never current */
606 } 606 }
607 else 607 else
608 current = PyThreadState_IsCurrent(tcur); 608 current = PyThreadState_IsCurrent(tcur);
609 if (current == 0) 609 if (current == 0)
610 PyEval_RestoreThread(tcur); 610 PyEval_RestoreThread(tcur);
611 /* Update our counter in the thread-state - no need for locks: 611 /* Update our counter in the thread-state - no need for locks:
612 - tcur will remain valid as we hold the GIL. 612 - tcur will remain valid as we hold the GIL.
613 - the counter is safe as we are the only thread "allowed" 613 - the counter is safe as we are the only thread "allowed"
614 to modify this value 614 to modify this value
615 */ 615 */
616 ++tcur->gilstate_counter; 616 ++tcur->gilstate_counter;
617 return current ? PyGILState_LOCKED : PyGILState_UNLOCKED; 617 return current ? PyGILState_LOCKED : PyGILState_UNLOCKED;
618 } 618 }
619 619
620 void 620 void
621 PyGILState_Release(PyGILState_STATE oldstate) 621 PyGILState_Release(PyGILState_STATE oldstate)
622 { 622 {
623 PyThreadState *tcur = (PyThreadState *)PyThread_get_key_value( 623 PyThreadState *tcur = (PyThreadState *)PyThread_get_key_value(
624 autoTLSkey); 624 autoTLSkey);
625 if (tcur == NULL) 625 if (tcur == NULL)
626 Py_FatalError("auto-releasing thread-state, " 626 Py_FatalError("auto-releasing thread-state, "
627 "but no thread-state for this thread"); 627 "but no thread-state for this thread");
628 /* We must hold the GIL and have our thread state current */ 628 /* We must hold the GIL and have our thread state current */
629 /* XXX - remove the check - the assert should be fine, 629 /* XXX - remove the check - the assert should be fine,
630 but while this is very new (April 2003), the extra check 630 but while this is very new (April 2003), the extra check
631 by release-only users can't hurt. 631 by release-only users can't hurt.
632 */ 632 */
633 if (! PyThreadState_IsCurrent(tcur)) 633 if (! PyThreadState_IsCurrent(tcur))
634 Py_FatalError("This thread state must be current when releasing"); 634 Py_FatalError("This thread state must be current when releasing");
635 assert(PyThreadState_IsCurrent(tcur)); 635 assert(PyThreadState_IsCurrent(tcur));
636 --tcur->gilstate_counter; 636 --tcur->gilstate_counter;
637 assert(tcur->gilstate_counter >= 0); /* illegal counter value */ 637 assert(tcur->gilstate_counter >= 0); /* illegal counter value */
638 638
639 /* If we're going to destroy this thread-state, we must 639 /* If we're going to destroy this thread-state, we must
640 * clear it while the GIL is held, as destructors may run. 640 * clear it while the GIL is held, as destructors may run.
641 */ 641 */
642 if (tcur->gilstate_counter == 0) { 642 if (tcur->gilstate_counter == 0) {
643 /* can't have been locked when we created it */ 643 /* can't have been locked when we created it */
644 assert(oldstate == PyGILState_UNLOCKED); 644 assert(oldstate == PyGILState_UNLOCKED);
645 PyThreadState_Clear(tcur); 645 PyThreadState_Clear(tcur);
646 /* Delete the thread-state. Note this releases the GIL too! 646 /* Delete the thread-state. Note this releases the GIL too!
647 * It's vital that the GIL be held here, to avoid shutdown 647 * It's vital that the GIL be held here, to avoid shutdown
648 * races; see bugs 225673 and 1061968 (that nasty bug has a 648 * races; see bugs 225673 and 1061968 (that nasty bug has a
649 * habit of coming back). 649 * habit of coming back).
650 */ 650 */
651 PyThreadState_DeleteCurrent(); 651 PyThreadState_DeleteCurrent();
652 } 652 }
653 /* Release the lock if necessary */ 653 /* Release the lock if necessary */
654 else if (oldstate == PyGILState_UNLOCKED) 654 else if (oldstate == PyGILState_UNLOCKED)
655 PyEval_SaveThread(); 655 PyEval_SaveThread();
656 } 656 }
657 657
658 #ifdef __cplusplus 658 #ifdef __cplusplus
659 } 659 }
660 #endif 660 #endif