Skip to content

simple zero-dependency timer implementation

Notifications You must be signed in to change notification settings

stuxnet147/ZeroTimer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 

Repository files navigation

ZeroTimer

This is a simple zero-dependency timer implementation.

Limit

Only Windows works (Kernel Mode Support)

Usage

typedef struct _KSYSTEM_TIME2
{
	unsigned long LowPart;
	long High1Time;
	long High2Time;
} KSYSTEM_TIME2, * PKSYSTEM_TIME2;
union TICK_COUNT_UNION
{
	KSYSTEM_TIME2 TickCount;
	unsigned long long TickCountQuad;
};
volatile TICK_COUNT_UNION* TickCountPtr = reinterpret_cast<TICK_COUNT_UNION* volatile>(0x7FFE0320);
volatile unsigned int* volatile TickCountMultiplierPtr = reinterpret_cast<unsigned int* volatile>(0x7FFE0004);
unsigned int GetTickCount_()
{
	return TickCountPtr->TickCountQuad * static_cast<unsigned long long>(*TickCountMultiplierPtr) >> 24;
}
#define concat_2(a1, a2) a1##a2
#define concat_1(a1, a2) concat_2(a1, a2)
#define var(prefix) concat_1(prefix, __LINE__)
#define TIMER(max_millisecond) \
	[]{\
		static unsigned int var(TIME_VAR_) = 0; \
		if (var(TIME_VAR_) == 0) \
		{ \
			var(TIME_VAR_) = GetTickCount_(); \
		} \
		else if (GetTickCount_() - var(TIME_VAR_) >= max_millisecond) \
		{ \
			var(TIME_VAR_) = 0; \
			return true; \
		} \
		return false;\
	}()
int main()
{
	while(1)
	{
		if (TIMER(1000))
		{
			// Timer expired
		}
		else
		{
			// Timer running
		}
	}
	return 0;
}

Releases

No releases published

Packages

No packages published

Languages