ua_timer.h 3.0 KB

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