ua_timer.h 3.5 KB

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