提交 3f671e6c 编写于 作者: D Daniel P. Berrange

Use a static initializer for static mutexes

Now that we ditched our custom pthread impl for Win32, we can
use PTHREAD_MUTEX_INITIALIZER for static mutexes. This avoids
the need to use a virOnce one-time global initializer in a
number of places.
Signed-off-by: NDaniel P. Berrange <berrange@redhat.com>
上级 25d5d705
......@@ -33,7 +33,7 @@
#define VIR_FROM_THIS VIR_FROM_NWFILTER
static virMutex ipAddressMapLock;
static virMutex ipAddressMapLock = VIR_MUTEX_INITIALIZER;
static virNWFilterHashTablePtr ipAddressMap;
......@@ -147,11 +147,6 @@ virNWFilterIPAddrMapInit(void)
if (!ipAddressMap)
return -1;
if (virMutexInit(&ipAddressMapLock) < 0) {
virNWFilterIPAddrMapShutdown();
return -1;
}
return 0;
}
......
......@@ -823,7 +823,7 @@ static int lxcControllerClearCapabilities(void)
}
static bool wantReboot = false;
static virMutex lock;
static virMutex lock = VIR_MUTEX_INITIALIZER;
static void virLXCControllerSignalChildIO(virNetServerPtr server,
......@@ -1108,9 +1108,6 @@ static int virLXCControllerMain(virLXCControllerPtr ctrl)
int rc = -1;
size_t i;
if (virMutexInit(&lock) < 0)
goto cleanup2;
if (virNetServerAddSignalHandler(ctrl->server,
SIGCHLD,
virLXCControllerSignalChildIO,
......@@ -1164,9 +1161,6 @@ static int virLXCControllerMain(virLXCControllerPtr ctrl)
rc = wantReboot ? 1 : 0;
cleanup:
virMutexDestroy(&lock);
cleanup2:
for (i = 0; i < ctrl->nconsoles; i++)
virLXCControllerConsoleClose(&(ctrl->consoles[i]));
......
......@@ -211,7 +211,7 @@ static void ebiptablesDriverShutdown(void);
static void ebtablesCleanAll(const char *ifname);
static int ebiptablesAllTeardown(const char *ifname);
static virMutex execCLIMutex;
static virMutex execCLIMutex = VIR_MUTEX_INITIALIZER;
struct ushort_map {
unsigned short attr;
......@@ -4376,9 +4376,6 @@ ebiptablesDriverInit(bool privileged)
if (!privileged)
return 0;
if (virMutexInit(&execCLIMutex) < 0)
return -EINVAL;
grep_cmd_path = virFindFileInPath("grep");
/*
......
......@@ -119,10 +119,10 @@ struct ether_vlan_header
} ATTRIBUTE_PACKED;
static virMutex pendingLearnReqLock;
static virMutex pendingLearnReqLock = VIR_MUTEX_INITIALIZER;
static virHashTablePtr pendingLearnReq;
static virMutex ifaceMapLock;
static virMutex ifaceMapLock = VIR_MUTEX_INITIALIZER;
static virHashTablePtr ifaceLockMap;
typedef struct _virNWFilterIfaceLock virNWFilterIfaceLock;
......@@ -806,22 +806,12 @@ virNWFilterLearnInit(void)
return -1;
}
if (virMutexInit(&pendingLearnReqLock) < 0) {
virNWFilterLearnShutdown();
return -1;
}
ifaceLockMap = virHashCreate(0, virHashValueFree);
if (!ifaceLockMap) {
virNWFilterLearnShutdown();
return -1;
}
if (virMutexInit(&ifaceMapLock) < 0) {
virNWFilterLearnShutdown();
return -1;
}
return 0;
}
......
......@@ -119,7 +119,7 @@ typedef struct _testConn *testConnPtr;
static testConn defaultConn;
static int defaultConnections;
static virMutex defaultLock;
static virMutex defaultLock = VIR_MUTEX_INITIALIZER;
#define TEST_MODEL "i686"
#define TEST_MODEL_WORDSIZE 32
......@@ -141,15 +141,6 @@ static int testConnectClose(virConnectPtr conn);
static void testObjectEventQueue(testConnPtr driver,
virObjectEventPtr event);
static int
testOnceInit(void)
{
return virMutexInit(&defaultLock);
}
VIR_ONCE_GLOBAL_INIT(test)
static void testDriverLock(testConnPtr driver)
{
virMutexLock(&driver->lock);
......@@ -1554,9 +1545,6 @@ static virDrvOpenStatus testConnectOpen(virConnectPtr conn,
virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR);
if (testInitialize() < 0)
return VIR_DRV_OPEN_ERROR;
if (!conn->uri)
return VIR_DRV_OPEN_DECLINED;
......@@ -1596,9 +1584,6 @@ static int testConnectClose(virConnectPtr conn)
{
testConnPtr privconn = conn->privateData;
if (testInitialize() < 0)
return -1;
if (privconn == &defaultConn) {
virMutexLock(&defaultLock);
if (--defaultConnections) {
......
......@@ -73,18 +73,7 @@ VIR_LOG_INIT("util.netdevmacvlan");
# define MACVLAN_NAME_PREFIX "macvlan"
# define MACVLAN_NAME_PATTERN "macvlan%d"
virMutex virNetDevMacVLanCreateMutex;
static int virNetDevMacVLanCreateMutexOnceInit(void)
{
if (virMutexInit(&virNetDevMacVLanCreateMutex) < 0) {
virReportSystemError(errno, "%s", _("unable to init mutex"));
return -1;
}
return 0;
}
VIR_ONCE_GLOBAL_INIT(virNetDevMacVLanCreateMutex);
virMutex virNetDevMacVLanCreateMutex = VIR_MUTEX_INITIALIZER;
/**
* virNetDevMacVLanCreate:
......@@ -873,8 +862,6 @@ int virNetDevMacVLanCreateWithVPortProfile(const char *tgifname,
} else {
create_name:
retries = 5;
if (virNetDevMacVLanCreateMutexInitialize() < 0)
return -1;
virMutexLock(&virNetDevMacVLanCreateMutex);
for (c = 0; c < 8192; c++) {
snprintf(ifname, sizeof(ifname), pattern, c);
......
......@@ -41,18 +41,7 @@ VIR_LOG_INIT("util.netdevveth");
/* Functions */
virMutex virNetDevVethCreateMutex;
static int virNetDevVethCreateMutexOnceInit(void)
{
if (virMutexInit(&virNetDevVethCreateMutex) < 0) {
virReportSystemError(errno, "%s", _("unable to init mutex"));
return -1;
}
return 0;
}
VIR_ONCE_GLOBAL_INIT(virNetDevVethCreateMutex);
virMutex virNetDevVethCreateMutex = VIR_MUTEX_INITIALIZER;
static int virNetDevVethExists(int devNum)
{
......@@ -132,9 +121,6 @@ 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
*/
if (virNetDevVethCreateMutexInitialize() < 0)
return -1;
virMutexLock(&virNetDevVethCreateMutex);
#define MAX_VETH_RETRIES 10
......
......@@ -46,7 +46,7 @@ VIR_LOG_INIT("util.nodesuspend");
static unsigned int nodeSuspendTargetMask;
static bool nodeSuspendTargetMaskInit;
static virMutex virNodeSuspendMutex;
static virMutex virNodeSuspendMutex = VIR_MUTEX_INITIALIZER;
static bool aboutToSuspend;
......@@ -61,20 +61,6 @@ static void virNodeSuspendUnlock(void)
}
static int virNodeSuspendOnceInit(void)
{
if (virMutexInit(&virNodeSuspendMutex) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("Unable to initialize mutex"));
return -1;
}
return 0;
}
VIR_ONCE_GLOBAL_INIT(virNodeSuspend)
/**
* virNodeSuspendSetNodeWakeup:
* @alarmTime: time in seconds from now, at which the RTC alarm has to be set.
......@@ -178,9 +164,6 @@ int nodeSuspendForDuration(unsigned int target,
virCheckFlags(0, -1);
if (virNodeSuspendInitialize() < 0)
return -1;
if (virNodeSuspendGetTargetMask(&supported) < 0)
return -1;
......@@ -267,9 +250,6 @@ virNodeSuspendSupportsTarget(unsigned int target, bool *supported)
int status;
int ret = -1;
if (virNodeSuspendInitialize() < 0)
return -1;
*supported = false;
switch (target) {
......
......@@ -56,7 +56,7 @@ enum {
static char randomState[RANDOM_STATE_SIZE];
static struct random_data randomData;
static virMutex randomLock;
static virMutex randomLock = VIR_MUTEX_INITIALIZER;
static int
......@@ -74,9 +74,6 @@ virRandomOnceInit(void)
return -1;
#endif
if (virMutexInit(&randomLock) < 0)
return -1;
if (initstate_r(seed,
randomState,
sizeof(randomState),
......
......@@ -71,6 +71,11 @@ struct virOnceControl {
};
# define VIR_MUTEX_INITIALIZER \
{ \
.lock = PTHREAD_MUTEX_INITIALIZER \
}
# define VIR_ONCE_CONTROL_INITIALIZER \
{ \
.once = PTHREAD_ONCE_INIT \
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册