os_mutex.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  1. /*
  2. * Copyright (c) 2021, Meco Jianting Man <jiantingman@foxmail.com>
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2020-10-12 Meco Man first version
  9. */
  10. /*
  11. *********************************************************************************************************
  12. * uC/OS-II
  13. * The Real-Time Kernel
  14. *
  15. * Copyright 1992-2020 Silicon Laboratories Inc. www.silabs.com
  16. *
  17. * SPDX-License-Identifier: APACHE-2.0
  18. *
  19. * This software is subject to an open source license and is distributed by
  20. * Silicon Laboratories Inc. pursuant to the terms of the Apache License,
  21. * Version 2.0 available at www.apache.org/licenses/LICENSE-2.0.
  22. *
  23. *********************************************************************************************************
  24. */
  25. /*
  26. *********************************************************************************************************
  27. *
  28. * MUTUAL EXCLUSION SEMAPHORE MANAGEMENT
  29. *
  30. * Filename : os_mutex.c
  31. * Version : V2.93.00
  32. *********************************************************************************************************
  33. */
  34. #include "ucos_ii.h"
  35. #if OS_MUTEX_EN > 0u
  36. /*
  37. *********************************************************************************************************
  38. * ACCEPT MUTUAL EXCLUSION SEMAPHORE
  39. *
  40. * Description: This function checks the mutual exclusion semaphore to see if a resource is available.
  41. * Unlike OSMutexPend(), OSMutexAccept() does not suspend the calling task if the resource is
  42. * not available or the event did not occur.
  43. *
  44. * Arguments : pevent is a pointer to the event control block
  45. *
  46. * perr is a pointer to an error code which will be returned to your application:
  47. * OS_ERR_NONE if the call was successful.
  48. * OS_ERR_EVENT_TYPE if 'pevent' is not a pointer to a mutex
  49. * OS_ERR_PEVENT_NULL 'pevent' is a NULL pointer
  50. * OS_ERR_PEND_ISR if you called this function from an ISR
  51. * - OS_ERR_PCP_LOWER If the priority of the task that owns the Mutex is
  52. * HIGHER (i.e. a lower number) than the PCP. This error
  53. * indicates that you did not set the PCP higher (lower
  54. * number) than ALL the tasks that compete for the Mutex.
  55. * Unfortunately, this is something that could not be
  56. * detected when the Mutex is created because we don't know
  57. * what tasks will be using the Mutex.
  58. *
  59. * Returns : == OS_TRUE if the resource is available, the mutual exclusion semaphore is acquired
  60. * == OS_FALSE a) if the resource is not available
  61. * b) you didn't pass a pointer to a mutual exclusion semaphore
  62. * c) you called this function from an ISR
  63. *
  64. * Warning(s) : This function CANNOT be called from an ISR because mutual exclusion semaphores are
  65. * intended to be used by tasks only.
  66. *********************************************************************************************************
  67. */
  68. #if OS_MUTEX_ACCEPT_EN > 0u
  69. BOOLEAN OSMutexAccept (OS_EVENT *pevent,
  70. INT8U *perr)
  71. {
  72. rt_mutex_t pmutex;
  73. #ifdef OS_SAFETY_CRITICAL
  74. if (perr == (INT8U *)0) {
  75. OS_SAFETY_CRITICAL_EXCEPTION();
  76. return (OS_FALSE);
  77. }
  78. #endif
  79. #if OS_ARG_CHK_EN > 0u
  80. if (pevent == (OS_EVENT *)0) { /* Validate 'pevent' */
  81. *perr = OS_ERR_PEVENT_NULL;
  82. return (OS_FALSE);
  83. }
  84. #endif
  85. pmutex = (rt_mutex_t)pevent->ipc_ptr;
  86. if (rt_object_get_type(&pmutex->parent.parent) /* Validate event block type */
  87. != RT_Object_Class_Mutex) {
  88. *perr = OS_ERR_EVENT_TYPE;
  89. return (OS_FALSE);
  90. }
  91. if (OSIntNesting > 0u) { /* Make sure it's not called from an ISR */
  92. *perr = OS_ERR_PEND_ISR;
  93. return (OS_FALSE);
  94. }
  95. *perr = OS_ERR_NONE;
  96. if(rt_mutex_take(pmutex, RT_WAITING_NO) == RT_EOK) {
  97. return (OS_TRUE);
  98. }
  99. return (OS_FALSE);
  100. }
  101. #endif
  102. /*
  103. *********************************************************************************************************
  104. * CREATE A MUTUAL EXCLUSION SEMAPHORE
  105. *
  106. * Description: This function creates a mutual exclusion semaphore.
  107. *
  108. * Arguments : prio is the priority to use when accessing the mutual exclusion semaphore. In
  109. * other words, when the semaphore is acquired and a higher priority task
  110. * attempts to obtain the semaphore then the priority of the task owning the
  111. * semaphore is raised to this priority. It is assumed that you will specify
  112. * a priority that is LOWER in value than ANY of the tasks competing for the
  113. * mutex. If the priority is specified as OS_PRIO_MUTEX_CEIL_DIS, then the
  114. * priority ceiling promotion is disabled. This way, the tasks accessing the
  115. * semaphore do not have their priority promoted.
  116. * 由于RT-Thread内核支持同一优先级含多个任务,因此prio在本兼容层无用,随便填什么都行
  117. *
  118. * perr is a pointer to an error code which will be returned to your application:
  119. * OS_ERR_NONE if the call was successful.
  120. * OS_ERR_CREATE_ISR if you attempted to create a MUTEX from an
  121. * ISR
  122. * OS_ERR_ILLEGAL_CREATE_RUN_TIME if you tried to create a mutex after
  123. * safety critical operation started.
  124. * - OS_ERR_PRIO_EXIST if a task at the priority ceiling priority
  125. * already exist.
  126. * OS_ERR_PEVENT_NULL No more event control blocks available.
  127. * - OS_ERR_PRIO_INVALID if the priority you specify is higher that
  128. * the maximum allowed (i.e. > OS_LOWEST_PRIO)
  129. *
  130. * Returns : != (void *)0 is a pointer to the event control clock (OS_EVENT) associated with the
  131. * created mutex.
  132. * == (void *)0 if an error is detected.
  133. *
  134. * Note(s) : 1) The LEAST significant 8 bits of '.OSEventCnt' hold the priority number of the task
  135. * owning the mutex or 0xFF if no task owns the mutex.
  136. *
  137. * 2) The MOST significant 8 bits of '.OSEventCnt' hold the priority number used to
  138. * reduce priority inversion or 0xFF (OS_PRIO_MUTEX_CEIL_DIS) if priority ceiling
  139. * promotion is disabled.
  140. *********************************************************************************************************
  141. */
  142. OS_EVENT *OSMutexCreate (INT8U prio,
  143. INT8U *perr)
  144. {
  145. OS_EVENT *pevent; /* 由于RT-Thread内核支持同一优先级含多个任务,因此prio在本兼容层无用*/
  146. #ifdef OS_SAFETY_CRITICAL
  147. if (perr == (INT8U *)0) {
  148. OS_SAFETY_CRITICAL_EXCEPTION();
  149. return ((OS_EVENT *)0);
  150. }
  151. #endif
  152. #ifdef OS_SAFETY_CRITICAL_IEC61508
  153. if (OSSafetyCriticalStartFlag == OS_TRUE) {
  154. OS_SAFETY_CRITICAL_EXCEPTION();
  155. *perr = OS_ERR_ILLEGAL_CREATE_RUN_TIME;
  156. return ((OS_EVENT *)0);
  157. }
  158. #endif
  159. if (OSIntNesting > 0u) { /* See if called from ISR ... */
  160. *perr = OS_ERR_CREATE_ISR; /* ... can't CREATE mutex from an ISR */
  161. return ((OS_EVENT *)0);
  162. }
  163. pevent = RT_KERNEL_MALLOC(sizeof(OS_EVENT)); /* Get next free event control block */
  164. if (pevent == (OS_EVENT *)0) { /* See if an ECB was available */
  165. *perr = OS_ERR_PEVENT_NULL; /* No more event control blocks */
  166. return (pevent);
  167. }
  168. pevent->ipc_ptr = (struct rt_ipc_object *)
  169. rt_mutex_create("uCOS-II", RT_IPC_FLAG_PRIO);
  170. if(pevent->ipc_ptr == 0) {
  171. RT_KERNEL_FREE(pevent);
  172. *perr = OS_ERR_PEVENT_NULL;
  173. return ((OS_EVENT *)0);
  174. }
  175. *perr = OS_ERR_NONE;
  176. return (pevent);
  177. }
  178. /*
  179. *********************************************************************************************************
  180. * CREATE A MUTUAL EXCLUSION SEMAPHORE
  181. * 额外实现OSMutexCreateEx()函数,该函数并不在uCOS-II原版的函数中,OSMutexCreate()函数中第一个
  182. * 参数prio在兼容层中没有任何意义,因此该函数将OSMutexCreate()函数中的第一个参数略去,以方便用户
  183. * 使用。原因是由于uCOS-II的实现方式过于落后,不支持相同任务在同一优先级。
  184. * 推荐用户使用这个API
  185. *********************************************************************************************************
  186. */
  187. OS_EVENT *OSMutexCreateEx (INT8U *perr)
  188. {
  189. return OSMutexCreate(0,perr);
  190. }
  191. /*
  192. *********************************************************************************************************
  193. * DELETE A MUTEX
  194. *
  195. * Description: This function deletes a mutual exclusion semaphore and readies all tasks pending on the it.
  196. *
  197. * Arguments : pevent is a pointer to the event control block associated with the desired mutex.
  198. *
  199. * opt determines delete options as follows:
  200. * opt == OS_DEL_NO_PEND Delete mutex ONLY if no task pending
  201. * opt == OS_DEL_ALWAYS Deletes the mutex even if tasks are waiting.
  202. * In this case, all the tasks pending will be readied.
  203. *
  204. * perr is a pointer to an error code that can contain one of the following values:
  205. * OS_ERR_NONE The call was successful and the mutex was deleted
  206. * OS_ERR_DEL_ISR If you attempted to delete the MUTEX from an ISR
  207. * OS_ERR_INVALID_OPT An invalid option was specified
  208. * OS_ERR_ILLEGAL_DEL_RUN_TIME If you tried to delete a mutex after safety
  209. * critical operation started.
  210. * OS_ERR_TASK_WAITING One or more tasks were waiting on the mutex
  211. * OS_ERR_EVENT_TYPE If you didn't pass a pointer to a mutex
  212. * OS_ERR_PEVENT_NULL If 'pevent' is a NULL pointer.
  213. *
  214. * Returns : pevent upon error
  215. * (OS_EVENT *)0 if the mutex was successfully deleted.
  216. *
  217. * Note(s) : 1) This function must be used with care. Tasks that would normally expect the presence of
  218. * the mutex MUST check the return code of OSMutexPend().
  219. *
  220. * 2) This call can potentially disable interrupts for a long time. The interrupt disable
  221. * time is directly proportional to the number of tasks waiting on the mutex.
  222. *
  223. * 3) Because ALL tasks pending on the mutex will be readied, you MUST be careful because the
  224. * resource(s) will no longer be guarded by the mutex.
  225. *
  226. * 4) IMPORTANT: In the 'OS_DEL_ALWAYS' case, we assume that the owner of the Mutex (if there
  227. * is one) is ready-to-run and is thus NOT pending on another kernel object or
  228. * has delayed itself. In other words, if a task owns the mutex being deleted,
  229. * that task will be made ready-to-run at its original priority.
  230. *********************************************************************************************************
  231. */
  232. #if OS_MUTEX_DEL_EN > 0u
  233. OS_EVENT *OSMutexDel (OS_EVENT *pevent,
  234. INT8U opt,
  235. INT8U *perr)
  236. {
  237. OS_EVENT *pevent_return;
  238. rt_mutex_t pmutex;
  239. #ifdef OS_SAFETY_CRITICAL
  240. if (perr == (INT8U *)0) {
  241. OS_SAFETY_CRITICAL_EXCEPTION();
  242. return ((OS_EVENT *)0);
  243. }
  244. #endif
  245. #ifdef OS_SAFETY_CRITICAL_IEC61508
  246. if (OSSafetyCriticalStartFlag == OS_TRUE) {
  247. OS_SAFETY_CRITICAL_EXCEPTION();
  248. *perr = OS_ERR_ILLEGAL_DEL_RUN_TIME;
  249. return ((OS_EVENT *)0);
  250. }
  251. #endif
  252. #if OS_ARG_CHK_EN > 0u
  253. if (pevent == (OS_EVENT *)0) { /* Validate 'pevent' */
  254. *perr = OS_ERR_PEVENT_NULL;
  255. return (pevent);
  256. }
  257. #endif
  258. pmutex = (rt_mutex_t)pevent->ipc_ptr;
  259. if (rt_object_get_type(&pmutex->parent.parent) /* Validate event block type */
  260. != RT_Object_Class_Mutex) {
  261. *perr = OS_ERR_EVENT_TYPE;
  262. return (pevent);
  263. }
  264. if (OSIntNesting > 0u) { /* See if called from ISR ... */
  265. *perr = OS_ERR_DEL_ISR; /* ... can't DELETE from an ISR */
  266. return (pevent);
  267. }
  268. switch (opt) {
  269. case OS_DEL_NO_PEND: /* DELETE MUTEX ONLY IF NO TASK WAITING --- */
  270. if(rt_list_isempty(&(pmutex->parent.suspend_thread))) { /* 若没有线程等待信号量 */
  271. rt_mutex_delete(pmutex); /* invoke RT-Thread API */
  272. RT_KERNEL_FREE(pevent);
  273. *perr = OS_ERR_NONE;
  274. pevent_return = (OS_EVENT *)0;
  275. } else {
  276. *perr = OS_ERR_TASK_WAITING;
  277. pevent_return = pevent;
  278. }
  279. break;
  280. case OS_DEL_ALWAYS: /* ALWAYS DELETE THE MUTEX ---------------- */
  281. rt_mutex_delete(pmutex); /* invoke RT-Thread API */
  282. RT_KERNEL_FREE(pevent);
  283. *perr = OS_ERR_NONE;
  284. pevent_return = (OS_EVENT *)0;
  285. break;
  286. default:
  287. *perr = OS_ERR_INVALID_OPT;
  288. pevent_return = pevent;
  289. break;
  290. }
  291. return (pevent_return);
  292. }
  293. #endif
  294. /*
  295. *********************************************************************************************************
  296. * PEND ON MUTUAL EXCLUSION SEMAPHORE
  297. *
  298. * Description: This function waits for a mutual exclusion semaphore.
  299. *
  300. * Arguments : pevent is a pointer to the event control block associated with the desired
  301. * mutex.
  302. *
  303. * timeout is an optional timeout period (in clock ticks). If non-zero, your task will
  304. * wait for the resource up to the amount of time specified by this argument.
  305. * If you specify 0, however, your task will wait forever at the specified
  306. * mutex or, until the resource becomes available.
  307. *
  308. * perr is a pointer to where an error message will be deposited. Possible error
  309. * messages are:
  310. * OS_ERR_NONE The call was successful and your task owns the mutex
  311. * OS_ERR_TIMEOUT The mutex was not available within the specified 'timeout'.
  312. * OS_ERR_PEND_ABORT The wait on the mutex was aborted.
  313. * OS_ERR_EVENT_TYPE If you didn't pass a pointer to a mutex
  314. * OS_ERR_PEVENT_NULL 'pevent' is a NULL pointer
  315. * OS_ERR_PEND_ISR If you called this function from an ISR and the result
  316. * would lead to a suspension.
  317. * - OS_ERR_PCP_LOWER If the priority of the task that owns the Mutex is
  318. * HIGHER (i.e. a lower number) than the PCP. This error
  319. * indicates that you did not set the PCP higher (lower
  320. * number) than ALL the tasks that compete for the Mutex.
  321. * Unfortunately, this is something that could not be
  322. * detected when the Mutex is created because we don't know
  323. * what tasks will be using the Mutex.
  324. * OS_ERR_PEND_LOCKED If you called this function when the scheduler is locked
  325. *
  326. * Returns : none
  327. *
  328. * Note(s) : 1) The task that owns the Mutex MUST NOT pend on any other event while it owns the mutex.
  329. *
  330. * 2) You MUST NOT change the priority of the task that owns the mutex
  331. *********************************************************************************************************
  332. */
  333. void OSMutexPend (OS_EVENT *pevent,
  334. INT32U timeout,
  335. INT8U *perr)
  336. {
  337. rt_mutex_t pmutex;
  338. rt_err_t rt_err;
  339. #if OS_CRITICAL_METHOD == 3u /* Allocate storage for CPU status register */
  340. OS_CPU_SR cpu_sr = 0u;
  341. #endif
  342. #ifdef OS_SAFETY_CRITICAL
  343. if (perr == (INT8U *)0) {
  344. OS_SAFETY_CRITICAL_EXCEPTION();
  345. return;
  346. }
  347. #endif
  348. #if OS_ARG_CHK_EN > 0u
  349. if (pevent == (OS_EVENT *)0) { /* Validate 'pevent' */
  350. *perr = OS_ERR_PEVENT_NULL;
  351. return;
  352. }
  353. #endif
  354. pmutex = (rt_mutex_t)pevent->ipc_ptr;
  355. if (rt_object_get_type(&pmutex->parent.parent) /* Validate event block type */
  356. != RT_Object_Class_Mutex) {
  357. *perr = OS_ERR_EVENT_TYPE;
  358. return;
  359. }
  360. if (OSIntNesting > 0u) { /* See if called from ISR ... */
  361. *perr = OS_ERR_PEND_ISR; /* ... can't PEND from an ISR */
  362. return;
  363. }
  364. if (OSLockNesting > 0u) { /* See if called with scheduler locked ... */
  365. *perr = OS_ERR_PEND_LOCKED; /* ... can't PEND when locked */
  366. return;
  367. }
  368. OS_ENTER_CRITICAL();
  369. OSTCBCur->OSTCBStat |= OS_STAT_MUTEX; /* Mutex not available, pend current task */
  370. OSTCBCur->OSTCBStatPend = OS_STAT_PEND_OK;
  371. #ifndef PKG_USING_UCOSII_WRAPPER_TINY
  372. OSTCBCur->OSTCBDly = timeout; /* Store timeout in current task's TCB */
  373. OSTCBCur->OSTCBEventPtr = pevent;
  374. #endif
  375. OS_EXIT_CRITICAL();
  376. if(timeout) { /* 0为永久等待 */
  377. rt_err = rt_mutex_take(pmutex, timeout);
  378. OS_ENTER_CRITICAL();
  379. if (rt_err == RT_EOK) {
  380. OSTCBCur->OSTCBStatPend = OS_STAT_PEND_OK;
  381. } else if(OSTCBCur->OSTCBStatPend == OS_STAT_PEND_ABORT) {
  382. OSTCBCur->OSTCBStatPend = OS_STAT_PEND_ABORT;
  383. } else {
  384. OSTCBCur->OSTCBStatPend = OS_STAT_PEND_TO;
  385. }
  386. }else {
  387. rt_mutex_take(pmutex, RT_WAITING_FOREVER);
  388. OS_ENTER_CRITICAL();
  389. if(OSTCBCur->OSTCBStatPend == OS_STAT_PEND_ABORT) {
  390. OSTCBCur->OSTCBStatPend = OS_STAT_PEND_ABORT;
  391. }else {
  392. OSTCBCur->OSTCBStatPend = OS_STAT_PEND_OK;
  393. }
  394. }
  395. switch (OSTCBCur->OSTCBStatPend) { /* See if we timed-out or aborted */
  396. case OS_STAT_PEND_OK:
  397. *perr = OS_ERR_NONE;
  398. break;
  399. case OS_STAT_PEND_ABORT:
  400. *perr = OS_ERR_PEND_ABORT; /* Indicate that we aborted getting mutex */
  401. break;
  402. case OS_STAT_PEND_TO:
  403. default:
  404. *perr = OS_ERR_TIMEOUT; /* Indicate that we didn't get mutex within TO */
  405. break;
  406. }
  407. OSTCBCur->OSTCBStat = OS_STAT_RDY; /* Set task status to ready */
  408. OSTCBCur->OSTCBStatPend = OS_STAT_PEND_OK; /* Clear pend status */
  409. #ifndef PKG_USING_UCOSII_WRAPPER_TINY
  410. OSTCBCur->OSTCBEventPtr = (OS_EVENT *)0; /* Clear event pointers */
  411. #endif
  412. OS_EXIT_CRITICAL();
  413. }
  414. /*
  415. *********************************************************************************************************
  416. * POST TO A MUTUAL EXCLUSION SEMAPHORE
  417. *
  418. * Description: This function signals a mutual exclusion semaphore
  419. *
  420. * Arguments : pevent is a pointer to the event control block associated with the desired
  421. * mutex.
  422. *
  423. * Returns : OS_ERR_NONE The call was successful and the mutex was signaled.
  424. * OS_ERR_EVENT_TYPE If you didn't pass a pointer to a mutex
  425. * OS_ERR_PEVENT_NULL 'pevent' is a NULL pointer
  426. * OS_ERR_POST_ISR Attempted to post from an ISR (not valid for MUTEXes)
  427. * OS_ERR_NOT_MUTEX_OWNER The task that did the post is NOT the owner of the MUTEX.
  428. * - OS_ERR_PCP_LOWER If the priority of the new task that owns the Mutex is
  429. * HIGHER (i.e. a lower number) than the PCP. This error
  430. * indicates that you did not set the PCP higher (lower
  431. * number) than ALL the tasks that compete for the Mutex.
  432. * Unfortunately, this is something that could not be
  433. * detected when the Mutex is created because we don't know
  434. * what tasks will be using the Mutex.
  435. *********************************************************************************************************
  436. */
  437. INT8U OSMutexPost (OS_EVENT *pevent)
  438. {
  439. rt_mutex_t pmutex;
  440. #if OS_CRITICAL_METHOD == 3u /* Allocate storage for CPU status register */
  441. OS_CPU_SR cpu_sr = 0u;
  442. #endif
  443. if (OSIntNesting > 0u) { /* See if called from ISR ... */
  444. return (OS_ERR_POST_ISR); /* ... can't POST mutex from an ISR */
  445. }
  446. #if OS_ARG_CHK_EN > 0u
  447. if (pevent == (OS_EVENT *)0) { /* Validate 'pevent' */
  448. return (OS_ERR_PEVENT_NULL);
  449. }
  450. #endif
  451. pmutex = (rt_mutex_t)pevent->ipc_ptr;
  452. if (rt_object_get_type(&pmutex->parent.parent) /* Validate event block type */
  453. != RT_Object_Class_Mutex) {
  454. return (OS_ERR_EVENT_TYPE);
  455. }
  456. OS_ENTER_CRITICAL();
  457. if (OSTCBCur != (OS_TCB *)pmutex->owner) { /* See if posting task owns the MUTEX */
  458. OS_EXIT_CRITICAL();
  459. return (OS_ERR_NOT_MUTEX_OWNER);
  460. }
  461. OS_EXIT_CRITICAL();
  462. rt_mutex_release(pmutex); /* invoke rt-thread API */
  463. return (OS_ERR_NONE);
  464. }
  465. /*
  466. *********************************************************************************************************
  467. * QUERY A MUTUAL EXCLUSION SEMAPHORE
  468. *
  469. * Description: This function obtains information about a mutex
  470. *
  471. * Arguments : pevent is a pointer to the event control block associated with the desired mutex
  472. *
  473. * p_mutex_data is a pointer to a structure that will contain information about the mutex
  474. *
  475. * Returns : OS_ERR_NONE The call was successful and the message was sent
  476. * OS_ERR_QUERY_ISR If you called this function from an ISR
  477. * OS_ERR_PEVENT_NULL If 'pevent' is a NULL pointer
  478. * OS_ERR_PDATA_NULL If 'p_mutex_data' is a NULL pointer
  479. * OS_ERR_EVENT_TYPE If you are attempting to obtain data from a non mutex.
  480. *********************************************************************************************************
  481. */
  482. #if OS_MUTEX_QUERY_EN > 0u
  483. INT8U OSMutexQuery (OS_EVENT *pevent,
  484. OS_MUTEX_DATA *p_mutex_data)
  485. {
  486. rt_mutex_t pmutex;
  487. #if OS_CRITICAL_METHOD == 3u /* Allocate storage for CPU status register */
  488. OS_CPU_SR cpu_sr = 0u;
  489. #endif
  490. if (OSIntNesting > 0u) { /* See if called from ISR ... */
  491. return (OS_ERR_QUERY_ISR); /* ... can't QUERY mutex from an ISR */
  492. }
  493. #if OS_ARG_CHK_EN > 0u
  494. if (pevent == (OS_EVENT *)0) { /* Validate 'pevent' */
  495. return (OS_ERR_PEVENT_NULL);
  496. }
  497. if (p_mutex_data == (OS_MUTEX_DATA *)0) { /* Validate 'p_mutex_data' */
  498. return (OS_ERR_PDATA_NULL);
  499. }
  500. #endif
  501. pmutex = (rt_mutex_t)pevent->ipc_ptr;
  502. if (rt_object_get_type(&pmutex->parent.parent) /* Validate event block type */
  503. != RT_Object_Class_Mutex) {
  504. return (OS_ERR_EVENT_TYPE);
  505. }
  506. OS_ENTER_CRITICAL();
  507. rt_memcpy(&p_mutex_data->OSMutex, pmutex, sizeof(struct rt_mutex));
  508. p_mutex_data->OSOwnerPrio = pmutex->owner->current_priority;
  509. OS_EXIT_CRITICAL();
  510. return (OS_ERR_NONE);
  511. }
  512. #endif /* OS_MUTEX_QUERY_EN */
  513. #endif /* OS_MUTEX_EN */