On 11/01/2017 16:48, Fam Zheng wrote: > 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.
For multi-threaded cases you may can create the BH just once (thread-pool does it, and aio_co_schedule will be there in the next part of the work to simplify "remote" qemu_coroutine_enter). The case where thread A creates the bottom half does occur with rbd.c, but then, the next point applies: >> 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/ Oops, I'll wait for full review and send v4. >> + * @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? Or 2017 even. :) Paolo >> + * >> + * Author: >> + * Paolo Bonzini <pbonz...@redhat.com> >> + */ >> +#include "qemu/osdep.h" >> +#include "qemu/thread.h" >> +#include "qemu/atomic.h"