ctasklet.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 "tasklet.hpp"
  39. using namespace cpp_freertos;
  40. Tasklet::Tasklet()
  41. {
  42. DtorLock = xSemaphoreCreateBinary();
  43. if (DtorLock == NULL) {
  44. #ifndef CPP_FREERTOS_NO_EXCEPTIONS
  45. throw TaskletCreateException("Create DtorLock Failed");
  46. #else
  47. configASSERT(!"Tasklet Constructor Failed");
  48. #endif
  49. }
  50. xSemaphoreGive(DtorLock);
  51. }
  52. Tasklet::~Tasklet()
  53. {
  54. }
  55. void Tasklet::CheckForSafeDelete()
  56. {
  57. xSemaphoreTake(DtorLock, portMAX_DELAY);
  58. vSemaphoreDelete(DtorLock);
  59. }
  60. void Tasklet::TaskletAdapterFunction(void *reference, uint32_t parameter)
  61. {
  62. Tasklet *tasklet = static_cast<Tasklet *>(reference);
  63. tasklet->Run(parameter);
  64. xSemaphoreGive(tasklet->DtorLock);
  65. }
  66. bool Tasklet::Schedule( uint32_t parameter,
  67. TickType_t CmdTimeout)
  68. {
  69. BaseType_t rc;
  70. xSemaphoreTake(DtorLock, portMAX_DELAY);
  71. rc = xTimerPendFunctionCall(TaskletAdapterFunction,
  72. this,
  73. parameter,
  74. CmdTimeout);
  75. if (rc == pdPASS) {
  76. return true;
  77. }
  78. else {
  79. xSemaphoreGive(DtorLock);
  80. return false;
  81. }
  82. }
  83. bool Tasklet::ScheduleFromISR( uint32_t parameter,
  84. BaseType_t *pxHigherPriorityTaskWoken)
  85. {
  86. BaseType_t rc;
  87. rc = xSemaphoreTakeFromISR(DtorLock, pxHigherPriorityTaskWoken);
  88. if (rc != pdTRUE) {
  89. return false;
  90. }
  91. rc = xTimerPendFunctionCallFromISR( TaskletAdapterFunction,
  92. this,
  93. parameter,
  94. pxHigherPriorityTaskWoken);
  95. if (rc == pdPASS) {
  96. return true;
  97. }
  98. else {
  99. xSemaphoreGive(DtorLock);
  100. return false;
  101. }
  102. }