8
0
mirror of https://github.com/FirebirdSQL/firebird.git synced 2025-01-22 21:23:04 +01:00

Added simple ITimer implementation, independent of system time changes.

This commit is contained in:
hvlad 2020-04-30 12:14:41 +03:00
parent 2d7b33f98c
commit 5ad667c195
8 changed files with 269 additions and 7 deletions

View File

@ -46,6 +46,7 @@
<ClCompile Include="..\..\..\src\common\classes\Synchronize.cpp" />
<ClCompile Include="..\..\..\src\common\classes\SyncObject.cpp" />
<ClCompile Include="..\..\..\src\common\classes\TempFile.cpp" />
<ClCompile Include="..\..\..\src\common\classes\TimerImpl.cpp" />
<ClCompile Include="..\..\..\src\common\classes\timestamp.cpp" />
<ClCompile Include="..\..\..\src\common\classes\TomCryptHash.cpp" />
<ClCompile Include="..\..\..\src\common\classes\UserBlob.cpp" />
@ -88,6 +89,7 @@
<ClCompile Include="..\..\..\src\common\security.cpp" />
<ClCompile Include="..\..\..\src\common\sha.cpp" />
<ClCompile Include="..\..\..\src\common\sha2\sha2.cpp" />
<ClCompile Include="..\..\..\src\common\SimilarToRegex.cpp" />
<ClCompile Include="..\..\..\src\common\StatementMetadata.cpp" />
<ClCompile Include="..\..\..\src\common\StatusArg.cpp" />
<ClCompile Include="..\..\..\src\common\StatusHolder.cpp" />
@ -152,6 +154,7 @@
<ClInclude Include="..\..\..\src\common\classes\Synchronize.h" />
<ClInclude Include="..\..\..\src\common\classes\SyncObject.h" />
<ClInclude Include="..\..\..\src\common\classes\TempFile.h" />
<ClInclude Include="..\..\..\src\common\classes\TimerImpl.h" />
<ClInclude Include="..\..\..\src\common\classes\timestamp.h" />
<ClInclude Include="..\..\..\src\common\classes\tree.h" />
<ClInclude Include="..\..\..\src\common\classes\UserBlob.h" />
@ -199,6 +202,7 @@
<ClInclude Include="..\..\..\src\common\sdl_proto.h" />
<ClInclude Include="..\..\..\src\common\sha.h" />
<ClInclude Include="..\..\..\src\common\sha2\sha2.h" />
<ClInclude Include="..\..\..\src\common\SimilarToRegex.h" />
<ClInclude Include="..\..\..\src\common\SimpleStatusVector.h" />
<ClInclude Include="..\..\..\src\common\StatementMetadata.h" />
<ClInclude Include="..\..\..\src\common\StatusArg.h" />

View File

@ -228,12 +228,15 @@
<ClCompile Include="..\..\..\src\common\sha2\sha2.cpp">
<Filter>common</Filter>
</ClCompile>
<ClCompile Include="..\..\..\src\common\classes\zip.cpp">
<Filter>classes</Filter>
<ClCompile Include="..\..\..\src\common\SimilarToRegex.cpp">
<Filter>common</Filter>
</ClCompile>
<ClCompile Include="..\..\..\src\common\TimeZoneUtil.cpp">
<Filter>common</Filter>
</ClCompile>
<ClCompile Include="..\..\..\src\common\classes\zip.cpp">
<Filter>classes</Filter>
</ClCompile>
<ClCompile Include="..\..\..\src\common\classes\ParsedList.cpp">
<Filter>classes</Filter>
</ClCompile>
@ -243,6 +246,9 @@
<ClCompile Include="..\..\..\src\common\Int128.cpp">
<Filter>common</Filter>
</ClCompile>
<ClCompile Include="..\..\..\src\common\classes\TimerImpl.cpp">
<Filter>classes</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\..\src\common\xdr_proto.h">
@ -566,10 +572,7 @@
<ClInclude Include="..\..\..\src\common\sha2\sha2.h">
<Filter>headers</Filter>
</ClInclude>
<ClInclude Include="..\..\..\src\common\keywords.h">
<Filter>headers</Filter>
</ClInclude>
<ClInclude Include="..\..\..\src\common\classes\zip.h">
<ClInclude Include="..\..\..\src\common\SimilarToRegex.h">
<Filter>headers</Filter>
</ClInclude>
<ClInclude Include="..\..\..\src\common\TimeZones.h">
@ -578,11 +581,20 @@
<ClInclude Include="..\..\..\src\common\TimeZoneUtil.h">
<Filter>headers</Filter>
</ClInclude>
<ClInclude Include="..\..\..\src\common\classes\zip.h">
<Filter>headers</Filter>
</ClInclude>
<ClInclude Include="..\..\..\src\common\classes\ParsedList.h">
<Filter>headers</Filter>
</ClInclude>
<ClInclude Include="..\..\..\src\common\keywords.h">
<Filter>headers</Filter>
</ClInclude>
<ClInclude Include="..\..\..\src\common\Int128.h">
<Filter>headers</Filter>
</ClInclude>
<ClInclude Include="..\..\..\src\common\classes\TimerImpl.h">
<Filter>headers</Filter>
</ClInclude>
</ItemGroup>
</Project>

View File

@ -46,6 +46,7 @@
<ClCompile Include="..\..\..\src\common\classes\Synchronize.cpp" />
<ClCompile Include="..\..\..\src\common\classes\SyncObject.cpp" />
<ClCompile Include="..\..\..\src\common\classes\TempFile.cpp" />
<ClCompile Include="..\..\..\src\common\classes\TimerImpl.cpp" />
<ClCompile Include="..\..\..\src\common\classes\timestamp.cpp" />
<ClCompile Include="..\..\..\src\common\classes\TomCryptHash.cpp" />
<ClCompile Include="..\..\..\src\common\classes\UserBlob.cpp" />
@ -153,6 +154,7 @@
<ClInclude Include="..\..\..\src\common\classes\Synchronize.h" />
<ClInclude Include="..\..\..\src\common\classes\SyncObject.h" />
<ClInclude Include="..\..\..\src\common\classes\TempFile.h" />
<ClInclude Include="..\..\..\src\common\classes\TimerImpl.h" />
<ClInclude Include="..\..\..\src\common\classes\timestamp.h" />
<ClInclude Include="..\..\..\src\common\classes\tree.h" />
<ClInclude Include="..\..\..\src\common\classes\UserBlob.h" />

View File

@ -246,6 +246,9 @@
<ClCompile Include="..\..\..\src\common\Int128.cpp">
<Filter>common</Filter>
</ClCompile>
<ClCompile Include="..\..\..\src\common\classes\TimerImpl.cpp">
<Filter>classes</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\..\src\common\xdr_proto.h">
@ -590,5 +593,8 @@
<ClInclude Include="..\..\..\src\common\Int128.h">
<Filter>headers</Filter>
</ClInclude>
<ClInclude Include="..\..\..\src\common\classes\TimerImpl.h">
<Filter>headers</Filter>
</ClInclude>
</ItemGroup>
</Project>

View File

@ -46,6 +46,7 @@
<ClCompile Include="..\..\..\src\common\classes\Synchronize.cpp" />
<ClCompile Include="..\..\..\src\common\classes\SyncObject.cpp" />
<ClCompile Include="..\..\..\src\common\classes\TempFile.cpp" />
<ClCompile Include="..\..\..\src\common\classes\TimerImpl.cpp" />
<ClCompile Include="..\..\..\src\common\classes\timestamp.cpp" />
<ClCompile Include="..\..\..\src\common\classes\TomCryptHash.cpp" />
<ClCompile Include="..\..\..\src\common\classes\UserBlob.cpp" />
@ -153,6 +154,7 @@
<ClInclude Include="..\..\..\src\common\classes\Synchronize.h" />
<ClInclude Include="..\..\..\src\common\classes\SyncObject.h" />
<ClInclude Include="..\..\..\src\common\classes\TempFile.h" />
<ClInclude Include="..\..\..\src\common\classes\TimerImpl.h" />
<ClInclude Include="..\..\..\src\common\classes\timestamp.h" />
<ClInclude Include="..\..\..\src\common\classes\tree.h" />
<ClInclude Include="..\..\..\src\common\classes\UserBlob.h" />

View File

@ -246,6 +246,9 @@
<ClCompile Include="..\..\..\src\common\Int128.cpp">
<Filter>common</Filter>
</ClCompile>
<ClCompile Include="..\..\..\src\common\classes\TimerImpl.cpp">
<Filter>classes</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\..\src\common\xdr_proto.h">
@ -590,5 +593,8 @@
<ClInclude Include="..\..\..\src\common\Int128.h">
<Filter>headers</Filter>
</ClInclude>
<ClInclude Include="..\..\..\src\common\classes\TimerImpl.h">
<Filter>headers</Filter>
</ClInclude>
</ItemGroup>
</Project>

View File

@ -0,0 +1,124 @@
/*
* PROGRAM: Firebird interface.
* MODULE: TimerImpl.cpp
* DESCRIPTION: ITimer implementaton
*
* The contents of this file are subject to the Initial
* Developer's Public License Version 1.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.ibphoenix.com/main.nfs?a=ibphoenix&page=ibp_idpl.
*
* Software distributed under the License is distributed AS IS,
* WITHOUT WARRANTY OF ANY KIND, either express or implied.
* See the License for the specific language governing rights
* and limitations under the License.
*
* The Original Code was created by Khorsun Vladyslav
* for the Firebird Open Source RDBMS project.
*
* Copyright (c) 2020 Khorsun Vladyslav <hvlad@users.sourceforge.net>
* and all contributors signed below.
*
* All Rights Reserved.
* Contributor(s): ______________________________________.
*
*/
#include "../../common/classes/TimerImpl.h"
#include "../../common/StatusHolder.h"
#include "../../common/utils_proto.h"
namespace Firebird {
void TimerImpl::handler()
{
{
MutexLockGuard guard(m_mutex, FB_FUNCTION);
m_fireTime = 0;
if (!m_expTime) // Timer was reset to zero, do nothing
return;
// If timer was reset to fire later, restart ITimer
const SINT64 curTime = fb_utils::query_performance_counter() / fb_utils::query_performance_frequency();
if (m_expTime > curTime)
{
reset(m_expTime - curTime);
return;
}
m_expTime = 0;
}
onTimer(this);
}
int TimerImpl::release()
{
if (--refCounter == 0)
{
delete this;
return 0;
}
return 1;
}
void TimerImpl::reset(unsigned int timeout)
{
LocalStatus ls;
CheckStatusWrapper s(&ls);
ITimerControl* timerCtrl = TimerInterfacePtr();
MutexLockGuard guard(m_mutex, FB_FUNCTION);
// Start timer if necessary. If timer was already started, don't restart
// (or stop) it - handler() will take care about it.
if (!timeout)
{
m_expTime = 0;
return;
}
const SINT64 curTime = fb_utils::query_performance_counter() / fb_utils::query_performance_frequency();
m_expTime = curTime + timeout;
if (m_fireTime)
{
if (m_fireTime <= m_expTime)
return;
timerCtrl->stop(&s, this);
check(&s);
}
m_fireTime = m_expTime;
// new ITimer timeout, ms
SINT64 tout = (m_expTime - curTime) * (1000 * 1000);
timerCtrl->start(&s, this, tout);
check(&s);
}
void TimerImpl::stop()
{
MutexLockGuard guard(m_mutex, FB_FUNCTION);
if (!m_fireTime)
return;
m_fireTime = 0;
LocalStatus ls;
CheckStatusWrapper s(&ls);
ITimerControl* timerCtrl = TimerInterfacePtr();
timerCtrl->stop(&s, this);
check(&s);
}
} // namespace Firebird

View File

@ -0,0 +1,106 @@
/*
* PROGRAM: Firebird interface.
* MODULE: TimerImpl.h
* DESCRIPTION: ITimer implementaton
*
* The contents of this file are subject to the Initial
* Developer's Public License Version 1.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.ibphoenix.com/main.nfs?a=ibphoenix&page=ibp_idpl.
*
* Software distributed under the License is distributed AS IS,
* WITHOUT WARRANTY OF ANY KIND, either express or implied.
* See the License for the specific language governing rights
* and limitations under the License.
*
* The Original Code was created by Khorsun Vladyslav
* for the Firebird Open Source RDBMS project.
*
* Copyright (c) 2020 Khorsun Vladyslav <hvlad@users.sourceforge.net>
* and all contributors signed below.
*
* All Rights Reserved.
* Contributor(s): ______________________________________.
*
*/
#ifndef FB_CLASSES_TIMER_IMPL
#define FB_CLASSES_TIMER_IMPL
#include "../../common/classes/ImplementHelper.h"
#include "../../common/classes/locks.h"
namespace Firebird {
class TimerImpl :
public RefCntIface<ITimerImpl<TimerImpl, CheckStatusWrapper> >
{
public:
TimerImpl() :
m_fireTime(0),
m_expTime(0)
{ }
// ITimer implementation
void handler();
int release();
// Set timeout, seconds
void reset(unsigned int timeout);
void stop();
SINT64 getExpireClock() const
{
return m_expTime;
}
protected:
// Descendants must override it
virtual void onTimer(TimerImpl*) = 0;
private:
Mutex m_mutex;
SINT64 m_fireTime; // when ITimer will fire, could be less than m_expTime
SINT64 m_expTime; // when actual idle timeout will expire
};
// Call member function of some class T::Fn()
template <typename T, void (T::*Fn)(TimerImpl*)>
class TimerTmpl : public TimerImpl
{
public:
TimerTmpl(T* obj) : m_obj(obj) {}
protected:
void onTimer(TimerImpl* arg)
{
(m_obj->*Fn)(arg);
}
private:
T* m_obj;
};
// Call static function with argument - instance of some RefCounted class T
template <typename T, void (Fn)(TimerImpl*, T*)>
class TimerTmplRef : public TimerImpl
{
public:
TimerTmplRef(T* obj) : m_obj(obj) {}
protected:
void onTimer(TimerImpl* arg1)
{
Fn(arg1, m_obj);
}
private:
RefPtr<T> m_obj;
};
} // namespace Firebird
#endif