训练营PLSR题目
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 
 

450 líneas
20 KiB

  1. /*
  2. ************************************************************************************************************************
  3. * uC/OS-III
  4. * The Real-Time Kernel
  5. *
  6. * (c) Copyright 2009-2014; Micrium, Inc.; Weston, FL
  7. * All rights reserved. Protected by international copyright laws.
  8. *
  9. * PEND ON MULTIPLE OBJECTS
  10. *
  11. * File : OS_PEND_MULTI.C
  12. * By : JJL
  13. * Version : V3.04.04
  14. *
  15. * LICENSING TERMS:
  16. * ---------------
  17. * uC/OS-III is provided in source form for FREE short-term evaluation, for educational use or
  18. * for peaceful research. If you plan or intend to use uC/OS-III in a commercial application/
  19. * product then, you need to contact Micrium to properly license uC/OS-III for its use in your
  20. * application/product. We provide ALL the source code for your convenience and to help you
  21. * experience uC/OS-III. The fact that the source is provided does NOT mean that you can use
  22. * it commercially without paying a licensing fee.
  23. *
  24. * Knowledge of the source code may NOT be used to develop a similar product.
  25. *
  26. * Please help us continue to provide the embedded community with the finest software available.
  27. * Your honesty is greatly appreciated.
  28. *
  29. * You can find our product's user manual, API reference, release notes and
  30. * more information at https://doc.micrium.com.
  31. * You can contact us at www.micrium.com.
  32. ************************************************************************************************************************
  33. */
  34. #define MICRIUM_SOURCE
  35. #include "os.h"
  36. #ifdef VSC_INCLUDE_SOURCE_FILE_NAMES
  37. const CPU_CHAR *os_pend_multi__c = "$Id: $";
  38. #endif
  39. #if (((OS_CFG_Q_EN > 0u) || (OS_CFG_SEM_EN > 0u)) && (OS_CFG_PEND_MULTI_EN > 0u))
  40. /*
  41. ************************************************************************************************************************
  42. * PEND ON MULTIPLE OBJECTS
  43. *
  44. * Description: This function pends on multiple objects. The objects pended on MUST be either semaphores or message
  45. * queues. If multiple objects are ready at the start of the pend call, then all available objects that
  46. * are ready will be indicated to the caller. If the task must pend on the multiple events then, as soon
  47. * as one of the object is either posted, aborted or deleted, the task will be readied.
  48. *
  49. * This function only allows you to pend on semaphores and/or message queues.
  50. *
  51. * Arguments : p_pend_data_tbl is a pointer to an array of type OS_PEND_DATA which contains a list of all the
  52. * objects we will be waiting on. The caller must declare an array of OS_PEND_DATA
  53. * and initialize the .PendObjPtr (see below) with a pointer to the object (semaphore or
  54. * message queue) to pend on.
  55. *
  56. * OS_PEND_DATA MyPendArray[?];
  57. *
  58. * The OS_PEND_DATA field are as follows:
  59. *
  60. * OS_PEND_DATA *PrevPtr; Used to link OS_PEND_DATA objects
  61. * OS_PEND_DATA *NextPtr; Used to link OS_PEND_DATA objects
  62. * OS_TCB *TCBPtr; Pointer to the TCB that is pending on multiple objects
  63. * OS_PEND_OBJ *PendObjPtr; USER supplied field which is a pointer to the
  64. * semaphore or message queue you want to pend on. When
  65. * you call OSPendMulti() you MUST fill this field for
  66. * each of the objects you want to pend on.
  67. * OS_PEND_OBJ *RdyObjPtr; OSPendMulti() will return the object that was posted,
  68. * aborted or deleted in this field.
  69. * void *RdyMsgPtr; OSPendMulti() will fill in this field if the object
  70. * posted was a message queue. This corresponds to the
  71. * message posted.
  72. * OS_MSG_SIZE RdyMsgSize; OSPendMulti() will fill in this field if the object
  73. * posted was a message queue. This corresponds to the
  74. * size of the message posted.
  75. * CPU_TS RdyTS; OSPendMulti() will fill in this field if the object
  76. * was a message queue. This corresponds to the time
  77. * stamp when the message was posted. However, if the
  78. * object is a semaphore and the object is already ready
  79. * the this field will be set to (CPU_TS)0 because it's
  80. * not possible to know when the semaphore was posted.
  81. *
  82. * tbl_size is the size (in number of elements) of the OS_PEND_DATA array passed to this function. In
  83. * other words, if the called needs to pend on 4 separate objects (semaphores and/or queues)
  84. * then you would pass 4 to this call.
  85. *
  86. * timeout is an optional timeout period (in clock ticks). If non-zero, your task will wait any of
  87. * the objects up to the amount of time specified by this argument. If you specify 0, however,
  88. * your task will wait forever for the specified objects or, until an object is posted,
  89. * aborted or deleted.
  90. *
  91. * opt determines whether the user wants to block if none of the objects are available.
  92. *
  93. * OS_OPT_PEND_BLOCKING
  94. * OS_OPT_PEND_NON_BLOCKING
  95. *
  96. * p_err is a pointer to where an error message will be deposited. Possible error messages are:
  97. *
  98. * OS_ERR_NONE The call was successful and your task owns the resources or,
  99. * the objects you are waiting for occurred. Check the .RdyObjPtr
  100. * fields to know which objects have been posted.
  101. * OS_ERR_OBJ_TYPE If any of the .PendPtr is NOT a semaphore or a message queue
  102. * OS_ERR_OPT_INVALID If you specified an invalid option for 'opt'
  103. * OS_ERR_PEND_ABORT The wait on the events was aborted; check the .RdyObjPtr fields
  104. * for which objects were aborted.
  105. * OS_ERR_PEND_DEL The wait on the events was aborted; check the .RdyObjPtr fields
  106. * for which objects were aborted.
  107. * OS_ERR_PEND_ISR If you called this function from an ISR
  108. * OS_ERR_PEND_LOCKED If you called this function when the scheduler is locked.
  109. * OS_ERR_PEND_WOULD_BLOCK If the caller didn't want to block and no object ready
  110. * OS_ERR_STATUS_INVALID Invalid pend status
  111. * OS_ERR_PTR_INVALID If you passes a NULL pointer of 'p_pend_data_tbl'
  112. * OS_ERR_TIMEOUT The objects were not posted within the specified 'timeout'.
  113. *
  114. * Returns : > 0 the number of objects returned as ready, aborted or deleted
  115. * == 0 if no events are returned as ready because of timeout or upon error.
  116. ************************************************************************************************************************
  117. */
  118. OS_OBJ_QTY OSPendMulti (OS_PEND_DATA *p_pend_data_tbl,
  119. OS_OBJ_QTY tbl_size,
  120. OS_TICK timeout,
  121. OS_OPT opt,
  122. OS_ERR *p_err)
  123. {
  124. CPU_BOOLEAN valid;
  125. OS_OBJ_QTY nbr_obj_rdy;
  126. CPU_SR_ALLOC();
  127. #ifdef OS_SAFETY_CRITICAL
  128. if (p_err == (OS_ERR *)0) {
  129. OS_SAFETY_CRITICAL_EXCEPTION();
  130. return ((OS_OBJ_QTY)0);
  131. }
  132. #endif
  133. #if OS_CFG_CALLED_FROM_ISR_CHK_EN > 0u
  134. if (OSIntNestingCtr > (OS_NESTING_CTR)0) { /* Can't pend from an ISR */
  135. *p_err = OS_ERR_PEND_ISR;
  136. return ((OS_OBJ_QTY)0);
  137. }
  138. #endif
  139. #if OS_CFG_ARG_CHK_EN > 0u
  140. if (p_pend_data_tbl == (OS_PEND_DATA *)0) { /* Validate 'p_pend_data_tbl' */
  141. *p_err = OS_ERR_PTR_INVALID;
  142. return ((OS_OBJ_QTY)0);
  143. }
  144. if (tbl_size == (OS_OBJ_QTY)0) { /* Array size must be > 0 */
  145. *p_err = OS_ERR_PTR_INVALID;
  146. return ((OS_OBJ_QTY)0);
  147. }
  148. switch (opt) {
  149. case OS_OPT_PEND_BLOCKING:
  150. case OS_OPT_PEND_NON_BLOCKING:
  151. break;
  152. default:
  153. *p_err = OS_ERR_OPT_INVALID;
  154. return ((OS_OBJ_QTY)0);
  155. }
  156. #endif
  157. valid = OS_PendMultiValidate(p_pend_data_tbl, /* -------- Validate objects to be OS_SEM or OS_Q ------- */
  158. tbl_size);
  159. if (valid == DEF_FALSE) {
  160. *p_err = OS_ERR_OBJ_TYPE; /* Invalid, not OS_SEM or OS_Q */
  161. return ((OS_OBJ_QTY)0);
  162. }
  163. CPU_CRITICAL_ENTER();
  164. nbr_obj_rdy = OS_PendMultiGetRdy(p_pend_data_tbl, /* --------- SEE IF OBJECT(s) HAVE BEEN POSTED ---------- */
  165. tbl_size);
  166. if (nbr_obj_rdy > (OS_OBJ_QTY)0) {
  167. CPU_CRITICAL_EXIT();
  168. *p_err = OS_ERR_NONE;
  169. return ((OS_OBJ_QTY)nbr_obj_rdy);
  170. }
  171. if ((opt & OS_OPT_PEND_NON_BLOCKING) != (OS_OPT)0) { /* Caller wants to block if not available? */
  172. CPU_CRITICAL_EXIT();
  173. *p_err = OS_ERR_PEND_WOULD_BLOCK; /* No */
  174. return ((OS_OBJ_QTY)0);
  175. } else {
  176. if (OSSchedLockNestingCtr > (OS_NESTING_CTR)0) { /* Can't pend when the scheduler is locked */
  177. CPU_CRITICAL_EXIT();
  178. *p_err = OS_ERR_SCHED_LOCKED;
  179. return ((OS_OBJ_QTY)0);
  180. }
  181. }
  182. /* Lock the scheduler/re-enable interrupts */
  183. OS_CRITICAL_ENTER_CPU_EXIT();
  184. /* ------ NO OBJECT READY, PEND ON MULTIPLE OBJECTS ----- */
  185. OS_PendMultiWait(p_pend_data_tbl, /* Suspend task until object posted or timeout occurs */
  186. tbl_size,
  187. timeout);
  188. OS_CRITICAL_EXIT_NO_SCHED();
  189. OSSched(); /* Find next highest priority task ready */
  190. CPU_CRITICAL_ENTER();
  191. switch (OSTCBCurPtr->PendStatus) {
  192. case OS_STATUS_PEND_OK: /* We got one of the objects posted to */
  193. *p_err = OS_ERR_NONE;
  194. break;
  195. case OS_STATUS_PEND_ABORT: /* Indicate that the multi-pend was aborted */
  196. *p_err = OS_ERR_PEND_ABORT;
  197. break;
  198. case OS_STATUS_PEND_TIMEOUT: /* Indicate that we didn't get semaphore within timeout */
  199. *p_err = OS_ERR_TIMEOUT;
  200. break;
  201. case OS_STATUS_PEND_DEL: /* Indicate that an object pended on has been deleted */
  202. *p_err = OS_ERR_OBJ_DEL;
  203. break;
  204. default:
  205. *p_err = OS_ERR_STATUS_INVALID;
  206. break;
  207. }
  208. OSTCBCurPtr->PendStatus = OS_STATUS_PEND_OK;
  209. CPU_CRITICAL_EXIT();
  210. return ((OS_OBJ_QTY)1);
  211. }
  212. /*
  213. ************************************************************************************************************************
  214. * GET A LIST OF OBJECTS READY
  215. *
  216. * Description: This function is called by OSPendMulti() to obtain the list of object that are ready.
  217. *
  218. * Arguments : p_pend_data_tbl is a pointer to an array of OS_PEND_DATA
  219. * ---------------
  220. *
  221. * tbl_size is the size of the array
  222. *
  223. * Returns : > 0 the number of objects ready
  224. * == 0 if no object ready
  225. *
  226. * Note : This function is INTERNAL to uC/OS-III and your application should not call it.
  227. ************************************************************************************************************************
  228. */
  229. OS_OBJ_QTY OS_PendMultiGetRdy (OS_PEND_DATA *p_pend_data_tbl,
  230. OS_OBJ_QTY tbl_size)
  231. {
  232. OS_OBJ_QTY i;
  233. OS_OBJ_QTY nbr_obj_rdy;
  234. #if OS_CFG_Q_EN > 0u
  235. OS_ERR err;
  236. OS_MSG_SIZE msg_size;
  237. OS_Q *p_q;
  238. void *p_void;
  239. CPU_TS ts;
  240. #endif
  241. #if OS_CFG_SEM_EN > 0u
  242. OS_SEM *p_sem;
  243. #endif
  244. nbr_obj_rdy = (OS_OBJ_QTY)0;
  245. for (i = 0u; i < tbl_size; i++) {
  246. p_pend_data_tbl->RdyObjPtr = (OS_PEND_OBJ *)0; /* Clear all fields */
  247. p_pend_data_tbl->RdyMsgPtr = (void *)0;
  248. p_pend_data_tbl->RdyMsgSize = (OS_MSG_SIZE )0;
  249. p_pend_data_tbl->RdyTS = (CPU_TS )0;
  250. p_pend_data_tbl->NextPtr = (OS_PEND_DATA *)0;
  251. p_pend_data_tbl->PrevPtr = (OS_PEND_DATA *)0;
  252. p_pend_data_tbl->TCBPtr = (OS_TCB *)0;
  253. #if OS_CFG_Q_EN > 0u
  254. p_q = (OS_Q *)((void *)p_pend_data_tbl->PendObjPtr); /* Assume we are pointing to a message queue object */
  255. if (p_q->Type == OS_OBJ_TYPE_Q) { /* Is it a message queue? */
  256. p_void = OS_MsgQGet(&p_q->MsgQ, /* Yes, Any message waiting in the message queue? */
  257. &msg_size,
  258. &ts,
  259. &err);
  260. if (err == OS_ERR_NONE) {
  261. p_pend_data_tbl->RdyObjPtr = p_pend_data_tbl->PendObjPtr;
  262. p_pend_data_tbl->RdyMsgPtr = p_void; /* Yes, save the message received */
  263. p_pend_data_tbl->RdyMsgSize = msg_size;
  264. p_pend_data_tbl->RdyTS = ts;
  265. nbr_obj_rdy++;
  266. }
  267. }
  268. #endif
  269. #if OS_CFG_SEM_EN > 0u
  270. p_sem = (OS_SEM *)((void *)p_pend_data_tbl->PendObjPtr); /* Assume we are pointing to a semaphore object */
  271. if (p_sem->Type == OS_OBJ_TYPE_SEM) { /* Is it a semaphore? */
  272. if (p_sem->Ctr > 0u) { /* Yes, Semaphore has been signaled? */
  273. p_sem->Ctr--; /* Yes, caller may proceed */
  274. p_pend_data_tbl->RdyObjPtr = p_pend_data_tbl->PendObjPtr;
  275. p_pend_data_tbl->RdyTS = p_sem->TS;
  276. nbr_obj_rdy++;
  277. }
  278. }
  279. #endif
  280. p_pend_data_tbl++;
  281. }
  282. return (nbr_obj_rdy);
  283. }
  284. /*
  285. ************************************************************************************************************************
  286. * VERIFY THAT OBJECTS PENDED ON ARE EITHER SEMAPHORES or QUEUES
  287. *
  288. * Description: This function is called by OSPendMulti() to verify that we are multi-pending on either semaphores or
  289. * message queues.
  290. *
  291. * Arguments : p_pend_data_tbl is a pointer to an array of OS_PEND_DATA
  292. * ---------------
  293. *
  294. * tbl_size is the size of the array
  295. *
  296. * Returns : TRUE if all objects pended on are either semaphores of queues
  297. * FALSE if at least one object is not a semaphore or queue.
  298. *
  299. * Note : This function is INTERNAL to uC/OS-III and your application should not call it.
  300. ************************************************************************************************************************
  301. */
  302. CPU_BOOLEAN OS_PendMultiValidate (OS_PEND_DATA *p_pend_data_tbl,
  303. OS_OBJ_QTY tbl_size)
  304. {
  305. OS_OBJ_QTY i;
  306. OS_OBJ_QTY ctr;
  307. #if OS_CFG_SEM_EN > 0u
  308. OS_SEM *p_sem;
  309. #endif
  310. #if OS_CFG_Q_EN > 0u
  311. OS_Q *p_q;
  312. #endif
  313. for (i = 0u; i < tbl_size; i++) {
  314. if (p_pend_data_tbl->PendObjPtr == (OS_PEND_OBJ *)0) { /* All .PendObjPtr in the table MUST be non NULL */
  315. return (DEF_FALSE);
  316. }
  317. ctr = 0u;
  318. #if OS_CFG_SEM_EN > 0u
  319. p_sem = (OS_SEM *)((void *)p_pend_data_tbl->PendObjPtr); /* All objects to pend on must be of type OS_SEM ... */
  320. if (p_sem->Type == OS_OBJ_TYPE_SEM) {
  321. ctr++;
  322. }
  323. #endif
  324. #if OS_CFG_Q_EN > 0u
  325. p_q = (OS_Q *)((void *)p_pend_data_tbl->PendObjPtr); /* ... or of type OS_Q */
  326. if (p_q->Type == OS_OBJ_TYPE_Q) {
  327. ctr++;
  328. }
  329. #endif
  330. if (ctr == (OS_OBJ_QTY)0) {
  331. return (DEF_FALSE); /* Found at least one invalid object type */
  332. }
  333. p_pend_data_tbl++;
  334. }
  335. return (DEF_TRUE);
  336. }
  337. /*
  338. ************************************************************************************************************************
  339. * MAKE TASK WAIT FOR ANY OF MULTIPLE EVENTS TO OCCUR
  340. *
  341. * Description: This function is called by OSPendMulti() to suspend a task because any one of multiple objects that have
  342. * not been posted to.
  343. *
  344. * Arguments : p_pend_data_tbl is a pointer to an array of OS_PEND_DATA
  345. * ---------------
  346. *
  347. * tbl_size is the size of the array
  348. *
  349. * timeout is the timeout to wait in case none of the objects become ready
  350. *
  351. * Returns : none
  352. *
  353. * Note : This function is INTERNAL to uC/OS-III and your application should not call it.
  354. ************************************************************************************************************************
  355. */
  356. void OS_PendMultiWait (OS_PEND_DATA *p_pend_data_tbl,
  357. OS_OBJ_QTY tbl_size,
  358. OS_TICK timeout)
  359. {
  360. OS_OBJ_QTY i;
  361. OS_PEND_LIST *p_pend_list;
  362. #if OS_CFG_Q_EN > 0u
  363. OS_Q *p_q;
  364. #endif
  365. #if OS_CFG_SEM_EN > 0u
  366. OS_SEM *p_sem;
  367. #endif
  368. OSTCBCurPtr->PendOn = OS_TASK_PEND_ON_MULTI; /* Resource not available, wait until it is */
  369. OSTCBCurPtr->PendStatus = OS_STATUS_PEND_OK;
  370. OSTCBCurPtr->PendDataTblEntries = tbl_size;
  371. OSTCBCurPtr->PendDataTblPtr = p_pend_data_tbl;
  372. OS_TaskBlock(OSTCBCurPtr, /* Block the task waiting for object to be posted ... */
  373. timeout); /* ... but with a timeout if not */
  374. for (i = 0u; i < tbl_size; i++) {
  375. p_pend_data_tbl->TCBPtr = OSTCBCurPtr; /* Every entry points back to the TCB of the task */
  376. #if OS_CFG_SEM_EN > 0u
  377. p_sem = (OS_SEM *)((void *)p_pend_data_tbl->PendObjPtr);
  378. if (p_sem->Type == OS_OBJ_TYPE_SEM) {
  379. p_pend_list = &p_sem->PendList;
  380. OS_PendListInsertPrio(p_pend_list,
  381. p_pend_data_tbl);
  382. }
  383. #endif
  384. #if OS_CFG_Q_EN > 0u
  385. p_q = (OS_Q *)((void *)p_pend_data_tbl->PendObjPtr);
  386. if (p_q->Type == OS_OBJ_TYPE_Q) {
  387. p_pend_list = &p_q->PendList;
  388. OS_PendListInsertPrio(p_pend_list,
  389. p_pend_data_tbl);
  390. }
  391. #endif
  392. p_pend_data_tbl++;
  393. }
  394. }
  395. #endif