rtx_mutex.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  1. /*
  2. * Copyright (c) 2013-2019 Arm Limited. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the License); you may
  7. * not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an AS IS BASIS, WITHOUT
  14. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. *
  18. * -----------------------------------------------------------------------------
  19. *
  20. * Project: CMSIS-RTOS RTX
  21. * Title: Mutex functions
  22. *
  23. * -----------------------------------------------------------------------------
  24. */
  25. #include "rtx_lib.h"
  26. // OS Runtime Object Memory Usage
  27. #if ((defined(OS_OBJ_MEM_USAGE) && (OS_OBJ_MEM_USAGE != 0)))
  28. osRtxObjectMemUsage_t osRtxMutexMemUsage \
  29. __attribute__((section(".data.os.mutex.obj"))) =
  30. { 0U, 0U, 0U };
  31. #endif
  32. // ==== Library functions ====
  33. /// Release Mutex list when owner Thread terminates.
  34. /// \param[in] mutex_list mutex list.
  35. void osRtxMutexOwnerRelease (os_mutex_t *mutex_list) {
  36. os_mutex_t *mutex;
  37. os_mutex_t *mutex_next;
  38. os_thread_t *thread;
  39. mutex = mutex_list;
  40. while (mutex != NULL) {
  41. mutex_next = mutex->owner_next;
  42. // Check if Mutex is Robust
  43. if ((mutex->attr & osMutexRobust) != 0U) {
  44. // Clear Lock counter
  45. mutex->lock = 0U;
  46. EvrRtxMutexReleased(mutex, 0U);
  47. // Check if Thread is waiting for a Mutex
  48. if (mutex->thread_list != NULL) {
  49. // Wakeup waiting Thread with highest Priority
  50. thread = osRtxThreadListGet(osRtxObject(mutex));
  51. osRtxThreadWaitExit(thread, (uint32_t)osOK, FALSE);
  52. // Thread is the new Mutex owner
  53. mutex->owner_thread = thread;
  54. mutex->owner_prev = NULL;
  55. mutex->owner_next = thread->mutex_list;
  56. if (thread->mutex_list != NULL) {
  57. thread->mutex_list->owner_prev = mutex;
  58. }
  59. thread->mutex_list = mutex;
  60. mutex->lock = 1U;
  61. EvrRtxMutexAcquired(mutex, 1U);
  62. }
  63. }
  64. mutex = mutex_next;
  65. }
  66. }
  67. /// Restore Mutex owner Thread priority.
  68. /// \param[in] mutex mutex object.
  69. /// \param[in] thread_wakeup thread wakeup object.
  70. void osRtxMutexOwnerRestore (const os_mutex_t *mutex, const os_thread_t *thread_wakeup) {
  71. const os_mutex_t *mutex0;
  72. os_thread_t *thread;
  73. os_thread_t *thread0;
  74. int8_t priority;
  75. // Restore owner Thread priority
  76. if ((mutex->attr & osMutexPrioInherit) != 0U) {
  77. thread = mutex->owner_thread;
  78. priority = thread->priority_base;
  79. mutex0 = thread->mutex_list;
  80. // Check Mutexes owned by Thread
  81. do {
  82. // Check Threads waiting for Mutex
  83. thread0 = mutex0->thread_list;
  84. if (thread0 == thread_wakeup) {
  85. // Skip thread that is waken-up
  86. thread0 = thread0->thread_next;
  87. }
  88. if ((thread0 != NULL) && (thread0->priority > priority)) {
  89. // Higher priority Thread is waiting for Mutex
  90. priority = thread0->priority;
  91. }
  92. mutex0 = mutex0->owner_next;
  93. } while (mutex0 != NULL);
  94. if (thread->priority != priority) {
  95. thread->priority = priority;
  96. osRtxThreadListSort(thread);
  97. }
  98. }
  99. }
  100. // ==== Service Calls ====
  101. /// Create and Initialize a Mutex object.
  102. /// \note API identical to osMutexNew
  103. static osMutexId_t svcRtxMutexNew (const osMutexAttr_t *attr) {
  104. os_mutex_t *mutex;
  105. uint32_t attr_bits;
  106. uint8_t flags;
  107. const char *name;
  108. // Process attributes
  109. if (attr != NULL) {
  110. name = attr->name;
  111. attr_bits = attr->attr_bits;
  112. //lint -e{9079} "conversion from pointer to void to pointer to other type" [MISRA Note 6]
  113. mutex = attr->cb_mem;
  114. if (mutex != NULL) {
  115. //lint -e(923) -e(9078) "cast from pointer to unsigned int" [MISRA Note 7]
  116. if ((((uint32_t)mutex & 3U) != 0U) || (attr->cb_size < sizeof(os_mutex_t))) {
  117. EvrRtxMutexError(NULL, osRtxErrorInvalidControlBlock);
  118. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  119. return NULL;
  120. }
  121. } else {
  122. if (attr->cb_size != 0U) {
  123. EvrRtxMutexError(NULL, osRtxErrorInvalidControlBlock);
  124. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  125. return NULL;
  126. }
  127. }
  128. } else {
  129. name = NULL;
  130. attr_bits = 0U;
  131. mutex = NULL;
  132. }
  133. // Allocate object memory if not provided
  134. if (mutex == NULL) {
  135. if (osRtxInfo.mpi.mutex != NULL) {
  136. //lint -e{9079} "conversion from pointer to void to pointer to other type" [MISRA Note 5]
  137. mutex = osRtxMemoryPoolAlloc(osRtxInfo.mpi.mutex);
  138. } else {
  139. //lint -e{9079} "conversion from pointer to void to pointer to other type" [MISRA Note 5]
  140. mutex = osRtxMemoryAlloc(osRtxInfo.mem.common, sizeof(os_mutex_t), 1U);
  141. }
  142. #if (defined(OS_OBJ_MEM_USAGE) && (OS_OBJ_MEM_USAGE != 0))
  143. if (mutex != NULL) {
  144. uint32_t used;
  145. osRtxMutexMemUsage.cnt_alloc++;
  146. used = osRtxMutexMemUsage.cnt_alloc - osRtxMutexMemUsage.cnt_free;
  147. if (osRtxMutexMemUsage.max_used < used) {
  148. osRtxMutexMemUsage.max_used = used;
  149. }
  150. }
  151. #endif
  152. flags = osRtxFlagSystemObject;
  153. } else {
  154. flags = 0U;
  155. }
  156. if (mutex != NULL) {
  157. // Initialize control block
  158. mutex->id = osRtxIdMutex;
  159. mutex->flags = flags;
  160. mutex->attr = (uint8_t)attr_bits;
  161. mutex->name = name;
  162. mutex->thread_list = NULL;
  163. mutex->owner_thread = NULL;
  164. mutex->owner_prev = NULL;
  165. mutex->owner_next = NULL;
  166. mutex->lock = 0U;
  167. EvrRtxMutexCreated(mutex, mutex->name);
  168. } else {
  169. EvrRtxMutexError(NULL, (int32_t)osErrorNoMemory);
  170. }
  171. return mutex;
  172. }
  173. /// Get name of a Mutex object.
  174. /// \note API identical to osMutexGetName
  175. static const char *svcRtxMutexGetName (osMutexId_t mutex_id) {
  176. os_mutex_t *mutex = osRtxMutexId(mutex_id);
  177. // Check parameters
  178. if ((mutex == NULL) || (mutex->id != osRtxIdMutex)) {
  179. EvrRtxMutexGetName(mutex, NULL);
  180. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  181. return NULL;
  182. }
  183. EvrRtxMutexGetName(mutex, mutex->name);
  184. return mutex->name;
  185. }
  186. /// Acquire a Mutex or timeout if it is locked.
  187. /// \note API identical to osMutexAcquire
  188. static osStatus_t svcRtxMutexAcquire (osMutexId_t mutex_id, uint32_t timeout) {
  189. os_mutex_t *mutex = osRtxMutexId(mutex_id);
  190. os_thread_t *thread;
  191. osStatus_t status;
  192. // Check running thread
  193. thread = osRtxThreadGetRunning();
  194. if (thread == NULL) {
  195. EvrRtxMutexError(mutex, osRtxErrorKernelNotRunning);
  196. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  197. return osError;
  198. }
  199. // Check parameters
  200. if ((mutex == NULL) || (mutex->id != osRtxIdMutex)) {
  201. EvrRtxMutexError(mutex, (int32_t)osErrorParameter);
  202. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  203. return osErrorParameter;
  204. }
  205. // Check if Mutex is not locked
  206. if (mutex->lock == 0U) {
  207. // Acquire Mutex
  208. mutex->owner_thread = thread;
  209. mutex->owner_prev = NULL;
  210. mutex->owner_next = thread->mutex_list;
  211. if (thread->mutex_list != NULL) {
  212. thread->mutex_list->owner_prev = mutex;
  213. }
  214. thread->mutex_list = mutex;
  215. mutex->lock = 1U;
  216. EvrRtxMutexAcquired(mutex, mutex->lock);
  217. status = osOK;
  218. } else {
  219. // Check if Mutex is recursive and running Thread is the owner
  220. if (((mutex->attr & osMutexRecursive) != 0U) && (mutex->owner_thread == thread)) {
  221. // Try to increment lock counter
  222. if (mutex->lock == osRtxMutexLockLimit) {
  223. EvrRtxMutexError(mutex, osRtxErrorMutexLockLimit);
  224. status = osErrorResource;
  225. } else {
  226. mutex->lock++;
  227. EvrRtxMutexAcquired(mutex, mutex->lock);
  228. status = osOK;
  229. }
  230. } else {
  231. // Check if timeout is specified
  232. if (timeout != 0U) {
  233. // Check if Priority inheritance protocol is enabled
  234. if ((mutex->attr & osMutexPrioInherit) != 0U) {
  235. // Raise priority of owner Thread if lower than priority of running Thread
  236. if (mutex->owner_thread->priority < thread->priority) {
  237. mutex->owner_thread->priority = thread->priority;
  238. osRtxThreadListSort(mutex->owner_thread);
  239. }
  240. }
  241. EvrRtxMutexAcquirePending(mutex, timeout);
  242. // Suspend current Thread
  243. if (osRtxThreadWaitEnter(osRtxThreadWaitingMutex, timeout)) {
  244. osRtxThreadListPut(osRtxObject(mutex), thread);
  245. } else {
  246. EvrRtxMutexAcquireTimeout(mutex);
  247. }
  248. status = osErrorTimeout;
  249. } else {
  250. EvrRtxMutexNotAcquired(mutex);
  251. status = osErrorResource;
  252. }
  253. }
  254. }
  255. return status;
  256. }
  257. /// Release a Mutex that was acquired by osMutexAcquire.
  258. /// \note API identical to osMutexRelease
  259. static osStatus_t svcRtxMutexRelease (osMutexId_t mutex_id) {
  260. os_mutex_t *mutex = osRtxMutexId(mutex_id);
  261. const os_mutex_t *mutex0;
  262. os_thread_t *thread;
  263. int8_t priority;
  264. // Check running thread
  265. thread = osRtxThreadGetRunning();
  266. if (thread == NULL) {
  267. EvrRtxMutexError(mutex, osRtxErrorKernelNotRunning);
  268. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  269. return osError;
  270. }
  271. // Check parameters
  272. if ((mutex == NULL) || (mutex->id != osRtxIdMutex)) {
  273. EvrRtxMutexError(mutex, (int32_t)osErrorParameter);
  274. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  275. return osErrorParameter;
  276. }
  277. // Check if Mutex is not locked
  278. if (mutex->lock == 0U) {
  279. EvrRtxMutexError(mutex, osRtxErrorMutexNotLocked);
  280. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  281. return osErrorResource;
  282. }
  283. // Check if running Thread is not the owner
  284. if (mutex->owner_thread != thread) {
  285. EvrRtxMutexError(mutex, osRtxErrorMutexNotOwned);
  286. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  287. return osErrorResource;
  288. }
  289. // Decrement Lock counter
  290. mutex->lock--;
  291. EvrRtxMutexReleased(mutex, mutex->lock);
  292. // Check Lock counter
  293. if (mutex->lock == 0U) {
  294. // Remove Mutex from Thread owner list
  295. if (mutex->owner_next != NULL) {
  296. mutex->owner_next->owner_prev = mutex->owner_prev;
  297. }
  298. if (mutex->owner_prev != NULL) {
  299. mutex->owner_prev->owner_next = mutex->owner_next;
  300. } else {
  301. thread->mutex_list = mutex->owner_next;
  302. }
  303. // Restore running Thread priority
  304. if ((mutex->attr & osMutexPrioInherit) != 0U) {
  305. priority = thread->priority_base;
  306. mutex0 = thread->mutex_list;
  307. // Check mutexes owned by running Thread
  308. while (mutex0 != NULL) {
  309. if ((mutex0->thread_list != NULL) && (mutex0->thread_list->priority > priority)) {
  310. // Higher priority Thread is waiting for Mutex
  311. priority = mutex0->thread_list->priority;
  312. }
  313. mutex0 = mutex0->owner_next;
  314. }
  315. thread->priority = priority;
  316. }
  317. // Check if Thread is waiting for a Mutex
  318. if (mutex->thread_list != NULL) {
  319. // Wakeup waiting Thread with highest Priority
  320. thread = osRtxThreadListGet(osRtxObject(mutex));
  321. osRtxThreadWaitExit(thread, (uint32_t)osOK, FALSE);
  322. // Thread is the new Mutex owner
  323. mutex->owner_thread = thread;
  324. mutex->owner_prev = NULL;
  325. mutex->owner_next = thread->mutex_list;
  326. if (thread->mutex_list != NULL) {
  327. thread->mutex_list->owner_prev = mutex;
  328. }
  329. thread->mutex_list = mutex;
  330. mutex->lock = 1U;
  331. EvrRtxMutexAcquired(mutex, 1U);
  332. }
  333. osRtxThreadDispatch(NULL);
  334. }
  335. return osOK;
  336. }
  337. /// Get Thread which owns a Mutex object.
  338. /// \note API identical to osMutexGetOwner
  339. static osThreadId_t svcRtxMutexGetOwner (osMutexId_t mutex_id) {
  340. os_mutex_t *mutex = osRtxMutexId(mutex_id);
  341. // Check parameters
  342. if ((mutex == NULL) || (mutex->id != osRtxIdMutex)) {
  343. EvrRtxMutexGetOwner(mutex, NULL);
  344. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  345. return NULL;
  346. }
  347. // Check if Mutex is not locked
  348. if (mutex->lock == 0U) {
  349. EvrRtxMutexGetOwner(mutex, NULL);
  350. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  351. return NULL;
  352. }
  353. EvrRtxMutexGetOwner(mutex, mutex->owner_thread);
  354. return mutex->owner_thread;
  355. }
  356. /// Delete a Mutex object.
  357. /// \note API identical to osMutexDelete
  358. static osStatus_t svcRtxMutexDelete (osMutexId_t mutex_id) {
  359. os_mutex_t *mutex = osRtxMutexId(mutex_id);
  360. const os_mutex_t *mutex0;
  361. os_thread_t *thread;
  362. int8_t priority;
  363. // Check parameters
  364. if ((mutex == NULL) || (mutex->id != osRtxIdMutex)) {
  365. EvrRtxMutexError(mutex, (int32_t)osErrorParameter);
  366. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  367. return osErrorParameter;
  368. }
  369. // Check if Mutex is locked
  370. if (mutex->lock != 0U) {
  371. thread = mutex->owner_thread;
  372. // Remove Mutex from Thread owner list
  373. if (mutex->owner_next != NULL) {
  374. mutex->owner_next->owner_prev = mutex->owner_prev;
  375. }
  376. if (mutex->owner_prev != NULL) {
  377. mutex->owner_prev->owner_next = mutex->owner_next;
  378. } else {
  379. thread->mutex_list = mutex->owner_next;
  380. }
  381. // Restore owner Thread priority
  382. if ((mutex->attr & osMutexPrioInherit) != 0U) {
  383. priority = thread->priority_base;
  384. mutex0 = thread->mutex_list;
  385. // Check Mutexes owned by Thread
  386. while (mutex0 != NULL) {
  387. if ((mutex0->thread_list != NULL) && (mutex0->thread_list->priority > priority)) {
  388. // Higher priority Thread is waiting for Mutex
  389. priority = mutex0->thread_list->priority;
  390. }
  391. mutex0 = mutex0->owner_next;
  392. }
  393. if (thread->priority != priority) {
  394. thread->priority = priority;
  395. osRtxThreadListSort(thread);
  396. }
  397. }
  398. // Unblock waiting threads
  399. while (mutex->thread_list != NULL) {
  400. thread = osRtxThreadListGet(osRtxObject(mutex));
  401. osRtxThreadWaitExit(thread, (uint32_t)osErrorResource, FALSE);
  402. }
  403. osRtxThreadDispatch(NULL);
  404. }
  405. // Mark object as invalid
  406. mutex->id = osRtxIdInvalid;
  407. // Free object memory
  408. if ((mutex->flags & osRtxFlagSystemObject) != 0U) {
  409. if (osRtxInfo.mpi.mutex != NULL) {
  410. (void)osRtxMemoryPoolFree(osRtxInfo.mpi.mutex, mutex);
  411. } else {
  412. (void)osRtxMemoryFree(osRtxInfo.mem.common, mutex);
  413. }
  414. #if (defined(OS_OBJ_MEM_USAGE) && (OS_OBJ_MEM_USAGE != 0))
  415. osRtxMutexMemUsage.cnt_free++;
  416. #endif
  417. }
  418. EvrRtxMutexDestroyed(mutex);
  419. return osOK;
  420. }
  421. // Service Calls definitions
  422. //lint ++flb "Library Begin" [MISRA Note 11]
  423. SVC0_1(MutexNew, osMutexId_t, const osMutexAttr_t *)
  424. SVC0_1(MutexGetName, const char *, osMutexId_t)
  425. SVC0_2(MutexAcquire, osStatus_t, osMutexId_t, uint32_t)
  426. SVC0_1(MutexRelease, osStatus_t, osMutexId_t)
  427. SVC0_1(MutexGetOwner, osThreadId_t, osMutexId_t)
  428. SVC0_1(MutexDelete, osStatus_t, osMutexId_t)
  429. //lint --flb "Library End"
  430. // ==== Public API ====
  431. /// Create and Initialize a Mutex object.
  432. osMutexId_t osMutexNew (const osMutexAttr_t *attr) {
  433. osMutexId_t mutex_id;
  434. EvrRtxMutexNew(attr);
  435. if (IsIrqMode() || IsIrqMasked()) {
  436. EvrRtxMutexError(NULL, (int32_t)osErrorISR);
  437. mutex_id = NULL;
  438. } else {
  439. mutex_id = __svcMutexNew(attr);
  440. }
  441. return mutex_id;
  442. }
  443. /// Get name of a Mutex object.
  444. const char *osMutexGetName (osMutexId_t mutex_id) {
  445. const char *name;
  446. if (IsIrqMode() || IsIrqMasked()) {
  447. EvrRtxMutexGetName(mutex_id, NULL);
  448. name = NULL;
  449. } else {
  450. name = __svcMutexGetName(mutex_id);
  451. }
  452. return name;
  453. }
  454. /// Acquire a Mutex or timeout if it is locked.
  455. osStatus_t osMutexAcquire (osMutexId_t mutex_id, uint32_t timeout) {
  456. osStatus_t status;
  457. EvrRtxMutexAcquire(mutex_id, timeout);
  458. if (IsIrqMode() || IsIrqMasked()) {
  459. EvrRtxMutexError(mutex_id, (int32_t)osErrorISR);
  460. status = osErrorISR;
  461. } else {
  462. status = __svcMutexAcquire(mutex_id, timeout);
  463. }
  464. return status;
  465. }
  466. /// Release a Mutex that was acquired by \ref osMutexAcquire.
  467. osStatus_t osMutexRelease (osMutexId_t mutex_id) {
  468. osStatus_t status;
  469. EvrRtxMutexRelease(mutex_id);
  470. if (IsIrqMode() || IsIrqMasked()) {
  471. EvrRtxMutexError(mutex_id, (int32_t)osErrorISR);
  472. status = osErrorISR;
  473. } else {
  474. status = __svcMutexRelease(mutex_id);
  475. }
  476. return status;
  477. }
  478. /// Get Thread which owns a Mutex object.
  479. osThreadId_t osMutexGetOwner (osMutexId_t mutex_id) {
  480. osThreadId_t thread;
  481. if (IsIrqMode() || IsIrqMasked()) {
  482. EvrRtxMutexGetOwner(mutex_id, NULL);
  483. thread = NULL;
  484. } else {
  485. thread = __svcMutexGetOwner(mutex_id);
  486. }
  487. return thread;
  488. }
  489. /// Delete a Mutex object.
  490. osStatus_t osMutexDelete (osMutexId_t mutex_id) {
  491. osStatus_t status;
  492. EvrRtxMutexDelete(mutex_id);
  493. if (IsIrqMode() || IsIrqMasked()) {
  494. EvrRtxMutexError(mutex_id, (int32_t)osErrorISR);
  495. status = osErrorISR;
  496. } else {
  497. status = __svcMutexDelete(mutex_id);
  498. }
  499. return status;
  500. }