locks.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include <sys/lock.h>
  15. #include <stdlib.h>
  16. #include <sys/reent.h>
  17. #include "esp_attr.h"
  18. #include "soc/cpu.h"
  19. #include "freertos/FreeRTOS.h"
  20. #include "freertos/semphr.h"
  21. #include "freertos/portmacro.h"
  22. #include "freertos/task.h"
  23. /* Notes on our newlib lock implementation:
  24. *
  25. * - Use FreeRTOS mutex semaphores as locks.
  26. * - lock_t is int, but we store an xSemaphoreHandle there.
  27. * - Locks are no-ops until the FreeRTOS scheduler is running.
  28. * - Due to this, locks need to be lazily initialised the first time
  29. * they are acquired. Initialisation/deinitialisation of locks is
  30. * protected by lock_init_spinlock.
  31. * - Race conditions around lazy initialisation (via lock_acquire) are
  32. * protected against.
  33. * - Anyone calling lock_close is reponsible for ensuring noone else
  34. * is holding the lock at this time.
  35. * - Race conditions between lock_close & lock_init (for the same lock)
  36. * are the responsibility of the caller.
  37. */
  38. static portMUX_TYPE lock_init_spinlock = portMUX_INITIALIZER_UNLOCKED;
  39. /* Initialise the given lock by allocating a new mutex semaphore
  40. as the _lock_t value.
  41. */
  42. static void IRAM_ATTR lock_init_generic(_lock_t *lock, uint8_t mutex_type) {
  43. portENTER_CRITICAL(&lock_init_spinlock);
  44. if (xTaskGetSchedulerState() == taskSCHEDULER_NOT_STARTED) {
  45. /* nothing to do until the scheduler is running */
  46. *lock = 0; /* ensure lock is zeroed out, in case it's an automatic variable */
  47. portEXIT_CRITICAL(&lock_init_spinlock);
  48. return;
  49. }
  50. if (*lock) {
  51. /* Lock already initialised (either we didn't check earlier,
  52. or it got initialised while we were waiting for the
  53. spinlock.) */
  54. }
  55. else
  56. {
  57. /* Create a new semaphore
  58. this is a bit of an API violation, as we're calling the
  59. private function xQueueCreateMutex(x) directly instead of
  60. the xSemaphoreCreateMutex / xSemaphoreCreateRecursiveMutex
  61. wrapper functions...
  62. The better alternative would be to pass pointers to one of
  63. the two xSemaphoreCreate___Mutex functions, but as FreeRTOS
  64. implements these as macros instead of inline functions
  65. (*party like it's 1998!*) it's not possible to do this
  66. without writing wrappers. Doing it this way seems much less
  67. spaghetti-like.
  68. */
  69. xSemaphoreHandle new_sem = xQueueCreateMutex(mutex_type);
  70. if (!new_sem) {
  71. abort(); /* No more semaphores available or OOM */
  72. }
  73. *lock = (_lock_t)new_sem;
  74. }
  75. portEXIT_CRITICAL(&lock_init_spinlock);
  76. }
  77. void IRAM_ATTR _lock_init(_lock_t *lock) {
  78. lock_init_generic(lock, queueQUEUE_TYPE_MUTEX);
  79. }
  80. void IRAM_ATTR _lock_init_recursive(_lock_t *lock) {
  81. lock_init_generic(lock, queueQUEUE_TYPE_RECURSIVE_MUTEX);
  82. }
  83. /* Free the mutex semaphore pointed to by *lock, and zero it out.
  84. Note that FreeRTOS doesn't account for deleting mutexes while they
  85. are held, and neither do we... so take care not to delete newlib
  86. locks while they may be held by other tasks!
  87. */
  88. void IRAM_ATTR _lock_close(_lock_t *lock) {
  89. portENTER_CRITICAL(&lock_init_spinlock);
  90. if (*lock) {
  91. xSemaphoreHandle h = (xSemaphoreHandle)(*lock);
  92. #if (INCLUDE_xSemaphoreGetMutexHolder == 1)
  93. configASSERT(xSemaphoreGetMutexHolder(h) == NULL); /* mutex should not be held */
  94. #endif
  95. vSemaphoreDelete(h);
  96. *lock = 0;
  97. }
  98. portEXIT_CRITICAL(&lock_init_spinlock);
  99. }
  100. /* Acquire the mutex semaphore for lock. wait up to delay ticks.
  101. mutex_type is queueQUEUE_TYPE_RECURSIVE_MUTEX or queueQUEUE_TYPE_MUTEX
  102. */
  103. static int IRAM_ATTR lock_acquire_generic(_lock_t *lock, uint32_t delay, uint8_t mutex_type) {
  104. xSemaphoreHandle h = (xSemaphoreHandle)(*lock);
  105. if (!h) {
  106. if (xTaskGetSchedulerState() == taskSCHEDULER_NOT_STARTED) {
  107. return 0; /* locking is a no-op before scheduler is up, so this "succeeds" */
  108. }
  109. /* lazy initialise lock - might have had a static initializer in newlib (that we don't use),
  110. or _lock_init might have been called before the scheduler was running... */
  111. lock_init_generic(lock, mutex_type);
  112. h = (xSemaphoreHandle)(*lock);
  113. configASSERT(h != NULL);
  114. }
  115. BaseType_t success;
  116. if (cpu_in_interrupt_context()) {
  117. /* In ISR Context */
  118. if (mutex_type == queueQUEUE_TYPE_RECURSIVE_MUTEX) {
  119. abort(); /* recursive mutexes make no sense in ISR context */
  120. }
  121. BaseType_t higher_task_woken = false;
  122. success = xSemaphoreTakeFromISR(h, &higher_task_woken);
  123. if (!success && delay > 0) {
  124. abort(); /* Tried to block on mutex from ISR, couldn't... rewrite your program to avoid libc interactions in ISRs! */
  125. }
  126. if (higher_task_woken) {
  127. portYIELD_FROM_ISR();
  128. }
  129. }
  130. else {
  131. /* In task context */
  132. if (mutex_type == queueQUEUE_TYPE_RECURSIVE_MUTEX) {
  133. success = xSemaphoreTakeRecursive(h, delay);
  134. } else {
  135. success = xSemaphoreTake(h, delay);
  136. }
  137. }
  138. return (success == pdTRUE) ? 0 : -1;
  139. }
  140. void IRAM_ATTR _lock_acquire(_lock_t *lock) {
  141. lock_acquire_generic(lock, portMAX_DELAY, queueQUEUE_TYPE_MUTEX);
  142. }
  143. void IRAM_ATTR _lock_acquire_recursive(_lock_t *lock) {
  144. lock_acquire_generic(lock, portMAX_DELAY, queueQUEUE_TYPE_RECURSIVE_MUTEX);
  145. }
  146. int IRAM_ATTR _lock_try_acquire(_lock_t *lock) {
  147. return lock_acquire_generic(lock, 0, queueQUEUE_TYPE_MUTEX);
  148. }
  149. int IRAM_ATTR _lock_try_acquire_recursive(_lock_t *lock) {
  150. return lock_acquire_generic(lock, 0, queueQUEUE_TYPE_RECURSIVE_MUTEX);
  151. }
  152. /* Release the mutex semaphore for lock.
  153. mutex_type is queueQUEUE_TYPE_RECURSIVE_MUTEX or queueQUEUE_TYPE_MUTEX
  154. */
  155. static void IRAM_ATTR lock_release_generic(_lock_t *lock, uint8_t mutex_type) {
  156. xSemaphoreHandle h = (xSemaphoreHandle)(*lock);
  157. if (h == NULL) {
  158. /* This is probably because the scheduler isn't running yet,
  159. or the scheduler just started running and some code was
  160. "holding" a not-yet-initialised lock... */
  161. return;
  162. }
  163. if (cpu_in_interrupt_context()) {
  164. if (mutex_type == queueQUEUE_TYPE_RECURSIVE_MUTEX) {
  165. abort(); /* indicates logic bug, it shouldn't be possible to lock recursively in ISR */
  166. }
  167. BaseType_t higher_task_woken = false;
  168. xSemaphoreGiveFromISR(h, &higher_task_woken);
  169. if (higher_task_woken) {
  170. portYIELD_FROM_ISR();
  171. }
  172. } else {
  173. if (mutex_type == queueQUEUE_TYPE_RECURSIVE_MUTEX) {
  174. xSemaphoreGiveRecursive(h);
  175. } else {
  176. xSemaphoreGive(h);
  177. }
  178. }
  179. }
  180. void IRAM_ATTR _lock_release(_lock_t *lock) {
  181. lock_release_generic(lock, queueQUEUE_TYPE_MUTEX);
  182. }
  183. void IRAM_ATTR _lock_release_recursive(_lock_t *lock) {
  184. lock_release_generic(lock, queueQUEUE_TYPE_RECURSIVE_MUTEX);
  185. }