os_mutex.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763
  1. /*
  2. *********************************************************************************************************
  3. * uC/OS-II
  4. * The Real-Time Kernel
  5. * MUTUAL EXCLUSION SEMAPHORE MANAGEMENT
  6. *
  7. * (c) Copyright 1992-2013, Micrium, Weston, FL
  8. * All Rights Reserved
  9. *
  10. * File : OS_MUTEX.C
  11. * By : Jean J. Labrosse
  12. * Version : V2.92.11
  13. *
  14. * LICENSING TERMS:
  15. * ---------------
  16. * uC/OS-II is provided in source form for FREE evaluation, for educational use or for peaceful research.
  17. * If you plan on using uC/OS-II in a commercial product you need to contact Micrium to properly license
  18. * its use in your product. We provide ALL the source code for your convenience and to help you experience
  19. * uC/OS-II. The fact that the source is provided does NOT mean that you can use it without paying a
  20. * licensing fee.
  21. *********************************************************************************************************
  22. */
  23. #define MICRIUM_SOURCE
  24. #ifndef OS_MASTER_FILE
  25. #include <ucos_ii.h>
  26. #endif
  27. #if OS_MUTEX_EN > 0u
  28. /*
  29. *********************************************************************************************************
  30. * LOCAL CONSTANTS
  31. *********************************************************************************************************
  32. */
  33. #define OS_MUTEX_KEEP_LOWER_8 ((INT16U)0x00FFu)
  34. #define OS_MUTEX_KEEP_UPPER_8 ((INT16U)0xFF00u)
  35. #define OS_MUTEX_AVAILABLE ((INT16U)0x00FFu)
  36. /*
  37. *********************************************************************************************************
  38. * LOCAL CONSTANTS
  39. *********************************************************************************************************
  40. */
  41. static void OSMutex_RdyAtPrio(OS_TCB *ptcb, INT8U prio);
  42. /*$PAGE*/
  43. /*
  44. *********************************************************************************************************
  45. * ACCEPT MUTUAL EXCLUSION SEMAPHORE
  46. *
  47. * Description: This function checks the mutual exclusion semaphore to see if a resource is available.
  48. * Unlike OSMutexPend(), OSMutexAccept() does not suspend the calling task if the resource is
  49. * not available or the event did not occur.
  50. *
  51. * Arguments : pevent is a pointer to the event control block
  52. *
  53. * perr is a pointer to an error code which will be returned to your application:
  54. * OS_ERR_NONE if the call was successful.
  55. * OS_ERR_EVENT_TYPE if 'pevent' is not a pointer to a mutex
  56. * OS_ERR_PEVENT_NULL 'pevent' is a NULL pointer
  57. * OS_ERR_PEND_ISR if you called this function from an ISR
  58. * OS_ERR_PCP_LOWER If the priority of the task that owns the Mutex is
  59. * HIGHER (i.e. a lower number) than the PCP. This error
  60. * indicates that you did not set the PCP higher (lower
  61. * number) than ALL the tasks that compete for the Mutex.
  62. * Unfortunately, this is something that could not be
  63. * detected when the Mutex is created because we don't know
  64. * what tasks will be using the Mutex.
  65. *
  66. * Returns : == OS_TRUE if the resource is available, the mutual exclusion semaphore is acquired
  67. * == OS_FALSE a) if the resource is not available
  68. * b) you didn't pass a pointer to a mutual exclusion semaphore
  69. * c) you called this function from an ISR
  70. *
  71. * Warning(s) : This function CANNOT be called from an ISR because mutual exclusion semaphores are
  72. * intended to be used by tasks only.
  73. *********************************************************************************************************
  74. */
  75. #if OS_MUTEX_ACCEPT_EN > 0u
  76. BOOLEAN OSMutexAccept (OS_EVENT *pevent,
  77. INT8U *perr)
  78. {
  79. INT8U pcp; /* Priority Ceiling Priority (PCP) */
  80. #if OS_CRITICAL_METHOD == 3u /* Allocate storage for CPU status register */
  81. OS_CPU_SR cpu_sr = 0u;
  82. #endif
  83. #ifdef OS_SAFETY_CRITICAL
  84. if (perr == (INT8U *)0) {
  85. OS_SAFETY_CRITICAL_EXCEPTION();
  86. return (OS_FALSE);
  87. }
  88. #endif
  89. #if OS_ARG_CHK_EN > 0u
  90. if (pevent == (OS_EVENT *)0) { /* Validate 'pevent' */
  91. *perr = OS_ERR_PEVENT_NULL;
  92. return (OS_FALSE);
  93. }
  94. #endif
  95. if (pevent->OSEventType != OS_EVENT_TYPE_MUTEX) { /* Validate event block type */
  96. *perr = OS_ERR_EVENT_TYPE;
  97. return (OS_FALSE);
  98. }
  99. if (OSIntNesting > 0u) { /* Make sure it's not called from an ISR */
  100. *perr = OS_ERR_PEND_ISR;
  101. return (OS_FALSE);
  102. }
  103. OS_ENTER_CRITICAL(); /* Get value (0 or 1) of Mutex */
  104. pcp = (INT8U)(pevent->OSEventCnt >> 8u); /* Get PCP from mutex */
  105. if ((pevent->OSEventCnt & OS_MUTEX_KEEP_LOWER_8) == OS_MUTEX_AVAILABLE) {
  106. pevent->OSEventCnt &= OS_MUTEX_KEEP_UPPER_8; /* Mask off LSByte (Acquire Mutex) */
  107. pevent->OSEventCnt |= OSTCBCur->OSTCBPrio; /* Save current task priority in LSByte */
  108. pevent->OSEventPtr = (void *)OSTCBCur; /* Link TCB of task owning Mutex */
  109. if ((pcp != OS_PRIO_MUTEX_CEIL_DIS) &&
  110. (OSTCBCur->OSTCBPrio <= pcp)) { /* PCP 'must' have a SMALLER prio ... */
  111. OS_EXIT_CRITICAL(); /* ... than current task! */
  112. *perr = OS_ERR_PCP_LOWER;
  113. } else {
  114. OS_EXIT_CRITICAL();
  115. *perr = OS_ERR_NONE;
  116. }
  117. return (OS_TRUE);
  118. }
  119. OS_EXIT_CRITICAL();
  120. *perr = OS_ERR_NONE;
  121. return (OS_FALSE);
  122. }
  123. #endif
  124. /*$PAGE*/
  125. /*
  126. *********************************************************************************************************
  127. * CREATE A MUTUAL EXCLUSION SEMAPHORE
  128. *
  129. * Description: This function creates a mutual exclusion semaphore.
  130. *
  131. * Arguments : prio is the priority to use when accessing the mutual exclusion semaphore. In
  132. * other words, when the semaphore is acquired and a higher priority task
  133. * attempts to obtain the semaphore then the priority of the task owning the
  134. * semaphore is raised to this priority. It is assumed that you will specify
  135. * a priority that is LOWER in value than ANY of the tasks competing for the
  136. * mutex. If the priority is specified as OS_PRIO_MUTEX_CEIL_DIS, then the
  137. * priority ceiling promotion is disabled. This way, the tasks accessing the
  138. * semaphore do not have their priority promoted.
  139. *
  140. * perr is a pointer to an error code which will be returned to your application:
  141. * OS_ERR_NONE if the call was successful.
  142. * OS_ERR_CREATE_ISR if you attempted to create a MUTEX from an ISR
  143. * OS_ERR_PRIO_EXIST if a task at the priority ceiling priority
  144. * already exist.
  145. * OS_ERR_PEVENT_NULL No more event control blocks available.
  146. * OS_ERR_PRIO_INVALID if the priority you specify is higher that the
  147. * maximum allowed (i.e. > OS_LOWEST_PRIO)
  148. *
  149. * Returns : != (void *)0 is a pointer to the event control clock (OS_EVENT) associated with the
  150. * created mutex.
  151. * == (void *)0 if an error is detected.
  152. *
  153. * Note(s) : 1) The LEAST significant 8 bits of '.OSEventCnt' hold the priority number of the task
  154. * owning the mutex or 0xFF if no task owns the mutex.
  155. *
  156. * 2) The MOST significant 8 bits of '.OSEventCnt' hold the priority number used to
  157. * reduce priority inversion or 0xFF (OS_PRIO_MUTEX_CEIL_DIS) if priority ceiling
  158. * promotion is disabled.
  159. *********************************************************************************************************
  160. */
  161. OS_EVENT *OSMutexCreate (INT8U prio,
  162. INT8U *perr)
  163. {
  164. OS_EVENT *pevent;
  165. #if OS_CRITICAL_METHOD == 3u /* Allocate storage for CPU status register */
  166. OS_CPU_SR cpu_sr = 0u;
  167. #endif
  168. #ifdef OS_SAFETY_CRITICAL
  169. if (perr == (INT8U *)0) {
  170. OS_SAFETY_CRITICAL_EXCEPTION();
  171. return ((OS_EVENT *)0);
  172. }
  173. #endif
  174. #ifdef OS_SAFETY_CRITICAL_IEC61508
  175. if (OSSafetyCriticalStartFlag == OS_TRUE) {
  176. OS_SAFETY_CRITICAL_EXCEPTION();
  177. return ((OS_EVENT *)0);
  178. }
  179. #endif
  180. #if OS_ARG_CHK_EN > 0u
  181. if (prio != OS_PRIO_MUTEX_CEIL_DIS) {
  182. if (prio >= OS_LOWEST_PRIO) { /* Validate PCP */
  183. *perr = OS_ERR_PRIO_INVALID;
  184. return ((OS_EVENT *)0);
  185. }
  186. }
  187. #endif
  188. if (OSIntNesting > 0u) { /* See if called from ISR ... */
  189. *perr = OS_ERR_CREATE_ISR; /* ... can't CREATE mutex from an ISR */
  190. return ((OS_EVENT *)0);
  191. }
  192. OS_ENTER_CRITICAL();
  193. if (prio != OS_PRIO_MUTEX_CEIL_DIS) {
  194. if (OSTCBPrioTbl[prio] != (OS_TCB *)0) { /* Mutex priority must not already exist */
  195. OS_EXIT_CRITICAL(); /* Task already exist at priority ... */
  196. *perr = OS_ERR_PRIO_EXIST; /* ... ceiling priority */
  197. return ((OS_EVENT *)0);
  198. }
  199. OSTCBPrioTbl[prio] = OS_TCB_RESERVED; /* Reserve the table entry */
  200. }
  201. pevent = OSEventFreeList; /* Get next free event control block */
  202. if (pevent == (OS_EVENT *)0) { /* See if an ECB was available */
  203. if (prio != OS_PRIO_MUTEX_CEIL_DIS) {
  204. OSTCBPrioTbl[prio] = (OS_TCB *)0; /* No, Release the table entry */
  205. }
  206. OS_EXIT_CRITICAL();
  207. *perr = OS_ERR_PEVENT_NULL; /* No more event control blocks */
  208. return (pevent);
  209. }
  210. OSEventFreeList = (OS_EVENT *)OSEventFreeList->OSEventPtr; /* Adjust the free list */
  211. OS_EXIT_CRITICAL();
  212. pevent->OSEventType = OS_EVENT_TYPE_MUTEX;
  213. pevent->OSEventCnt = (INT16U)((INT16U)prio << 8u) | OS_MUTEX_AVAILABLE; /* Resource is avail. */
  214. pevent->OSEventPtr = (void *)0; /* No task owning the mutex */
  215. #if OS_EVENT_NAME_EN > 0u
  216. pevent->OSEventName = (INT8U *)(void *)"?";
  217. #endif
  218. OS_EventWaitListInit(pevent);
  219. *perr = OS_ERR_NONE;
  220. return (pevent);
  221. }
  222. /*$PAGE*/
  223. /*
  224. *********************************************************************************************************
  225. * DELETE A MUTEX
  226. *
  227. * Description: This function deletes a mutual exclusion semaphore and readies all tasks pending on the it.
  228. *
  229. * Arguments : pevent is a pointer to the event control block associated with the desired mutex.
  230. *
  231. * opt determines delete options as follows:
  232. * opt == OS_DEL_NO_PEND Delete mutex ONLY if no task pending
  233. * opt == OS_DEL_ALWAYS Deletes the mutex even if tasks are waiting.
  234. * In this case, all the tasks pending will be readied.
  235. *
  236. * perr is a pointer to an error code that can contain one of the following values:
  237. * OS_ERR_NONE The call was successful and the mutex was deleted
  238. * OS_ERR_DEL_ISR If you attempted to delete the MUTEX from an ISR
  239. * OS_ERR_INVALID_OPT An invalid option was specified
  240. * OS_ERR_TASK_WAITING One or more tasks were waiting on the mutex
  241. * OS_ERR_EVENT_TYPE If you didn't pass a pointer to a mutex
  242. * OS_ERR_PEVENT_NULL If 'pevent' is a NULL pointer.
  243. *
  244. * Returns : pevent upon error
  245. * (OS_EVENT *)0 if the mutex was successfully deleted.
  246. *
  247. * Note(s) : 1) This function must be used with care. Tasks that would normally expect the presence of
  248. * the mutex MUST check the return code of OSMutexPend().
  249. *
  250. * 2) This call can potentially disable interrupts for a long time. The interrupt disable
  251. * time is directly proportional to the number of tasks waiting on the mutex.
  252. *
  253. * 3) Because ALL tasks pending on the mutex will be readied, you MUST be careful because the
  254. * resource(s) will no longer be guarded by the mutex.
  255. *
  256. * 4) IMPORTANT: In the 'OS_DEL_ALWAYS' case, we assume that the owner of the Mutex (if there
  257. * is one) is ready-to-run and is thus NOT pending on another kernel object or
  258. * has delayed itself. In other words, if a task owns the mutex being deleted,
  259. * that task will be made ready-to-run at its original priority.
  260. *********************************************************************************************************
  261. */
  262. #if OS_MUTEX_DEL_EN > 0u
  263. OS_EVENT *OSMutexDel (OS_EVENT *pevent,
  264. INT8U opt,
  265. INT8U *perr)
  266. {
  267. BOOLEAN tasks_waiting;
  268. OS_EVENT *pevent_return;
  269. INT8U pcp; /* Priority ceiling priority */
  270. INT8U prio;
  271. OS_TCB *ptcb;
  272. #if OS_CRITICAL_METHOD == 3u /* Allocate storage for CPU status register */
  273. OS_CPU_SR cpu_sr = 0u;
  274. #endif
  275. #ifdef OS_SAFETY_CRITICAL
  276. if (perr == (INT8U *)0) {
  277. OS_SAFETY_CRITICAL_EXCEPTION();
  278. return ((OS_EVENT *)0);
  279. }
  280. #endif
  281. #if OS_ARG_CHK_EN > 0u
  282. if (pevent == (OS_EVENT *)0) { /* Validate 'pevent' */
  283. *perr = OS_ERR_PEVENT_NULL;
  284. return (pevent);
  285. }
  286. #endif
  287. if (pevent->OSEventType != OS_EVENT_TYPE_MUTEX) { /* Validate event block type */
  288. *perr = OS_ERR_EVENT_TYPE;
  289. return (pevent);
  290. }
  291. if (OSIntNesting > 0u) { /* See if called from ISR ... */
  292. *perr = OS_ERR_DEL_ISR; /* ... can't DELETE from an ISR */
  293. return (pevent);
  294. }
  295. OS_ENTER_CRITICAL();
  296. if (pevent->OSEventGrp != 0u) { /* See if any tasks waiting on mutex */
  297. tasks_waiting = OS_TRUE; /* Yes */
  298. } else {
  299. tasks_waiting = OS_FALSE; /* No */
  300. }
  301. switch (opt) {
  302. case OS_DEL_NO_PEND: /* DELETE MUTEX ONLY IF NO TASK WAITING --- */
  303. if (tasks_waiting == OS_FALSE) {
  304. #if OS_EVENT_NAME_EN > 0u
  305. pevent->OSEventName = (INT8U *)(void *)"?";
  306. #endif
  307. pcp = (INT8U)(pevent->OSEventCnt >> 8u);
  308. if (pcp != OS_PRIO_MUTEX_CEIL_DIS) {
  309. OSTCBPrioTbl[pcp] = (OS_TCB *)0; /* Free up the PCP */
  310. }
  311. pevent->OSEventType = OS_EVENT_TYPE_UNUSED;
  312. pevent->OSEventPtr = OSEventFreeList; /* Return Event Control Block to free list */
  313. pevent->OSEventCnt = 0u;
  314. OSEventFreeList = pevent;
  315. OS_EXIT_CRITICAL();
  316. *perr = OS_ERR_NONE;
  317. pevent_return = (OS_EVENT *)0; /* Mutex has been deleted */
  318. } else {
  319. OS_EXIT_CRITICAL();
  320. *perr = OS_ERR_TASK_WAITING;
  321. pevent_return = pevent;
  322. }
  323. break;
  324. case OS_DEL_ALWAYS: /* ALWAYS DELETE THE MUTEX ---------------- */
  325. pcp = (INT8U)(pevent->OSEventCnt >> 8u); /* Get PCP of mutex */
  326. if (pcp != OS_PRIO_MUTEX_CEIL_DIS) {
  327. prio = (INT8U)(pevent->OSEventCnt & OS_MUTEX_KEEP_LOWER_8); /* Get owner's orig prio */
  328. ptcb = (OS_TCB *)pevent->OSEventPtr;
  329. if (ptcb != (OS_TCB *)0) { /* See if any task owns the mutex */
  330. if (ptcb->OSTCBPrio == pcp) { /* See if original prio was changed */
  331. OSMutex_RdyAtPrio(ptcb, prio); /* Yes, Restore the task's original prio */
  332. }
  333. }
  334. }
  335. while (pevent->OSEventGrp != 0u) { /* Ready ALL tasks waiting for mutex */
  336. (void)OS_EventTaskRdy(pevent, (void *)0, OS_STAT_MUTEX, OS_STAT_PEND_ABORT);
  337. }
  338. #if OS_EVENT_NAME_EN > 0u
  339. pevent->OSEventName = (INT8U *)(void *)"?";
  340. #endif
  341. pcp = (INT8U)(pevent->OSEventCnt >> 8u);
  342. if (pcp != OS_PRIO_MUTEX_CEIL_DIS) {
  343. OSTCBPrioTbl[pcp] = (OS_TCB *)0; /* Free up the PCP */
  344. }
  345. pevent->OSEventType = OS_EVENT_TYPE_UNUSED;
  346. pevent->OSEventPtr = OSEventFreeList; /* Return Event Control Block to free list */
  347. pevent->OSEventCnt = 0u;
  348. OSEventFreeList = pevent; /* Get next free event control block */
  349. OS_EXIT_CRITICAL();
  350. if (tasks_waiting == OS_TRUE) { /* Reschedule only if task(s) were waiting */
  351. OS_Sched(); /* Find highest priority task ready to run */
  352. }
  353. *perr = OS_ERR_NONE;
  354. pevent_return = (OS_EVENT *)0; /* Mutex has been deleted */
  355. break;
  356. default:
  357. OS_EXIT_CRITICAL();
  358. *perr = OS_ERR_INVALID_OPT;
  359. pevent_return = pevent;
  360. break;
  361. }
  362. return (pevent_return);
  363. }
  364. #endif
  365. /*$PAGE*/
  366. /*
  367. *********************************************************************************************************
  368. * PEND ON MUTUAL EXCLUSION SEMAPHORE
  369. *
  370. * Description: This function waits for a mutual exclusion semaphore.
  371. *
  372. * Arguments : pevent is a pointer to the event control block associated with the desired
  373. * mutex.
  374. *
  375. * timeout is an optional timeout period (in clock ticks). If non-zero, your task will
  376. * wait for the resource up to the amount of time specified by this argument.
  377. * If you specify 0, however, your task will wait forever at the specified
  378. * mutex or, until the resource becomes available.
  379. *
  380. * perr is a pointer to where an error message will be deposited. Possible error
  381. * messages are:
  382. * OS_ERR_NONE The call was successful and your task owns the mutex
  383. * OS_ERR_TIMEOUT The mutex was not available within the specified 'timeout'.
  384. * OS_ERR_PEND_ABORT The wait on the mutex was aborted.
  385. * OS_ERR_EVENT_TYPE If you didn't pass a pointer to a mutex
  386. * OS_ERR_PEVENT_NULL 'pevent' is a NULL pointer
  387. * OS_ERR_PEND_ISR If you called this function from an ISR and the result
  388. * would lead to a suspension.
  389. * OS_ERR_PCP_LOWER If the priority of the task that owns the Mutex is
  390. * HIGHER (i.e. a lower number) than the PCP. This error
  391. * indicates that you did not set the PCP higher (lower
  392. * number) than ALL the tasks that compete for the Mutex.
  393. * Unfortunately, this is something that could not be
  394. * detected when the Mutex is created because we don't know
  395. * what tasks will be using the Mutex.
  396. * OS_ERR_PEND_LOCKED If you called this function when the scheduler is locked
  397. *
  398. * Returns : none
  399. *
  400. * Note(s) : 1) The task that owns the Mutex MUST NOT pend on any other event while it owns the mutex.
  401. *
  402. * 2) You MUST NOT change the priority of the task that owns the mutex
  403. *********************************************************************************************************
  404. */
  405. void OSMutexPend (OS_EVENT *pevent,
  406. INT32U timeout,
  407. INT8U *perr)
  408. {
  409. INT8U pcp; /* Priority Ceiling Priority (PCP) */
  410. INT8U mprio; /* Mutex owner priority */
  411. BOOLEAN rdy; /* Flag indicating task was ready */
  412. OS_TCB *ptcb;
  413. OS_EVENT *pevent2;
  414. INT8U y;
  415. #if OS_CRITICAL_METHOD == 3u /* Allocate storage for CPU status register */
  416. OS_CPU_SR cpu_sr = 0u;
  417. #endif
  418. #ifdef OS_SAFETY_CRITICAL
  419. if (perr == (INT8U *)0) {
  420. OS_SAFETY_CRITICAL_EXCEPTION();
  421. return;
  422. }
  423. #endif
  424. #if OS_ARG_CHK_EN > 0u
  425. if (pevent == (OS_EVENT *)0) { /* Validate 'pevent' */
  426. *perr = OS_ERR_PEVENT_NULL;
  427. return;
  428. }
  429. #endif
  430. if (pevent->OSEventType != OS_EVENT_TYPE_MUTEX) { /* Validate event block type */
  431. *perr = OS_ERR_EVENT_TYPE;
  432. return;
  433. }
  434. if (OSIntNesting > 0u) { /* See if called from ISR ... */
  435. *perr = OS_ERR_PEND_ISR; /* ... can't PEND from an ISR */
  436. return;
  437. }
  438. if (OSLockNesting > 0u) { /* See if called with scheduler locked ... */
  439. *perr = OS_ERR_PEND_LOCKED; /* ... can't PEND when locked */
  440. return;
  441. }
  442. /*$PAGE*/
  443. OS_ENTER_CRITICAL();
  444. pcp = (INT8U)(pevent->OSEventCnt >> 8u); /* Get PCP from mutex */
  445. /* Is Mutex available? */
  446. if ((INT8U)(pevent->OSEventCnt & OS_MUTEX_KEEP_LOWER_8) == OS_MUTEX_AVAILABLE) {
  447. pevent->OSEventCnt &= OS_MUTEX_KEEP_UPPER_8; /* Yes, Acquire the resource */
  448. pevent->OSEventCnt |= OSTCBCur->OSTCBPrio; /* Save priority of owning task */
  449. pevent->OSEventPtr = (void *)OSTCBCur; /* Point to owning task's OS_TCB */
  450. if ((pcp != OS_PRIO_MUTEX_CEIL_DIS) &&
  451. (OSTCBCur->OSTCBPrio <= pcp)) { /* PCP 'must' have a SMALLER prio ... */
  452. OS_EXIT_CRITICAL(); /* ... than current task! */
  453. *perr = OS_ERR_PCP_LOWER;
  454. } else {
  455. OS_EXIT_CRITICAL();
  456. *perr = OS_ERR_NONE;
  457. }
  458. return;
  459. }
  460. if (pcp != OS_PRIO_MUTEX_CEIL_DIS) {
  461. mprio = (INT8U)(pevent->OSEventCnt & OS_MUTEX_KEEP_LOWER_8); /* Get priority of mutex owner */
  462. ptcb = (OS_TCB *)(pevent->OSEventPtr); /* Point to TCB of mutex owner */
  463. if (ptcb->OSTCBPrio > pcp) { /* Need to promote prio of owner?*/
  464. if (mprio > OSTCBCur->OSTCBPrio) {
  465. y = ptcb->OSTCBY;
  466. if ((OSRdyTbl[y] & ptcb->OSTCBBitX) != 0u) { /* See if mutex owner is ready */
  467. OSRdyTbl[y] &= (OS_PRIO)~ptcb->OSTCBBitX; /* Yes, Remove owner from Rdy ...*/
  468. if (OSRdyTbl[y] == 0u) { /* ... list at current prio */
  469. OSRdyGrp &= (OS_PRIO)~ptcb->OSTCBBitY;
  470. }
  471. rdy = OS_TRUE;
  472. } else {
  473. pevent2 = ptcb->OSTCBEventPtr;
  474. if (pevent2 != (OS_EVENT *)0) { /* Remove from event wait list */
  475. y = ptcb->OSTCBY;
  476. pevent2->OSEventTbl[y] &= (OS_PRIO)~ptcb->OSTCBBitX;
  477. if (pevent2->OSEventTbl[y] == 0u) {
  478. pevent2->OSEventGrp &= (OS_PRIO)~ptcb->OSTCBBitY;
  479. }
  480. }
  481. rdy = OS_FALSE; /* No */
  482. }
  483. ptcb->OSTCBPrio = pcp; /* Change owner task prio to PCP */
  484. #if OS_LOWEST_PRIO <= 63u
  485. ptcb->OSTCBY = (INT8U)( ptcb->OSTCBPrio >> 3u);
  486. ptcb->OSTCBX = (INT8U)( ptcb->OSTCBPrio & 0x07u);
  487. #else
  488. ptcb->OSTCBY = (INT8U)((INT8U)(ptcb->OSTCBPrio >> 4u) & 0xFFu);
  489. ptcb->OSTCBX = (INT8U)( ptcb->OSTCBPrio & 0x0Fu);
  490. #endif
  491. ptcb->OSTCBBitY = (OS_PRIO)(1uL << ptcb->OSTCBY);
  492. ptcb->OSTCBBitX = (OS_PRIO)(1uL << ptcb->OSTCBX);
  493. if (rdy == OS_TRUE) { /* If task was ready at owner's priority ...*/
  494. OSRdyGrp |= ptcb->OSTCBBitY; /* ... make it ready at new priority. */
  495. OSRdyTbl[ptcb->OSTCBY] |= ptcb->OSTCBBitX;
  496. } else {
  497. pevent2 = ptcb->OSTCBEventPtr;
  498. if (pevent2 != (OS_EVENT *)0) { /* Add to event wait list */
  499. pevent2->OSEventGrp |= ptcb->OSTCBBitY;
  500. pevent2->OSEventTbl[ptcb->OSTCBY] |= ptcb->OSTCBBitX;
  501. }
  502. }
  503. OSTCBPrioTbl[pcp] = ptcb;
  504. }
  505. }
  506. }
  507. OSTCBCur->OSTCBStat |= OS_STAT_MUTEX; /* Mutex not available, pend current task */
  508. OSTCBCur->OSTCBStatPend = OS_STAT_PEND_OK;
  509. OSTCBCur->OSTCBDly = timeout; /* Store timeout in current task's TCB */
  510. OS_EventTaskWait(pevent); /* Suspend task until event or timeout occurs */
  511. OS_EXIT_CRITICAL();
  512. OS_Sched(); /* Find next highest priority task ready */
  513. OS_ENTER_CRITICAL();
  514. switch (OSTCBCur->OSTCBStatPend) { /* See if we timed-out or aborted */
  515. case OS_STAT_PEND_OK:
  516. *perr = OS_ERR_NONE;
  517. break;
  518. case OS_STAT_PEND_ABORT:
  519. *perr = OS_ERR_PEND_ABORT; /* Indicate that we aborted getting mutex */
  520. break;
  521. case OS_STAT_PEND_TO:
  522. default:
  523. OS_EventTaskRemove(OSTCBCur, pevent);
  524. *perr = OS_ERR_TIMEOUT; /* Indicate that we didn't get mutex within TO */
  525. break;
  526. }
  527. OSTCBCur->OSTCBStat = OS_STAT_RDY; /* Set task status to ready */
  528. OSTCBCur->OSTCBStatPend = OS_STAT_PEND_OK; /* Clear pend status */
  529. OSTCBCur->OSTCBEventPtr = (OS_EVENT *)0; /* Clear event pointers */
  530. #if (OS_EVENT_MULTI_EN > 0u)
  531. OSTCBCur->OSTCBEventMultiPtr = (OS_EVENT **)0;
  532. #endif
  533. OS_EXIT_CRITICAL();
  534. }
  535. /*$PAGE*/
  536. /*
  537. *********************************************************************************************************
  538. * POST TO A MUTUAL EXCLUSION SEMAPHORE
  539. *
  540. * Description: This function signals a mutual exclusion semaphore
  541. *
  542. * Arguments : pevent is a pointer to the event control block associated with the desired
  543. * mutex.
  544. *
  545. * Returns : OS_ERR_NONE The call was successful and the mutex was signaled.
  546. * OS_ERR_EVENT_TYPE If you didn't pass a pointer to a mutex
  547. * OS_ERR_PEVENT_NULL 'pevent' is a NULL pointer
  548. * OS_ERR_POST_ISR Attempted to post from an ISR (not valid for MUTEXes)
  549. * OS_ERR_NOT_MUTEX_OWNER The task that did the post is NOT the owner of the MUTEX.
  550. * OS_ERR_PCP_LOWER If the priority of the new task that owns the Mutex is
  551. * HIGHER (i.e. a lower number) than the PCP. This error
  552. * indicates that you did not set the PCP higher (lower
  553. * number) than ALL the tasks that compete for the Mutex.
  554. * Unfortunately, this is something that could not be
  555. * detected when the Mutex is created because we don't know
  556. * what tasks will be using the Mutex.
  557. *********************************************************************************************************
  558. */
  559. INT8U OSMutexPost (OS_EVENT *pevent)
  560. {
  561. INT8U pcp; /* Priority ceiling priority */
  562. INT8U prio;
  563. #if OS_CRITICAL_METHOD == 3u /* Allocate storage for CPU status register */
  564. OS_CPU_SR cpu_sr = 0u;
  565. #endif
  566. if (OSIntNesting > 0u) { /* See if called from ISR ... */
  567. return (OS_ERR_POST_ISR); /* ... can't POST mutex from an ISR */
  568. }
  569. #if OS_ARG_CHK_EN > 0u
  570. if (pevent == (OS_EVENT *)0) { /* Validate 'pevent' */
  571. return (OS_ERR_PEVENT_NULL);
  572. }
  573. #endif
  574. if (pevent->OSEventType != OS_EVENT_TYPE_MUTEX) { /* Validate event block type */
  575. return (OS_ERR_EVENT_TYPE);
  576. }
  577. OS_ENTER_CRITICAL();
  578. pcp = (INT8U)(pevent->OSEventCnt >> 8u); /* Get priority ceiling priority of mutex */
  579. prio = (INT8U)(pevent->OSEventCnt & OS_MUTEX_KEEP_LOWER_8); /* Get owner's original priority */
  580. if (OSTCBCur != (OS_TCB *)pevent->OSEventPtr) { /* See if posting task owns the MUTEX */
  581. OS_EXIT_CRITICAL();
  582. return (OS_ERR_NOT_MUTEX_OWNER);
  583. }
  584. if (pcp != OS_PRIO_MUTEX_CEIL_DIS) {
  585. if (OSTCBCur->OSTCBPrio == pcp) { /* Did we have to raise current task's priority? */
  586. OSMutex_RdyAtPrio(OSTCBCur, prio); /* Restore the task's original priority */
  587. }
  588. OSTCBPrioTbl[pcp] = OS_TCB_RESERVED; /* Reserve table entry */
  589. }
  590. if (pevent->OSEventGrp != 0u) { /* Any task waiting for the mutex? */
  591. /* Yes, Make HPT waiting for mutex ready */
  592. prio = OS_EventTaskRdy(pevent, (void *)0, OS_STAT_MUTEX, OS_STAT_PEND_OK);
  593. pevent->OSEventCnt &= OS_MUTEX_KEEP_UPPER_8; /* Save priority of mutex's new owner */
  594. pevent->OSEventCnt |= prio;
  595. pevent->OSEventPtr = OSTCBPrioTbl[prio]; /* Link to new mutex owner's OS_TCB */
  596. if ((pcp != OS_PRIO_MUTEX_CEIL_DIS) &&
  597. (prio <= pcp)) { /* PCP 'must' have a SMALLER prio ... */
  598. OS_EXIT_CRITICAL(); /* ... than current task! */
  599. OS_Sched(); /* Find highest priority task ready to run */
  600. return (OS_ERR_PCP_LOWER);
  601. } else {
  602. OS_EXIT_CRITICAL();
  603. OS_Sched(); /* Find highest priority task ready to run */
  604. return (OS_ERR_NONE);
  605. }
  606. }
  607. pevent->OSEventCnt |= OS_MUTEX_AVAILABLE; /* No, Mutex is now available */
  608. pevent->OSEventPtr = (void *)0;
  609. OS_EXIT_CRITICAL();
  610. return (OS_ERR_NONE);
  611. }
  612. /*$PAGE*/
  613. /*
  614. *********************************************************************************************************
  615. * QUERY A MUTUAL EXCLUSION SEMAPHORE
  616. *
  617. * Description: This function obtains information about a mutex
  618. *
  619. * Arguments : pevent is a pointer to the event control block associated with the desired mutex
  620. *
  621. * p_mutex_data is a pointer to a structure that will contain information about the mutex
  622. *
  623. * Returns : OS_ERR_NONE The call was successful and the message was sent
  624. * OS_ERR_QUERY_ISR If you called this function from an ISR
  625. * OS_ERR_PEVENT_NULL If 'pevent' is a NULL pointer
  626. * OS_ERR_PDATA_NULL If 'p_mutex_data' is a NULL pointer
  627. * OS_ERR_EVENT_TYPE If you are attempting to obtain data from a non mutex.
  628. *********************************************************************************************************
  629. */
  630. #if OS_MUTEX_QUERY_EN > 0u
  631. INT8U OSMutexQuery (OS_EVENT *pevent,
  632. OS_MUTEX_DATA *p_mutex_data)
  633. {
  634. INT8U i;
  635. OS_PRIO *psrc;
  636. OS_PRIO *pdest;
  637. #if OS_CRITICAL_METHOD == 3u /* Allocate storage for CPU status register */
  638. OS_CPU_SR cpu_sr = 0u;
  639. #endif
  640. if (OSIntNesting > 0u) { /* See if called from ISR ... */
  641. return (OS_ERR_QUERY_ISR); /* ... can't QUERY mutex from an ISR */
  642. }
  643. #if OS_ARG_CHK_EN > 0u
  644. if (pevent == (OS_EVENT *)0) { /* Validate 'pevent' */
  645. return (OS_ERR_PEVENT_NULL);
  646. }
  647. if (p_mutex_data == (OS_MUTEX_DATA *)0) { /* Validate 'p_mutex_data' */
  648. return (OS_ERR_PDATA_NULL);
  649. }
  650. #endif
  651. if (pevent->OSEventType != OS_EVENT_TYPE_MUTEX) { /* Validate event block type */
  652. return (OS_ERR_EVENT_TYPE);
  653. }
  654. OS_ENTER_CRITICAL();
  655. p_mutex_data->OSMutexPCP = (INT8U)(pevent->OSEventCnt >> 8u);
  656. p_mutex_data->OSOwnerPrio = (INT8U)(pevent->OSEventCnt & OS_MUTEX_KEEP_LOWER_8);
  657. if (p_mutex_data->OSOwnerPrio == 0xFFu) {
  658. p_mutex_data->OSValue = OS_TRUE;
  659. } else {
  660. p_mutex_data->OSValue = OS_FALSE;
  661. }
  662. p_mutex_data->OSEventGrp = pevent->OSEventGrp; /* Copy wait list */
  663. psrc = &pevent->OSEventTbl[0];
  664. pdest = &p_mutex_data->OSEventTbl[0];
  665. for (i = 0u; i < OS_EVENT_TBL_SIZE; i++) {
  666. *pdest++ = *psrc++;
  667. }
  668. OS_EXIT_CRITICAL();
  669. return (OS_ERR_NONE);
  670. }
  671. #endif /* OS_MUTEX_QUERY_EN */
  672. /*$PAGE*/
  673. /*
  674. *********************************************************************************************************
  675. * RESTORE A TASK BACK TO ITS ORIGINAL PRIORITY
  676. *
  677. * Description: This function makes a task ready at the specified priority
  678. *
  679. * Arguments : ptcb is a pointer to OS_TCB of the task to make ready
  680. *
  681. * prio is the desired priority
  682. *
  683. * Returns : none
  684. *********************************************************************************************************
  685. */
  686. static void OSMutex_RdyAtPrio (OS_TCB *ptcb,
  687. INT8U prio)
  688. {
  689. INT8U y;
  690. y = ptcb->OSTCBY; /* Remove owner from ready list at 'pcp' */
  691. OSRdyTbl[y] &= (OS_PRIO)~ptcb->OSTCBBitX;
  692. if (OSRdyTbl[y] == 0u) {
  693. OSRdyGrp &= (OS_PRIO)~ptcb->OSTCBBitY;
  694. }
  695. ptcb->OSTCBPrio = prio;
  696. OSPrioCur = prio; /* The current task is now at this priority */
  697. #if OS_LOWEST_PRIO <= 63u
  698. ptcb->OSTCBY = (INT8U)((INT8U)(prio >> 3u) & 0x07u);
  699. ptcb->OSTCBX = (INT8U)(prio & 0x07u);
  700. #else
  701. ptcb->OSTCBY = (INT8U)((INT8U)(prio >> 4u) & 0x0Fu);
  702. ptcb->OSTCBX = (INT8U) (prio & 0x0Fu);
  703. #endif
  704. ptcb->OSTCBBitY = (OS_PRIO)(1uL << ptcb->OSTCBY);
  705. ptcb->OSTCBBitX = (OS_PRIO)(1uL << ptcb->OSTCBX);
  706. OSRdyGrp |= ptcb->OSTCBBitY; /* Make task ready at original priority */
  707. OSRdyTbl[ptcb->OSTCBY] |= ptcb->OSTCBBitX;
  708. OSTCBPrioTbl[prio] = ptcb;
  709. }
  710. #endif /* OS_MUTEX_EN */