diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-27 16:51:28 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-27 16:51:28 +0000 |
commit | 940b4d1848e8c70ab7642901a68594e8016caffc (patch) | |
tree | eb72f344ee6c3d9b80a7ecc079ea79e9fba8676d /salhelper/source | |
parent | Initial commit. (diff) | |
download | libreoffice-upstream.tar.xz libreoffice-upstream.zip |
Adding upstream version 1:7.0.4.upstream/1%7.0.4upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r-- | salhelper/source/condition.cxx | 131 | ||||
-rw-r--r-- | salhelper/source/dynload.cxx | 112 | ||||
-rw-r--r-- | salhelper/source/gcc3.map | 139 | ||||
-rw-r--r-- | salhelper/source/simplereferenceobject.cxx | 62 | ||||
-rw-r--r-- | salhelper/source/thread.cxx | 51 | ||||
-rw-r--r-- | salhelper/source/timer.cxx | 415 |
6 files changed, 910 insertions, 0 deletions
diff --git a/salhelper/source/condition.cxx b/salhelper/source/condition.cxx new file mode 100644 index 000000000..a83cc753e --- /dev/null +++ b/salhelper/source/condition.cxx @@ -0,0 +1,131 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + + +#include <salhelper/condition.hxx> +#include <osl/time.h> +#include <osl/mutex.hxx> + +using namespace salhelper; + + +/****************************************************************** + * * + * Condition * + * * + ******************************************************************/ + +Condition::Condition(osl::Mutex& aMutex) + : m_aMutex(aMutex), + m_aCondition() +{ +} + + +Condition::~Condition() +{ +} + + +/****************************************************************** + * * + * ConditionModifier * + * * + ******************************************************************/ + +ConditionModifier::ConditionModifier(Condition& aCond) + : m_aCond(aCond) +{ + m_aCond.m_aMutex.acquire(); +} + + +ConditionModifier::~ConditionModifier() +{ + if(m_aCond.applies()) + m_aCond.m_aCondition.set(); + + m_aCond.m_aMutex.release(); +} + + +/****************************************************************** + * * + * ConditionWaiter * + * * + ******************************************************************/ + +ConditionWaiter::timedout::timedout() {} + +ConditionWaiter::timedout::timedout(timedout const &) {} + +ConditionWaiter::timedout::~timedout() {} + +ConditionWaiter::timedout & +ConditionWaiter::timedout::operator =(timedout const &) { return *this; } + +ConditionWaiter::ConditionWaiter(Condition& aCond) + : m_aCond(aCond) +{ + while(true) { + m_aCond.m_aCondition.wait(); + m_aCond.m_aMutex.acquire(); + + if(m_aCond.applies()) + break; + else { + m_aCond.m_aCondition.reset(); + m_aCond.m_aMutex.release(); + } + } +} + + +ConditionWaiter::ConditionWaiter(Condition& aCond,sal_uInt32 milliSec) + : m_aCond(aCond) +{ + TimeValue aTime; + aTime.Seconds = milliSec / 1000; + aTime.Nanosec = 1000000 * ( milliSec % 1000 ); + + while(true) { + if( m_aCond.m_aCondition.wait(&aTime) == + osl::Condition::result_timeout ) + throw timedout(); + + m_aCond.m_aMutex.acquire(); + + if(m_aCond.applies()) + break; + else { + m_aCond.m_aCondition.reset(); + m_aCond.m_aMutex.release(); + } + } +} + + +ConditionWaiter::~ConditionWaiter() +{ + if(! m_aCond.applies()) + m_aCond.m_aCondition.reset(); + m_aCond.m_aMutex.release(); +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/salhelper/source/dynload.cxx b/salhelper/source/dynload.cxx new file mode 100644 index 000000000..d3b8e70fb --- /dev/null +++ b/salhelper/source/dynload.cxx @@ -0,0 +1,112 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include <salhelper/dynload.hxx> + +namespace salhelper +{ + +typedef void* (*ApiInitFunction) (); + +ORealDynamicLoader::ORealDynamicLoader(ORealDynamicLoader ** ppSetToZeroInDestructor_, + const OUString& moduleName, + const OUString& initFunction, + void* pApi, + oslModule pModule) + : m_pApi(pApi) + , m_refCount(1) + , m_pModule(pModule) + , m_strModuleName(moduleName) + , m_strInitFunction(initFunction) + , ppSetToZeroInDestructor( ppSetToZeroInDestructor_ ) +{ +} + +ORealDynamicLoader* ORealDynamicLoader::newInstance(ORealDynamicLoader ** ppSetToZeroInDestructor, + const OUString& moduleName, + const OUString& initFunction) +{ +#ifdef DISABLE_DYNLOADING + (void) ppSetToZeroInDestructor; + (void) moduleName; + (void) initFunction; + + return NULL; +#else + ApiInitFunction initFunc; + oslModule pModule = osl_loadModule(moduleName.pData, SAL_LOADMODULE_DEFAULT); + + if ( !pModule ) + { + return nullptr; + } + + initFunc = reinterpret_cast<ApiInitFunction>(osl_getFunctionSymbol( + pModule, initFunction.pData)); + + if ( !initFunc ) + { + osl_unloadModule(pModule); + return nullptr; + } + + return(new ORealDynamicLoader(ppSetToZeroInDestructor, moduleName, + initFunction, + initFunc(), + pModule)); +#endif +} + +ORealDynamicLoader::~ORealDynamicLoader() +{ + // set the address to zero + if( ppSetToZeroInDestructor ) + *ppSetToZeroInDestructor = nullptr; + + if (m_pModule) + { +#ifndef DISABLE_DYNLOADING + osl_unloadModule(m_pModule); +#endif + m_pModule = nullptr; + } +} + +sal_uInt32 ORealDynamicLoader::acquire() +{ + return ++m_refCount; +} + +sal_uInt32 ORealDynamicLoader::release() +{ + sal_uInt32 nRet = --m_refCount; + if( nRet == 0 ) + delete this; + return nRet; +} + + +void* ORealDynamicLoader::getApi() const +{ + return m_pApi; +} + +} // namespace salhelper + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/salhelper/source/gcc3.map b/salhelper/source/gcc3.map new file mode 100644 index 000000000..3d0d90d4a --- /dev/null +++ b/salhelper/source/gcc3.map @@ -0,0 +1,139 @@ +# +# This file is part of the LibreOffice project. +# +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. +# +# This file incorporates work covered by the following license notice: +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed +# with this work for additional information regarding copyright +# ownership. The ASF licenses this file to you under the Apache +# License, Version 2.0 (the "License"); you may not use this file +# except in compliance with the License. You may obtain a copy of +# the License at http://www.apache.org/licenses/LICENSE-2.0 . +# +UDK_3_0_0 { # should have been UDK_3.0 + global: + _ZTI*; _ZTS*; # weak RTTI symbols for C++ exceptions + + _ZN9salhelper18ORealDynamicLoader11newInstanceEPPS0_RKN3rtl8OUStringES6_; + _ZN9salhelper18ORealDynamicLoader7acquireEv; + _ZN9salhelper18ORealDynamicLoader7releaseEv; + _ZN9salhelper18ORealDynamicLoaderC1EPPS0_RKN3rtl8OUStringES6_PvS7_; + _ZN9salhelper18ORealDynamicLoaderC2EPPS0_RKN3rtl8OUStringES6_PvS7_; + _ZN9salhelper18ORealDynamicLoaderD0Ev; + _ZN9salhelper18ORealDynamicLoaderD1Ev; + _ZN9salhelper18ORealDynamicLoaderD2Ev; + _ZN9salhelper21SimpleReferenceObjectD0Ev; + _ZN9salhelper21SimpleReferenceObjectD1Ev; + _ZN9salhelper21SimpleReferenceObjectD2Ev; + _ZN9salhelper21SimpleReferenceObjectdlEPv; + + # Introducing a question mark at the end because of + # marginal type discrepancy there is a difference in the + # mangled name between Linux and macOS, see #i69351# + _ZN9salhelper21SimpleReferenceObjectnwE?; # salhelper::SimpleReferenceObject::operator new (std::size_t) + + _ZNK9salhelper18ORealDynamicLoader6getApiEv; + # _ZTIN9salhelper18ORealDynamicLoaderE; + # _ZTSN9salhelper18ORealDynamicLoaderE; + _ZTVN9salhelper18ORealDynamicLoaderE; + # _ZTIN9salhelper21SimpleReferenceObjectE; + # _ZTSN9salhelper21SimpleReferenceObjectE; + _ZTVN9salhelper21SimpleReferenceObjectE; + + local: + *; +}; + +UDK_3.1 { + global: + _ZN9salhelper21SimpleReferenceObjectdlEPvRKSt9nothrow_t; + + # Introducing a wildcard right in the middle because due to + # marginal type discrepancy there is a difference in the + # mangled name between Linux and macOS see #i69351# + _ZN9salhelper21SimpleReferenceObjectnwE?RKSt9nothrow_t; # salhelper::SimpleReferenceObject::operator new (std::size_t, std::nothrow_t const&) + + _ZN9salhelper9ConditionC1ERN3osl5MutexE; + _ZN9salhelper9ConditionC2ERN3osl5MutexE; + _ZN9salhelper9ConditionD0Ev; + _ZN9salhelper9ConditionD1Ev; + _ZN9salhelper9ConditionD2Ev; + # _ZTIN9salhelper9ConditionE; + # _ZTIS9salhelper9ConditionE; + + _ZN9salhelper17ConditionModifierC1ERNS_9ConditionE; + _ZN9salhelper17ConditionModifierC2ERNS_9ConditionE; + _ZN9salhelper17ConditionModifierD1Ev; + _ZN9salhelper17ConditionModifierD2Ev; + + _ZN9salhelper15ConditionWaiterC1ERNS_9ConditionE; + _ZN9salhelper15ConditionWaiterC1ERNS_9ConditionE?; + _ZN9salhelper15ConditionWaiterC2ERNS_9ConditionE; + _ZN9salhelper15ConditionWaiterC2ERNS_9ConditionE?; + _ZN9salhelper15ConditionWaiterD1Ev; + _ZN9salhelper15ConditionWaiterD2Ev; + + _ZN9salhelper15ConditionWaiter8timedoutaSERKS1_; + _ZN9salhelper15ConditionWaiter8timedoutC1ERKS1_; + _ZN9salhelper15ConditionWaiter8timedoutC1Ev; + _ZN9salhelper15ConditionWaiter8timedoutC2ERKS1_; + _ZN9salhelper15ConditionWaiter8timedoutC2Ev; + _ZN9salhelper15ConditionWaiter8timedoutD0Ev; + _ZN9salhelper15ConditionWaiter8timedoutD1Ev; + _ZN9salhelper15ConditionWaiter8timedoutD2Ev; + # _ZTIN9salhelper15ConditionWaiter8timedoutE; + # _ZTSN9salhelper15ConditionWaiter8timedoutE; + + + _ZN9salhelper5TimerC1ERKNS_10TTimeValueE; + _ZN9salhelper5TimerC1ERKNS_10TTimeValueES3_; + _ZN9salhelper5TimerC1Ev; + _ZN9salhelper5TimerC2ERKNS_10TTimeValueE; + _ZN9salhelper5TimerC2ERKNS_10TTimeValueES3_; + _ZN9salhelper5TimerC2Ev; + _ZN9salhelper5TimerD0Ev; + _ZN9salhelper5TimerD1Ev; + _ZN9salhelper5TimerD2Ev; + _ZN9salhelper5Timer5startEv; + _ZN9salhelper5Timer4stopEv; + _ZNK9salhelper5Timer9isTickingEv; + _ZNK9salhelper5Timer9isExpiredEv; + _ZNK9salhelper5Timer13expiresBeforeEPKS0_; + _ZN9salhelper5Timer15setAbsoluteTimeERKNS_10TTimeValueE; + _ZN9salhelper5Timer16setRemainingTimeERKNS_10TTimeValueE; + _ZN9salhelper5Timer16setRemainingTimeERKNS_10TTimeValueES3_; + _ZN9salhelper5Timer7addTimeERKNS_10TTimeValueE; + _ZNK9salhelper5Timer16getRemainingTimeEv; + + +} UDK_3_0_0; + +LIBO_UDK_3.6 { # symbols available in >= LibO 3.6 + global: + _ZN9salhelper6Thread12onTerminatedEv; + # salhelper::Thread::onTerminated() + _ZN9salhelper6Thread3runEv; # salhelper::Thread::run() + _ZN9salhelper6Thread6launchEv; # salhelper::Thread::launch() + _ZN9salhelper6ThreadC1EPKc; # salhelper::Thread::Thread(char const*) + _ZN9salhelper6ThreadC2EPKc; # salhelper::Thread::Thread(char const*) + _ZN9salhelper6ThreadD0Ev; # salhelper::Thread::~Thread() + _ZN9salhelper6ThreadD1Ev; # salhelper::Thread::~Thread() + _ZN9salhelper6ThreadD2Ev; # salhelper::Thread::~Thread() + _ZTVN9salhelper6ThreadE; # vtable for salhelper::Thread + _ZThn*_N9salhelper6Thread12onTerminatedEv; + # non-virtual thunk to salhelper::Thread::onTerminated() + _ZThn*_N9salhelper6Thread3runEv; + # non-virtual thunk to salhelper::Thread::run() +} UDK_3.1; + +# Unique libstdc++ symbols: +GLIBCXX_3.4 { + global: + _ZGVNSt7num_put*; _ZNSt7num_put*; + _ZNSs4_Rep20_S_empty_rep_storageE; +}; diff --git a/salhelper/source/simplereferenceobject.cxx b/salhelper/source/simplereferenceobject.cxx new file mode 100644 index 000000000..d57e7f0cd --- /dev/null +++ b/salhelper/source/simplereferenceobject.cxx @@ -0,0 +1,62 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include <salhelper/simplereferenceobject.hxx> + +#include <cassert> +#include <new> + +using salhelper::SimpleReferenceObject; + +SimpleReferenceObject::~SimpleReferenceObject() +{ + assert(m_nCount == 0); +} + +void *SimpleReferenceObject::operator new(std::size_t nSize) +{ + return ::operator new(nSize); +} + +void *SimpleReferenceObject::operator new(std::size_t nSize, + std::nothrow_t const &) +{ +#if defined(_WIN32) + return ::operator new(nSize); + // WNT lacks a global nothrow operator new... +#else // _WIN32 + return ::operator new(nSize, std::nothrow); +#endif // _WIN32 +} + +void SimpleReferenceObject::operator delete(void * pPtr) +{ + ::operator delete(pPtr); +} + +void SimpleReferenceObject::operator delete(void * pPtr, std::nothrow_t const &) +{ +#if defined(_WIN32) + ::operator delete(pPtr); // WNT lacks a global nothrow operator delete... +#else // _WIN32 + ::operator delete(pPtr, std::nothrow); +#endif // _WIN32 +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/salhelper/source/thread.cxx b/salhelper/source/thread.cxx new file mode 100644 index 000000000..1a06bf3b2 --- /dev/null +++ b/salhelper/source/thread.cxx @@ -0,0 +1,51 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#include <sal/config.h> + +#include <stdexcept> +#include <string> + +#include <sal/log.hxx> +#include <salhelper/thread.hxx> + +salhelper::Thread::Thread(char const * name): name_(name) {} + +void salhelper::Thread::launch() { + SAL_INFO("salhelper.thread", "launch " << name_); + // Assumption is that osl::Thread::create returns normally with a true + // return value iff it causes osl::Thread::run to start executing: + acquire(); + try { + if (!create()) { + throw std::runtime_error("osl::Thread::create failed"); + } + } catch (...) { + release(); + throw; + } +} + +salhelper::Thread::~Thread() {} + +void salhelper::Thread::run() { + try { + setName(name_); + execute(); + } catch (...) { + // Work around the problem that onTerminated is not called if run throws + // an exception: + onTerminated(); + throw; + } +} + +void salhelper::Thread::onTerminated() { release(); } + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/salhelper/source/timer.cxx b/salhelper/source/timer.cxx new file mode 100644 index 000000000..8101b2387 --- /dev/null +++ b/salhelper/source/timer.cxx @@ -0,0 +1,415 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ +#include <salhelper/timer.hxx> + +#include <osl/thread.hxx> +#include <osl/conditn.hxx> +#include <osl/mutex.hxx> +#include <rtl/instance.hxx> + +using namespace salhelper; + +class salhelper::TimerManager : public osl::Thread +{ +public: + TimerManager(); + + virtual ~TimerManager() override; + + /// register timer + void registerTimer(salhelper::Timer* pTimer); + + /// unregister timer + void unregisterTimer(salhelper::Timer const * pTimer); + + /// lookup timer + bool lookupTimer(const salhelper::Timer* pTimer); + + /// retrieves the "Singleton" TimerManager Instance + static TimerManager* getTimerManager(); + +protected: + /// worker-function of thread + virtual void SAL_CALL run() override; + + /// Checking and triggering of a timer event + void checkForTimeout(); + + /// cleanup Method + virtual void SAL_CALL onTerminated() override; + + /// sorted-queue data + salhelper::Timer* m_pHead; + /// List Protection + osl::Mutex m_Lock; + /// Signal the insertion of a timer + osl::Condition m_notEmpty; + + /// "Singleton Pattern" + static salhelper::TimerManager* m_pManager; + +}; + +Timer::Timer() + : m_aTimeOut(0), + m_aExpired(0), + m_aRepeatDelta(0), + m_pNext(nullptr) +{ +} + +Timer::Timer(const TTimeValue& rTime) + : m_aTimeOut(rTime), + m_aExpired(0), + m_aRepeatDelta(0), + m_pNext(nullptr) +{ +} + +Timer::Timer(const TTimeValue& rTime, const TTimeValue& Repeat) + : m_aTimeOut(rTime), + m_aExpired(0), + m_aRepeatDelta(Repeat), + m_pNext(nullptr) +{ +} + +Timer::~Timer() +{ + stop(); +} + +void Timer::start() +{ + if (!isTicking()) + { + if (!m_aTimeOut.isEmpty()) + setRemainingTime(m_aTimeOut); + + TimerManager *pManager = TimerManager::getTimerManager(); + + if (pManager) + pManager->registerTimer(this); + } +} + +void Timer::stop() +{ + TimerManager *pManager = TimerManager::getTimerManager(); + + if (pManager) + pManager->unregisterTimer(this); +} + +sal_Bool Timer::isTicking() const +{ + TimerManager *pManager = TimerManager::getTimerManager(); + + if (pManager) + return pManager->lookupTimer(this); + else + return false; +} + +sal_Bool Timer::isExpired() const +{ + TTimeValue Now; + + osl_getSystemTime(&Now); + + return !(Now < m_aExpired); +} + +sal_Bool Timer::expiresBefore(const Timer* pTimer) const +{ + if (pTimer) + return m_aExpired < pTimer->m_aExpired; + else + return false; +} + +void Timer::setAbsoluteTime(const TTimeValue& Time) +{ + m_aTimeOut = 0; + m_aExpired = Time; + m_aRepeatDelta = 0; + + m_aExpired.normalize(); +} + +void Timer::setRemainingTime(const TTimeValue& Remaining) +{ + osl_getSystemTime(&m_aExpired); + + m_aExpired.addTime(Remaining); +} + +void Timer::setRemainingTime(const TTimeValue& Remaining, const TTimeValue& Repeat) +{ + osl_getSystemTime(&m_aExpired); + + m_aExpired.addTime(Remaining); + + m_aRepeatDelta = Repeat; +} + +void Timer::addTime(const TTimeValue& Delta) +{ + m_aExpired.addTime(Delta); +} + +TTimeValue Timer::getRemainingTime() const +{ + TTimeValue Now; + + osl_getSystemTime(&Now); + + sal_Int32 secs = m_aExpired.Seconds - Now.Seconds; + + if (secs < 0) + return TTimeValue(0, 0); + + sal_Int32 nsecs = m_aExpired.Nanosec - Now.Nanosec; + + if (nsecs < 0) + { + if (secs > 0) + { + secs -= 1; + nsecs += 1000000000; + } + else + return TTimeValue(0, 0); + } + + return TTimeValue(secs, nsecs); +} + +namespace +{ + // Synchronize access to TimerManager + struct theTimerManagerMutex : public rtl::Static< osl::Mutex, theTimerManagerMutex> {}; +} + +TimerManager* salhelper::TimerManager::m_pManager = nullptr; + +/** The timer manager cleanup has been removed (no thread is killed anymore), + so the thread leaks. + + This will result in a GPF in case the salhelper-library gets unloaded before + process termination. + + @TODO : rewrite this file, so that the timerManager thread gets destroyed, + when there are no timers anymore ! +**/ + +TimerManager::TimerManager() +{ + osl::MutexGuard Guard(theTimerManagerMutex::get()); + + assert(m_pManager == nullptr); + + m_pManager = this; + m_pHead= nullptr; + m_notEmpty.reset(); + + // start thread + create(); +} + +TimerManager::~TimerManager() +{ + osl::MutexGuard Guard(theTimerManagerMutex::get()); + + if (m_pManager == this) + m_pManager = nullptr; +} + +void TimerManager::onTerminated() +{ + delete this; // FIXME +} + +TimerManager* TimerManager::getTimerManager() +{ + osl::MutexGuard Guard(theTimerManagerMutex::get()); + + if (! m_pManager) + new TimerManager; + + return m_pManager; +} + +void TimerManager::registerTimer(Timer* pTimer) +{ + if (!pTimer) + return; + + osl::MutexGuard Guard(m_Lock); + + // try to find one with equal or lower remaining time. + Timer** ppIter = &m_pHead; + + while (*ppIter) + { + if (pTimer->expiresBefore(*ppIter)) + { + // next element has higher remaining time, + // => insert new timer before + break; + } + ppIter= &((*ppIter)->m_pNext); + } + + // next element has higher remaining time, + // => insert new timer before + pTimer->m_pNext= *ppIter; + *ppIter = pTimer; + + + if (pTimer == m_pHead) + { + // it was inserted as new head + // signal it to TimerManager Thread + m_notEmpty.set(); + } +} + +void TimerManager::unregisterTimer(Timer const * pTimer) +{ + if (!pTimer) + return; + + // lock access + osl::MutexGuard Guard(m_Lock); + + Timer** ppIter = &m_pHead; + + while (*ppIter) + { + if (pTimer == (*ppIter)) + { + // remove timer from list + *ppIter = (*ppIter)->m_pNext; + return; + } + ppIter= &((*ppIter)->m_pNext); + } +} + +bool TimerManager::lookupTimer(const Timer* pTimer) +{ + if (!pTimer) + return false; + + // lock access + osl::MutexGuard Guard(m_Lock); + + // check the list + for (Timer* pIter = m_pHead; pIter != nullptr; pIter= pIter->m_pNext) + { + if (pIter == pTimer) + return true; + } + + return false; +} + +void TimerManager::checkForTimeout() +{ + m_Lock.acquire(); + + if (!m_pHead) + { + m_Lock.release(); + return; + } + + Timer* pTimer = m_pHead; + + if (pTimer->isExpired()) + { + // remove expired timer + m_pHead = pTimer->m_pNext; + + pTimer->acquire(); + + m_Lock.release(); + + pTimer->onShot(); + + // restart timer if specified + if (!pTimer->m_aRepeatDelta.isEmpty()) + { + TTimeValue Now; + + osl_getSystemTime(&Now); + + Now.Seconds += pTimer->m_aRepeatDelta.Seconds; + Now.Nanosec += pTimer->m_aRepeatDelta.Nanosec; + + pTimer->m_aExpired = Now; + + registerTimer(pTimer); + } + pTimer->release(); + } + else + { + m_Lock.release(); + } +} + +void TimerManager::run() +{ + osl_setThreadName("salhelper::TimerManager"); + + setPriority( osl_Thread_PriorityBelowNormal ); + + while (schedule()) + { + TTimeValue delay; + TTimeValue* pDelay=nullptr; + + m_Lock.acquire(); + + if (m_pHead != nullptr) + { + delay = m_pHead->getRemainingTime(); + pDelay=&delay; + } + else + { + pDelay=nullptr; + } + + + m_notEmpty.reset(); + + m_Lock.release(); + + + m_notEmpty.wait(pDelay); + + checkForTimeout(); + } + +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |