mutex.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #include "mutex.h"
  2. #include "check.h"
  3. #include "common_defines.h"
  4. #include <semphr.h>
  5. FuriMutex* furi_mutex_alloc(FuriMutexType type) {
  6. furi_assert(!FURI_IS_IRQ_MODE());
  7. SemaphoreHandle_t hMutex = NULL;
  8. if(type == FuriMutexTypeNormal) {
  9. hMutex = xSemaphoreCreateMutex();
  10. } else if(type == FuriMutexTypeRecursive) {
  11. hMutex = xSemaphoreCreateRecursiveMutex();
  12. } else {
  13. furi_crash("Programming error");
  14. }
  15. furi_check(hMutex != NULL);
  16. if(type == FuriMutexTypeRecursive) {
  17. /* Set LSB as 'recursive mutex flag' */
  18. hMutex = (SemaphoreHandle_t)((uint32_t)hMutex | 1U);
  19. }
  20. /* Return mutex ID */
  21. return ((FuriMutex*)hMutex);
  22. }
  23. void furi_mutex_free(FuriMutex* instance) {
  24. furi_assert(!FURI_IS_IRQ_MODE());
  25. furi_assert(instance);
  26. vSemaphoreDelete((SemaphoreHandle_t)((uint32_t)instance & ~1U));
  27. }
  28. FuriStatus furi_mutex_acquire(FuriMutex* instance, uint32_t timeout) {
  29. SemaphoreHandle_t hMutex;
  30. FuriStatus stat;
  31. uint32_t rmtx;
  32. hMutex = (SemaphoreHandle_t)((uint32_t)instance & ~1U);
  33. /* Extract recursive mutex flag */
  34. rmtx = (uint32_t)instance & 1U;
  35. stat = FuriStatusOk;
  36. if(FURI_IS_IRQ_MODE()) {
  37. stat = FuriStatusErrorISR;
  38. } else if(hMutex == NULL) {
  39. stat = FuriStatusErrorParameter;
  40. } else {
  41. if(rmtx != 0U) {
  42. if(xSemaphoreTakeRecursive(hMutex, timeout) != pdPASS) {
  43. if(timeout != 0U) {
  44. stat = FuriStatusErrorTimeout;
  45. } else {
  46. stat = FuriStatusErrorResource;
  47. }
  48. }
  49. } else {
  50. if(xSemaphoreTake(hMutex, timeout) != pdPASS) {
  51. if(timeout != 0U) {
  52. stat = FuriStatusErrorTimeout;
  53. } else {
  54. stat = FuriStatusErrorResource;
  55. }
  56. }
  57. }
  58. }
  59. /* Return execution status */
  60. return (stat);
  61. }
  62. FuriStatus furi_mutex_release(FuriMutex* instance) {
  63. SemaphoreHandle_t hMutex;
  64. FuriStatus stat;
  65. uint32_t rmtx;
  66. hMutex = (SemaphoreHandle_t)((uint32_t)instance & ~1U);
  67. /* Extract recursive mutex flag */
  68. rmtx = (uint32_t)instance & 1U;
  69. stat = FuriStatusOk;
  70. if(FURI_IS_IRQ_MODE()) {
  71. stat = FuriStatusErrorISR;
  72. } else if(hMutex == NULL) {
  73. stat = FuriStatusErrorParameter;
  74. } else {
  75. if(rmtx != 0U) {
  76. if(xSemaphoreGiveRecursive(hMutex) != pdPASS) {
  77. stat = FuriStatusErrorResource;
  78. }
  79. } else {
  80. if(xSemaphoreGive(hMutex) != pdPASS) {
  81. stat = FuriStatusErrorResource;
  82. }
  83. }
  84. }
  85. /* Return execution status */
  86. return (stat);
  87. }
  88. FuriThreadId furi_mutex_get_owner(FuriMutex* instance) {
  89. SemaphoreHandle_t hMutex;
  90. FuriThreadId owner;
  91. hMutex = (SemaphoreHandle_t)((uint32_t)instance & ~1U);
  92. if((FURI_IS_IRQ_MODE()) || (hMutex == NULL)) {
  93. owner = 0;
  94. } else {
  95. owner = (FuriThreadId)xSemaphoreGetMutexHolder(hMutex);
  96. }
  97. /* Return owner thread ID */
  98. return (owner);
  99. }