rtx_mutex.c 17 KB

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