1/* 2 * Recursive FIFO lock 3 * 4 * Copyright Red Hat, Inc. 2013 5 * 6 * Authors: 7 * Stefan Hajnoczi <stefanha@redhat.com> 8 * 9 * This work is licensed under the terms of the GNU GPL, version 2 or later. 10 * See the COPYING file in the top-level directory. 11 * 12 */ 13 14#ifndef QEMU_RFIFOLOCK_H 15#define QEMU_RFIFOLOCK_H 16 17#include "qemu/thread.h" 18 19/* Recursive FIFO lock 20 * 21 * This lock provides more features than a plain mutex: 22 * 23 * 1. Fairness - enforces FIFO order. 24 * 2. Nesting - can be taken recursively. 25 * 3. Contention callback - optional, called when thread must wait. 26 * 27 * The recursive FIFO lock is heavyweight so prefer other synchronization 28 * primitives if you do not need its features. 29 */ 30typedef struct { 31 QemuMutex lock; /* protects all fields */ 32 33 /* FIFO order */ 34 unsigned int head; /* active ticket number */ 35 unsigned int tail; /* waiting ticket number */ 36 QemuCond cond; /* used to wait for our ticket number */ 37 38 /* Nesting */ 39 QemuThread owner_thread; /* thread that currently has ownership */ 40 unsigned int nesting; /* amount of nesting levels */ 41 42 /* Contention callback */ 43 void (*cb)(void *); /* called when thread must wait, with ->lock 44 * held so it may not recursively lock/unlock 45 */ 46 void *cb_opaque; 47} RFifoLock; 48 49void rfifolock_init(RFifoLock *r, void (*cb)(void *), void *opaque); 50void rfifolock_destroy(RFifoLock *r); 51void rfifolock_lock(RFifoLock *r); 52void rfifolock_unlock(RFifoLock *r); 53 54#endif /* QEMU_RFIFOLOCK_H */ 55