qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [Qemu-devel] [RFC PATCH 0/5] Scoped locks using attribute((cleanup))
@ 2017-12-08 10:55 Paolo Bonzini
  2017-12-08 10:55 ` [Qemu-devel] [PATCH 1/5] compiler: add a helper for C99 inline functions Paolo Bonzini
                   ` (9 more replies)
  0 siblings, 10 replies; 31+ messages in thread
From: Paolo Bonzini @ 2017-12-08 10:55 UTC (permalink / raw)
  To: qemu-devel; +Cc: Emilio G . Cota, Stefan Hajnoczi, Fam Zheng

This is an attempt to make a C API that resembles the C++
std::unique_lock (mostly untested).  The idea is that you can write

    QEMU_LOCK_GUARD(QemuMutex, guard_name, &some_mutex);

instead of

    qemu_mutex_lock(&some_mutex);
    ...
out:
    qemu_mutex_unlock(&some_mutex);

and the mutex will be unlocked on all exit paths.  In C++ that
would be "std::unique_lock<QemuMutex> guard_name(some_mutex);".
Likewise,

    QEMU_WITH_LOCK(QemuMutex, guard_name, &some_mutex) {
        ...
    }

is the same as

    qemu_mutex_lock(&some_mutex);
    ...
    qemu_mutex_unlock(&some_mutex);

except that any returns within the region will unlock the mutex.
It's possible to use QemuLockGuard also with a spinlock or a
CoMutex.  However, it is _not_ possible to return a QemuLockGuard
since C doesn't have an equivalent of C++'s "move semantics", so
there are other "constructor" macros such as QEMU_ADOPT_LOCK
and QEMU_TAKEN_LOCK.

On the positive side, I checked that the entire abstraction
is removed by the compiler, the generated code is more or less
the same.  Also, it would let us for example change block/curl.c
to use a CoQueue instead of a home-grown list+QemuMutex.

However, I am still not sure about the readability, and it doesn't play
well with another common idiom in QEMU, which is to wrap global mutexes
with function such as

    static void block_job_lock(void)
    {
        qemu_mutex_lock(&block_job_mutex);
    }

    static void block_job_unlock(void)
    {
        qemu_mutex_unlock(&block_job_mutex);
    }

So I'm a bit underwhelmed by this experiment.  Other opinions?

Paolo

Paolo Bonzini (5):
  compiler: add a helper for C99 inline functions
  lock-guard: add scoped lock implementation
  qemu-timer: convert to use lock guards
  qht: convert to use lock guards
  thread-pool: convert to use lock guards

 include/qemu/compiler.h   | 15 +++++++
 include/qemu/coroutine.h  |  4 ++
 include/qemu/lock-guard.h | 99 +++++++++++++++++++++++++++++++++++++++++++++++
 include/qemu/thread.h     |  7 ++++
 util/Makefile.objs        |  1 +
 util/qemu-thread.c        | 17 ++++++++
 util/qemu-timer.c         | 84 ++++++++++++++++++++--------------------
 util/qht.c                | 59 ++++++++++++++--------------
 util/thread-pool.c        | 26 ++++++-------
 9 files changed, 225 insertions(+), 87 deletions(-)
 create mode 100644 include/qemu/lock-guard.h
 create mode 100644 util/qemu-thread.c

-- 
2.14.3

^ permalink raw reply	[flat|nested] 31+ messages in thread

end of thread, other threads:[~2017-12-15 15:50 UTC | newest]

Thread overview: 31+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-12-08 10:55 [Qemu-devel] [RFC PATCH 0/5] Scoped locks using attribute((cleanup)) Paolo Bonzini
2017-12-08 10:55 ` [Qemu-devel] [PATCH 1/5] compiler: add a helper for C99 inline functions Paolo Bonzini
2017-12-08 10:55 ` [Qemu-devel] [PATCH 2/5] lock-guard: add scoped lock implementation Paolo Bonzini
2017-12-08 15:30   ` Stefan Hajnoczi
2017-12-08 17:56     ` Paolo Bonzini
2017-12-08 20:12       ` Eric Blake
2017-12-11 10:16       ` Stefan Hajnoczi
2017-12-11 13:51         ` Eric Blake
2017-12-12  9:16           ` Stefan Hajnoczi
2017-12-08 10:55 ` [Qemu-devel] [PATCH 3/5] qemu-timer: convert to use lock guards Paolo Bonzini
2017-12-08 14:26   ` Stefan Hajnoczi
2017-12-08 10:55 ` [Qemu-devel] [PATCH 4/5] qht: " Paolo Bonzini
2017-12-08 14:27   ` Stefan Hajnoczi
2017-12-08 10:55 ` [Qemu-devel] [PATCH 5/5] thread-pool: " Paolo Bonzini
2017-12-08 15:13   ` Stefan Hajnoczi
2017-12-08 18:12     ` Paolo Bonzini
2017-12-08 20:02       ` Eric Blake
2017-12-11 10:23         ` Stefan Hajnoczi
2017-12-11 22:03           ` Paolo Bonzini
2017-12-08 19:50     ` Eric Blake
2017-12-11  6:35     ` Peter Xu
2017-12-08 19:40 ` [Qemu-devel] [RFC PATCH 0/5] Scoped locks using attribute((cleanup)) Eric Blake
2017-12-11  9:38   ` Peter Maydell
2017-12-11 14:11     ` Eric Blake
2017-12-11 21:32       ` Paolo Bonzini
2017-12-12 20:41         ` Eric Blake
2017-12-15 15:50           ` Richard Henderson
2017-12-11  6:40 ` no-reply
2017-12-11  6:40 ` no-reply
2017-12-11  6:46 ` no-reply
2017-12-11 22:06 ` Emilio G. Cota

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).