\r
\r
#include "complib/cl_timer.h"\r
+ \r
\r
+#define CL_MAX_TIME 0xFFFFFFFF\r
\r
-static HANDLE\r
-__clear_timer_handle(\r
- IN cl_timer_t* const p_timer )\r
-{\r
- HANDLE timer;\r
-\r
- EnterCriticalSection( &p_timer->lock );\r
- timer = p_timer->h_timer;\r
- p_timer->h_timer = NULL;\r
- LeaveCriticalSection( &p_timer->lock );\r
-\r
- return timer;\r
-}\r
\r
+/*\r
+ * If timeout_time is 0, the timer has been stopped.\r
+ */\r
static void CALLBACK\r
__timer_callback( \r
IN cl_timer_t* const p_timer,\r
IN BOOLEAN timer_signalled )\r
{\r
+ uint64_t timeout;\r
UNUSED_PARAM( timer_signalled );\r
\r
- if( !__clear_timer_handle( p_timer ))\r
- return;\r
-\r
EnterCriticalSection( &p_timer->cb_lock );\r
- (p_timer->pfn_callback)( (void*)p_timer->context );\r
+ EnterCriticalSection( &p_timer->lock );\r
+ timeout = p_timer->timeout_time;\r
+ p_timer->timeout_time = 0;\r
+ LeaveCriticalSection( &p_timer->lock );\r
+\r
+ if( timeout )\r
+ (p_timer->pfn_callback)( (void*)p_timer->context );\r
LeaveCriticalSection( &p_timer->cb_lock );\r
}\r
\r
\r
p_timer->pfn_callback = pfn_callback;\r
p_timer->context = context;\r
- p_timer->h_timer_queue = CreateTimerQueue();\r
- if( !p_timer->h_timer_queue )\r
- return CL_ERROR;\r
-\r
InitializeCriticalSection( &p_timer->lock );\r
InitializeCriticalSection( &p_timer->cb_lock );\r
+ if( !CreateTimerQueueTimer( &p_timer->h_timer, NULL, __timer_callback,\r
+ p_timer, CL_MAX_TIME, CL_MAX_TIME, WT_EXECUTEINIOTHREAD ) )\r
+ return CL_ERROR;\r
return( CL_SUCCESS );\r
}\r
\r
{\r
CL_ASSERT( p_timer );\r
\r
- if( p_timer->h_timer_queue )\r
- {\r
- DeleteTimerQueueEx( p_timer->h_timer_queue, INVALID_HANDLE_VALUE );\r
- DeleteCriticalSection( &p_timer->lock );\r
- DeleteCriticalSection( &p_timer->cb_lock );\r
- }\r
+ DeleteTimerQueueTimer( NULL, p_timer->h_timer, INVALID_HANDLE_VALUE );\r
+ DeleteCriticalSection( &p_timer->lock );\r
+ DeleteCriticalSection( &p_timer->cb_lock );\r
}\r
\r
\r
CL_ASSERT( p_timer );\r
CL_ASSERT( p_timer->pfn_callback );\r
\r
- timeout = cl_get_time_stamp() + (((uint64_t)time_ms) * 1000);\r
-\r
EnterCriticalSection( &p_timer->lock );\r
- if ( !p_timer->h_timer || timeout < p_timer->timeout_time )\r
+ timeout = cl_get_time_stamp() + (((uint64_t)time_ms) * 1000);\r
+ if ( !p_timer->timeout_time || timeout < p_timer->timeout_time )\r
{\r
- if( p_timer->h_timer )\r
- DeleteTimerQueueTimer( p_timer->h_timer_queue, p_timer->h_timer, NULL );\r
-\r
- p_timer->timeout_time = timeout;\r
- if( !CreateTimerQueueTimer( &p_timer->h_timer, p_timer->h_timer_queue,\r
- __timer_callback, p_timer, time_ms, 0, WT_EXECUTEINIOTHREAD ) )\r
- {\r
- p_timer->h_timer = NULL;\r
+ if( ChangeTimerQueueTimer( NULL, p_timer->h_timer, time_ms, CL_MAX_TIME ) )\r
+ p_timer->timeout_time = timeout;\r
+ else\r
status = CL_ERROR;\r
- }\r
}\r
LeaveCriticalSection( &p_timer->lock );\r
return status;\r
}\r
\r
\r
+/*\r
+ * Acquire cb_lock to ensure that all callbacks have completed.\r
+ */\r
void\r
cl_timer_stop(\r
IN cl_timer_t* const p_timer )\r
{\r
- HANDLE timer;\r
-\r
CL_ASSERT( p_timer );\r
\r
- timer = __clear_timer_handle( p_timer );\r
-\r
- if( timer )\r
- DeleteTimerQueueTimer( p_timer->h_timer_queue, timer, INVALID_HANDLE_VALUE );\r
+ EnterCriticalSection( &p_timer->cb_lock );\r
+ EnterCriticalSection( &p_timer->lock );\r
+ p_timer->timeout_time = 0;\r
+ ChangeTimerQueueTimer( NULL, p_timer->h_timer, CL_MAX_TIME, CL_MAX_TIME );\r
+ LeaveCriticalSection( &p_timer->lock );\r
+ LeaveCriticalSection( &p_timer->cb_lock );\r
}\r
\r
\r