int
virNWFilterLockIface(const char *ifname)
{
- virNWFilterIfaceLock *ifaceLock;
+ VIR_LOCK_GUARD lock = virLockGuardLock(&ifaceMapLock);
+ virNWFilterIfaceLock *ifaceLock = virHashLookup(ifaceLockMap, ifname);
- virMutexLock(&ifaceMapLock);
-
- ifaceLock = virHashLookup(ifaceLockMap, ifname);
if (!ifaceLock) {
ifaceLock = g_new0(virNWFilterIfaceLock, 1);
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("mutex initialization failed"));
g_free(ifaceLock);
- goto error;
+ return -1;
}
if (virStrcpyStatic(ifaceLock->ifname, ifname) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR,
- _("interface name %s does not fit into "
- "buffer "),
+ _("interface name %s does not fit into buffer"),
ifaceLock->ifname);
g_free(ifaceLock);
- goto error;
+ return -1;
}
while (virHashAddEntry(ifaceLockMap, ifname, ifaceLock)) {
g_free(ifaceLock);
- goto error;
+ return -1;
}
ifaceLock->refctr = 0;
ifaceLock->refctr++;
- virMutexUnlock(&ifaceMapLock);
-
virMutexLock(&ifaceLock->lock);
return 0;
-
- error:
- virMutexUnlock(&ifaceMapLock);
-
- return -1;
}
void
virNWFilterUnlockIface(const char *ifname)
{
- virNWFilterIfaceLock *ifaceLock;
-
- virMutexLock(&ifaceMapLock);
-
- ifaceLock = virHashLookup(ifaceLockMap, ifname);
+ VIR_LOCK_GUARD lock = virLockGuardLock(&ifaceMapLock);
+ virNWFilterIfaceLock *ifaceLock = virHashLookup(ifaceLockMap, ifname);
if (ifaceLock) {
virMutexUnlock(&ifaceLock->lock);
if (ifaceLock->refctr == 0)
virHashRemoveEntry(ifaceLockMap, ifname);
}
-
- virMutexUnlock(&ifaceMapLock);
}
static int
virNWFilterRegisterLearnReq(virNWFilterIPAddrLearnReq *req)
{
- int res = -1;
g_autofree char *ifindex_str = g_strdup_printf("%d", req->ifindex);
+ VIR_LOCK_GUARD lock = virLockGuardLock(&pendingLearnReqLock);
- virMutexLock(&pendingLearnReqLock);
-
- if (!virHashLookup(pendingLearnReq, ifindex_str))
- res = virHashAddEntry(pendingLearnReq, ifindex_str, req);
-
- virMutexUnlock(&pendingLearnReqLock);
+ if (virHashLookup(pendingLearnReq, ifindex_str))
+ return -1;
- return res;
+ return virHashAddEntry(pendingLearnReq, ifindex_str, req);
}
int
virNWFilterTerminateLearnReq(const char *ifname)
{
- int rc = -1;
int ifindex;
- virNWFilterIPAddrLearnReq *req;
g_autofree char *ifindex_str = NULL;
/* It's possible that it's already been removed as a result of
if (virNetDevGetIndex(ifname, &ifindex) < 0) {
virResetLastError();
- return rc;
+ return -1;
}
ifindex_str = g_strdup_printf("%d", ifindex);
- virMutexLock(&pendingLearnReqLock);
-
- req = virHashLookup(pendingLearnReq, ifindex_str);
- if (req) {
- rc = 0;
- req->terminate = true;
+ VIR_WITH_MUTEX_LOCK_GUARD(&pendingLearnReqLock) {
+ virNWFilterIPAddrLearnReq *req;
+ if ((req = virHashLookup(pendingLearnReq, ifindex_str))) {
+ req->terminate = true;
+ return 0;
+ }
}
- virMutexUnlock(&pendingLearnReqLock);
-
- return rc;
+ return -1;
}
bool
virNWFilterHasLearnReq(int ifindex)
{
- void *res;
g_autofree char *ifindex_str = g_strdup_printf("%d", ifindex);
+ VIR_LOCK_GUARD lock = virLockGuardLock(&pendingLearnReqLock);
- virMutexLock(&pendingLearnReqLock);
-
- res = virHashLookup(pendingLearnReq, ifindex_str);
-
- virMutexUnlock(&pendingLearnReqLock);
-
- return res != NULL;
+ return virHashLookup(pendingLearnReq, ifindex_str) != NULL;
}
static virNWFilterIPAddrLearnReq *
virNWFilterDeregisterLearnReq(int ifindex)
{
- virNWFilterIPAddrLearnReq *res;
g_autofree char *ifindex_str = g_strdup_printf("%d", ifindex);
+ VIR_LOCK_GUARD lock = virLockGuardLock(&pendingLearnReqLock);
- virMutexLock(&pendingLearnReqLock);
-
- res = virHashSteal(pendingLearnReq, ifindex_str);
-
- virMutexUnlock(&pendingLearnReqLock);
-
- return res;
+ return virHashSteal(pendingLearnReq, ifindex_str);
}
#endif