Function simics_api_sys::PyEval_SaveThread

source ·
pub unsafe extern "C" fn PyEval_SaveThread() -> *mut PyThreadState
Expand description

Interface for threads.

A module that plans to do a blocking system call (or something else that lasts a long time and doesn’t touch Python data) can allow other threads to run as follows:

…preparations here… Py_BEGIN_ALLOW_THREADS …blocking system call here… Py_END_ALLOW_THREADS …interpret result here…

The Py_BEGIN_ALLOW_THREADS/Py_END_ALLOW_THREADS pair expands to a {}-surrounded block. To leave the block in the middle (e.g., with return), you must insert a line containing Py_BLOCK_THREADS before the return, e.g.

if (…premature_exit…) { Py_BLOCK_THREADS PyErr_SetFromErrno(PyExc_OSError); return NULL; }

An alternative is:

Py_BLOCK_THREADS if (…premature_exit…) { PyErr_SetFromErrno(PyExc_OSError); return NULL; } Py_UNBLOCK_THREADS

For convenience, that the value of ‘errno’ is restored across Py_END_ALLOW_THREADS and Py_BLOCK_THREADS.

WARNING: NEVER NEST CALLS TO Py_BEGIN_ALLOW_THREADS AND Py_END_ALLOW_THREADS!!!

Note that not yet all candidates have been converted to use this mechanism!