esx_stream: 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-16 16:54:13 +01:00
parent 440a8d271d
commit 12e19f172d

View File

@ -198,9 +198,8 @@ esxStreamTransfer(esxStreamPrivate *priv, bool blocking)
static int static int
esxStreamSend(virStreamPtr stream, const char *data, size_t nbytes) esxStreamSend(virStreamPtr stream, const char *data, size_t nbytes)
{ {
int result = -1;
esxStreamPrivate *priv = stream->privateData; esxStreamPrivate *priv = stream->privateData;
int status; VIR_LOCK_GUARD lock = virLockGuardLock(&priv->curl->lock);
if (nbytes == 0) if (nbytes == 0)
return 0; return 0;
@ -215,38 +214,29 @@ esxStreamSend(virStreamPtr stream, const char *data, size_t nbytes)
return -1; return -1;
} }
virMutexLock(&priv->curl->lock);
priv->buffer = (char *)data; priv->buffer = (char *)data;
priv->buffer_size = nbytes; priv->buffer_size = nbytes;
priv->buffer_used = nbytes; priv->buffer_used = nbytes;
if (stream->flags & VIR_STREAM_NONBLOCK) { if (stream->flags & VIR_STREAM_NONBLOCK) {
if (esxStreamTransfer(priv, false) < 0) if (esxStreamTransfer(priv, false) < 0)
goto cleanup; return -1;
if (priv->buffer_used < priv->buffer_size) if (priv->buffer_used >= priv->buffer_size)
result = priv->buffer_size - priv->buffer_used; return -2;
else
result = -2;
} else /* blocking */ { } else /* blocking */ {
do { do {
status = esxStreamTransfer(priv, true); int status = esxStreamTransfer(priv, true);
if (status < 0) if (status < 0)
goto cleanup; return -1;
if (status > 0) if (status > 0)
break; break;
} while (priv->buffer_used > 0); } while (priv->buffer_used > 0);
result = priv->buffer_size - priv->buffer_used;
} }
cleanup: return priv->buffer_size - priv->buffer_used;
virMutexUnlock(&priv->curl->lock);
return result;
} }
static int static int
@ -255,9 +245,8 @@ esxStreamRecvFlags(virStreamPtr stream,
size_t nbytes, size_t nbytes,
unsigned int flags) unsigned int flags)
{ {
int result = -1;
esxStreamPrivate *priv = stream->privateData; esxStreamPrivate *priv = stream->privateData;
int status; VIR_LOCK_GUARD lock = virLockGuardLock(&priv->curl->lock);
virCheckFlags(0, -1); virCheckFlags(0, -1);
@ -274,8 +263,6 @@ esxStreamRecvFlags(virStreamPtr stream,
return -1; return -1;
} }
virMutexLock(&priv->curl->lock);
priv->buffer = data; priv->buffer = data;
priv->buffer_size = nbytes; priv->buffer_size = nbytes;
priv->buffer_used = 0; priv->buffer_used = 0;
@ -291,33 +278,25 @@ esxStreamRecvFlags(virStreamPtr stream,
priv->backlog_used - priv->buffer_used); priv->backlog_used - priv->buffer_used);
priv->backlog_used -= priv->buffer_used; priv->backlog_used -= priv->buffer_used;
result = priv->buffer_used;
} else if (stream->flags & VIR_STREAM_NONBLOCK) { } else if (stream->flags & VIR_STREAM_NONBLOCK) {
if (esxStreamTransfer(priv, false) < 0) if (esxStreamTransfer(priv, false) < 0)
goto cleanup; return -1;
if (priv->buffer_used > 0) if (priv->buffer_used <= 0)
result = priv->buffer_used; return -2;
else
result = -2;
} else /* blocking */ { } else /* blocking */ {
do { do {
status = esxStreamTransfer(priv, true); int status = esxStreamTransfer(priv, true);
if (status < 0) if (status < 0)
goto cleanup; return -1;
if (status > 0) if (status > 0)
break; break;
} while (priv->buffer_used < priv->buffer_size); } while (priv->buffer_used < priv->buffer_size);
result = priv->buffer_used;
} }
cleanup: return priv->buffer_used;
virMutexUnlock(&priv->curl->lock);
return result;
} }
static int static int
@ -348,8 +327,7 @@ esxStreamClose(virStreamPtr stream, bool finish)
if (!priv) if (!priv)
return 0; return 0;
virMutexLock(&priv->curl->lock); VIR_WITH_MUTEX_LOCK_GUARD(&priv->curl->lock) {
if (finish && priv->backlog_used > 0) { if (finish && priv->backlog_used > 0) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("Stream has untransferred data left")); _("Stream has untransferred data left"));
@ -357,8 +335,7 @@ esxStreamClose(virStreamPtr stream, bool finish)
} }
stream->privateData = NULL; stream->privateData = NULL;
}
virMutexUnlock(&priv->curl->lock);
esxFreeStreamPrivate(&priv); esxFreeStreamPrivate(&priv);