2 * Copyright (c) 2005 SilverStorm Technologies. All rights reserved.
\r
4 * This software is available to you under the OpenIB.org BSD license
\r
7 * Redistribution and use in source and binary forms, with or
\r
8 * without modification, are permitted provided that the following
\r
9 * conditions are met:
\r
11 * - Redistributions of source code must retain the above
\r
12 * copyright notice, this list of conditions and the following
\r
15 * - Redistributions in binary form must reproduce the above
\r
16 * copyright notice, this list of conditions and the following
\r
17 * disclaimer in the documentation and/or other materials
\r
18 * provided with the distribution.
\r
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
\r
21 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
\r
22 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
\r
23 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
\r
24 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
\r
25 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
26 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
\r
34 #include "complib/cl_timer.h"
\r
37 static void CALLBACK
\r
39 IN cl_timer_t* const p_timer,
\r
40 IN BOOLEAN timer_signalled )
\r
42 /* timer_signalled is always TRUE, and has no value. */
\r
43 CL_ASSERT( timer_signalled );
\r
44 UNUSED_PARAM( timer_signalled );
\r
46 p_timer->timeout_time = 0;
\r
47 p_timer->thread_id = GetCurrentThreadId();
\r
49 (p_timer->pfn_callback)( (void*)p_timer->context );
\r
51 p_timer->thread_id = 0;
\r
57 IN cl_timer_t* const p_timer )
\r
59 p_timer->h_timer = NULL;
\r
60 p_timer->timeout_time = 0;
\r
61 p_timer->thread_id = 0;
\r
68 IN cl_timer_t* const p_timer,
\r
69 IN cl_pfn_timer_callback_t pfn_callback,
\r
70 IN const void* const context )
\r
72 CL_ASSERT( p_timer );
\r
73 CL_ASSERT( pfn_callback );
\r
75 cl_timer_construct( p_timer );
\r
77 p_timer->pfn_callback = pfn_callback;
\r
78 p_timer->context = context;
\r
79 return( CL_SUCCESS );
\r
85 IN cl_timer_t* const p_timer )
\r
87 CL_ASSERT( p_timer );
\r
89 cl_timer_stop( p_timer );
\r
95 IN cl_timer_t* const p_timer,
\r
96 IN const uint32_t time_ms )
\r
98 CL_ASSERT( p_timer );
\r
100 cl_timer_stop( p_timer );
\r
102 p_timer->timeout_time = cl_get_time_stamp() + (((uint64_t)time_ms) * 1000);
\r
104 if( !CreateTimerQueueTimer( &p_timer->h_timer, NULL, __timer_callback,
\r
105 p_timer, time_ms, 0, WT_EXECUTEINIOTHREAD ) )
\r
107 return( CL_ERROR );
\r
110 return( CL_SUCCESS );
\r
116 IN cl_timer_t* const p_timer,
\r
117 IN const uint32_t time_ms )
\r
119 uint64_t timeout_time;
\r
121 CL_ASSERT( p_timer );
\r
122 CL_ASSERT( p_timer->pfn_callback );
\r
124 /* Calculate the timeout time in the timer object. */
\r
125 timeout_time = cl_get_time_stamp() + (((uint64_t)time_ms) * 1000);
\r
127 /* Only pull in the timeout time. */
\r
128 if( p_timer->timeout_time && p_timer->timeout_time < timeout_time )
\r
129 return( CL_SUCCESS );
\r
131 return cl_timer_start( p_timer, time_ms );
\r
137 IN cl_timer_t* const p_timer )
\r
139 CL_ASSERT( p_timer );
\r
141 if( p_timer->h_timer && p_timer->thread_id != GetCurrentThreadId() )
\r
143 /* Make sure we block until the timer is cancelled. */
\r
144 DeleteTimerQueueTimer( NULL, p_timer->h_timer, INVALID_HANDLE_VALUE );
\r
145 p_timer->h_timer = NULL;
\r
147 p_timer->timeout_time = 0;
\r
151 #define SEC_TO_MICRO 1000000ULL // s to µs conversion
\r
154 cl_get_time_stamp( void )
\r
156 LARGE_INTEGER tick_count, frequency;
\r
158 if( !QueryPerformanceFrequency( &frequency ) )
\r
161 if( !QueryPerformanceCounter( &tick_count ) )
\r
164 return( tick_count.QuadPart / (frequency.QuadPart / SEC_TO_MICRO) );
\r
168 cl_get_time_stamp_sec( void )
\r
170 return( (uint32_t)(cl_get_time_stamp() / SEC_TO_MICRO) );
\r