RTI Connext Micro  Version 2.4.1.0
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups
osapi_timer.h
Go to the documentation of this file.
1 /*
2  * FILE: osapi_timer.h - Definition of Timer interface
3  *
4  * Copyright 2012-2014 Real-Time Innovations, Inc.
5  *
6  * No duplications, whole or partial, manual or electronic, may be made
7  * without express written permission. Any such copies, or
8  * revisions thereof, must display this notice unaltered.
9  * This code contains trade secrets of Real-Time Innovations, Inc.
10  *
11  * Modification History
12  * --------------------
13  * 22mar2012,tk Written
14  */
15 /*ce
16  * \file
17  * \brief Timer interface definition
18  * \defgroup OSAPI_TimerClass OSAPI Timer
19  * \ingroup OSAPIModule
20  *
21  * \details
22  */
23 #ifndef osapi_timer_h
24 #define osapi_timer_h
25 
26 
27 #ifndef osapi_dll_h
28 #include "osapi/osapi_dll.h"
29 #endif
30 #ifndef osapi_types_h
31 #include "osapi/osapi_types.h"
32 #endif
33 #ifndef osapi_thread_h
34 #include "osapi/osapi_thread.h"
35 #endif
36 #ifndef osapi_mutex_h
37 #include "osapi/osapi_mutex.h"
38 #endif
39 
40 #ifdef __cplusplus
41 extern "C"
42 {
43 #endif
44 
45 /*e \defgroup OSAPI_TimerClass OSAPI Timer
46  * \ingroup OSAPIModule
47 */
48 
49 /*e \file
50  \brief Timer API
51 */
52 
53 /*e \ingroup OSAPI_TimerClass
54  * \brief Create a periodic timer
55  */
56 #define OSAPI_TIMER_PERIODIC (0x1)
57 
58 /*e \ingroup OSAPI_TimerClass
59  * \brief Create a one-shot timer
60  */
61 #define OSAPI_TIMER_ONE_SHOT (0x0)
62 
63 /*e \ingroup OSAPI_TimerClass
64  * \brief Action taken by timer module when a timer callback returns.
65  */
66 typedef enum
67 {
68  OSAPI_TIMEOUT_OP_AUTOMATIC = 1,
69  OSAPI_TIMEOUT_OP_MANUAL
71 
72 /*e \ingroup OSAPI_TimerClass
73  * \brief User-data passed to timer-handler.
74  */
76 {
77  RTI_UINT32 count[2];
78  void *field[2];
79 };
80 
81 struct OSAPI_TimerEntry;
82 struct OSAPI_TimeoutHandle
83 {
84  /* In case an entry is re-used */
85  struct OSAPI_TimerEntry *_entry;
86 
87  /* In case an entry is re-used */
88  RTI_INT32 epoch;
89 };
90 
91 typedef struct OSAPI_TimeoutHandle OSAPI_TimeoutHandle_T;
92 
93 #define OSAPI_TimeoutHandle_INITIALIZER \
94 {\
95  NULL,\
96  -1\
97 }
98 
99 struct OSAPI_TimerProperty
100 {
101  RTI_INT32 max_entries;
102  RTI_INT32 max_slots;
103  struct OSAPI_ThreadProperty thread;
104 };
105 
106 #define OSAPI_TimerProperty_INITIALIZER \
107 {\
108  128,\
109  32,\
110  { /* OSAPI_ThreadProperty */ \
111  16*1024,\
112  OSAPI_THREAD_PRIORITY_NORMAL,\
113  OSAPI_THREAD_DEFAULT_OPTIONS,\
114  },\
115 }
116 
117 struct OSAPI_Timer;
118 typedef struct OSAPI_Timer *OSAPI_Timer_T;
119 
120 FUNCTION_MUST_TYPEDEF(
122 (*OSAPI_TimeoutFunction_T)(struct OSAPI_TimeoutUserData *user_data)
123 )
124 
125 typedef void
126 (*OSAPI_TimerTickHandlerFunction)(OSAPI_Timer_T timer);
127 
128 /*e \ingroup OSAPI_TimerClass
129  \brief Create a Timer.
130 
131  \details Create a new Timer. A Timer can manage multiple timeouts.
132 
133  Example:
134  \code
135  OSAPI_Timer_t my_timer;
136  struct OSAPI_TimerProperty timer_property = OSAPI_TimerProperty_INITIALIZER;
137  timer = OSAPI_Timer_new(&timer_property);
138  if (timer == NULL) {
139  return error;
140  }
141  \endcode
142 
143  The created Timer should be deleted with \ref OSAPI_Timer_delete.
144 
145  @param[in] property Timer property.
146 
147  @param[in] mutex Shared mutex.
148 
149  @mtsafety SAFE
150 */
151 MUST_CHECK_RETURN OSAPIDllExport OSAPI_Timer_T
152 OSAPI_Timer_new(struct OSAPI_TimerProperty *property,struct OSAPI_Mutex *mutex);
153 
154 /*e \ingroup OSAPI_TimerClass
155  \brief Delete a Timer.
156 
157  \details Delete a previously created Timer. All timeouts are cancelled.
158 
159  Example:
160  \code
161  OSAPI_Timer_t my_timer;
162 
163  ......
164 
165  OSAPI_Timer_delete(my_timer);
166  if (timer == NULL) {
167  return error;
168  }
169  \endcode
170 
171  The created Timer should be deleted with \ref OSAPI_Timer_delete.
172 
173  @param [in] timer Timer.
174 
175  @mtsafety SAFE
176 */
177 SHOULD_CHECK_RETURN OSAPIDllExport RTI_BOOL
178 OSAPI_Timer_delete(OSAPI_Timer_T timer);
179 
180 /*e \ingroup OSAPI_TimerClass
181  \brief Schedule a timeout.
182 
183  \details This function schedules a timeout with the specified period. The
184  timeout can either be rescheduled automatically or a new timeout must
185  be created.
186 
187  Example:
188  \code
189  OSAPI_Timer_t my_timer;
190  OSAPITimeoutHandle_t my_handle = OSAPITimeoutHandle_t_INITIALIZER;
191  RTI_BOOL result;
192  struct OSAPI_TimerEntryUserData user_data;
193 
194  result = OSAPI_Timer_create_timeout(my_timer,
195  &my_handle,
196  1000,
197  OSAPI_TIMER_PERIODIC,
198  &user_data);
199 
200  if (!result) {
201  report error;
202  }
203 
204  ......
205 
206  \endcode
207 
208  A timeout can cancelled with \ref OSAPI_Timer_delete_timeout or the timeout
209  can be modified with \ref OSAPI_Timer_update_timeout.
210 
211  @param[in] timer Timer object. Cannot be NULL.
212  @param[out] out_handle Reference to the timeout. Cannot be NULL.
213  @param[in] timeout Timeout in ms.
214  @param[in] flags Flags.
215  @param[in] timeout_handler Function to call at timeout. Cannot be NULL.
216  @param[in] user_data User data associated with the timeout. Can be NULL.
217 
218  @mtsafety SAFE
219 */
220 MUST_CHECK_RETURN OSAPIDllExport RTI_BOOL
221 OSAPI_Timer_create_timeout(OSAPI_Timer_T timer,
222  OSAPI_TimeoutHandle_T * out_handle,
223  RTI_INT32 timeout,
224  RTI_INT32 flags,
225  OSAPI_TimeoutFunction_T timeout_handler,
226  struct OSAPI_TimeoutUserData *user_data);
227 
228 /*e \ingroup OSAPI_TimerClass
229  \brief Reschedule a timeout.
230 
231  \details This function reschedules a previously scheduled timeout with
232  the specified period.
233 
234  Example:
235  \code
236  OSAPI_Timer_t my_timer;
237  OSAPITimeoutHandle_t my_handle = OSAPITimeoutHandle_t_INITIALIZER;
238  RTI_BOOL result;
239  struct OSAPI_TimerEntryUserData user_data;
240 
241  result = OSAPI_Timer_update_timeout(my_timer,
242  &my_handle,
243  2000);
244 
245  if (!result) {
246  report error;
247  }
248 
249  ......
250 
251  \endcode
252 
253  @param [in] timer Timer object. Cannot be NULL.
254  @param [out] out_handle Reference to the timeout. Cannot be NULL.
255  @param [in] timeout Timeout in ms.
256 
257  @mtsafety SAFE
258 */
259 MUST_CHECK_RETURN OSAPIDllExport RTI_BOOL
260 OSAPI_Timer_update_timeout(OSAPI_Timer_T timer,
261  OSAPI_TimeoutHandle_T *out_handle, RTI_INT32 timeout);
262 
263 /*e \ingroup OSAPI_TimerClass
264  \brief Stop a previously scheduled timeout
265 
266  \details This function stops a previously scheduled timeout.
267 
268  Example:
269  \code
270  OSAPI_Timer_t my_timer;
271  OSAPITimeoutHandle_t my_handle = OSAPITimeoutHandle_t_INITIALIZER;
272  RTI_BOOL result;
273 
274  ....
275 
276  result = OSAPI_Timer_stop_timer(my_timer,&my_handle);
277 
278  if (!result) {
279  report error;
280  }
281 
282  ......
283 
284  \endcode
285 
286  @param [in] timer Timer object. Cannot be NULL.
287  @param [in] handle Reference to the timeout. Cannot be NULL.
288 
289  @mtsafety SAFE
290 */
291 SHOULD_CHECK_RETURN OSAPIDllExport RTI_BOOL
292 OSAPI_Timer_delete_timeout(OSAPI_Timer_T timer, OSAPI_TimeoutHandle_T *handle);
293 
294 /*e \ingroup OSAPI_TimerClass
295  \brief Get the user_data from a handle.
296 
297  Example:
298  \code
299  OSAPI_Timer_t my_timer;
300  OSAPITimeoutHandle_t my_handle = OSAPITimeoutHandle_t_INITIALIZER;
301  RTI_BOOL result;
302  struct OSAPI_TimerEntryUserData user_data;
303 
304  ....
305 
306  result = OSAPITimeoutHandle_get_user_data(&user_data,&my_handle);
307 
308  if (!result) {
309  report error;
310  }
311 
312  ......
313 
314  \endcode
315 
316  @param [out] user_data User-data associated with handle. Cannot be NULL.
317  @param [in] handle Timer handle.
318 
319  @mtsafety SAFE
320 */
321 MUST_CHECK_RETURN OSAPIDllExport RTI_BOOL
323  OSAPI_TimeoutHandle_T *handle);
324 
325 #ifdef __cplusplus
326 } /* extern "C" */
327 #endif
328 
329 
330 #endif /* osapi_timer_h */

RTI Connext Micro Version 2.4.1.0 Copyright © Thu Nov 20 2014 Real-Time Innovations, Inc