mutex.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. vSemaphoreDelete((SemaphoreHandle_t)((uint32_t)instance & ~1U));
  26. }
  27. FuriStatus furi_mutex_acquire(FuriMutex* instance, uint32_t timeout) {
  28. SemaphoreHandle_t hMutex;
  29. FuriStatus stat;
  30. uint32_t rmtx;
  31. hMutex = (SemaphoreHandle_t)((uint32_t)instance & ~1U);
  32. /* Extract recursive mutex flag */
  33. rmtx = (uint32_t)instance & 1U;
  34. stat = FuriStatusOk;
  35. if(FURI_IS_IRQ_MODE()) {
  36. stat = FuriStatusErrorISR;
  37. } else if(hMutex == NULL) {
  38. stat = FuriStatusErrorParameter;
  39. } else {
  40. if(rmtx != 0U) {
  41. if(xSemaphoreTakeRecursive(hMutex, timeout) != pdPASS) {
  42. if(timeout != 0U) {
  43. stat = FuriStatusErrorTimeout;
  44. } else {
  45. stat = FuriStatusErrorResource;
  46. }
  47. }
  48. } else {
  49. if(xSemaphoreTake(hMutex, timeout) != pdPASS) {
  50. if(timeout != 0U) {
  51. stat = FuriStatusErrorTimeout;
  52. } else {
  53. stat = FuriStatusErrorResource;
  54. }
  55. }
  56. }
  57. }
  58. /* Return execution status */
  59. return (stat);
  60. }
  61. FuriStatus furi_mutex_release(FuriMutex* instance) {
  62. SemaphoreHandle_t hMutex;
  63. FuriStatus stat;
  64. uint32_t rmtx;
  65. hMutex = (SemaphoreHandle_t)((uint32_t)instance & ~1U);
  66. /* Extract recursive mutex flag */
  67. rmtx = (uint32_t)instance & 1U;
  68. stat = FuriStatusOk;
  69. if(FURI_IS_IRQ_MODE()) {
  70. stat = FuriStatusErrorISR;
  71. } else if(hMutex == NULL) {
  72. stat = FuriStatusErrorParameter;
  73. } else {
  74. if(rmtx != 0U) {
  75. if(xSemaphoreGiveRecursive(hMutex) != pdPASS) {
  76. stat = FuriStatusErrorResource;
  77. }
  78. } else {
  79. if(xSemaphoreGive(hMutex) != pdPASS) {
  80. stat = FuriStatusErrorResource;
  81. }
  82. }
  83. }
  84. /* Return execution status */
  85. return (stat);
  86. }
  87. FuriThreadId furi_mutex_get_owner(FuriMutex* instance) {
  88. SemaphoreHandle_t hMutex;
  89. FuriThreadId owner;
  90. hMutex = (SemaphoreHandle_t)((uint32_t)instance & ~1U);
  91. if((FURI_IS_IRQ_MODE()) || (hMutex == NULL)) {
  92. owner = 0;
  93. } else {
  94. owner = (FuriThreadId)xSemaphoreGetMutexHolder(hMutex);
  95. }
  96. /* Return owner thread ID */
  97. return (owner);
  98. }