Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 19 additions & 11 deletions peps/pep-0788.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ interpreter. For example:
static int
thread_function(PyInterpreterView view)
{
PyInterpreterLock lock = PyInterpreterLock_AcquireView(view);
PyInterpreterLock lock = PyInterpreterLock_FromView(view);
if (lock == 0) {
return -1;
}
Expand Down Expand Up @@ -300,7 +300,7 @@ Interpreter Locks

This type is guaranteed to be pointer-sized.

.. c:function:: PyInterpreterLock PyInterpreterLock_AcquireCurrent(void)
.. c:function:: PyInterpreterLock PyInterpreterLock_FromCurrent(void)

Acquire a lock for the current interpreter.

Expand All @@ -310,7 +310,7 @@ Interpreter Locks
The caller must hold an :term:`attached thread state`.


.. c:function:: PyInterpreterLock PyInterpreterLock_AcquireView(PyInterpreterView view)
.. c:function:: PyInterpreterLock PyInterpreterLock_FromView(PyInterpreterView view)

Acquire a lock to an interpreter through a view.

Expand Down Expand Up @@ -364,7 +364,7 @@ Interpreter Views
Create a view to the current interpreter.

This function is generally meant to be used in tandem with
:c:func:`PyInterpreterLock_AcquireView`.
:c:func:`PyInterpreterLock_FromView`.

On success, this function returns a view to the current
interpreter, and returns ``0`` with an exception set on failure.
Expand Down Expand Up @@ -402,7 +402,7 @@ Interpreter Views
The caller does not need to hold an :term:`attached thread state`.


Ensuring And Releasing Thread States
Ensuring and Releasing Thread States
------------------------------------

This proposal includes two new high-level threading APIs that intend to
Expand Down Expand Up @@ -508,7 +508,7 @@ With this PEP, you would implement it like this:
PyObject *file,
PyObject *text)
{
PyInterpreterLock lock = PyInterpreterLock_AcquireView(view);
PyInterpreterLock lock = PyInterpreterLock_FromView(view);
if (lock == 0) {
/* Python interpreter has shut down */
return -1;
Expand Down Expand Up @@ -556,7 +556,7 @@ held. Any future finalizer that attempted to acquire the lock would be deadlocke
my_critical_operation(PyObject *self, PyObject *Py_UNUSED(args))
{
assert(PyThreadState_GetUnchecked() != NULL);
PyInterpreterLock lock = PyInterpreterLock_AcquireCurrent();
PyInterpreterLock lock = PyInterpreterLock_FromCurrent();
if (lock == 0) {
/* Python interpreter has shut down */
return NULL;
Expand Down Expand Up @@ -639,7 +639,7 @@ This is the same code, rewritten to use the new functions:
PyThread_handle_t handle;
PyThead_indent_t indent;

PyInterpreterLock lock = PyInterpreterLock_AcquireCurrent();
PyInterpreterLock lock = PyInterpreterLock_FromCurrent();
if (lock == 0) {
return NULL;
}
Expand Down Expand Up @@ -690,7 +690,7 @@ hang the current thread forever).
PyThread_handle_t handle;
PyThead_indent_t indent;

PyInterpreterLock lock = PyInterpreterLock_AcquireCurrent();
PyInterpreterLock lock = PyInterpreterLock_FromCurrent();
if (lock == 0) {
return NULL;
}
Expand All @@ -716,7 +716,7 @@ Example: An Asynchronous Callback
{
ThreadData *tdata = (ThreadData *)arg;
PyInterpreterView view = tdata->view;
PyInterpreterLock lock = PyInterpreterLock_AcquireView(view);
PyInterpreterLock lock = PyInterpreterLock_FromView(view);
if (lock == 0) {
fputs("Python has shut down!\n", stderr);
return -1;
Expand Down Expand Up @@ -771,7 +771,13 @@ interpreter through :c:func:`PyUnstable_InterpreterView_FromDefault`.
static void
call_python(void)
{
PyInterpreterLock lock = PyUnstable_InterpreterView_FromDefault();
PyInterpreterView view = PyUnstable_InterpreterView_FromDefault();
if (lock == 0) {
fputs("Python has shut down.", stderr);
return;
}

PyInterpreterLock lock = PyInterpreterLock_FromView(view);
if (lock == 0) {
fputs("Python has shut down.", stderr);
return;
Expand All @@ -780,13 +786,15 @@ interpreter through :c:func:`PyUnstable_InterpreterView_FromDefault`.
PyThreadView thread_view = PyThreadState_Ensure(lock);
if (thread_view == 0) {
PyInterpreterLock_Release(lock);
PyInterpreterView_Close(view);
return -1;
}
if (PyRun_SimpleString("print(42)") < 0) {
PyErr_Print();
}
PyThreadState_Release(thread_view);
PyInterpreterLock_Release(lock);
PyInterpreterView_Close(view);
return 0;
}

Expand Down