Commit 0231a65d by Kirill Smelkov

bigfile/virtmem: Factor functionality to unlock/retake GIL into own functions

Previously we were doing virt_lock() / virt_unlock() which automatically
were making sure to unlock GIL before locking virtmem, and to restore
GIL state to previous after virtmem lock happened. virt_unlock() was
unlocking just the virtmem lock without touching GIL at all - that works
because the running code would eventually release GIL as python
regularly does so to allowing multiple threads to run.

In the next patch however, we'll need to wait for in-progress-loading
page to complete, and that wait has to be done with GIL released (so
other python threads could run), and for doing so we'll need
functionality to make sure GIL is unlocked and retake it back, not tied
to virt_lock().

So factor it out.
1 parent 99cd1f03
......@@ -61,20 +61,35 @@ static int __ram_reclaim(RAM *ram);
static pthread_mutex_t virtmem_lock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
static const VirtGilHooks *virtmem_gilhooks;
void virt_lock()
void *virt_gil_ensure_unlocked(void)
{
void *gilstate = NULL;
/* make sure we don't hold e.g. python GIL (not to deadlock, as GIL oscillates) */
if (virtmem_gilhooks)
gilstate = virtmem_gilhooks->gil_ensure_unlocked();
return gilstate;
}
void virt_gil_retake_if_waslocked(void *gilstate)
{
if (gilstate)
virtmem_gilhooks->gil_retake_if_waslocked(gilstate);
}
void virt_lock()
{
void *gilstate = NULL;
/* make sure we don't hold e.g. python GIL (not to deadlock, as GIL oscillates) */
gilstate = virt_gil_ensure_unlocked();
/* acquire virtmem lock */
xpthread_mutex_lock(&virtmem_lock);
/* retake GIL if we were holding it originally */
if (gilstate)
virtmem_gilhooks->gil_retake_if_waslocked(gilstate);
virt_gil_retake_if_waslocked(gilstate);
}
void virt_unlock()
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!