diff options
author | Arnaud Versini <arnaud.versini@libreoffice.org> | 2022-04-01 16:07:35 +0200 |
---|---|---|
committer | Noel Grandin <noel.grandin@collabora.co.uk> | 2022-04-10 12:08:22 +0200 |
commit | 583b2e6bdb5550ff435ce739dac4318bc4a650e0 (patch) | |
tree | 87d5fe5a291510d1aec359f384ce06c338ebea10 /sal | |
parent | c1285d4d033a13667a942145db77e3aa38d3f953 (diff) |
osl signalshared : use std::mutex instead of oslMutex.
It also avoids possible double signal init and deinit.
Change-Id: I4a24a011ee82d885e7a42abedea6a9fd96d3cab3
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/132434
Tested-by: Jenkins
Reviewed-by: Noel Grandin <noel.grandin@collabora.co.uk>
Diffstat (limited to 'sal')
-rw-r--r-- | sal/inc/signalshared.hxx | 3 | ||||
-rw-r--r-- | sal/osl/all/signalshared.cxx | 55 |
2 files changed, 18 insertions, 40 deletions
diff --git a/sal/inc/signalshared.hxx b/sal/inc/signalshared.hxx index fecedf52ff75..10bc0f57b974 100644 --- a/sal/inc/signalshared.hxx +++ b/sal/inc/signalshared.hxx @@ -21,7 +21,6 @@ #include <sal/config.h> #include <osl/signal.h> -#include <osl/mutex.h> struct oslSignalHandlerImpl { @@ -30,8 +29,6 @@ struct oslSignalHandlerImpl oslSignalHandlerImpl* pNext; }; -extern bool bInitSignal; - oslSignalAction callSignalHandler(oslSignalInfo* pInfo); // platform-specific functions that need to be implemented diff --git a/sal/osl/all/signalshared.cxx b/sal/osl/all/signalshared.cxx index 2fc72c478fcc..530413f2604c 100644 --- a/sal/osl/all/signalshared.cxx +++ b/sal/osl/all/signalshared.cxx @@ -24,28 +24,17 @@ #include <signalshared.hxx> #include <osl/diagnose.h> - -bool bInitSignal = false; +#include <mutex> namespace { oslSignalHandlerImpl* SignalList; -oslMutex SignalListMutex; - -bool initSignal() -{ - SignalListMutex = osl_createMutex(); - - return onInitSignal(); -} +bool bInitSignal = false; -bool deInitSignal() +std::mutex& getSignalMutex() { - bool bRet = onDeInitSignal(); - - osl_destroyMutex(SignalListMutex); - - return bRet; + static std::mutex aMutex; + return aMutex; } } @@ -70,24 +59,22 @@ oslSignalHandler SAL_CALL osl_addSignalHandler(oslSignalHandlerFunction handler, if (!handler) return nullptr; - if (!bInitSignal) - bInitSignal = initSignal(); - oslSignalHandlerImpl* pHandler = static_cast<oslSignalHandlerImpl*>(calloc(1, sizeof(oslSignalHandlerImpl))); + std::scoped_lock aGuard(getSignalMutex()); + + if (!bInitSignal) + bInitSignal = onInitSignal(); + if (pHandler) { pHandler->Handler = handler; pHandler->pData = pData; - osl_acquireMutex(SignalListMutex); - pHandler->pNext = SignalList; SignalList = pHandler; - osl_releaseMutex(SignalListMutex); - return pHandler; } @@ -96,10 +83,10 @@ oslSignalHandler SAL_CALL osl_addSignalHandler(oslSignalHandlerFunction handler, sal_Bool SAL_CALL osl_removeSignalHandler(oslSignalHandler handler) { - if (!bInitSignal) - bInitSignal = initSignal(); + std::scoped_lock aGuard(getSignalMutex()); - osl_acquireMutex(SignalListMutex); + if (!bInitSignal) + bInitSignal = onInitSignal(); oslSignalHandlerImpl* pHandler = SignalList; oslSignalHandlerImpl* pPrevious = nullptr; @@ -113,10 +100,8 @@ sal_Bool SAL_CALL osl_removeSignalHandler(oslSignalHandler handler) else SignalList = pHandler->pNext; - osl_releaseMutex(SignalListMutex); - - if (!SignalList) - bInitSignal = deInitSignal(); + if (SignalList == nullptr) + bInitSignal = onDeInitSignal(); free(pHandler); @@ -127,17 +112,15 @@ sal_Bool SAL_CALL osl_removeSignalHandler(oslSignalHandler handler) pHandler = pHandler->pNext; } - osl_releaseMutex(SignalListMutex); - return false; } oslSignalAction SAL_CALL osl_raiseSignal(sal_Int32 userSignal, void* userData) { - if (!bInitSignal) - bInitSignal = initSignal(); + std::scoped_lock aGuard(getSignalMutex()); - osl_acquireMutex(SignalListMutex); + if (!bInitSignal) + bInitSignal = onInitSignal(); oslSignalInfo info; info.Signal = osl_Signal_User; @@ -146,8 +129,6 @@ oslSignalAction SAL_CALL osl_raiseSignal(sal_Int32 userSignal, void* userData) oslSignalAction action = callSignalHandler(&info); - osl_releaseMutex(SignalListMutex); - return action; } |