src/Mayaqua/Tick64.c
// Real-time clock measuring thread

Real-time clock measuring thread
Real - time clock measuring thread
void Tick64Thread(THREAD *thread, void *param)
{
	UINT n = 0;
	bool first = false;
	bool create_first_entry = true;
	UINT tick_span;
	if (thread == NULL)
	{
		return;
	}
#ifdef	OS_WIN32
	MsSetThreadPriorityRealtime();
	tick_span = TICK64_SPAN_WIN32;
#else	
	UnixSetThreadPriorityRealtime();
	tick_span = TICK64_SPAN;
#endif	
	while (true)
	{
		UINT tick;
		UINT64 tick64;
#ifndef	OS_WIN32
		tick = TickRealtime();		
		if (tk64->LastTick > tick)
		{
			if ((tk64->LastTick - tick) >= (UINT64)0x0fffffff)
			{
				tk64->RoundCount++;
			}
			else
			{
				tick = tk64->LastTick;
			}
		}
		tk64->LastTick = tick;
		tick64 = (UINT64)tk64->RoundCount * (UINT64)4294967296LL + (UINT64)tick;
		Lock(tk64->TickLock);
		{
			if (tk64->TickStart == 0)
			{
				tk64->TickStart = tick64;
			}
			tick64 = tk64->Tick = tick64 - tk64->TickStart + (UINT64)1;
		}
		Unlock(tk64->TickLock);
#else	
		tick64 = Win32FastTick64();
		tick = (UINT)tick64;
#endif	
		if (create_first_entry)
		{
			ADJUST_TIME *t = ZeroMalloc(sizeof(ADJUST_TIME));
			t->Tick = tick64;
			t->Time = SystemTime64();
			tk64->Tick64WithTime64 = tick64;
			tk64->Time64 = t->Time;
			Add(tk64->AdjustTime, t);
			NoticeThreadInit(thread);
			create_first_entry = false;
		}
		n += tick_span;
		if (n >= 1000 || first == false)
		{
			UINT64 now = SystemTime64();
			if (now < tk64->Time64 ||
				Diff64((now - tk64->Time64) + tk64->Tick64WithTime64, tick64) >= tick_span)
			{
				ADJUST_TIME *t = ZeroMalloc(sizeof(ADJUST_TIME));
				LockList(tk64->AdjustTime);
				{
					t->Tick = tick64;
					t->Time = now;
					Add(tk64->AdjustTime, t);
					Debug("Adjust Time: Tick = %I64u, Time = %I64u\n",
						t->Tick, t->Time);
					if (LIST_NUM(tk64->AdjustTime) > MAX_ADJUST_TIME)
					{
						ADJUST_TIME *t2 = LIST_DATA(tk64->AdjustTime, 1);
						Delete(tk64->AdjustTime, t2);
						Debug("NUM_ADJUST TIME: %u\n", LIST_NUM(tk64->AdjustTime));
						Free(t2);
					}
				}
				UnlockList(tk64->AdjustTime);
				tk64->Time64 = now;
				tk64->Tick64WithTime64 = tick64;
			}
			first = true;
			n = 0;
		}
		if (tk64->Halt)
		{
			break;
		}
#ifdef	OS_WIN32
		Wait(halt_tick_event, tick_span);
#else	
		SleepThread(tick_span);
#endif	
	}
}
void Tick64Thread ( THREAD * thread , void * param ) { UINT n = 0 ; bool first = false ; bool create_first_entry = true ; UINT tick_span ; if ( thread == NULL ) { return ; } #ifdef OS_WIN32 MsSetThreadPriorityRealtime ( ) ; tick_span = TICK64_SPAN_WIN32 ; #else UnixSetThreadPriorityRealtime ( ) ; tick_span = TICK64_SPAN ; #endif while ( true ) { UINT tick ; UINT64 tick64 ; #ifndef OS_WIN32 tick = TickRealtime ( ) ; if ( tk64 -> LastTick > tick ) { if ( ( tk64 -> LastTick - tick ) >= ( UINT64 ) 0x0fffffff ) { tk64 -> RoundCount ++ ; } else { tick = tk64 -> LastTick ; } } tk64 -> LastTick = tick ; tick64 = ( UINT64 ) tk64 -> RoundCount * ( UINT64 ) 4294967296LL + ( UINT64 ) tick ; Lock ( tk64 -> TickLock ) ; { if ( tk64 -> TickStart == 0 ) { tk64 -> TickStart = tick64 ; } tick64 = tk64 -> Tick = tick64 - tk64 -> TickStart + ( UINT64 ) 1 ; } Unlock ( tk64 -> TickLock ) ; #else tick64 = Win32FastTick64 ( ) ; tick = ( UINT ) tick64 ; #endif if ( create_first_entry ) { ADJUST_TIME * t = ZeroMalloc ( sizeof ( ADJUST_TIME ) ) ; t -> Tick = tick64 ; t -> Time = SystemTime64 ( ) ; tk64 -> Tick64WithTime64 = tick64 ; tk64 -> Time64 = t -> Time ; Add ( tk64 -> AdjustTime , t ) ; NoticeThreadInit ( thread ) ; create_first_entry = false ; } n += tick_span ; if ( n >= 1000 || first == false ) { UINT64 now = SystemTime64 ( ) ; if ( now < tk64 -> Time64 || Diff64 ( ( now - tk64 -> Time64 ) + tk64 -> Tick64WithTime64 , tick64 ) >= tick_span ) { ADJUST_TIME * t = ZeroMalloc ( sizeof ( ADJUST_TIME ) ) ; LockList ( tk64 -> AdjustTime ) ; { t -> Tick = tick64 ; t -> Time = now ; Add ( tk64 -> AdjustTime , t ) ; Debug ( " \n " , t -> Tick , t -> Time ) ; if ( LIST_NUM ( tk64 -> AdjustTime ) > MAX_ADJUST_TIME ) { ADJUST_TIME * t2 = LIST_DATA ( tk64 -> AdjustTime , 1 ) ; Delete ( tk64 -> AdjustTime , t2 ) ; Debug ( " \n " , LIST_NUM ( tk64 -> AdjustTime ) ) ; Free ( t2 ) ; } } UnlockList ( tk64 -> AdjustTime ) ; tk64 -> Time64 = now ; tk64 -> Tick64WithTime64 = tick64 ; } first = true ; n = 0 ; } if ( tk64 -> Halt ) { break ; } #ifdef OS_WIN32 Wait ( halt_tick_event , tick_span ) ; #else SleepThread ( tick_span ) ; #endif } }
Real-time clock measuring thread
Real - time clock measuring thread
// Validate arguments
 // Raise the priority of the Win32 thread
 // OS_WIN32
 // Raise the priority of a POSIX threads
 // OS_WIN32
 // Get the current system clock
 // The Tick has gone lap around
 // tick skewed (System administrator might change hardware clock)
 // Normally, the clock skew appears as sub-seconds error
 // OS_WIN32
 // OS_WIN32
 // Notify the completion of the initialization 
 // Time correction
 // To prevent consuming memory infinite on a system that clock is skewd
 // Remove the second
 // OS_WIN32
 // OS_WIN32
