mutex.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. * SPDX-FileCopyrightText: 2017-2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include "mesh/common.h"
  7. static bt_mesh_mutex_t alarm_lock;
  8. static bt_mesh_mutex_t list_lock;
  9. static bt_mesh_mutex_t buf_lock;
  10. static bt_mesh_mutex_t atomic_lock;
  11. void bt_mesh_mutex_create(bt_mesh_mutex_t *mutex)
  12. {
  13. if (!mutex) {
  14. BT_ERR("Create, invalid mutex");
  15. return;
  16. }
  17. #if CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC
  18. #if CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC_EXTERNAL
  19. mutex->buffer = heap_caps_calloc_prefer(1, sizeof(StaticQueue_t), 2, MALLOC_CAP_SPIRAM|MALLOC_CAP_8BIT, MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT);
  20. #elif CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC_IRAM_8BIT
  21. mutex->buffer = heap_caps_calloc_prefer(1, sizeof(StaticQueue_t), 2, MALLOC_CAP_INTERNAL|MALLOC_CAP_IRAM_8BIT, MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT);
  22. #endif
  23. __ASSERT(mutex->buffer, "Failed to create mutex buffer");
  24. mutex->mutex = xSemaphoreCreateMutexStatic(mutex->buffer);
  25. __ASSERT(mutex->mutex, "Failed to create static mutex");
  26. #else /* CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC */
  27. mutex->mutex = xSemaphoreCreateMutex();
  28. __ASSERT(mutex->mutex, "Failed to create mutex");
  29. #endif /* CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC */
  30. }
  31. void bt_mesh_mutex_free(bt_mesh_mutex_t *mutex)
  32. {
  33. if (!mutex) {
  34. BT_ERR("Free, invalid mutex");
  35. return;
  36. }
  37. if (mutex->mutex) {
  38. vSemaphoreDelete(mutex->mutex);
  39. mutex->mutex = NULL;
  40. #if CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC
  41. heap_caps_free(mutex->buffer);
  42. mutex->buffer = NULL;
  43. #endif
  44. }
  45. }
  46. void bt_mesh_mutex_lock(bt_mesh_mutex_t *mutex)
  47. {
  48. if (!mutex) {
  49. BT_ERR("Lock, invalid mutex");
  50. return;
  51. }
  52. if (mutex->mutex) {
  53. xSemaphoreTake(mutex->mutex, portMAX_DELAY);
  54. }
  55. }
  56. void bt_mesh_mutex_unlock(bt_mesh_mutex_t *mutex)
  57. {
  58. if (!mutex) {
  59. BT_ERR("Unlock, invalid mutex");
  60. return;
  61. }
  62. if (mutex->mutex) {
  63. xSemaphoreGive(mutex->mutex);
  64. }
  65. }
  66. void bt_mesh_r_mutex_create(bt_mesh_mutex_t *mutex)
  67. {
  68. if (!mutex) {
  69. BT_ERR("Create, invalid recursive mutex");
  70. return;
  71. }
  72. #if CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC
  73. #if CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC_EXTERNAL
  74. mutex->buffer = heap_caps_calloc_prefer(1, sizeof(StaticQueue_t), 2, MALLOC_CAP_SPIRAM|MALLOC_CAP_8BIT, MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT);
  75. #elif CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC_IRAM_8BIT
  76. mutex->buffer = heap_caps_calloc_prefer(1, sizeof(StaticQueue_t), 2, MALLOC_CAP_INTERNAL|MALLOC_CAP_IRAM_8BIT, MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT);
  77. #endif
  78. __ASSERT(mutex->buffer, "Failed to create recursive mutex buffer");
  79. mutex->mutex = xSemaphoreCreateRecursiveMutexStatic(mutex->buffer);
  80. __ASSERT(mutex->mutex, "Failed to create static recursive mutex");
  81. #else /* CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC */
  82. mutex->mutex = xSemaphoreCreateRecursiveMutex();
  83. __ASSERT(mutex->mutex, "Failed to create recursive mutex");
  84. #endif /* CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC */
  85. }
  86. void bt_mesh_r_mutex_free(bt_mesh_mutex_t *mutex)
  87. {
  88. bt_mesh_mutex_free(mutex);
  89. }
  90. void bt_mesh_r_mutex_lock(bt_mesh_mutex_t *mutex)
  91. {
  92. if (!mutex) {
  93. BT_ERR("Lock, invalid recursive mutex");
  94. return;
  95. }
  96. if (mutex->mutex) {
  97. xSemaphoreTakeRecursive(mutex->mutex, portMAX_DELAY);
  98. }
  99. }
  100. void bt_mesh_r_mutex_unlock(bt_mesh_mutex_t *mutex)
  101. {
  102. if (!mutex) {
  103. BT_ERR("Unlock, invalid recursive mutex");
  104. return;
  105. }
  106. if (mutex->mutex) {
  107. xSemaphoreGiveRecursive(mutex->mutex);
  108. }
  109. }
  110. void bt_mesh_alarm_lock(void)
  111. {
  112. bt_mesh_mutex_lock(&alarm_lock);
  113. }
  114. void bt_mesh_alarm_unlock(void)
  115. {
  116. bt_mesh_mutex_unlock(&alarm_lock);
  117. }
  118. void bt_mesh_list_lock(void)
  119. {
  120. bt_mesh_mutex_lock(&list_lock);
  121. }
  122. void bt_mesh_list_unlock(void)
  123. {
  124. bt_mesh_mutex_unlock(&list_lock);
  125. }
  126. void bt_mesh_buf_lock(void)
  127. {
  128. bt_mesh_mutex_lock(&buf_lock);
  129. }
  130. void bt_mesh_buf_unlock(void)
  131. {
  132. bt_mesh_mutex_unlock(&buf_lock);
  133. }
  134. void bt_mesh_atomic_lock(void)
  135. {
  136. bt_mesh_mutex_lock(&atomic_lock);
  137. }
  138. void bt_mesh_atomic_unlock(void)
  139. {
  140. bt_mesh_mutex_unlock(&atomic_lock);
  141. }
  142. void bt_mesh_mutex_init(void)
  143. {
  144. bt_mesh_mutex_create(&alarm_lock);
  145. bt_mesh_mutex_create(&list_lock);
  146. bt_mesh_mutex_create(&buf_lock);
  147. bt_mesh_mutex_create(&atomic_lock);
  148. }
  149. #if CONFIG_BLE_MESH_DEINIT
  150. void bt_mesh_mutex_deinit(void)
  151. {
  152. bt_mesh_mutex_free(&alarm_lock);
  153. bt_mesh_mutex_free(&list_lock);
  154. bt_mesh_mutex_free(&buf_lock);
  155. bt_mesh_mutex_free(&atomic_lock);
  156. }
  157. #endif /* CONFIG_BLE_MESH_DEINIT */