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: Daniel P. Berrange <berrange@xxxxxxxxxx> --- src/conf/nwfilter_ipaddrmap.c | 7 +------ src/lxc/lxc_controller.c | 8 +------- src/nwfilter/nwfilter_ebiptables_driver.c | 5 +---- src/nwfilter/nwfilter_learnipaddr.c | 14 ++------------ src/test/test_driver.c | 17 +---------------- src/util/virnetdevmacvlan.c | 15 +-------------- src/util/virnetdevveth.c | 16 +--------------- src/util/virnodesuspend.c | 22 +--------------------- src/util/virrandom.c | 5 +---- src/util/virthread.h | 5 +++++ 10 files changed, 15 insertions(+), 99 deletions(-) diff --git a/src/conf/nwfilter_ipaddrmap.c b/src/conf/nwfilter_ipaddrmap.c index 9cbad49c..4bb6775 100644 --- a/src/conf/nwfilter_ipaddrmap.c +++ b/src/conf/nwfilter_ipaddrmap.c @@ -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; } diff --git a/src/lxc/lxc_controller.c b/src/lxc/lxc_controller.c index 1d3a74b..c05dfec 100644 --- a/src/lxc/lxc_controller.c +++ b/src/lxc/lxc_controller.c @@ -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])); diff --git a/src/nwfilter/nwfilter_ebiptables_driver.c b/src/nwfilter/nwfilter_ebiptables_driver.c index 8dbab18..ce0b7e3 100644 --- a/src/nwfilter/nwfilter_ebiptables_driver.c +++ b/src/nwfilter/nwfilter_ebiptables_driver.c @@ -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"); /* diff --git a/src/nwfilter/nwfilter_learnipaddr.c b/src/nwfilter/nwfilter_learnipaddr.c index 1ffed78..a6cdc87 100644 --- a/src/nwfilter/nwfilter_learnipaddr.c +++ b/src/nwfilter/nwfilter_learnipaddr.c @@ -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; } diff --git a/src/test/test_driver.c b/src/test/test_driver.c index 77e30b2..64f3daa 100644 --- a/src/test/test_driver.c +++ b/src/test/test_driver.c @@ -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) { diff --git a/src/util/virnetdevmacvlan.c b/src/util/virnetdevmacvlan.c index 42313b7..7bbf540 100644 --- a/src/util/virnetdevmacvlan.c +++ b/src/util/virnetdevmacvlan.c @@ -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); diff --git a/src/util/virnetdevveth.c b/src/util/virnetdevveth.c index 14febb7..e9d6f9c 100644 --- a/src/util/virnetdevveth.c +++ b/src/util/virnetdevveth.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 diff --git a/src/util/virnodesuspend.c b/src/util/virnodesuspend.c index 8088931..08c47aa 100644 --- a/src/util/virnodesuspend.c +++ b/src/util/virnodesuspend.c @@ -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) { diff --git a/src/util/virrandom.c b/src/util/virrandom.c index 3f17f5e..4301f3f 100644 --- a/src/util/virrandom.c +++ b/src/util/virrandom.c @@ -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), diff --git a/src/util/virthread.h b/src/util/virthread.h index eba8dc3..4b92a43 100644 --- a/src/util/virthread.h +++ b/src/util/virthread.h @@ -71,6 +71,11 @@ struct virOnceControl { }; +# define VIR_MUTEX_INITIALIZER \ + { \ + .lock = PTHREAD_MUTEX_INITIALIZER \ + } + # define VIR_ONCE_CONTROL_INITIALIZER \ { \ .once = PTHREAD_ONCE_INIT \ -- 1.9.0 -- libvir-list mailing list libvir-list@xxxxxxxxxx https://www.redhat.com/mailman/listinfo/libvir-list