TimerThread.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*******************************************************************************
  2. *
  3. * Copyright (c) 2000-2003 Intel Corporation
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * * Redistributions of source code must retain the above copyright notice,
  10. * this list of conditions and the following disclaimer.
  11. * * Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. * * Neither name of Intel Corporation nor the names of its contributors
  15. * may be used to endorse or promote products derived from this software
  16. * without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTEL OR
  22. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  23. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  24. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  25. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  26. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  27. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. *
  30. ******************************************************************************/
  31. #ifndef TIMERTHREAD_H
  32. #define TIMERTHREAD_H
  33. /*!
  34. * \file
  35. */
  36. #include "FreeList.h"
  37. #include "ithread.h"
  38. #include "LinkedList.h"
  39. #include "ThreadPool.h"
  40. #ifdef __cplusplus
  41. extern "C" {
  42. #endif
  43. #define INVALID_EVENT_ID (-10 & 1<<29)
  44. /*! Timeout Types. */
  45. typedef enum timeoutType {
  46. /*! seconds from Jan 1, 1970. */
  47. ABS_SEC,
  48. /*! seconds from current time. */
  49. REL_SEC
  50. } TimeoutType;
  51. /*!
  52. * A timer thread similar to the one in the Upnp SDK that allows
  53. * the scheduling of a job to run at a specified time in the future.
  54. *
  55. * Because the timer thread uses the thread pool there is no
  56. * gurantee of timing, only approximate timing.
  57. *
  58. * Uses ThreadPool, Mutex, Condition, Thread.
  59. */
  60. typedef struct TIMERTHREAD
  61. {
  62. ithread_mutex_t mutex;
  63. ithread_cond_t condition;
  64. int lastEventId;
  65. LinkedList eventQ;
  66. int shutdown;
  67. FreeList freeEvents;
  68. ThreadPool *tp;
  69. } TimerThread;
  70. /*!
  71. * Struct to contain information for a timer event.
  72. *
  73. * Internal to the TimerThread.
  74. */
  75. typedef struct TIMEREVENT
  76. {
  77. ThreadPoolJob job;
  78. /*! [in] Absolute time for event in seconds since Jan 1, 1970. */
  79. time_t eventTime;
  80. /*! [in] Long term or short term job. */
  81. Duration persistent;
  82. int id;
  83. } TimerEvent;
  84. /*!
  85. * \brief Initializes and starts timer thread.
  86. *
  87. * \return 0 on success, nonzero on failure. Returns error from
  88. * ThreadPoolAddPersistent on failure.
  89. */
  90. int TimerThreadInit(
  91. /*! [in] Valid timer thread pointer. */
  92. TimerThread *timer,
  93. /*! [in] Valid thread pool to use. Must be started. Must be valid for
  94. * lifetime of timer. Timer must be shutdown BEFORE thread pool. */
  95. ThreadPool *tp);
  96. /*!
  97. * \brief Schedules an event to run at a specified time.
  98. *
  99. * \return 0 on success, nonzero on failure, EOUTOFMEM if not enough memory
  100. * to schedule job.
  101. */
  102. int TimerThreadSchedule(
  103. /*! [in] Valid timer thread pointer. */
  104. TimerThread* timer,
  105. /*! [in] time of event. Either in absolute seconds, or relative
  106. * seconds in the future. */
  107. time_t time,
  108. /*! [in] either ABS_SEC, or REL_SEC. If REL_SEC, then the event
  109. * will be scheduled at the current time + REL_SEC. */
  110. TimeoutType type,
  111. /*! [in] Valid Thread pool job with following fields. */
  112. ThreadPoolJob *job,
  113. /*! [in] . */
  114. Duration duration,
  115. /*! [in] Id of timer event. (out, can be null). */
  116. int *id);
  117. /*!
  118. * \brief Removes an event from the timer Q.
  119. *
  120. * Events can only be removed before they have been placed in the thread pool.
  121. *
  122. * \return 0 on success, INVALID_EVENT_ID on failure.
  123. */
  124. int TimerThreadRemove(
  125. /*! [in] Valid timer thread pointer. */
  126. TimerThread *timer,
  127. /*! [in] Id of event to remove. */
  128. int id,
  129. /*! [in] Space for thread pool job. */
  130. ThreadPoolJob *out);
  131. /*!
  132. * \brief Shutdown the timer thread.
  133. *
  134. * Events scheduled in the future will NOT be run.
  135. *
  136. * Timer thread should be shutdown BEFORE it's associated thread pool.
  137. *
  138. * \return 0 if succesfull, nonzero otherwise. Always returns 0.
  139. */
  140. int TimerThreadShutdown(
  141. /*! [in] Valid timer thread pointer. */
  142. TimerThread *timer);
  143. #ifdef __cplusplus
  144. }
  145. #endif
  146. #endif /* TIMER_THREAD_H */