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
22 changes: 16 additions & 6 deletions Doc/c-api/object.rst
Original file line number Diff line number Diff line change
Expand Up @@ -642,20 +642,31 @@ Object Protocol

.. code-block:: c

PyMutex mutex;

PyObject *
add_entry(weakmap_key_type *key, PyObject* value)
{
PyUnstable_EnableTryIncRef(value);
weakmap_type weakmap = ...;
weakmap_add_entry(weakmap, key, value);
Py_RETURN_NONE;
}

PyObject *
get_value(weakmap_key_type *key)
{
weakmap_type weakmap = ...;
weakmap_lock_mutex(weakmap);
PyMutex_Lock(mutex);
PyObject *result = weakmap_find(weakmap, key);
if (PyUnstable_TryIncRef(result)) {
// `result` is safe to use
weakmap_unlock_mutex(weakmap);
PyMutex_Unlock(mutex);
return result;
}
// if we get here, `result` is starting to be garbage-collected,
// but has not been removed from the weakmap yet
weakmap_unlock_mutex(weakmap->mutex);
PyMutex_Unlock(mutex);
return NULL;
}

Expand All @@ -664,14 +675,13 @@ Object Protocol
value_dealloc(PyObject *value)
{
weakmap_type weakmap = ...;
weakmap_lock_mutex(weakmap);
PyMutex_Lock(mutex);
weakmap_remove_value(weakmap, value);

...
weakmap_unlock_mutex(weakmap);
PyMutex_Unlock(mutex);
}


.. versionadded:: 3.14

.. c:function:: void PyUnstable_EnableTryIncRef(PyObject *obj)
Expand Down
Loading