ua_timer.h 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. #ifndef UA_TIMER_H_
  5. #define UA_TIMER_H_
  6. #ifdef __cplusplus
  7. extern "C" {
  8. #endif
  9. #include "ua_util.h"
  10. /* An (event) timer triggers callbacks with a recurring interval. Adding,
  11. * removing and changing repeated callbacks can be done from independent
  12. * threads. Processing the changes and dispatching callbacks must be done by a
  13. * single "mainloop" process. */
  14. /* Forward declaration */
  15. struct UA_TimerCallbackEntry;
  16. typedef struct UA_TimerCallbackEntry UA_TimerCallbackEntry;
  17. /* Linked-list definition */
  18. typedef SLIST_HEAD(UA_TimerCallbackList, UA_TimerCallbackEntry) UA_TimerCallbackList;
  19. typedef struct {
  20. /* The linked list of callbacks is sorted according to the execution timestamp. */
  21. UA_TimerCallbackList repeatedCallbacks;
  22. /* Changes to the repeated callbacks in a multi-producer single-consumer queue */
  23. UA_TimerCallbackEntry * volatile changes_head;
  24. UA_TimerCallbackEntry *changes_tail;
  25. UA_TimerCallbackEntry *changes_stub;
  26. UA_UInt64 idCounter;
  27. } UA_Timer;
  28. /* Initialize the Timer. Not thread-safe. */
  29. void UA_Timer_init(UA_Timer *t);
  30. /* Add a repated callback. Thread-safe, can be used in parallel and in parallel
  31. * with UA_Timer_process. */
  32. typedef void (*UA_TimerCallback)(void *application, void *data);
  33. UA_StatusCode
  34. UA_Timer_addRepeatedCallback(UA_Timer *t, UA_TimerCallback callback, void *data,
  35. UA_UInt32 interval, UA_UInt64 *callbackId);
  36. /* Change the callback interval. If this is called from within the callback. The
  37. * adjustment is made during the next _process call. */
  38. UA_StatusCode
  39. UA_Timer_changeRepeatedCallbackInterval(UA_Timer *t, UA_UInt64 callbackId,
  40. UA_UInt32 interval);
  41. /* Remove a repated callback. Thread-safe, can be used in parallel and in
  42. * parallel with UA_Timer_process. */
  43. UA_StatusCode
  44. UA_Timer_removeRepeatedCallback(UA_Timer *t, UA_UInt64 callbackId);
  45. /* Process (dispatch) the repeated callbacks that have timed out. Returns the
  46. * timestamp of the next scheduled repeated callback. Not thread-safe.
  47. * Application is a pointer to the client / server environment for the callback.
  48. * Dispatched is set to true when at least one callback was run / dispatched. */
  49. typedef void (*UA_TimerDispatchCallback)(void *application, UA_TimerCallback callback,
  50. void *data);
  51. UA_DateTime
  52. UA_Timer_process(UA_Timer *t, UA_DateTime nowMonotonic,
  53. UA_TimerDispatchCallback dispatchCallback,
  54. void *application);
  55. /* Remove all repeated callbacks. Not thread-safe. */
  56. void UA_Timer_deleteMembers(UA_Timer *t);
  57. #ifdef __cplusplus
  58. } // extern "C"
  59. #endif
  60. #endif /* UA_TIMER_H_ */