os_sem.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  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-06 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. * SEMAPHORE MANAGEMENT
  29. *
  30. * Filename : os_sem.c
  31. * Version : V2.93.00
  32. *********************************************************************************************************
  33. */
  34. #include "ucos_ii.h"
  35. #if OS_SEM_EN > 0u
  36. /*
  37. *********************************************************************************************************
  38. * ACCEPT SEMAPHORE
  39. *
  40. * Description: This function checks the semaphore to see if a resource is available or, if an event
  41. * occurred. Unlike OSSemPend(), OSSemAccept() does not suspend the calling task if the
  42. * resource is not available or the event did not occur.
  43. *
  44. * Arguments : pevent is a pointer to the event control block
  45. *
  46. * Returns : > 0 if the resource is available or the event did not occur the semaphore is
  47. * decremented to obtain the resource.
  48. * == 0 if the resource is not available or the event did not occur or,
  49. * if 'pevent' is a NULL pointer or,
  50. * if you didn't pass a pointer to a semaphore
  51. *********************************************************************************************************
  52. */
  53. #if OS_SEM_ACCEPT_EN > 0u
  54. INT16U OSSemAccept (OS_EVENT *pevent)
  55. {
  56. INT16U cnt;
  57. rt_sem_t psem;
  58. #if OS_ARG_CHK_EN > 0u
  59. if (pevent == (OS_EVENT *)0) { /* Validate 'pevent' */
  60. return (0u);
  61. }
  62. #endif
  63. psem = (rt_sem_t)pevent->ipc_ptr;
  64. if (rt_object_get_type(&psem->parent.parent)
  65. != RT_Object_Class_Semaphore) { /* Validate event block type */
  66. return (0u);
  67. }
  68. if(rt_sem_take(psem, RT_WAITING_NO) == RT_EOK) {
  69. cnt = psem->value;
  70. } else {
  71. cnt = 0u;
  72. }
  73. return (cnt); /* Return semaphore count */
  74. }
  75. #endif
  76. /*
  77. *********************************************************************************************************
  78. * CREATE A SEMAPHORE
  79. *
  80. * Description: This function creates a semaphore.
  81. *
  82. * Arguments : cnt is the initial value for the semaphore. If the value is 0, no resource is
  83. * available (or no event has occurred). You initialize the semaphore to a
  84. * non-zero value to specify how many resources are available (e.g. if you have
  85. * 10 resources, you would initialize the semaphore to 10).
  86. *
  87. * Returns : != (void *)0 is a pointer to the event control block (OS_EVENT) associated with the
  88. * created semaphore
  89. * == (void *)0 if no event control blocks were available
  90. *********************************************************************************************************
  91. */
  92. OS_EVENT *OSSemCreate (INT16U cnt)
  93. {
  94. OS_EVENT *pevent;
  95. #ifdef OS_SAFETY_CRITICAL_IEC61508
  96. if (OSSafetyCriticalStartFlag == OS_TRUE) {
  97. OS_SAFETY_CRITICAL_EXCEPTION();
  98. return ((OS_EVENT *)0);
  99. }
  100. #endif
  101. if (OSIntNesting > 0u) { /* See if called from ISR ... */
  102. return ((OS_EVENT *)0); /* ... can't CREATE from an ISR */
  103. }
  104. pevent = RT_KERNEL_MALLOC(sizeof(OS_EVENT));
  105. if (pevent == (OS_EVENT *)0) { /* Get an event control block */
  106. return ((OS_EVENT *)0);
  107. }
  108. pevent->ipc_ptr = (struct rt_ipc_object *)
  109. rt_sem_create("uCOS-II", cnt, RT_IPC_FLAG_PRIO);
  110. if(!pevent->ipc_ptr) {
  111. RT_KERNEL_FREE(pevent);
  112. return ((OS_EVENT *)0);
  113. }
  114. return (pevent);
  115. }
  116. /*
  117. *********************************************************************************************************
  118. * DELETE A SEMAPHORE
  119. *
  120. * Description: This function deletes a semaphore and readies all tasks pending on the semaphore.
  121. *
  122. * Arguments : pevent is a pointer to the event control block associated with the desired
  123. * semaphore.
  124. *
  125. * opt determines delete options as follows:
  126. * opt == OS_DEL_NO_PEND Delete semaphore ONLY if no task pending
  127. * opt == OS_DEL_ALWAYS Deletes the semaphore even if tasks are waiting.
  128. * In this case, all the tasks pending will be readied.
  129. *
  130. * perr is a pointer to an error code that can contain one of the following values:
  131. * OS_ERR_NONE The call was successful and the semaphore was
  132. * deleted
  133. * OS_ERR_DEL_ISR If you attempted to delete the semaphore from an
  134. * ISR
  135. * OS_ERR_ILLEGAL_DEL_RUN_TIME If you tried to delete the semaphore after
  136. * safety critical operation started.
  137. * OS_ERR_INVALID_OPT An invalid option was specified
  138. * OS_ERR_TASK_WAITING One or more tasks were waiting on the semaphore
  139. * OS_ERR_EVENT_TYPE If you didn't pass a pointer to a semaphore
  140. * OS_ERR_PEVENT_NULL If 'pevent' is a NULL pointer.
  141. *
  142. * Returns : pevent upon error
  143. * (OS_EVENT *)0 if the semaphore was successfully deleted.
  144. *
  145. * Note(s) : 1) This function must be used with care. Tasks that would normally expect the presence of
  146. * the semaphore MUST check the return code of OSSemPend().
  147. * 2) OSSemAccept() callers will not know that the intended semaphore has been deleted unless
  148. * they check 'pevent' to see that it's a NULL pointer.
  149. * 3) This call can potentially disable interrupts for a long time. The interrupt disable
  150. * time is directly proportional to the number of tasks waiting on the semaphore.
  151. * 4) Because ALL tasks pending on the semaphore will be readied, you MUST be careful in
  152. * applications where the semaphore is used for mutual exclusion because the resource(s)
  153. * will no longer be guarded by the semaphore.
  154. * 5) All tasks that were waiting for the semaphore will be readied and returned an
  155. * OS_ERR_PEND_ABORT if OSSemDel() was called with OS_DEL_ALWAYS
  156. *********************************************************************************************************
  157. */
  158. #if OS_SEM_DEL_EN > 0u
  159. OS_EVENT *OSSemDel (OS_EVENT *pevent,
  160. INT8U opt,
  161. INT8U *perr)
  162. {
  163. rt_sem_t psem;
  164. OS_EVENT *pevent_return;
  165. #ifdef OS_SAFETY_CRITICAL
  166. if (perr == (INT8U *)0) {
  167. OS_SAFETY_CRITICAL_EXCEPTION();
  168. return ((OS_EVENT *)0);
  169. }
  170. #endif
  171. #ifdef OS_SAFETY_CRITICAL_IEC61508
  172. if (OSSafetyCriticalStartFlag == OS_TRUE) {
  173. OS_SAFETY_CRITICAL_EXCEPTION();
  174. *perr = OS_ERR_ILLEGAL_DEL_RUN_TIME;
  175. return ((OS_EVENT *)0);
  176. }
  177. #endif
  178. #if OS_ARG_CHK_EN > 0u
  179. if (pevent == (OS_EVENT *)0) { /* Validate 'pevent' */
  180. *perr = OS_ERR_PEVENT_NULL;
  181. return (pevent);
  182. }
  183. #endif
  184. psem = (rt_sem_t)pevent->ipc_ptr;
  185. if (rt_object_get_type(&psem->parent.parent) /* Validate event block type */
  186. != RT_Object_Class_Semaphore) {
  187. return (pevent);
  188. }
  189. if (OSIntNesting > 0u) { /* See if called from ISR ... */
  190. *perr = OS_ERR_DEL_ISR; /* ... can't DELETE from an ISR */
  191. return (pevent);
  192. }
  193. switch (opt) {
  194. case OS_DEL_NO_PEND: /* Delete semaphore only if no task waiting */
  195. if(rt_list_isempty(&(psem->parent.suspend_thread))) { /* 若没有线程等待信号量 */
  196. rt_sem_delete(psem); /* invoke RT-Thread API */
  197. RT_KERNEL_FREE(pevent);
  198. *perr = OS_ERR_NONE;
  199. pevent_return = (OS_EVENT *)0;
  200. } else {
  201. *perr = OS_ERR_TASK_WAITING;
  202. pevent_return = pevent;
  203. }
  204. break;
  205. case OS_DEL_ALWAYS: /* Always delete the semaphore */
  206. rt_sem_delete(psem); /* invoke RT-Thread API */
  207. RT_KERNEL_FREE(pevent);
  208. *perr = OS_ERR_NONE;
  209. pevent_return = (OS_EVENT *)0;
  210. break;
  211. default:
  212. *perr = OS_ERR_INVALID_OPT;
  213. pevent_return = pevent;
  214. break;
  215. }
  216. return (pevent_return);
  217. }
  218. #endif
  219. /*
  220. *********************************************************************************************************
  221. * PEND ON SEMAPHORE
  222. *
  223. * Description: This function waits for a semaphore.
  224. *
  225. * Arguments : pevent is a pointer to the event control block associated with the desired
  226. * semaphore.
  227. *
  228. * timeout is an optional timeout period (in clock ticks). If non-zero, your task will
  229. * wait for the resource up to the amount of time specified by this argument.
  230. * If you specify 0, however, your task will wait forever at the specified
  231. * semaphore or, until the resource becomes available (or the event occurs).
  232. *
  233. * perr is a pointer to where an error message will be deposited. Possible error
  234. * messages are:
  235. *
  236. * OS_ERR_NONE The call was successful and your task owns the resource
  237. * or, the event you are waiting for occurred.
  238. * OS_ERR_TIMEOUT The semaphore was not received within the specified
  239. * 'timeout'.
  240. * OS_ERR_PEND_ABORT The wait on the semaphore was aborted.
  241. * OS_ERR_EVENT_TYPE If you didn't pass a pointer to a semaphore.
  242. * OS_ERR_PEND_ISR If you called this function from an ISR and the result
  243. * would lead to a suspension.
  244. * OS_ERR_PEVENT_NULL If 'pevent' is a NULL pointer.
  245. * OS_ERR_PEND_LOCKED If you called this function when the scheduler is locked
  246. *
  247. * Returns : none
  248. *********************************************************************************************************
  249. */
  250. void OSSemPend (OS_EVENT *pevent,
  251. INT32U timeout,
  252. INT8U *perr)
  253. {
  254. rt_sem_t psem;
  255. rt_err_t rt_err;
  256. #if OS_CRITICAL_METHOD == 3u /* Allocate storage for CPU status register */
  257. OS_CPU_SR cpu_sr = 0u;
  258. #endif
  259. #ifdef OS_SAFETY_CRITICAL
  260. if (perr == (INT8U *)0) {
  261. OS_SAFETY_CRITICAL_EXCEPTION();
  262. return;
  263. }
  264. #endif
  265. #if OS_ARG_CHK_EN > 0u
  266. if (pevent == (OS_EVENT *)0) { /* Validate 'pevent' */
  267. *perr = OS_ERR_PEVENT_NULL;
  268. return;
  269. }
  270. #endif
  271. psem = (rt_sem_t)pevent->ipc_ptr;
  272. if (rt_object_get_type(&psem->parent.parent) /* Validate event block type */
  273. != RT_Object_Class_Semaphore) {
  274. *perr = OS_ERR_EVENT_TYPE;
  275. return;
  276. }
  277. if (OSIntNesting > 0u) { /* See if called from ISR ... */
  278. *perr = OS_ERR_PEND_ISR; /* ... can't PEND from an ISR */
  279. return;
  280. }
  281. if (OSLockNesting > 0u) { /* See if called with scheduler locked ... */
  282. *perr = OS_ERR_PEND_LOCKED; /* ... can't PEND when locked */
  283. return;
  284. }
  285. OS_ENTER_CRITICAL(); /* Otherwise, must wait until event occurs */
  286. OSTCBCur->OSTCBStat |= OS_STAT_SEM; /* Resource not available, pend on semaphore */
  287. OSTCBCur->OSTCBStatPend = OS_STAT_PEND_OK;
  288. #ifndef PKG_USING_UCOSII_WRAPPER_TINY
  289. OSTCBCur->OSTCBDly = timeout; /* Store pend timeout in TCB */
  290. OSTCBCur->OSTCBEventPtr = pevent;
  291. #endif
  292. OS_EXIT_CRITICAL();
  293. if(timeout) { /* 0为永久等待 */
  294. rt_err = rt_sem_take(psem, timeout);
  295. OS_ENTER_CRITICAL();
  296. if (rt_err == RT_EOK) {
  297. OSTCBCur->OSTCBStatPend = OS_STAT_PEND_OK;
  298. } else if(OSTCBCur->OSTCBStatPend == OS_STAT_PEND_ABORT) {
  299. OSTCBCur->OSTCBStatPend = OS_STAT_PEND_ABORT;
  300. } else {
  301. OSTCBCur->OSTCBStatPend = OS_STAT_PEND_TO;
  302. }
  303. }else {
  304. rt_sem_take(psem, RT_WAITING_FOREVER);
  305. OS_ENTER_CRITICAL();
  306. if(OSTCBCur->OSTCBStatPend == OS_STAT_PEND_ABORT) {
  307. OSTCBCur->OSTCBStatPend = OS_STAT_PEND_ABORT;
  308. }else {
  309. OSTCBCur->OSTCBStatPend = OS_STAT_PEND_OK;
  310. }
  311. }
  312. switch (OSTCBCur->OSTCBStatPend) { /* See if we timed-out or aborted */
  313. case OS_STAT_PEND_OK:
  314. *perr = OS_ERR_NONE;
  315. break;
  316. case OS_STAT_PEND_ABORT:
  317. *perr = OS_ERR_PEND_ABORT; /* Indicate that we aborted */
  318. break;
  319. case OS_STAT_PEND_TO:
  320. default:
  321. *perr = OS_ERR_TIMEOUT; /* Indicate that we didn't get event within TO */
  322. break;
  323. }
  324. OSTCBCur->OSTCBStat = OS_STAT_RDY; /* Set task status to ready */
  325. OSTCBCur->OSTCBStatPend = OS_STAT_PEND_OK; /* Clear pend status */
  326. #ifndef PKG_USING_UCOSII_WRAPPER_TINY
  327. OSTCBCur->OSTCBEventPtr = (OS_EVENT *)0; /* Clear event pointers */
  328. #endif
  329. OS_EXIT_CRITICAL();
  330. }
  331. /*
  332. *********************************************************************************************************
  333. * ABORT WAITING ON A SEMAPHORE
  334. *
  335. * Description: This function aborts & readies any tasks currently waiting on a semaphore. This function
  336. * should be used to fault-abort the wait on the semaphore, rather than to normally signal
  337. * the semaphore via OSSemPost().
  338. *
  339. * Arguments : pevent is a pointer to the event control block associated with the desired
  340. * semaphore.
  341. *
  342. * opt determines the type of ABORT performed:
  343. * OS_PEND_OPT_NONE ABORT wait for a single task (HPT) waiting on the
  344. * semaphore
  345. * OS_PEND_OPT_BROADCAST ABORT wait for ALL tasks that are waiting on the
  346. * semaphore
  347. *
  348. * perr is a pointer to where an error message will be deposited. Possible error
  349. * messages are:
  350. *
  351. * OS_ERR_NONE No tasks were waiting on the semaphore.
  352. * OS_ERR_PEND_ABORT At least one task waiting on the semaphore was readied
  353. * and informed of the aborted wait; check return value
  354. * for the number of tasks whose wait on the semaphore
  355. * was aborted.
  356. * OS_ERR_EVENT_TYPE If you didn't pass a pointer to a semaphore.
  357. * OS_ERR_PEVENT_NULL If 'pevent' is a NULL pointer.
  358. *
  359. * Returns : == 0 if no tasks were waiting on the semaphore, or upon error.
  360. * > 0 if one or more tasks waiting on the semaphore are now readied and informed.
  361. *********************************************************************************************************
  362. */
  363. #if OS_SEM_PEND_ABORT_EN > 0u
  364. INT8U OSSemPendAbort (OS_EVENT *pevent,
  365. INT8U opt,
  366. INT8U *perr)
  367. {
  368. rt_sem_t psem;
  369. INT8U nbr_tasks = 0u;
  370. #ifdef OS_SAFETY_CRITICAL
  371. if (perr == (INT8U *)0) {
  372. OS_SAFETY_CRITICAL_EXCEPTION();
  373. return (0u);
  374. }
  375. #endif
  376. #if OS_ARG_CHK_EN > 0u
  377. if (pevent == (OS_EVENT *)0) { /* Validate 'pevent' */
  378. *perr = OS_ERR_PEVENT_NULL;
  379. return (0u);
  380. }
  381. #endif
  382. psem = (rt_sem_t)pevent->ipc_ptr;
  383. if (rt_object_get_type(&psem->parent.parent) /* Validate event block type */
  384. != RT_Object_Class_Semaphore) {
  385. *perr = OS_ERR_EVENT_TYPE;
  386. return (0u);
  387. }
  388. switch (opt)
  389. {
  390. case OS_ERR_PEND_ABORT:
  391. nbr_tasks = rt_ipc_pend_abort_all(&(psem->parent.suspend_thread));
  392. case OS_PEND_OPT_NONE:
  393. default:
  394. rt_ipc_pend_abort_1(&(psem->parent.suspend_thread));
  395. nbr_tasks = 1u;
  396. }
  397. *perr = OS_ERR_NONE;
  398. return (nbr_tasks); /* No tasks waiting on semaphore */
  399. }
  400. #endif
  401. /*
  402. *********************************************************************************************************
  403. * POST TO A SEMAPHORE
  404. *
  405. * Description: This function signals a semaphore
  406. *
  407. * Arguments : pevent is a pointer to the event control block associated with the desired
  408. * semaphore.
  409. *
  410. * Returns : OS_ERR_NONE The call was successful and the semaphore was signaled.
  411. * OS_ERR_SEM_OVF If the semaphore count exceeded its limit. In other words, you have
  412. * signaled the semaphore more often than you waited on it with either
  413. * OSSemAccept() or OSSemPend().
  414. * OS_ERR_EVENT_TYPE If you didn't pass a pointer to a semaphore
  415. * OS_ERR_PEVENT_NULL If 'pevent' is a NULL pointer.
  416. *********************************************************************************************************
  417. */
  418. INT8U OSSemPost (OS_EVENT *pevent)
  419. {
  420. rt_sem_t psem;
  421. #if OS_ARG_CHK_EN > 0u
  422. if (pevent == (OS_EVENT *)0) { /* Validate 'pevent' */
  423. return (OS_ERR_PEVENT_NULL);
  424. }
  425. #endif
  426. psem = (rt_sem_t)pevent->ipc_ptr;
  427. if (rt_object_get_type(&psem->parent.parent) /* Validate event block type */
  428. != RT_Object_Class_Semaphore) {
  429. return (OS_ERR_EVENT_TYPE);
  430. }
  431. if (rt_sem_release(psem) == RT_EOK) {
  432. return (OS_ERR_NONE);
  433. }
  434. return (OS_ERR_SEM_OVF);
  435. }
  436. /*
  437. *********************************************************************************************************
  438. * QUERY A SEMAPHORE
  439. *
  440. * Description: This function obtains information about a semaphore
  441. *
  442. * Arguments : pevent is a pointer to the event control block associated with the desired
  443. * semaphore
  444. *
  445. * p_sem_data is a pointer to a structure that will contain information about the
  446. * semaphore.
  447. *
  448. * Returns : OS_ERR_NONE The call was successful and the message was sent
  449. * OS_ERR_EVENT_TYPE If you are attempting to obtain data from a non semaphore.
  450. * OS_ERR_PEVENT_NULL If 'pevent' is a NULL pointer.
  451. * OS_ERR_PDATA_NULL If 'p_sem_data' is a NULL pointer
  452. *********************************************************************************************************
  453. */
  454. #if OS_SEM_QUERY_EN > 0u
  455. INT8U OSSemQuery (OS_EVENT *pevent,
  456. OS_SEM_DATA *p_sem_data)
  457. {
  458. rt_sem_t psem;
  459. #if OS_CRITICAL_METHOD == 3u /* Allocate storage for CPU status register */
  460. OS_CPU_SR cpu_sr = 0u;
  461. #endif
  462. #if OS_ARG_CHK_EN > 0u
  463. if (pevent == (OS_EVENT *)0) { /* Validate 'pevent' */
  464. return (OS_ERR_PEVENT_NULL);
  465. }
  466. if (p_sem_data == (OS_SEM_DATA *)0) { /* Validate 'p_sem_data' */
  467. return (OS_ERR_PDATA_NULL);
  468. }
  469. #endif
  470. psem = (rt_sem_t)(pevent->ipc_ptr);
  471. if (rt_object_get_type(&psem->parent.parent) /* Validate event block type */
  472. != RT_Object_Class_Semaphore) {
  473. return (OS_ERR_EVENT_TYPE);
  474. }
  475. OS_ENTER_CRITICAL();
  476. rt_memcpy(&p_sem_data->OSSem, psem, sizeof(struct rt_semaphore));
  477. p_sem_data->OSCnt = psem->value;
  478. OS_EXIT_CRITICAL();
  479. return (OS_ERR_NONE);
  480. }
  481. #endif /* OS_SEM_QUERY_EN */
  482. /*
  483. *********************************************************************************************************
  484. * SET SEMAPHORE
  485. *
  486. * Description: This function sets the semaphore count to the value specified as an argument. Typically,
  487. * this value would be 0.
  488. *
  489. * You would typically use this function when a semaphore is used as a signaling mechanism
  490. * and, you want to reset the count value.
  491. *
  492. * Arguments : pevent is a pointer to the event control block
  493. *
  494. * cnt is the new value for the semaphore count. You would pass 0 to reset the
  495. * semaphore count.
  496. *
  497. * perr is a pointer to an error code returned by the function as follows:
  498. *
  499. * OS_ERR_NONE The call was successful and the semaphore value was set.
  500. * OS_ERR_EVENT_TYPE If you didn't pass a pointer to a semaphore.
  501. * OS_ERR_PEVENT_NULL If 'pevent' is a NULL pointer.
  502. * OS_ERR_TASK_WAITING If tasks are waiting on the semaphore.
  503. *********************************************************************************************************
  504. */
  505. #if OS_SEM_SET_EN > 0u
  506. void OSSemSet (OS_EVENT *pevent,
  507. INT16U cnt,
  508. INT8U *perr)
  509. {
  510. rt_sem_t psem;
  511. #if OS_CRITICAL_METHOD == 3u /* Allocate storage for CPU status register */
  512. OS_CPU_SR cpu_sr = 0u;
  513. #endif
  514. #ifdef OS_SAFETY_CRITICAL
  515. if (perr == (INT8U *)0) {
  516. OS_SAFETY_CRITICAL_EXCEPTION();
  517. return;
  518. }
  519. #endif
  520. #if OS_ARG_CHK_EN > 0u
  521. if (pevent == (OS_EVENT *)0) { /* Validate 'pevent' */
  522. *perr = OS_ERR_PEVENT_NULL;
  523. return;
  524. }
  525. #endif
  526. psem = (rt_sem_t)pevent->ipc_ptr;
  527. if (rt_object_get_type(&psem->parent.parent) /* Validate event block type */
  528. != RT_Object_Class_Semaphore) {
  529. *perr = OS_ERR_EVENT_TYPE;
  530. return;
  531. }
  532. OS_ENTER_CRITICAL();
  533. *perr = OS_ERR_NONE;
  534. if (psem->value>0) {
  535. psem->value = cnt;
  536. } else {
  537. if(rt_list_isempty(&(psem->parent.suspend_thread))) { /* 若没有线程等待信号量 */
  538. psem->value = cnt;
  539. } else {
  540. *perr = OS_ERR_TASK_WAITING; /* 有任务正在等待该信号量,不可以设置value */
  541. }
  542. }
  543. OS_EXIT_CRITICAL();
  544. }
  545. #endif
  546. #endif /* OS_SEM_EN */