[PATCH 00/43] Replace virMutex & co. by the Glib equivalent

This patch series replaces virMutex and virRWLock by GMutex and GRWLock, respectively. And because virCond usage is connected to virCond, it also replaces the latter by GCond. Rafael Fonseca (43): admin: convert virMutex to GMutex bhyve: convert virMutex to GMutex conf: nwfilter_ipaddrmap: convert virMutex to GMutex conf: virchrdev: convert virMutex to GMutex esx: convert virMutex to GMutex util: virtpm: convert virMutex to GMutex vbox: convert virMutex to GMutex util: virnodesuspend: convert virMutex to GMutex libxl: convert virMutex to GMutex in libxlPrivate vz: vz_driver: convert virMutex to GMutex locking: convert virMutex to GMutex logging: convert virMutex to GMutex conf: nwfilter_conf: convert virRWLock to GRWLock util: virlog: convert virMutex to GMutex test: convert virMutex to GMutex nwfilter: nwfilter_dhcpsnoop: convert virMutex to GMutex nwfilter: nwfilter_gentech_driver: convert virMutex to GRecMutex lxc: lxc_fuse: convert virMutex to GMutex lxc: convert virMutex to GMutex network: bridge_driver: convert virMutex to GMutex node_device: convert virMutex to GMutex vmware: convert virMutex to GMutex tests: qemusecuritymock: convert virMutex to GMutex tests: qemumonitortestutils: convert virMutex to GMutex qemu: convert virMutex to GMutex remote: remote_driver: convert virMutex to GMutex nwfilter: convert virMutex to GMutex openvz: convert virMutex to GMutex storage: convert virMutex to GMutex secret: convert virMutex to GMutex remote: convert virMutex to GMutex in daemonClientPrivate util: virfirewall: convert virMutex to GMutex util: virlockspace: convert virMutex to GMutex util: virnetdevmacvlan: convert virMutex to GMutex util: virnetdevveth: convert virMutex to GMutex util: virnetlink: convert virMutex to GMutex tools: virsh: convert virMutex to GMutex nwfilter: nwfilter_learnipaddr: convert virMutex to GMutex util: virthreadpool: convert virMutex to GMutex util: virObjectRWLockable: convert virRWLock to GRWLock util: virthread: remove virRWLock util: virObjectLockable: convert virMutex to GMutex util: virthread: remove GMutex and Gcond examples/systemtap/lock-debug.stp | 4 +- src/admin/admin_server_dispatch.c | 13 +-- src/bhyve/bhyve_driver.c | 11 +- src/bhyve/bhyve_utils.h | 2 +- src/conf/domain_conf.c | 27 +---- src/conf/domain_conf.h | 5 +- src/conf/nwfilter_conf.c | 21 ++-- src/conf/nwfilter_conf.h | 5 +- src/conf/nwfilter_ipaddrmap.c | 14 +-- src/conf/virchrdev.c | 27 ++--- src/conf/virdomainobjlist.c | 35 +++--- src/conf/virinterfaceobj.c | 18 +-- src/conf/virnetworkobj.c | 20 ++-- src/conf/virnodedeviceobj.c | 16 +-- src/conf/virnodedeviceobj.h | 4 +- src/conf/virnwfilterbindingobjlist.c | 14 +-- src/conf/virnwfilterobj.c | 15 +-- src/conf/virnwfilterobj.h | 2 +- src/conf/virsecretobj.c | 16 +-- src/conf/virstorageobj.c | 50 ++++----- src/conf/virstorageobj.h | 2 +- src/esx/esx_stream.c | 25 ++--- src/esx/esx_vi.c | 68 ++++------- src/esx/esx_vi.h | 6 +- src/libvirt_private.syms | 22 +--- src/libxl/libxl_conf.h | 6 +- src/libxl/libxl_domain.c | 18 +-- src/libxl/libxl_domain.h | 2 +- src/libxl/libxl_driver.c | 9 +- src/locking/lock_daemon.c | 32 ++---- src/locking/lock_daemon.h | 2 +- src/locking/lock_daemon_dispatch.c | 32 ++---- src/logging/log_daemon.c | 26 +---- src/logging/log_daemon.h | 2 +- src/lxc/lxc_conf.h | 6 +- src/lxc/lxc_controller.c | 25 ++--- src/lxc/lxc_domain.c | 18 +-- src/lxc/lxc_domain.h | 2 +- src/lxc/lxc_driver.c | 11 +- src/lxc/lxc_fuse.c | 13 +-- src/lxc/lxc_fuse.h | 2 +- src/network/bridge_driver.c | 11 +- src/network/bridge_driver_platform.h | 2 +- src/node_device/node_device_driver.c | 11 +- src/node_device/node_device_hal.c | 20 +--- src/node_device/node_device_udev.c | 43 ++----- src/nwfilter/nwfilter_dhcpsnoop.c | 48 ++++---- src/nwfilter/nwfilter_driver.c | 15 ++- src/nwfilter/nwfilter_gentech_driver.c | 40 +++---- src/nwfilter/nwfilter_learnipaddr.c | 43 ++++--- src/openvz/openvz_conf.h | 2 +- src/openvz/openvz_driver.c | 4 +- src/qemu/qemu_agent.c | 50 ++++----- src/qemu/qemu_conf.c | 4 +- src/qemu/qemu_conf.h | 2 +- src/qemu/qemu_domain.c | 95 +++++++--------- src/qemu/qemu_domain.h | 4 +- src/qemu/qemu_driver.c | 21 ++-- src/qemu/qemu_hotplug.c | 28 ++--- src/qemu/qemu_hotplug.h | 2 +- src/qemu/qemu_migration.c | 2 +- src/qemu/qemu_monitor.c | 27 ++--- src/qemu/qemu_process.c | 4 +- src/remote/remote_daemon.h | 2 +- src/remote/remote_daemon_dispatch.c | 100 ++++------------- src/remote/remote_daemon_stream.c | 29 ++--- src/remote/remote_driver.c | 15 +-- src/rpc/virnetclient.c | 25 ++--- src/secret/secret_driver.c | 13 +-- src/storage/storage_driver.c | 11 +- src/test/test_driver.c | 12 +- src/util/virfdstream.c | 26 ++--- src/util/virfirewall.c | 6 +- src/util/virlockspace.c | 83 ++++---------- src/util/virlog.c | 9 +- src/util/virnetdevmacvlan.c | 20 ++-- src/util/virnetdevveth.c | 6 +- src/util/virnetlink.c | 15 +-- src/util/virnodesuspend.c | 6 +- src/util/virobject.c | 40 +++---- src/util/virobject.h | 10 +- src/util/virthread.c | 134 ---------------------- src/util/virthread.h | 57 ---------- src/util/virthreadpool.c | 149 +++++++++---------------- src/util/virtpm.c | 18 +-- src/vbox/vbox_common.c | 12 +- src/vmware/vmware_conf.c | 2 +- src/vmware/vmware_conf.h | 2 +- src/vmware/vmware_driver.c | 7 +- src/vz/vz_driver.c | 26 ++--- src/vz/vz_utils.c | 41 ++----- src/vz/vz_utils.h | 6 +- tests/qemuhotplugmock.c | 6 +- tests/qemuhotplugtest.c | 2 + tests/qemumonitortestutils.c | 42 +++---- tests/qemusecuritymock.c | 30 ++--- tests/qemusecuritytest.c | 10 +- tests/qemuxml2argvtest.c | 1 + tests/testutilslxc.c | 9 +- tests/testutilsqemu.c | 5 +- tests/testutilsxen.c | 9 +- tools/virsh-console.c | 23 +--- tools/virsh.c | 11 +- tools/virt-admin.c | 11 +- tools/vsh.c | 4 +- tools/vsh.h | 2 +- 106 files changed, 747 insertions(+), 1426 deletions(-) -- 2.25.2

Signed-off-by: Rafael Fonseca <r4f4rfs@gmail.com> --- src/admin/admin_server_dispatch.c | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/src/admin/admin_server_dispatch.c b/src/admin/admin_server_dispatch.c index b3da577995..2515528779 100644 --- a/src/admin/admin_server_dispatch.c +++ b/src/admin/admin_server_dispatch.c @@ -45,7 +45,7 @@ typedef daemonAdmClientPrivate *daemonAdmClientPrivatePtr; /* Separate private data for admin connection */ struct daemonAdmClientPrivate { /* Just a placeholder, not that there is anything to be locked */ - virMutex lock; + GMutex lock; virNetDaemonPtr dmn; }; @@ -55,7 +55,7 @@ remoteAdmClientFree(void *data) { struct daemonAdmClientPrivate *priv = data; - virMutexDestroy(&priv->lock); + g_mutex_clear(&priv->lock); virObjectUnref(priv->dmn); VIR_FREE(priv); } @@ -91,11 +91,7 @@ remoteAdmClientNew(virNetServerClientPtr client G_GNUC_UNUSED, if (VIR_ALLOC(priv) < 0) return NULL; - if (virMutexInit(&priv->lock) < 0) { - VIR_FREE(priv); - virReportSystemError(errno, "%s", _("unable to init mutex")); - return NULL; - } + g_mutex_init(&priv->lock); /* * We don't necessarily need to ref this object right now as there @@ -167,9 +163,9 @@ adminDispatchConnectOpen(virNetServerPtr server G_GNUC_UNUSED, struct daemonAdmClientPrivate *priv = virNetServerClientGetPrivateData(client); int ret = -1; + g_autoptr(GMutexLocker) locker = g_mutex_locker_new(&priv->lock); VIR_DEBUG("priv=%p dmn=%p", priv, priv->dmn); - virMutexLock(&priv->lock); flags = args->flags; virCheckFlagsGoto(0, cleanup); @@ -178,7 +174,6 @@ adminDispatchConnectOpen(virNetServerPtr server G_GNUC_UNUSED, cleanup: if (ret < 0) virNetMessageSaveError(rerr); - virMutexUnlock(&priv->lock); return ret; } -- 2.25.2

On Fri, Apr 10, 2020 at 03:54:28PM +0200, Rafael Fonseca wrote:
Signed-off-by: Rafael Fonseca <r4f4rfs@gmail.com> --- src/admin/admin_server_dispatch.c | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-)
diff --git a/src/admin/admin_server_dispatch.c b/src/admin/admin_server_dispatch.c index b3da577995..2515528779 100644 --- a/src/admin/admin_server_dispatch.c +++ b/src/admin/admin_server_dispatch.c @@ -45,7 +45,7 @@ typedef daemonAdmClientPrivate *daemonAdmClientPrivatePtr; /* Separate private data for admin connection */ struct daemonAdmClientPrivate { /* Just a placeholder, not that there is anything to be locked */ - virMutex lock; + GMutex lock;
virNetDaemonPtr dmn; }; @@ -55,7 +55,7 @@ remoteAdmClientFree(void *data) { struct daemonAdmClientPrivate *priv = data;
- virMutexDestroy(&priv->lock); + g_mutex_clear(&priv->lock); virObjectUnref(priv->dmn); VIR_FREE(priv); } @@ -91,11 +91,7 @@ remoteAdmClientNew(virNetServerClientPtr client G_GNUC_UNUSED, if (VIR_ALLOC(priv) < 0) return NULL;
- if (virMutexInit(&priv->lock) < 0) { - VIR_FREE(priv); - virReportSystemError(errno, "%s", _("unable to init mutex")); - return NULL; - } + g_mutex_init(&priv->lock);
/* * We don't necessarily need to ref this object right now as there @@ -167,9 +163,9 @@ adminDispatchConnectOpen(virNetServerPtr server G_GNUC_UNUSED, struct daemonAdmClientPrivate *priv = virNetServerClientGetPrivateData(client); int ret = -1; + g_autoptr(GMutexLocker) locker = g_mutex_locker_new(&priv->lock);
VIR_DEBUG("priv=%p dmn=%p", priv, priv->dmn); - virMutexLock(&priv->lock);
flags = args->flags; virCheckFlagsGoto(0, cleanup); @@ -178,7 +174,6 @@ adminDispatchConnectOpen(virNetServerPtr server G_GNUC_UNUSED, cleanup: if (ret < 0) virNetMessageSaveError(rerr); - virMutexUnlock(&priv->lock); return ret; }
-- 2.25.2
I was wondering why virMutexInit() returns a value whereas g_mutex_init() does not, to make sure there weren't any additional adjustments necessary, but it's probably OK. I mean, it does feel slightly dubious as virMutexInit() fails if pthread_mutex_init() fails which can happen under a bunch of seemingly fairly realistic scenarios. I assume g_mutex_init() ultimately calls pthread_mutex_init() as well so these scenarios should still apply. However, this seems ultimately a problem of glib API designers to decide how realistic the scenarios are (at least some of them seem to be related to memory allocation which glib solves by aborting) and whether to report them to their users, and they made the decision that they made, hopefully for good reasons. Reviewed-by: Pavel Mores <pmores@redhat.com>

On Tue, Apr 14, 2020 at 05:24:50PM +0200, Pavel Mores wrote:
On Fri, Apr 10, 2020 at 03:54:28PM +0200, Rafael Fonseca wrote:
Signed-off-by: Rafael Fonseca <r4f4rfs@gmail.com> --- src/admin/admin_server_dispatch.c | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-)
diff --git a/src/admin/admin_server_dispatch.c b/src/admin/admin_server_dispatch.c index b3da577995..2515528779 100644 --- a/src/admin/admin_server_dispatch.c +++ b/src/admin/admin_server_dispatch.c @@ -45,7 +45,7 @@ typedef daemonAdmClientPrivate *daemonAdmClientPrivatePtr; /* Separate private data for admin connection */ struct daemonAdmClientPrivate { /* Just a placeholder, not that there is anything to be locked */ - virMutex lock; + GMutex lock;
virNetDaemonPtr dmn; }; @@ -55,7 +55,7 @@ remoteAdmClientFree(void *data) { struct daemonAdmClientPrivate *priv = data;
- virMutexDestroy(&priv->lock); + g_mutex_clear(&priv->lock); virObjectUnref(priv->dmn); VIR_FREE(priv); } @@ -91,11 +91,7 @@ remoteAdmClientNew(virNetServerClientPtr client G_GNUC_UNUSED, if (VIR_ALLOC(priv) < 0) return NULL;
- if (virMutexInit(&priv->lock) < 0) { - VIR_FREE(priv); - virReportSystemError(errno, "%s", _("unable to init mutex")); - return NULL; - } + g_mutex_init(&priv->lock);
/* * We don't necessarily need to ref this object right now as there @@ -167,9 +163,9 @@ adminDispatchConnectOpen(virNetServerPtr server G_GNUC_UNUSED, struct daemonAdmClientPrivate *priv = virNetServerClientGetPrivateData(client); int ret = -1; + g_autoptr(GMutexLocker) locker = g_mutex_locker_new(&priv->lock);
VIR_DEBUG("priv=%p dmn=%p", priv, priv->dmn); - virMutexLock(&priv->lock);
flags = args->flags; virCheckFlagsGoto(0, cleanup); @@ -178,7 +174,6 @@ adminDispatchConnectOpen(virNetServerPtr server G_GNUC_UNUSED, cleanup: if (ret < 0) virNetMessageSaveError(rerr); - virMutexUnlock(&priv->lock); return ret; }
-- 2.25.2
I was wondering why virMutexInit() returns a value whereas g_mutex_init() does not, to make sure there weren't any additional adjustments necessary, but it's probably OK.
I mean, it does feel slightly dubious as virMutexInit() fails if pthread_mutex_init() fails which can happen under a bunch of seemingly fairly realistic scenarios. I assume g_mutex_init() ultimately calls pthread_mutex_init() as well so these scenarios should still apply.
However, this seems ultimately a problem of glib API designers to decide how realistic the scenarios are (at least some of them seem to be related to memory allocation which glib solves by aborting) and whether to report them to their users, and they made the decision that they made, hopefully for good reasons.
Honestly, none of the reasons mutex init can fail are especially interesting to callers. There's essentially nothing useful callers can do when a mutex init fails, as its symptomatic of much bigger problems. Thus I think abort'ing is a reasonable approach. Likewise for lock/unlock. Regards, Daniel -- |: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|

On Wed, Apr 15, 2020 at 01:55:34PM +0100, Daniel P. Berrangé wrote:
On Tue, Apr 14, 2020 at 05:24:50PM +0200, Pavel Mores wrote:
On Fri, Apr 10, 2020 at 03:54:28PM +0200, Rafael Fonseca wrote:
Signed-off-by: Rafael Fonseca <r4f4rfs@gmail.com> --- src/admin/admin_server_dispatch.c | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-)
diff --git a/src/admin/admin_server_dispatch.c b/src/admin/admin_server_dispatch.c index b3da577995..2515528779 100644 --- a/src/admin/admin_server_dispatch.c +++ b/src/admin/admin_server_dispatch.c @@ -45,7 +45,7 @@ typedef daemonAdmClientPrivate *daemonAdmClientPrivatePtr; /* Separate private data for admin connection */ struct daemonAdmClientPrivate { /* Just a placeholder, not that there is anything to be locked */ - virMutex lock; + GMutex lock;
virNetDaemonPtr dmn; }; @@ -55,7 +55,7 @@ remoteAdmClientFree(void *data) { struct daemonAdmClientPrivate *priv = data;
- virMutexDestroy(&priv->lock); + g_mutex_clear(&priv->lock); virObjectUnref(priv->dmn); VIR_FREE(priv); } @@ -91,11 +91,7 @@ remoteAdmClientNew(virNetServerClientPtr client G_GNUC_UNUSED, if (VIR_ALLOC(priv) < 0) return NULL;
- if (virMutexInit(&priv->lock) < 0) { - VIR_FREE(priv); - virReportSystemError(errno, "%s", _("unable to init mutex")); - return NULL; - } + g_mutex_init(&priv->lock);
/* * We don't necessarily need to ref this object right now as there @@ -167,9 +163,9 @@ adminDispatchConnectOpen(virNetServerPtr server G_GNUC_UNUSED, struct daemonAdmClientPrivate *priv = virNetServerClientGetPrivateData(client); int ret = -1; + g_autoptr(GMutexLocker) locker = g_mutex_locker_new(&priv->lock);
VIR_DEBUG("priv=%p dmn=%p", priv, priv->dmn); - virMutexLock(&priv->lock);
flags = args->flags; virCheckFlagsGoto(0, cleanup); @@ -178,7 +174,6 @@ adminDispatchConnectOpen(virNetServerPtr server G_GNUC_UNUSED, cleanup: if (ret < 0) virNetMessageSaveError(rerr); - virMutexUnlock(&priv->lock); return ret; }
-- 2.25.2
I was wondering why virMutexInit() returns a value whereas g_mutex_init() does not, to make sure there weren't any additional adjustments necessary, but it's probably OK.
I mean, it does feel slightly dubious as virMutexInit() fails if pthread_mutex_init() fails which can happen under a bunch of seemingly fairly realistic scenarios. I assume g_mutex_init() ultimately calls pthread_mutex_init() as well so these scenarios should still apply.
However, this seems ultimately a problem of glib API designers to decide how realistic the scenarios are (at least some of them seem to be related to memory allocation which glib solves by aborting) and whether to report them to their users, and they made the decision that they made, hopefully for good reasons.
Honestly, none of the reasons mutex init can fail are especially interesting to callers. There's essentially nothing useful callers can do when a mutex init fails, as its symptomatic of much bigger problems. Thus I think abort'ing is a reasonable approach. Likewise for lock/unlock.
Yes, agreed. At the risk of going off-topic, what makes me wonder is that phtread_mutex_init() POSIX manual page lists also EPERM among the reasons why the call might fail. I've never heard of that, let alone encountered it, and it might not actually be implemented by OS's in practice. However if EPERM can happen, then I guess that could be worth reporting to the user. Well it's not like we need to solve this problem on this list anyway... pvl

On Wed, Apr 15, 2020 at 04:11:30PM +0200, Pavel Mores wrote:
On Wed, Apr 15, 2020 at 01:55:34PM +0100, Daniel P. Berrangé wrote:
On Tue, Apr 14, 2020 at 05:24:50PM +0200, Pavel Mores wrote:
On Fri, Apr 10, 2020 at 03:54:28PM +0200, Rafael Fonseca wrote:
Signed-off-by: Rafael Fonseca <r4f4rfs@gmail.com> --- src/admin/admin_server_dispatch.c | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-)
diff --git a/src/admin/admin_server_dispatch.c b/src/admin/admin_server_dispatch.c index b3da577995..2515528779 100644 --- a/src/admin/admin_server_dispatch.c +++ b/src/admin/admin_server_dispatch.c @@ -45,7 +45,7 @@ typedef daemonAdmClientPrivate *daemonAdmClientPrivatePtr; /* Separate private data for admin connection */ struct daemonAdmClientPrivate { /* Just a placeholder, not that there is anything to be locked */ - virMutex lock; + GMutex lock;
virNetDaemonPtr dmn; }; @@ -55,7 +55,7 @@ remoteAdmClientFree(void *data) { struct daemonAdmClientPrivate *priv = data;
- virMutexDestroy(&priv->lock); + g_mutex_clear(&priv->lock); virObjectUnref(priv->dmn); VIR_FREE(priv); } @@ -91,11 +91,7 @@ remoteAdmClientNew(virNetServerClientPtr client G_GNUC_UNUSED, if (VIR_ALLOC(priv) < 0) return NULL;
- if (virMutexInit(&priv->lock) < 0) { - VIR_FREE(priv); - virReportSystemError(errno, "%s", _("unable to init mutex")); - return NULL; - } + g_mutex_init(&priv->lock);
/* * We don't necessarily need to ref this object right now as there @@ -167,9 +163,9 @@ adminDispatchConnectOpen(virNetServerPtr server G_GNUC_UNUSED, struct daemonAdmClientPrivate *priv = virNetServerClientGetPrivateData(client); int ret = -1; + g_autoptr(GMutexLocker) locker = g_mutex_locker_new(&priv->lock);
VIR_DEBUG("priv=%p dmn=%p", priv, priv->dmn); - virMutexLock(&priv->lock);
flags = args->flags; virCheckFlagsGoto(0, cleanup); @@ -178,7 +174,6 @@ adminDispatchConnectOpen(virNetServerPtr server G_GNUC_UNUSED, cleanup: if (ret < 0) virNetMessageSaveError(rerr); - virMutexUnlock(&priv->lock); return ret; }
-- 2.25.2
I was wondering why virMutexInit() returns a value whereas g_mutex_init() does not, to make sure there weren't any additional adjustments necessary, but it's probably OK.
I mean, it does feel slightly dubious as virMutexInit() fails if pthread_mutex_init() fails which can happen under a bunch of seemingly fairly realistic scenarios. I assume g_mutex_init() ultimately calls pthread_mutex_init() as well so these scenarios should still apply.
However, this seems ultimately a problem of glib API designers to decide how realistic the scenarios are (at least some of them seem to be related to memory allocation which glib solves by aborting) and whether to report them to their users, and they made the decision that they made, hopefully for good reasons.
Honestly, none of the reasons mutex init can fail are especially interesting to callers. There's essentially nothing useful callers can do when a mutex init fails, as its symptomatic of much bigger problems. Thus I think abort'ing is a reasonable approach. Likewise for lock/unlock.
Yes, agreed. At the risk of going off-topic, what makes me wonder is that phtread_mutex_init() POSIX manual page lists also EPERM among the reasons why the call might fail. I've never heard of that, let alone encountered it, and it might not actually be implemented by OS's in practice. However if EPERM can happen, then I guess that could be worth reporting to the user.
Any OS that is crazy enough to report EPERM for initializing mutex is not an OS I wish to support :-) Regards, Daniel -- |: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|

On Wed, Apr 15, 2020 at 03:58:55PM +0100, Daniel P. Berrangé wrote:
On Wed, Apr 15, 2020 at 04:11:30PM +0200, Pavel Mores wrote:
On Wed, Apr 15, 2020 at 01:55:34PM +0100, Daniel P. Berrangé wrote:
On Tue, Apr 14, 2020 at 05:24:50PM +0200, Pavel Mores wrote:
On Fri, Apr 10, 2020 at 03:54:28PM +0200, Rafael Fonseca wrote:
Signed-off-by: Rafael Fonseca <r4f4rfs@gmail.com> --- src/admin/admin_server_dispatch.c | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-)
diff --git a/src/admin/admin_server_dispatch.c b/src/admin/admin_server_dispatch.c index b3da577995..2515528779 100644 --- a/src/admin/admin_server_dispatch.c +++ b/src/admin/admin_server_dispatch.c @@ -45,7 +45,7 @@ typedef daemonAdmClientPrivate *daemonAdmClientPrivatePtr; /* Separate private data for admin connection */ struct daemonAdmClientPrivate { /* Just a placeholder, not that there is anything to be locked */ - virMutex lock; + GMutex lock;
virNetDaemonPtr dmn; }; @@ -55,7 +55,7 @@ remoteAdmClientFree(void *data) { struct daemonAdmClientPrivate *priv = data;
- virMutexDestroy(&priv->lock); + g_mutex_clear(&priv->lock); virObjectUnref(priv->dmn); VIR_FREE(priv); } @@ -91,11 +91,7 @@ remoteAdmClientNew(virNetServerClientPtr client G_GNUC_UNUSED, if (VIR_ALLOC(priv) < 0) return NULL;
- if (virMutexInit(&priv->lock) < 0) { - VIR_FREE(priv); - virReportSystemError(errno, "%s", _("unable to init mutex")); - return NULL; - } + g_mutex_init(&priv->lock);
/* * We don't necessarily need to ref this object right now as there @@ -167,9 +163,9 @@ adminDispatchConnectOpen(virNetServerPtr server G_GNUC_UNUSED, struct daemonAdmClientPrivate *priv = virNetServerClientGetPrivateData(client); int ret = -1; + g_autoptr(GMutexLocker) locker = g_mutex_locker_new(&priv->lock);
VIR_DEBUG("priv=%p dmn=%p", priv, priv->dmn); - virMutexLock(&priv->lock);
flags = args->flags; virCheckFlagsGoto(0, cleanup); @@ -178,7 +174,6 @@ adminDispatchConnectOpen(virNetServerPtr server G_GNUC_UNUSED, cleanup: if (ret < 0) virNetMessageSaveError(rerr); - virMutexUnlock(&priv->lock); return ret; }
-- 2.25.2
I was wondering why virMutexInit() returns a value whereas g_mutex_init() does not, to make sure there weren't any additional adjustments necessary, but it's probably OK.
I mean, it does feel slightly dubious as virMutexInit() fails if pthread_mutex_init() fails which can happen under a bunch of seemingly fairly realistic scenarios. I assume g_mutex_init() ultimately calls pthread_mutex_init() as well so these scenarios should still apply.
However, this seems ultimately a problem of glib API designers to decide how realistic the scenarios are (at least some of them seem to be related to memory allocation which glib solves by aborting) and whether to report them to their users, and they made the decision that they made, hopefully for good reasons.
Honestly, none of the reasons mutex init can fail are especially interesting to callers. There's essentially nothing useful callers can do when a mutex init fails, as its symptomatic of much bigger problems. Thus I think abort'ing is a reasonable approach. Likewise for lock/unlock.
Yes, agreed. At the risk of going off-topic, what makes me wonder is that phtread_mutex_init() POSIX manual page lists also EPERM among the reasons why the call might fail. I've never heard of that, let alone encountered it, and it might not actually be implemented by OS's in practice. However if EPERM can happen, then I guess that could be worth reporting to the user.
Any OS that is crazy enough to report EPERM for initializing mutex is not an OS I wish to support :-)
That seems fair enough. :-) pvl

Signed-off-by: Rafael Fonseca <r4f4rfs@gmail.com> --- src/bhyve/bhyve_driver.c | 11 ++++------- src/bhyve/bhyve_utils.h | 2 +- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/src/bhyve/bhyve_driver.c b/src/bhyve/bhyve_driver.c index b6204c7fb9..6e9a79cb52 100644 --- a/src/bhyve/bhyve_driver.c +++ b/src/bhyve/bhyve_driver.c @@ -73,13 +73,13 @@ bhyveConnPtr bhyve_driver = NULL; void bhyveDriverLock(bhyveConnPtr driver) { - virMutexLock(&driver->lock); + g_mutex_lock(&driver->lock); } void bhyveDriverUnlock(bhyveConnPtr driver) { - virMutexUnlock(&driver->lock); + g_mutex_unlock(&driver->lock); } static int @@ -1199,7 +1199,7 @@ bhyveStateCleanup(void) if (bhyve_driver->lockFD != -1) virPidFileRelease(BHYVE_STATE_DIR, "driver", bhyve_driver->lockFD); - virMutexDestroy(&bhyve_driver->lock); + g_mutex_clear(&bhyve_driver->lock); VIR_FREE(bhyve_driver); return 0; @@ -1228,10 +1228,7 @@ bhyveStateInitialize(bool privileged, return VIR_DRV_STATE_INIT_ERROR; bhyve_driver->lockFD = -1; - if (virMutexInit(&bhyve_driver->lock) < 0) { - VIR_FREE(bhyve_driver); - return VIR_DRV_STATE_INIT_ERROR; - } + g_mutex_init(&bhyve_driver->lock); if (!(bhyve_driver->closeCallbacks = virCloseCallbacksNew())) goto cleanup; diff --git a/src/bhyve/bhyve_utils.h b/src/bhyve/bhyve_utils.h index f3e80b6121..a92ecb48c4 100644 --- a/src/bhyve/bhyve_utils.h +++ b/src/bhyve/bhyve_utils.h @@ -44,7 +44,7 @@ struct _virBhyveDriverConfig { }; struct _bhyveConn { - virMutex lock; + GMutex lock; virBhyveDriverConfigPtr config; -- 2.25.2

On Fri, Apr 10, 2020 at 03:54:29PM +0200, Rafael Fonseca wrote:
Signed-off-by: Rafael Fonseca <r4f4rfs@gmail.com> --- src/bhyve/bhyve_driver.c | 11 ++++------- src/bhyve/bhyve_utils.h | 2 +- 2 files changed, 5 insertions(+), 8 deletions(-)
diff --git a/src/bhyve/bhyve_driver.c b/src/bhyve/bhyve_driver.c index b6204c7fb9..6e9a79cb52 100644 --- a/src/bhyve/bhyve_driver.c +++ b/src/bhyve/bhyve_driver.c @@ -73,13 +73,13 @@ bhyveConnPtr bhyve_driver = NULL; void bhyveDriverLock(bhyveConnPtr driver) { - virMutexLock(&driver->lock); + g_mutex_lock(&driver->lock); }
void bhyveDriverUnlock(bhyveConnPtr driver) { - virMutexUnlock(&driver->lock); + g_mutex_unlock(&driver->lock); }
static int @@ -1199,7 +1199,7 @@ bhyveStateCleanup(void) if (bhyve_driver->lockFD != -1) virPidFileRelease(BHYVE_STATE_DIR, "driver", bhyve_driver->lockFD);
- virMutexDestroy(&bhyve_driver->lock); + g_mutex_clear(&bhyve_driver->lock); VIR_FREE(bhyve_driver);
return 0; @@ -1228,10 +1228,7 @@ bhyveStateInitialize(bool privileged, return VIR_DRV_STATE_INIT_ERROR;
bhyve_driver->lockFD = -1; - if (virMutexInit(&bhyve_driver->lock) < 0) { - VIR_FREE(bhyve_driver); - return VIR_DRV_STATE_INIT_ERROR; - } + g_mutex_init(&bhyve_driver->lock);
if (!(bhyve_driver->closeCallbacks = virCloseCallbacksNew())) goto cleanup; diff --git a/src/bhyve/bhyve_utils.h b/src/bhyve/bhyve_utils.h index f3e80b6121..a92ecb48c4 100644 --- a/src/bhyve/bhyve_utils.h +++ b/src/bhyve/bhyve_utils.h @@ -44,7 +44,7 @@ struct _virBhyveDriverConfig { };
struct _bhyveConn { - virMutex lock; + GMutex lock;
virBhyveDriverConfigPtr config;
-- 2.25.2
By the way, the approach taken here with bhyveDriver{Lock,Unlock}() might make sense with the whole series - implement e.g. virMutexInit() in terms of g_mutex_init() in the first phase and only then replace the actual virMutexInit() calls if considered beneficial... Reviewed-by: Pavel Mores <pmores@redhat.com>

On Tue, Apr 14, 2020 at 6:06 PM Pavel Mores <pmores@redhat.com> wrote:
By the way, the approach taken here with bhyveDriver{Lock,Unlock}() might make sense with the whole series - implement e.g. virMutexInit() in terms of g_mutex_init() in the first phase and only then replace the actual virMutexInit() calls if considered beneficial...
So you mean one patch doing 's/virMutex/GMutex' and then inside virMutex*() we call the g_mutex_*() equivalent? And maybe make virMutex*() `inline`? Att. -- Rafael Fonseca

On Tue, Apr 14, 2020 at 07:05:03PM +0200, Rafael Fonseca wrote:
On Tue, Apr 14, 2020 at 6:06 PM Pavel Mores <pmores@redhat.com> wrote:
By the way, the approach taken here with bhyveDriver{Lock,Unlock}() might make sense with the whole series - implement e.g. virMutexInit() in terms of g_mutex_init() in the first phase and only then replace the actual virMutexInit() calls if considered beneficial...
So you mean one patch doing 's/virMutex/GMutex' and then inside virMutex*() we call the g_mutex_*() equivalent? And maybe make virMutex*() `inline`?
Yes - I mean, I'm not familiar enough with this to be sure off-hand that just doing a literal find & replace would work with no undesired side-effects, but conceptually yes, that's the idea. That's just a thought though - taking that approach would have broken the refactor into two more manageable & testable chunks but seeing as you've done the hard work already, there's no need to rework the series just because of me. :-) pvl

On Wed, Apr 15, 2020 at 02:15:35PM +0200, Pavel Mores wrote:
On Tue, Apr 14, 2020 at 07:05:03PM +0200, Rafael Fonseca wrote:
On Tue, Apr 14, 2020 at 6:06 PM Pavel Mores <pmores@redhat.com> wrote:
By the way, the approach taken here with bhyveDriver{Lock,Unlock}() might make sense with the whole series - implement e.g. virMutexInit() in terms of g_mutex_init() in the first phase and only then replace the actual virMutexInit() calls if considered beneficial...
So you mean one patch doing 's/virMutex/GMutex' and then inside virMutex*() we call the g_mutex_*() equivalent? And maybe make virMutex*() `inline`?
Yes - I mean, I'm not familiar enough with this to be sure off-hand that just doing a literal find & replace would work with no undesired side-effects, but conceptually yes, that's the idea.
That's just a thought though - taking that approach would have broken the refactor into two more manageable & testable chunks but seeing as you've done the hard work already, there's no need to rework the series just because of me. :-)
Replacing the virMutex calls with GMutex APis in all callers is the desirable approach. The goal of using GLib APIs is to remove any libvirt specfic APIs which duplicate GLib. Thus re-writing virMutex APIs impls to call GMutex is just delaying the desired end state where virMutex ceases to exist. Regards, Daniel -- |: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|

On Wed, Apr 15, 2020 at 01:59:46PM +0100, Daniel P. Berrangé wrote:
On Wed, Apr 15, 2020 at 02:15:35PM +0200, Pavel Mores wrote:
On Tue, Apr 14, 2020 at 07:05:03PM +0200, Rafael Fonseca wrote:
On Tue, Apr 14, 2020 at 6:06 PM Pavel Mores <pmores@redhat.com> wrote:
By the way, the approach taken here with bhyveDriver{Lock,Unlock}() might make sense with the whole series - implement e.g. virMutexInit() in terms of g_mutex_init() in the first phase and only then replace the actual virMutexInit() calls if considered beneficial...
So you mean one patch doing 's/virMutex/GMutex' and then inside virMutex*() we call the g_mutex_*() equivalent? And maybe make virMutex*() `inline`?
Yes - I mean, I'm not familiar enough with this to be sure off-hand that just doing a literal find & replace would work with no undesired side-effects, but conceptually yes, that's the idea.
That's just a thought though - taking that approach would have broken the refactor into two more manageable & testable chunks but seeing as you've done the hard work already, there's no need to rework the series just because of me. :-)
Replacing the virMutex calls with GMutex APis in all callers is the desirable approach. The goal of using GLib APIs is to remove any libvirt specfic APIs which duplicate GLib. Thus re-writing virMutex APIs impls to call GMutex is just delaying the desired end state where virMutex ceases to exist.
Sure, I agree with the goal, no question about that. I was just thinking about getting there in two steps (with no delay between them implied) which might have been at least easier to review. Also bisecting would be easier that way if we later suspect that there might have been an unexpected change of behaviour after all, possibly related to the migration to glib sync primitives. Anyway, the work has been pretty much done already so the point seems moot. Thanks, pvl

Signed-off-by: Rafael Fonseca <r4f4rfs@gmail.com> --- src/conf/nwfilter_ipaddrmap.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/conf/nwfilter_ipaddrmap.c b/src/conf/nwfilter_ipaddrmap.c index 672a58be3a..3c1927f9ff 100644 --- a/src/conf/nwfilter_ipaddrmap.c +++ b/src/conf/nwfilter_ipaddrmap.c @@ -32,7 +32,7 @@ #define VIR_FROM_THIS VIR_FROM_NWFILTER -static virMutex ipAddressMapLock = VIR_MUTEX_INITIALIZER; +G_LOCK_DEFINE_STATIC(ipAddressMapLock); static virHashTablePtr ipAddressMap; @@ -55,7 +55,7 @@ virNWFilterIPAddrMapAddIPAddr(const char *ifname, char *addr) addrCopy = g_strdup(addr); - virMutexLock(&ipAddressMapLock); + G_LOCK(ipAddressMapLock); val = virHashLookup(ipAddressMap, ifname); if (!val) { @@ -76,7 +76,7 @@ virNWFilterIPAddrMapAddIPAddr(const char *ifname, char *addr) ret = 0; cleanup: - virMutexUnlock(&ipAddressMapLock); + G_UNLOCK(ipAddressMapLock); VIR_FREE(addrCopy); return ret; @@ -102,7 +102,7 @@ virNWFilterIPAddrMapDelIPAddr(const char *ifname, const char *ipaddr) int ret = -1; virNWFilterVarValuePtr val = NULL; - virMutexLock(&ipAddressMapLock); + G_LOCK(ipAddressMapLock); if (ipaddr != NULL) { val = virHashLookup(ipAddressMap, ifname); @@ -121,7 +121,7 @@ virNWFilterIPAddrMapDelIPAddr(const char *ifname, const char *ipaddr) ret = 0; } - virMutexUnlock(&ipAddressMapLock); + G_UNLOCK(ipAddressMapLock); return ret; } @@ -137,11 +137,11 @@ virNWFilterIPAddrMapGetIPAddr(const char *ifname) { virNWFilterVarValuePtr res; - virMutexLock(&ipAddressMapLock); + G_LOCK(ipAddressMapLock); res = virHashLookup(ipAddressMap, ifname); - virMutexUnlock(&ipAddressMapLock); + G_UNLOCK(ipAddressMapLock); return res; } -- 2.25.2

On 4/10/20 9:54 AM, Rafael Fonseca wrote:
Signed-off-by: Rafael Fonseca <r4f4rfs@gmail.com> --- src/conf/nwfilter_ipaddrmap.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-)
diff --git a/src/conf/nwfilter_ipaddrmap.c b/src/conf/nwfilter_ipaddrmap.c index 672a58be3a..3c1927f9ff 100644 --- a/src/conf/nwfilter_ipaddrmap.c +++ b/src/conf/nwfilter_ipaddrmap.c @@ -32,7 +32,7 @@
#define VIR_FROM_THIS VIR_FROM_NWFILTER
-static virMutex ipAddressMapLock = VIR_MUTEX_INITIALIZER; +G_LOCK_DEFINE_STATIC(ipAddressMapLock);
The documentation for the G_LOCK() macros says that the name provided in the args will be mangled, so you can just use the same name as the object you're going to lock, e.g.: G_LOCK_DEFINE_STATIC(ipAddressMap); instead of "ipAddressMapLock". The upside is that's less typing, and if you do a keyword search you'll find all the places where ipAddressMap is locked/unlocked along with its uses. The downside would be that you couldn't as easily do a keyword search for just the lock manipulation (you'd need to use a regex to search for "G_.*ipAddressMap" or something like that); I still kind of like the idea of using the exact same name, just because it encourages consistency. Beyond that, why not just use the G_*() macros for *all* locks in libvirt instead of only using them in cases of static locks? It seems strange to use the convenience macros in some cases and the raw functions in others. (I'm looking at this with 0 experience using the Glib locks, so maybe there's something basic I'm just not aware of - maybe something necessary is missing from the G_LOCK() macros?).

On 4/10/20 1:01 PM, Laine Stump wrote:
On 4/10/20 9:54 AM, Rafael Fonseca wrote:
Signed-off-by: Rafael Fonseca <r4f4rfs@gmail.com> --- src/conf/nwfilter_ipaddrmap.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-)
diff --git a/src/conf/nwfilter_ipaddrmap.c b/src/conf/nwfilter_ipaddrmap.c index 672a58be3a..3c1927f9ff 100644 --- a/src/conf/nwfilter_ipaddrmap.c +++ b/src/conf/nwfilter_ipaddrmap.c @@ -32,7 +32,7 @@ #define VIR_FROM_THIS VIR_FROM_NWFILTER -static virMutex ipAddressMapLock = VIR_MUTEX_INITIALIZER; +G_LOCK_DEFINE_STATIC(ipAddressMapLock);
The documentation for the G_LOCK() macros says that the name provided in the args will be mangled, so you can just use the same name as the object you're going to lock, e.g.:
G_LOCK_DEFINE_STATIC(ipAddressMap);
instead of "ipAddressMapLock". The upside is that's less typing, and if you do a keyword search you'll find all the places where ipAddressMap is locked/unlocked along with its uses. The downside would be that you couldn't as easily do a keyword search for just the lock manipulation (you'd need to use a regex to search for "G_.*ipAddressMap" or something like that); I still kind of like the idea of using the exact same name, just because it encourages consistency.
Beyond that, why not just use the G_*() macros for *all* locks in libvirt instead of only using them in cases of static locks? It seems strange to use the convenience macros in some cases and the raw functions in others. (I'm looking at this with 0 experience using the Glib locks, so maybe there's something basic I'm just not aware of - maybe something necessary is missing from the G_LOCK() macros?).
Okay, I already see that the G_LOCK macros don't cover everything - no recursive mutexes and no RW mutexes for example. Too bad - it would be good to be consistent.

On Fri, Apr 10, 2020 at 7:06 PM Laine Stump <laine@laine.org> wrote:
Beyond that, why not just use the G_*() macros for *all* locks in libvirt instead of only using them in cases of static locks? It seems strange to use the convenience macros in some cases and the raw functions in others. (I'm looking at this with 0 experience using the Glib locks, so maybe there's something basic I'm just not aware of - maybe something necessary is missing from the G_LOCK() macros?).
Okay, I already see that the G_LOCK macros don't cover everything - no recursive mutexes and no RW mutexes for example. Too bad - it would be good to be consistent.
Yes, that's one issue. Another is: how do you use those macros with locks inside structs? You can't do `G_LOCK(obj->parent.lock)` because it'll result in `g_mutex_lock(&g__obj->parent.lock_lock)` which is wrong. You'd have to use the raw function `g_mutex_lock(obj->parent.LOCK_NAME(lock))` anyway, which imho, is even worse than `g_mutex_lock(&obj->parent.lock)`. The same issue happens when using mutexes with conditions: `g_cond_wait(cond, obj->parent.LOCK_NAME(lock)) ` instead of just `g_cond_wait(cond, obj->parent.lock)`. So they work better for statically-defined locks I don't mind doing whichever you guys prefer, just let me know. Att. -- Rafael Fonseca

On Sat, Apr 11, 2020 at 08:15:15PM +0200, Rafael Fonseca wrote:
On Fri, Apr 10, 2020 at 7:06 PM Laine Stump <laine@laine.org> wrote:
Beyond that, why not just use the G_*() macros for *all* locks in libvirt instead of only using them in cases of static locks? It seems strange to use the convenience macros in some cases and the raw functions in others. (I'm looking at this with 0 experience using the Glib locks, so maybe there's something basic I'm just not aware of - maybe something necessary is missing from the G_LOCK() macros?).
Okay, I already see that the G_LOCK macros don't cover everything - no recursive mutexes and no RW mutexes for example. Too bad - it would be good to be consistent.
Yes, that's one issue. Another is: how do you use those macros with locks inside structs? You can't do `G_LOCK(obj->parent.lock)` because it'll result in `g_mutex_lock(&g__obj->parent.lock_lock)` which is wrong. You'd have to use the raw function `g_mutex_lock(obj->parent.LOCK_NAME(lock))` anyway, which imho, is even worse than `g_mutex_lock(&obj->parent.lock)`. The same issue happens when using mutexes with conditions: `g_cond_wait(cond, obj->parent.LOCK_NAME(lock)) ` instead of just `g_cond_wait(cond, obj->parent.lock)`. So they work better for statically-defined locks
I don't mind doing whichever you guys prefer, just let me know.
Looking at the source code, the name mangling is pretty much all that the G_LOCK_DEFINE* macros do. So - besides some logging - the only advantage to using them is that you don't have to mangle the lock names manually and can use names of existing variables as the macros' arguments. Considering the above, I'd say either use the macros and don't mangle the lock names in their arguments manually, or don't use the macros. If consistent style is a priority I'd lean towards raw functions - unlike the macros, they can be used everywhere, and having to mangle the lock names by hand doesn't seem a huge burden to me. We do loose the logging that the macros do but in my experience, mutex logging often doesn't turn out as useful in practice as it might first appear... pvl

On 4/15/20 8:43 AM, Pavel Mores wrote:
On Sat, Apr 11, 2020 at 08:15:15PM +0200, Rafael Fonseca wrote:
On Fri, Apr 10, 2020 at 7:06 PM Laine Stump <laine@laine.org> wrote:
Beyond that, why not just use the G_*() macros for *all* locks in libvirt instead of only using them in cases of static locks? It seems strange to use the convenience macros in some cases and the raw functions in others. (I'm looking at this with 0 experience using the Glib locks, so maybe there's something basic I'm just not aware of - maybe something necessary is missing from the G_LOCK() macros?).
Okay, I already see that the G_LOCK macros don't cover everything - no recursive mutexes and no RW mutexes for example. Too bad - it would be good to be consistent. Yes, that's one issue. Another is: how do you use those macros with locks inside structs? You can't do `G_LOCK(obj->parent.lock)` because it'll result in `g_mutex_lock(&g__obj->parent.lock_lock)` which is wrong. You'd have to use the raw function `g_mutex_lock(obj->parent.LOCK_NAME(lock))` anyway, which imho, is even worse than `g_mutex_lock(&obj->parent.lock)`. The same issue happens when using mutexes with conditions: `g_cond_wait(cond, obj->parent.LOCK_NAME(lock)) ` instead of just `g_cond_wait(cond, obj->parent.lock)`. So they work better for statically-defined locks
I don't mind doing whichever you guys prefer, just let me know. Looking at the source code, the name mangling is pretty much all that the G_LOCK_DEFINE* macros do. So - besides some logging - the only advantage to using them is that you don't have to mangle the lock names manually and can use names of existing variables as the macros' arguments.
Considering the above, I'd say either use the macros and don't mangle the lock names in their arguments manually, or don't use the macros. If consistent style is a priority I'd lean towards raw functions
I agree with this - consistency is king :-)
- unlike the macros, they can be used everywhere, and having to mangle the lock names by hand doesn't seem a huge burden to me. We do loose the logging that the macros do but in my experience, mutex logging often doesn't turn out as useful in practice as it might first appear...
pvl

On Fri, Apr 10, 2020 at 7:01 PM Laine Stump <laine@laine.org> wrote:
On 4/10/20 9:54 AM, Rafael Fonseca wrote:
Signed-off-by: Rafael Fonseca <r4f4rfs@gmail.com> --- src/conf/nwfilter_ipaddrmap.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-)
diff --git a/src/conf/nwfilter_ipaddrmap.c b/src/conf/nwfilter_ipaddrmap.c index 672a58be3a..3c1927f9ff 100644 --- a/src/conf/nwfilter_ipaddrmap.c +++ b/src/conf/nwfilter_ipaddrmap.c @@ -32,7 +32,7 @@
#define VIR_FROM_THIS VIR_FROM_NWFILTER
-static virMutex ipAddressMapLock = VIR_MUTEX_INITIALIZER; +G_LOCK_DEFINE_STATIC(ipAddressMapLock);
The documentation for the G_LOCK() macros says that the name provided in the args will be mangled, so you can just use the same name as the object you're going to lock, e.g.:
G_LOCK_DEFINE_STATIC(ipAddressMap);
instead of "ipAddressMapLock". The upside is that's less typing, and if you do a keyword search you'll find all the places where ipAddressMap is locked/unlocked along with its uses. The downside would be that you couldn't as easily do a keyword search for just the lock manipulation (you'd need to use a regex to search for "G_.*ipAddressMap" or something like that); I still kind of like the idea of using the exact same name, just because it encourages consistency.
Ok I can do that. Att. -- Rafael Fonseca

Signed-off-by: Rafael Fonseca <r4f4rfs@gmail.com> --- src/conf/virchrdev.c | 27 +++++++-------------------- 1 file changed, 7 insertions(+), 20 deletions(-) diff --git a/src/conf/virchrdev.c b/src/conf/virchrdev.c index 800e82869e..8280f8e188 100644 --- a/src/conf/virchrdev.c +++ b/src/conf/virchrdev.c @@ -44,7 +44,7 @@ VIR_LOG_INIT("conf.chrdev"); /* structure holding information about character devices * open in a given domain */ struct _virChrdevs { - virMutex lock; + GMutex lock; virHashTablePtr hash; }; @@ -238,12 +238,10 @@ static void virChrdevFDStreamCloseCb(virStreamPtr st G_GNUC_UNUSED, void *opaque) { virChrdevStreamInfoPtr priv = opaque; - virMutexLock(&priv->devs->lock); + g_autoptr(GMutexLocker) locker = g_mutex_locker_new(&priv->devs->lock); /* remove entry from hash */ virHashRemoveEntry(priv->devs->hash, priv->path); - - virMutexUnlock(&priv->devs->lock); } /** @@ -258,12 +256,7 @@ virChrdevsPtr virChrdevAlloc(void) if (VIR_ALLOC(devs) < 0) return NULL; - if (virMutexInit(&devs->lock) < 0) { - virReportSystemError(errno, "%s", - _("Unable to init device stream mutex")); - VIR_FREE(devs); - return NULL; - } + g_mutex_init(&devs->lock); /* there will hardly be any devices most of the time, the hash * does not have to be huge */ @@ -299,11 +292,11 @@ void virChrdevFree(virChrdevsPtr devs) if (!devs) return; - virMutexLock(&devs->lock); + g_mutex_lock(&devs->lock); virHashForEach(devs->hash, virChrdevFreeClearCallbacks, NULL); virHashFree(devs->hash); - virMutexUnlock(&devs->lock); - virMutexDestroy(&devs->lock); + g_mutex_unlock(&devs->lock); + g_mutex_clear(&devs->lock); VIR_FREE(devs); } @@ -334,6 +327,7 @@ int virChrdevOpen(virChrdevsPtr devs, char *path; int ret; bool added = false; + g_autoptr(GMutexLocker) locker = g_mutex_locker_new(&devs->lock); switch (source->type) { case VIR_DOMAIN_CHR_TYPE_PTY: @@ -354,12 +348,9 @@ int virChrdevOpen(virChrdevsPtr devs, return -1; } - virMutexLock(&devs->lock); - if ((ent = virHashLookup(devs->hash, path))) { if (!force) { /* entry found, device is busy */ - virMutexUnlock(&devs->lock); return 1; } else { /* terminate existing connection */ @@ -378,13 +369,11 @@ int virChrdevOpen(virChrdevsPtr devs, /* create the lock file */ if ((ret = virChrdevLockFileCreate(path)) < 0) { - virMutexUnlock(&devs->lock); return ret; } /* obtain a reference to the stream */ if (virStreamRef(st) < 0) { - virMutexUnlock(&devs->lock); return -1; } @@ -428,7 +417,6 @@ int virChrdevOpen(virChrdevsPtr devs, cbdata, virChrdevFDStreamCloseCbFree); - virMutexUnlock(&devs->lock); return 0; error: @@ -440,7 +428,6 @@ int virChrdevOpen(virChrdevsPtr devs, if (cbdata) VIR_FREE(cbdata->path); VIR_FREE(cbdata); - virMutexUnlock(&devs->lock); virChrdevHashEntryFree(ent); return -1; } -- 2.25.2

On Fri, Apr 10, 2020 at 03:54:31PM +0200, Rafael Fonseca wrote:
Signed-off-by: Rafael Fonseca <r4f4rfs@gmail.com> --- src/conf/virchrdev.c | 27 +++++++-------------------- 1 file changed, 7 insertions(+), 20 deletions(-)
diff --git a/src/conf/virchrdev.c b/src/conf/virchrdev.c index 800e82869e..8280f8e188 100644 --- a/src/conf/virchrdev.c +++ b/src/conf/virchrdev.c @@ -44,7 +44,7 @@ VIR_LOG_INIT("conf.chrdev"); /* structure holding information about character devices * open in a given domain */ struct _virChrdevs { - virMutex lock; + GMutex lock; virHashTablePtr hash; };
@@ -238,12 +238,10 @@ static void virChrdevFDStreamCloseCb(virStreamPtr st G_GNUC_UNUSED, void *opaque) { virChrdevStreamInfoPtr priv = opaque; - virMutexLock(&priv->devs->lock); + g_autoptr(GMutexLocker) locker = g_mutex_locker_new(&priv->devs->lock);
/* remove entry from hash */ virHashRemoveEntry(priv->devs->hash, priv->path); - - virMutexUnlock(&priv->devs->lock); }
/** @@ -258,12 +256,7 @@ virChrdevsPtr virChrdevAlloc(void) if (VIR_ALLOC(devs) < 0) return NULL;
- if (virMutexInit(&devs->lock) < 0) { - virReportSystemError(errno, "%s", - _("Unable to init device stream mutex")); - VIR_FREE(devs); - return NULL; - } + g_mutex_init(&devs->lock);
/* there will hardly be any devices most of the time, the hash * does not have to be huge */ @@ -299,11 +292,11 @@ void virChrdevFree(virChrdevsPtr devs) if (!devs) return;
- virMutexLock(&devs->lock); + g_mutex_lock(&devs->lock); virHashForEach(devs->hash, virChrdevFreeClearCallbacks, NULL); virHashFree(devs->hash); - virMutexUnlock(&devs->lock); - virMutexDestroy(&devs->lock); + g_mutex_unlock(&devs->lock); + g_mutex_clear(&devs->lock);
VIR_FREE(devs); } @@ -334,6 +327,7 @@ int virChrdevOpen(virChrdevsPtr devs, char *path; int ret; bool added = false; + g_autoptr(GMutexLocker) locker = g_mutex_locker_new(&devs->lock);
switch (source->type) { case VIR_DOMAIN_CHR_TYPE_PTY: @@ -354,12 +348,9 @@ int virChrdevOpen(virChrdevsPtr devs, return -1; }
- virMutexLock(&devs->lock); - if ((ent = virHashLookup(devs->hash, path))) { if (!force) { /* entry found, device is busy */ - virMutexUnlock(&devs->lock); return 1; } else { /* terminate existing connection */ @@ -378,13 +369,11 @@ int virChrdevOpen(virChrdevsPtr devs,
/* create the lock file */ if ((ret = virChrdevLockFileCreate(path)) < 0) { - virMutexUnlock(&devs->lock); return ret; }
/* obtain a reference to the stream */ if (virStreamRef(st) < 0) { - virMutexUnlock(&devs->lock); return -1; }
@@ -428,7 +417,6 @@ int virChrdevOpen(virChrdevsPtr devs, cbdata, virChrdevFDStreamCloseCbFree);
- virMutexUnlock(&devs->lock); return 0;
error: @@ -440,7 +428,6 @@ int virChrdevOpen(virChrdevsPtr devs, if (cbdata) VIR_FREE(cbdata->path); VIR_FREE(cbdata); - virMutexUnlock(&devs->lock); virChrdevHashEntryFree(ent); return -1; } -- 2.25.2
Reviewed-by: Pavel Mores <pmores@redhat.com>

Signed-off-by: Rafael Fonseca <r4f4rfs@gmail.com> --- src/esx/esx_stream.c | 25 +++++++--------- src/esx/esx_vi.c | 68 +++++++++++++++----------------------------- src/esx/esx_vi.h | 6 ++-- 3 files changed, 36 insertions(+), 63 deletions(-) diff --git a/src/esx/esx_stream.c b/src/esx/esx_stream.c index fe3c42ae02..474c0f4739 100644 --- a/src/esx/esx_stream.c +++ b/src/esx/esx_stream.c @@ -203,6 +203,7 @@ esxStreamSend(virStreamPtr stream, const char *data, size_t nbytes) int result = -1; esxStreamPrivate *priv = stream->privateData; int status; + g_autoptr(GMutexLocker) locker = NULL; if (nbytes == 0) return 0; @@ -217,7 +218,7 @@ esxStreamSend(virStreamPtr stream, const char *data, size_t nbytes) return -1; } - virMutexLock(&priv->curl->lock); + locker = g_mutex_locker_new(&priv->curl->lock); priv->buffer = (char *)data; priv->buffer_size = nbytes; @@ -225,7 +226,7 @@ esxStreamSend(virStreamPtr stream, const char *data, size_t nbytes) if (stream->flags & VIR_STREAM_NONBLOCK) { if (esxStreamTransfer(priv, false) < 0) - goto cleanup; + return -1; if (priv->buffer_used < priv->buffer_size) result = priv->buffer_size - priv->buffer_used; @@ -236,7 +237,7 @@ esxStreamSend(virStreamPtr stream, const char *data, size_t nbytes) status = esxStreamTransfer(priv, true); if (status < 0) - goto cleanup; + return -1; if (status > 0) break; @@ -245,9 +246,6 @@ esxStreamSend(virStreamPtr stream, const char *data, size_t nbytes) result = priv->buffer_size - priv->buffer_used; } - cleanup: - virMutexUnlock(&priv->curl->lock); - return result; } @@ -260,6 +258,7 @@ esxStreamRecvFlags(virStreamPtr stream, int result = -1; esxStreamPrivate *priv = stream->privateData; int status; + g_autoptr(GMutexLocker) locker = NULL; virCheckFlags(0, -1); @@ -276,7 +275,7 @@ esxStreamRecvFlags(virStreamPtr stream, return -1; } - virMutexLock(&priv->curl->lock); + locker = g_mutex_locker_new(&priv->curl->lock); priv->buffer = data; priv->buffer_size = nbytes; @@ -296,7 +295,7 @@ esxStreamRecvFlags(virStreamPtr stream, result = priv->buffer_used; } else if (stream->flags & VIR_STREAM_NONBLOCK) { if (esxStreamTransfer(priv, false) < 0) - goto cleanup; + return -1; if (priv->buffer_used > 0) result = priv->buffer_used; @@ -307,7 +306,7 @@ esxStreamRecvFlags(virStreamPtr stream, status = esxStreamTransfer(priv, true); if (status < 0) - goto cleanup; + return -1; if (status > 0) break; @@ -316,9 +315,6 @@ esxStreamRecvFlags(virStreamPtr stream, result = priv->buffer_used; } - cleanup: - virMutexUnlock(&priv->curl->lock); - return result; } @@ -346,11 +342,12 @@ esxStreamClose(virStreamPtr stream, bool finish) { int result = 0; esxStreamPrivate *priv = stream->privateData; + g_autoptr(GMutexLocker) locker = NULL; if (!priv) return 0; - virMutexLock(&priv->curl->lock); + locker = g_mutex_locker_new(&priv->curl->lock); if (finish && priv->backlog_used > 0) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", @@ -360,8 +357,6 @@ esxStreamClose(virStreamPtr stream, bool finish) stream->privateData = NULL; - virMutexUnlock(&priv->curl->lock); - esxFreeStreamPrivate(&priv); return result; diff --git a/src/esx/esx_vi.c b/src/esx/esx_vi.c index 16690edfbe..ed6c6c28cd 100644 --- a/src/esx/esx_vi.c +++ b/src/esx/esx_vi.c @@ -112,7 +112,7 @@ ESX_VI__TEMPLATE__FREE(CURL, if (item->headers) curl_slist_free_all(item->headers); - virMutexDestroy(&item->lock); + g_mutex_clear(&item->lock); }) static size_t @@ -356,11 +356,7 @@ esxVI_CURL_Connect(esxVI_CURL *curl, esxUtil_ParsedUri *parsedUri) parsedUri->proxy_port); } - if (virMutexInit(&curl->lock) < 0) { - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", - _("Could not initialize CURL mutex")); - return -1; - } + g_mutex_init(&curl->lock); return 0; } @@ -392,7 +388,7 @@ esxVI_CURL_Download(esxVI_CURL *curl, const char *url, char **content, range = g_strdup_printf("%llu-", offset); } - virMutexLock(&curl->lock); + g_mutex_lock(&curl->lock); curl_easy_setopt(curl->handle, CURLOPT_URL, url); curl_easy_setopt(curl->handle, CURLOPT_RANGE, range); @@ -402,7 +398,7 @@ esxVI_CURL_Download(esxVI_CURL *curl, const char *url, char **content, responseCode = esxVI_CURL_Perform(curl, url); - virMutexUnlock(&curl->lock); + g_mutex_unlock(&curl->lock); if (responseCode < 0) { goto cleanup; @@ -433,14 +429,13 @@ int esxVI_CURL_Upload(esxVI_CURL *curl, const char *url, const char *content) { int responseCode = 0; + g_autoptr(GMutexLocker) locker = g_mutex_locker_new(&curl->lock); if (!content) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument")); return -1; } - virMutexLock(&curl->lock); - curl_easy_setopt(curl->handle, CURLOPT_URL, url); curl_easy_setopt(curl->handle, CURLOPT_RANGE, NULL); curl_easy_setopt(curl->handle, CURLOPT_READDATA, &content); @@ -449,8 +444,6 @@ esxVI_CURL_Upload(esxVI_CURL *curl, const char *url, const char *content) responseCode = esxVI_CURL_Perform(curl, url); - virMutexUnlock(&curl->lock); - if (responseCode < 0) { return -1; } else if (responseCode != 200 && responseCode != 201) { @@ -494,7 +487,7 @@ esxVI_SharedCURL_Lock(CURL *handle G_GNUC_UNUSED, curl_lock_data data, return; } - virMutexLock(&shared->locks[i]); + g_mutex_lock(&shared->locks[i]); } static void @@ -522,7 +515,7 @@ esxVI_SharedCURL_Unlock(CURL *handle G_GNUC_UNUSED, curl_lock_data data, return; } - virMutexUnlock(&shared->locks[i]); + g_mutex_unlock(&shared->locks[i]); } /* esxVI_SharedCURL_Alloc */ @@ -543,7 +536,7 @@ ESX_VI__TEMPLATE__FREE(SharedCURL, curl_share_cleanup(item->handle); for (i = 0; i < G_N_ELEMENTS(item->locks); ++i) - virMutexDestroy(&item->locks[i]); + g_mutex_clear(&item->locks[i]); }) int @@ -583,22 +576,18 @@ esxVI_SharedCURL_Add(esxVI_SharedCURL *shared, esxVI_CURL *curl) CURL_LOCK_DATA_DNS); for (i = 0; i < G_N_ELEMENTS(shared->locks); ++i) { - if (virMutexInit(&shared->locks[i]) < 0) { - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", - _("Could not initialize a CURL (share) mutex")); - return -1; - } + g_mutex_init(&shared->locks[i]); } } - virMutexLock(&curl->lock); + g_mutex_lock(&curl->lock); curl_easy_setopt(curl->handle, CURLOPT_SHARE, shared->handle); curl->shared = shared; ++shared->count; - virMutexUnlock(&curl->lock); + g_mutex_unlock(&curl->lock); return 0; } @@ -606,6 +595,8 @@ esxVI_SharedCURL_Add(esxVI_SharedCURL *shared, esxVI_CURL *curl) int esxVI_SharedCURL_Remove(esxVI_SharedCURL *shared, esxVI_CURL *curl) { + g_autoptr(GMutexLocker) locker = g_mutex_locker_new(&curl->lock); + if (!curl->handle) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Cannot unshare uninitialized CURL handle")); @@ -623,15 +614,11 @@ esxVI_SharedCURL_Remove(esxVI_SharedCURL *shared, esxVI_CURL *curl) return -1; } - virMutexLock(&curl->lock); - curl_easy_setopt(curl->handle, CURLOPT_SHARE, NULL); curl->shared = NULL; --shared->count; - virMutexUnlock(&curl->lock); - return 0; } @@ -661,6 +648,8 @@ ESX_VI__TEMPLATE__FREE(MultiCURL, int esxVI_MultiCURL_Add(esxVI_MultiCURL *multi, esxVI_CURL *curl) { + g_autoptr(GMutexLocker) locker = g_mutex_locker_new(&curl->lock); + if (!curl->handle) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Cannot add uninitialized CURL handle to a multi handle")); @@ -684,21 +673,19 @@ esxVI_MultiCURL_Add(esxVI_MultiCURL *multi, esxVI_CURL *curl) } - virMutexLock(&curl->lock); - curl_multi_add_handle(multi->handle, curl->handle); curl->multi = multi; ++multi->count; - virMutexUnlock(&curl->lock); - return 0; } int esxVI_MultiCURL_Remove(esxVI_MultiCURL *multi, esxVI_CURL *curl) { + g_autoptr(GMutexLocker) locker = g_mutex_locker_new(&curl->lock); + if (!curl->handle) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Cannot remove uninitialized CURL handle from a " @@ -718,15 +705,11 @@ esxVI_MultiCURL_Remove(esxVI_MultiCURL *multi, esxVI_CURL *curl) return -1; } - virMutexLock(&curl->lock); - curl_multi_remove_handle(multi->handle, curl->handle); curl->multi = NULL; --multi->count; - virMutexUnlock(&curl->lock); - return 0; } @@ -809,7 +792,7 @@ ESX_VI__TEMPLATE__ALLOC(Context) ESX_VI__TEMPLATE__FREE(Context, { if (item->sessionLock) - virMutexDestroy(item->sessionLock); + g_mutex_clear(item->sessionLock); esxVI_CURL_Free(&item->curl); VIR_FREE(item->url); @@ -870,11 +853,7 @@ esxVI_Context_Connect(esxVI_Context *ctx, const char *url, goto cleanup; - if (virMutexInit(ctx->sessionLock) < 0) { - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", - _("Could not initialize session mutex")); - goto cleanup; - } + g_mutex_init(ctx->sessionLock); if (esxVI_RetrieveServiceContent(ctx, &ctx->service) < 0) goto cleanup; @@ -1262,7 +1241,7 @@ esxVI_Context_Execute(esxVI_Context *ctx, const char *methodName, if (esxVI_Response_Alloc(response) < 0) return -1; - virMutexLock(&ctx->curl->lock); + g_mutex_lock(&ctx->curl->lock); curl_easy_setopt(ctx->curl->handle, CURLOPT_URL, ctx->url); curl_easy_setopt(ctx->curl->handle, CURLOPT_RANGE, NULL); @@ -1273,7 +1252,7 @@ esxVI_Context_Execute(esxVI_Context *ctx, const char *methodName, (*response)->responseCode = esxVI_CURL_Perform(ctx->curl, ctx->url); - virMutexUnlock(&ctx->curl->lock); + g_mutex_unlock(&ctx->curl->lock); if ((*response)->responseCode < 0) goto cleanup; @@ -1908,13 +1887,14 @@ esxVI_EnsureSession(esxVI_Context *ctx) esxVI_DynamicProperty *dynamicProperty = NULL; esxVI_UserSession *currentSession = NULL; char *escapedPassword = NULL; + g_autoptr(GMutexLocker) locker = NULL; if (!ctx->sessionLock) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid call, no mutex")); return -1; } - virMutexLock(ctx->sessionLock); + locker = g_mutex_locker_new(ctx->sessionLock); if (!ctx->session) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid call, no session")); @@ -1969,8 +1949,6 @@ esxVI_EnsureSession(esxVI_Context *ctx) result = 0; cleanup: - virMutexUnlock(ctx->sessionLock); - VIR_FREE(escapedPassword); esxVI_String_Free(&propertyNameList); esxVI_ObjectContent_Free(&sessionManager); diff --git a/src/esx/esx_vi.h b/src/esx/esx_vi.h index b960c0900a..949f90a43d 100644 --- a/src/esx/esx_vi.h +++ b/src/esx/esx_vi.h @@ -115,7 +115,7 @@ struct _esxVI_ParsedHostCpuIdInfo { struct _esxVI_CURL { CURL *handle; - virMutex lock; + GMutex lock; struct curl_slist *headers; char error[CURL_ERROR_SIZE]; esxVI_SharedCURL *shared; @@ -137,7 +137,7 @@ int esxVI_CURL_Upload(esxVI_CURL *curl, const char *url, const char *content); struct _esxVI_SharedCURL { CURLSH *handle; - virMutex locks[3]; /* share, cookie, dns */ + GMutex locks[3]; /* share, cookie, dns */ size_t count; /* number of added easy handle */ }; @@ -184,7 +184,7 @@ struct _esxVI_Context { esxVI_ProductLine productLine; unsigned long productVersion; /* = 1000000 * major + 1000 * minor + micro */ esxVI_UserSession *session; /* ... except the session ... */ - virMutexPtr sessionLock; /* ... that is protected by this mutex */ + GMutex *sessionLock; /* ... that is protected by this mutex */ esxVI_Datacenter *datacenter; char *datacenterPath; /* including folders */ esxVI_ComputeResource *computeResource; -- 2.25.2

On Friday, 10 April 2020 15:54:32 CEST Rafael Fonseca wrote:
@@ -346,11 +342,12 @@ esxStreamClose(virStreamPtr stream, bool finish) { int result = 0; esxStreamPrivate *priv = stream->privateData; + g_autoptr(GMutexLocker) locker = NULL;
if (!priv) return 0;
- virMutexLock(&priv->curl->lock); + locker = g_mutex_locker_new(&priv->curl->lock);
if (finish && priv->backlog_used > 0) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", @@ -360,8 +357,6 @@ esxStreamClose(virStreamPtr stream, bool finish)
stream->privateData = NULL;
- virMutexUnlock(&priv->curl->lock); - esxFreeStreamPrivate(&priv);
Careful here, this is a problematic situation: - the lock is indirectly part of the @priv structure - esxFreeStreamPrivate calls esxVI_CURL_Free(priv->curl) - esxVI_CURL_Free calls virMutexDestroy(&item->lock) - lock is still locked, so it will deadlock (or crash, or something not good anyway) You must unlock the mutex before esxFreeStreamPrivate is called. I did not check other patches of this long series for similar potential issues, please do check them.
diff --git a/src/esx/esx_vi.c b/src/esx/esx_vi.c index 16690edfbe..ed6c6c28cd 100644 --- a/src/esx/esx_vi.c +++ b/src/esx/esx_vi.c @@ -433,14 +429,13 @@ int esxVI_CURL_Upload(esxVI_CURL *curl, const char *url, const char *content) { int responseCode = 0; + g_autoptr(GMutexLocker) locker = g_mutex_locker_new(&curl->lock);
if (!content) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument")); return -1; }
- virMutexLock(&curl->lock); -
Careful #2 here about locking earlier: while usually this is not an issue, it could be in case the code that was executed without the lock held can be called by other code branches with the lock held. Again, this must be thoroughly checked in the whole patch series. -- Pino Toscano

Pino, thank you for the review. Could you please take a look at patch #42 in this series? It's the one in which I had to add some explicit unlock calls, so it'd be good for someone who knows the code to review this part. On Tue, Apr 14, 2020 at 8:15 AM Pino Toscano <ptoscano@redhat.com> wrote:
On Friday, 10 April 2020 15:54:32 CEST Rafael Fonseca wrote:
@@ -346,11 +342,12 @@ esxStreamClose(virStreamPtr stream, bool finish) { int result = 0; esxStreamPrivate *priv = stream->privateData; + g_autoptr(GMutexLocker) locker = NULL;
if (!priv) return 0;
- virMutexLock(&priv->curl->lock); + locker = g_mutex_locker_new(&priv->curl->lock);
if (finish && priv->backlog_used > 0) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", @@ -360,8 +357,6 @@ esxStreamClose(virStreamPtr stream, bool finish)
stream->privateData = NULL;
- virMutexUnlock(&priv->curl->lock); - esxFreeStreamPrivate(&priv);
Careful here, this is a problematic situation: - the lock is indirectly part of the @priv structure - esxFreeStreamPrivate calls esxVI_CURL_Free(priv->curl) - esxVI_CURL_Free calls virMutexDestroy(&item->lock) - lock is still locked, so it will deadlock (or crash, or something not good anyway)
You must unlock the mutex before esxFreeStreamPrivate is called.
Ops, nice catch.
I did not check other patches of this long series for similar potential issues, please do check them.
Ok, will do.
diff --git a/src/esx/esx_vi.c b/src/esx/esx_vi.c index 16690edfbe..ed6c6c28cd 100644 --- a/src/esx/esx_vi.c +++ b/src/esx/esx_vi.c @@ -433,14 +429,13 @@ int esxVI_CURL_Upload(esxVI_CURL *curl, const char *url, const char *content) { int responseCode = 0; + g_autoptr(GMutexLocker) locker = g_mutex_locker_new(&curl->lock);
if (!content) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument")); return -1; }
- virMutexLock(&curl->lock); -
Careful #2 here about locking earlier: while usually this is not an issue, it could be in case the code that was executed without the lock held can be called by other code branches with the lock held.
Again, this must be thoroughly checked in the whole patch series.
I'll recheck but I tried to do it in places where it was obvious the lock was not held because of an early return case. Att. -- Rafael Fonseca

Signed-off-by: Rafael Fonseca <r4f4rfs@gmail.com> --- src/util/virtpm.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/util/virtpm.c b/src/util/virtpm.c index c734bf941a..5fd6396f2f 100644 --- a/src/util/virtpm.c +++ b/src/util/virtpm.c @@ -91,7 +91,7 @@ virTPMCreateCancelPath(const char *devpath) * executables for the swtpm; to be found on the host along with * capabilties bitmap */ -static virMutex swtpm_tools_lock = VIR_MUTEX_INITIALIZER; +G_LOCK_DEFINE_STATIC(swtpm_tools_lock); static char *swtpm_path; static struct stat swtpm_stat; static virBitmapPtr swtpm_caps; @@ -113,9 +113,9 @@ virTPMGetSwtpm(void) if (!swtpm_path && virTPMEmulatorInit() < 0) return NULL; - virMutexLock(&swtpm_tools_lock); + G_LOCK(swtpm_tools_lock); s = g_strdup(swtpm_path); - virMutexUnlock(&swtpm_tools_lock); + G_UNLOCK(swtpm_tools_lock); return s; } @@ -128,9 +128,9 @@ virTPMGetSwtpmSetup(void) if (!swtpm_setup && virTPMEmulatorInit() < 0) return NULL; - virMutexLock(&swtpm_tools_lock); + G_LOCK(swtpm_tools_lock); s = g_strdup(swtpm_setup); - virMutexUnlock(&swtpm_tools_lock); + G_UNLOCK(swtpm_tools_lock); return s; } @@ -143,9 +143,9 @@ virTPMGetSwtpmIoctl(void) if (!swtpm_ioctl && virTPMEmulatorInit() < 0) return NULL; - virMutexLock(&swtpm_tools_lock); + G_LOCK(swtpm_tools_lock); s = g_strdup(swtpm_ioctl); - virMutexUnlock(&swtpm_tools_lock); + G_UNLOCK(swtpm_tools_lock); return s; } @@ -284,7 +284,7 @@ virTPMEmulatorInit(void) }; size_t i; - virMutexLock(&swtpm_tools_lock); + G_LOCK(swtpm_tools_lock); for (i = 0; i < G_N_ELEMENTS(prgs); i++) { g_autofree char *path = NULL; @@ -341,7 +341,7 @@ virTPMEmulatorInit(void) ret = 0; cleanup: - virMutexUnlock(&swtpm_tools_lock); + G_UNLOCK(swtpm_tools_lock); return ret; } -- 2.25.2

On Fri, Apr 10, 2020 at 03:54:33PM +0200, Rafael Fonseca wrote:
Signed-off-by: Rafael Fonseca <r4f4rfs@gmail.com> --- src/util/virtpm.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-)
diff --git a/src/util/virtpm.c b/src/util/virtpm.c index c734bf941a..5fd6396f2f 100644 --- a/src/util/virtpm.c +++ b/src/util/virtpm.c @@ -91,7 +91,7 @@ virTPMCreateCancelPath(const char *devpath) * executables for the swtpm; to be found on the host along with * capabilties bitmap */ -static virMutex swtpm_tools_lock = VIR_MUTEX_INITIALIZER; +G_LOCK_DEFINE_STATIC(swtpm_tools_lock); static char *swtpm_path; static struct stat swtpm_stat; static virBitmapPtr swtpm_caps; @@ -113,9 +113,9 @@ virTPMGetSwtpm(void) if (!swtpm_path && virTPMEmulatorInit() < 0) return NULL;
- virMutexLock(&swtpm_tools_lock); + G_LOCK(swtpm_tools_lock); s = g_strdup(swtpm_path); - virMutexUnlock(&swtpm_tools_lock); + G_UNLOCK(swtpm_tools_lock);
return s; } @@ -128,9 +128,9 @@ virTPMGetSwtpmSetup(void) if (!swtpm_setup && virTPMEmulatorInit() < 0) return NULL;
- virMutexLock(&swtpm_tools_lock); + G_LOCK(swtpm_tools_lock); s = g_strdup(swtpm_setup); - virMutexUnlock(&swtpm_tools_lock); + G_UNLOCK(swtpm_tools_lock);
return s; } @@ -143,9 +143,9 @@ virTPMGetSwtpmIoctl(void) if (!swtpm_ioctl && virTPMEmulatorInit() < 0) return NULL;
- virMutexLock(&swtpm_tools_lock); + G_LOCK(swtpm_tools_lock); s = g_strdup(swtpm_ioctl); - virMutexUnlock(&swtpm_tools_lock); + G_UNLOCK(swtpm_tools_lock);
return s; } @@ -284,7 +284,7 @@ virTPMEmulatorInit(void) }; size_t i;
- virMutexLock(&swtpm_tools_lock); + G_LOCK(swtpm_tools_lock);
for (i = 0; i < G_N_ELEMENTS(prgs); i++) { g_autofree char *path = NULL; @@ -341,7 +341,7 @@ virTPMEmulatorInit(void) ret = 0;
cleanup: - virMutexUnlock(&swtpm_tools_lock); + G_UNLOCK(swtpm_tools_lock);
return ret; } -- 2.25.2
Reviewed-by: Pavel Mores <pmores@redhat.com>

Signed-off-by: Rafael Fonseca <r4f4rfs@gmail.com> --- src/vbox/vbox_common.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/vbox/vbox_common.c b/src/vbox/vbox_common.c index e98ae04ec0..98d6978c51 100644 --- a/src/vbox/vbox_common.c +++ b/src/vbox/vbox_common.c @@ -58,7 +58,7 @@ VIR_LOG_INIT("vbox.vbox_common"); static vboxUniformedAPI gVBoxAPI; static virClassPtr vboxDriverClass; -static virMutex vbox_driver_lock = VIR_MUTEX_INITIALIZER; +G_LOCK_DEFINE_STATIC(vbox_driver_lock); static vboxDriverPtr vbox_driver; static vboxDriverPtr vboxDriverObjNew(void); @@ -233,7 +233,7 @@ vboxSdkUninitialize(void) static vboxDriverPtr vboxGetDriverConnection(void) { - virMutexLock(&vbox_driver_lock); + G_LOCK(vbox_driver_lock); if (vbox_driver) { virObjectRef(vbox_driver); @@ -253,14 +253,14 @@ vboxGetDriverConnection(void) if (!virObjectUnref(vbox_driver)) vbox_driver = NULL; - virMutexUnlock(&vbox_driver_lock); + G_UNLOCK(vbox_driver_lock); return NULL; } vbox_driver->connectionCount++; - virMutexUnlock(&vbox_driver_lock); + G_UNLOCK(vbox_driver_lock); return vbox_driver; } @@ -268,7 +268,7 @@ vboxGetDriverConnection(void) static void vboxDestroyDriverConnection(void) { - virMutexLock(&vbox_driver_lock); + G_LOCK(vbox_driver_lock); if (!vbox_driver) goto cleanup; @@ -281,7 +281,7 @@ vboxDestroyDriverConnection(void) vbox_driver = NULL; cleanup: - virMutexUnlock(&vbox_driver_lock); + G_UNLOCK(vbox_driver_lock); } static int openSessionForMachine(vboxDriverPtr data, const unsigned char *dom_uuid, -- 2.25.2

Signed-off-by: Rafael Fonseca <r4f4rfs@gmail.com> --- src/util/virnodesuspend.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/util/virnodesuspend.c b/src/util/virnodesuspend.c index 544a29783c..f336b3aa98 100644 --- a/src/util/virnodesuspend.c +++ b/src/util/virnodesuspend.c @@ -49,18 +49,18 @@ VIR_LOG_INIT("util.nodesuspend"); static unsigned int nodeSuspendTargetMask; static bool nodeSuspendTargetMaskInit; -static virMutex virNodeSuspendMutex = VIR_MUTEX_INITIALIZER; +G_LOCK_DEFINE_STATIC(virNodeSuspendMutex); static bool aboutToSuspend; static void virNodeSuspendLock(void) { - virMutexLock(&virNodeSuspendMutex); + G_LOCK(virNodeSuspendMutex); } static void virNodeSuspendUnlock(void) { - virMutexUnlock(&virNodeSuspendMutex); + G_UNLOCK(virNodeSuspendMutex); } -- 2.25.2

Signed-off-by: Rafael Fonseca <r4f4rfs@gmail.com> --- src/libxl/libxl_conf.h | 6 +++--- src/libxl/libxl_driver.c | 9 ++------- tests/testutilsxen.c | 9 ++------- 3 files changed, 7 insertions(+), 17 deletions(-) diff --git a/src/libxl/libxl_conf.h b/src/libxl/libxl_conf.h index 07b3373170..b29752ad0c 100644 --- a/src/libxl/libxl_conf.h +++ b/src/libxl/libxl_conf.h @@ -106,7 +106,7 @@ G_DEFINE_AUTOPTR_CLEANUP_FUNC(libxlDriverConfig, virObjectUnref); struct _libxlDriverPrivate { - virMutex lock; + GMutex lock; virHostdevManagerPtr hostdevMgr; /* Require lock to get reference on 'config', @@ -225,11 +225,11 @@ libxlBuildDomainConfig(virPortAllocatorRangePtr graphicsports, static inline void libxlDriverLock(libxlDriverPrivatePtr driver) { - virMutexLock(&driver->lock); + g_mutex_lock(&driver->lock); } static inline void libxlDriverUnlock(libxlDriverPrivatePtr driver) { - virMutexUnlock(&driver->lock); + g_mutex_unlock(&driver->lock); } diff --git a/src/libxl/libxl_driver.c b/src/libxl/libxl_driver.c index 980984b199..dae20cf453 100644 --- a/src/libxl/libxl_driver.c +++ b/src/libxl/libxl_driver.c @@ -520,7 +520,7 @@ libxlStateCleanup(void) virPidFileRelease(libxl_driver->config->stateDir, "driver", libxl_driver->lockFD); virObjectUnref(libxl_driver->config); - virMutexDestroy(&libxl_driver->lock); + g_mutex_clear(&libxl_driver->lock); VIR_FREE(libxl_driver); return 0; @@ -669,12 +669,7 @@ libxlStateInitialize(bool privileged, return VIR_DRV_STATE_INIT_ERROR; libxl_driver->lockFD = -1; - if (virMutexInit(&libxl_driver->lock) < 0) { - virReportError(VIR_ERR_INTERNAL_ERROR, - "%s", _("cannot initialize mutex")); - VIR_FREE(libxl_driver); - return VIR_DRV_STATE_INIT_ERROR; - } + g_mutex_init(&libxl_driver->lock); libxl_driver->inhibitCallback = callback; libxl_driver->inhibitOpaque = opaque; diff --git a/tests/testutilsxen.c b/tests/testutilsxen.c index d50c3003da..fcc7009f74 100644 --- a/tests/testutilsxen.c +++ b/tests/testutilsxen.c @@ -87,12 +87,7 @@ libxlDriverPrivatePtr testXLInitDriver(void) { libxlDriverPrivatePtr driver = g_new0(libxlDriverPrivate, 1); - if (virMutexInit(&driver->lock) < 0) { - virReportError(VIR_ERR_INTERNAL_ERROR, - "%s", "cannot initialize mutex"); - g_free(driver); - return NULL; - } + g_mutex_init(&driver->lock); if (!(driver->config = libxlDriverConfigNew())) return NULL; @@ -114,6 +109,6 @@ void testXLFreeDriver(libxlDriverPrivatePtr driver) { virObjectUnref(driver->config); virObjectUnref(driver->xmlopt); - virMutexDestroy(&driver->lock); + g_mutex_clear(&driver->lock); g_free(driver); } -- 2.25.2

Signed-off-by: Rafael Fonseca <r4f4rfs@gmail.com> --- src/vz/vz_driver.c | 26 +++++++++----------------- 1 file changed, 9 insertions(+), 17 deletions(-) diff --git a/src/vz/vz_driver.c b/src/vz/vz_driver.c index d882b91def..058e5b8614 100644 --- a/src/vz/vz_driver.c +++ b/src/vz/vz_driver.c @@ -67,7 +67,7 @@ static virClassPtr vzDriverClass; static bool vz_driver_privileged; /* pid file FD, ensures two copies of the driver can't use the same root */ static int vz_driver_lock_fd = -1; -static virMutex vz_driver_lock; +G_LOCK_DEFINE_STATIC(vz_driver_lock); static vzDriverPtr vz_driver; static vzConnPtr vz_conn_list; @@ -177,11 +177,11 @@ vzGetDriverConnection(void) "%s", _("vz state driver is not active")); return NULL; } - virMutexLock(&vz_driver_lock); + G_LOCK(vz_driver_lock); if (!vz_driver) vz_driver = vzDriverObjNew(); virObjectRef(vz_driver); - virMutexUnlock(&vz_driver_lock); + G_UNLOCK(vz_driver_lock); return vz_driver; } @@ -192,10 +192,10 @@ vzDestroyDriverConnection(void) vzDriverPtr driver; vzConnPtr privconn_list; - virMutexLock(&vz_driver_lock); + G_LOCK(vz_driver_lock); driver = g_steal_pointer(&vz_driver); privconn_list = g_steal_pointer(&vz_conn_list); - virMutexUnlock(&vz_driver_lock); + G_UNLOCK(vz_driver_lock); while (privconn_list) { vzConnPtr privconn = privconn_list; @@ -389,10 +389,10 @@ vzConnectOpen(virConnectPtr conn, if (!(privconn->closeCallback = virNewConnectCloseCallbackData())) goto error; - virMutexLock(&vz_driver_lock); + G_LOCK(vz_driver_lock); privconn->next = vz_conn_list; vz_conn_list = privconn; - virMutexUnlock(&vz_driver_lock); + G_UNLOCK(vz_driver_lock); return VIR_DRV_OPEN_SUCCESS; @@ -413,7 +413,7 @@ vzConnectClose(virConnectPtr conn) if (!privconn) return 0; - virMutexLock(&vz_driver_lock); + G_LOCK(vz_driver_lock); for (curr = vz_conn_list; curr; prev = &curr->next, curr = curr->next) { if (curr == privconn) { *prev = curr->next; @@ -421,7 +421,7 @@ vzConnectClose(virConnectPtr conn) } } - virMutexUnlock(&vz_driver_lock); + G_UNLOCK(vz_driver_lock); virObjectUnref(privconn->closeCallback); virObjectUnref(privconn->driver); @@ -4094,7 +4094,6 @@ vzStateCleanup(void) vz_driver = NULL; if (vz_driver_lock_fd != -1) virPidFileRelease(VZ_STATEDIR, "driver", vz_driver_lock_fd); - virMutexDestroy(&vz_driver_lock); prlsdkDeinit(); } return 0; @@ -4132,17 +4131,10 @@ vzStateInitialize(bool privileged, return VIR_DRV_STATE_INIT_ERROR; } - if (virMutexInit(&vz_driver_lock) < 0) - goto error; - /* Failing to create driver here is not fatal and only means * that next driver client will try once more when connecting */ vz_driver = vzDriverObjNew(); return VIR_DRV_STATE_INIT_COMPLETE; - - error: - vzStateCleanup(); - return VIR_DRV_STATE_INIT_ERROR; } static virStateDriver vzStateDriver = { -- 2.25.2

Signed-off-by: Rafael Fonseca <r4f4rfs@gmail.com> --- src/locking/lock_daemon.c | 32 +++++++++--------------------- src/locking/lock_daemon.h | 2 +- src/locking/lock_daemon_dispatch.c | 32 ++++++++---------------------- 3 files changed, 18 insertions(+), 48 deletions(-) diff --git a/src/locking/lock_daemon.c b/src/locking/lock_daemon.c index 4eff63014a..69aa1f688a 100644 --- a/src/locking/lock_daemon.c +++ b/src/locking/lock_daemon.c @@ -59,7 +59,7 @@ VIR_LOG_INIT("locking.lock_daemon"); #define VIR_LOCK_DAEMON_NUM_LOCKSPACES 3 struct _virLockDaemon { - virMutex lock; + GMutex lock; virNetDaemonPtr dmn; virHashTablePtr lockspaces; virLockSpacePtr defaultLockspace; @@ -89,7 +89,7 @@ virLockDaemonFree(virLockDaemonPtr lockd) if (!lockd) return; - virMutexDestroy(&lockd->lock); + g_mutex_clear(&lockd->lock); virObjectUnref(lockd->dmn); virHashFree(lockd->lockspaces); virLockSpaceFree(lockd->defaultLockspace); @@ -100,13 +100,13 @@ virLockDaemonFree(virLockDaemonPtr lockd) static inline void virLockDaemonLock(virLockDaemonPtr lockd) { - virMutexLock(&lockd->lock); + g_mutex_lock(&lockd->lock); } static inline void virLockDaemonUnlock(virLockDaemonPtr lockd) { - virMutexUnlock(&lockd->lock); + g_mutex_unlock(&lockd->lock); } static void virLockDaemonLockSpaceDataFree(void *data) @@ -123,12 +123,7 @@ virLockDaemonNew(virLockDaemonConfigPtr config, bool privileged) if (VIR_ALLOC(lockd) < 0) return NULL; - if (virMutexInit(&lockd->lock) < 0) { - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", - _("Unable to initialize mutex")); - VIR_FREE(lockd); - return NULL; - } + g_mutex_init(&lockd->lock); if (!(lockd->dmn = virNetDaemonNew())) goto error; @@ -220,12 +215,7 @@ virLockDaemonNewPostExecRestart(virJSONValuePtr object, bool privileged) if (VIR_ALLOC(lockd) < 0) return NULL; - if (virMutexInit(&lockd->lock) < 0) { - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", - _("Unable to initialize mutex")); - VIR_FREE(lockd); - return NULL; - } + g_mutex_init(&lockd->lock); if (!(lockd->lockspaces = virHashCreate(VIR_LOCK_DAEMON_NUM_LOCKSPACES, virLockDaemonLockSpaceDataFree))) @@ -450,7 +440,7 @@ virLockDaemonClientFree(void *opaque) } } - virMutexDestroy(&priv->lock); + g_mutex_clear(&priv->lock); VIR_FREE(priv->ownerName); VIR_FREE(priv); } @@ -469,11 +459,7 @@ virLockDaemonClientNew(virNetServerClientPtr client, if (VIR_ALLOC(priv) < 0) return NULL; - if (virMutexInit(&priv->lock) < 0) { - VIR_FREE(priv); - virReportSystemError(errno, "%s", _("unable to init mutex")); - return NULL; - } + g_mutex_init(&priv->lock); if (virNetServerClientGetUNIXIdentity(client, &clientuid, @@ -508,7 +494,7 @@ virLockDaemonClientNew(virNetServerClientPtr client, return priv; error: - virMutexDestroy(&priv->lock); + g_mutex_clear(&priv->lock); VIR_FREE(priv); return NULL; } diff --git a/src/locking/lock_daemon.h b/src/locking/lock_daemon.h index 7d30551c32..2405ef2036 100644 --- a/src/locking/lock_daemon.h +++ b/src/locking/lock_daemon.h @@ -30,7 +30,7 @@ typedef struct _virLockDaemonClient virLockDaemonClient; typedef virLockDaemonClient *virLockDaemonClientPtr; struct _virLockDaemonClient { - virMutex lock; + GMutex lock; bool restricted; pid_t ownerPid; diff --git a/src/locking/lock_daemon_dispatch.c b/src/locking/lock_daemon_dispatch.c index 1a53772366..1587785606 100644 --- a/src/locking/lock_daemon_dispatch.c +++ b/src/locking/lock_daemon_dispatch.c @@ -48,8 +48,7 @@ virLockSpaceProtocolDispatchAcquireResource(virNetServerPtr server G_GNUC_UNUSED virNetServerClientGetPrivateData(client); virLockSpacePtr lockspace; unsigned int newFlags; - - virMutexLock(&priv->lock); + g_autoptr(GMutexLocker) locker = g_mutex_locker_new(&priv->lock); virCheckFlagsGoto(VIR_LOCK_SPACE_PROTOCOL_ACQUIRE_RESOURCE_SHARED | VIR_LOCK_SPACE_PROTOCOL_ACQUIRE_RESOURCE_AUTOCREATE, cleanup); @@ -90,7 +89,6 @@ virLockSpaceProtocolDispatchAcquireResource(virNetServerPtr server G_GNUC_UNUSED cleanup: if (rv < 0) virNetMessageSaveError(rerr); - virMutexUnlock(&priv->lock); return rv; } @@ -107,8 +105,7 @@ virLockSpaceProtocolDispatchCreateResource(virNetServerPtr server G_GNUC_UNUSED, virLockDaemonClientPtr priv = virNetServerClientGetPrivateData(client); virLockSpacePtr lockspace; - - virMutexLock(&priv->lock); + g_autoptr(GMutexLocker) locker = g_mutex_locker_new(&priv->lock); virCheckFlagsGoto(0, cleanup); @@ -139,7 +136,6 @@ virLockSpaceProtocolDispatchCreateResource(virNetServerPtr server G_GNUC_UNUSED, cleanup: if (rv < 0) virNetMessageSaveError(rerr); - virMutexUnlock(&priv->lock); return rv; } @@ -156,8 +152,7 @@ virLockSpaceProtocolDispatchDeleteResource(virNetServerPtr server G_GNUC_UNUSED, virLockDaemonClientPtr priv = virNetServerClientGetPrivateData(client); virLockSpacePtr lockspace; - - virMutexLock(&priv->lock); + g_autoptr(GMutexLocker) locker = g_mutex_locker_new(&priv->lock); virCheckFlagsGoto(0, cleanup); @@ -188,7 +183,6 @@ virLockSpaceProtocolDispatchDeleteResource(virNetServerPtr server G_GNUC_UNUSED, cleanup: if (rv < 0) virNetMessageSaveError(rerr); - virMutexUnlock(&priv->lock); return rv; } @@ -205,8 +199,7 @@ virLockSpaceProtocolDispatchNew(virNetServerPtr server G_GNUC_UNUSED, virLockDaemonClientPtr priv = virNetServerClientGetPrivateData(client); virLockSpacePtr lockspace; - - virMutexLock(&priv->lock); + g_autoptr(GMutexLocker) locker = g_mutex_locker_new(&priv->lock); virCheckFlagsGoto(0, cleanup); @@ -244,7 +237,6 @@ virLockSpaceProtocolDispatchNew(virNetServerPtr server G_GNUC_UNUSED, cleanup: if (rv < 0) virNetMessageSaveError(rerr); - virMutexUnlock(&priv->lock); return rv; } @@ -260,8 +252,7 @@ virLockSpaceProtocolDispatchRegister(virNetServerPtr server G_GNUC_UNUSED, unsigned int flags = args->flags; virLockDaemonClientPtr priv = virNetServerClientGetPrivateData(client); - - virMutexLock(&priv->lock); + g_autoptr(GMutexLocker) locker = g_mutex_locker_new(&priv->lock); virCheckFlagsGoto(0, cleanup); @@ -289,7 +280,6 @@ virLockSpaceProtocolDispatchRegister(virNetServerPtr server G_GNUC_UNUSED, cleanup: if (rv < 0) virNetMessageSaveError(rerr); - virMutexUnlock(&priv->lock); return rv; } @@ -306,8 +296,7 @@ virLockSpaceProtocolDispatchReleaseResource(virNetServerPtr server G_GNUC_UNUSED virLockDaemonClientPtr priv = virNetServerClientGetPrivateData(client); virLockSpacePtr lockspace; - - virMutexLock(&priv->lock); + g_autoptr(GMutexLocker) locker = g_mutex_locker_new(&priv->lock); virCheckFlagsGoto(0, cleanup); @@ -340,7 +329,6 @@ virLockSpaceProtocolDispatchReleaseResource(virNetServerPtr server G_GNUC_UNUSED cleanup: if (rv < 0) virNetMessageSaveError(rerr); - virMutexUnlock(&priv->lock); return rv; } @@ -356,8 +344,7 @@ virLockSpaceProtocolDispatchRestrict(virNetServerPtr server G_GNUC_UNUSED, unsigned int flags = args->flags; virLockDaemonClientPtr priv = virNetServerClientGetPrivateData(client); - - virMutexLock(&priv->lock); + g_autoptr(GMutexLocker) locker = g_mutex_locker_new(&priv->lock); virCheckFlagsGoto(0, cleanup); @@ -379,7 +366,6 @@ virLockSpaceProtocolDispatchRestrict(virNetServerPtr server G_GNUC_UNUSED, cleanup: if (rv < 0) virNetMessageSaveError(rerr); - virMutexUnlock(&priv->lock); return rv; } @@ -395,8 +381,7 @@ virLockSpaceProtocolDispatchCreateLockSpace(virNetServerPtr server G_GNUC_UNUSED virLockDaemonClientPtr priv = virNetServerClientGetPrivateData(client); virLockSpacePtr lockspace; - - virMutexLock(&priv->lock); + g_autoptr(GMutexLocker) locker = g_mutex_locker_new(&priv->lock); if (priv->restricted) { virReportError(VIR_ERR_OPERATION_DENIED, "%s", @@ -424,6 +409,5 @@ virLockSpaceProtocolDispatchCreateLockSpace(virNetServerPtr server G_GNUC_UNUSED cleanup: if (rv < 0) virNetMessageSaveError(rerr); - virMutexUnlock(&priv->lock); return rv; } -- 2.25.2

Signed-off-by: Rafael Fonseca <r4f4rfs@gmail.com> --- src/logging/log_daemon.c | 26 ++++++-------------------- src/logging/log_daemon.h | 2 +- 2 files changed, 7 insertions(+), 21 deletions(-) diff --git a/src/logging/log_daemon.c b/src/logging/log_daemon.c index f37054706e..f4b94adf17 100644 --- a/src/logging/log_daemon.c +++ b/src/logging/log_daemon.c @@ -56,7 +56,7 @@ VIR_LOG_INIT("logging.log_daemon"); struct _virLogDaemon { - virMutex lock; + GMutex lock; virNetDaemonPtr dmn; virLogHandlerPtr handler; }; @@ -86,7 +86,7 @@ virLogDaemonFree(virLogDaemonPtr logd) return; virObjectUnref(logd->handler); - virMutexDestroy(&logd->lock); + g_mutex_clear(&logd->lock); virObjectUnref(logd->dmn); VIR_FREE(logd); @@ -119,12 +119,7 @@ virLogDaemonNew(virLogDaemonConfigPtr config, bool privileged) if (VIR_ALLOC(logd) < 0) return NULL; - if (virMutexInit(&logd->lock) < 0) { - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", - _("Unable to initialize mutex")); - VIR_FREE(logd); - return NULL; - } + g_mutex_init(&logd->lock); if (!(logd->dmn = virNetDaemonNew())) goto error; @@ -222,12 +217,7 @@ virLogDaemonNewPostExecRestart(virJSONValuePtr object, bool privileged, if (VIR_ALLOC(logd) < 0) return NULL; - if (virMutexInit(&logd->lock) < 0) { - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", - _("Unable to initialize mutex")); - VIR_FREE(logd); - return NULL; - } + g_mutex_init(&logd->lock); if (!(child = virJSONValueObjectGet(object, "daemon"))) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", @@ -325,7 +315,7 @@ virLogDaemonClientFree(void *opaque) priv, (unsigned long long)priv->clientPid); - virMutexDestroy(&priv->lock); + g_mutex_clear(&priv->lock); VIR_FREE(priv); } @@ -343,11 +333,7 @@ virLogDaemonClientNew(virNetServerClientPtr client, if (VIR_ALLOC(priv) < 0) return NULL; - if (virMutexInit(&priv->lock) < 0) { - VIR_FREE(priv); - virReportSystemError(errno, "%s", _("unable to init mutex")); - return NULL; - } + g_mutex_init(&priv->lock); if (virNetServerClientGetUNIXIdentity(client, &clientuid, diff --git a/src/logging/log_daemon.h b/src/logging/log_daemon.h index fa661e1793..a66b2ff245 100644 --- a/src/logging/log_daemon.h +++ b/src/logging/log_daemon.h @@ -30,7 +30,7 @@ typedef struct _virLogDaemonClient virLogDaemonClient; typedef virLogDaemonClient *virLogDaemonClientPtr; struct _virLogDaemonClient { - virMutex lock; + GMutex lock; pid_t clientPid; }; -- 2.25.2

Signed-off-by: Rafael Fonseca <r4f4rfs@gmail.com> --- src/conf/nwfilter_conf.c | 21 +++++++++++---------- src/conf/nwfilter_conf.h | 5 ++++- src/libvirt_private.syms | 3 ++- src/lxc/lxc_driver.c | 4 ++-- src/nwfilter/nwfilter_driver.c | 6 +++--- src/nwfilter/nwfilter_gentech_driver.c | 2 +- src/qemu/qemu_driver.c | 12 ++++++------ src/qemu/qemu_migration.c | 2 +- src/qemu/qemu_process.c | 4 ++-- 9 files changed, 32 insertions(+), 27 deletions(-) diff --git a/src/conf/nwfilter_conf.c b/src/conf/nwfilter_conf.c index 680f4184c3..405cedd002 100644 --- a/src/conf/nwfilter_conf.c +++ b/src/conf/nwfilter_conf.c @@ -154,27 +154,33 @@ static const struct int_map chain_priorities[] = { /* * only one filter update allowed */ -static virRWLock updateLock; +static GRWLock updateLock; static bool initialized; void virNWFilterReadLockFilterUpdates(void) { - virRWLockRead(&updateLock); + g_rw_lock_reader_lock(&updateLock); } void virNWFilterWriteLockFilterUpdates(void) { - virRWLockWrite(&updateLock); + g_rw_lock_writer_lock(&updateLock); } void -virNWFilterUnlockFilterUpdates(void) +virNWFilterReadUnlockFilterUpdates(void) { - virRWLockUnlock(&updateLock); + g_rw_lock_reader_unlock(&updateLock); +} + +void +virNWFilterWriteUnlockFilterUpdates(void) +{ + g_rw_lock_writer_unlock(&updateLock); } @@ -3090,9 +3096,6 @@ virNWFilterConfLayerInit(virNWFilterTriggerRebuildCallback cb, initialized = true; - if (virRWLockInit(&updateLock) < 0) - return -1; - return 0; } @@ -3103,8 +3106,6 @@ virNWFilterConfLayerShutdown(void) if (!initialized) return; - virRWLockDestroy(&updateLock); - initialized = false; rebuildCallback = NULL; rebuildOpaque = NULL; diff --git a/src/conf/nwfilter_conf.h b/src/conf/nwfilter_conf.h index 13db58e380..875c3a1e0d 100644 --- a/src/conf/nwfilter_conf.h +++ b/src/conf/nwfilter_conf.h @@ -582,7 +582,10 @@ void virNWFilterReadLockFilterUpdates(void); void -virNWFilterUnlockFilterUpdates(void); +virNWFilterWriteUnlockFilterUpdates(void); + +void +virNWFilterReadUnlockFilterUpdates(void); typedef int (*virNWFilterTriggerRebuildCallback)(void *opaque); diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index ec367653d5..c95c1a078c 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -874,6 +874,7 @@ virNWFilterJumpTargetTypeToString; virNWFilterPrintStateMatchFlags; virNWFilterPrintTCPFlags; virNWFilterReadLockFilterUpdates; +virNWFilterReadUnlockFilterUpdates; virNWFilterRuleActionTypeToString; virNWFilterRuleDirectionTypeToString; virNWFilterRuleIsProtocolEthernet; @@ -882,8 +883,8 @@ virNWFilterRuleIsProtocolIPv6; virNWFilterRuleProtocolTypeToString; virNWFilterSaveConfig; virNWFilterTriggerRebuild; -virNWFilterUnlockFilterUpdates; virNWFilterWriteLockFilterUpdates; +virNWFilterWriteUnlockFilterUpdates; # conf/nwfilter_ipaddrmap.h diff --git a/src/lxc/lxc_driver.c b/src/lxc/lxc_driver.c index 851894c459..7b232b4bac 100644 --- a/src/lxc/lxc_driver.c +++ b/src/lxc/lxc_driver.c @@ -1030,7 +1030,7 @@ static int lxcDomainCreateWithFiles(virDomainPtr dom, virDomainObjEndAPI(&vm); virObjectEventStateQueue(driver->domainEventState, event); virObjectUnref(cfg); - virNWFilterUnlockFilterUpdates(); + virNWFilterReadUnlockFilterUpdates(); return ret; } @@ -1158,7 +1158,7 @@ lxcDomainCreateXMLWithFiles(virConnectPtr conn, virObjectEventStateQueue(driver->domainEventState, event); virObjectUnref(caps); virObjectUnref(cfg); - virNWFilterUnlockFilterUpdates(); + virNWFilterReadUnlockFilterUpdates(); return dom; } diff --git a/src/nwfilter/nwfilter_driver.c b/src/nwfilter/nwfilter_driver.c index 1c407727db..650ef19746 100644 --- a/src/nwfilter/nwfilter_driver.c +++ b/src/nwfilter/nwfilter_driver.c @@ -333,7 +333,7 @@ nwfilterStateReload(void) virNWFilterObjListLoadAllConfigs(driver->nwfilters, driver->configDir); - virNWFilterUnlockFilterUpdates(); + virNWFilterWriteUnlockFilterUpdates(); virNWFilterBuildAll(driver, false); @@ -602,7 +602,7 @@ nwfilterDefineXML(virConnectPtr conn, if (obj) virNWFilterObjUnlock(obj); - virNWFilterUnlockFilterUpdates(); + virNWFilterWriteUnlockFilterUpdates(); nwfilterDriverUnlock(); return nwfilter; } @@ -643,7 +643,7 @@ nwfilterUndefine(virNWFilterPtr nwfilter) if (obj) virNWFilterObjUnlock(obj); - virNWFilterUnlockFilterUpdates(); + virNWFilterWriteUnlockFilterUpdates(); nwfilterDriverUnlock(); return ret; } diff --git a/src/nwfilter/nwfilter_gentech_driver.c b/src/nwfilter/nwfilter_gentech_driver.c index 6789a4a3fa..4b369e5b1a 100644 --- a/src/nwfilter/nwfilter_gentech_driver.c +++ b/src/nwfilter/nwfilter_gentech_driver.c @@ -807,7 +807,7 @@ virNWFilterInstantiateFilterLate(virNWFilterDriverStatePtr driver, } } - virNWFilterUnlockFilterUpdates(); + virNWFilterReadUnlockFilterUpdates(); virMutexUnlock(&updateMutex); return rc; diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c index 31f199fdef..d6ed6d3b3f 100644 --- a/src/qemu/qemu_driver.c +++ b/src/qemu/qemu_driver.c @@ -1796,7 +1796,7 @@ static virDomainPtr qemuDomainCreateXML(virConnectPtr conn, virDomainObjEndAPI(&vm); virObjectEventStateQueue(driver->domainEventState, event); virObjectEventStateQueue(driver->domainEventState, event2); - virNWFilterUnlockFilterUpdates(); + virNWFilterReadUnlockFilterUpdates(); return dom; } @@ -7053,7 +7053,7 @@ qemuDomainRestoreFlags(virConnectPtr conn, if (vm && ret < 0) qemuDomainRemoveInactiveJob(driver, vm); virDomainObjEndAPI(&vm); - virNWFilterUnlockFilterUpdates(); + virNWFilterReadUnlockFilterUpdates(); return ret; } @@ -7590,7 +7590,7 @@ qemuDomainCreateWithFlags(virDomainPtr dom, unsigned int flags) cleanup: virDomainObjEndAPI(&vm); - virNWFilterUnlockFilterUpdates(); + virNWFilterReadUnlockFilterUpdates(); return ret; } @@ -8802,7 +8802,7 @@ qemuDomainAttachDeviceFlags(virDomainPtr dom, cleanup: virDomainObjEndAPI(&vm); - virNWFilterUnlockFilterUpdates(); + virNWFilterReadUnlockFilterUpdates(); return ret; } @@ -8920,7 +8920,7 @@ static int qemuDomainUpdateDeviceFlags(virDomainPtr dom, virDomainDeviceDefFree(dev_copy); virDomainDeviceDefFree(dev); virDomainObjEndAPI(&vm); - virNWFilterUnlockFilterUpdates(); + virNWFilterReadUnlockFilterUpdates(); return ret; } @@ -16695,7 +16695,7 @@ qemuDomainRevertToSnapshot(virDomainSnapshotPtr snapshot, virObjectEventStateQueue(driver->domainEventState, event); virObjectEventStateQueue(driver->domainEventState, event2); virDomainObjEndAPI(&vm); - virNWFilterUnlockFilterUpdates(); + virNWFilterReadUnlockFilterUpdates(); virCPUDefFree(origCPU); virDomainDefFree(config); virDomainDefFree(inactiveConfig); diff --git a/src/qemu/qemu_migration.c b/src/qemu/qemu_migration.c index bc280e856a..5057aa2528 100644 --- a/src/qemu/qemu_migration.c +++ b/src/qemu/qemu_migration.c @@ -2716,7 +2716,7 @@ qemuMigrationDstPrepareAny(virQEMUDriverPtr driver, virDomainObjEndAPI(&vm); virObjectEventStateQueue(driver->domainEventState, event); qemuMigrationCookieFree(mig); - virNWFilterUnlockFilterUpdates(); + virNWFilterReadUnlockFilterUpdates(); virErrorRestore(&origErr); return ret; diff --git a/src/qemu/qemu_process.c b/src/qemu/qemu_process.c index 8ea470f75f..803994afe8 100644 --- a/src/qemu/qemu_process.c +++ b/src/qemu/qemu_process.c @@ -8196,7 +8196,7 @@ qemuProcessReconnect(void *opaque) qemuDomainRemoveInactiveJob(driver, obj); } virDomainObjEndAPI(&obj); - virNWFilterUnlockFilterUpdates(); + virNWFilterReadUnlockFilterUpdates(); virIdentitySetCurrent(NULL); return; @@ -8272,7 +8272,7 @@ qemuProcessReconnectHelper(virDomainObjPtr obj, qemuDomainRemoveInactiveJobLocked(src->driver, obj); virDomainObjEndAPI(&obj); - virNWFilterUnlockFilterUpdates(); + virNWFilterReadUnlockFilterUpdates(); g_clear_object(&data->identity); VIR_FREE(data); return -1; -- 2.25.2

Signed-off-by: Rafael Fonseca <r4f4rfs@gmail.com> --- src/util/virlog.c | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/util/virlog.c b/src/util/virlog.c index e4944d1258..2c98a4cca8 100644 --- a/src/util/virlog.c +++ b/src/util/virlog.c @@ -125,19 +125,19 @@ static void virLogOutputToFd(virLogSourcePtr src, /* * Logs accesses must be serialized though a mutex */ -virMutex virLogMutex; +G_LOCK_DEFINE(virLogMutex); void virLogLock(void) { - virMutexLock(&virLogMutex); + G_LOCK(virLogMutex); } void virLogUnlock(void) { - virMutexUnlock(&virLogMutex); + G_UNLOCK(virLogMutex); } @@ -247,9 +247,6 @@ virLogPriorityString(virLogPriority lvl) static int virLogOnceInit(void) { - if (virMutexInit(&virLogMutex) < 0) - return -1; - virLogLock(); virLogDefaultPriority = VIR_LOG_DEFAULT; -- 2.25.2

Signed-off-by: Rafael Fonseca <r4f4rfs@gmail.com> --- src/test/test_driver.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/test/test_driver.c b/src/test/test_driver.c index 7759847c2d..1c23d569d5 100644 --- a/src/test/test_driver.c +++ b/src/test/test_driver.c @@ -125,7 +125,7 @@ typedef struct _testDriver testDriver; typedef testDriver *testDriverPtr; static testDriverPtr defaultPrivconn; -static virMutex defaultLock = VIR_MUTEX_INITIALIZER; +G_LOCK_DEFINE_STATIC(defaultLock); static virClassPtr testDriverClass; static void testDriverDispose(void *obj); @@ -1334,10 +1334,10 @@ testOpenDefault(virConnectPtr conn) xmlXPathContextPtr ctxt = NULL; size_t i; - virMutexLock(&defaultLock); + G_LOCK(defaultLock); if (defaultPrivconn) { conn->privateData = virObjectRef(defaultPrivconn); - virMutexUnlock(&defaultLock); + G_UNLOCK(defaultLock); return VIR_DRV_OPEN_SUCCESS; } @@ -1379,7 +1379,7 @@ testOpenDefault(virConnectPtr conn) defaultPrivconn = privconn; ret = VIR_DRV_OPEN_SUCCESS; cleanup: - virMutexUnlock(&defaultLock); + G_UNLOCK(defaultLock); xmlXPathFreeContext(ctxt); xmlFreeDoc(doc); return ret; @@ -1445,11 +1445,11 @@ testConnectAuthenticate(virConnectPtr conn, static void testDriverCloseInternal(testDriverPtr driver) { - virMutexLock(&defaultLock); + G_LOCK(defaultLock); bool disposed = !virObjectUnref(driver); if (disposed && driver == defaultPrivconn) defaultPrivconn = NULL; - virMutexUnlock(&defaultLock); + G_UNLOCK(defaultLock); } -- 2.25.2

Signed-off-by: Rafael Fonseca <r4f4rfs@gmail.com> --- src/conf/virnwfilterobj.c | 15 ++++------ src/nwfilter/nwfilter_dhcpsnoop.c | 48 +++++++++++++------------------ 2 files changed, 25 insertions(+), 38 deletions(-) diff --git a/src/conf/virnwfilterobj.c b/src/conf/virnwfilterobj.c index c9e224061d..e4b90dcdb7 100644 --- a/src/conf/virnwfilterobj.c +++ b/src/conf/virnwfilterobj.c @@ -34,7 +34,7 @@ VIR_LOG_INIT("conf.virnwfilterobj"); struct _virNWFilterObj { - virMutex lock; + GRecMutex lock; bool wantRemoved; @@ -56,12 +56,7 @@ virNWFilterObjNew(void) if (VIR_ALLOC(obj) < 0) return NULL; - if (virMutexInitRecursive(&obj->lock) < 0) { - virReportError(VIR_ERR_INTERNAL_ERROR, - "%s", _("cannot initialize mutex")); - VIR_FREE(obj); - return NULL; - } + g_rec_mutex_init(&obj->lock); virNWFilterObjLock(obj); return obj; @@ -98,7 +93,7 @@ virNWFilterObjFree(virNWFilterObjPtr obj) virNWFilterDefFree(obj->def); virNWFilterDefFree(obj->newDef); - virMutexDestroy(&obj->lock); + g_rec_mutex_clear(&obj->lock); VIR_FREE(obj); } @@ -554,12 +549,12 @@ virNWFilterObjListLoadAllConfigs(virNWFilterObjListPtr nwfilters, void virNWFilterObjLock(virNWFilterObjPtr obj) { - virMutexLock(&obj->lock); + g_rec_mutex_lock(&obj->lock); } void virNWFilterObjUnlock(virNWFilterObjPtr obj) { - virMutexUnlock(&obj->lock); + g_rec_mutex_unlock(&obj->lock); } diff --git a/src/nwfilter/nwfilter_dhcpsnoop.c b/src/nwfilter/nwfilter_dhcpsnoop.c index 953d8936a4..191d85b2a4 100644 --- a/src/nwfilter/nwfilter_dhcpsnoop.c +++ b/src/nwfilter/nwfilter_dhcpsnoop.c @@ -80,26 +80,26 @@ struct virNWFilterSnoopState { /* thread management */ virHashTablePtr snoopReqs; virHashTablePtr ifnameToKey; - virMutex snoopLock; /* protects SnoopReqs and IfNameToKey */ + GRecMutex snoopLock; /* protects SnoopReqs and IfNameToKey */ virHashTablePtr active; - virMutex activeLock; /* protects Active */ + GMutex activeLock; /* protects Active */ }; # define virNWFilterSnoopLock() \ do { \ - virMutexLock(&virNWFilterSnoopState.snoopLock); \ + g_rec_mutex_lock(&virNWFilterSnoopState.snoopLock); \ } while (0) # define virNWFilterSnoopUnlock() \ do { \ - virMutexUnlock(&virNWFilterSnoopState.snoopLock); \ + g_rec_mutex_unlock(&virNWFilterSnoopState.snoopLock); \ } while (0) # define virNWFilterSnoopActiveLock() \ do { \ - virMutexLock(&virNWFilterSnoopState.activeLock); \ + g_mutex_lock(&virNWFilterSnoopState.activeLock); \ } while (0) # define virNWFilterSnoopActiveUnlock() \ do { \ - virMutexUnlock(&virNWFilterSnoopState.activeLock); \ + g_mutex_unlock(&virNWFilterSnoopState.activeLock); \ } while (0) # define VIR_IFKEY_LEN ((VIR_UUID_STRING_BUFLEN) + (VIR_MAC_STRING_BUFLEN)) @@ -136,7 +136,7 @@ struct _virNWFilterSnoopReq { virErrorPtr threadError; virNWFilterSnoopThreadStatus threadStatus; - virCond threadStatusCond; + GCond threadStatusCond; int jobCompletionStatus; /* the number of submitted jobs in the worker's queue */ @@ -152,7 +152,7 @@ struct _virNWFilterSnoopReq { * - threadStatus * (for refctr, see above) */ - virMutex lock; + GRecMutex lock; }; /* @@ -567,20 +567,17 @@ virNWFilterSnoopReqNew(const char *ifkey) req->threadStatus = THREAD_STATUS_NONE; - if (virStrcpyStatic(req->ifkey, ifkey) < 0|| - virMutexInitRecursive(&req->lock) < 0) + if (virStrcpyStatic(req->ifkey, ifkey) < 0) goto err_free_req; - if (virCondInit(&req->threadStatusCond) < 0) - goto err_destroy_mutex; + g_rec_mutex_init(&req->lock); + + g_cond_init(&req->threadStatusCond); virNWFilterSnoopReqGet(req); return req; - err_destroy_mutex: - virMutexDestroy(&req->lock); - err_free_req: VIR_FREE(req); @@ -610,8 +607,8 @@ virNWFilterSnoopReqFree(virNWFilterSnoopReqPtr req) /* free all req data */ virNWFilterBindingDefFree(req->binding); - virMutexDestroy(&req->lock); - virCondDestroy(&req->threadStatusCond); + g_rec_mutex_clear(&req->lock); + g_cond_clear(&req->threadStatusCond); virFreeError(req->threadError); VIR_FREE(req); @@ -623,7 +620,7 @@ virNWFilterSnoopReqFree(virNWFilterSnoopReqPtr req) static void virNWFilterSnoopReqLock(virNWFilterSnoopReqPtr req) { - virMutexLock(&req->lock); + g_rec_mutex_lock(&req->lock); } /* @@ -632,7 +629,7 @@ virNWFilterSnoopReqLock(virNWFilterSnoopReqPtr req) static void virNWFilterSnoopReqUnlock(virNWFilterSnoopReqPtr req) { - virMutexUnlock(&req->lock); + g_rec_mutex_unlock(&req->lock); } /* @@ -1381,7 +1378,7 @@ virNWFilterDHCPSnoopThread(void *req0) req->threadStatus = THREAD_STATUS_OK; } - virCondSignal(&req->threadStatusCond); + g_cond_signal(&req->threadStatusCond); virNWFilterSnoopReqUnlock(req); @@ -1667,11 +1664,7 @@ virNWFilterDHCPSnoopReq(virNWFilterTechDriverPtr techdriver, } /* sync with thread */ - if (virCondWait(&req->threadStatusCond, &req->lock) < 0) { - virReportSystemError(errno, "%s", - _("unable to wait on dhcp snoop thread")); - goto exit_snoop_cancel; - } + g_cond_wait(&req->threadStatusCond, (GMutex *)&req->lock); if (req->threadStatus != THREAD_STATUS_OK) { virErrorRestore(&req->threadError); @@ -2039,9 +2032,8 @@ virNWFilterDHCPSnoopInit(void) VIR_DEBUG("Initializing DHCP snooping"); - if (virMutexInitRecursive(&virNWFilterSnoopState.snoopLock) < 0 || - virMutexInit(&virNWFilterSnoopState.activeLock) < 0) - return -1; + g_rec_mutex_init(&virNWFilterSnoopState.snoopLock); + g_mutex_init(&virNWFilterSnoopState.activeLock); virNWFilterSnoopState.ifnameToKey = virHashCreate(0, NULL); virNWFilterSnoopState.active = virHashCreate(0, NULL); -- 2.25.2

Signed-off-by: Rafael Fonseca <r4f4rfs@gmail.com> --- src/nwfilter/nwfilter_gentech_driver.c | 38 ++++++++------------------ 1 file changed, 12 insertions(+), 26 deletions(-) diff --git a/src/nwfilter/nwfilter_gentech_driver.c b/src/nwfilter/nwfilter_gentech_driver.c index 4b369e5b1a..fd98d7f07e 100644 --- a/src/nwfilter/nwfilter_gentech_driver.c +++ b/src/nwfilter/nwfilter_gentech_driver.c @@ -69,14 +69,12 @@ static virNWFilterTechDriverPtr filter_tech_drivers[] = { * hash table as is done for virDomainObjList. You can then get * lockless lookup of objects by name. */ -static virMutex updateMutex; +static GRecMutex updateMutex; int virNWFilterTechDriversInit(bool privileged) { size_t i = 0; VIR_DEBUG("Initializing NWFilter technology drivers"); - if (virMutexInitRecursive(&updateMutex) < 0) - return -1; while (filter_tech_drivers[i]) { if (!(filter_tech_drivers[i]->flags & TECHDRV_FLAG_INITIALIZED)) @@ -95,7 +93,6 @@ void virNWFilterTechDriversShutdown(void) filter_tech_drivers[i]->shutdown(); i++; } - virMutexDestroy(&updateMutex); } @@ -753,9 +750,7 @@ virNWFilterInstantiateFilterInternal(virNWFilterDriverStatePtr driver, bool *foundNewFilter) { int ifindex; - int rc; - - virMutexLock(&updateMutex); + g_autoptr(GRecMutexLocker) locker = g_rec_mutex_locker_new(&updateMutex); /* after grabbing the filter update lock check for the interface; if it's not there anymore its filters will be or are being removed @@ -765,20 +760,14 @@ virNWFilterInstantiateFilterInternal(virNWFilterDriverStatePtr driver, /* interfaces / VMs can disappear during filter instantiation; don't mark it as an error */ virResetLastError(); - rc = 0; - goto cleanup; + return 0; } - rc = virNWFilterInstantiateFilterUpdate(driver, teardownOld, - binding, - ifindex, - useNewFilter, - false, foundNewFilter); - - cleanup: - virMutexUnlock(&updateMutex); - - return rc; + return virNWFilterInstantiateFilterUpdate(driver, teardownOld, + binding, + ifindex, + useNewFilter, + false, foundNewFilter); } @@ -789,9 +778,10 @@ virNWFilterInstantiateFilterLate(virNWFilterDriverStatePtr driver, { int rc; bool foundNewFilter = false; + g_autoptr(GRecMutexLocker) locker = NULL; virNWFilterReadLockFilterUpdates(); - virMutexLock(&updateMutex); + locker = g_rec_mutex_locker_new(&updateMutex); rc = virNWFilterInstantiateFilterUpdate(driver, true, binding, ifindex, @@ -808,7 +798,6 @@ virNWFilterInstantiateFilterLate(virNWFilterDriverStatePtr driver, } virNWFilterReadUnlockFilterUpdates(); - virMutexUnlock(&updateMutex); return rc; } @@ -930,11 +919,8 @@ _virNWFilterTeardownFilter(const char *ifname) int virNWFilterTeardownFilter(virNWFilterBindingDefPtr binding) { - int ret; - virMutexLock(&updateMutex); - ret = _virNWFilterTeardownFilter(binding->portdevname); - virMutexUnlock(&updateMutex); - return ret; + g_autoptr(GRecMutexLocker) locker = g_rec_mutex_locker_new(&updateMutex); + return _virNWFilterTeardownFilter(binding->portdevname); } enum { -- 2.25.2

Signed-off-by: Rafael Fonseca <r4f4rfs@gmail.com> --- src/lxc/lxc_fuse.c | 13 +++++-------- src/lxc/lxc_fuse.h | 2 +- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/src/lxc/lxc_fuse.c b/src/lxc/lxc_fuse.c index e73b4d0690..e35086c859 100644 --- a/src/lxc/lxc_fuse.c +++ b/src/lxc/lxc_fuse.c @@ -262,11 +262,10 @@ static struct fuse_operations lxcProcOper = { static void lxcFuseDestroy(virLXCFusePtr fuse) { - virMutexLock(&fuse->lock); + g_autoptr(GMutexLocker) locker = g_mutex_locker_new(&fuse->lock); fuse_unmount(fuse->mountpoint, fuse->ch); fuse_destroy(fuse->fuse); fuse->fuse = NULL; - virMutexUnlock(&fuse->lock); } static void lxcFuseRun(void *opaque) @@ -291,8 +290,7 @@ int lxcSetupFuse(virLXCFusePtr *f, virDomainDefPtr def) fuse->def = def; - if (virMutexInit(&fuse->lock) < 0) - goto cleanup2; + g_mutex_init(&fuse->lock); fuse->mountpoint = g_strdup_printf("%s/%s.fuse/", LXC_STATE_DIR, def->name); @@ -327,8 +325,7 @@ int lxcSetupFuse(virLXCFusePtr *f, virDomainDefPtr def) return ret; cleanup1: VIR_FREE(fuse->mountpoint); - virMutexDestroy(&fuse->lock); - cleanup2: + g_mutex_clear(&fuse->lock); VIR_FREE(fuse); goto cleanup; } @@ -351,10 +348,10 @@ void lxcFreeFuse(virLXCFusePtr *f) if (fuse) { /* exit fuse_loop, lxcFuseRun thread may try to destroy * fuse->fuse at the same time,so add a lock here. */ - virMutexLock(&fuse->lock); + g_mutex_lock(&fuse->lock); if (fuse->fuse) fuse_exit(fuse->fuse); - virMutexUnlock(&fuse->lock); + g_mutex_unlock(&fuse->lock); VIR_FREE(fuse->mountpoint); VIR_FREE(*f); diff --git a/src/lxc/lxc_fuse.h b/src/lxc/lxc_fuse.h index e9cfd9a20a..47ca7197fc 100644 --- a/src/lxc/lxc_fuse.h +++ b/src/lxc/lxc_fuse.h @@ -48,7 +48,7 @@ struct virLXCFuse { char *mountpoint; struct fuse *fuse; struct fuse_chan *ch; - virMutex lock; + GMutex lock; }; typedef struct virLXCFuse *virLXCFusePtr; -- 2.25.2

Signed-off-by: Rafael Fonseca <r4f4rfs@gmail.com> --- src/lxc/lxc_conf.h | 6 +++--- src/lxc/lxc_controller.c | 25 ++++++++++--------------- src/lxc/lxc_driver.c | 7 ++----- tests/testutilslxc.c | 9 ++------- 4 files changed, 17 insertions(+), 30 deletions(-) diff --git a/src/lxc/lxc_conf.h b/src/lxc/lxc_conf.h index f2f0e0a570..27cb861c08 100644 --- a/src/lxc/lxc_conf.h +++ b/src/lxc/lxc_conf.h @@ -64,7 +64,7 @@ struct _virLXCDriverConfig { }; struct _virLXCDriver { - virMutex lock; + GMutex lock; /* Require lock to get reference on 'config', * then lockless thereafter */ @@ -116,9 +116,9 @@ virDomainXMLOptionPtr lxcDomainXMLConfInit(virLXCDriverPtr driver); static inline void lxcDriverLock(virLXCDriverPtr driver) { - virMutexLock(&driver->lock); + g_mutex_lock(&driver->lock); } static inline void lxcDriverUnlock(virLXCDriverPtr driver) { - virMutexUnlock(&driver->lock); + g_mutex_unlock(&driver->lock); } diff --git a/src/lxc/lxc_controller.c b/src/lxc/lxc_controller.c index 453b435dd6..f7394c53a6 100644 --- a/src/lxc/lxc_controller.c +++ b/src/lxc/lxc_controller.c @@ -161,12 +161,7 @@ virLXCControllerDriverNew(void) { virLXCDriverPtr driver = g_new0(virLXCDriver, 1); - if (virMutexInit(&driver->lock) < 0) { - virReportError(VIR_ERR_INTERNAL_ERROR, - "%s", _("cannot initialize mutex")); - g_free(driver); - return NULL; - } + g_mutex_init(&driver->lock); driver->caps = virLXCDriverCapsInit(NULL); driver->xmlopt = lxcDomainXMLConfInit(driver); @@ -182,7 +177,7 @@ virLXCControllerDriverFree(virLXCDriverPtr driver) return; virObjectUnref(driver->xmlopt); virObjectUnref(driver->caps); - virMutexDestroy(&driver->lock); + g_mutex_clear(&driver->lock); g_free(driver); } @@ -1034,7 +1029,7 @@ static int lxcControllerClearCapabilities(void) } static bool wantReboot; -static virMutex lock = VIR_MUTEX_INITIALIZER; +G_LOCK_DEFINE_STATIC(lock); static void virLXCControllerSignalChildIO(virNetDaemonPtr dmn, @@ -1049,13 +1044,13 @@ static void virLXCControllerSignalChildIO(virNetDaemonPtr dmn, VIR_DEBUG("Got sig child %d vs %lld", ret, (long long)ctrl->initpid); if (ret == ctrl->initpid) { virNetDaemonQuit(dmn); - virMutexLock(&lock); + G_LOCK(lock); if (WIFSIGNALED(status) && WTERMSIG(status) == SIGHUP) { VIR_DEBUG("Status indicates reboot"); wantReboot = true; } - virMutexUnlock(&lock); + G_UNLOCK(lock); } } @@ -1169,7 +1164,7 @@ static void virLXCControllerConsoleEPoll(int watch, int fd, int events, void *op { virLXCControllerConsolePtr console = opaque; - virMutexLock(&lock); + G_LOCK(lock); VIR_DEBUG("IO event watch=%d fd=%d events=%d fromHost=%zu fromcont=%zu", watch, fd, events, console->fromHostLen, @@ -1209,14 +1204,14 @@ static void virLXCControllerConsoleEPoll(int watch, int fd, int events, void *op } cleanup: - virMutexUnlock(&lock); + G_UNLOCK(lock); } static void virLXCControllerConsoleIO(int watch, int fd, int events, void *opaque) { virLXCControllerConsolePtr console = opaque; - virMutexLock(&lock); + G_LOCK(lock); VIR_DEBUG("IO event watch=%d fd=%d events=%d fromHost=%zu fromcont=%zu", watch, fd, events, console->fromHostLen, @@ -1290,7 +1285,7 @@ static void virLXCControllerConsoleIO(int watch, int fd, int events, void *opaqu } virLXCControllerConsoleUpdateWatch(console); - virMutexUnlock(&lock); + G_UNLOCK(lock); return; error: @@ -1298,7 +1293,7 @@ static void virLXCControllerConsoleIO(int watch, int fd, int events, void *opaqu virEventRemoveHandle(console->hostWatch); console->contWatch = console->hostWatch = -1; virNetDaemonQuit(console->daemon); - virMutexUnlock(&lock); + G_UNLOCK(lock); } diff --git a/src/lxc/lxc_driver.c b/src/lxc/lxc_driver.c index 7b232b4bac..3253c08117 100644 --- a/src/lxc/lxc_driver.c +++ b/src/lxc/lxc_driver.c @@ -1494,10 +1494,7 @@ static int lxcStateInitialize(bool privileged, if (VIR_ALLOC(lxc_driver) < 0) return VIR_DRV_STATE_INIT_ERROR; lxc_driver->lockFD = -1; - if (virMutexInit(&lxc_driver->lock) < 0) { - VIR_FREE(lxc_driver); - return VIR_DRV_STATE_INIT_ERROR; - } + g_mutex_init(&lxc_driver->lock); if (!(lxc_driver->domains = virDomainObjListNew())) goto cleanup; @@ -1631,7 +1628,7 @@ static int lxcStateCleanup(void) virPidFileRelease(lxc_driver->config->stateDir, "driver", lxc_driver->lockFD); virObjectUnref(lxc_driver->config); - virMutexDestroy(&lxc_driver->lock); + g_mutex_clear(&lxc_driver->lock); VIR_FREE(lxc_driver); return 0; diff --git a/tests/testutilslxc.c b/tests/testutilslxc.c index b5e2f542e7..1e04263d1f 100644 --- a/tests/testutilslxc.c +++ b/tests/testutilslxc.c @@ -63,12 +63,7 @@ testLXCDriverInit(void) { virLXCDriverPtr driver = g_new0(virLXCDriver, 1); - if (virMutexInit(&driver->lock) < 0) { - virReportError(VIR_ERR_INTERNAL_ERROR, - "%s", "cannot initialize mutex"); - g_free(driver); - return NULL; - } + g_mutex_init(&driver->lock); driver->caps = testLXCCapsInit(); driver->xmlopt = lxcDomainXMLConfInit(driver); @@ -82,7 +77,7 @@ testLXCDriverFree(virLXCDriverPtr driver) { virObjectUnref(driver->xmlopt); virObjectUnref(driver->caps); - virMutexDestroy(&driver->lock); + g_mutex_clear(&driver->lock); g_free(driver); } -- 2.25.2

Signed-off-by: Rafael Fonseca <r4f4rfs@gmail.com> --- src/network/bridge_driver.c | 11 ++++------- src/network/bridge_driver_platform.h | 2 +- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/src/network/bridge_driver.c b/src/network/bridge_driver.c index f06099297a..2eaab9c667 100644 --- a/src/network/bridge_driver.c +++ b/src/network/bridge_driver.c @@ -101,14 +101,14 @@ networkGetDriver(void) static void networkDriverLock(virNetworkDriverStatePtr driver) { - virMutexLock(&driver->lock); + g_mutex_lock(&driver->lock); } static void networkDriverUnlock(virNetworkDriverStatePtr driver) { - virMutexUnlock(&driver->lock); + g_mutex_unlock(&driver->lock); } @@ -726,10 +726,7 @@ networkStateInitialize(bool privileged, goto error; network_driver->lockFD = -1; - if (virMutexInit(&network_driver->lock) < 0) { - VIR_FREE(network_driver); - goto error; - } + g_mutex_init(&network_driver->lock); network_driver->privileged = privileged; @@ -907,7 +904,7 @@ networkStateCleanup(void) virObjectUnref(network_driver->dnsmasqCaps); - virMutexDestroy(&network_driver->lock); + g_mutex_clear(&network_driver->lock); VIR_FREE(network_driver); diff --git a/src/network/bridge_driver_platform.h b/src/network/bridge_driver_platform.h index 169417a6c0..6528bf6647 100644 --- a/src/network/bridge_driver_platform.h +++ b/src/network/bridge_driver_platform.h @@ -29,7 +29,7 @@ /* Main driver state */ struct _virNetworkDriverState { - virMutex lock; + GMutex lock; /* Read-only */ bool privileged; -- 2.25.2

On Fri, Apr 10, 2020 at 03:54:47PM +0200, Rafael Fonseca wrote:
Signed-off-by: Rafael Fonseca <r4f4rfs@gmail.com> --- src/network/bridge_driver.c | 11 ++++------- src/network/bridge_driver_platform.h | 2 +- 2 files changed, 5 insertions(+), 8 deletions(-)
diff --git a/src/network/bridge_driver.c b/src/network/bridge_driver.c index f06099297a..2eaab9c667 100644 --- a/src/network/bridge_driver.c +++ b/src/network/bridge_driver.c @@ -101,14 +101,14 @@ networkGetDriver(void) static void networkDriverLock(virNetworkDriverStatePtr driver) { - virMutexLock(&driver->lock); + g_mutex_lock(&driver->lock); }
static void networkDriverUnlock(virNetworkDriverStatePtr driver) { - virMutexUnlock(&driver->lock); + g_mutex_unlock(&driver->lock); }
@@ -726,10 +726,7 @@ networkStateInitialize(bool privileged, goto error;
network_driver->lockFD = -1; - if (virMutexInit(&network_driver->lock) < 0) { - VIR_FREE(network_driver); - goto error; - } + g_mutex_init(&network_driver->lock);
network_driver->privileged = privileged;
@@ -907,7 +904,7 @@ networkStateCleanup(void)
virObjectUnref(network_driver->dnsmasqCaps);
- virMutexDestroy(&network_driver->lock); + g_mutex_clear(&network_driver->lock);
VIR_FREE(network_driver);
diff --git a/src/network/bridge_driver_platform.h b/src/network/bridge_driver_platform.h index 169417a6c0..6528bf6647 100644 --- a/src/network/bridge_driver_platform.h +++ b/src/network/bridge_driver_platform.h @@ -29,7 +29,7 @@
/* Main driver state */ struct _virNetworkDriverState { - virMutex lock; + GMutex lock;
/* Read-only */ bool privileged; -- 2.25.2
Reviewed-by: Pavel Mores <pmores@redhat.com>

Signed-off-by: Rafael Fonseca <r4f4rfs@gmail.com> --- src/conf/virnodedeviceobj.h | 4 ++-- src/node_device/node_device_driver.c | 11 +++-------- src/node_device/node_device_hal.c | 20 +++++--------------- src/node_device/node_device_udev.c | 21 +++++---------------- 4 files changed, 15 insertions(+), 41 deletions(-) diff --git a/src/conf/virnodedeviceobj.h b/src/conf/virnodedeviceobj.h index c9df8dedab..2ffefddd20 100644 --- a/src/conf/virnodedeviceobj.h +++ b/src/conf/virnodedeviceobj.h @@ -35,8 +35,8 @@ typedef virNodeDeviceObjList *virNodeDeviceObjListPtr; typedef struct _virNodeDeviceDriverState virNodeDeviceDriverState; typedef virNodeDeviceDriverState *virNodeDeviceDriverStatePtr; struct _virNodeDeviceDriverState { - virMutex lock; - virCond initCond; + GMutex lock; + GCond initCond; bool initialized; /* pid file FD, ensures two copies of the driver can't use the same root */ diff --git a/src/node_device/node_device_driver.c b/src/node_device/node_device_driver.c index ee175e1095..7d626948ec 100644 --- a/src/node_device/node_device_driver.c +++ b/src/node_device/node_device_driver.c @@ -145,14 +145,14 @@ nodeDeviceUpdateDriverName(virNodeDeviceDefPtr def G_GNUC_UNUSED) void nodeDeviceLock(void) { - virMutexLock(&driver->lock); + g_mutex_lock(&driver->lock); } void nodeDeviceUnlock(void) { - virMutexUnlock(&driver->lock); + g_mutex_unlock(&driver->lock); } @@ -161,12 +161,7 @@ nodeDeviceWaitInit(void) { nodeDeviceLock(); while (!driver->initialized) { - if (virCondWait(&driver->initCond, &driver->lock) < 0) { - virReportSystemError(errno, "%s", - _("failed to wait on condition")); - nodeDeviceUnlock(); - return -1; - } + g_cond_wait(&driver->initCond, &driver->lock); } nodeDeviceUnlock(); return 0; diff --git a/src/node_device/node_device_hal.c b/src/node_device/node_device_hal.c index 53a49ba2aa..fe78c3c4ae 100644 --- a/src/node_device/node_device_hal.c +++ b/src/node_device/node_device_hal.c @@ -607,18 +607,8 @@ nodeStateInitialize(bool privileged G_GNUC_UNUSED, return VIR_DRV_STATE_INIT_ERROR; driver->lockFD = -1; - if (virMutexInit(&driver->lock) < 0) { - VIR_FREE(driver); - return VIR_DRV_STATE_INIT_ERROR; - } - - if (virCondInit(&driver->initCond) < 0) { - virReportSystemError(errno, "%s", - _("Unable to initialize condition variable")); - virMutexDestroy(&driver->lock); - VIR_FREE(driver); - return VIR_DRV_STATE_INIT_ERROR; - } + g_mutex_init(&driver->lock); + g_cond_init(&driver->initCond); nodeDeviceLock(); @@ -713,7 +703,7 @@ nodeStateInitialize(bool privileged G_GNUC_UNUSED, nodeDeviceLock(); driver->initialized = true; nodeDeviceUnlock(); - virCondBroadcast(&driver->initCond); + g_cond_broadcast(&driver->initCond); return VIR_DRV_STATE_INIT_COMPLETE; @@ -747,8 +737,8 @@ nodeStateCleanup(void) VIR_FREE(driver->stateDir); nodeDeviceUnlock(); - virCondDestroy(&driver->initCond); - virMutexDestroy(&driver->lock); + g_cond_clear(&driver->initCond); + g_mutex_clear(&driver->lock); VIR_FREE(driver); return 0; } diff --git a/src/node_device/node_device_udev.c b/src/node_device/node_device_udev.c index 8451903e8a..433de0aae0 100644 --- a/src/node_device/node_device_udev.c +++ b/src/node_device/node_device_udev.c @@ -1477,8 +1477,8 @@ nodeStateCleanup(void) virPidFileRelease(driver->stateDir, "driver", driver->lockFD); VIR_FREE(driver->stateDir); - virCondDestroy(&driver->initCond); - virMutexDestroy(&driver->lock); + g_cond_clear(&driver->initCond); + g_mutex_clear(&driver->lock); VIR_FREE(driver); udevPCITranslateDeinit(); @@ -1748,7 +1748,7 @@ nodeStateInitializeEnumerate(void *opaque) nodeDeviceLock(); driver->initialized = true; nodeDeviceUnlock(); - virCondBroadcast(&driver->initCond); + g_cond_broadcast(&driver->initCond); return; @@ -1806,19 +1806,8 @@ nodeStateInitialize(bool privileged, return VIR_DRV_STATE_INIT_ERROR; driver->lockFD = -1; - if (virMutexInit(&driver->lock) < 0) { - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", - _("Unable to initialize mutex")); - VIR_FREE(driver); - return VIR_DRV_STATE_INIT_ERROR; - } - if (virCondInit(&driver->initCond) < 0) { - virReportSystemError(errno, "%s", - _("Unable to initialize condition variable")); - virMutexDestroy(&driver->lock); - VIR_FREE(driver); - return VIR_DRV_STATE_INIT_ERROR; - } + g_mutex_init(&driver->lock); + g_cond_init(&driver->initCond); driver->privileged = privileged; -- 2.25.2

Signed-off-by: Rafael Fonseca <r4f4rfs@gmail.com> --- src/vmware/vmware_conf.c | 2 +- src/vmware/vmware_conf.h | 2 +- src/vmware/vmware_driver.c | 7 +++---- 3 files changed, 5 insertions(+), 6 deletions(-) diff --git a/src/vmware/vmware_conf.c b/src/vmware/vmware_conf.c index fd62bb96f7..dbe67fcd97 100644 --- a/src/vmware/vmware_conf.c +++ b/src/vmware/vmware_conf.c @@ -52,7 +52,7 @@ vmwareFreeDriver(struct vmware_driver *driver) if (!driver) return; - virMutexDestroy(&driver->lock); + g_mutex_clear(&driver->lock); virObjectUnref(driver->domains); virObjectUnref(driver->caps); virObjectUnref(driver->xmlopt); diff --git a/src/vmware/vmware_conf.h b/src/vmware/vmware_conf.h index 5e0ef3744f..11610c1fcb 100644 --- a/src/vmware/vmware_conf.h +++ b/src/vmware/vmware_conf.h @@ -41,7 +41,7 @@ enum vmwareDriverType { VIR_ENUM_DECL(vmwareDriver); struct vmware_driver { - virMutex lock; + GMutex lock; virCapsPtr caps; virDomainXMLOptionPtr xmlopt; diff --git a/src/vmware/vmware_driver.c b/src/vmware/vmware_driver.c index d5dd6e4f5e..986615a515 100644 --- a/src/vmware/vmware_driver.c +++ b/src/vmware/vmware_driver.c @@ -50,13 +50,13 @@ static const char * const vmrun_candidates[] = { static void vmwareDriverLock(struct vmware_driver *driver) { - virMutexLock(&driver->lock); + g_mutex_lock(&driver->lock); } static void vmwareDriverUnlock(struct vmware_driver *driver) { - virMutexUnlock(&driver->lock); + g_mutex_unlock(&driver->lock); } @@ -211,8 +211,7 @@ vmwareConnectOpen(virConnectPtr conn, goto cleanup; } - if (virMutexInit(&driver->lock) < 0) - goto cleanup; + g_mutex_init(&driver->lock); if ((tmp = STRSKIP(conn->uri->scheme, "vmware")) == NULL) { virReportError(VIR_ERR_INTERNAL_ERROR, _("unable to parse URI " -- 2.25.2

Signed-off-by: Rafael Fonseca <r4f4rfs@gmail.com> --- tests/qemusecuritymock.c | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/tests/qemusecuritymock.c b/tests/qemusecuritymock.c index ad182c7bf7..0aef565bf9 100644 --- a/tests/qemusecuritymock.c +++ b/tests/qemusecuritymock.c @@ -57,7 +57,7 @@ static int (*real_close)(int fd); /* Global mutex to avoid races */ -virMutex m = VIR_MUTEX_INITIALIZER; +G_LOCK_DEFINE(m); /* Hash table to store XATTRs for paths. For simplicity, key is * "$path:$name" and value is just XATTR "$value". We don't need @@ -134,7 +134,7 @@ virFileGetXAttrQuiet(const char *path, key = get_key(path, name); - virMutexLock(&m); + G_LOCK(m); init_syms(); init_hash(); @@ -147,7 +147,7 @@ virFileGetXAttrQuiet(const char *path, ret = 0; cleanup: - virMutexUnlock(&m); + G_UNLOCK(m); VIR_FREE(key); return ret; } @@ -192,7 +192,7 @@ int virFileSetXAttr(const char *path, key = get_key(path, name); val = g_strdup(value); - virMutexLock(&m); + G_LOCK(m); init_syms(); init_hash(); @@ -202,7 +202,7 @@ int virFileSetXAttr(const char *path, ret = 0; cleanup: - virMutexUnlock(&m); + G_UNLOCK(m); VIR_FREE(val); VIR_FREE(key); return ret; @@ -217,14 +217,14 @@ int virFileRemoveXAttr(const char *path, key = get_key(path, name); - virMutexLock(&m); + G_LOCK(m); init_syms(); init_hash(); if ((ret = virHashRemoveEntry(xattr_paths, key)) < 0) errno = ENODATA; - virMutexUnlock(&m); + G_UNLOCK(m); VIR_FREE(key); return ret; } @@ -235,7 +235,7 @@ int virFileRemoveXAttr(const char *path, if (getenv(ENVVAR)) { \ uint32_t *val; \ \ - virMutexLock(&m); \ + G_LOCK(m); \ init_hash(); \ \ memset(sb, 0, sizeof(*sb)); \ @@ -254,7 +254,7 @@ int virFileRemoveXAttr(const char *path, sb->st_gid = *val >> 16; \ } \ \ - virMutexUnlock(&m); \ + G_UNLOCK(m); \ \ return 0; \ } \ @@ -279,7 +279,7 @@ mock_chown(const char *path, *val = (gid << 16) + uid; - virMutexLock(&m); + G_LOCK(m); init_hash(); if (virHashUpdateEntry(chown_paths, path, val) < 0) @@ -288,7 +288,7 @@ mock_chown(const char *path, ret = 0; cleanup: - virMutexUnlock(&m); + G_UNLOCK(m); VIR_FREE(val); return ret; } @@ -440,7 +440,7 @@ int checkPaths(const char **paths) bool xattr_fail = false; size_t i; - virMutexLock(&m); + G_LOCK(m); init_hash(); for (i = 0; paths && paths[i]; i++) { @@ -461,20 +461,20 @@ int checkPaths(const char **paths) ret = 0; cleanup: - virMutexUnlock(&m); + G_UNLOCK(m); return ret; } void freePaths(void) { - virMutexLock(&m); + G_LOCK(m); init_hash(); virHashFree(chown_paths); virHashFree(xattr_paths); chown_paths = xattr_paths = NULL; - virMutexUnlock(&m); + G_UNLOCK(m); } -- 2.25.2

Signed-off-by: Rafael Fonseca <r4f4rfs@gmail.com> --- tests/qemumonitortestutils.c | 37 +++++++++++++----------------------- 1 file changed, 13 insertions(+), 24 deletions(-) diff --git a/tests/qemumonitortestutils.c b/tests/qemumonitortestutils.c index 992d2a4e71..78fc94e710 100644 --- a/tests/qemumonitortestutils.c +++ b/tests/qemumonitortestutils.c @@ -49,7 +49,7 @@ struct _qemuMonitorTestItem { }; struct _qemuMonitorTest { - virMutex lock; + GMutex lock; virThread thread; bool quit; @@ -233,10 +233,9 @@ qemuMonitorTestIO(virNetSocketPtr sock, { qemuMonitorTestPtr test = opaque; bool err = false; + g_autoptr(GMutexLocker) locker = g_mutex_locker_new(&test->lock); - virMutexLock(&test->lock); if (test->quit) { - virMutexUnlock(&test->lock); return; } if (events & VIR_EVENT_HANDLE_WRITABLE) { @@ -320,7 +319,6 @@ qemuMonitorTestIO(virNetSocketPtr sock, virNetSocketUpdateIOCallback(sock, events); } - virMutexUnlock(&test->lock); } @@ -329,23 +327,23 @@ qemuMonitorTestWorker(void *opaque) { qemuMonitorTestPtr test = opaque; - virMutexLock(&test->lock); + g_mutex_lock(&test->lock); while (!test->quit) { - virMutexUnlock(&test->lock); + g_mutex_unlock(&test->lock); if (virEventRunDefaultImpl() < 0) { - virMutexLock(&test->lock); + g_mutex_lock(&test->lock); test->quit = true; break; } - virMutexLock(&test->lock); + g_mutex_lock(&test->lock); } test->running = false; - virMutexUnlock(&test->lock); + g_mutex_unlock(&test->lock); return; } @@ -367,13 +365,13 @@ qemuMonitorTestFree(qemuMonitorTestPtr test) if (!test) return; - virMutexLock(&test->lock); + g_mutex_lock(&test->lock); if (test->running) { test->quit = true; /* HACK: Add a dummy timeout to break event loop */ timer = virEventAddTimeout(0, qemuMonitorTestFreeTimer, NULL, NULL); } - virMutexUnlock(&test->lock); + g_mutex_unlock(&test->lock); if (test->client) { virNetSocketRemoveIOCallback(test->client); @@ -414,7 +412,7 @@ qemuMonitorTestFree(qemuMonitorTestPtr test) VIR_FREE(test->tmpdir); - virMutexDestroy(&test->lock); + g_mutex_clear(&test->lock); VIR_FREE(test); } @@ -426,6 +424,7 @@ qemuMonitorTestAddHandler(qemuMonitorTestPtr test, virFreeCallback freecb) { qemuMonitorTestItemPtr item; + g_autoptr(GMutexLocker) locker = g_mutex_locker_new(&test->lock); if (VIR_ALLOC(item) < 0) goto error; @@ -434,12 +433,9 @@ qemuMonitorTestAddHandler(qemuMonitorTestPtr test, item->freecb = freecb; item->opaque = opaque; - virMutexLock(&test->lock); if (VIR_APPEND_ELEMENT(test->items, test->nitems, item) < 0) { - virMutexUnlock(&test->lock); goto error; } - virMutexUnlock(&test->lock); return 0; @@ -1046,12 +1042,7 @@ qemuMonitorCommonTestNew(virDomainXMLOptionPtr xmlopt, if (VIR_ALLOC(test) < 0) goto error; - if (virMutexInit(&test->lock) < 0) { - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", - "Cannot initialize mutex"); - VIR_FREE(test); - return NULL; - } + g_mutex_init(&test->lock); tmpdir_template = g_strdup("/tmp/libvirt_XXXXXX"); @@ -1103,6 +1094,7 @@ static int qemuMonitorCommonTestInit(qemuMonitorTestPtr test) { int events = VIR_EVENT_HANDLE_READABLE; + g_autoptr(GMutexLocker) locker = g_mutex_locker_new(&test->lock); if (!test) return -1; @@ -1123,16 +1115,13 @@ qemuMonitorCommonTestInit(qemuMonitorTestPtr test) NULL) < 0) return -1; - virMutexLock(&test->lock); if (virThreadCreate(&test->thread, true, qemuMonitorTestWorker, test) < 0) { - virMutexUnlock(&test->lock); return -1; } test->started = test->running = true; - virMutexUnlock(&test->lock); return 0; } -- 2.25.2

Signed-off-by: Rafael Fonseca <r4f4rfs@gmail.com> --- src/qemu/qemu_conf.c | 4 ++-- src/qemu/qemu_conf.h | 2 +- src/qemu/qemu_driver.c | 9 ++------- tests/testutilsqemu.c | 5 ++--- 4 files changed, 7 insertions(+), 13 deletions(-) diff --git a/src/qemu/qemu_conf.c b/src/qemu/qemu_conf.c index c59824006c..f4460e77ec 100644 --- a/src/qemu/qemu_conf.c +++ b/src/qemu/qemu_conf.c @@ -86,12 +86,12 @@ VIR_ONCE_GLOBAL_INIT(virQEMUConfig); static void qemuDriverLock(virQEMUDriverPtr driver) { - virMutexLock(&driver->lock); + g_mutex_lock(&driver->lock); } static void qemuDriverUnlock(virQEMUDriverPtr driver) { - virMutexUnlock(&driver->lock); + g_mutex_unlock(&driver->lock); } #ifndef DEFAULT_LOADER_NVRAM diff --git a/src/qemu/qemu_conf.h b/src/qemu/qemu_conf.h index b9ef4551a3..f28486d62b 100644 --- a/src/qemu/qemu_conf.h +++ b/src/qemu/qemu_conf.h @@ -225,7 +225,7 @@ G_DEFINE_AUTOPTR_CLEANUP_FUNC(virQEMUDriverConfig, virObjectUnref); /* Main driver state */ struct _virQEMUDriver { - virMutex lock; + GMutex lock; /* Require lock to get reference on 'config', * then lockless thereafter */ diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c index d6ed6d3b3f..f6a9edf93c 100644 --- a/src/qemu/qemu_driver.c +++ b/src/qemu/qemu_driver.c @@ -645,12 +645,7 @@ qemuStateInitialize(bool privileged, qemu_driver->lockFD = -1; - if (virMutexInit(&qemu_driver->lock) < 0) { - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", - _("cannot initialize mutex")); - VIR_FREE(qemu_driver); - return VIR_DRV_STATE_INIT_ERROR; - } + g_mutex_init(&qemu_driver->lock); qemu_driver->inhibitCallback = callback; qemu_driver->inhibitOpaque = opaque; @@ -1134,7 +1129,7 @@ qemuStateCleanup(void) virPidFileRelease(qemu_driver->config->stateDir, "driver", qemu_driver->lockFD); virObjectUnref(qemu_driver->config); - virMutexDestroy(&qemu_driver->lock); + g_mutex_clear(&qemu_driver->lock); VIR_FREE(qemu_driver); return 0; diff --git a/tests/testutilsqemu.c b/tests/testutilsqemu.c index cb68ac0488..27053603a0 100644 --- a/tests/testutilsqemu.c +++ b/tests/testutilsqemu.c @@ -307,7 +307,7 @@ qemuTestParseCapabilitiesArch(virArch arch, void qemuTestDriverFree(virQEMUDriver *driver) { - virMutexDestroy(&driver->lock); + g_mutex_clear(&driver->lock); if (driver->config) { virFileDeleteTree(driver->config->stateDir); virFileDeleteTree(driver->config->configDir); @@ -386,8 +386,7 @@ int qemuTestDriverInit(virQEMUDriver *driver) !(cpuPower9 = virCPUDefCopy(&cpuPower9Data))) return -1; - if (virMutexInit(&driver->lock) < 0) - return -1; + g_mutex_init(&driver->lock); driver->hostarch = virArchFromHost(); driver->config = virQEMUDriverConfigNew(false, NULL); -- 2.25.2

Signed-off-by: Rafael Fonseca <r4f4rfs@gmail.com> --- src/remote/remote_driver.c | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/src/remote/remote_driver.c b/src/remote/remote_driver.c index 7bae0c2514..6cd38e86ef 100644 --- a/src/remote/remote_driver.c +++ b/src/remote/remote_driver.c @@ -116,7 +116,7 @@ VIR_ENUM_IMPL(remoteDriverMode, static bool inside_daemon; struct private_data { - virMutex lock; + GMutex lock; virNetClientPtr client; virNetClientProgramPtr remoteProgram; @@ -147,12 +147,12 @@ enum { static void remoteDriverLock(struct private_data *driver) { - virMutexLock(&driver->lock); + g_mutex_lock(&driver->lock); } static void remoteDriverUnlock(struct private_data *driver) { - virMutexUnlock(&driver->lock); + g_mutex_unlock(&driver->lock); } static int call(virConnectPtr conn, struct private_data *priv, @@ -1393,12 +1393,7 @@ remoteAllocPrivateData(void) if (VIR_ALLOC(priv) < 0) return NULL; - if (virMutexInit(&priv->lock) < 0) { - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", - _("cannot initialize mutex")); - VIR_FREE(priv); - return NULL; - } + g_mutex_init(&priv->lock); remoteDriverLock(priv); priv->localUses = 1; @@ -1561,7 +1556,7 @@ remoteConnectClose(virConnectPtr conn) ret = doRemoteClose(conn, priv); conn->privateData = NULL; remoteDriverUnlock(priv); - virMutexDestroy(&priv->lock); + g_mutex_clear(&priv->lock); VIR_FREE(priv); } if (priv) -- 2.25.2

Signed-off-by: Rafael Fonseca <r4f4rfs@gmail.com> --- src/conf/virnwfilterobj.h | 2 +- src/nwfilter/nwfilter_driver.c | 9 ++++----- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/src/conf/virnwfilterobj.h b/src/conf/virnwfilterobj.h index a6bdfb3864..f862f78650 100644 --- a/src/conf/virnwfilterobj.h +++ b/src/conf/virnwfilterobj.h @@ -33,7 +33,7 @@ typedef virNWFilterObjList *virNWFilterObjListPtr; typedef struct _virNWFilterDriverState virNWFilterDriverState; typedef virNWFilterDriverState *virNWFilterDriverStatePtr; struct _virNWFilterDriverState { - virMutex lock; + GMutex lock; bool privileged; /* pid file FD, ensures two copies of the driver can't use the same root */ diff --git a/src/nwfilter/nwfilter_driver.c b/src/nwfilter/nwfilter_driver.c index 650ef19746..8dc928160b 100644 --- a/src/nwfilter/nwfilter_driver.c +++ b/src/nwfilter/nwfilter_driver.c @@ -70,11 +70,11 @@ static int nwfilterStateReload(void); static void nwfilterDriverLock(void) { - virMutexLock(&driver->lock); + g_mutex_lock(&driver->lock); } static void nwfilterDriverUnlock(void) { - virMutexUnlock(&driver->lock); + g_mutex_lock(&driver->lock); } #ifdef WITH_FIREWALLD @@ -197,8 +197,7 @@ nwfilterStateInitialize(bool privileged, return VIR_DRV_STATE_INIT_ERROR; driver->lockFD = -1; - if (virMutexInit(&driver->lock) < 0) - goto err_free_driverstate; + g_mutex_init(&driver->lock); driver->privileged = privileged; if (!(driver->nwfilters = virNWFilterObjListNew())) @@ -379,7 +378,7 @@ nwfilterStateCleanup(void) /* free inactive nwfilters */ virNWFilterObjListFree(driver->nwfilters); - virMutexDestroy(&driver->lock); + g_mutex_clear(&driver->lock); VIR_FREE(driver); return 0; -- 2.25.2

Signed-off-by: Rafael Fonseca <r4f4rfs@gmail.com> --- src/openvz/openvz_conf.h | 2 +- src/openvz/openvz_driver.c | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/openvz/openvz_conf.h b/src/openvz/openvz_conf.h index 9892f39481..1680227e95 100644 --- a/src/openvz/openvz_conf.h +++ b/src/openvz/openvz_conf.h @@ -37,7 +37,7 @@ #define VZCTL_BRIDGE_MIN_VERSION ((3 * 1000 * 1000) + (0 * 1000) + 22 + 1) struct openvz_driver { - virMutex lock; + GMutex lock; virCapsPtr caps; virDomainXMLOptionPtr xmlopt; diff --git a/src/openvz/openvz_driver.c b/src/openvz/openvz_driver.c index 1a189dbbe7..36a22a4e88 100644 --- a/src/openvz/openvz_driver.c +++ b/src/openvz/openvz_driver.c @@ -67,12 +67,12 @@ static int openvzGetVEStatus(virDomainObjPtr vm, int *status, int *reason); static void openvzDriverLock(struct openvz_driver *driver) { - virMutexLock(&driver->lock); + g_mutex_lock(&driver->lock); } static void openvzDriverUnlock(struct openvz_driver *driver) { - virMutexUnlock(&driver->lock); + g_mutex_unlock(&driver->lock); } struct openvz_driver ovz_driver; -- 2.25.2

Signed-off-by: Rafael Fonseca <r4f4rfs@gmail.com> --- src/conf/virstorageobj.h | 2 +- src/storage/storage_driver.c | 11 ++++------- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/src/conf/virstorageobj.h b/src/conf/virstorageobj.h index 54100ac22a..0f1f7c3d8d 100644 --- a/src/conf/virstorageobj.h +++ b/src/conf/virstorageobj.h @@ -35,7 +35,7 @@ typedef struct _virStorageDriverState virStorageDriverState; typedef virStorageDriverState *virStorageDriverStatePtr; struct _virStorageDriverState { - virMutex lock; + GMutex lock; /* pid file FD, ensures two copies of the driver can't use the same root */ int lockFD; diff --git a/src/storage/storage_driver.c b/src/storage/storage_driver.c index 2db763caa5..3a2816dede 100644 --- a/src/storage/storage_driver.c +++ b/src/storage/storage_driver.c @@ -68,11 +68,11 @@ struct _virStorageVolStreamInfo { static void storageDriverLock(void) { - virMutexLock(&driver->lock); + g_mutex_lock(&driver->lock); } static void storageDriverUnlock(void) { - virMutexUnlock(&driver->lock); + g_mutex_unlock(&driver->lock); } @@ -269,10 +269,7 @@ storageStateInitialize(bool privileged, return VIR_DRV_STATE_INIT_ERROR; driver->lockFD = -1; - if (virMutexInit(&driver->lock) < 0) { - VIR_FREE(driver); - return VIR_DRV_STATE_INIT_ERROR; - } + g_mutex_init(&driver->lock); storageDriverLock(); if (!(driver->pools = virStoragePoolObjListNew())) @@ -391,7 +388,7 @@ storageStateCleanup(void) VIR_FREE(driver->autostartDir); VIR_FREE(driver->stateDir); storageDriverUnlock(); - virMutexDestroy(&driver->lock); + g_mutex_clear(&driver->lock); VIR_FREE(driver); return 0; -- 2.25.2

Signed-off-by: Rafael Fonseca <r4f4rfs@gmail.com> --- src/secret/secret_driver.c | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/src/secret/secret_driver.c b/src/secret/secret_driver.c index 1cb342878f..51a8a6e53e 100644 --- a/src/secret/secret_driver.c +++ b/src/secret/secret_driver.c @@ -54,7 +54,7 @@ enum { SECRET_MAX_XML_FILE = 10*1024*1024 }; typedef struct _virSecretDriverState virSecretDriverState; typedef virSecretDriverState *virSecretDriverStatePtr; struct _virSecretDriverState { - virMutex lock; + GMutex lock; bool privileged; /* readonly */ char *embeddedRoot; /* readonly */ int embeddedRefs; @@ -74,14 +74,14 @@ static virSecretDriverStatePtr driver; static void secretDriverLock(void) { - virMutexLock(&driver->lock); + g_mutex_lock(&driver->lock); } static void secretDriverUnlock(void) { - virMutexUnlock(&driver->lock); + g_mutex_unlock(&driver->lock); } @@ -446,7 +446,7 @@ secretStateCleanup(void) VIR_FREE(driver->stateDir); secretDriverUnlock(); - virMutexDestroy(&driver->lock); + g_mutex_clear(&driver->lock); VIR_FREE(driver); return 0; @@ -463,10 +463,7 @@ secretStateInitialize(bool privileged, return VIR_DRV_STATE_INIT_ERROR; driver->lockFD = -1; - if (virMutexInit(&driver->lock) < 0) { - VIR_FREE(driver); - return VIR_DRV_STATE_INIT_ERROR; - } + g_mutex_init(&driver->lock); secretDriverLock(); driver->secretEventState = virObjectEventStateNew(); -- 2.25.2

Signed-off-by: Rafael Fonseca <r4f4rfs@gmail.com> --- src/remote/remote_daemon.h | 2 +- src/remote/remote_daemon_dispatch.c | 100 +++++++--------------------- src/remote/remote_daemon_stream.c | 29 ++++---- 3 files changed, 37 insertions(+), 94 deletions(-) diff --git a/src/remote/remote_daemon.h b/src/remote/remote_daemon.h index a2d9af4036..5535ce72dd 100644 --- a/src/remote/remote_daemon.h +++ b/src/remote/remote_daemon.h @@ -45,7 +45,7 @@ typedef daemonClientEventCallback *daemonClientEventCallbackPtr; /* Stores the per-client connection state */ struct daemonClientPrivate { /* Hold while accessing any data except conn */ - virMutex lock; + GMutex lock; daemonClientEventCallbackPtr *domainEventCallbacks; size_t ndomainEventCallbacks; diff --git a/src/remote/remote_daemon_dispatch.c b/src/remote/remote_daemon_dispatch.c index c5506c2e11..72d30df0c1 100644 --- a/src/remote/remote_daemon_dispatch.c +++ b/src/remote/remote_daemon_dispatch.c @@ -1912,11 +1912,7 @@ void *remoteClientNew(virNetServerClientPtr client, if (VIR_ALLOC(priv) < 0) return NULL; - if (virMutexInit(&priv->lock) < 0) { - VIR_FREE(priv); - virReportSystemError(errno, "%s", _("unable to init mutex")); - return NULL; - } + g_mutex_init(&priv->lock); virNetServerClientSetCloseHook(client, remoteClientCloseFunc); return priv; @@ -2062,9 +2058,9 @@ remoteDispatchConnectOpen(virNetServerPtr server G_GNUC_UNUSED, const char *type = NULL; #endif /* !MODULE_NAME */ bool preserveIdentity = false; + g_autoptr(GMutexLocker) locker = g_mutex_locker_new(&priv->lock); VIR_DEBUG("priv=%p conn=%p", priv, priv->conn); - virMutexLock(&priv->lock); /* Already opened? */ if (priv->conn) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection already open")); @@ -2193,7 +2189,6 @@ remoteDispatchConnectOpen(virNetServerPtr server G_GNUC_UNUSED, priv->conn = NULL; } } - virMutexUnlock(&priv->lock); return rv; } @@ -3690,8 +3685,7 @@ remoteDispatchAuthSaslInit(virNetServerPtr server G_GNUC_UNUSED, virNetSASLSessionPtr sasl = NULL; struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client); - - virMutexLock(&priv->lock); + g_autoptr(GMutexLocker) locker = g_mutex_locker_new(&priv->lock); VIR_DEBUG("Initialize SASL auth %d", virNetServerClientGetFD(client)); if (virNetServerClientGetAuth(client) != VIR_NET_SERVER_SERVICE_AUTH_SASL || @@ -3736,7 +3730,6 @@ remoteDispatchAuthSaslInit(virNetServerPtr server G_GNUC_UNUSED, VIR_DEBUG("Available mechanisms for client: '%s'", ret->mechlist); priv->sasl = sasl; - virMutexUnlock(&priv->lock); return 0; authfail: @@ -3748,7 +3741,6 @@ remoteDispatchAuthSaslInit(virNetServerPtr server G_GNUC_UNUSED, "client=%p auth=%d", client, REMOTE_AUTH_SASL); virObjectUnref(sasl); - virMutexUnlock(&priv->lock); return -1; } @@ -3818,8 +3810,7 @@ remoteDispatchAuthSaslStart(virNetServerPtr server, struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client); const char *identity; - - virMutexLock(&priv->lock); + g_autoptr(GMutexLocker) locker = g_mutex_locker_new(&priv->lock); VIR_DEBUG("Start SASL auth %d", virNetServerClientGetFD(client)); if (virNetServerClientGetAuth(client) != VIR_NET_SERVER_SERVICE_AUTH_SASL || @@ -3872,7 +3863,6 @@ remoteDispatchAuthSaslStart(virNetServerPtr server, ret->complete = 1; } - virMutexUnlock(&priv->lock); return 0; authfail: @@ -3895,7 +3885,6 @@ remoteDispatchAuthSaslStart(virNetServerPtr server, virReportError(VIR_ERR_AUTH_FAILED, "%s", _("authentication failed")); virNetMessageSaveError(rerr); - virMutexUnlock(&priv->lock); return -1; } @@ -3914,8 +3903,7 @@ remoteDispatchAuthSaslStep(virNetServerPtr server, struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client); const char *identity; - - virMutexLock(&priv->lock); + g_autoptr(GMutexLocker) locker = g_mutex_locker_new(&priv->lock); VIR_DEBUG("Step SASL auth %d", virNetServerClientGetFD(client)); if (virNetServerClientGetAuth(client) != VIR_NET_SERVER_SERVICE_AUTH_SASL || @@ -3968,7 +3956,6 @@ remoteDispatchAuthSaslStep(virNetServerPtr server, ret->complete = 1; } - virMutexUnlock(&priv->lock); return 0; authfail: @@ -3991,7 +3978,6 @@ remoteDispatchAuthSaslStep(virNetServerPtr server, virReportError(VIR_ERR_AUTH_FAILED, "%s", _("authentication failed")); virNetMessageSaveError(rerr); - virMutexUnlock(&priv->lock); return -1; } #else @@ -4056,8 +4042,8 @@ remoteDispatchAuthPolkit(virNetServerPtr server, struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client); int rv; + g_autoptr(GMutexLocker) locker = g_mutex_locker_new(&priv->lock); - virMutexLock(&priv->lock); action = virNetServerClientGetReadonly(client) ? "org.libvirt.unix.monitor" : "org.libvirt.unix.manage"; @@ -4101,13 +4087,11 @@ remoteDispatchAuthPolkit(virNetServerPtr server, ret->complete = 1; virNetServerSetClientAuthenticated(server, client); - virMutexUnlock(&priv->lock); return 0; error: virNetMessageSaveError(rerr); - virMutexUnlock(&priv->lock); return -1; authfail: @@ -4173,8 +4157,7 @@ remoteDispatchConnectRegisterCloseCallback(virNetServerPtr server G_GNUC_UNUSED, struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client); virConnectPtr conn = remoteGetHypervisorConn(client); - - virMutexLock(&priv->lock); + g_autoptr(GMutexLocker) locker = g_mutex_locker_new(&priv->lock); if (!conn) goto cleanup; @@ -4188,7 +4171,6 @@ remoteDispatchConnectRegisterCloseCallback(virNetServerPtr server G_GNUC_UNUSED, rv = 0; cleanup: - virMutexUnlock(&priv->lock); if (rv < 0) virNetMessageSaveError(rerr); return rv; @@ -4204,8 +4186,7 @@ remoteDispatchConnectUnregisterCloseCallback(virNetServerPtr server G_GNUC_UNUSE struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client); virConnectPtr conn = remoteGetHypervisorConn(client); - - virMutexLock(&priv->lock); + g_autoptr(GMutexLocker) locker = g_mutex_locker_new(&priv->lock); if (!conn) goto cleanup; @@ -4218,7 +4199,6 @@ remoteDispatchConnectUnregisterCloseCallback(virNetServerPtr server G_GNUC_UNUSE rv = 0; cleanup: - virMutexUnlock(&priv->lock); if (rv < 0) virNetMessageSaveError(rerr); return rv; @@ -4238,8 +4218,7 @@ remoteDispatchConnectDomainEventRegister(virNetServerPtr server G_GNUC_UNUSED, struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client); virConnectPtr conn = remoteGetHypervisorConn(client); - - virMutexLock(&priv->lock); + g_autoptr(GMutexLocker) locker = g_mutex_locker_new(&priv->lock); if (!conn) goto cleanup; @@ -4282,7 +4261,6 @@ remoteDispatchConnectDomainEventRegister(virNetServerPtr server G_GNUC_UNUSED, rv = 0; cleanup: - virMutexUnlock(&priv->lock); remoteEventCallbackFree(callback); if (rv < 0) virNetMessageSaveError(rerr); @@ -4302,8 +4280,7 @@ remoteDispatchConnectDomainEventDeregister(virNetServerPtr server G_GNUC_UNUSED, struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client); virConnectPtr conn = remoteGetHypervisorConn(client); - - virMutexLock(&priv->lock); + g_autoptr(GMutexLocker) locker = g_mutex_locker_new(&priv->lock); if (!conn) goto cleanup; @@ -4331,7 +4308,6 @@ remoteDispatchConnectDomainEventDeregister(virNetServerPtr server G_GNUC_UNUSED, rv = 0; cleanup: - virMutexUnlock(&priv->lock); if (rv < 0) virNetMessageSaveError(rerr); return rv; @@ -4459,8 +4435,7 @@ remoteDispatchConnectDomainEventRegisterAny(virNetServerPtr server G_GNUC_UNUSED struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client); virConnectPtr conn = remoteGetHypervisorConn(client); - - virMutexLock(&priv->lock); + g_autoptr(GMutexLocker) locker = g_mutex_locker_new(&priv->lock); if (!conn) goto cleanup; @@ -4511,7 +4486,6 @@ remoteDispatchConnectDomainEventRegisterAny(virNetServerPtr server G_GNUC_UNUSED rv = 0; cleanup: - virMutexUnlock(&priv->lock); remoteEventCallbackFree(callback); if (rv < 0) virNetMessageSaveError(rerr); @@ -4535,8 +4509,7 @@ remoteDispatchConnectDomainEventCallbackRegisterAny(virNetServerPtr server G_GNU virNetServerClientGetPrivateData(client); virDomainPtr dom = NULL; virConnectPtr conn = remoteGetHypervisorConn(client); - - virMutexLock(&priv->lock); + g_autoptr(GMutexLocker) locker = g_mutex_locker_new(&priv->lock); if (!conn) goto cleanup; @@ -4587,7 +4560,6 @@ remoteDispatchConnectDomainEventCallbackRegisterAny(virNetServerPtr server G_GNU rv = 0; cleanup: - virMutexUnlock(&priv->lock); remoteEventCallbackFree(callback); if (rv < 0) virNetMessageSaveError(rerr); @@ -4609,8 +4581,7 @@ remoteDispatchConnectDomainEventDeregisterAny(virNetServerPtr server G_GNUC_UNUS struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client); virConnectPtr conn = remoteGetHypervisorConn(client); - - virMutexLock(&priv->lock); + g_autoptr(GMutexLocker) locker = g_mutex_locker_new(&priv->lock); if (!conn) goto cleanup; @@ -4646,7 +4617,6 @@ remoteDispatchConnectDomainEventDeregisterAny(virNetServerPtr server G_GNUC_UNUS rv = 0; cleanup: - virMutexUnlock(&priv->lock); if (rv < 0) virNetMessageSaveError(rerr); return rv; @@ -4665,8 +4635,7 @@ remoteDispatchConnectDomainEventCallbackDeregisterAny(virNetServerPtr server G_G struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client); virConnectPtr conn = remoteGetHypervisorConn(client); - - virMutexLock(&priv->lock); + g_autoptr(GMutexLocker) locker = g_mutex_locker_new(&priv->lock); if (!conn) goto cleanup; @@ -4691,7 +4660,6 @@ remoteDispatchConnectDomainEventCallbackDeregisterAny(virNetServerPtr server G_G rv = 0; cleanup: - virMutexUnlock(&priv->lock); if (rv < 0) virNetMessageSaveError(rerr); return rv; @@ -6018,8 +5986,7 @@ remoteDispatchConnectNetworkEventRegisterAny(virNetServerPtr server G_GNUC_UNUSE struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client); virConnectPtr conn = remoteGetNetworkConn(client); - - virMutexLock(&priv->lock); + g_autoptr(GMutexLocker) locker = g_mutex_locker_new(&priv->lock); if (!conn) goto cleanup; @@ -6070,7 +6037,6 @@ remoteDispatchConnectNetworkEventRegisterAny(virNetServerPtr server G_GNUC_UNUSE rv = 0; cleanup: - virMutexUnlock(&priv->lock); remoteEventCallbackFree(callback); if (rv < 0) virNetMessageSaveError(rerr); @@ -6091,8 +6057,7 @@ remoteDispatchConnectNetworkEventDeregisterAny(virNetServerPtr server G_GNUC_UNU struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client); virConnectPtr conn = remoteGetNetworkConn(client); - - virMutexLock(&priv->lock); + g_autoptr(GMutexLocker) locker = g_mutex_locker_new(&priv->lock); if (!conn) goto cleanup; @@ -6117,7 +6082,6 @@ remoteDispatchConnectNetworkEventDeregisterAny(virNetServerPtr server G_GNUC_UNU rv = 0; cleanup: - virMutexUnlock(&priv->lock); if (rv < 0) virNetMessageSaveError(rerr); return rv; @@ -6139,8 +6103,7 @@ remoteDispatchConnectStoragePoolEventRegisterAny(virNetServerPtr server G_GNUC_U virNetServerClientGetPrivateData(client); virStoragePoolPtr pool = NULL; virConnectPtr conn = remoteGetStorageConn(client); - - virMutexLock(&priv->lock); + g_autoptr(GMutexLocker) locker = g_mutex_locker_new(&priv->lock); if (!conn) goto cleanup; @@ -6191,7 +6154,6 @@ remoteDispatchConnectStoragePoolEventRegisterAny(virNetServerPtr server G_GNUC_U rv = 0; cleanup: - virMutexUnlock(&priv->lock); remoteEventCallbackFree(callback); if (rv < 0) virNetMessageSaveError(rerr); @@ -6211,8 +6173,7 @@ remoteDispatchConnectStoragePoolEventDeregisterAny(virNetServerPtr server G_GNUC struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client); virConnectPtr conn = remoteGetStorageConn(client); - - virMutexLock(&priv->lock); + g_autoptr(GMutexLocker) locker = g_mutex_locker_new(&priv->lock); if (!conn) goto cleanup; @@ -6237,7 +6198,6 @@ remoteDispatchConnectStoragePoolEventDeregisterAny(virNetServerPtr server G_GNUC rv = 0; cleanup: - virMutexUnlock(&priv->lock); if (rv < 0) virNetMessageSaveError(rerr); return rv; @@ -6259,8 +6219,7 @@ remoteDispatchConnectNodeDeviceEventRegisterAny(virNetServerPtr server G_GNUC_UN virNetServerClientGetPrivateData(client); virNodeDevicePtr dev = NULL; virConnectPtr conn = remoteGetNodeDevConn(client); - - virMutexLock(&priv->lock); + g_autoptr(GMutexLocker) locker = g_mutex_locker_new(&priv->lock); if (!conn) goto cleanup; @@ -6311,7 +6270,6 @@ remoteDispatchConnectNodeDeviceEventRegisterAny(virNetServerPtr server G_GNUC_UN rv = 0; cleanup: - virMutexUnlock(&priv->lock); remoteEventCallbackFree(callback); if (rv < 0) virNetMessageSaveError(rerr); @@ -6331,8 +6289,7 @@ remoteDispatchConnectNodeDeviceEventDeregisterAny(virNetServerPtr server G_GNUC_ struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client); virConnectPtr conn = remoteGetNodeDevConn(client); - - virMutexLock(&priv->lock); + g_autoptr(GMutexLocker) locker = g_mutex_locker_new(&priv->lock); if (!conn) goto cleanup; @@ -6357,7 +6314,6 @@ remoteDispatchConnectNodeDeviceEventDeregisterAny(virNetServerPtr server G_GNUC_ rv = 0; cleanup: - virMutexUnlock(&priv->lock); if (rv < 0) virNetMessageSaveError(rerr); return rv; @@ -6379,8 +6335,7 @@ remoteDispatchConnectSecretEventRegisterAny(virNetServerPtr server G_GNUC_UNUSED virNetServerClientGetPrivateData(client); virSecretPtr secret = NULL; virConnectPtr conn = remoteGetSecretConn(client); - - virMutexLock(&priv->lock); + g_autoptr(GMutexLocker) locker = g_mutex_locker_new(&priv->lock); if (!conn) goto cleanup; @@ -6431,7 +6386,6 @@ remoteDispatchConnectSecretEventRegisterAny(virNetServerPtr server G_GNUC_UNUSED rv = 0; cleanup: - virMutexUnlock(&priv->lock); remoteEventCallbackFree(callback); if (rv < 0) virNetMessageSaveError(rerr); @@ -6451,8 +6405,7 @@ remoteDispatchConnectSecretEventDeregisterAny(virNetServerPtr server G_GNUC_UNUS struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client); virConnectPtr conn = remoteGetSecretConn(client); - - virMutexLock(&priv->lock); + g_autoptr(GMutexLocker) locker = g_mutex_locker_new(&priv->lock); if (!conn) goto cleanup; @@ -6477,7 +6430,6 @@ remoteDispatchConnectSecretEventDeregisterAny(virNetServerPtr server G_GNUC_UNUS rv = 0; cleanup: - virMutexUnlock(&priv->lock); if (rv < 0) virNetMessageSaveError(rerr); return rv; @@ -6500,8 +6452,7 @@ qemuDispatchConnectDomainMonitorEventRegister(virNetServerPtr server G_GNUC_UNUS virDomainPtr dom = NULL; const char *event = args->event ? *args->event : NULL; virConnectPtr conn = remoteGetHypervisorConn(client); - - virMutexLock(&priv->lock); + g_autoptr(GMutexLocker) locker = g_mutex_locker_new(&priv->lock); if (!conn) goto cleanup; @@ -6547,7 +6498,6 @@ qemuDispatchConnectDomainMonitorEventRegister(virNetServerPtr server G_GNUC_UNUS rv = 0; cleanup: - virMutexUnlock(&priv->lock); remoteEventCallbackFree(callback); if (rv < 0) virNetMessageSaveError(rerr); @@ -6568,8 +6518,7 @@ qemuDispatchConnectDomainMonitorEventDeregister(virNetServerPtr server G_GNUC_UN struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client); virConnectPtr conn = remoteGetHypervisorConn(client); - - virMutexLock(&priv->lock); + g_autoptr(GMutexLocker) locker = g_mutex_locker_new(&priv->lock); if (!conn) goto cleanup; @@ -6595,7 +6544,6 @@ qemuDispatchConnectDomainMonitorEventDeregister(virNetServerPtr server G_GNUC_UN rv = 0; cleanup: - virMutexUnlock(&priv->lock); if (rv < 0) virNetMessageSaveError(rerr); return rv; diff --git a/src/remote/remote_daemon_stream.c b/src/remote/remote_daemon_stream.c index 73e4d7befb..11f6d3c14e 100644 --- a/src/remote/remote_daemon_stream.c +++ b/src/remote/remote_daemon_stream.c @@ -119,8 +119,7 @@ daemonStreamEvent(virStreamPtr st, int events, void *opaque) virNetServerClientPtr client = opaque; daemonClientStream *stream; daemonClientPrivatePtr priv = virNetServerClientGetPrivateData(client); - - virMutexLock(&priv->lock); + g_autoptr(GMutexLocker) locker = g_mutex_locker_new(&priv->lock); stream = priv->streams; while (stream) { @@ -132,7 +131,7 @@ daemonStreamEvent(virStreamPtr st, int events, void *opaque) if (!stream) { VIR_WARN("event for client=%p stream st=%p, but missing stream state", client, st); virStreamEventRemoveCallback(st); - goto cleanup; + return; } VIR_DEBUG("st=%p events=%d EOF=%d closed=%d", st, events, stream->recvEOF, stream->closed); @@ -142,7 +141,7 @@ daemonStreamEvent(virStreamPtr st, int events, void *opaque) if (daemonStreamHandleWrite(client, stream) < 0) { daemonRemoveClientStream(client, stream); virNetServerClientClose(client); - goto cleanup; + return; } } @@ -152,7 +151,7 @@ daemonStreamEvent(virStreamPtr st, int events, void *opaque) if (daemonStreamHandleRead(client, stream) < 0) { daemonRemoveClientStream(client, stream); virNetServerClientClose(client); - goto cleanup; + return; } /* If we detected EOF during read processing, * then clear hangup/error conditions, since @@ -177,7 +176,7 @@ daemonStreamEvent(virStreamPtr st, int events, void *opaque) virNetMessageFree(msg); daemonRemoveClientStream(client, stream); virNetServerClientClose(client); - goto cleanup; + return; } break; case VIR_NET_ERROR: @@ -187,7 +186,7 @@ daemonStreamEvent(virStreamPtr st, int events, void *opaque) virNetMessageFree(msg); daemonRemoveClientStream(client, stream); virNetServerClientClose(client); - goto cleanup; + return; } break; } @@ -206,7 +205,7 @@ daemonStreamEvent(virStreamPtr st, int events, void *opaque) if (!(msg = virNetMessageNew(false))) { daemonRemoveClientStream(client, stream); virNetServerClientClose(client); - goto cleanup; + return; } msg->cb = daemonStreamMessageFinished; msg->opaque = stream; @@ -220,7 +219,7 @@ daemonStreamEvent(virStreamPtr st, int events, void *opaque) virNetMessageFree(msg); daemonRemoveClientStream(client, stream); virNetServerClientClose(client); - goto cleanup; + return; } } @@ -263,7 +262,7 @@ daemonStreamEvent(virStreamPtr st, int events, void *opaque) daemonRemoveClientStream(client, stream); if (ret < 0) virNetServerClientClose(client); - goto cleanup; + return; } if (stream->closed) { @@ -271,9 +270,6 @@ daemonStreamEvent(virStreamPtr st, int events, void *opaque) } else { daemonStreamUpdateEvents(stream); } - - cleanup: - virMutexUnlock(&priv->lock); } @@ -292,6 +288,7 @@ daemonStreamFilter(virNetServerClientPtr client, { daemonClientStream *stream = opaque; int ret = 0; + g_autoptr(GMutexLocker) locker = g_mutex_locker_new(&stream->priv->lock); /* We must honour lock ordering here. Client private data lock must * be acquired before client lock. Bu we are already called with @@ -300,7 +297,6 @@ daemonStreamFilter(virNetServerClientPtr client, * implications though. */ stream->refs++; virObjectUnlock(client); - virMutexLock(&stream->priv->lock); virObjectLock(client); if (stream->refs == 1) { @@ -332,7 +328,6 @@ daemonStreamFilter(virNetServerClientPtr client, ret = 1; cleanup: - virMutexUnlock(&stream->priv->lock); /* Don't pass client here, because client is locked here and this * function might try to lock it again which would result in a * deadlock. */ @@ -460,13 +455,13 @@ int daemonAddClientStream(virNetServerClientPtr client, if (transmit) stream->tx = true; - virMutexLock(&priv->lock); + g_mutex_lock(&priv->lock); stream->next = priv->streams; priv->streams = stream; daemonStreamUpdateEvents(stream); - virMutexUnlock(&priv->lock); + g_mutex_unlock(&priv->lock); return 0; } -- 2.25.2

Signed-off-by: Rafael Fonseca <r4f4rfs@gmail.com> --- src/util/virfirewall.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/util/virfirewall.c b/src/util/virfirewall.c index 22760e6389..3bf85020af 100644 --- a/src/util/virfirewall.c +++ b/src/util/virfirewall.c @@ -83,7 +83,7 @@ struct _virFirewall { }; static virFirewallBackend currentBackend = VIR_FIREWALL_BACKEND_AUTOMATIC; -static virMutex ruleLock = VIR_MUTEX_INITIALIZER; +G_LOCK_DEFINE_STATIC(ruleLock); static int virFirewallValidateBackend(virFirewallBackend backend); @@ -807,7 +807,7 @@ virFirewallApply(virFirewallPtr firewall) size_t i, j; int ret = -1; - virMutexLock(&ruleLock); + G_LOCK(ruleLock); if (currentBackend == VIR_FIREWALL_BACKEND_AUTOMATIC) { /* a specific backend should have been set when the firewall @@ -863,6 +863,6 @@ virFirewallApply(virFirewallPtr firewall) ret = 0; cleanup: - virMutexUnlock(&ruleLock); + G_UNLOCK(ruleLock); return ret; } -- 2.25.2

Signed-off-by: Rafael Fonseca <r4f4rfs@gmail.com> --- src/util/virlockspace.c | 83 +++++++++++------------------------------ 1 file changed, 22 insertions(+), 61 deletions(-) diff --git a/src/util/virlockspace.c b/src/util/virlockspace.c index 9a54192cd1..c29d6f37fe 100644 --- a/src/util/virlockspace.c +++ b/src/util/virlockspace.c @@ -56,7 +56,7 @@ struct _virLockSpaceResource { struct _virLockSpace { char *dir; - virMutex lock; + GMutex lock; virHashTablePtr resources; }; @@ -244,12 +244,7 @@ virLockSpacePtr virLockSpaceNew(const char *directory) if (VIR_ALLOC(lockspace) < 0) return NULL; - if (virMutexInit(&lockspace->lock) < 0) { - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", - _("Unable to initialize lockspace mutex")); - VIR_FREE(lockspace); - return NULL; - } + g_mutex_init(&lockspace->lock); lockspace->dir = g_strdup(directory); @@ -295,12 +290,7 @@ virLockSpacePtr virLockSpaceNewPostExecRestart(virJSONValuePtr object) if (VIR_ALLOC(lockspace) < 0) return NULL; - if (virMutexInit(&lockspace->lock) < 0) { - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", - _("Unable to initialize lockspace mutex")); - VIR_FREE(lockspace); - return NULL; - } + g_mutex_init(&lockspace->lock); if (!(lockspace->resources = virHashCreate(VIR_LOCKSPACE_TABLE_SIZE, virLockSpaceResourceDataFree))) @@ -430,8 +420,7 @@ virJSONValuePtr virLockSpacePreExecRestart(virLockSpacePtr lockspace) virJSONValuePtr object = virJSONValueNewObject(); virJSONValuePtr resources; virHashKeyValuePairPtr pairs = NULL, tmp; - - virMutexLock(&lockspace->lock); + g_autoptr(GMutexLocker) locker = g_mutex_locker_new(&lockspace->lock); if (lockspace->dir && virJSONValueObjectAppendString(object, "directory", lockspace->dir) < 0) @@ -491,13 +480,11 @@ virJSONValuePtr virLockSpacePreExecRestart(virLockSpacePtr lockspace) } VIR_FREE(pairs); - virMutexUnlock(&lockspace->lock); return object; error: VIR_FREE(pairs); virJSONValueFree(object); - virMutexUnlock(&lockspace->lock); return NULL; } @@ -509,7 +496,7 @@ void virLockSpaceFree(virLockSpacePtr lockspace) virHashFree(lockspace->resources); VIR_FREE(lockspace->dir); - virMutexDestroy(&lockspace->lock); + g_mutex_clear(&lockspace->lock); VIR_FREE(lockspace); } @@ -525,11 +512,10 @@ int virLockSpaceCreateResource(virLockSpacePtr lockspace, { int ret = -1; char *respath = NULL; + g_autoptr(GMutexLocker) locker = g_mutex_locker_new(&lockspace->lock); VIR_DEBUG("lockspace=%p resname=%s", lockspace, resname); - virMutexLock(&lockspace->lock); - if (virHashLookup(lockspace->resources, resname) != NULL) { virReportError(VIR_ERR_RESOURCE_BUSY, _("Lockspace resource '%s' is locked"), @@ -546,7 +532,6 @@ int virLockSpaceCreateResource(virLockSpacePtr lockspace, ret = 0; cleanup: - virMutexUnlock(&lockspace->lock); VIR_FREE(respath); return ret; } @@ -557,11 +542,10 @@ int virLockSpaceDeleteResource(virLockSpacePtr lockspace, { int ret = -1; char *respath = NULL; + g_autoptr(GMutexLocker) locker = g_mutex_locker_new(&lockspace->lock); VIR_DEBUG("lockspace=%p resname=%s", lockspace, resname); - virMutexLock(&lockspace->lock); - if (virHashLookup(lockspace->resources, resname) != NULL) { virReportError(VIR_ERR_RESOURCE_BUSY, _("Lockspace resource '%s' is locked"), @@ -583,7 +567,6 @@ int virLockSpaceDeleteResource(virLockSpacePtr lockspace, ret = 0; cleanup: - virMutexUnlock(&lockspace->lock); VIR_FREE(respath); return ret; } @@ -594,8 +577,8 @@ int virLockSpaceAcquireResource(virLockSpacePtr lockspace, pid_t owner, unsigned int flags) { - int ret = -1; virLockSpaceResourcePtr res; + g_autoptr(GMutexLocker) locker = g_mutex_locker_new(&lockspace->lock); VIR_DEBUG("lockspace=%p resname=%s flags=0x%x owner=%lld", lockspace, resname, flags, (unsigned long long)owner); @@ -603,38 +586,31 @@ int virLockSpaceAcquireResource(virLockSpacePtr lockspace, virCheckFlags(VIR_LOCK_SPACE_ACQUIRE_SHARED | VIR_LOCK_SPACE_ACQUIRE_AUTOCREATE, -1); - virMutexLock(&lockspace->lock); - if ((res = virHashLookup(lockspace->resources, resname))) { if ((res->flags & VIR_LOCK_SPACE_ACQUIRE_SHARED) && (flags & VIR_LOCK_SPACE_ACQUIRE_SHARED)) { if (VIR_EXPAND_N(res->owners, res->nOwners, 1) < 0) - goto cleanup; + return -1; res->owners[res->nOwners-1] = owner; - goto done; + return 0; } virReportError(VIR_ERR_RESOURCE_BUSY, _("Lockspace resource '%s' is locked"), resname); - goto cleanup; + return -1; } if (!(res = virLockSpaceResourceNew(lockspace, resname, flags, owner))) - goto cleanup; + return -1; if (virHashAddEntry(lockspace->resources, resname, res) < 0) { virLockSpaceResourceFree(res); - goto cleanup; + return -1; } - done: - ret = 0; - - cleanup: - virMutexUnlock(&lockspace->lock); - return ret; + return 0; } @@ -642,20 +618,18 @@ int virLockSpaceReleaseResource(virLockSpacePtr lockspace, const char *resname, pid_t owner) { - int ret = -1; virLockSpaceResourcePtr res; size_t i; + g_autoptr(GMutexLocker) locker = g_mutex_locker_new(&lockspace->lock); VIR_DEBUG("lockspace=%p resname=%s owner=%lld", lockspace, resname, (unsigned long long)owner); - virMutexLock(&lockspace->lock); - if (!(res = virHashLookup(lockspace->resources, resname))) { virReportError(VIR_ERR_RESOURCE_BUSY, _("Lockspace resource '%s' is not locked"), resname); - goto cleanup; + return -1; } for (i = 0; i < res->nOwners; i++) { @@ -667,20 +641,16 @@ int virLockSpaceReleaseResource(virLockSpacePtr lockspace, virReportError(VIR_ERR_INTERNAL_ERROR, _("owner %lld does not hold the resource lock"), (unsigned long long)owner); - goto cleanup; + return -1; } VIR_DELETE_ELEMENT(res->owners, i, res->nOwners); if ((res->nOwners == 0) && virHashRemoveEntry(lockspace->resources, resname) < 0) - goto cleanup; - - ret = 0; + return -1; - cleanup: - virMutexUnlock(&lockspace->lock); - return ret; + return 0; } @@ -726,26 +696,17 @@ virLockSpaceRemoveResourcesForOwner(const void *payload, int virLockSpaceReleaseResourcesForOwner(virLockSpacePtr lockspace, pid_t owner) { - int ret = 0; struct virLockSpaceRemoveData data = { owner, 0 }; + g_autoptr(GMutexLocker) locker = g_mutex_locker_new(&lockspace->lock); VIR_DEBUG("lockspace=%p owner=%lld", lockspace, (unsigned long long)owner); - virMutexLock(&lockspace->lock); - if (virHashRemoveSet(lockspace->resources, virLockSpaceRemoveResourcesForOwner, &data) < 0) - goto error; + return -1; - ret = data.count; - - virMutexUnlock(&lockspace->lock); - return ret; - - error: - virMutexUnlock(&lockspace->lock); - return -1; + return data.count; } -- 2.25.2

Signed-off-by: Rafael Fonseca <r4f4rfs@gmail.com> --- src/util/virnetdevmacvlan.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/util/virnetdevmacvlan.c b/src/util/virnetdevmacvlan.c index dcea93a5fe..bcdb2322b5 100644 --- a/src/util/virnetdevmacvlan.c +++ b/src/util/virnetdevmacvlan.c @@ -71,7 +71,7 @@ VIR_LOG_INIT("util.netdevmacvlan"); # define MACVLAN_MAX_ID 8191 -virMutex virNetDevMacVLanCreateMutex = VIR_MUTEX_INITIALIZER; +G_LOCK_DEFINE(virNetDevMacVLanCreateMutex); virBitmapPtr macvtapIDs = NULL; virBitmapPtr macvlanIDs = NULL; @@ -951,10 +951,10 @@ virNetDevMacVLanCreateWithVPortProfile(const char *ifnameRequested, STRPREFIX(ifnameRequested, VIR_NET_GENERATED_MACVLAN_PREFIX)); VIR_INFO("Requested macvtap device name: %s", ifnameRequested); - virMutexLock(&virNetDevMacVLanCreateMutex); + G_LOCK(virNetDevMacVLanCreateMutex); if ((rc = virNetDevExists(ifnameRequested)) < 0) { - virMutexUnlock(&virNetDevMacVLanCreateMutex); + G_UNLOCK(virNetDevMacVLanCreateMutex); return -1; } if (rc) { @@ -963,7 +963,7 @@ virNetDevMacVLanCreateWithVPortProfile(const char *ifnameRequested, virReportSystemError(EEXIST, _("Unable to create %s device %s"), type, ifnameRequested); - virMutexUnlock(&virNetDevMacVLanCreateMutex); + G_UNLOCK(virNetDevMacVLanCreateMutex); return -1; } if (isAutoName && @@ -979,28 +979,28 @@ virNetDevMacVLanCreateWithVPortProfile(const char *ifnameRequested, reservedID = -1; goto create_name; } - virMutexUnlock(&virNetDevMacVLanCreateMutex); + G_UNLOCK(virNetDevMacVLanCreateMutex); return -1; } /* virNetDevMacVLanCreate() was successful - use this name */ ifnameCreated = ifnameRequested; create_name: - virMutexUnlock(&virNetDevMacVLanCreateMutex); + G_UNLOCK(virNetDevMacVLanCreateMutex); } retries = MACVLAN_MAX_ID; while (!ifnameCreated && retries) { - virMutexLock(&virNetDevMacVLanCreateMutex); + G_LOCK(virNetDevMacVLanCreateMutex); reservedID = virNetDevMacVLanReserveID(reservedID, flags, false, true); if (reservedID < 0) { - virMutexUnlock(&virNetDevMacVLanCreateMutex); + G_UNLOCK(virNetDevMacVLanCreateMutex); return -1; } g_snprintf(ifname, sizeof(ifname), pattern, reservedID); if (virNetDevMacVLanCreate(ifname, type, macaddress, linkdev, macvtapMode, &do_retry) < 0) { virNetDevMacVLanReleaseID(reservedID, flags); - virMutexUnlock(&virNetDevMacVLanCreateMutex); + G_UNLOCK(virNetDevMacVLanCreateMutex); if (!do_retry) return -1; VIR_INFO("Device %s wasn't reserved but already existed, skipping", @@ -1009,7 +1009,7 @@ virNetDevMacVLanCreateWithVPortProfile(const char *ifnameRequested, continue; } ifnameCreated = ifname; - virMutexUnlock(&virNetDevMacVLanCreateMutex); + G_UNLOCK(virNetDevMacVLanCreateMutex); } if (!ifnameCreated) { -- 2.25.2

Signed-off-by: Rafael Fonseca <r4f4rfs@gmail.com> --- src/util/virnetdevveth.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/util/virnetdevveth.c b/src/util/virnetdevveth.c index b3eee1afce..d1fc4c372d 100644 --- a/src/util/virnetdevveth.c +++ b/src/util/virnetdevveth.c @@ -34,7 +34,7 @@ VIR_LOG_INIT("util.netdevveth"); /* Functions */ -virMutex virNetDevVethCreateMutex = VIR_MUTEX_INITIALIZER; +G_LOCK_DEFINE(virNetDevVethCreateMutex); static int virNetDevVethExists(int devNum) { @@ -110,7 +110,7 @@ int virNetDevVethCreate(char** veth1, char** veth2) * We might race with other containers, but this is reasonably * unlikely, so don't do too many retries for device creation */ - virMutexLock(&virNetDevVethCreateMutex); + G_LOCK(virNetDevVethCreateMutex); #define MAX_VETH_RETRIES 10 for (i = 0; i < MAX_VETH_RETRIES; i++) { @@ -171,7 +171,7 @@ int virNetDevVethCreate(char** veth1, char** veth2) MAX_VETH_RETRIES); cleanup: - virMutexUnlock(&virNetDevVethCreateMutex); + G_UNLOCK(virNetDevVethCreateMutex); return ret; } -- 2.25.2

Signed-off-by: Rafael Fonseca <r4f4rfs@gmail.com> --- src/util/virnetlink.c | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/src/util/virnetlink.c b/src/util/virnetlink.c index 3117dcbe65..e9f2c273d9 100644 --- a/src/util/virnetlink.c +++ b/src/util/virnetlink.c @@ -62,7 +62,7 @@ typedef struct _virNetlinkEventSrvPrivate virNetlinkEventSrvPrivate; typedef virNetlinkEventSrvPrivate *virNetlinkEventSrvPrivatePtr; struct _virNetlinkEventSrvPrivate { /* Server */ - virMutex lock; + GMutex lock; int eventwatch; int netlinkfd; virNetlinkHandle *netlinknh; @@ -791,13 +791,13 @@ virNetlinkGetErrorCode(struct nlmsghdr *resp, unsigned int recvbuflen) static void virNetlinkEventServerLock(virNetlinkEventSrvPrivatePtr driver) { - virMutexLock(&driver->lock); + g_mutex_lock(&driver->lock); } static void virNetlinkEventServerUnlock(virNetlinkEventSrvPrivatePtr driver) { - virMutexUnlock(&driver->lock); + g_mutex_unlock(&driver->lock); } /** @@ -916,7 +916,7 @@ virNetlinkEventServiceStop(unsigned int protocol) VIR_FREE(srv->handles); virNetlinkEventServerUnlock(srv); - virMutexDestroy(&srv->lock); + g_mutex_clear(&srv->lock); VIR_FREE(srv); return 0; } @@ -1017,10 +1017,7 @@ virNetlinkEventServiceStart(unsigned int protocol, unsigned int groups) if (VIR_ALLOC(srv) < 0) return -1; - if (virMutexInit(&srv->lock) < 0) { - VIR_FREE(srv); - return -1; - } + g_mutex_init(&srv->lock); virNetlinkEventServerLock(srv); @@ -1070,7 +1067,7 @@ virNetlinkEventServiceStart(unsigned int protocol, unsigned int groups) error_locked: virNetlinkEventServerUnlock(srv); if (ret < 0) { - virMutexDestroy(&srv->lock); + g_mutex_clear(&srv->lock); VIR_FREE(srv); } return ret; -- 2.25.2

Signed-off-by: Rafael Fonseca <r4f4rfs@gmail.com> --- tools/virsh.c | 11 ++++------- tools/virt-admin.c | 11 ++++------- tools/vsh.c | 4 ++-- tools/vsh.h | 2 +- 4 files changed, 11 insertions(+), 17 deletions(-) diff --git a/tools/virsh.c b/tools/virsh.c index 197a90636d..f2da8ad340 100644 --- a/tools/virsh.c +++ b/tools/virsh.c @@ -414,11 +414,11 @@ virshDeinit(vshControl *ctl) if (ctl->eventLoopStarted) { int timer; - virMutexLock(&ctl->lock); + g_mutex_lock(&ctl->lock); ctl->quit = true; /* HACK: Add a dummy timeout to break event loop */ timer = virEventAddTimeout(0, virshDeinitTimer, NULL, NULL); - virMutexUnlock(&ctl->lock); + g_mutex_unlock(&ctl->lock); virThreadJoin(&ctl->eventLoop); @@ -431,7 +431,7 @@ virshDeinit(vshControl *ctl) ctl->eventLoopStarted = false; } - virMutexDestroy(&ctl->lock); + g_mutex_clear(&ctl->lock); return true; } @@ -877,10 +877,7 @@ main(int argc, char **argv) #endif } - if (virMutexInit(&ctl->lock) < 0) { - vshError(ctl, "%s", _("Failed to initialize mutex")); - return EXIT_FAILURE; - } + g_mutex_init(&ctl->lock); if (virInitialize() < 0) { vshError(ctl, "%s", _("Failed to initialize libvirt")); diff --git a/tools/virt-admin.c b/tools/virt-admin.c index a8e5e0a5af..712bac07df 100644 --- a/tools/virt-admin.c +++ b/tools/virt-admin.c @@ -1203,11 +1203,11 @@ vshAdmDeinit(vshControl *ctl) if (ctl->eventLoopStarted) { int timer; - virMutexLock(&ctl->lock); + g_mutex_lock(&ctl->lock); ctl->quit = true; /* HACK: Add a dummy timeout to break event loop */ timer = virEventAddTimeout(0, vshAdmDeinitTimer, NULL, NULL); - virMutexUnlock(&ctl->lock); + g_mutex_unlock(&ctl->lock); virThreadJoin(&ctl->eventLoop); @@ -1217,7 +1217,7 @@ vshAdmDeinit(vshControl *ctl) ctl->eventLoopStarted = false; } - virMutexDestroy(&ctl->lock); + g_mutex_clear(&ctl->lock); } /* @@ -1563,10 +1563,7 @@ main(int argc, char **argv) #endif } - if (virMutexInit(&ctl->lock) < 0) { - vshError(ctl, "%s", _("Failed to initialize mutex")); - return EXIT_FAILURE; - } + g_mutex_init(&ctl->lock); if (virAdmInitialize() < 0) { vshError(ctl, "%s", _("Failed to initialize libvirt")); diff --git a/tools/vsh.c b/tools/vsh.c index 58bb1e6a3c..b803dd6aa5 100644 --- a/tools/vsh.c +++ b/tools/vsh.c @@ -1989,9 +1989,9 @@ vshEventLoop(void *opaque) while (1) { bool quit; - virMutexLock(&ctl->lock); + g_mutex_lock(&ctl->lock); quit = ctl->quit; - virMutexUnlock(&ctl->lock); + g_mutex_unlock(&ctl->lock); if (quit) break; diff --git a/tools/vsh.h b/tools/vsh.h index 174116b369..49e1fef06e 100644 --- a/tools/vsh.h +++ b/tools/vsh.h @@ -208,7 +208,7 @@ struct _vshControl { char *historydir; /* readline history directory name */ char *historyfile; /* readline history file name */ virThread eventLoop; - virMutex lock; + GMutex lock; bool eventLoopStarted; bool quit; int eventPipe[2]; /* Write-to-self pipe to end waiting for an -- 2.25.2

Signed-off-by: Rafael Fonseca <r4f4rfs@gmail.com> --- src/nwfilter/nwfilter_learnipaddr.c | 43 +++++++++++++---------------- 1 file changed, 19 insertions(+), 24 deletions(-) diff --git a/src/nwfilter/nwfilter_learnipaddr.c b/src/nwfilter/nwfilter_learnipaddr.c index 4ce8d5ba03..c46fbca40d 100644 --- a/src/nwfilter/nwfilter_learnipaddr.c +++ b/src/nwfilter/nwfilter_learnipaddr.c @@ -111,17 +111,17 @@ struct ether_vlan_header } ATTRIBUTE_PACKED; -static virMutex pendingLearnReqLock = VIR_MUTEX_INITIALIZER; +G_LOCK_DEFINE_STATIC(pendingLearnReqLock); static virHashTablePtr pendingLearnReq; -static virMutex ifaceMapLock = VIR_MUTEX_INITIALIZER; +G_LOCK_DEFINE_STATIC(ifaceMapLock); static virHashTablePtr ifaceLockMap; typedef struct _virNWFilterIfaceLock virNWFilterIfaceLock; typedef virNWFilterIfaceLock *virNWFilterIfaceLockPtr; struct _virNWFilterIfaceLock { char ifname[IF_NAMESIZE]; - virMutex lock; + GRecMutex lock; int refctr; }; @@ -147,19 +147,14 @@ virNWFilterLockIface(const char *ifname) { virNWFilterIfaceLockPtr ifaceLock; - virMutexLock(&ifaceMapLock); + G_LOCK(ifaceMapLock); ifaceLock = virHashLookup(ifaceLockMap, ifname); if (!ifaceLock) { if (VIR_ALLOC(ifaceLock) < 0) goto err_exit; - if (virMutexInitRecursive(&ifaceLock->lock) < 0) { - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", - _("mutex initialization failed")); - VIR_FREE(ifaceLock); - goto err_exit; - } + g_rec_mutex_init(&ifaceLock->lock); if (virStrcpyStatic(ifaceLock->ifname, ifname) < 0) { virReportError(VIR_ERR_INTERNAL_ERROR, @@ -180,14 +175,14 @@ virNWFilterLockIface(const char *ifname) ifaceLock->refctr++; - virMutexUnlock(&ifaceMapLock); + G_UNLOCK(ifaceMapLock); - virMutexLock(&ifaceLock->lock); + g_rec_mutex_lock(&ifaceLock->lock); return 0; err_exit: - virMutexUnlock(&ifaceMapLock); + G_UNLOCK(ifaceMapLock); return -1; } @@ -198,19 +193,19 @@ virNWFilterUnlockIface(const char *ifname) { virNWFilterIfaceLockPtr ifaceLock; - virMutexLock(&ifaceMapLock); + G_LOCK(ifaceMapLock); ifaceLock = virHashLookup(ifaceLockMap, ifname); if (ifaceLock) { - virMutexUnlock(&ifaceLock->lock); + g_rec_mutex_unlock(&ifaceLock->lock); ifaceLock->refctr--; if (ifaceLock->refctr == 0) virHashRemoveEntry(ifaceLockMap, ifname); } - virMutexUnlock(&ifaceMapLock); + G_UNLOCK(ifaceMapLock); } @@ -234,12 +229,12 @@ virNWFilterRegisterLearnReq(virNWFilterIPAddrLearnReqPtr req) int res = -1; g_autofree char *ifindex_str = g_strdup_printf("%d", req->ifindex); - virMutexLock(&pendingLearnReqLock); + G_LOCK(pendingLearnReqLock); if (!virHashLookup(pendingLearnReq, ifindex_str)) res = virHashAddEntry(pendingLearnReq, ifindex_str, req); - virMutexUnlock(&pendingLearnReqLock); + G_UNLOCK(pendingLearnReqLock); return res; } @@ -270,7 +265,7 @@ virNWFilterTerminateLearnReq(const char *ifname) ifindex_str = g_strdup_printf("%d", ifindex); - virMutexLock(&pendingLearnReqLock); + G_LOCK(pendingLearnReqLock); req = virHashLookup(pendingLearnReq, ifindex_str); if (req) { @@ -278,7 +273,7 @@ virNWFilterTerminateLearnReq(const char *ifname) req->terminate = true; } - virMutexUnlock(&pendingLearnReqLock); + G_UNLOCK(pendingLearnReqLock); return rc; } @@ -290,11 +285,11 @@ virNWFilterHasLearnReq(int ifindex) void *res; g_autofree char *ifindex_str = g_strdup_printf("%d", ifindex); - virMutexLock(&pendingLearnReqLock); + G_LOCK(pendingLearnReqLock); res = virHashLookup(pendingLearnReq, ifindex_str); - virMutexUnlock(&pendingLearnReqLock); + G_UNLOCK(pendingLearnReqLock); return res != NULL; } @@ -315,11 +310,11 @@ virNWFilterDeregisterLearnReq(int ifindex) virNWFilterIPAddrLearnReqPtr res; g_autofree char *ifindex_str = g_strdup_printf("%d", ifindex); - virMutexLock(&pendingLearnReqLock); + G_LOCK(pendingLearnReqLock); res = virHashSteal(pendingLearnReq, ifindex_str); - virMutexUnlock(&pendingLearnReqLock); + G_UNLOCK(pendingLearnReqLock); return res; } -- 2.25.2

Signed-off-by: Rafael Fonseca <r4f4rfs@gmail.com> --- src/util/virthreadpool.c | 149 +++++++++++++-------------------------- 1 file changed, 50 insertions(+), 99 deletions(-) diff --git a/src/util/virthreadpool.c b/src/util/virthreadpool.c index 379d2369ad..147943f395 100644 --- a/src/util/virthreadpool.c +++ b/src/util/virthreadpool.c @@ -59,9 +59,9 @@ struct _virThreadPool { virThreadPoolJobList jobList; size_t jobQueueDepth; - virMutex mutex; - virCond cond; - virCond quit_cond; + GMutex mutex; + GCond cond; + GCond quit_cond; size_t maxWorkers; size_t minWorkers; @@ -72,12 +72,12 @@ struct _virThreadPool { size_t maxPrioWorkers; size_t nPrioWorkers; virThreadPtr prioWorkers; - virCond prioCond; + GCond prioCond; }; struct virThreadPoolWorkerData { virThreadPoolPtr pool; - virCondPtr cond; + GCond *cond; bool priority; }; @@ -93,7 +93,7 @@ static void virThreadPoolWorker(void *opaque) { struct virThreadPoolWorkerData *data = opaque; virThreadPoolPtr pool = data->pool; - virCondPtr cond = data->cond; + GCond *cond = data->cond; bool priority = data->priority; size_t *curWorkers = priority ? &pool->nPrioWorkers : &pool->nWorkers; size_t *maxLimit = priority ? &pool->maxPrioWorkers : &pool->maxWorkers; @@ -101,7 +101,7 @@ static void virThreadPoolWorker(void *opaque) VIR_FREE(data); - virMutexLock(&pool->mutex); + g_mutex_lock(&pool->mutex); while (1) { /* In order to support async worker termination, we need ensure that @@ -117,11 +117,7 @@ static void virThreadPoolWorker(void *opaque) (priority && !pool->jobList.firstPrio))) { if (!priority) pool->freeWorkers++; - if (virCondWait(cond, &pool->mutex) < 0) { - if (!priority) - pool->freeWorkers--; - goto out; - } + g_cond_wait(cond, &pool->mutex); if (!priority) pool->freeWorkers--; @@ -159,10 +155,10 @@ static void virThreadPoolWorker(void *opaque) pool->jobQueueDepth--; - virMutexUnlock(&pool->mutex); + g_mutex_unlock(&pool->mutex); (pool->jobFunc)(job->data, pool->jobOpaque); VIR_FREE(job); - virMutexLock(&pool->mutex); + g_mutex_lock(&pool->mutex); } out: @@ -171,8 +167,8 @@ static void virThreadPoolWorker(void *opaque) else pool->nWorkers--; if (pool->nWorkers == 0 && pool->nPrioWorkers == 0) - virCondSignal(&pool->quit_cond); - virMutexUnlock(&pool->mutex); + g_cond_signal(&pool->quit_cond); + g_mutex_unlock(&pool->mutex); } static int @@ -241,12 +237,9 @@ virThreadPoolNewFull(size_t minWorkers, pool->jobName = name; pool->jobOpaque = opaque; - if (virMutexInit(&pool->mutex) < 0) - goto error; - if (virCondInit(&pool->cond) < 0) - goto error; - if (virCondInit(&pool->quit_cond) < 0) - goto error; + g_mutex_init(&pool->mutex); + g_cond_init(&pool->cond); + g_cond_init(&pool->quit_cond); pool->minWorkers = minWorkers; pool->maxWorkers = maxWorkers; @@ -256,8 +249,7 @@ virThreadPoolNewFull(size_t minWorkers, goto error; if (prioWorkers) { - if (virCondInit(&pool->prioCond) < 0) - goto error; + g_cond_init(&pool->prioCond); if (virThreadPoolExpand(pool, prioWorkers, true) < 0) goto error; @@ -279,17 +271,17 @@ void virThreadPoolFree(virThreadPoolPtr pool) if (!pool) return; - virMutexLock(&pool->mutex); + g_mutex_lock(&pool->mutex); pool->quit = true; if (pool->nWorkers > 0) - virCondBroadcast(&pool->cond); + g_cond_broadcast(&pool->cond); if (pool->nPrioWorkers > 0) { priority = true; - virCondBroadcast(&pool->prioCond); + g_cond_broadcast(&pool->prioCond); } while (pool->nWorkers > 0 || pool->nPrioWorkers > 0) - ignore_value(virCondWait(&pool->quit_cond, &pool->mutex)); + g_cond_wait(&pool->quit_cond, &pool->mutex); while ((job = pool->jobList.head)) { pool->jobList.head = pool->jobList.head->next; @@ -297,13 +289,13 @@ void virThreadPoolFree(virThreadPoolPtr pool) } VIR_FREE(pool->workers); - virMutexUnlock(&pool->mutex); - virMutexDestroy(&pool->mutex); - virCondDestroy(&pool->quit_cond); - virCondDestroy(&pool->cond); + g_mutex_unlock(&pool->mutex); + g_mutex_clear(&pool->mutex); + g_cond_clear(&pool->quit_cond); + g_cond_clear(&pool->cond); if (priority) { VIR_FREE(pool->prioWorkers); - virCondDestroy(&pool->prioCond); + g_cond_clear(&pool->prioCond); } VIR_FREE(pool); } @@ -311,68 +303,38 @@ void virThreadPoolFree(virThreadPoolPtr pool) size_t virThreadPoolGetMinWorkers(virThreadPoolPtr pool) { - size_t ret; - - virMutexLock(&pool->mutex); - ret = pool->minWorkers; - virMutexUnlock(&pool->mutex); - - return ret; + g_autoptr(GMutexLocker) locker = g_mutex_locker_new(&pool->mutex); + return pool->minWorkers; } size_t virThreadPoolGetMaxWorkers(virThreadPoolPtr pool) { - size_t ret; - - virMutexLock(&pool->mutex); - ret = pool->maxWorkers; - virMutexUnlock(&pool->mutex); - - return ret; + g_autoptr(GMutexLocker) locker = g_mutex_locker_new(&pool->mutex); + return pool->maxWorkers; } size_t virThreadPoolGetPriorityWorkers(virThreadPoolPtr pool) { - size_t ret; - - virMutexLock(&pool->mutex); - ret = pool->nPrioWorkers; - virMutexUnlock(&pool->mutex); - - return ret; + g_autoptr(GMutexLocker) locker = g_mutex_locker_new(&pool->mutex); + return pool->nPrioWorkers; } size_t virThreadPoolGetCurrentWorkers(virThreadPoolPtr pool) { - size_t ret; - - virMutexLock(&pool->mutex); - ret = pool->nWorkers; - virMutexUnlock(&pool->mutex); - - return ret; + g_autoptr(GMutexLocker) locker = g_mutex_locker_new(&pool->mutex); + return pool->nWorkers; } size_t virThreadPoolGetFreeWorkers(virThreadPoolPtr pool) { - size_t ret; - - virMutexLock(&pool->mutex); - ret = pool->freeWorkers; - virMutexUnlock(&pool->mutex); - - return ret; + g_autoptr(GMutexLocker) locker = g_mutex_locker_new(&pool->mutex); + return pool->freeWorkers; } size_t virThreadPoolGetJobQueueDepth(virThreadPoolPtr pool) { - size_t ret; - - virMutexLock(&pool->mutex); - ret = pool->jobQueueDepth; - virMutexUnlock(&pool->mutex); - - return ret; + g_autoptr(GMutexLocker) locker = g_mutex_locker_new(&pool->mutex); + return pool->jobQueueDepth; } /* @@ -384,18 +346,18 @@ int virThreadPoolSendJob(virThreadPoolPtr pool, void *jobData) { virThreadPoolJobPtr job; + g_autoptr(GMutexLocker) locker = g_mutex_locker_new(&pool->mutex); - virMutexLock(&pool->mutex); if (pool->quit) - goto error; + return -1; if (pool->freeWorkers - pool->jobQueueDepth <= 0 && pool->nWorkers < pool->maxWorkers && virThreadPoolExpand(pool, 1, false) < 0) - goto error; + return -1; if (VIR_ALLOC(job) < 0) - goto error; + return -1; job->data = jobData; job->priority = priority; @@ -413,16 +375,11 @@ int virThreadPoolSendJob(virThreadPoolPtr pool, pool->jobQueueDepth++; - virCondSignal(&pool->cond); + g_cond_signal(&pool->cond); if (priority) - virCondSignal(&pool->prioCond); + g_cond_signal(&pool->prioCond); - virMutexUnlock(&pool->mutex); return 0; - - error: - virMutexUnlock(&pool->mutex); - return -1; } int @@ -433,15 +390,14 @@ virThreadPoolSetParameters(virThreadPoolPtr pool, { size_t max; size_t min; - - virMutexLock(&pool->mutex); + g_autoptr(GMutexLocker) locker = g_mutex_locker_new(&pool->mutex); max = maxWorkers >= 0 ? maxWorkers : pool->maxWorkers; min = minWorkers >= 0 ? minWorkers : pool->minWorkers; if (min > max) { virReportError(VIR_ERR_INVALID_ARG, "%s", _("minWorkers cannot be larger than maxWorkers")); - goto error; + return -1; } if ((maxWorkers == 0 && pool->maxWorkers > 0) || @@ -449,37 +405,32 @@ virThreadPoolSetParameters(virThreadPoolPtr pool, virReportError(VIR_ERR_INVALID_ARG, "%s", _("maxWorkers must not be switched from zero to non-zero" " and vice versa")); - goto error; + return -1; } if (minWorkers >= 0) { if ((size_t) minWorkers > pool->nWorkers && virThreadPoolExpand(pool, minWorkers - pool->nWorkers, false) < 0) - goto error; + return -1; pool->minWorkers = minWorkers; } if (maxWorkers >= 0) { pool->maxWorkers = maxWorkers; - virCondBroadcast(&pool->cond); + g_cond_broadcast(&pool->cond); } if (prioWorkers >= 0) { if (prioWorkers < pool->nPrioWorkers) { - virCondBroadcast(&pool->prioCond); + g_cond_broadcast(&pool->prioCond); } else if ((size_t) prioWorkers > pool->nPrioWorkers && virThreadPoolExpand(pool, prioWorkers - pool->nPrioWorkers, true) < 0) { - goto error; + return -1; } pool->maxPrioWorkers = prioWorkers; } - virMutexUnlock(&pool->mutex); return 0; - - error: - virMutexUnlock(&pool->mutex); - return -1; } -- 2.25.2

Signed-off-by: Rafael Fonseca <r4f4rfs@gmail.com> --- src/conf/virdomainobjlist.c | 35 ++++++++++--------- src/conf/virinterfaceobj.c | 18 +++++----- src/conf/virnetworkobj.c | 20 +++++------ src/conf/virnodedeviceobj.c | 16 ++++----- src/conf/virnwfilterbindingobjlist.c | 14 ++++---- src/conf/virsecretobj.c | 16 ++++----- src/conf/virstorageobj.c | 50 ++++++++++++++-------------- src/libvirt_private.syms | 3 +- src/util/virobject.c | 27 +++++++++------ src/util/virobject.h | 8 +++-- 10 files changed, 110 insertions(+), 97 deletions(-) diff --git a/src/conf/virdomainobjlist.c b/src/conf/virdomainobjlist.c index 417025ae9f..f3d4d81bb1 100644 --- a/src/conf/virdomainobjlist.c +++ b/src/conf/virdomainobjlist.c @@ -120,7 +120,7 @@ virDomainObjListFindByID(virDomainObjListPtr doms, virObjectRWLockRead(doms); obj = virHashSearch(doms->objs, virDomainObjListSearchID, &id, NULL); virObjectRef(obj); - virObjectRWUnlock(doms); + virObjectRWUnlockRead(doms); if (obj) { virObjectLock(obj); if (obj->removing) { @@ -167,7 +167,7 @@ virDomainObjListFindByUUID(virDomainObjListPtr doms, virObjectRWLockRead(doms); obj = virDomainObjListFindByUUIDLocked(doms, uuid); - virObjectRWUnlock(doms); + virObjectRWUnlockRead(doms); if (obj && obj->removing) { virObjectUnlock(obj); @@ -210,7 +210,7 @@ virDomainObjListFindByName(virDomainObjListPtr doms, virObjectRWLockRead(doms); obj = virDomainObjListFindByNameLocked(doms, name); - virObjectRWUnlock(doms); + virObjectRWUnlockRead(doms); if (obj && obj->removing) { virObjectUnlock(obj); @@ -361,7 +361,7 @@ virDomainObjPtr virDomainObjListAdd(virDomainObjListPtr doms, virObjectRWLockWrite(doms); ret = virDomainObjListAddLocked(doms, def, xmlopt, flags, oldDef); - virObjectRWUnlock(doms); + virObjectRWUnlockWrite(doms); return ret; } @@ -409,7 +409,7 @@ virDomainObjListRemove(virDomainObjListPtr doms, virObjectLock(dom); virDomainObjListRemoveLocked(doms, dom); virObjectUnref(dom); - virObjectRWUnlock(doms); + virObjectRWUnlockWrite(doms); } @@ -475,7 +475,7 @@ virDomainObjListRename(virDomainObjListPtr doms, ret = 0; cleanup: - virObjectRWUnlock(doms); + virObjectRWUnlockWrite(doms); VIR_FREE(old_name); return ret; } @@ -634,7 +634,7 @@ virDomainObjListLoadAllConfigs(virDomainObjListPtr doms, } VIR_DIR_CLOSE(dir); - virObjectRWUnlock(doms); + virObjectRWUnlockWrite(doms); return ret; } @@ -680,7 +680,7 @@ virDomainObjListNumOfDomains(virDomainObjListPtr doms, struct virDomainObjListData data = { filter, conn, active, 0 }; virObjectRWLockRead(doms); virHashForEach(doms->objs, virDomainObjListCount, &data); - virObjectRWUnlock(doms); + virObjectRWUnlockRead(doms); return data.count; } @@ -724,7 +724,7 @@ virDomainObjListGetActiveIDs(virDomainObjListPtr doms, 0, maxids, ids }; virObjectRWLockRead(doms); virHashForEach(doms->objs, virDomainObjListCopyActiveIDs, &data); - virObjectRWUnlock(doms); + virObjectRWUnlockRead(doms); return data.numids; } @@ -777,7 +777,7 @@ virDomainObjListGetInactiveNames(virDomainObjListPtr doms, size_t i; virObjectRWLockRead(doms); virHashForEach(doms->objs, virDomainObjListCopyInactiveNames, &data); - virObjectRWUnlock(doms); + virObjectRWUnlockRead(doms); if (data.oom) { for (i = 0; i < data.numnames; i++) VIR_FREE(data.names[i]); @@ -839,7 +839,10 @@ virDomainObjListForEach(virDomainObjListPtr doms, else virObjectRWLockRead(doms); virHashForEach(doms->objs, virDomainObjListHelper, &data); - virObjectRWUnlock(doms); + if (modify) + virObjectRWUnlockWrite(doms); + else + virObjectRWUnlockRead(doms); return data.ret; } @@ -982,12 +985,12 @@ virDomainObjListCollect(virDomainObjListPtr domlist, virObjectRWLockRead(domlist); sa_assert(domlist->objs); if (VIR_ALLOC_N(data.vms, virHashSize(domlist->objs)) < 0) { - virObjectRWUnlock(domlist); + virObjectRWUnlockRead(domlist); return -1; } virHashForEach(domlist->objs, virDomainObjListCollectIterator, &data); - virObjectRWUnlock(domlist); + virObjectRWUnlockRead(domlist); virDomainObjListFilter(&data.vms, &data.nvms, conn, filter, flags); @@ -1026,7 +1029,7 @@ virDomainObjListConvert(virDomainObjListPtr domlist, if (skip_missing) continue; - virObjectRWUnlock(domlist); + virObjectRWUnlockRead(domlist); virReportError(VIR_ERR_NO_DOMAIN, _("no domain with matching uuid '%s' (%s)"), uuidstr, dom->name); @@ -1036,12 +1039,12 @@ virDomainObjListConvert(virDomainObjListPtr domlist, virObjectRef(vm); if (VIR_APPEND_ELEMENT(*vms, *nvms, vm) < 0) { - virObjectRWUnlock(domlist); + virObjectRWUnlockRead(domlist); virObjectUnref(vm); goto error; } } - virObjectRWUnlock(domlist); + virObjectRWUnlockRead(domlist); sa_assert(*vms); virDomainObjListFilter(vms, nvms, conn, filter, flags); diff --git a/src/conf/virinterfaceobj.c b/src/conf/virinterfaceobj.c index 43ba395d2b..f52ff88c2a 100644 --- a/src/conf/virinterfaceobj.c +++ b/src/conf/virinterfaceobj.c @@ -200,7 +200,7 @@ virInterfaceObjListFindByMACString(virInterfaceObjListPtr interfaces, virObjectRWLockRead(interfaces); virHashForEach(interfaces->objsName, virInterfaceObjListFindByMACStringCb, &data); - virObjectRWUnlock(interfaces); + virObjectRWUnlockRead(interfaces); if (data.error) goto error; @@ -230,7 +230,7 @@ virInterfaceObjListFindByName(virInterfaceObjListPtr interfaces, virInterfaceObjPtr obj; virObjectRWLockRead(interfaces); obj = virInterfaceObjListFindByNameLocked(interfaces, name); - virObjectRWUnlock(interfaces); + virObjectRWUnlockRead(interfaces); if (obj) virObjectLock(obj); @@ -337,7 +337,7 @@ virInterfaceObjListExport(virConnectPtr conn, ret = data.nifaces; cleanup: - virObjectRWUnlock(ifaceobjs); + virObjectRWUnlockRead(ifaceobjs); while (data.ifaces && data.nifaces) virObjectUnref(data.ifaces[--data.nifaces]); @@ -413,7 +413,7 @@ virInterfaceObjListClone(virInterfaceObjListPtr interfaces) virObjectRWLockRead(interfaces); virHashForEach(interfaces->objsName, virInterfaceObjListCloneCb, &data); - virObjectRWUnlock(interfaces); + virObjectRWUnlockRead(interfaces); if (data.error) goto error; @@ -445,13 +445,13 @@ virInterfaceObjListAssignDef(virInterfaceObjListPtr interfaces, } obj->def = def; - virObjectRWUnlock(interfaces); + virObjectRWUnlockWrite(interfaces); return obj; error: virInterfaceObjEndAPI(&obj); - virObjectRWUnlock(interfaces); + virObjectRWUnlockWrite(interfaces); return NULL; } @@ -470,7 +470,7 @@ virInterfaceObjListRemove(virInterfaceObjListPtr interfaces, virHashRemoveEntry(interfaces->objsName, obj->def->name); virObjectUnlock(obj); virObjectUnref(obj); - virObjectRWUnlock(interfaces); + virObjectRWUnlockWrite(interfaces); } @@ -507,7 +507,7 @@ virInterfaceObjListNumOfInterfaces(virInterfaceObjListPtr interfaces, virObjectRWLockRead(interfaces); virHashForEach(interfaces->objsName, virInterfaceObjListNumOfInterfacesCb, &data); - virObjectRWUnlock(interfaces); + virObjectRWUnlockRead(interfaces); return data.count; } @@ -562,7 +562,7 @@ virInterfaceObjListGetNames(virInterfaceObjListPtr interfaces, virObjectRWLockRead(interfaces); virHashForEach(interfaces->objsName, virInterfaceObjListGetNamesCb, &data); - virObjectRWUnlock(interfaces); + virObjectRWUnlockRead(interfaces); if (data.error) goto error; diff --git a/src/conf/virnetworkobj.c b/src/conf/virnetworkobj.c index b2affaacd3..d47e2ab793 100644 --- a/src/conf/virnetworkobj.c +++ b/src/conf/virnetworkobj.c @@ -395,7 +395,7 @@ virNetworkObjFindByUUID(virNetworkObjListPtr nets, virObjectRWLockRead(nets); obj = virNetworkObjFindByUUIDLocked(nets, uuid); - virObjectRWUnlock(nets); + virObjectRWUnlockRead(nets); if (obj) virObjectLock(obj); return obj; @@ -449,7 +449,7 @@ virNetworkObjFindByName(virNetworkObjListPtr nets, virObjectRWLockRead(nets); obj = virNetworkObjFindByNameLocked(nets, name); - virObjectRWUnlock(nets); + virObjectRWUnlockRead(nets); if (obj) virObjectLock(obj); return obj; @@ -652,7 +652,7 @@ virNetworkObjAssignDef(virNetworkObjListPtr nets, virObjectRWLockWrite(nets); obj = virNetworkObjAssignDefLocked(nets, def, flags); - virObjectRWUnlock(nets); + virObjectRWUnlockWrite(nets); return obj; } @@ -805,7 +805,7 @@ virNetworkObjRemoveInactive(virNetworkObjListPtr nets, virObjectRWLockWrite(nets); virObjectLock(obj); virHashRemoveEntry(nets->objs, uuidstr); - virObjectRWUnlock(nets); + virObjectRWUnlockWrite(nets); virObjectUnref(obj); } @@ -1218,7 +1218,7 @@ virNetworkObjBridgeInUse(virNetworkObjListPtr nets, virObjectRWLockRead(nets); obj = virHashSearch(nets->objs, virNetworkObjBridgeInUseHelper, &data, NULL); - virObjectRWUnlock(nets); + virObjectRWUnlockRead(nets); return obj != NULL; } @@ -1428,7 +1428,7 @@ virNetworkObjListExport(virConnectPtr conn, ret = data.nnets; cleanup: - virObjectRWUnlock(netobjs); + virObjectRWUnlockWrite(netobjs); while (data.nets && data.nnets) virObjectUnref(data.nets[--data.nnets]); @@ -1478,7 +1478,7 @@ virNetworkObjListForEach(virNetworkObjListPtr nets, .callback = callback, .opaque = opaque, .ret = 0}; virObjectRWLockRead(nets); virHashForEach(nets->objs, virNetworkObjListForEachHelper, &data); - virObjectRWUnlock(nets); + virObjectRWUnlockRead(nets); return data.ret; } @@ -1543,7 +1543,7 @@ virNetworkObjListGetNames(virNetworkObjListPtr nets, virObjectRWLockRead(nets); virHashForEach(nets->objs, virNetworkObjListGetHelper, &data); - virObjectRWUnlock(nets); + virObjectRWUnlockRead(nets); if (data.error) goto cleanup; @@ -1570,7 +1570,7 @@ virNetworkObjListNumOfNetworks(virNetworkObjListPtr nets, virObjectRWLockRead(nets); virHashForEach(nets->objs, virNetworkObjListGetHelper, &data); - virObjectRWUnlock(nets); + virObjectRWUnlockRead(nets); return data.nnames; } @@ -1612,7 +1612,7 @@ virNetworkObjListPrune(virNetworkObjListPtr nets, virObjectRWLockWrite(nets); virHashRemoveSet(nets->objs, virNetworkObjListPruneHelper, &data); - virObjectRWUnlock(nets); + virObjectRWUnlockWrite(nets); } diff --git a/src/conf/virnodedeviceobj.c b/src/conf/virnodedeviceobj.c index 3a34a324ca..14abf77cbf 100644 --- a/src/conf/virnodedeviceobj.c +++ b/src/conf/virnodedeviceobj.c @@ -188,7 +188,7 @@ virNodeDeviceObjListSearch(virNodeDeviceObjListPtr devs, virObjectRWLockRead(devs); obj = virHashSearch(devs->objs, callback, data, NULL); virObjectRef(obj); - virObjectRWUnlock(devs); + virObjectRWUnlockRead(devs); if (obj) virObjectLock(obj); @@ -241,7 +241,7 @@ virNodeDeviceObjListFindByName(virNodeDeviceObjListPtr devs, virObjectRWLockRead(devs); obj = virNodeDeviceObjListFindByNameLocked(devs, name); - virObjectRWUnlock(devs); + virObjectRWUnlockRead(devs); if (obj) virObjectLock(obj); @@ -462,7 +462,7 @@ virNodeDeviceObjListAssignDef(virNodeDeviceObjListPtr devs, } cleanup: - virObjectRWUnlock(devs); + virObjectRWUnlockWrite(devs); return obj; } @@ -484,7 +484,7 @@ virNodeDeviceObjListRemove(virNodeDeviceObjListPtr devs, virHashRemoveEntry(devs->objs, def->name); virObjectUnlock(obj); virObjectUnref(obj); - virObjectRWUnlock(devs); + virObjectRWUnlockWrite(devs); } @@ -724,7 +724,7 @@ virNodeDeviceObjListNumOfDevices(virNodeDeviceObjListPtr devs, virObjectRWLockRead(devs); virHashForEach(devs->objs, virNodeDeviceObjListNumOfDevicesCallback, &data); - virObjectRWUnlock(devs); + virObjectRWUnlockRead(devs); return data.count; } @@ -784,7 +784,7 @@ virNodeDeviceObjListGetNames(virNodeDeviceObjListPtr devs, virObjectRWLockRead(devs); virHashForEach(devs->objs, virNodeDeviceObjListGetNamesCallback, &data); - virObjectRWUnlock(devs); + virObjectRWUnlockRead(devs); if (data.error) goto error; @@ -896,12 +896,12 @@ virNodeDeviceObjListExport(virConnectPtr conn, virObjectRWLockRead(devs); if (devices && VIR_ALLOC_N(data.devices, virHashSize(devs->objs) + 1) < 0) { - virObjectRWUnlock(devs); + virObjectRWUnlockRead(devs); return -1; } virHashForEach(devs->objs, virNodeDeviceObjListExportCallback, &data); - virObjectRWUnlock(devs); + virObjectRWUnlockRead(devs); if (data.error) goto cleanup; diff --git a/src/conf/virnwfilterbindingobjlist.c b/src/conf/virnwfilterbindingobjlist.c index 97e4d3c49b..64b75a0cc3 100644 --- a/src/conf/virnwfilterbindingobjlist.c +++ b/src/conf/virnwfilterbindingobjlist.c @@ -115,7 +115,7 @@ virNWFilterBindingObjListFindByPortDev(virNWFilterBindingObjListPtr bindings, virObjectRWLockRead(bindings); obj = virNWFilterBindingObjListFindByPortDevLocked(bindings, name); - virObjectRWUnlock(bindings); + virObjectRWUnlockRead(bindings); if (obj && virNWFilterBindingObjGetRemoving(obj)) { virObjectUnlock(obj); @@ -212,7 +212,7 @@ virNWFilterBindingObjListAdd(virNWFilterBindingObjListPtr bindings, virObjectRWLockWrite(bindings); ret = virNWFilterBindingObjListAddLocked(bindings, def); - virObjectRWUnlock(bindings); + virObjectRWUnlockWrite(bindings); return ret; } @@ -256,7 +256,7 @@ virNWFilterBindingObjListRemove(virNWFilterBindingObjListPtr bindings, virObjectLock(binding); virNWFilterBindingObjListRemoveLocked(bindings, binding); virObjectUnref(binding); - virObjectRWUnlock(bindings); + virObjectRWUnlockWrite(bindings); } @@ -331,7 +331,7 @@ virNWFilterBindingObjListLoadAllConfigs(virNWFilterBindingObjListPtr bindings, } VIR_DIR_CLOSE(dir); - virObjectRWUnlock(bindings); + virObjectRWUnlockWrite(bindings); return ret; } @@ -366,7 +366,7 @@ virNWFilterBindingObjListForEach(virNWFilterBindingObjListPtr bindings, }; virObjectRWLockRead(bindings); virHashForEach(bindings->objs, virNWFilterBindingObjListHelper, &data); - virObjectRWUnlock(bindings); + virObjectRWUnlockRead(bindings); return data.ret; } @@ -435,12 +435,12 @@ virNWFilterBindingObjListCollect(virNWFilterBindingObjListPtr domlist, virObjectRWLockRead(domlist); sa_assert(domlist->objs); if (VIR_ALLOC_N(data.bindings, virHashSize(domlist->objs)) < 0) { - virObjectRWUnlock(domlist); + virObjectRWUnlockRead(domlist); return -1; } virHashForEach(domlist->objs, virNWFilterBindingObjListCollectIterator, &data); - virObjectRWUnlock(domlist); + virObjectRWUnlockRead(domlist); virNWFilterBindingObjListFilter(&data.bindings, &data.nbindings, conn, filter); diff --git a/src/conf/virsecretobj.c b/src/conf/virsecretobj.c index 81c9f2c346..77cf03ad74 100644 --- a/src/conf/virsecretobj.c +++ b/src/conf/virsecretobj.c @@ -188,7 +188,7 @@ virSecretObjListFindByUUID(virSecretObjListPtr secrets, virObjectRWLockRead(secrets); obj = virSecretObjListFindByUUIDLocked(secrets, uuidstr); - virObjectRWUnlock(secrets); + virObjectRWUnlockRead(secrets); if (obj) virObjectLock(obj); return obj; @@ -267,7 +267,7 @@ virSecretObjListFindByUsage(virSecretObjListPtr secrets, virObjectRWLockRead(secrets); obj = virSecretObjListFindByUsageLocked(secrets, usageType, usageID); - virObjectRWUnlock(secrets); + virObjectRWUnlockRead(secrets); if (obj) virObjectLock(obj); return obj; @@ -303,7 +303,7 @@ virSecretObjListRemove(virSecretObjListPtr secrets, virHashRemoveEntry(secrets->objs, uuidstr); virObjectUnlock(obj); virObjectUnref(obj); - virObjectRWUnlock(secrets); + virObjectRWUnlockWrite(secrets); } @@ -397,7 +397,7 @@ virSecretObjListAdd(virSecretObjListPtr secrets, cleanup: virSecretObjEndAPI(&obj); - virObjectRWUnlock(secrets); + virObjectRWUnlockWrite(secrets); return ret; } @@ -490,7 +490,7 @@ virSecretObjListNumOfSecrets(virSecretObjListPtr secrets, virObjectRWLockRead(secrets); virHashForEach(secrets->objs, virSecretObjListNumOfSecretsCallback, &data); - virObjectRWUnlock(secrets); + virObjectRWUnlockRead(secrets); return data.count; } @@ -592,12 +592,12 @@ virSecretObjListExport(virConnectPtr conn, virObjectRWLockRead(secretobjs); if (secrets && VIR_ALLOC_N(data.secrets, virHashSize(secretobjs->objs) + 1) < 0) { - virObjectRWUnlock(secretobjs); + virObjectRWUnlockRead(secretobjs); return -1; } virHashForEach(secretobjs->objs, virSecretObjListExportCallback, &data); - virObjectRWUnlock(secretobjs); + virObjectRWUnlockRead(secretobjs); if (data.error) goto error; @@ -629,7 +629,7 @@ virSecretObjListGetUUIDs(virSecretObjListPtr secrets, virObjectRWLockRead(secrets); virHashForEach(secrets->objs, virSecretObjListGetUUIDsCallback, &data); - virObjectRWUnlock(secrets); + virObjectRWUnlockRead(secrets); if (data.error) goto error; diff --git a/src/conf/virstorageobj.c b/src/conf/virstorageobj.c index 5cbd30f93c..2f1e9e72aa 100644 --- a/src/conf/virstorageobj.c +++ b/src/conf/virstorageobj.c @@ -515,7 +515,7 @@ virStoragePoolObjListSearch(virStoragePoolObjListPtr pools, virObjectRWLockRead(pools); obj = virHashSearch(pools->objs, virStoragePoolObjListSearchCb, &data, NULL); - virObjectRWUnlock(pools); + virObjectRWUnlockRead(pools); return virObjectRef(obj); } @@ -535,7 +535,7 @@ virStoragePoolObjRemove(virStoragePoolObjListPtr pools, virHashRemoveEntry(pools->objs, uuidstr); virHashRemoveEntry(pools->objsName, obj->def->name); virObjectUnref(obj); - virObjectRWUnlock(pools); + virObjectRWUnlockWrite(pools); } @@ -568,7 +568,7 @@ virStoragePoolObjFindByUUID(virStoragePoolObjListPtr pools, virObjectRWLockRead(pools); obj = virStoragePoolObjFindByUUIDLocked(pools, uuid); - virObjectRWUnlock(pools); + virObjectRWUnlockRead(pools); if (obj) virObjectLock(obj); @@ -601,7 +601,7 @@ virStoragePoolObjFindByName(virStoragePoolObjListPtr pools, virObjectRWLockRead(pools); obj = virStoragePoolObjFindByNameLocked(pools, name); - virObjectRWUnlock(pools); + virObjectRWUnlockRead(pools); if (obj) virObjectLock(obj); @@ -668,13 +668,13 @@ virStoragePoolObjAddVol(virStoragePoolObjPtr obj, virObjectRef(volobj); volobj->voldef = voldef; - virObjectRWUnlock(volumes); + virObjectRWUnlockWrite(volumes); virStorageVolObjEndAPI(&volobj); return 0; error: virStorageVolObjEndAPI(&volobj); - virObjectRWUnlock(volumes); + virObjectRWUnlockWrite(volumes); return -1; } @@ -691,7 +691,7 @@ virStoragePoolObjRemoveVol(virStoragePoolObjPtr obj, if (!volobj) { VIR_INFO("Cannot find volume '%s' from storage pool '%s'", voldef->name, obj->def->name); - virObjectRWUnlock(volumes); + virObjectRWUnlockWrite(volumes); return; } VIR_INFO("Deleting volume '%s' from storage pool '%s'", @@ -704,7 +704,7 @@ virStoragePoolObjRemoveVol(virStoragePoolObjPtr obj, virHashRemoveEntry(volumes->objsPath, voldef->target.path); virStorageVolObjEndAPI(&volobj); - virObjectRWUnlock(volumes); + virObjectRWUnlockWrite(volumes); } @@ -715,7 +715,7 @@ virStoragePoolObjGetVolumesCount(virStoragePoolObjPtr obj) virObjectRWLockRead(obj->volumes); nbElems = virHashSize(obj->volumes->objsKey); - virObjectRWUnlock(obj->volumes); + virObjectRWUnlockRead(obj->volumes); return nbElems; } @@ -755,7 +755,7 @@ virStoragePoolObjForEachVolume(virStoragePoolObjPtr obj, virObjectRWLockRead(obj->volumes); virHashForEach(obj->volumes->objsKey, virStoragePoolObjForEachVolumeCb, &data); - virObjectRWUnlock(obj->volumes); + virObjectRWUnlockRead(obj->volumes); return 0; } @@ -797,7 +797,7 @@ virStoragePoolObjSearchVolume(virStoragePoolObjPtr obj, volobj = virHashSearch(obj->volumes->objsKey, virStoragePoolObjSearchVolumeCb, &data, NULL); - virObjectRWUnlock(obj->volumes); + virObjectRWUnlockRead(obj->volumes); if (volobj) return volobj->voldef; @@ -814,7 +814,7 @@ virStorageVolDefFindByKey(virStoragePoolObjPtr obj, virObjectRWLockRead(obj->volumes); volobj = virHashLookup(obj->volumes->objsKey, key); - virObjectRWUnlock(obj->volumes); + virObjectRWUnlockRead(obj->volumes); if (volobj) return volobj->voldef; @@ -830,7 +830,7 @@ virStorageVolDefFindByPath(virStoragePoolObjPtr obj, virObjectRWLockRead(obj->volumes); volobj = virHashLookup(obj->volumes->objsPath, path); - virObjectRWUnlock(obj->volumes); + virObjectRWUnlockRead(obj->volumes); if (volobj) return volobj->voldef; @@ -846,7 +846,7 @@ virStorageVolDefFindByName(virStoragePoolObjPtr obj, virObjectRWLockRead(obj->volumes); volobj = virHashLookup(obj->volumes->objsName, name); - virObjectRWUnlock(obj->volumes); + virObjectRWUnlockRead(obj->volumes); if (volobj) return volobj->voldef; @@ -895,7 +895,7 @@ virStoragePoolObjNumOfVolumes(virStoragePoolObjPtr obj, virObjectRWLockRead(volumes); virHashForEach(volumes->objsName, virStoragePoolObjNumOfVolumesCb, &data); - virObjectRWUnlock(volumes); + virObjectRWUnlockRead(volumes); return data.count; } @@ -956,7 +956,7 @@ virStoragePoolObjVolumeGetNames(virStoragePoolObjPtr obj, virObjectRWLockRead(volumes); virHashForEach(volumes->objsName, virStoragePoolObjVolumeGetNamesCb, &data); - virObjectRWUnlock(volumes); + virObjectRWUnlockRead(volumes); if (data.error) goto error; @@ -1032,17 +1032,17 @@ virStoragePoolObjVolumeListExport(virConnectPtr conn, if (!vols) { int ret = virHashSize(volumes->objsName); - virObjectRWUnlock(volumes); + virObjectRWUnlockRead(volumes); return ret; } if (VIR_ALLOC_N(data.vols, virHashSize(volumes->objsName) + 1) < 0) { - virObjectRWUnlock(volumes); + virObjectRWUnlockRead(volumes); return -1; } virHashForEach(volumes->objsName, virStoragePoolObjVolumeListExportCallback, &data); - virObjectRWUnlock(volumes); + virObjectRWUnlockRead(volumes); if (data.error) goto error; @@ -1584,7 +1584,7 @@ virStoragePoolObjListAdd(virStoragePoolObjListPtr pools, goto error; if (rc > 0) { virStoragePoolObjAssignDef(obj, def, flags); - virObjectRWUnlock(pools); + virObjectRWUnlockWrite(pools); return obj; } @@ -1602,12 +1602,12 @@ virStoragePoolObjListAdd(virStoragePoolObjListPtr pools, } virObjectRef(obj); obj->def = def; - virObjectRWUnlock(pools); + virObjectRWUnlockWrite(pools); return obj; error: virStoragePoolObjEndAPI(&obj); - virObjectRWUnlock(pools); + virObjectRWUnlockWrite(pools); return NULL; } @@ -1867,7 +1867,7 @@ virStoragePoolObjNumOfStoragePools(virStoragePoolObjListPtr pools, virObjectRWLockRead(pools); virHashForEach(pools->objs, virStoragePoolObjNumOfStoragePoolsCb, &data); - virObjectRWUnlock(pools); + virObjectRWUnlockRead(pools); return data.count; } @@ -1931,7 +1931,7 @@ virStoragePoolObjGetNames(virStoragePoolObjListPtr pools, virObjectRWLockRead(pools); virHashForEach(pools->objs, virStoragePoolObjGetNamesCb, &data); - virObjectRWUnlock(pools); + virObjectRWUnlockRead(pools); if (data.error) goto error; @@ -2081,7 +2081,7 @@ virStoragePoolObjListExport(virConnectPtr conn, goto error; virHashForEach(poolobjs->objs, virStoragePoolObjListExportCallback, &data); - virObjectRWUnlock(poolobjs); + virObjectRWUnlockRead(poolobjs); if (data.error) goto error; diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index c95c1a078c..c2abbf65c2 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -2745,7 +2745,8 @@ virObjectRef; virObjectRWLockableNew; virObjectRWLockRead; virObjectRWLockWrite; -virObjectRWUnlock; +virObjectRWUnlockRead; +virObjectRWUnlockWrite; virObjectUnlock; virObjectUnref; diff --git a/src/util/virobject.c b/src/util/virobject.c index c71781550f..aee1bb4af6 100644 --- a/src/util/virobject.c +++ b/src/util/virobject.c @@ -294,12 +294,7 @@ virObjectRWLockableNew(virClassPtr klass) if (!(obj = virObjectNew(klass))) return NULL; - if (virRWLockInit(&obj->lock) < 0) { - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", - _("Unable to initialize RW lock")); - virObjectUnref(obj); - return NULL; - } + g_rw_lock_init(&obj->lock); return obj; } @@ -319,7 +314,7 @@ virObjectRWLockableDispose(void *anyobj) { virObjectRWLockablePtr obj = anyobj; - virRWLockDestroy(&obj->lock); + g_rw_lock_clear(&obj->lock); } @@ -458,7 +453,7 @@ virObjectRWLockRead(void *anyobj) if (!obj) return; - virRWLockRead(&obj->lock); + g_rw_lock_reader_lock(&obj->lock); } @@ -487,7 +482,7 @@ virObjectRWLockWrite(void *anyobj) if (!obj) return; - virRWLockWrite(&obj->lock); + g_rw_lock_writer_lock(&obj->lock); } @@ -518,16 +513,26 @@ virObjectUnlock(void *anyobj) * virObjectRWLockRead or virObjectRWLockWrite. */ void -virObjectRWUnlock(void *anyobj) +virObjectRWUnlockWrite(void *anyobj) { virObjectRWLockablePtr obj = virObjectGetRWLockableObj(anyobj); if (!obj) return; - virRWLockUnlock(&obj->lock); + g_rw_lock_writer_unlock(&obj->lock); } +void +virObjectRWUnlockRead(void *anyobj) +{ + virObjectRWLockablePtr obj = virObjectGetRWLockableObj(anyobj); + + if (!obj) + return; + + g_rw_lock_reader_unlock(&obj->lock); +} /** * virObjectIsClass: diff --git a/src/util/virobject.h b/src/util/virobject.h index 62a8a3d132..fcfe77578b 100644 --- a/src/util/virobject.h +++ b/src/util/virobject.h @@ -63,7 +63,7 @@ struct _virObjectLockable { struct _virObjectRWLockable { virObject parent; - virRWLock lock; + GRWLock lock; }; virClassPtr virClassForObject(void); @@ -150,7 +150,11 @@ virObjectUnlock(void *lockableobj) ATTRIBUTE_NONNULL(1); void -virObjectRWUnlock(void *lockableobj) +virObjectRWUnlockRead(void *lockableobj) + ATTRIBUTE_NONNULL(1); + +void +virObjectRWUnlockWrite(void *lockableobj) ATTRIBUTE_NONNULL(1); void -- 2.25.2

All instances where virRWLock where used were replaced by GRWLock. Signed-off-by: Rafael Fonseca <r4f4rfs@gmail.com> --- src/libvirt_private.syms | 5 ----- src/util/virthread.c | 33 --------------------------------- src/util/virthread.h | 16 ---------------- 3 files changed, 54 deletions(-) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index c2abbf65c2..4dfafdfd3e 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -3271,11 +3271,6 @@ virMutexInitRecursive; virMutexLock; virMutexUnlock; virOnce; -virRWLockDestroy; -virRWLockInit; -virRWLockRead; -virRWLockUnlock; -virRWLockWrite; virThreadCancel; virThreadCreateFull; virThreadID; diff --git a/src/util/virthread.c b/src/util/virthread.c index 64013b575c..1ec5096ced 100644 --- a/src/util/virthread.c +++ b/src/util/virthread.c @@ -89,39 +89,6 @@ void virMutexUnlock(virMutexPtr m) } -int virRWLockInit(virRWLockPtr m) -{ - int ret; - ret = pthread_rwlock_init(&m->lock, NULL); - if (ret != 0) { - errno = ret; - return -1; - } - return 0; -} - -void virRWLockDestroy(virRWLockPtr m) -{ - pthread_rwlock_destroy(&m->lock); -} - - -void virRWLockRead(virRWLockPtr m) -{ - pthread_rwlock_rdlock(&m->lock); -} - -void virRWLockWrite(virRWLockPtr m) -{ - pthread_rwlock_wrlock(&m->lock); -} - - -void virRWLockUnlock(virRWLockPtr m) -{ - pthread_rwlock_unlock(&m->lock); -} - int virCondInit(virCondPtr c) { int ret; diff --git a/src/util/virthread.h b/src/util/virthread.h index c227951ddd..a57c99bdf0 100644 --- a/src/util/virthread.h +++ b/src/util/virthread.h @@ -33,14 +33,6 @@ struct virMutex { pthread_mutex_t lock; }; -typedef struct virRWLock virRWLock; -typedef virRWLock *virRWLockPtr; - -struct virRWLock { - pthread_rwlock_t lock; -}; - - typedef struct virCond virCond; typedef virCond *virCondPtr; @@ -133,14 +125,6 @@ void virMutexLock(virMutexPtr m); void virMutexUnlock(virMutexPtr m); -int virRWLockInit(virRWLockPtr m) G_GNUC_WARN_UNUSED_RESULT; -void virRWLockDestroy(virRWLockPtr m); - -void virRWLockRead(virRWLockPtr m); -void virRWLockWrite(virRWLockPtr m); -void virRWLockUnlock(virRWLockPtr m); - - int virCondInit(virCondPtr c) G_GNUC_WARN_UNUSED_RESULT; int virCondDestroy(virCondPtr c); -- 2.25.2

Because virCond usage is connected to virMutex, this patch also converts virCond to GCond. An important note to make here: GMutex doesn't like when you try to clear a lock that's still locked. So I had to hunt the few cases where that happened and add an explicit virObjectUnlock call before unreffing the object. Signed-off-by: Rafael Fonseca <r4f4rfs@gmail.com> --- src/conf/domain_conf.c | 27 ++------- src/conf/domain_conf.h | 5 +- src/libxl/libxl_domain.c | 18 ++---- src/libxl/libxl_domain.h | 2 +- src/lxc/lxc_domain.c | 18 ++---- src/lxc/lxc_domain.h | 2 +- src/node_device/node_device_udev.c | 22 +++---- src/qemu/qemu_agent.c | 50 +++++++--------- src/qemu/qemu_domain.c | 95 ++++++++++++++---------------- src/qemu/qemu_domain.h | 4 +- src/qemu/qemu_hotplug.c | 28 +++------ src/qemu/qemu_hotplug.h | 2 +- src/qemu/qemu_monitor.c | 27 ++++----- src/rpc/virnetclient.c | 25 +++----- src/util/virfdstream.c | 26 +++----- src/util/virobject.c | 13 ++-- src/util/virobject.h | 2 +- src/vz/vz_utils.c | 41 ++++--------- src/vz/vz_utils.h | 6 +- tests/qemuhotplugmock.c | 6 +- tests/qemuhotplugtest.c | 2 + tests/qemumonitortestutils.c | 5 ++ tests/qemusecuritytest.c | 10 +++- tests/qemuxml2argvtest.c | 1 + tools/virsh-console.c | 23 ++------ 25 files changed, 172 insertions(+), 288 deletions(-) diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index 639d471424..d4629a936a 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -3535,7 +3535,7 @@ static void virDomainObjDispose(void *obj) virDomainObjPtr dom = obj; VIR_DEBUG("obj=%p", dom); - virCondDestroy(&dom->cond); + g_cond_clear(&dom->cond); virDomainDefFree(dom->def); virDomainDefFree(dom->newDef); @@ -3557,11 +3557,7 @@ virDomainObjNew(virDomainXMLOptionPtr xmlopt) if (!(domain = virObjectLockableNew(virDomainObjClass))) return NULL; - if (virCondInit(&domain->cond) < 0) { - virReportSystemError(errno, "%s", - _("failed to initialize domain condition")); - goto error; - } + g_cond_init(&domain->cond); if (xmlopt->privateData.alloc) { domain->privateData = (xmlopt->privateData.alloc)(xmlopt->config.priv); @@ -3670,18 +3666,14 @@ virDomainObjEndAPI(virDomainObjPtr *vm) void virDomainObjBroadcast(virDomainObjPtr vm) { - virCondBroadcast(&vm->cond); + g_cond_broadcast(&vm->cond); } int virDomainObjWait(virDomainObjPtr vm) { - if (virCondWait(&vm->cond, &vm->parent.lock) < 0) { - virReportSystemError(errno, "%s", - _("failed to wait for domain condition")); - return -1; - } + g_cond_wait(&vm->cond, &vm->parent.lock); if (!virDomainObjIsActive(vm)) { virReportError(VIR_ERR_OPERATION_FAILED, "%s", @@ -3697,22 +3689,15 @@ virDomainObjWait(virDomainObjPtr vm) * Waits for domain condition to be triggered for a specific period of time. * * Returns: - * -1 in case of error * 0 on success * 1 on timeout */ int virDomainObjWaitUntil(virDomainObjPtr vm, - unsigned long long whenms) + gint64 whenms) { - if (virCondWaitUntil(&vm->cond, &vm->parent.lock, whenms) < 0) { - if (errno != ETIMEDOUT) { - virReportSystemError(errno, "%s", - _("failed to wait for domain condition")); - return -1; - } + if (!g_cond_wait_until(&vm->cond, &vm->parent.lock, whenms)) return 1; - } return 0; } diff --git a/src/conf/domain_conf.h b/src/conf/domain_conf.h index 9310fab169..647baf40b2 100644 --- a/src/conf/domain_conf.h +++ b/src/conf/domain_conf.h @@ -2630,7 +2630,7 @@ struct _virDomainStateReason { struct _virDomainObj { virObjectLockable parent; - virCond cond; + GCond cond; pid_t pid; virDomainStateReason state; @@ -2892,8 +2892,7 @@ bool virDomainObjTaint(virDomainObjPtr obj, void virDomainObjBroadcast(virDomainObjPtr vm); int virDomainObjWait(virDomainObjPtr vm); -int virDomainObjWaitUntil(virDomainObjPtr vm, - unsigned long long whenms); +int virDomainObjWaitUntil(virDomainObjPtr vm, gint64 whenms); void virDomainPanicDefFree(virDomainPanicDefPtr panic); void virDomainResourceDefFree(virDomainResourceDefPtr resource); diff --git a/src/libxl/libxl_domain.c b/src/libxl/libxl_domain.c index cc53a765e1..671e49670b 100644 --- a/src/libxl/libxl_domain.c +++ b/src/libxl/libxl_domain.c @@ -69,8 +69,7 @@ libxlDomainObjInitJob(libxlDomainObjPrivatePtr priv) { memset(&priv->job, 0, sizeof(priv->job)); - if (virCondInit(&priv->job.cond) < 0) - return -1; + g_cond_init(&priv->job.cond); if (VIR_ALLOC(priv->job.current) < 0) return -1; @@ -90,12 +89,12 @@ libxlDomainObjResetJob(libxlDomainObjPrivatePtr priv) static void libxlDomainObjFreeJob(libxlDomainObjPrivatePtr priv) { - ignore_value(virCondDestroy(&priv->job.cond)); + g_cond_clear(&priv->job.cond); VIR_FREE(priv->job.current); } /* Give up waiting for mutex after 30 seconds */ -#define LIBXL_JOB_WAIT_TIME (1000ull * 30) +#define LIBXL_JOB_WAIT_TIME (30 * G_TIME_SPAN_SECOND) /* * obj must be locked before calling, libxlDriverPrivatePtr must NOT be locked @@ -112,17 +111,12 @@ libxlDomainObjBeginJob(libxlDriverPrivatePtr driver G_GNUC_UNUSED, enum libxlDomainJob job) { libxlDomainObjPrivatePtr priv = obj->privateData; - unsigned long long now; - unsigned long long then; - - if (virTimeMillisNow(&now) < 0) - return -1; - then = now + LIBXL_JOB_WAIT_TIME; + gint64 then = g_get_monotonic_time() + LIBXL_JOB_WAIT_TIME; while (priv->job.active) { VIR_DEBUG("Wait normal job condition for starting job: %s", libxlDomainJobTypeToString(job)); - if (virCondWaitUntil(&priv->job.cond, &obj->parent.lock, then) < 0) + if (!g_cond_wait_until(&priv->job.cond, &obj->parent.lock, then)) goto error; } @@ -175,7 +169,7 @@ libxlDomainObjEndJob(libxlDriverPrivatePtr driver G_GNUC_UNUSED, libxlDomainJobTypeToString(job)); libxlDomainObjResetJob(priv); - virCondSignal(&priv->job.cond); + g_cond_signal(&priv->job.cond); } int diff --git a/src/libxl/libxl_domain.h b/src/libxl/libxl_domain.h index 7e28093722..5f04f3aacb 100644 --- a/src/libxl/libxl_domain.h +++ b/src/libxl/libxl_domain.h @@ -47,7 +47,7 @@ VIR_ENUM_DECL(libxlDomainJob); struct libxlDomainJobObj { - virCond cond; /* Use to coordinate jobs */ + GCond cond; /* Use to coordinate jobs */ enum libxlDomainJob active; /* Currently running job */ int owner; /* Thread which set current job */ unsigned long long started; /* When the job started */ diff --git a/src/lxc/lxc_domain.c b/src/lxc/lxc_domain.c index 59f803837a..a09788dd12 100644 --- a/src/lxc/lxc_domain.c +++ b/src/lxc/lxc_domain.c @@ -50,8 +50,7 @@ virLXCDomainObjInitJob(virLXCDomainObjPrivatePtr priv) { memset(&priv->job, 0, sizeof(priv->job)); - if (virCondInit(&priv->job.cond) < 0) - return -1; + g_cond_init(&priv->job.cond); return 0; } @@ -68,11 +67,11 @@ virLXCDomainObjResetJob(virLXCDomainObjPrivatePtr priv) static void virLXCDomainObjFreeJob(virLXCDomainObjPrivatePtr priv) { - ignore_value(virCondDestroy(&priv->job.cond)); + g_cond_clear(&priv->job.cond); } /* Give up waiting for mutex after 30 seconds */ -#define LXC_JOB_WAIT_TIME (1000ull * 30) +#define LXC_JOB_WAIT_TIME (30 * G_TIME_SPAN_SECOND) /* * obj must be locked before calling, virLXCDriverPtr must NOT be locked @@ -89,17 +88,12 @@ virLXCDomainObjBeginJob(virLXCDriverPtr driver G_GNUC_UNUSED, enum virLXCDomainJob job) { virLXCDomainObjPrivatePtr priv = obj->privateData; - unsigned long long now; - unsigned long long then; - - if (virTimeMillisNow(&now) < 0) - return -1; - then = now + LXC_JOB_WAIT_TIME; + gint64 then = g_get_monotonic_time() + LXC_JOB_WAIT_TIME; while (priv->job.active) { VIR_DEBUG("Wait normal job condition for starting job: %s", virLXCDomainJobTypeToString(job)); - if (virCondWaitUntil(&priv->job.cond, &obj->parent.lock, then) < 0) + if (!g_cond_wait_until(&priv->job.cond, &obj->parent.lock, then)) goto error; } @@ -146,7 +140,7 @@ virLXCDomainObjEndJob(virLXCDriverPtr driver G_GNUC_UNUSED, virLXCDomainJobTypeToString(job)); virLXCDomainObjResetJob(priv); - virCondSignal(&priv->job.cond); + g_cond_signal(&priv->job.cond); } diff --git a/src/lxc/lxc_domain.h b/src/lxc/lxc_domain.h index 319f83338f..dfc16fae2b 100644 --- a/src/lxc/lxc_domain.h +++ b/src/lxc/lxc_domain.h @@ -69,7 +69,7 @@ VIR_ENUM_DECL(virLXCDomainJob); struct virLXCDomainJobObj { - virCond cond; /* Use to coordinate jobs */ + GCond cond; /* Use to coordinate jobs */ enum virLXCDomainJob active; /* Currently running job */ int owner; /* Thread which set current job */ }; diff --git a/src/node_device/node_device_udev.c b/src/node_device/node_device_udev.c index 433de0aae0..22106c3f30 100644 --- a/src/node_device/node_device_udev.c +++ b/src/node_device/node_device_udev.c @@ -63,7 +63,7 @@ struct _udevEventData { /* Thread data */ virThread th; - virCond threadCond; + GCond threadCond; bool threadQuit; bool dataReady; }; @@ -86,7 +86,7 @@ udevEventDataDispose(void *obj) udev_monitor_unref(priv->udev_monitor); udev_unref(udev); - virCondDestroy(&priv->threadCond); + g_cond_clear(&priv->threadCond); } @@ -112,10 +112,7 @@ udevEventDataNew(void) if (!(ret = virObjectLockableNew(udevEventDataClass))) return NULL; - if (virCondInit(&ret->threadCond) < 0) { - virObjectUnref(ret); - return NULL; - } + g_cond_init(&ret->threadCond); ret->watch = -1; return ret; @@ -1463,7 +1460,7 @@ nodeStateCleanup(void) if (priv) { virObjectLock(priv); priv->threadQuit = true; - virCondSignal(&priv->threadCond); + g_cond_signal(&priv->threadCond); virObjectUnlock(priv); virThreadJoin(&priv->th); } @@ -1563,12 +1560,7 @@ udevEventHandleThread(void *opaque G_GNUC_UNUSED) while (1) { virObjectLock(priv); while (!priv->dataReady && !priv->threadQuit) { - if (virCondWait(&priv->threadCond, &priv->parent.lock)) { - virReportSystemError(errno, "%s", - _("handler failed to wait on condition")); - virObjectUnlock(priv); - return; - } + g_cond_wait(&priv->threadCond, &priv->parent.lock); } if (priv->threadQuit) { @@ -1635,7 +1627,7 @@ udevEventHandleCallback(int watch G_GNUC_UNUSED, else priv->dataReady = true; - virCondSignal(&priv->threadCond); + g_cond_signal(&priv->threadCond); virObjectUnlock(priv); } @@ -1757,7 +1749,7 @@ nodeStateInitializeEnumerate(void *opaque) ignore_value(virEventRemoveHandle(priv->watch)); priv->watch = -1; priv->threadQuit = true; - virCondSignal(&priv->threadCond); + g_cond_signal(&priv->threadCond); virObjectUnlock(priv); } diff --git a/src/qemu/qemu_agent.c b/src/qemu/qemu_agent.c index d7fcc869c6..597df38752 100644 --- a/src/qemu/qemu_agent.c +++ b/src/qemu/qemu_agent.c @@ -99,7 +99,7 @@ struct _qemuAgentMessage { struct _qemuAgent { virObjectLockable parent; - virCond notify; + GCond notify; int fd; @@ -176,7 +176,7 @@ static void qemuAgentDispose(void *obj) VIR_DEBUG("agent=%p", agent); if (agent->cb && agent->cb->destroy) (agent->cb->destroy)(agent, agent->vm); - virCondDestroy(&agent->notify); + g_cond_clear(&agent->notify); VIR_FREE(agent->buffer); g_main_context_unref(agent->context); virResetError(&agent->lastError); @@ -403,7 +403,7 @@ qemuAgentIOProcess(qemuAgentPtr agent) VIR_DEBUG("Process done %zu used %d", agent->bufferOffset, len); #endif if (msg && msg->finished) - virCondBroadcast(&agent->notify); + g_cond_broadcast(&agent->notify); return len; } @@ -628,7 +628,7 @@ qemuAgentIO(GSocket *socket G_GNUC_UNUSED, * then wakeup that waiter */ if (agent->msg && !agent->msg->finished) { agent->msg->finished = 1; - virCondSignal(&agent->notify); + g_cond_signal(&agent->notify); } } @@ -643,7 +643,7 @@ qemuAgentIO(GSocket *socket G_GNUC_UNUSED, virDomainObjPtr vm = agent->vm; /* Make sure anyone waiting wakes up now */ - virCondSignal(&agent->notify); + g_cond_signal(&agent->notify); virObjectUnlock(agent); virObjectUnref(agent); VIR_DEBUG("Triggering EOF callback"); @@ -654,7 +654,7 @@ qemuAgentIO(GSocket *socket G_GNUC_UNUSED, virDomainObjPtr vm = agent->vm; /* Make sure anyone waiting wakes up now */ - virCondSignal(&agent->notify); + g_cond_signal(&agent->notify); virObjectUnlock(agent); virObjectUnref(agent); VIR_DEBUG("Triggering error callback"); @@ -692,12 +692,8 @@ qemuAgentOpen(virDomainObjPtr vm, agent->timeout = QEMU_DOMAIN_PRIVATE(vm)->agentTimeout; agent->fd = -1; - if (virCondInit(&agent->notify) < 0) { - virReportSystemError(errno, "%s", - _("cannot initialize agent condition")); - virObjectUnref(agent); - return NULL; - } + g_cond_init(&agent->notify); + agent->vm = vm; agent->cb = cb; agent->singleSync = singleSync; @@ -752,7 +748,7 @@ qemuAgentNotifyCloseLocked(qemuAgentPtr agent) * wake him up. No message will arrive anyway. */ if (agent->msg && !agent->msg->finished) { agent->msg->finished = 1; - virCondSignal(&agent->notify); + g_cond_signal(&agent->notify); } } } @@ -820,7 +816,7 @@ static int qemuAgentSend(qemuAgentPtr agent, int seconds) { int ret = -1; - unsigned long long then = 0; + gint64 then = 0; /* Check whether qemu quit unexpectedly */ if (agent->lastError.code != VIR_ERR_OK) { @@ -831,29 +827,23 @@ static int qemuAgentSend(qemuAgentPtr agent, } if (seconds > VIR_DOMAIN_QEMU_AGENT_COMMAND_BLOCK) { - unsigned long long now; - if (virTimeMillisNow(&now) < 0) - return -1; if (seconds == VIR_DOMAIN_QEMU_AGENT_COMMAND_DEFAULT) seconds = QEMU_AGENT_WAIT_TIME; - then = now + seconds * 1000ull; + then = g_get_monotonic_time() + seconds * G_TIME_SPAN_SECOND; } agent->msg = msg; qemuAgentUpdateWatch(agent); while (!agent->msg->finished) { - if ((then && virCondWaitUntil(&agent->notify, &agent->parent.lock, then) < 0) || - (!then && virCondWait(&agent->notify, &agent->parent.lock) < 0)) { - if (errno == ETIMEDOUT) { - virReportError(VIR_ERR_AGENT_UNRESPONSIVE, "%s", - _("Guest agent not available for now")); - ret = -2; - } else { - virReportSystemError(errno, "%s", - _("Unable to wait on agent socket " - "condition")); - } + if (!then) { + g_cond_wait(&agent->notify, &agent->parent.lock); + } else if (!g_cond_wait_until(&agent->notify, + &agent->parent.lock, + then)) { + virReportError(VIR_ERR_AGENT_UNRESPONSIVE, "%s", + _("Guest agent not available for now")); + ret = -2; agent->inSync = false; goto cleanup; } @@ -1210,7 +1200,7 @@ void qemuAgentNotifyEvent(qemuAgentPtr agent, /* somebody waiting for this event, wake him up. */ if (agent->msg && !agent->msg->finished) { agent->msg->finished = 1; - virCondSignal(&agent->notify); + g_cond_signal(&agent->notify); } } diff --git a/src/qemu/qemu_domain.c b/src/qemu/qemu_domain.c index 91e234d644..ac02f54cb8 100644 --- a/src/qemu/qemu_domain.c +++ b/src/qemu/qemu_domain.c @@ -334,13 +334,8 @@ qemuDomainObjInitJob(qemuDomainObjPrivatePtr priv) { memset(&priv->job, 0, sizeof(priv->job)); - if (virCondInit(&priv->job.cond) < 0) - return -1; - - if (virCondInit(&priv->job.asyncCond) < 0) { - virCondDestroy(&priv->job.cond); - return -1; - } + g_cond_init(&priv->job.cond); + g_cond_init(&priv->job.asyncCond); return 0; } @@ -417,8 +412,8 @@ qemuDomainObjFreeJob(qemuDomainObjPrivatePtr priv) qemuDomainObjResetAsyncJob(priv); VIR_FREE(priv->job.current); VIR_FREE(priv->job.completed); - virCondDestroy(&priv->job.cond); - virCondDestroy(&priv->job.asyncCond); + g_cond_clear(&priv->job.cond); + g_cond_clear(&priv->job.asyncCond); } static bool @@ -6181,7 +6176,7 @@ qemuDomainObjCanSetJob(qemuDomainObjPrivatePtr priv, } /* Give up waiting for mutex after 30 seconds */ -#define QEMU_JOB_WAIT_TIME (1000ull * 30) +#define QEMU_JOB_WAIT_TIME (30 * G_TIME_SPAN_SECOND) /** * qemuDomainObjBeginJobInternal: @@ -6214,17 +6209,16 @@ qemuDomainObjBeginJobInternal(virQEMUDriverPtr driver, bool nowait) { qemuDomainObjPrivatePtr priv = obj->privateData; - unsigned long long now; - unsigned long long then; + gint64 now, then = 0; bool nested = job == QEMU_JOB_ASYNC_NESTED; bool async = job == QEMU_JOB_ASYNC; g_autoptr(virQEMUDriverConfig) cfg = virQEMUDriverGetConfig(driver); const char *blocker = NULL; const char *agentBlocker = NULL; int ret = -1; - unsigned long long duration = 0; - unsigned long long agentDuration = 0; - unsigned long long asyncDuration = 0; + gint64 duration = 0; + gint64 agentDuration = 0; + gint64 asyncDuration = 0; VIR_DEBUG("Starting job: job=%s agentJob=%s asyncJob=%s " "(vm=%p name=%s, current job=%s agentJob=%s async=%s)", @@ -6236,11 +6230,8 @@ qemuDomainObjBeginJobInternal(virQEMUDriverPtr driver, qemuDomainAgentJobTypeToString(priv->job.agentActive), qemuDomainAsyncJobTypeToString(priv->job.asyncJob)); - if (virTimeMillisNow(&now) < 0) - return -1; - priv->jobs_queued++; - then = now + QEMU_JOB_WAIT_TIME; + then = g_get_monotonic_time() + QEMU_JOB_WAIT_TIME; retry: if ((!async && job != QEMU_JOB_DESTROY) && @@ -6254,7 +6245,7 @@ qemuDomainObjBeginJobInternal(virQEMUDriverPtr driver, goto cleanup; VIR_DEBUG("Waiting for async job (vm=%p name=%s)", obj, obj->def->name); - if (virCondWaitUntil(&priv->job.asyncCond, &obj->parent.lock, then) < 0) + if (!g_cond_wait_until(&priv->job.asyncCond, &obj->parent.lock, then)) goto error; } @@ -6263,7 +6254,7 @@ qemuDomainObjBeginJobInternal(virQEMUDriverPtr driver, goto cleanup; VIR_DEBUG("Waiting for job (vm=%p name=%s)", obj, obj->def->name); - if (virCondWaitUntil(&priv->job.cond, &obj->parent.lock, then) < 0) + if (!g_cond_wait_until(&priv->job.cond, &obj->parent.lock, then)) goto error; } @@ -6272,7 +6263,7 @@ qemuDomainObjBeginJobInternal(virQEMUDriverPtr driver, if (!nested && !qemuDomainNestedJobAllowed(priv, job)) goto retry; - ignore_value(virTimeMillisNow(&now)); + now = g_get_monotonic_time(); if (job) { qemuDomainObjResetJob(priv); @@ -6322,7 +6313,7 @@ qemuDomainObjBeginJobInternal(virQEMUDriverPtr driver, return 0; error: - ignore_value(virTimeMillisNow(&now)); + now = g_get_monotonic_time(); if (priv->job.active && priv->job.started) duration = now - priv->job.started; if (priv->job.agentActive && priv->job.agentStarted) @@ -6333,7 +6324,7 @@ qemuDomainObjBeginJobInternal(virQEMUDriverPtr driver, VIR_WARN("Cannot start job (%s, %s, %s) for domain %s; " "current job is (%s, %s, %s) " "owned by (%llu %s, %llu %s, %llu %s (flags=0x%lx)) " - "for (%llus, %llus, %llus)", + "for (%"G_GINT64_FORMAT"s, %"G_GINT64_FORMAT"s, %"G_GINT64_FORMAT"s)", qemuDomainJobTypeToString(job), qemuDomainAgentJobTypeToString(agentJob), qemuDomainAsyncJobTypeToString(asyncJob), @@ -6345,7 +6336,9 @@ qemuDomainObjBeginJobInternal(virQEMUDriverPtr driver, priv->job.agentOwner, NULLSTR(priv->job.agentOwnerAPI), priv->job.asyncOwner, NULLSTR(priv->job.asyncOwnerAPI), priv->job.apiFlags, - duration / 1000, agentDuration / 1000, asyncDuration / 1000); + duration / G_TIME_SPAN_MILLISECOND, + agentDuration / G_TIME_SPAN_MILLISECOND, + asyncDuration / G_TIME_SPAN_MILLISECOND); if (job) { if (nested || qemuDomainNestedJobAllowed(priv, job)) @@ -6357,29 +6350,8 @@ qemuDomainObjBeginJobInternal(virQEMUDriverPtr driver, if (agentJob) agentBlocker = priv->job.agentOwnerAPI; - if (errno == ETIMEDOUT) { - if (blocker && agentBlocker) { - virReportError(VIR_ERR_OPERATION_TIMEOUT, - _("cannot acquire state change " - "lock (held by monitor=%s agent=%s)"), - blocker, agentBlocker); - } else if (blocker) { - virReportError(VIR_ERR_OPERATION_TIMEOUT, - _("cannot acquire state change " - "lock (held by monitor=%s)"), - blocker); - } else if (agentBlocker) { - virReportError(VIR_ERR_OPERATION_TIMEOUT, - _("cannot acquire state change " - "lock (held by agent=%s)"), - agentBlocker); - } else { - virReportError(VIR_ERR_OPERATION_TIMEOUT, "%s", - _("cannot acquire state change lock")); - } - ret = -2; - } else if (cfg->maxQueuedJobs && - priv->jobs_queued > cfg->maxQueuedJobs) { + if (cfg->maxQueuedJobs && + priv->jobs_queued > cfg->maxQueuedJobs) { if (blocker && agentBlocker) { virReportError(VIR_ERR_OPERATION_FAILED, _("cannot acquire state change " @@ -6405,7 +6377,26 @@ qemuDomainObjBeginJobInternal(virQEMUDriverPtr driver, } ret = -2; } else { - virReportSystemError(errno, "%s", _("cannot acquire job mutex")); + if (blocker && agentBlocker) { + virReportError(VIR_ERR_OPERATION_TIMEOUT, + _("cannot acquire state change " + "lock (held by monitor=%s agent=%s)"), + blocker, agentBlocker); + } else if (blocker) { + virReportError(VIR_ERR_OPERATION_TIMEOUT, + _("cannot acquire state change " + "lock (held by monitor=%s)"), + blocker); + } else if (agentBlocker) { + virReportError(VIR_ERR_OPERATION_TIMEOUT, + _("cannot acquire state change " + "lock (held by agent=%s)"), + agentBlocker); + } else { + virReportError(VIR_ERR_OPERATION_TIMEOUT, "%s", + _("cannot acquire state change lock")); + } + ret = -2; } cleanup: @@ -6542,7 +6533,7 @@ qemuDomainObjEndJob(virQEMUDriverPtr driver, virDomainObjPtr obj) qemuDomainObjSaveStatus(driver, obj); /* We indeed need to wake up ALL threads waiting because * grabbing a job requires checking more variables. */ - virCondBroadcast(&priv->job.cond); + g_cond_broadcast(&priv->job.cond); } void @@ -6561,7 +6552,7 @@ qemuDomainObjEndAgentJob(virDomainObjPtr obj) qemuDomainObjResetAgentJob(priv); /* We indeed need to wake up ALL threads waiting because * grabbing a job requires checking more variables. */ - virCondBroadcast(&priv->job.cond); + g_cond_broadcast(&priv->job.cond); } void @@ -6577,7 +6568,7 @@ qemuDomainObjEndAsyncJob(virQEMUDriverPtr driver, virDomainObjPtr obj) qemuDomainObjResetAsyncJob(priv); qemuDomainObjSaveStatus(driver, obj); - virCondBroadcast(&priv->job.asyncCond); + g_cond_broadcast(&priv->job.asyncCond); } void diff --git a/src/qemu/qemu_domain.h b/src/qemu/qemu_domain.h index cf19f4d101..0d1bafac94 100644 --- a/src/qemu/qemu_domain.h +++ b/src/qemu/qemu_domain.h @@ -180,7 +180,7 @@ struct _qemuDomainJobInfo { typedef struct _qemuDomainJobObj qemuDomainJobObj; typedef qemuDomainJobObj *qemuDomainJobObjPtr; struct _qemuDomainJobObj { - virCond cond; /* Use to coordinate jobs */ + GCond cond; /* Use to coordinate jobs */ /* The following members are for QEMU_JOB_* */ qemuDomainJob active; /* Currently running job */ @@ -195,7 +195,7 @@ struct _qemuDomainJobObj { unsigned long long agentStarted; /* When the current agent job started */ /* The following members are for QEMU_ASYNC_JOB_* */ - virCond asyncCond; /* Use to coordinate with async jobs */ + GCond asyncCond; /* Use to coordinate with async jobs */ qemuDomainAsyncJob asyncJob; /* Currently active async job */ unsigned long long asyncOwner; /* Thread which set current async job */ const char *asyncOwnerAPI; /* The API which owns the async job */ diff --git a/src/qemu/qemu_hotplug.c b/src/qemu/qemu_hotplug.c index 14654a17d7..0e677bdce9 100644 --- a/src/qemu/qemu_hotplug.c +++ b/src/qemu/qemu_hotplug.c @@ -59,14 +59,14 @@ VIR_LOG_INIT("qemu.qemu_hotplug"); -#define CHANGE_MEDIA_TIMEOUT 5000 +#define CHANGE_MEDIA_TIMEOUT (5 * G_TIME_SPAN_MILLISECOND) /* Timeout in miliseconds for device removal. PPC64 domains * can experience a bigger delay in unplug operations during * heavy guest activity (vcpu being the most notable case), thus * the timeout for PPC64 is also bigger. */ -#define QEMU_UNPLUG_TIMEOUT 1000ull * 5 -#define QEMU_UNPLUG_TIMEOUT_PPC64 1000ull * 10 +#define QEMU_UNPLUG_TIMEOUT (5 * G_TIME_SPAN_MILLISECOND) +#define QEMU_UNPLUG_TIMEOUT_PPC64 (10 * G_TIME_SPAN_MILLISECOND) static void @@ -201,15 +201,11 @@ static int qemuHotplugWaitForTrayEject(virDomainObjPtr vm, virDomainDiskDefPtr disk) { - unsigned long long now; + gint64 now = g_get_monotonic_time(); int rc; - if (virTimeMillisNow(&now) < 0) - return -1; - while (disk->tray_status != VIR_DOMAIN_DISK_TRAY_OPEN) { - if ((rc = virDomainObjWaitUntil(vm, now + CHANGE_MEDIA_TIMEOUT)) < 0) - return -1; + rc = virDomainObjWaitUntil(vm, now + CHANGE_MEDIA_TIMEOUT); if (rc > 0) { /* the caller called qemuMonitorEjectMedia which usually reports an @@ -5122,7 +5118,7 @@ qemuDomainResetDeviceRemoval(virDomainObjPtr vm) } -unsigned long long +gint64 qemuDomainGetUnplugTimeout(virDomainObjPtr vm) { if (qemuDomainIsPSeries(vm->def)) @@ -5145,22 +5141,14 @@ static int qemuDomainWaitForDeviceRemoval(virDomainObjPtr vm) { qemuDomainObjPrivatePtr priv = vm->privateData; - unsigned long long until; + gint64 until; int rc; - if (virTimeMillisNow(&until) < 0) - return 1; - until += qemuDomainGetUnplugTimeout(vm); + until = g_get_monotonic_time() + qemuDomainGetUnplugTimeout(vm); while (priv->unplug.alias) { if ((rc = virDomainObjWaitUntil(vm, until)) == 1) return 0; - - if (rc < 0) { - VIR_WARN("Failed to wait on unplug condition for domain '%s' " - "device '%s'", vm->def->name, priv->unplug.alias); - return 1; - } } if (priv->unplug.status == QEMU_DOMAIN_UNPLUGGING_DEVICE_STATUS_GUEST_REJECTED) { diff --git a/src/qemu/qemu_hotplug.h b/src/qemu/qemu_hotplug.h index 4a49e04a15..f610d7c70f 100644 --- a/src/qemu/qemu_hotplug.h +++ b/src/qemu/qemu_hotplug.h @@ -151,7 +151,7 @@ int qemuDomainSetVcpuInternal(virQEMUDriverPtr driver, virBitmapPtr vcpus, bool state); -unsigned long long qemuDomainGetUnplugTimeout(virDomainObjPtr vm); +gint64 qemuDomainGetUnplugTimeout(virDomainObjPtr vm); int qemuHotplugAttachDBusVMState(virQEMUDriverPtr driver, virDomainObjPtr vm, diff --git a/src/qemu/qemu_monitor.c b/src/qemu/qemu_monitor.c index a62fed845e..4537b3e562 100644 --- a/src/qemu/qemu_monitor.c +++ b/src/qemu/qemu_monitor.c @@ -70,7 +70,7 @@ VIR_LOG_INIT("qemu.qemu_monitor"); struct _qemuMonitor { virObjectLockable parent; - virCond notify; + GCond notify; int fd; @@ -228,7 +228,7 @@ qemuMonitorDispose(void *obj) g_main_context_unref(mon->context); virResetError(&mon->lastError); - virCondDestroy(&mon->notify); + g_cond_clear(&mon->notify); VIR_FREE(mon->buffer); virJSONValueFree(mon->options); VIR_FREE(mon->balloonpath); @@ -357,7 +357,7 @@ qemuMonitorIOProcess(qemuMonitorPtr mon) * while dealing with qemu event, mon->msg could be changed which * means the above 'msg' may be invalid, thus we use 'mon->msg' here */ if (mon->msg && mon->msg->finished) - virCondBroadcast(&mon->notify); + g_cond_broadcast(&mon->notify); return len; } @@ -618,7 +618,7 @@ qemuMonitorIO(GSocket *socket G_GNUC_UNUSED, * then wakeup that waiter */ if (mon->msg && !mon->msg->finished) { mon->msg->finished = 1; - virCondSignal(&mon->notify); + g_cond_signal(&mon->notify); } } @@ -632,7 +632,7 @@ qemuMonitorIO(GSocket *socket G_GNUC_UNUSED, virDomainObjPtr vm = mon->vm; /* Make sure anyone waiting wakes up now */ - virCondSignal(&mon->notify); + g_cond_signal(&mon->notify); virObjectUnlock(mon); VIR_DEBUG("Triggering EOF callback"); (eofNotify)(mon, vm, mon->callbackOpaque); @@ -642,7 +642,7 @@ qemuMonitorIO(GSocket *socket G_GNUC_UNUSED, virDomainObjPtr vm = mon->vm; /* Make sure anyone waiting wakes up now */ - virCondSignal(&mon->notify); + g_cond_signal(&mon->notify); virObjectUnlock(mon); VIR_DEBUG("Triggering error callback"); (errorNotify)(mon, vm, mon->callbackOpaque); @@ -683,11 +683,8 @@ qemuMonitorOpenInternal(virDomainObjPtr vm, if (!(mon = virObjectLockableNew(qemuMonitorClass))) return NULL; - if (virCondInit(&mon->notify) < 0) { - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", - _("cannot initialize monitor condition")); - goto cleanup; - } + g_cond_init(&mon->notify); + mon->fd = fd; mon->context = g_main_context_ref(context); mon->vm = virObjectRef(vm); @@ -881,7 +878,7 @@ qemuMonitorClose(qemuMonitorPtr mon) virResetLastError(); } mon->msg->finished = 1; - virCondSignal(&mon->notify); + g_cond_signal(&mon->notify); } /* Propagate existing monitor error in case the current thread has no @@ -935,11 +932,7 @@ qemuMonitorSend(qemuMonitorPtr mon, mon, mon->msg->txBuffer, mon->msg->txFD); while (!mon->msg->finished) { - if (virCondWait(&mon->notify, &mon->parent.lock) < 0) { - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", - _("Unable to wait on monitor condition")); - goto cleanup; - } + g_cond_wait(&mon->notify, &mon->parent.lock); } if (mon->lastError.code != VIR_ERR_OK) { diff --git a/src/rpc/virnetclient.c b/src/rpc/virnetclient.c index 1c5bef86a1..28192c7046 100644 --- a/src/rpc/virnetclient.c +++ b/src/rpc/virnetclient.c @@ -58,7 +58,7 @@ struct _virNetClientCall { bool nonBlock; bool haveThread; - virCond cond; + GCond cond; virNetClientCallPtr next; }; @@ -1513,12 +1513,12 @@ static bool virNetClientIOEventLoopRemoveDone(virNetClientCallPtr call, */ if (call->haveThread) { VIR_DEBUG("Waking up sleep %p", call); - virCondSignal(&call->cond); + g_cond_signal(&call->cond); } else { VIR_DEBUG("Removing completed call %p", call); if (call->expectReply) VIR_WARN("Got a call expecting a reply but without a waiting thread"); - virCondDestroy(&call->cond); + g_cond_clear(&call->cond); VIR_FREE(call->msg); VIR_FREE(call); } @@ -1545,7 +1545,7 @@ virNetClientIOEventLoopRemoveAll(virNetClientCallPtr call, return false; VIR_DEBUG("Removing call %p", call); - virCondDestroy(&call->cond); + g_cond_clear(&call->cond); VIR_FREE(call->msg); VIR_FREE(call); return true; @@ -1563,7 +1563,7 @@ virNetClientIOEventLoopPassTheBuck(virNetClientPtr client, while (tmp) { if (tmp != thiscall && tmp->haveThread) { VIR_DEBUG("Passing the buck to %p", tmp); - virCondSignal(&tmp->cond); + g_cond_signal(&tmp->cond); return; } tmp = tmp->next; @@ -1897,12 +1897,7 @@ static int virNetClientIO(virNetClientPtr client, VIR_DEBUG("Going to sleep head=%p call=%p", client->waitDispatch, thiscall); /* Go to sleep while other thread is working... */ - if (virCondWait(&thiscall->cond, &client->parent.lock) < 0) { - virNetClientCallRemove(&client->waitDispatch, thiscall); - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", - _("failed to wait on condition")); - return -1; - } + g_cond_wait(&thiscall->cond, &client->parent.lock); VIR_DEBUG("Woken up from sleep head=%p call=%p", client->waitDispatch, thiscall); @@ -2042,11 +2037,7 @@ virNetClientCallNew(virNetMessagePtr msg, if (VIR_ALLOC(call) < 0) goto error; - if (virCondInit(&call->cond) < 0) { - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", - _("cannot initialize condition variable")); - goto error; - } + g_cond_init(&call->cond); msg->donefds = 0; if (msg->bufferLength) @@ -2124,7 +2115,7 @@ static int virNetClientSendInternal(virNetClientPtr client, if (ret == 1) return 1; - virCondDestroy(&call->cond); + g_cond_clear(&call->cond); VIR_FREE(call); return ret; } diff --git a/src/util/virfdstream.c b/src/util/virfdstream.c index 111e451f8c..26619472da 100644 --- a/src/util/virfdstream.c +++ b/src/util/virfdstream.c @@ -103,7 +103,7 @@ struct virFDStreamData { /* Thread data */ virThreadPtr thread; - virCond threadCond; + GCond threadCond; virErrorPtr threadErr; bool threadQuit; bool threadAbort; @@ -149,7 +149,7 @@ virFDStreamMsgQueuePush(virFDStreamDataPtr fdst, tmp = &(*tmp)->next; *tmp = msg; - virCondSignal(&fdst->threadCond); + g_cond_signal(&fdst->threadCond); if (safewrite(fd, &c, sizeof(c)) != sizeof(c)) { virReportSystemError(errno, @@ -175,7 +175,7 @@ virFDStreamMsgQueuePop(virFDStreamDataPtr fdst, tmp->next = NULL; } - virCondSignal(&fdst->threadCond); + g_cond_signal(&fdst->threadCond); if (saferead(fd, &c, sizeof(c)) != sizeof(c)) { virReportSystemError(errno, @@ -591,11 +591,7 @@ virFDStreamThread(void *opaque) while (doRead == (fdst->msg != NULL) && !fdst->threadQuit) { - if (virCondWait(&fdst->threadCond, &fdst->parent.lock)) { - virReportSystemError(errno, "%s", - _("failed to wait on condition")); - goto error; - } + g_cond_wait(&fdst->threadCond, &fdst->parent.lock); } if (fdst->threadQuit) { @@ -654,7 +650,7 @@ virFDStreamJoinWorker(virFDStreamDataPtr fdst, fdst->threadAbort = streamAbort; fdst->threadQuit = true; - virCondSignal(&fdst->threadCond); + g_cond_signal(&fdst->threadCond); /* Give the thread a chance to lock the FD stream object. */ virObjectUnlock(fdst); @@ -669,7 +665,7 @@ virFDStreamJoinWorker(virFDStreamDataPtr fdst, ret = 0; cleanup: VIR_FREE(fdst->thread); - virCondDestroy(&fdst->threadCond); + g_cond_clear(&fdst->threadCond); return ret; } @@ -891,7 +887,7 @@ static int virFDStreamRead(virStreamPtr st, char *bytes, size_t nbytes) goto cleanup; } else { virObjectUnlock(fdst); - virCondSignal(&fdst->threadCond); + g_cond_signal(&fdst->threadCond); virObjectLock(fdst); } } @@ -1057,7 +1053,7 @@ virFDStreamInData(virStreamPtr st, goto cleanup; } else { virObjectUnlock(fdst); - virCondSignal(&fdst->threadCond); + g_cond_signal(&fdst->threadCond); virObjectLock(fdst); } } @@ -1128,11 +1124,7 @@ static int virFDStreamOpenInternal(virStreamPtr st, if (VIR_ALLOC(fdst->thread) < 0) goto error; - if (virCondInit(&fdst->threadCond) < 0) { - virReportSystemError(errno, "%s", - _("cannot initialize condition variable")); - goto error; - } + g_cond_init(&fdst->threadCond); if (virThreadCreateFull(fdst->thread, true, diff --git a/src/util/virobject.c b/src/util/virobject.c index aee1bb4af6..5bf4706f79 100644 --- a/src/util/virobject.c +++ b/src/util/virobject.c @@ -268,12 +268,7 @@ virObjectLockableNew(virClassPtr klass) if (!(obj = virObjectNew(klass))) return NULL; - if (virMutexInit(&obj->lock) < 0) { - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", - _("Unable to initialize mutex")); - virObjectUnref(obj); - return NULL; - } + g_mutex_init(&obj->lock); return obj; } @@ -305,7 +300,7 @@ virObjectLockableDispose(void *anyobj) { virObjectLockablePtr obj = anyobj; - virMutexDestroy(&obj->lock); + g_mutex_clear(&obj->lock); } @@ -424,7 +419,7 @@ virObjectLock(void *anyobj) if (!obj) return; - virMutexLock(&obj->lock); + g_mutex_lock(&obj->lock); } @@ -501,7 +496,7 @@ virObjectUnlock(void *anyobj) if (!obj) return; - virMutexUnlock(&obj->lock); + g_mutex_unlock(&obj->lock); } diff --git a/src/util/virobject.h b/src/util/virobject.h index fcfe77578b..1f6f581e28 100644 --- a/src/util/virobject.h +++ b/src/util/virobject.h @@ -58,7 +58,7 @@ struct _virObject { struct _virObjectLockable { virObject parent; - virMutex lock; + GMutex lock; }; struct _virObjectRWLockable { diff --git a/src/vz/vz_utils.c b/src/vz/vz_utils.c index c31ff53cfb..73dec18515 100644 --- a/src/vz/vz_utils.c +++ b/src/vz/vz_utils.c @@ -559,17 +559,11 @@ vzDomObjAlloc(void *opaque G_GNUC_UNUSED) if (VIR_ALLOC(pdom) < 0) return NULL; - if (virCondInit(&pdom->job.cond) < 0) - goto error; + g_cond_init(&pdom->job.cond); pdom->stats = PRL_INVALID_HANDLE; return pdom; - - error: - VIR_FREE(pdom); - - return NULL; } void @@ -582,45 +576,33 @@ vzDomObjFree(void* p) PrlHandle_Free(pdom->sdkdom); PrlHandle_Free(pdom->stats); - virCondDestroy(&pdom->job.cond); + g_cond_clear(&pdom->job.cond); VIR_FREE(pdom); }; -#define VZ_JOB_WAIT_TIME (1000 * 30) +#define VZ_JOB_WAIT_TIME (30 * G_TIME_SPAN_SECOND) int vzDomainObjBeginJob(virDomainObjPtr dom) { vzDomObjPtr pdom = dom->privateData; - unsigned long long now; - unsigned long long then; - - if (virTimeMillisNow(&now) < 0) - return -1; - then = now + VZ_JOB_WAIT_TIME; + gint64 then = g_get_monotonic_time() + VZ_JOB_WAIT_TIME; while (pdom->job.active) { - if (virCondWaitUntil(&pdom->job.cond, &dom->parent.lock, then) < 0) + if (!g_cond_wait_until(&pdom->job.cond, &dom->parent.lock, then)) goto error; } - if (virTimeMillisNow(&now) < 0) - return -1; - pdom->job.active = true; - pdom->job.started = now; + pdom->job.started = g_get_monotonic_time(); pdom->job.elapsed = 0; pdom->job.progress = 0; pdom->job.hasProgress = false; return 0; error: - if (errno == ETIMEDOUT) - virReportError(VIR_ERR_OPERATION_TIMEOUT, - "%s", _("cannot acquire state change lock")); - else - virReportSystemError(errno, - "%s", _("cannot acquire job mutex")); + virReportError(VIR_ERR_OPERATION_TIMEOUT, + "%s", _("cannot acquire state change lock")); return -1; } @@ -631,19 +613,18 @@ vzDomainObjEndJob(virDomainObjPtr dom) pdom->job.active = false; pdom->job.cancelled = false; - virCondSignal(&pdom->job.cond); + g_cond_signal(&pdom->job.cond); } int vzDomainJobUpdateTime(vzDomainJobObjPtr job) { - unsigned long long now; + gint64 now; if (!job->started) return 0; - if (virTimeMillisNow(&now) < 0) - return -1; + now = g_get_monotonic_time(); if (now < job->started) { VIR_WARN("Async job starts in the future"); diff --git a/src/vz/vz_utils.h b/src/vz/vz_utils.h index 7ebb34bb11..6497e5530f 100644 --- a/src/vz/vz_utils.h +++ b/src/vz/vz_utils.h @@ -93,11 +93,11 @@ typedef struct _vzConn vzConn; typedef struct _vzConn *vzConnPtr; struct _vzDomainJobObj { - virCond cond; + GCond cond; bool active; /* when the job started, zeroed on time discontinuities */ - unsigned long long started; - unsigned long long elapsed; + gint64 started; + gint64 elapsed; bool hasProgress; int progress; /* percents */ PRL_HANDLE sdkJob; diff --git a/tests/qemuhotplugmock.c b/tests/qemuhotplugmock.c index 8e5b07788d..71fe2db88c 100644 --- a/tests/qemuhotplugmock.c +++ b/tests/qemuhotplugmock.c @@ -38,15 +38,15 @@ init_syms(void) VIR_MOCK_REAL_INIT(virFileExists); } -unsigned long long +gint64 qemuDomainGetUnplugTimeout(virDomainObjPtr vm G_GNUC_UNUSED) { /* Wait only 100ms for DEVICE_DELETED event. Give a greater * timeout in case of PSeries guest to be consistent with the * original logic. */ if (qemuDomainIsPSeries(vm->def)) - return 200; - return 100; + return 200 * G_TIME_SPAN_MILLISECOND; + return 100 * G_TIME_SPAN_MILLISECOND; } diff --git a/tests/qemuhotplugtest.c b/tests/qemuhotplugtest.c index d9244dca44..581a09d204 100644 --- a/tests/qemuhotplugtest.c +++ b/tests/qemuhotplugtest.c @@ -352,6 +352,7 @@ testQemuHotplug(const void *data) if (keep) { test->vm = vm; } else { + virObjectUnlock(vm); virObjectUnref(vm); test->vm = NULL; } @@ -396,6 +397,7 @@ testQemuHotplugCpuDataFree(struct testQemuHotplugCpuData *data) priv = data->vm->privateData; priv->mon = NULL; + virObjectUnlock(data->vm); virObjectUnref(data->vm); } diff --git a/tests/qemumonitortestutils.c b/tests/qemumonitortestutils.c index 78fc94e710..9c5a7249ae 100644 --- a/tests/qemumonitortestutils.c +++ b/tests/qemumonitortestutils.c @@ -79,6 +79,8 @@ struct _qemuMonitorTest { virDomainObjPtr vm; virHashTablePtr qapischema; + + bool vmCreated; }; @@ -392,6 +394,8 @@ qemuMonitorTestFree(qemuMonitorTestPtr test) g_object_unref(test->eventThread); + if (test->vmCreated) + virObjectUnlock(test->vm); virObjectUnref(test->vm); if (test->started) @@ -1062,6 +1066,7 @@ qemuMonitorCommonTestNew(virDomainXMLOptionPtr xmlopt, test->vm = virDomainObjNew(xmlopt); if (!test->vm) goto error; + test->vmCreated = true; if (!(test->vm->def = virDomainDefNew())) goto error; } diff --git a/tests/qemusecuritytest.c b/tests/qemusecuritytest.c index a1ef9d8802..65322ae29e 100644 --- a/tests/qemusecuritytest.c +++ b/tests/qemusecuritytest.c @@ -100,19 +100,19 @@ testDomain(const void *opaque) if (virStorageSourceIsLocalStorage(src) && src->path && (src->shared || src->readonly) && virStringListAdd(¬Restored, src->path) < 0) - return -1; + goto unlock; for (n = src->backingStore; virStorageSourceIsBacking(n); n = n->backingStore) { if (virStorageSourceIsLocalStorage(n) && n->path && virStringListAdd(¬Restored, n->path) < 0) - return -1; + goto unlock; } } /* Mocking is enabled only when this env variable is set. * See mock code for explanation. */ if (g_setenv(ENVVAR, "1", FALSE) == FALSE) - return -1; + goto unlock; if (qemuSecuritySetAllLabel(data->driver, vm, NULL, false) < 0) goto cleanup; @@ -124,9 +124,13 @@ testDomain(const void *opaque) ret = 0; cleanup: + virObjectUnlock(vm); g_unsetenv(ENVVAR); freePaths(); return ret; +unlock: + virObjectUnlock(vm); + return -1; } diff --git a/tests/qemuxml2argvtest.c b/tests/qemuxml2argvtest.c index 405227fdf6..180201f74e 100644 --- a/tests/qemuxml2argvtest.c +++ b/tests/qemuxml2argvtest.c @@ -592,6 +592,7 @@ testCompareXMLToArgv(const void *data) VIR_FREE(actualargv); virDomainChrSourceDefClear(&monitor_chr); virCommandFree(cmd); + virObjectUnlock(vm); virObjectUnref(vm); virSetConnectSecret(NULL); virSetConnectStorage(NULL); diff --git a/tools/virsh-console.c b/tools/virsh-console.c index 2e498a6903..ac4d65cb0e 100644 --- a/tools/virsh-console.c +++ b/tools/virsh-console.c @@ -63,7 +63,7 @@ struct virConsole { virStreamPtr st; bool quit; - virCond cond; + GCond cond; int stdinWatch; int stdoutWatch; @@ -130,7 +130,7 @@ virConsoleShutdown(virConsolePtr con, con->stdoutWatch = -1; if (!con->quit) { con->quit = true; - virCondSignal(&con->cond); + g_cond_signal(&con->cond); } } @@ -143,7 +143,7 @@ virConsoleDispose(void *obj) if (con->st) virStreamFree(con->st); - virCondDestroy(&con->cond); + g_cond_clear(&con->cond); virResetError(&con->error); } @@ -376,21 +376,12 @@ virConsoleNew(void) if (!(con = virObjectLockableNew(virConsoleClass))) return NULL; - if (virCondInit(&con->cond) < 0) { - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", - _("cannot initialize console condition")); - - goto error; - } + g_cond_init(&con->cond); con->stdinWatch = -1; con->stdoutWatch = -1; return con; - - error: - virObjectUnref(con); - return NULL; } @@ -484,11 +475,7 @@ virshRunConsole(vshControl *ctl, } while (!con->quit) { - if (virCondWait(&con->cond, &con->parent.lock) < 0) { - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", - _("unable to wait on console condition")); - goto cleanup; - } + g_cond_wait(&con->cond, &con->parent.lock); } if (con->error.code == VIR_ERR_OK) -- 2.25.2

All instances where they were used have now been converted to the Glib equivalents. Signed-off-by: Rafael Fonseca <r4f4rfs@gmail.com> --- examples/systemtap/lock-debug.stp | 4 +- src/libvirt_private.syms | 11 ---- src/util/virthread.c | 101 ------------------------------ src/util/virthread.h | 41 ------------ 4 files changed, 2 insertions(+), 155 deletions(-) diff --git a/examples/systemtap/lock-debug.stp b/examples/systemtap/lock-debug.stp index 0c9cc38692..dd8ee42b54 100644 --- a/examples/systemtap/lock-debug.stp +++ b/examples/systemtap/lock-debug.stp @@ -35,12 +35,12 @@ probe library = process( %( $# > 0 %? @1 %: "/usr/lib/libvirt.so" %) ) if (filter()) next } -probe lock = library.function("virMutexLock") +probe lock = library.function("g_mutex_lock") { lockname = usymdata($m) } -probe unlock = library.function("virMutexUnlock") +probe unlock = library.function("g_mutex_unlock") { lockname = usymdata($m) } diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 4dfafdfd3e..054a1a5fce 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -3259,17 +3259,6 @@ virSystemdTerminateMachine; # util/virthread.h -virCondBroadcast; -virCondDestroy; -virCondInit; -virCondSignal; -virCondWait; -virCondWaitUntil; -virMutexDestroy; -virMutexInit; -virMutexInitRecursive; -virMutexLock; -virMutexUnlock; virOnce; virThreadCancel; virThreadCreateFull; diff --git a/src/util/virthread.c b/src/util/virthread.c index 1ec5096ced..5300273f0e 100644 --- a/src/util/virthread.c +++ b/src/util/virthread.c @@ -43,107 +43,6 @@ int virOnce(virOnceControlPtr once, virOnceFunc init) } -int virMutexInit(virMutexPtr m) -{ - int ret; - pthread_mutexattr_t attr; - pthread_mutexattr_init(&attr); - pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL); - ret = pthread_mutex_init(&m->lock, &attr); - pthread_mutexattr_destroy(&attr); - if (ret != 0) { - errno = ret; - return -1; - } - return 0; -} - -int virMutexInitRecursive(virMutexPtr m) -{ - int ret; - pthread_mutexattr_t attr; - pthread_mutexattr_init(&attr); - pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); - ret = pthread_mutex_init(&m->lock, &attr); - pthread_mutexattr_destroy(&attr); - if (ret != 0) { - errno = ret; - return -1; - } - return 0; -} - -void virMutexDestroy(virMutexPtr m) -{ - pthread_mutex_destroy(&m->lock); -} - -void virMutexLock(virMutexPtr m) -{ - pthread_mutex_lock(&m->lock); -} - -void virMutexUnlock(virMutexPtr m) -{ - pthread_mutex_unlock(&m->lock); -} - - -int virCondInit(virCondPtr c) -{ - int ret; - if ((ret = pthread_cond_init(&c->cond, NULL)) != 0) { - errno = ret; - return -1; - } - return 0; -} - -int virCondDestroy(virCondPtr c) -{ - int ret; - if ((ret = pthread_cond_destroy(&c->cond)) != 0) { - errno = ret; - return -1; - } - return 0; -} - -int virCondWait(virCondPtr c, virMutexPtr m) -{ - int ret; - if ((ret = pthread_cond_wait(&c->cond, &m->lock)) != 0) { - errno = ret; - return -1; - } - return 0; -} - -int virCondWaitUntil(virCondPtr c, virMutexPtr m, unsigned long long whenms) -{ - int ret; - struct timespec ts; - - ts.tv_sec = whenms / 1000; - ts.tv_nsec = (whenms % 1000) * 1000000; - - if ((ret = pthread_cond_timedwait(&c->cond, &m->lock, &ts)) != 0) { - errno = ret; - return -1; - } - return 0; -} - -void virCondSignal(virCondPtr c) -{ - pthread_cond_signal(&c->cond); -} - -void virCondBroadcast(virCondPtr c) -{ - pthread_cond_broadcast(&c->cond); -} - struct virThreadArgs { virThreadFunc func; char *name; diff --git a/src/util/virthread.h b/src/util/virthread.h index a57c99bdf0..d55407d693 100644 --- a/src/util/virthread.h +++ b/src/util/virthread.h @@ -26,20 +26,6 @@ #include <pthread.h> -typedef struct virMutex virMutex; -typedef virMutex *virMutexPtr; - -struct virMutex { - pthread_mutex_t lock; -}; - -typedef struct virCond virCond; -typedef virCond *virCondPtr; - -struct virCond { - pthread_cond_t cond; -}; - typedef struct virThreadLocal virThreadLocal; typedef virThreadLocal *virThreadLocalPtr; @@ -62,11 +48,6 @@ struct virOnceControl { }; -#define VIR_MUTEX_INITIALIZER \ - { \ - .lock = PTHREAD_MUTEX_INITIALIZER \ - } - #define VIR_ONCE_CONTROL_INITIALIZER \ { \ .once = PTHREAD_ONCE_INIT \ @@ -117,28 +98,6 @@ unsigned long long virThreadID(virThreadPtr thread); int virOnce(virOnceControlPtr once, virOnceFunc init) ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) G_GNUC_WARN_UNUSED_RESULT; -int virMutexInit(virMutexPtr m) G_GNUC_WARN_UNUSED_RESULT; -int virMutexInitRecursive(virMutexPtr m) G_GNUC_WARN_UNUSED_RESULT; -void virMutexDestroy(virMutexPtr m); - -void virMutexLock(virMutexPtr m); -void virMutexUnlock(virMutexPtr m); - - -int virCondInit(virCondPtr c) G_GNUC_WARN_UNUSED_RESULT; -int virCondDestroy(virCondPtr c); - -/* virCondWait, virCondWaitUntil: - * These functions can return without the associated predicate - * changing value. Therefore in nearly all cases they - * should be enclosed in a while loop that checks the predicate. - */ -int virCondWait(virCondPtr c, virMutexPtr m) G_GNUC_WARN_UNUSED_RESULT; -int virCondWaitUntil(virCondPtr c, virMutexPtr m, unsigned long long whenms) G_GNUC_WARN_UNUSED_RESULT; - -void virCondSignal(virCondPtr c); -void virCondBroadcast(virCondPtr c); - typedef void (*virThreadLocalCleanup)(void *); int virThreadLocalInit(virThreadLocalPtr l, -- 2.25.2
participants (5)
-
Daniel P. Berrangé
-
Laine Stump
-
Pavel Mores
-
Pino Toscano
-
Rafael Fonseca