qemumonitortestutils: Use automatic mutex management

Signed-off-by: Tim Wiederhake <twiederh@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
This commit is contained in:
Tim Wiederhake 2022-02-08 15:31:35 +01:00
parent 8e5b04e332
commit 6ccafcb53e

View File

@ -253,12 +253,11 @@ qemuMonitorTestIO(virNetSocket *sock,
{
qemuMonitorTest *test = opaque;
bool err = false;
VIR_LOCK_GUARD lock = virLockGuardLock(&test->lock);
virMutexLock(&test->lock);
if (test->quit) {
virMutexUnlock(&test->lock);
if (test->quit)
return;
}
if (events & VIR_EVENT_HANDLE_WRITABLE) {
ssize_t ret;
if ((ret = virNetSocketWrite(sock,
@ -336,7 +335,6 @@ qemuMonitorTestIO(virNetSocket *sock,
virNetSocketUpdateIOCallback(sock, events);
}
virMutexUnlock(&test->lock);
}
@ -345,24 +343,22 @@ qemuMonitorTestWorker(void *opaque)
{
qemuMonitorTest *test = opaque;
virMutexLock(&test->lock);
while (!test->quit) {
virMutexUnlock(&test->lock);
if (virEventRunDefaultImpl() < 0) {
virMutexLock(&test->lock);
test->quit = true;
break;
while (true) {
VIR_WITH_MUTEX_LOCK_GUARD(&test->lock) {
if (test->quit) {
test->running = false;
return;
}
}
virMutexLock(&test->lock);
if (virEventRunDefaultImpl() < 0) {
VIR_WITH_MUTEX_LOCK_GUARD(&test->lock) {
test->quit = true;
test->running = false;
return;
}
}
}
test->running = false;
virMutexUnlock(&test->lock);
return;
}
@ -383,13 +379,13 @@ qemuMonitorTestFree(qemuMonitorTest *test)
if (!test)
return;
virMutexLock(&test->lock);
if (test->running) {
test->quit = true;
/* HACK: Add a dummy timeout to break event loop */
timer = virEventAddTimeout(0, qemuMonitorTestFreeTimer, NULL, NULL);
VIR_WITH_MUTEX_LOCK_GUARD(&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);
if (test->client) {
virNetSocketRemoveIOCallback(test->client);
@ -463,9 +459,9 @@ qemuMonitorTestAddHandler(qemuMonitorTest *test,
item->freecb = freecb;
item->opaque = opaque;
virMutexLock(&test->lock);
VIR_APPEND_ELEMENT(test->items, test->nitems, item);
virMutexUnlock(&test->lock);
VIR_WITH_MUTEX_LOCK_GUARD(&test->lock) {
VIR_APPEND_ELEMENT(test->items, test->nitems, item);
}
return 0;
}
@ -1072,16 +1068,11 @@ qemuMonitorCommonTestInit(qemuMonitorTest *test)
NULL) < 0)
return -1;
virMutexLock(&test->lock);
if (virThreadCreate(&test->thread,
true,
qemuMonitorTestWorker,
test) < 0) {
virMutexUnlock(&test->lock);
return -1;
VIR_WITH_MUTEX_LOCK_GUARD(&test->lock) {
if (virThreadCreate(&test->thread, true, qemuMonitorTestWorker, test) < 0)
return -1;
test->started = test->running = true;
}
test->started = test->running = true;
virMutexUnlock(&test->lock);
return 0;
}