fix method to exit properly the Timer Loop
authorgreg
Fri, 17 Jul 2009 20:30:38 +0200
changeset 571 6eddab0b7ca8
parent 570 c18397a2b035
child 572 1dde213b1ca6
fix method to exit properly the Timer Loop
fix time unit
drivers/timers_win32/timers_win32.c
--- a/drivers/timers_win32/timers_win32.c	Fri Jul 17 20:26:22 2009 +0200
+++ b/drivers/timers_win32/timers_win32.c	Fri Jul 17 20:30:38 2009 +0200
@@ -69,15 +69,16 @@
 
 int TimerThreadLoop(void)
 {
-	int ret = 0;
 	EnterMutex();
 	// At first, TimeDispatch will call init_callback.
 	SetAlarm(NULL, 0, init_callback, 0, 0);
 	LeaveMutex();
 
-	while(!stop_timer && ret == WAIT_OBJECT_0)
+	while(!stop_timer)
 	{
-		ret = WaitForSingleObject(timer, INFINITE);
+		WaitForSingleObject(timer, INFINITE);
+		if(stop_timer)
+			break;
 		_ftime(&timebuffer);
 		EnterMutex();
 		TimeDispatch();
@@ -93,7 +94,7 @@
 
 	InitializeCriticalSection(&CanFestival_mutex);
 
-	timer = CreateWaitableTimer(NULL, TRUE, NULL);
+	timer = CreateWaitableTimer(NULL, FALSE, NULL);
 	if(NULL == timer)
     {
         printf("CreateWaitableTimer failed (%d)\n", GetLastError());
@@ -115,8 +116,9 @@
 	LeaveMutex();
 
 	stop_timer = 1;
+	setTimer(0);
+	WaitForSingleObject(timer_thread, INFINITE);
 	CloseHandle(timer);
-	WaitForSingleObject(timer_thread, INFINITE);
 	CloseHandle(timer_thread);
 }
 
@@ -132,10 +134,7 @@
 void setTimer(TIMEVAL value)
 {
 	if(value == TIMEVAL_MAX)
-	{
-		/* cancel timer */
 		CancelWaitableTimer(timer);
-	}
 	else
 	{
 		LARGE_INTEGER liDueTime;
@@ -156,6 +155,6 @@
 {
 	struct _timeb timetmp;
 	_ftime(&timetmp);
-	return (timetmp.time - timebuffer.time) * 1000000 + (timetmp.millitm - timebuffer.millitm) * 1000;
+	return (timetmp.time - timebuffer.time) * 10000000 + (timetmp.millitm - timebuffer.millitm) * 10000;
 }