/* Recursive locking does not take a ticket since the head is only incremented when the outermost recursive caller unlocks.  */
void rfifolock_lock(RFifoLock *r)
/* Recursive locking does not take a ticket since the head is only incremented when the outermost recursive caller unlocks.  */
void rfifolock_lock(RFifoLock *r)
{
    qemu_mutex_lock(&r->lock);
    unsigned int ticket = r->tail++;
    if (r->nesting > 0 && qemu_thread_is_self(&r->owner_thread)) {
        r->tail--;
    } else {
        while (ticket != r->head) {
            if (r->cb) {
                r->cb(r->cb_opaque);
            }
            qemu_cond_wait(&r->cond, &r->lock);
        }
    }
    qemu_thread_get_self(&r->owner_thread);
    r->nesting++;
    qemu_mutex_unlock(&r->lock);
}