drivers/timers_win32/timers_win32.c
changeset 571 6eddab0b7ca8
parent 556 8296acd119a9
child 575 18db803e593f
equal deleted inserted replaced
570:c18397a2b035 571:6eddab0b7ca8
    67 	CloseHandle(*Thread);
    67 	CloseHandle(*Thread);
    68 }
    68 }
    69 
    69 
    70 int TimerThreadLoop(void)
    70 int TimerThreadLoop(void)
    71 {
    71 {
    72 	int ret = 0;
       
    73 	EnterMutex();
    72 	EnterMutex();
    74 	// At first, TimeDispatch will call init_callback.
    73 	// At first, TimeDispatch will call init_callback.
    75 	SetAlarm(NULL, 0, init_callback, 0, 0);
    74 	SetAlarm(NULL, 0, init_callback, 0, 0);
    76 	LeaveMutex();
    75 	LeaveMutex();
    77 
    76 
    78 	while(!stop_timer && ret == WAIT_OBJECT_0)
    77 	while(!stop_timer)
    79 	{
    78 	{
    80 		ret = WaitForSingleObject(timer, INFINITE);
    79 		WaitForSingleObject(timer, INFINITE);
       
    80 		if(stop_timer)
       
    81 			break;
    81 		_ftime(&timebuffer);
    82 		_ftime(&timebuffer);
    82 		EnterMutex();
    83 		EnterMutex();
    83 		TimeDispatch();
    84 		TimeDispatch();
    84 		LeaveMutex();
    85 		LeaveMutex();
    85 	}
    86 	}
    91 	LARGE_INTEGER liDueTime;
    92 	LARGE_INTEGER liDueTime;
    92 	liDueTime.QuadPart = 0;
    93 	liDueTime.QuadPart = 0;
    93 
    94 
    94 	InitializeCriticalSection(&CanFestival_mutex);
    95 	InitializeCriticalSection(&CanFestival_mutex);
    95 
    96 
    96 	timer = CreateWaitableTimer(NULL, TRUE, NULL);
    97 	timer = CreateWaitableTimer(NULL, FALSE, NULL);
    97 	if(NULL == timer)
    98 	if(NULL == timer)
    98     {
    99     {
    99         printf("CreateWaitableTimer failed (%d)\n", GetLastError());
   100         printf("CreateWaitableTimer failed (%d)\n", GetLastError());
   100     }
   101     }
   101 
   102 
   113 	EnterMutex();
   114 	EnterMutex();
   114 	exitfunction(NULL,0);
   115 	exitfunction(NULL,0);
   115 	LeaveMutex();
   116 	LeaveMutex();
   116 
   117 
   117 	stop_timer = 1;
   118 	stop_timer = 1;
       
   119 	setTimer(0);
       
   120 	WaitForSingleObject(timer_thread, INFINITE);
   118 	CloseHandle(timer);
   121 	CloseHandle(timer);
   119 	WaitForSingleObject(timer_thread, INFINITE);
       
   120 	CloseHandle(timer_thread);
   122 	CloseHandle(timer_thread);
   121 }
   123 }
   122 
   124 
   123 void StartTimerLoop(TimerCallback_t _init_callback)
   125 void StartTimerLoop(TimerCallback_t _init_callback)
   124 {
   126 {
   130 
   132 
   131 /* Set the next alarm */
   133 /* Set the next alarm */
   132 void setTimer(TIMEVAL value)
   134 void setTimer(TIMEVAL value)
   133 {
   135 {
   134 	if(value == TIMEVAL_MAX)
   136 	if(value == TIMEVAL_MAX)
   135 	{
       
   136 		/* cancel timer */
       
   137 		CancelWaitableTimer(timer);
   137 		CancelWaitableTimer(timer);
   138 	}
       
   139 	else
   138 	else
   140 	{
   139 	{
   141 		LARGE_INTEGER liDueTime;
   140 		LARGE_INTEGER liDueTime;
   142 
   141 
   143 		/* arg 2 of SetWaitableTimer take 100 ns interval */
   142 		/* arg 2 of SetWaitableTimer take 100 ns interval */
   154 /* Get the elapsed time since the last occured alarm */
   153 /* Get the elapsed time since the last occured alarm */
   155 TIMEVAL getElapsedTime(void)
   154 TIMEVAL getElapsedTime(void)
   156 {
   155 {
   157 	struct _timeb timetmp;
   156 	struct _timeb timetmp;
   158 	_ftime(&timetmp);
   157 	_ftime(&timetmp);
   159 	return (timetmp.time - timebuffer.time) * 1000000 + (timetmp.millitm - timebuffer.millitm) * 1000;
   158 	return (timetmp.time - timebuffer.time) * 10000000 + (timetmp.millitm - timebuffer.millitm) * 10000;
   160 }
   159 }
   161 
   160