4 * Copyright Red Hat, Inc. 2013
7 * Stefan Hajnoczi <stefanha@redhat.com>
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.
14 #ifndef QEMU_RFIFOLOCK_H
15 #define QEMU_RFIFOLOCK_H
17 #include "qemu/thread.h"
19 /* Recursive FIFO lock
21 * This lock provides more features than a plain mutex:
23 * 1. Fairness - enforces FIFO order.
24 * 2. Nesting - can be taken recursively.
25 * 3. Contention callback - optional, called when thread must wait.
27 * The recursive FIFO lock is heavyweight so prefer other synchronization
28 * primitives if you do not need its features.
31 QemuMutex lock
; /* protects all fields */
34 unsigned int head
; /* active ticket number */
35 unsigned int tail
; /* waiting ticket number */
36 QemuCond cond
; /* used to wait for our ticket number */
39 QemuThread owner_thread
; /* thread that currently has ownership */
40 unsigned int nesting
; /* amount of nesting levels */
42 /* Contention callback */
43 void (*cb
)(void *); /* called when thread must wait, with ->lock
44 * held so it may not recursively lock/unlock
49 void rfifolock_init(RFifoLock
*r
, void (*cb
)(void *), void *opaque
);
50 void rfifolock_destroy(RFifoLock
*r
);
51 void rfifolock_lock(RFifoLock
*r
);
52 void rfifolock_unlock(RFifoLock
*r
);
54 #endif /* QEMU_RFIFOLOCK_H */