locks.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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/task.h"
  22. #include "freertos/portable.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. /* Initialize the given lock by allocating a new mutex semaphore
  40. as the _lock_t value.
  41. Called by _lock_init*, also called by _lock_acquire* to lazily initialize locks that might have
  42. been initialised (to zero only) before the RTOS scheduler started.
  43. */
  44. static void IRAM_ATTR lock_init_generic(_lock_t *lock, uint8_t mutex_type) {
  45. portENTER_CRITICAL(&lock_init_spinlock);
  46. if (xTaskGetSchedulerState() == taskSCHEDULER_NOT_STARTED) {
  47. /* nothing to do until the scheduler is running */
  48. portEXIT_CRITICAL(&lock_init_spinlock);
  49. return;
  50. }
  51. if (*lock) {
  52. /* Lock already initialised (either we didn't check earlier,
  53. or it got initialised while we were waiting for the
  54. spinlock.) */
  55. }
  56. else
  57. {
  58. /* Create a new semaphore
  59. this is a bit of an API violation, as we're calling the
  60. private function xQueueCreateMutex(x) directly instead of
  61. the xSemaphoreCreateMutex / xSemaphoreCreateRecursiveMutex
  62. wrapper functions...
  63. The better alternative would be to pass pointers to one of
  64. the two xSemaphoreCreate___Mutex functions, but as FreeRTOS
  65. implements these as macros instead of inline functions
  66. (*party like it's 1998!*) it's not possible to do this
  67. without writing wrappers. Doing it this way seems much less
  68. spaghetti-like.
  69. */
  70. xSemaphoreHandle new_sem = xQueueCreateMutex(mutex_type);
  71. if (!new_sem) {
  72. abort(); /* No more semaphores available or OOM */
  73. }
  74. *lock = (_lock_t)new_sem;
  75. }
  76. portEXIT_CRITICAL(&lock_init_spinlock);
  77. }
  78. void IRAM_ATTR _lock_init(_lock_t *lock) {
  79. *lock = 0; // In case lock's memory is uninitialized
  80. lock_init_generic(lock, queueQUEUE_TYPE_MUTEX);
  81. }
  82. void IRAM_ATTR _lock_init_recursive(_lock_t *lock) {
  83. *lock = 0; // In case lock's memory is uninitialized
  84. lock_init_generic(lock, queueQUEUE_TYPE_RECURSIVE_MUTEX);
  85. }
  86. /* Free the mutex semaphore pointed to by *lock, and zero it out.
  87. Note that FreeRTOS doesn't account for deleting mutexes while they
  88. are held, and neither do we... so take care not to delete newlib
  89. locks while they may be held by other tasks!
  90. Also, deleting a lock in this way will cause it to be lazily
  91. re-initialised if it is used again. Caller has to avoid doing
  92. this!
  93. */
  94. void IRAM_ATTR _lock_close(_lock_t *lock) {
  95. portENTER_CRITICAL(&lock_init_spinlock);
  96. if (*lock) {
  97. xSemaphoreHandle h = (xSemaphoreHandle)(*lock);
  98. #if (INCLUDE_xSemaphoreGetMutexHolder == 1)
  99. configASSERT(xSemaphoreGetMutexHolder(h) == NULL); /* mutex should not be held */
  100. #endif
  101. vSemaphoreDelete(h);
  102. *lock = 0;
  103. }
  104. portEXIT_CRITICAL(&lock_init_spinlock);
  105. }
  106. void _lock_close_recursive(_lock_t *lock) __attribute__((alias("_lock_close")));
  107. /* Acquire the mutex semaphore for lock. wait up to delay ticks.
  108. mutex_type is queueQUEUE_TYPE_RECURSIVE_MUTEX or queueQUEUE_TYPE_MUTEX
  109. */
  110. static int IRAM_ATTR lock_acquire_generic(_lock_t *lock, uint32_t delay, uint8_t mutex_type) {
  111. xSemaphoreHandle h = (xSemaphoreHandle)(*lock);
  112. if (!h) {
  113. if (xTaskGetSchedulerState() == taskSCHEDULER_NOT_STARTED) {
  114. return 0; /* locking is a no-op before scheduler is up, so this "succeeds" */
  115. }
  116. /* lazy initialise lock - might have had a static initializer in newlib (that we don't use),
  117. or _lock_init might have been called before the scheduler was running... */
  118. lock_init_generic(lock, mutex_type);
  119. h = (xSemaphoreHandle)(*lock);
  120. configASSERT(h != NULL);
  121. }
  122. BaseType_t success;
  123. if (!xPortCanYield()) {
  124. /* In ISR Context */
  125. if (mutex_type == queueQUEUE_TYPE_RECURSIVE_MUTEX) {
  126. abort(); /* recursive mutexes make no sense in ISR context */
  127. }
  128. BaseType_t higher_task_woken = false;
  129. success = xSemaphoreTakeFromISR(h, &higher_task_woken);
  130. if (!success && delay > 0) {
  131. abort(); /* Tried to block on mutex from ISR, couldn't... rewrite your program to avoid libc interactions in ISRs! */
  132. }
  133. if (higher_task_woken) {
  134. portYIELD_FROM_ISR();
  135. }
  136. }
  137. else {
  138. /* In task context */
  139. if (mutex_type == queueQUEUE_TYPE_RECURSIVE_MUTEX) {
  140. success = xSemaphoreTakeRecursive(h, delay);
  141. } else {
  142. success = xSemaphoreTake(h, delay);
  143. }
  144. }
  145. return (success == pdTRUE) ? 0 : -1;
  146. }
  147. void IRAM_ATTR _lock_acquire(_lock_t *lock) {
  148. lock_acquire_generic(lock, portMAX_DELAY, queueQUEUE_TYPE_MUTEX);
  149. }
  150. void IRAM_ATTR _lock_acquire_recursive(_lock_t *lock) {
  151. lock_acquire_generic(lock, portMAX_DELAY, queueQUEUE_TYPE_RECURSIVE_MUTEX);
  152. }
  153. int IRAM_ATTR _lock_try_acquire(_lock_t *lock) {
  154. return lock_acquire_generic(lock, 0, queueQUEUE_TYPE_MUTEX);
  155. }
  156. int IRAM_ATTR _lock_try_acquire_recursive(_lock_t *lock) {
  157. return lock_acquire_generic(lock, 0, queueQUEUE_TYPE_RECURSIVE_MUTEX);
  158. }
  159. /* Release the mutex semaphore for lock.
  160. mutex_type is queueQUEUE_TYPE_RECURSIVE_MUTEX or queueQUEUE_TYPE_MUTEX
  161. */
  162. static void IRAM_ATTR lock_release_generic(_lock_t *lock, uint8_t mutex_type) {
  163. xSemaphoreHandle h = (xSemaphoreHandle)(*lock);
  164. if (h == NULL) {
  165. /* This is probably because the scheduler isn't running yet,
  166. or the scheduler just started running and some code was
  167. "holding" a not-yet-initialised lock... */
  168. return;
  169. }
  170. if (!xPortCanYield()) {
  171. if (mutex_type == queueQUEUE_TYPE_RECURSIVE_MUTEX) {
  172. abort(); /* indicates logic bug, it shouldn't be possible to lock recursively in ISR */
  173. }
  174. BaseType_t higher_task_woken = false;
  175. xSemaphoreGiveFromISR(h, &higher_task_woken);
  176. if (higher_task_woken) {
  177. portYIELD_FROM_ISR();
  178. }
  179. } else {
  180. if (mutex_type == queueQUEUE_TYPE_RECURSIVE_MUTEX) {
  181. xSemaphoreGiveRecursive(h);
  182. } else {
  183. xSemaphoreGive(h);
  184. }
  185. }
  186. }
  187. void IRAM_ATTR _lock_release(_lock_t *lock) {
  188. lock_release_generic(lock, queueQUEUE_TYPE_MUTEX);
  189. }
  190. void IRAM_ATTR _lock_release_recursive(_lock_t *lock) {
  191. lock_release_generic(lock, queueQUEUE_TYPE_RECURSIVE_MUTEX);
  192. }
  193. /* No-op function, used to force linking this file,
  194. instead of the dummy locks implementation from newlib.
  195. */
  196. void newlib_include_locks_impl(void)
  197. {
  198. }