On Wed, 01/04 14:26, Paolo Bonzini wrote: > +For example, QEMU uses QemuLockCnt to manage an AioContext's list of > +bottom halves and file descriptor handlers. Modifications to the list > +of file descriptor handlers are rare. Creation of a new bottom half is > +frequent and can happen on a fast path; however: 1) it is almost never > +concurrent with a visit to the list of bottom halves;
Isn't it common that thread A is busy notifying thread B with BH, and thread B is busy processing the notification? In that case creation and visiting the BH list are concurrent. > 2) it only has > +three instructions in the critical path, two assignments and a smp_wmb(). > + > +/** > + * qemu_lockcnt_dec: possibly decrement a QemuLockCnt's counter and lock it. s/qemu_lockcnt_dec/qemu_lockcnt_dec_if_lock/ > + * @lockcnt: the lockcnt to operate on > + * > + * If the count is 1, decrement the count to zero, lock > + * the mutex and return true. Otherwise, return false. > + */ > +bool qemu_lockcnt_dec_if_lock(QemuLockCnt *lockcnt); > + > +/** > + * qemu_lockcnt_lock: lock a QemuLockCnt's mutex. > + * @lockcnt: the lockcnt to operate on > + * > + * Remember that concurrent visits are not blocked unless the count is > + * also zero. You can use qemu_lockcnt_count to check for this inside a > + * critical section. > + */ > +void qemu_lockcnt_lock(QemuLockCnt *lockcnt); > diff --git a/util/lockcnt.c b/util/lockcnt.c > new file mode 100644 > index 0000000..78ed1e4 > --- /dev/null > +++ b/util/lockcnt.c > @@ -0,0 +1,113 @@ > +/* > + * QemuLockCnt implementation > + * > + * Copyright Red Hat, Inc. 2015 2015, 2016? > + * > + * Author: > + * Paolo Bonzini <pbonz...@redhat.com> > + */ > +#include "qemu/osdep.h" > +#include "qemu/thread.h" > +#include "qemu/atomic.h"