qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Qemu-devel] [PATCH 2/5] lock-guard: add scoped lock implementation


From: Stefan Hajnoczi
Subject: Re: [Qemu-devel] [PATCH 2/5] lock-guard: add scoped lock implementation
Date: Fri, 8 Dec 2017 15:30:10 +0000
User-agent: Mutt/1.9.1 (2017-09-22)

On Fri, Dec 08, 2017 at 11:55:50AM +0100, Paolo Bonzini wrote:

The implementation is somewhat complex.  Please structure the header
file so the public interfaces are clear and documented.  Move the
implementation out of the way and mark it private.  That will make it
easier for someone to figure out "how do I use lock-guard.h".

> diff --git a/include/qemu/lock-guard.h b/include/qemu/lock-guard.h
> new file mode 100644
> index 0000000000..e6a83bf9ee
> --- /dev/null
> +++ b/include/qemu/lock-guard.h
> @@ -0,0 +1,103 @@
> +#ifndef QEMU_LOCK_GUARD_H
> +#define QEMU_LOCK_GUARD_H 1
> +
> +typedef void QemuLockGuardFunc(void *);
> +typedef struct QemuLockGuard {
> +    QemuLockGuardFunc *p_lock_fn, *p_unlock_fn;
> +    void *lock;
> +    int locked;

bool?

> +#define QEMU_WITH_LOCK(type, name, lock)                                   \
> +    for (QEMU_LOCK_GUARD(type, name, lock);                                \
> +         qemu_lock_guard_is_taken(&name);                                  \
> +         qemu_lock_guard_unlock(&name))

I don't understand the need for the qemu_lock_guard_is_taken(&name)
condition, why not do the following?

  for (QEMU_LOCK_GUARD(type, name, lock);
       ;
       qemu_lock_guard_unlock(&name))

Also, the for loop means that break statements do not work inside
QEMU_WITH_LOCK() { ... }.  This needs to be documented.

Attachment: signature.asc
Description: PGP signature


reply via email to

[Prev in Thread] Current Thread [Next in Thread]