RTI Connext DDS Micro  Version 2.4.11
 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  *
19  * \details
20  * The Timer API provides a platform independent set of function to start
21  * timers and be notified when the timer expires. The concrete implementation
22  * of the platform dependent timer API are located in the platform specific
23  * files, such as vxTimer.c
24  *
25  * \defgroup OSAPI_TimerClass OSAPI Timer
26  * \ingroup OSAPIModule
27  *
28  * \details
29  */
30 #ifndef osapi_timer_h
31 #define osapi_timer_h
32 
33 
34 #ifndef osapi_dll_h
35 #include "osapi/osapi_dll.h"
36 #endif
37 #ifndef osapi_types_h
38 #include "osapi/osapi_types.h"
39 #endif
40 #ifndef osapi_thread_h
41 #include "osapi/osapi_thread.h"
42 #endif
43 #ifndef osapi_mutex_h
44 #include "osapi/osapi_mutex.h"
45 #endif
46 
47 #ifdef __cplusplus
48 extern "C"
49 {
50 #endif
51 
52 /*e \defgroup OSAPI_TimerClass OSAPI Timer
53  * \ingroup OSAPIModule
54 */
55 
56 /*e \file
57  \brief Timer API
58 */
59 
60 /*e \ingroup OSAPI_TimerClass
61  * \brief Create a periodic timer
62  */
63 #define OSAPI_TIMER_PERIODIC (0x1)
64 
65 /*e \ingroup OSAPI_TimerClass
66  * \brief Create a one-shot timer
67  */
68 #define OSAPI_TIMER_ONE_SHOT (0x0)
69 
70 /*e \ingroup OSAPI_TimerClass
71  * \brief Action taken by timer module when a timer callback returns.
72  */
73 typedef enum
74 {
75  OSAPI_TIMEOUT_OP_AUTOMATIC = 1,
76  OSAPI_TIMEOUT_OP_MANUAL
78 
79 /*e \ingroup OSAPI_TimerClass
80  * \brief User-data passed to timer-handler.
81  */
83 {
84  RTI_UINT32 count[2];
85  void *field[2];
86 };
87 
88 #define OSAPI_TimeoutUserData_INITIALIZER \
89 { \
90  {0,0},\
91  {NULL,NULL}\
92 }
93 
94 struct OSAPI_TimerEntry;
95 struct OSAPI_TimeoutHandle
96 {
97  /* In case an entry is re-used */
98  struct OSAPI_TimerEntry *_entry;
99 
100  /* In case an entry is re-used */
101  RTI_INT32 epoch;
102 };
103 
104 typedef struct OSAPI_TimeoutHandle OSAPI_TimeoutHandle_T;
105 
106 #define OSAPI_TimeoutHandle_INITIALIZER \
107 {\
108  NULL,\
109  -1\
110 }
111 
112 /*e \ingroup OSAPI_TimerClass
113  * \brief Timer properies
114  */
116 {
117  /*ci
118  * \brief The maximum number of timeouts
119  */
120  RTI_INT32 max_entries;
121 
122  /*ci
123  * \brief The maximum number slots in one round
124  */
125  RTI_INT32 max_slots;
126 
127  /*ci
128  * \brief The thread settings for the timer thread, if a thread is created
129  */
130  struct OSAPI_ThreadProperty thread;
131 };
132 
133 /*ci
134  * \brief Initializer for the \ref OSAPI_TimerProperty
135  */
136 #define OSAPI_TimerProperty_INITIALIZER \
137 {\
138  128,\
139  32,\
140  { /* OSAPI_ThreadProperty */ \
141  OSAPI_THREAD_USE_OSDEFAULT_STACKSIZE,\
142  OSAPI_THREAD_PRIORITY_NORMAL,\
143  OSAPI_THREAD_DEFAULT_OPTIONS,\
144  },\
145 }
146 
147 struct OSAPI_Timer;
148 typedef struct OSAPI_Timer *OSAPI_Timer_T;
149 
150 FUNCTION_MUST_TYPEDEF(
152 (*OSAPI_TimeoutFunction_T)(struct OSAPI_TimeoutUserData *user_data)
153 )
154 
155 typedef void
156 (*OSAPI_TimerTickHandlerFunction)(OSAPI_Timer_T timer);
157 
158 /*e \ingroup OSAPI_TimerClass
159  * \brief Create a Timer.
160  *
161  * \details Create a new Timer. A Timer can manage multiple timeouts.
162  *
163  * Example:
164  * \code
165  * OSAPI_Timer_t my_timer;
166  * struct OSAPI_TimerProperty timer_property = OSAPI_TimerProperty_INITIALIZER;
167  * timer = OSAPI_Timer_new(&timer_property);
168  * if (timer == NULL) {
169  * return error;
170  * }
171  * \endcode
172  *
173  * The created Timer should be deleted with \ref OSAPI_Timer_delete.
174  *
175  * @param[in] property Timer property.
176  * @param[in] mutex Shared mutex.
177  *
178  * @return New timer on success, NULL on failure
179  */
180 MUST_CHECK_RETURN OSAPIDllExport OSAPI_Timer_T
181 OSAPI_Timer_new(struct OSAPI_TimerProperty *property,struct OSAPI_Mutex *mutex);
182 
183 #ifndef RTI_CERT
184 /*e \ingroup OSAPI_TimerClass
185  * \brief Delete a Timer.
186  *
187  * \details Delete a previously created Timer. All timeouts are cancelled.
188  *
189  * Example:
190  * \code
191  * OSAPI_Timer_t my_timer;
192  *
193  * ......
194  *
195  * OSAPI_Timer_delete(my_timer);
196  * if (timer == NULL) {
197  * return error;
198  * }
199  * \endcode
200  *
201  * The created Timer should be deleted with \ref OSAPI_Timer_delete.
202  *
203  * @param [in] timer Timer.
204  *
205  * \return RTI_TRUE on success, RTI_FALSE on failure
206  *
207  * @mtsafety SAFE
208  */
209 SHOULD_CHECK_RETURN OSAPIDllExport RTI_BOOL
210 OSAPI_Timer_delete(OSAPI_Timer_T timer);
211 #endif /* !RTI_CERT */
212 
213 /*e \ingroup OSAPI_TimerClass
214  * \brief Schedule a timeout.
215  *
216  * \details This function schedules a timeout with the specified period. The
217  * timeout can either be rescheduled automatically or a new timeout must
218  * be created.
219  *
220  * Example:
221  * \code
222  * OSAPI_Timer_t my_timer;
223  * OSAPITimeoutHandle_t my_handle = OSAPITimeoutHandle_t_INITIALIZER;
224  * RTI_BOOL result;
225  * struct OSAPI_TimerEntryUserData user_data;
226  *
227  * result = OSAPI_Timer_create_timeout(my_timer,
228  * &my_handle,
229  * 1000,0,
230  * OSAPI_TIMER_PERIODIC,
231  * &user_data);
232  *
233  * if (!result) {
234  * report error;
235  * }
236  *
237  * ......
238  *
239  * \endcode
240  *
241  * A timeout can cancelled with \ref OSAPI_Timer_delete_timeout or the timeout
242  * can be modified with \ref OSAPI_Timer_update_timeout.
243  *
244  * @param[in] timer Timer object. Cannot be NULL.
245  * @param[out] out_handle Reference to the timeout. Cannot be NULL.
246  * @param[in] timeout_sec The number of seconds before timeout
247  * @param[in] timeout_nsec Additional number of nanoseconds before timeout
248  * @param[in] flags Flags OSAPI_TIMER_PERIODIC or OSAPI_TIMER_ONE_SHOT
249  * @param[in] timeout_handler Function to call at timeout. Cannot be NULL.
250  * @param[in] user_data User data associated with the timeout. Can be NULL.
251  *
252  * \return RTI_TRUE on success, RTI_FALSE on failure
253  *
254  * @mtsafety SAFE
255  */
256 MUST_CHECK_RETURN OSAPIDllExport RTI_BOOL
257 OSAPI_Timer_create_timeout(OSAPI_Timer_T timer,
258  OSAPI_TimeoutHandle_T *out_handle,
259  RTI_INT32 timeout_sec,
260  RTI_INT32 timeout_nsec,
261  RTI_INT32 flags,
262  OSAPI_TimeoutFunction_T timeout_handler,
263  struct OSAPI_TimeoutUserData *user_data);
264 
265 /*e \ingroup OSAPI_TimerClass
266  * \brief Reschedule a timeout.
267  *
268  * \details This function reschedules a previously scheduled timeout with
269  * the specified period.
270  *
271  * Example:
272  * \code
273  * OSAPI_Timer_t my_timer;
274  * OSAPITimeoutHandle_t my_handle = OSAPITimeoutHandle_t_INITIALIZER;
275  * RTI_BOOL result;
276  * struct OSAPI_TimerEntryUserData user_data;
277  *
278  * result = OSAPI_Timer_update_timeout(my_timer,
279  * &my_handle,
280  * 2000,0);
281  *
282  * if (!result) {
283  * report error;
284  * }
285  *
286  * ......
287  *
288  * \endcode
289  *
290  * @param [in] timer Timer object. Cannot be NULL.
291  * @param [out] out_handle Reference to the timeout. Cannot be NULL.
292  * @param[in] timeout_sec The number of seconds before timeout
293  * @param[in] timeout_nsec Additional number of nanoseconds before timeout
294  *
295  * \return RTI_TRUE on success, RTI_FALSE on failure
296  */
297 MUST_CHECK_RETURN OSAPIDllExport RTI_BOOL
298 OSAPI_Timer_update_timeout(OSAPI_Timer_T timer,
299  OSAPI_TimeoutHandle_T *out_handle,
300  RTI_INT32 timeout_sec,
301  RTI_INT32 timeout_nsec);
302 
303 /*e \ingroup OSAPI_TimerClass
304  * \brief Stop a previously scheduled timeout
305  *
306  * \details This function stops a previously scheduled timeout.
307  *
308  * Example:
309  * \code
310  * OSAPI_Timer_t my_timer;
311  * OSAPITimeoutHandle_t my_handle = OSAPITimeoutHandle_t_INITIALIZER;
312  * RTI_BOOL result;
313  *
314  * ....
315  *
316  * result = OSAPI_Timer_stop_timer(my_timer,&my_handle);
317  *
318  * if (!result) {
319  * report error;
320  * }
321  *
322  * ......
323  *
324  * \endcode
325  *
326  * @param [in] timer Timer object. Cannot be NULL.
327  * @param [in] handle Reference to the timeout. Cannot be NULL.
328  *
329  * \return RTI_TRUE on success, RTI_FALSE on failure
330  *
331  * @mtsafety SAFE
332  */
333 SHOULD_CHECK_RETURN OSAPIDllExport RTI_BOOL
334 OSAPI_Timer_delete_timeout(OSAPI_Timer_T timer, OSAPI_TimeoutHandle_T *handle);
335 
336 /*e \ingroup OSAPI_TimerClass
337  * \brief Get the user_data from a handle.
338  *
339  * Example:
340  * \code
341  * OSAPI_Timer_t my_timer;
342  * OSAPITimeoutHandle_t my_handle = OSAPITimeoutHandle_t_INITIALIZER;
343  * RTI_BOOL result;
344  * struct OSAPI_TimerEntryUserData user_data;
345  *
346  * ....
347  *
348  * result = OSAPITimeoutHandle_get_user_data(&user_data,&my_handle);
349  *
350  * if (!result) {
351  * report error;
352  * }
353  *
354  * ......
355  *
356  * \endcode
357  *
358  * @param [out] user_data User-data associated with handle. Cannot be NULL.
359  * @param [in] handle Timer handle.
360  *
361  * \return RTI_TRUE on success, RTI_FALSE on failure
362  */
363 MUST_CHECK_RETURN OSAPIDllExport RTI_BOOL
365  OSAPI_TimeoutHandle_T *handle);
366 
367 #ifdef __cplusplus
368 } /* extern "C" */
369 #endif
370 
371 
372 #endif /* osapi_timer_h */

RTI Connext DDS Micro Version 2.4.11 Copyright © Mon Jul 23 2018 Real-Time Innovations, Inc