os_mutex.c 36 KB

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