ua_timer.h 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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_internal.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. * Timer callbacks with the same recurring interval are batched into blocks in
  19. * order to reduce linear search for re-entry to the sorted list after processing.
  20. * Callbacks are inserted in reversed order (last callback are put first in the block)
  21. * to allow the monitored items of a subscription (if created in a sequence with the
  22. * same publish/sample interval) to be executed before the subscription publish the
  23. * notifications. When callbacks are entered to the timer list after execution they
  24. * are added in the same order as before execution. */
  25. /* Forward declaration */
  26. struct UA_TimerCallbackEntry;
  27. typedef struct UA_TimerCallbackEntry UA_TimerCallbackEntry;
  28. /* Linked-list definition */
  29. typedef SLIST_HEAD(UA_TimerCallbackList, UA_TimerCallbackEntry) UA_TimerCallbackList;
  30. typedef struct {
  31. /* The linked list of callbacks is sorted according to the execution timestamp. */
  32. UA_TimerCallbackList repeatedCallbacks;
  33. /* Changes to the repeated callbacks in a multi-producer single-consumer queue */
  34. UA_TimerCallbackEntry * volatile changes_head;
  35. UA_TimerCallbackEntry *changes_tail;
  36. UA_TimerCallbackEntry *changes_stub;
  37. UA_UInt64 idCounter;
  38. } UA_Timer;
  39. /* Initialize the Timer. Not thread-safe. */
  40. void UA_Timer_init(UA_Timer *t);
  41. /* Add a repated callback. Thread-safe, can be used in parallel and in parallel
  42. * with UA_Timer_process. */
  43. typedef void (*UA_TimerCallback)(void *application, void *data);
  44. UA_StatusCode
  45. UA_Timer_addRepeatedCallback(UA_Timer *t, UA_TimerCallback callback, void *data,
  46. UA_UInt32 interval, UA_UInt64 *callbackId);
  47. /* Change the callback interval. If this is called from within the callback. The
  48. * adjustment is made during the next _process call. */
  49. UA_StatusCode
  50. UA_Timer_changeRepeatedCallbackInterval(UA_Timer *t, UA_UInt64 callbackId,
  51. UA_UInt32 interval);
  52. /* Remove a repated callback. Thread-safe, can be used in parallel and in
  53. * parallel with UA_Timer_process. */
  54. UA_StatusCode
  55. UA_Timer_removeRepeatedCallback(UA_Timer *t, UA_UInt64 callbackId);
  56. /* Process (dispatch) the repeated callbacks that have timed out. Returns the
  57. * timestamp of the next scheduled repeated callback. Not thread-safe.
  58. * Application is a pointer to the client / server environment for the callback.
  59. * Dispatched is set to true when at least one callback was run / dispatched. */
  60. typedef void (*UA_TimerDispatchCallback)(void *application, UA_TimerCallback callback,
  61. void *data);
  62. UA_DateTime
  63. UA_Timer_process(UA_Timer *t, UA_DateTime nowMonotonic,
  64. UA_TimerDispatchCallback dispatchCallback,
  65. void *application);
  66. /* Remove all repeated callbacks. Not thread-safe. */
  67. void UA_Timer_deleteMembers(UA_Timer *t);
  68. #ifdef __cplusplus
  69. } // extern "C"
  70. #endif
  71. #endif /* UA_TIMER_H_ */