tasklet.hpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /****************************************************************************
  2. *
  3. * Copyright (c) 2017, Michael Becker (michael.f.becker@gmail.com)
  4. *
  5. * This file is part of the FreeRTOS Add-ons project.
  6. *
  7. * Source Code:
  8. * https://github.com/michaelbecker/freertos-addons
  9. *
  10. * Project Page:
  11. * http://michaelbecker.github.io/freertos-addons/
  12. *
  13. * On-line Documentation:
  14. * http://michaelbecker.github.io/freertos-addons/docs/html/index.html
  15. *
  16. * Permission is hereby granted, free of charge, to any person obtaining a
  17. * copy of this software and associated documentation files
  18. * (the "Software"), to deal in the Software without restriction, including
  19. * without limitation the rights to use, copy, modify, merge, publish,
  20. * distribute, sublicense, and/or sell copies of the Software, and to
  21. * permit persons to whom the Software is furnished to do so,subject to the
  22. * following conditions:
  23. *
  24. * + The above copyright notice and this permission notice shall be included
  25. * in all copies or substantial portions of the Software.
  26. * + Credit is appreciated, but not required, if you find this project
  27. * useful enough to include in your application, product, device, etc.
  28. *
  29. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  30. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  31. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  32. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  33. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  34. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  35. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  36. *
  37. ***************************************************************************/
  38. #ifndef TASKLET_HPP_
  39. #define TASKLET_HPP_
  40. /**
  41. * C++ exceptions are used by default when constructors fail.
  42. * If you do not want this behavior, define the following in your makefile
  43. * or project. Note that in most / all cases when a constructor fails,
  44. * it's a fatal error. In the cases when you've defined this, the new
  45. * default behavior will be to issue a configASSERT() instead.
  46. */
  47. #ifndef CPP_FREERTOS_NO_EXCEPTIONS
  48. #include <exception>
  49. #include <string>
  50. #include <cstdio>
  51. #ifdef CPP_FREERTOS_NO_CPP_STRINGS
  52. #error "FreeRTOS-Addons require C++ Strings if you are using exceptions"
  53. #endif
  54. #endif
  55. #include "FreeRTOS.h"
  56. #include "timers.h"
  57. #include "semphr.h"
  58. namespace cpp_freertos {
  59. #ifndef CPP_FREERTOS_NO_EXCEPTIONS
  60. /**
  61. * This is the exception that is thrown if a Tasklet constructor fails.
  62. */
  63. class TaskletCreateException : public std::exception {
  64. public:
  65. /**
  66. * Create the exception.
  67. */
  68. TaskletCreateException()
  69. {
  70. sprintf(errorString, "Tasklet Constructor Failed");
  71. }
  72. /**
  73. * Create the exception.
  74. */
  75. explicit TaskletCreateException(const char *info)
  76. {
  77. snprintf(errorString, sizeof(errorString),
  78. "Tasklet Constructor Failed %s", info);
  79. }
  80. /**
  81. * Get what happened as a string.
  82. * We are overriding the base implementation here.
  83. */
  84. virtual const char *what() const throw()
  85. {
  86. return errorString;
  87. }
  88. private:
  89. /**
  90. * A text string representing what failed.
  91. */
  92. char errorString[80];
  93. };
  94. #endif
  95. /**
  96. * A FreeRTOS wrapper for its concept of a Pended Function.
  97. * In Linux, one permutation of this would be a Tasklet, or
  98. * bottom half processing from an ISR.
  99. *
  100. * This is an abstract base class.
  101. * To use this, you need to subclass it. All of your Tasklets should
  102. * be derived from the Tasklet class. Then implement the virtual Run
  103. * function. This is a similar design to Java threading.
  104. */
  105. class Tasklet {
  106. /////////////////////////////////////////////////////////////////////////
  107. //
  108. // Public API
  109. //
  110. /////////////////////////////////////////////////////////////////////////
  111. public:
  112. /**
  113. * Constructor
  114. * @note Do not construct inside an ISR! This includes creating
  115. * local instances of this object.
  116. */
  117. Tasklet();
  118. /**
  119. * Destructor
  120. * @note Do not delete inside an ISR! This includes the automatic
  121. * deletion of local instances of this object when leaving scope.
  122. */
  123. virtual ~Tasklet();
  124. /**
  125. * Schedule this Tasklet to run.
  126. *
  127. * @param parameter Value passed to your Run method.
  128. * @param CmdTimeout How long to wait to send this command to the
  129. * timer daemon.
  130. * @returns true if this command will be sent to the timer daemon,
  131. * false if it will not (i.e. timeout).
  132. */
  133. bool Schedule( uint32_t parameter,
  134. TickType_t CmdTimeout = portMAX_DELAY);
  135. /**
  136. * Schedule this Tasklet to run from ISR context.
  137. * This allows FreeRTOS ISRs to defer processing from the ISR
  138. * into a task context.
  139. *
  140. * @param parameter Value passed to your Run method.
  141. * @param pxHigherPriorityTaskWoken Did this operation result in a
  142. * rescheduling event.
  143. * @returns true if this command will be sent to the timer daemon,
  144. * false if it will not (i.e. timeout).
  145. */
  146. bool ScheduleFromISR( uint32_t parameter,
  147. BaseType_t *pxHigherPriorityTaskWoken);
  148. /////////////////////////////////////////////////////////////////////////
  149. //
  150. // Protected API
  151. // Available from inside your Tasklet implementation.
  152. // You should make sure that you are only calling these methods
  153. // from within your Run() method, or that your Run() method is on the
  154. // callstack.
  155. //
  156. /////////////////////////////////////////////////////////////////////////
  157. protected:
  158. /**
  159. * Implementation of your actual tasklet code.
  160. * You must override this function.
  161. *
  162. * @param parameter Value passed to you from the Schedule() methods.
  163. */
  164. virtual void Run(uint32_t parameter) = 0;
  165. /**
  166. * You must call this in your dtor, to synchronize between
  167. * being called and being deleted.
  168. */
  169. void CheckForSafeDelete();
  170. /////////////////////////////////////////////////////////////////////////
  171. //
  172. // Private API
  173. // The internals of this wrapper class.
  174. //
  175. /////////////////////////////////////////////////////////////////////////
  176. private:
  177. /**
  178. * Adapter function that allows you to write a class
  179. * specific Run() function that interfaces with FreeRTOS.
  180. * Look at the implementation of the constructors and this
  181. * code to see how the interface between C and C++ is performed.
  182. */
  183. static void TaskletAdapterFunction(void *ref, uint32_t parameter);
  184. /**
  185. * Protect against accidental deletion before we were executed.
  186. */
  187. SemaphoreHandle_t DtorLock;
  188. };
  189. }
  190. #endif