RTI Connext Micro  Version 2.4.4.0
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
osapi_timer.h
Go to the documentation of this file.
1 /*
2  * FILE: osapi_timer.h - Definition of Timer interface
3  *
4  * Copyright 2012-2015 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 #ifndef RTI_CERT
155 /*e \ingroup OSAPI_TimerClass
156  \brief Delete a Timer.
157 
158  \details Delete a previously created Timer. All timeouts are cancelled.
159 
160  Example:
161  \code
162  OSAPI_Timer_t my_timer;
163 
164  ......
165 
166  OSAPI_Timer_delete(my_timer);
167  if (timer == NULL) {
168  return error;
169  }
170  \endcode
171 
172  The created Timer should be deleted with \ref OSAPI_Timer_delete.
173 
174  @param [in] timer Timer.
175 
176  @mtsafety SAFE
177 */
178 SHOULD_CHECK_RETURN OSAPIDllExport RTI_BOOL
179 OSAPI_Timer_delete(OSAPI_Timer_T timer);
180 #endif /* !RTI_CERT */
181 
182 /*e \ingroup OSAPI_TimerClass
183  \brief Schedule a timeout.
184 
185  \details This function schedules a timeout with the specified period. The
186  timeout can either be rescheduled automatically or a new timeout must
187  be created.
188 
189  Example:
190  \code
191  OSAPI_Timer_t my_timer;
192  OSAPITimeoutHandle_t my_handle = OSAPITimeoutHandle_t_INITIALIZER;
193  RTI_BOOL result;
194  struct OSAPI_TimerEntryUserData user_data;
195 
196  result = OSAPI_Timer_create_timeout(my_timer,
197  &my_handle,
198  1000,
199  OSAPI_TIMER_PERIODIC,
200  &user_data);
201 
202  if (!result) {
203  report error;
204  }
205 
206  ......
207 
208  \endcode
209 
210  A timeout can cancelled with \ref OSAPI_Timer_delete_timeout or the timeout
211  can be modified with \ref OSAPI_Timer_update_timeout.
212 
213  @param[in] timer Timer object. Cannot be NULL.
214  @param[out] out_handle Reference to the timeout. Cannot be NULL.
215  @param[in] timeout Timeout in ms.
216  @param[in] flags Flags.
217  @param[in] timeout_handler Function to call at timeout. Cannot be NULL.
218  @param[in] user_data User data associated with the timeout. Can be NULL.
219 
220  @mtsafety SAFE
221 */
222 MUST_CHECK_RETURN OSAPIDllExport RTI_BOOL
223 OSAPI_Timer_create_timeout(OSAPI_Timer_T timer,
224  OSAPI_TimeoutHandle_T * out_handle,
225  RTI_INT32 timeout,
226  RTI_INT32 flags,
227  OSAPI_TimeoutFunction_T timeout_handler,
228  struct OSAPI_TimeoutUserData *user_data);
229 
230 /*e \ingroup OSAPI_TimerClass
231  \brief Reschedule a timeout.
232 
233  \details This function reschedules a previously scheduled timeout with
234  the specified period.
235 
236  Example:
237  \code
238  OSAPI_Timer_t my_timer;
239  OSAPITimeoutHandle_t my_handle = OSAPITimeoutHandle_t_INITIALIZER;
240  RTI_BOOL result;
241  struct OSAPI_TimerEntryUserData user_data;
242 
243  result = OSAPI_Timer_update_timeout(my_timer,
244  &my_handle,
245  2000);
246 
247  if (!result) {
248  report error;
249  }
250 
251  ......
252 
253  \endcode
254 
255  @param [in] timer Timer object. Cannot be NULL.
256  @param [out] out_handle Reference to the timeout. Cannot be NULL.
257  @param [in] timeout Timeout in ms.
258 
259  @mtsafety SAFE
260 */
261 MUST_CHECK_RETURN OSAPIDllExport RTI_BOOL
262 OSAPI_Timer_update_timeout(OSAPI_Timer_T timer,
263  OSAPI_TimeoutHandle_T *out_handle, RTI_INT32 timeout);
264 
265 /*e \ingroup OSAPI_TimerClass
266  \brief Stop a previously scheduled timeout
267 
268  \details This function stops a previously scheduled timeout.
269 
270  Example:
271  \code
272  OSAPI_Timer_t my_timer;
273  OSAPITimeoutHandle_t my_handle = OSAPITimeoutHandle_t_INITIALIZER;
274  RTI_BOOL result;
275 
276  ....
277 
278  result = OSAPI_Timer_stop_timer(my_timer,&my_handle);
279 
280  if (!result) {
281  report error;
282  }
283 
284  ......
285 
286  \endcode
287 
288  @param [in] timer Timer object. Cannot be NULL.
289  @param [in] handle Reference to the timeout. Cannot be NULL.
290 
291  @mtsafety SAFE
292 */
293 SHOULD_CHECK_RETURN OSAPIDllExport RTI_BOOL
294 OSAPI_Timer_delete_timeout(OSAPI_Timer_T timer, OSAPI_TimeoutHandle_T *handle);
295 
296 /*e \ingroup OSAPI_TimerClass
297  \brief Get the user_data from a handle.
298 
299  Example:
300  \code
301  OSAPI_Timer_t my_timer;
302  OSAPITimeoutHandle_t my_handle = OSAPITimeoutHandle_t_INITIALIZER;
303  RTI_BOOL result;
304  struct OSAPI_TimerEntryUserData user_data;
305 
306  ....
307 
308  result = OSAPITimeoutHandle_get_user_data(&user_data,&my_handle);
309 
310  if (!result) {
311  report error;
312  }
313 
314  ......
315 
316  \endcode
317 
318  @param [out] user_data User-data associated with handle. Cannot be NULL.
319  @param [in] handle Timer handle.
320 
321  @mtsafety SAFE
322 */
323 MUST_CHECK_RETURN OSAPIDllExport RTI_BOOL
325  OSAPI_TimeoutHandle_T *handle);
326 
327 #ifdef __cplusplus
328 } /* extern "C" */
329 #endif
330 
331 
332 #endif /* osapi_timer_h */

RTI Connext Micro Version 2.4.4.0 Copyright © Thu Apr 30 2015 Real-Time Innovations, Inc