Commit | Line | Data |
---|---|---|
86530b38 AT |
1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
2 | <html> | |
3 | <head> | |
4 | <link rel="STYLESHEET" href="api.css" type='text/css' /> | |
5 | <link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" /> | |
6 | <link rel='start' href='../index.html' title='Python Documentation Index' /> | |
7 | <link rel="first" href="api.html" title='Python/C API Reference Manual' /> | |
8 | <link rel='contents' href='contents.html' title="Contents" /> | |
9 | <link rel='index' href='genindex.html' title='Index' /> | |
10 | <link rel='last' href='about.html' title='About this document...' /> | |
11 | <link rel='help' href='about.html' title='About this document...' /> | |
12 | <link rel="next" href="profiling.html" /> | |
13 | <link rel="prev" href="initialization.html" /> | |
14 | <link rel="parent" href="initialization.html" /> | |
15 | <link rel="next" href="profiling.html" /> | |
16 | <meta name='aesop' content='information' /> | |
17 | <title>8.1 Thread State and the Global Interpreter Lock </title> | |
18 | </head> | |
19 | <body> | |
20 | <DIV CLASS="navigation"> | |
21 | <div id='top-navigation-panel' xml:id='top-navigation-panel'> | |
22 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
23 | <tr> | |
24 | <td class='online-navigation'><a rel="prev" title="8. Initialization, Finalization, and" | |
25 | href="initialization.html"><img src='../icons/previous.png' | |
26 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
27 | <td class='online-navigation'><a rel="parent" title="8. Initialization, Finalization, and" | |
28 | href="initialization.html"><img src='../icons/up.png' | |
29 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
30 | <td class='online-navigation'><a rel="next" title="8.2 Profiling and Tracing" | |
31 | href="profiling.html"><img src='../icons/next.png' | |
32 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
33 | <td align="center" width="100%">Python/C API Reference Manual</td> | |
34 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
35 | href="contents.html"><img src='../icons/contents.png' | |
36 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
37 | <td class='online-navigation'><img src='../icons/blank.png' | |
38 | border='0' height='32' alt='' width='32' /></td> | |
39 | <td class='online-navigation'><a rel="index" title="Index" | |
40 | href="genindex.html"><img src='../icons/index.png' | |
41 | border='0' height='32' alt='Index' width='32' /></A></td> | |
42 | </tr></table> | |
43 | <div class='online-navigation'> | |
44 | <b class="navlabel">Previous:</b> | |
45 | <a class="sectref" rel="prev" href="initialization.html">8. Initialization, Finalization, and</A> | |
46 | <b class="navlabel">Up:</b> | |
47 | <a class="sectref" rel="parent" href="initialization.html">8. Initialization, Finalization, and</A> | |
48 | <b class="navlabel">Next:</b> | |
49 | <a class="sectref" rel="next" href="profiling.html">8.2 Profiling and Tracing</A> | |
50 | </div> | |
51 | <hr /></div> | |
52 | </DIV> | |
53 | <!--End of Navigation Panel--> | |
54 | ||
55 | <H1><A NAME="SECTION0010100000000000000000"></A><A NAME="threads"></A> | |
56 | <BR> | |
57 | 8.1 Thread State and the Global Interpreter Lock | |
58 | ||
59 | </H1> | |
60 | ||
61 | <P> | |
62 | <a id='l2h-865' xml:id='l2h-865'></a> | |
63 | ||
64 | <P> | |
65 | The Python interpreter is not fully thread safe. In order to support | |
66 | multi-threaded Python programs, there's a global lock that must be | |
67 | held by the current thread before it can safely access Python objects. | |
68 | Without the lock, even the simplest operations could cause problems in | |
69 | a multi-threaded program: for example, when two threads simultaneously | |
70 | increment the reference count of the same object, the reference count | |
71 | could end up being incremented only once instead of twice. | |
72 | ||
73 | <P> | |
74 | Therefore, the rule exists that only the thread that has acquired the | |
75 | global interpreter lock may operate on Python objects or call Python/C | |
76 | API functions. In order to support multi-threaded Python programs, | |
77 | the interpreter regularly releases and reacquires the lock -- by | |
78 | default, every 100 bytecode instructions (this can be changed with | |
79 | <a id='l2h-838' xml:id='l2h-838'></a><tt class="function">sys.setcheckinterval()</tt>). The lock is also released and | |
80 | reacquired around potentially blocking I/O operations like reading or | |
81 | writing a file, so that other threads can run while the thread that | |
82 | requests the I/O is waiting for the I/O operation to complete. | |
83 | ||
84 | <P> | |
85 | The Python interpreter needs to keep some bookkeeping information | |
86 | separate per thread -- for this it uses a data structure called | |
87 | <tt class="ctype">PyThreadState</tt><a id='l2h-866' xml:id='l2h-866'></a>. There's one global | |
88 | variable, however: the pointer to the current | |
89 | <tt class="ctype">PyThreadState</tt><a id='l2h-867' xml:id='l2h-867'></a> structure. While most | |
90 | thread packages have a way to store ``per-thread global data,'' | |
91 | Python's internal platform independent thread abstraction doesn't | |
92 | support this yet. Therefore, the current thread state must be | |
93 | manipulated explicitly. | |
94 | ||
95 | <P> | |
96 | This is easy enough in most cases. Most code manipulating the global | |
97 | interpreter lock has the following simple structure: | |
98 | ||
99 | <P> | |
100 | <div class="verbatim"><pre> | |
101 | Save the thread state in a local variable. | |
102 | Release the interpreter lock. | |
103 | ...Do some blocking I/O operation... | |
104 | Reacquire the interpreter lock. | |
105 | Restore the thread state from the local variable. | |
106 | </pre></div> | |
107 | ||
108 | <P> | |
109 | This is so common that a pair of macros exists to simplify it: | |
110 | ||
111 | <P> | |
112 | <div class="verbatim"><pre> | |
113 | Py_BEGIN_ALLOW_THREADS | |
114 | ...Do some blocking I/O operation... | |
115 | Py_END_ALLOW_THREADS | |
116 | </pre></div> | |
117 | ||
118 | <P> | |
119 | The | |
120 | Py_BEGIN_ALLOW_THREADS<a id='l2h-868' xml:id='l2h-868'></a> | |
121 | macro opens a new block and declares a hidden local variable; the | |
122 | Py_END_ALLOW_THREADS<a id='l2h-869' xml:id='l2h-869'></a> | |
123 | macro closes the block. Another advantage of using these two macros | |
124 | is that when Python is compiled without thread support, they are | |
125 | defined empty, thus saving the thread state and lock manipulations. | |
126 | ||
127 | <P> | |
128 | When thread support is enabled, the block above expands to the | |
129 | following code: | |
130 | ||
131 | <P> | |
132 | <div class="verbatim"><pre> | |
133 | PyThreadState *_save; | |
134 | ||
135 | _save = PyEval_SaveThread(); | |
136 | ...Do some blocking I/O operation... | |
137 | PyEval_RestoreThread(_save); | |
138 | </pre></div> | |
139 | ||
140 | <P> | |
141 | Using even lower level primitives, we can get roughly the same effect | |
142 | as follows: | |
143 | ||
144 | <P> | |
145 | <div class="verbatim"><pre> | |
146 | PyThreadState *_save; | |
147 | ||
148 | _save = PyThreadState_Swap(NULL); | |
149 | PyEval_ReleaseLock(); | |
150 | ...Do some blocking I/O operation... | |
151 | PyEval_AcquireLock(); | |
152 | PyThreadState_Swap(_save); | |
153 | </pre></div> | |
154 | ||
155 | <P> | |
156 | There are some subtle differences; in particular, | |
157 | <tt class="cfunction">PyEval_RestoreThread()</tt><a id='l2h-870' xml:id='l2h-870'></a> saves | |
158 | and restores the value of the global variable | |
159 | <tt class="cdata">errno</tt><a id='l2h-871' xml:id='l2h-871'></a>, since the lock manipulation does not | |
160 | guarantee that <tt class="cdata">errno</tt> is left alone. Also, when thread support | |
161 | is disabled, | |
162 | <tt class="cfunction">PyEval_SaveThread()</tt><a id='l2h-872' xml:id='l2h-872'></a> and | |
163 | <tt class="cfunction">PyEval_RestoreThread()</tt> don't manipulate the lock; in this | |
164 | case, <tt class="cfunction">PyEval_ReleaseLock()</tt><a id='l2h-873' xml:id='l2h-873'></a> and | |
165 | <tt class="cfunction">PyEval_AcquireLock()</tt><a id='l2h-874' xml:id='l2h-874'></a> are not | |
166 | available. This is done so that dynamically loaded extensions | |
167 | compiled with thread support enabled can be loaded by an interpreter | |
168 | that was compiled with disabled thread support. | |
169 | ||
170 | <P> | |
171 | The global interpreter lock is used to protect the pointer to the | |
172 | current thread state. When releasing the lock and saving the thread | |
173 | state, the current thread state pointer must be retrieved before the | |
174 | lock is released (since another thread could immediately acquire the | |
175 | lock and store its own thread state in the global variable). | |
176 | Conversely, when acquiring the lock and restoring the thread state, | |
177 | the lock must be acquired before storing the thread state pointer. | |
178 | ||
179 | <P> | |
180 | Why am I going on with so much detail about this? Because when | |
181 | threads are created from C, they don't have the global interpreter | |
182 | lock, nor is there a thread state data structure for them. Such | |
183 | threads must bootstrap themselves into existence, by first creating a | |
184 | thread state data structure, then acquiring the lock, and finally | |
185 | storing their thread state pointer, before they can start using the | |
186 | Python/C API. When they are done, they should reset the thread state | |
187 | pointer, release the lock, and finally free their thread state data | |
188 | structure. | |
189 | ||
190 | <P> | |
191 | Beginning with version 2.3, threads can now take advantage of the | |
192 | <tt class="cfunction">PyGILState_*()</tt> functions to do all of the above | |
193 | automatically. The typical idiom for calling into Python from a C | |
194 | thread is now: | |
195 | ||
196 | <P> | |
197 | <div class="verbatim"><pre> | |
198 | PyGILState_STATE gstate; | |
199 | gstate = PyGILState_Ensure(); | |
200 | ||
201 | /* Perform Python actions here. */ | |
202 | result = CallSomeFunction(); | |
203 | /* evaluate result */ | |
204 | ||
205 | /* Release the thread. No Python API allowed beyond this point. */ | |
206 | PyGILState_Release(gstate); | |
207 | </pre></div> | |
208 | ||
209 | <P> | |
210 | Note that the <tt class="cfunction">PyGILState_*()</tt> functions assume there is only | |
211 | one global interpreter (created automatically by | |
212 | <tt class="cfunction">Py_Initialize()</tt>). Python still supports the creation of | |
213 | additional interpreters (using <tt class="cfunction">Py_NewInterpreter()</tt>), but | |
214 | mixing multiple interpreters and the <tt class="cfunction">PyGILState_*()</tt> API is | |
215 | unsupported. | |
216 | ||
217 | <P> | |
218 | <dl><dt><b><tt class="ctype"><a id='l2h-839' xml:id='l2h-839'>PyInterpreterState</a></tt></b></dt> | |
219 | <dd> | |
220 | This data structure represents the state shared by a number of | |
221 | cooperating threads. Threads belonging to the same interpreter | |
222 | share their module administration and a few other internal items. | |
223 | There are no public members in this structure. | |
224 | ||
225 | <P> | |
226 | Threads belonging to different interpreters initially share nothing, | |
227 | except process state like available memory, open file descriptors | |
228 | and such. The global interpreter lock is also shared by all | |
229 | threads, regardless of to which interpreter they belong. | |
230 | </dl> | |
231 | ||
232 | <P> | |
233 | <dl><dt><b><tt class="ctype"><a id='l2h-840' xml:id='l2h-840'>PyThreadState</a></tt></b></dt> | |
234 | <dd> | |
235 | This data structure represents the state of a single thread. The | |
236 | only public data member is <tt class="ctype">PyInterpreterState | |
237 | *</tt><tt class="member">interp</tt>, which points to this thread's interpreter state. | |
238 | </dl> | |
239 | ||
240 | <P> | |
241 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"><td><nobr>void <b><tt id='l2h-841' xml:id='l2h-841' class="cfunction">PyEval_InitThreads</tt></b>(</nobr></td><td>)</td></tr></table></dt> | |
242 | <dd> | |
243 | Initialize and acquire the global interpreter lock. It should be | |
244 | called in the main thread before creating a second thread or | |
245 | engaging in any other thread operations such as | |
246 | <tt class="cfunction">PyEval_ReleaseLock()</tt><a id='l2h-875' xml:id='l2h-875'></a> or | |
247 | <code>PyEval_ReleaseThread(<var>tstate</var>)</code><a id='l2h-876' xml:id='l2h-876'></a>. | |
248 | It is not needed before calling | |
249 | <tt class="cfunction">PyEval_SaveThread()</tt><a id='l2h-877' xml:id='l2h-877'></a> or | |
250 | <tt class="cfunction">PyEval_RestoreThread()</tt><a id='l2h-878' xml:id='l2h-878'></a>. | |
251 | ||
252 | <P> | |
253 | This is a no-op when called for a second time. It is safe to call | |
254 | this function before calling | |
255 | <tt class="cfunction">Py_Initialize()</tt><a id='l2h-879' xml:id='l2h-879'></a>. | |
256 | ||
257 | <P> | |
258 | When only the main thread exists, no lock operations are needed. | |
259 | This is a common situation (most Python programs do not use | |
260 | threads), and the lock operations slow the interpreter down a bit. | |
261 | Therefore, the lock is not created initially. This situation is | |
262 | equivalent to having acquired the lock: when there is only a single | |
263 | thread, all object accesses are safe. Therefore, when this function | |
264 | initializes the lock, it also acquires it. Before the Python | |
265 | <tt class="module">thread</tt><a id='l2h-880' xml:id='l2h-880'></a> module creates a new thread, | |
266 | knowing that either it has the lock or the lock hasn't been created | |
267 | yet, it calls <tt class="cfunction">PyEval_InitThreads()</tt>. When this call | |
268 | returns, it is guaranteed that the lock has been created and that the | |
269 | calling thread has acquired it. | |
270 | ||
271 | <P> | |
272 | It is <strong>not</strong> safe to call this function when it is unknown | |
273 | which thread (if any) currently has the global interpreter lock. | |
274 | ||
275 | <P> | |
276 | This function is not available when thread support is disabled at | |
277 | compile time. | |
278 | </dd></dl> | |
279 | ||
280 | <P> | |
281 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"><td><nobr>int <b><tt id='l2h-842' xml:id='l2h-842' class="cfunction">PyEval_ThreadsInitialized</tt></b>(</nobr></td><td>)</td></tr></table></dt> | |
282 | <dd> | |
283 | Returns a non-zero value if <tt class="cfunction">PyEval_InitThreads()</tt> has been | |
284 | called. This function can be called without holding the lock, and | |
285 | therefore can be used to avoid calls to the locking API when running | |
286 | single-threaded. This function is not available when thread support | |
287 | is disabled at compile time. | |
288 | <span class="versionnote">New in version 2.4.</span> | |
289 | ||
290 | </dd></dl> | |
291 | ||
292 | <P> | |
293 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"><td><nobr>void <b><tt id='l2h-843' xml:id='l2h-843' class="cfunction">PyEval_AcquireLock</tt></b>(</nobr></td><td>)</td></tr></table></dt> | |
294 | <dd> | |
295 | Acquire the global interpreter lock. The lock must have been | |
296 | created earlier. If this thread already has the lock, a deadlock | |
297 | ensues. This function is not available when thread support is | |
298 | disabled at compile time. | |
299 | </dd></dl> | |
300 | ||
301 | <P> | |
302 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"><td><nobr>void <b><tt id='l2h-844' xml:id='l2h-844' class="cfunction">PyEval_ReleaseLock</tt></b>(</nobr></td><td>)</td></tr></table></dt> | |
303 | <dd> | |
304 | Release the global interpreter lock. The lock must have been | |
305 | created earlier. This function is not available when thread support | |
306 | is disabled at compile time. | |
307 | </dd></dl> | |
308 | ||
309 | <P> | |
310 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"><td><nobr>void <b><tt id='l2h-845' xml:id='l2h-845' class="cfunction">PyEval_AcquireThread</tt></b>(</nobr></td><td>PyThreadState *<var>tstate</var>)</td></tr></table></dt> | |
311 | <dd> | |
312 | Acquire the global interpreter lock and set the current thread | |
313 | state to <var>tstate</var>, which should not be <tt class="constant">NULL</tt>. The lock must | |
314 | have been created earlier. If this thread already has the lock, | |
315 | deadlock ensues. This function is not available when thread support | |
316 | is disabled at compile time. | |
317 | </dd></dl> | |
318 | ||
319 | <P> | |
320 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"><td><nobr>void <b><tt id='l2h-846' xml:id='l2h-846' class="cfunction">PyEval_ReleaseThread</tt></b>(</nobr></td><td>PyThreadState *<var>tstate</var>)</td></tr></table></dt> | |
321 | <dd> | |
322 | Reset the current thread state to <tt class="constant">NULL</tt> and release the global | |
323 | interpreter lock. The lock must have been created earlier and must | |
324 | be held by the current thread. The <var>tstate</var> argument, which | |
325 | must not be <tt class="constant">NULL</tt>, is only used to check that it represents the | |
326 | current thread state -- if it isn't, a fatal error is reported. | |
327 | This function is not available when thread support is disabled at | |
328 | compile time. | |
329 | </dd></dl> | |
330 | ||
331 | <P> | |
332 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"><td><nobr>PyThreadState* <b><tt id='l2h-847' xml:id='l2h-847' class="cfunction">PyEval_SaveThread</tt></b>(</nobr></td><td>)</td></tr></table></dt> | |
333 | <dd> | |
334 | Release the interpreter lock (if it has been created and thread | |
335 | support is enabled) and reset the thread state to <tt class="constant">NULL</tt>, returning | |
336 | the previous thread state (which is not <tt class="constant">NULL</tt>). If the lock has | |
337 | been created, the current thread must have acquired it. (This | |
338 | function is available even when thread support is disabled at | |
339 | compile time.) | |
340 | </dd></dl> | |
341 | ||
342 | <P> | |
343 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"><td><nobr>void <b><tt id='l2h-848' xml:id='l2h-848' class="cfunction">PyEval_RestoreThread</tt></b>(</nobr></td><td>PyThreadState *<var>tstate</var>)</td></tr></table></dt> | |
344 | <dd> | |
345 | Acquire the interpreter lock (if it has been created and thread | |
346 | support is enabled) and set the thread state to <var>tstate</var>, which | |
347 | must not be <tt class="constant">NULL</tt>. If the lock has been created, the current thread | |
348 | must not have acquired it, otherwise deadlock ensues. (This | |
349 | function is available even when thread support is disabled at | |
350 | compile time.) | |
351 | </dd></dl> | |
352 | ||
353 | <P> | |
354 | The following macros are normally used without a trailing semicolon; | |
355 | look for example usage in the Python source distribution. | |
356 | ||
357 | <P> | |
358 | <dl><dt><b><tt id='l2h-849' xml:id='l2h-849' class="macro">Py_BEGIN_ALLOW_THREADS</tt></b></dt> | |
359 | <dd> | |
360 | This macro expands to | |
361 | "<tt class="samp">{ PyThreadState *_save; _save = PyEval_SaveThread();</tt>". | |
362 | Note that it contains an opening brace; it must be matched with a | |
363 | following Py_END_ALLOW_THREADS macro. See above for | |
364 | further discussion of this macro. It is a no-op when thread support | |
365 | is disabled at compile time. | |
366 | </dl> | |
367 | ||
368 | <P> | |
369 | <dl><dt><b><tt id='l2h-850' xml:id='l2h-850' class="macro">Py_END_ALLOW_THREADS</tt></b></dt> | |
370 | <dd> | |
371 | This macro expands to "<tt class="samp">PyEval_RestoreThread(_save); }</tt>". | |
372 | Note that it contains a closing brace; it must be matched with an | |
373 | earlier Py_BEGIN_ALLOW_THREADS macro. See above for | |
374 | further discussion of this macro. It is a no-op when thread support | |
375 | is disabled at compile time. | |
376 | </dl> | |
377 | ||
378 | <P> | |
379 | <dl><dt><b><tt id='l2h-851' xml:id='l2h-851' class="macro">Py_BLOCK_THREADS</tt></b></dt> | |
380 | <dd> | |
381 | This macro expands to "<tt class="samp">PyEval_RestoreThread(_save);</tt>": it is | |
382 | equivalent to Py_END_ALLOW_THREADS without the | |
383 | closing brace. It is a no-op when thread support is disabled at | |
384 | compile time. | |
385 | </dl> | |
386 | ||
387 | <P> | |
388 | <dl><dt><b><tt id='l2h-852' xml:id='l2h-852' class="macro">Py_UNBLOCK_THREADS</tt></b></dt> | |
389 | <dd> | |
390 | This macro expands to "<tt class="samp">_save = PyEval_SaveThread();</tt>": it is | |
391 | equivalent to Py_BEGIN_ALLOW_THREADS without the | |
392 | opening brace and variable declaration. It is a no-op when thread | |
393 | support is disabled at compile time. | |
394 | </dl> | |
395 | ||
396 | <P> | |
397 | All of the following functions are only available when thread support | |
398 | is enabled at compile time, and must be called only when the | |
399 | interpreter lock has been created. | |
400 | ||
401 | <P> | |
402 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"><td><nobr>PyInterpreterState* <b><tt id='l2h-853' xml:id='l2h-853' class="cfunction">PyInterpreterState_New</tt></b>(</nobr></td><td>)</td></tr></table></dt> | |
403 | <dd> | |
404 | Create a new interpreter state object. The interpreter lock need | |
405 | not be held, but may be held if it is necessary to serialize calls | |
406 | to this function. | |
407 | </dd></dl> | |
408 | ||
409 | <P> | |
410 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"><td><nobr>void <b><tt id='l2h-854' xml:id='l2h-854' class="cfunction">PyInterpreterState_Clear</tt></b>(</nobr></td><td>PyInterpreterState *<var>interp</var>)</td></tr></table></dt> | |
411 | <dd> | |
412 | Reset all information in an interpreter state object. The | |
413 | interpreter lock must be held. | |
414 | </dd></dl> | |
415 | ||
416 | <P> | |
417 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"><td><nobr>void <b><tt id='l2h-855' xml:id='l2h-855' class="cfunction">PyInterpreterState_Delete</tt></b>(</nobr></td><td>PyInterpreterState *<var>interp</var>)</td></tr></table></dt> | |
418 | <dd> | |
419 | Destroy an interpreter state object. The interpreter lock need not | |
420 | be held. The interpreter state must have been reset with a previous | |
421 | call to <tt class="cfunction">PyInterpreterState_Clear()</tt>. | |
422 | </dd></dl> | |
423 | ||
424 | <P> | |
425 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"><td><nobr>PyThreadState* <b><tt id='l2h-856' xml:id='l2h-856' class="cfunction">PyThreadState_New</tt></b>(</nobr></td><td>PyInterpreterState *<var>interp</var>)</td></tr></table></dt> | |
426 | <dd> | |
427 | Create a new thread state object belonging to the given interpreter | |
428 | object. The interpreter lock need not be held, but may be held if | |
429 | it is necessary to serialize calls to this function. | |
430 | </dd></dl> | |
431 | ||
432 | <P> | |
433 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"><td><nobr>void <b><tt id='l2h-857' xml:id='l2h-857' class="cfunction">PyThreadState_Clear</tt></b>(</nobr></td><td>PyThreadState *<var>tstate</var>)</td></tr></table></dt> | |
434 | <dd> | |
435 | Reset all information in a thread state object. The interpreter lock | |
436 | must be held. | |
437 | </dd></dl> | |
438 | ||
439 | <P> | |
440 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"><td><nobr>void <b><tt id='l2h-858' xml:id='l2h-858' class="cfunction">PyThreadState_Delete</tt></b>(</nobr></td><td>PyThreadState *<var>tstate</var>)</td></tr></table></dt> | |
441 | <dd> | |
442 | Destroy a thread state object. The interpreter lock need not be | |
443 | held. The thread state must have been reset with a previous call to | |
444 | <tt class="cfunction">PyThreadState_Clear()</tt>. | |
445 | </dd></dl> | |
446 | ||
447 | <P> | |
448 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"><td><nobr>PyThreadState* <b><tt id='l2h-859' xml:id='l2h-859' class="cfunction">PyThreadState_Get</tt></b>(</nobr></td><td>)</td></tr></table></dt> | |
449 | <dd> | |
450 | Return the current thread state. The interpreter lock must be | |
451 | held. When the current thread state is <tt class="constant">NULL</tt>, this issues a fatal | |
452 | error (so that the caller needn't check for <tt class="constant">NULL</tt>). | |
453 | </dd></dl> | |
454 | ||
455 | <P> | |
456 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"><td><nobr>PyThreadState* <b><tt id='l2h-860' xml:id='l2h-860' class="cfunction">PyThreadState_Swap</tt></b>(</nobr></td><td>PyThreadState *<var>tstate</var>)</td></tr></table></dt> | |
457 | <dd> | |
458 | Swap the current thread state with the thread state given by the | |
459 | argument <var>tstate</var>, which may be <tt class="constant">NULL</tt>. The interpreter lock | |
460 | must be held. | |
461 | </dd></dl> | |
462 | ||
463 | <P> | |
464 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"><td><nobr>PyObject* <b><tt id='l2h-861' xml:id='l2h-861' class="cfunction">PyThreadState_GetDict</tt></b>(</nobr></td><td>)</td></tr></table></dt> | |
465 | <dd> | |
466 | <div class="refcount-info"> | |
467 | <span class="label">Return value:</span> | |
468 | <span class="value">Borrowed reference.</span> | |
469 | </div> | |
470 | Return a dictionary in which extensions can store thread-specific | |
471 | state information. Each extension should use a unique key to use to | |
472 | store state in the dictionary. It is okay to call this function | |
473 | when no current thread state is available. | |
474 | If this function returns <tt class="constant">NULL</tt>, no exception has been raised and the | |
475 | caller should assume no current thread state is available. | |
476 | ||
477 | <span class="versionnote">Changed in version 2.3: | |
478 | Previously this could only be called when a current | |
479 | thread is active, and <tt class="constant">NULL</tt> meant that an exception was raised.</span> | |
480 | ||
481 | </dd></dl> | |
482 | ||
483 | <P> | |
484 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"><td><nobr>int <b><tt id='l2h-862' xml:id='l2h-862' class="cfunction">PyThreadState_SetAsyncExc</tt></b>(</nobr></td><td>long <var>id</var>, PyObject *<var>exc</var>)</td></tr></table></dt> | |
485 | <dd> | |
486 | Asynchronously raise an exception in a thread. | |
487 | The <var>id</var> argument is the thread id of the target thread; | |
488 | <var>exc</var> is the exception object to be raised. | |
489 | This function does not steal any references to <var>exc</var>. | |
490 | To prevent naive misuse, you must write your own C extension | |
491 | to call this. Must be called with the GIL held. | |
492 | Returns the number of thread states modified; if it returns a number | |
493 | greater than one, you're in trouble, and you should call it again | |
494 | with <var>exc</var> set to <tt class="constant">NULL</tt> to revert the effect. | |
495 | This raises no exceptions. | |
496 | ||
497 | <span class="versionnote">New in version 2.3.</span> | |
498 | ||
499 | </dd></dl> | |
500 | ||
501 | <P> | |
502 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"><td><nobr>PyGILState_STATE <b><tt id='l2h-863' xml:id='l2h-863' class="cfunction">PyGILState_Ensure</tt></b>(</nobr></td><td>)</td></tr></table></dt> | |
503 | <dd> | |
504 | Ensure that the current thread is ready to call the Python | |
505 | C API regardless of the current state of Python, or of its | |
506 | thread lock. This may be called as many times as desired | |
507 | by a thread as long as each call is matched with a call to | |
508 | <tt class="cfunction">PyGILState_Release()</tt>. | |
509 | In general, other thread-related APIs may | |
510 | be used between <tt class="cfunction">PyGILState_Ensure()</tt> and <tt class="cfunction">PyGILState_Release()</tt> calls as long as the | |
511 | thread state is restored to its previous state before the Release(). | |
512 | For example, normal usage of the Py_BEGIN_ALLOW_THREADS | |
513 | and Py_END_ALLOW_THREADS macros is acceptable. | |
514 | ||
515 | <P> | |
516 | The return value is an opaque "handle" to the thread state when | |
517 | <tt class="cfunction">PyGILState_Acquire()</tt> was called, and must be passed to | |
518 | <tt class="cfunction">PyGILState_Release()</tt> to ensure Python is left in the same | |
519 | state. Even though recursive calls are allowed, these handles | |
520 | <em>cannot</em> be shared - each unique call to | |
521 | <tt class="cfunction">PyGILState_Ensure</tt> must save the handle for its call to | |
522 | <tt class="cfunction">PyGILState_Release</tt>. | |
523 | ||
524 | <P> | |
525 | When the function returns, the current thread will hold the GIL. | |
526 | Failure is a fatal error. | |
527 | ||
528 | <span class="versionnote">New in version 2.3.</span> | |
529 | ||
530 | </dd></dl> | |
531 | ||
532 | <P> | |
533 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"><td><nobr>void <b><tt id='l2h-864' xml:id='l2h-864' class="cfunction">PyGILState_Release</tt></b>(</nobr></td><td>PyGILState_STATE)</td></tr></table></dt> | |
534 | <dd> | |
535 | Release any resources previously acquired. After this call, Python's | |
536 | state will be the same as it was prior to the corresponding | |
537 | <tt class="cfunction">PyGILState_Ensure</tt> call (but generally this state will be | |
538 | unknown to the caller, hence the use of the GILState API.) | |
539 | ||
540 | <P> | |
541 | Every call to <tt class="cfunction">PyGILState_Ensure()</tt> must be matched by a call to | |
542 | <tt class="cfunction">PyGILState_Release()</tt> on the same thread. | |
543 | ||
544 | <span class="versionnote">New in version 2.3.</span> | |
545 | ||
546 | </dd></dl> | |
547 | ||
548 | <P> | |
549 | ||
550 | <DIV CLASS="navigation"> | |
551 | <div class='online-navigation'> | |
552 | <p></p><hr /> | |
553 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
554 | <tr> | |
555 | <td class='online-navigation'><a rel="prev" title="8. Initialization, Finalization, and" | |
556 | href="initialization.html"><img src='../icons/previous.png' | |
557 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
558 | <td class='online-navigation'><a rel="parent" title="8. Initialization, Finalization, and" | |
559 | href="initialization.html"><img src='../icons/up.png' | |
560 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
561 | <td class='online-navigation'><a rel="next" title="8.2 Profiling and Tracing" | |
562 | href="profiling.html"><img src='../icons/next.png' | |
563 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
564 | <td align="center" width="100%">Python/C API Reference Manual</td> | |
565 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
566 | href="contents.html"><img src='../icons/contents.png' | |
567 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
568 | <td class='online-navigation'><img src='../icons/blank.png' | |
569 | border='0' height='32' alt='' width='32' /></td> | |
570 | <td class='online-navigation'><a rel="index" title="Index" | |
571 | href="genindex.html"><img src='../icons/index.png' | |
572 | border='0' height='32' alt='Index' width='32' /></A></td> | |
573 | </tr></table> | |
574 | <div class='online-navigation'> | |
575 | <b class="navlabel">Previous:</b> | |
576 | <a class="sectref" rel="prev" href="initialization.html">8. Initialization, Finalization, and</A> | |
577 | <b class="navlabel">Up:</b> | |
578 | <a class="sectref" rel="parent" href="initialization.html">8. Initialization, Finalization, and</A> | |
579 | <b class="navlabel">Next:</b> | |
580 | <a class="sectref" rel="next" href="profiling.html">8.2 Profiling and Tracing</A> | |
581 | </div> | |
582 | </div> | |
583 | <hr /> | |
584 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> | |
585 | </DIV> | |
586 | <!--End of Navigation Panel--> | |
587 | <ADDRESS> | |
588 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. | |
589 | </ADDRESS> | |
590 | </BODY> | |
591 | </HTML> |