cthread.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. #include <cstring>
  39. #include "thread.hpp"
  40. using namespace cpp_freertos;
  41. volatile bool Thread::SchedulerActive = false;
  42. MutexStandard Thread::StartGuardLock;
  43. //
  44. // We want to use C++ strings. This is the default.
  45. //
  46. #ifndef CPP_FREERTOS_NO_CPP_STRINGS
  47. Thread::Thread( const std::string pcName,
  48. uint16_t usStackDepth,
  49. UBaseType_t uxPriority)
  50. : Name(pcName),
  51. StackDepth(usStackDepth),
  52. Priority(uxPriority),
  53. ThreadStarted(false)
  54. {
  55. #if (INCLUDE_vTaskDelayUntil == 1)
  56. delayUntilInitialized = false;
  57. #endif
  58. }
  59. Thread::Thread( uint16_t usStackDepth,
  60. UBaseType_t uxPriority)
  61. : Name("Default"),
  62. StackDepth(usStackDepth),
  63. Priority(uxPriority),
  64. ThreadStarted(false)
  65. {
  66. #if (INCLUDE_vTaskDelayUntil == 1)
  67. delayUntilInitialized = false;
  68. #endif
  69. }
  70. //
  71. // We do not want to use C++ strings. Fall back to character arrays.
  72. //
  73. #else
  74. Thread::Thread( const char *pcName,
  75. uint16_t usStackDepth,
  76. UBaseType_t uxPriority)
  77. : StackDepth(usStackDepth),
  78. Priority(uxPriority),
  79. ThreadStarted(false)
  80. {
  81. for (int i = 0; i < configMAX_TASK_NAME_LEN - 1; i++) {
  82. Name[i] = *pcName;
  83. if (*pcName == 0)
  84. break;
  85. pcName++;
  86. }
  87. Name[configMAX_TASK_NAME_LEN - 1] = 0;
  88. #if (INCLUDE_vTaskDelayUntil == 1)
  89. delayUntilInitialized = false;
  90. #endif
  91. }
  92. Thread::Thread( uint16_t usStackDepth,
  93. UBaseType_t uxPriority)
  94. : StackDepth(usStackDepth),
  95. Priority(uxPriority),
  96. ThreadStarted(false)
  97. {
  98. memset(Name, 0, sizeof(Name));
  99. #if (INCLUDE_vTaskDelayUntil == 1)
  100. delayUntilInitialized = false;
  101. #endif
  102. }
  103. #endif
  104. bool Thread::Start()
  105. {
  106. //
  107. // If the Scheduler is on, we need to lock before checking
  108. // the ThreadStarted variable. We'll leverage the LockGuard
  109. // pattern, so we can create the guard and just forget it.
  110. // Leaving scope, including the return, will automatically
  111. // unlock it.
  112. //
  113. if (SchedulerActive) {
  114. LockGuard guard (StartGuardLock);
  115. if (ThreadStarted)
  116. return false;
  117. else
  118. ThreadStarted = true;
  119. }
  120. //
  121. // If the Scheduler isn't running, just check it.
  122. //
  123. else {
  124. if (ThreadStarted)
  125. return false;
  126. else
  127. ThreadStarted = true;
  128. }
  129. #ifndef CPP_FREERTOS_NO_CPP_STRINGS
  130. BaseType_t rc = xTaskCreate(TaskFunctionAdapter,
  131. Name.c_str(),
  132. StackDepth,
  133. this,
  134. Priority,
  135. &handle);
  136. #else
  137. BaseType_t rc = xTaskCreate(TaskFunctionAdapter,
  138. Name,
  139. StackDepth,
  140. this,
  141. Priority,
  142. &handle);
  143. #endif
  144. return rc != pdPASS ? false : true;
  145. }
  146. #if (INCLUDE_vTaskDelete == 1)
  147. //
  148. // Deliberately empty. If this is needed, it will be overloaded.
  149. //
  150. void Thread::Cleanup()
  151. {
  152. }
  153. Thread::~Thread()
  154. {
  155. vTaskDelete(handle);
  156. handle = (TaskHandle_t)-1;
  157. }
  158. #else
  159. Thread::~Thread()
  160. {
  161. configASSERT( ! "Cannot actually delete a thread object "
  162. "if INCLUDE_vTaskDelete is not defined.");
  163. }
  164. #endif
  165. void Thread::TaskFunctionAdapter(void *pvParameters)
  166. {
  167. Thread *thread = static_cast<Thread *>(pvParameters);
  168. thread->Run();
  169. #if (INCLUDE_vTaskDelete == 1)
  170. thread->Cleanup();
  171. vTaskDelete(thread->handle);
  172. #else
  173. configASSERT( ! "Cannot return from a thread.run function "
  174. "if INCLUDE_vTaskDelete is not defined.");
  175. #endif
  176. }
  177. #if (INCLUDE_vTaskDelayUntil == 1)
  178. void Thread::DelayUntil(const TickType_t Period)
  179. {
  180. if (!delayUntilInitialized) {
  181. delayUntilInitialized = true;
  182. delayUntilPreviousWakeTime = xTaskGetTickCount();
  183. }
  184. vTaskDelayUntil(&delayUntilPreviousWakeTime, Period);
  185. }
  186. void Thread::ResetDelayUntil()
  187. {
  188. delayUntilInitialized = false;
  189. }
  190. #endif
  191. #ifdef CPP_FREERTOS_CONDITION_VARIABLES
  192. bool Thread::Wait( ConditionVariable &Cv,
  193. Mutex &CvLock,
  194. TickType_t Timeout)
  195. {
  196. Cv.AddToWaitList(this);
  197. //
  198. // Drop the associated external lock, as per cv semantics.
  199. //
  200. CvLock.Unlock();
  201. //
  202. // And block on the internal semaphore. The associated Cv
  203. // will call Thread::Signal, which will release the semaphore.
  204. //
  205. bool timed_out = ThreadWaitSem.Take(Timeout);
  206. //
  207. // Grab the external lock again, as per cv semantics.
  208. //
  209. CvLock.Lock();
  210. return timed_out;
  211. }
  212. #endif