📄 os_sem.lst
字号:
C51 COMPILER V6.23a OS_SEM 12/09/2004 16:50:26 PAGE 1
C51 COMPILER V6.23a, COMPILATION OF MODULE OS_SEM
OBJECT MODULE PLACED IN OS_SEM.OBJ
COMPILER INVOKED BY: C:\Program Files\Keil\C51\BIN\C51.EXE OS_SEM.C LARGE BROWSE DEBUG OBJECTEXTEND
stmt level source
1 /*
2 *********************************************************************************************************
3 * uC/OS-II
4 * The Real-Time Kernel
5 * SEMAPHORE MANAGEMENT
6 *
7 * (c) Copyright 1992-2002, Jean J. Labrosse, Weston, FL
8 * All Rights Reserved
9 *
10 * File : OS_SEM.C
11 * By : Jean J. Labrosse
12 *********************************************************************************************************
13 */
14
15 #ifndef OS_MASTER_FILE
16 #include "includes.h"
17 #endif
18
19 #if OS_SEM_EN > 0
20 /*
21 *********************************************************************************************************
22 * ACCEPT SEMAPHORE
23 *
24 * Description: This function checks the semaphore to see if a resource is available or, if an event
25 * occurred. Unlike OSSemPend(), OSSemAccept() does not suspend the calling task if the
26 * resource is not available or the event did not occur.
27 *
28 * Arguments : pevent is a pointer to the event control block
29 *
30 * Returns : > 0 if the resource is available or the event did not occur the semaphore is
31 * decremented to obtain the resource.
32 * == 0 if the resource is not available or the event did not occur or,
33 * if 'pevent' is a NULL pointer or,
34 * if you didn't pass a pointer to a semaphore
35 *********************************************************************************************************
36 */
37
38 #if OS_SEM_ACCEPT_EN > 0
39 INT16U OSSemAccept (OS_EVENT *pevent) KCREENTRANT
40 {
41 1 #if OS_CRITICAL_METHOD == 3 /* Allocate storage for CPU status register */
OS_CPU_SR cpu_sr;
#endif
44 1 INT16U cnt;
45 1
46 1
47 1 #if OS_ARG_CHK_EN > 0
48 1 if (pevent == (OS_EVENT *)0) { /* Validate 'pevent' */
49 2 return (0);
50 2 }
51 1 if (pevent->OSEventType != OS_EVENT_TYPE_SEM) { /* Validate event block type */
52 2 return (0);
53 2 }
54 1 #endif
55 1 OS_ENTER_CRITICAL();
C51 COMPILER V6.23a OS_SEM 12/09/2004 16:50:26 PAGE 2
56 1 cnt = pevent->OSEventCnt;
57 1 if (cnt > 0) { /* See if resource is available */
58 2 pevent->OSEventCnt--; /* Yes, decrement semaphore and notify caller */
59 2 }
60 1 OS_EXIT_CRITICAL();
61 1 return (cnt); /* Return semaphore count */
62 1 }
63 #endif
64
65 /*$PAGE*/
66 /*
67 *********************************************************************************************************
68 * CREATE A SEMAPHORE
69 *
70 * Description: This function creates a semaphore.
71 *
72 * Arguments : cnt is the initial value for the semaphore. If the value is 0, no resource is
73 * available (or no event has occurred). You initialize the semaphore to a
74 * non-zero value to specify how many resources are available (e.g. if you have
75 * 10 resources, you would initialize the semaphore to 10).
76 *
77 * Returns : != (void *)0 is a pointer to the event control clock (OS_EVENT) associated with the
78 * created semaphore
79 * == (void *)0 if no event control blocks were available
80 *********************************************************************************************************
81 */
82
83 OS_EVENT *OSSemCreate (INT16U cnt) KCREENTRANT
84 {
85 1 #if OS_CRITICAL_METHOD == 3 /* Allocate storage for CPU status register */
OS_CPU_SR cpu_sr;
#endif
88 1 OS_EVENT *pevent;
89 1
90 1
91 1 if (OSIntNesting > 0) { /* See if called from ISR ... */
92 2 return ((OS_EVENT *)0); /* ... can't CREATE from an ISR */
93 2 }
94 1 OS_ENTER_CRITICAL();
95 1 pevent = OSEventFreeList; /* Get next free event control block */
96 1 if (OSEventFreeList != (OS_EVENT *)0) { /* See if pool of free ECB pool was empty */
97 2 OSEventFreeList = (OS_EVENT *)OSEventFreeList->OSEventPtr;
98 2 }
99 1 OS_EXIT_CRITICAL();
100 1 if (pevent != (OS_EVENT *)0) { /* Get an event control block */
101 2 pevent->OSEventType = OS_EVENT_TYPE_SEM;
102 2 pevent->OSEventCnt = cnt; /* Set semaphore value */
103 2 pevent->OSEventPtr = (void *)0; /* Unlink from ECB free list */
104 2 OS_EventWaitListInit(pevent); /* Initialize to 'nobody waiting' on sem. */
105 2 }
106 1 return (pevent);
107 1 }
108
109 /*$PAGE*/
110 /*
111 *********************************************************************************************************
112 * DELETE A SEMAPHORE
113 *
114 * Description: This function deletes a semaphore and readies all tasks pending on the semaphore.
115 *
116 * Arguments : pevent is a pointer to the event control block associated with the desired
117 * semaphore.
C51 COMPILER V6.23a OS_SEM 12/09/2004 16:50:26 PAGE 3
118 *
119 * opt determines delete options as follows:
120 * opt == OS_DEL_NO_PEND Delete semaphore ONLY if no task pending
121 * opt == OS_DEL_ALWAYS Deletes the semaphore even if tasks are waiting.
122 * In this case, all the tasks pending will be readied.
123 *
124 * err is a pointer to an error code that can contain one of the following values:
125 * OS_NO_ERR The call was successful and the semaphore was deleted
126 * OS_ERR_DEL_ISR If you attempted to delete the semaphore from an ISR
127 * OS_ERR_INVALID_OPT An invalid option was specified
128 * OS_ERR_TASK_WAITING One or more tasks were waiting on the semaphore
129 * OS_ERR_EVENT_TYPE If you didn't pass a pointer to a semaphore
130 * OS_ERR_PEVENT_NULL If 'pevent' is a NULL pointer.
131 *
132 * Returns : pevent upon error
133 * (OS_EVENT *)0 if the semaphore was successfully deleted.
134 *
135 * Note(s) : 1) This function must be used with care. Tasks that would normally expect the presence of
136 * the semaphore MUST check the return code of OSSemPend().
137 * 2) OSSemAccept() callers will not know that the intended semaphore has been deleted unless
138 * they check 'pevent' to see that it's a NULL pointer.
139 * 3) This call can potentially disable interrupts for a long time. The interrupt disable
140 * time is directly proportional to the number of tasks waiting on the semaphore.
141 * 4) Because ALL tasks pending on the semaphore will be readied, you MUST be careful in
142 * applications where the semaphore is used for mutual exclusion because the resource(s)
143 * will no longer be guarded by the semaphore.
144 *********************************************************************************************************
145 */
146
147 #if OS_SEM_DEL_EN > 0
148 OS_EVENT *OSSemDel (OS_EVENT *pevent, INT8U opt, INT8U *err) KCREENTRANT
149 {
150 1 #if OS_CRITICAL_METHOD == 3 /* Allocate storage for CPU status register */
OS_CPU_SR cpu_sr;
#endif
153 1 BOOLEAN tasks_waiting;
154 1
155 1
156 1 if (OSIntNesting > 0) { /* See if called from ISR ... */
157 2 *err = OS_ERR_DEL_ISR; /* ... can't DELETE from an ISR */
158 2 return (pevent);
159 2 }
160 1 #if OS_ARG_CHK_EN > 0
161 1 if (pevent == (OS_EVENT *)0) { /* Validate 'pevent' */
162 2 *err = OS_ERR_PEVENT_NULL;
163 2 return (pevent);
164 2 }
165 1 if (pevent->OSEventType != OS_EVENT_TYPE_SEM) { /* Validate event block type */
166 2 *err = OS_ERR_EVENT_TYPE;
167 2 return (pevent);
168 2 }
169 1 #endif
170 1 OS_ENTER_CRITICAL();
171 1 if (pevent->OSEventGrp != 0x00) { /* See if any tasks waiting on semaphore */
172 2 tasks_waiting = TRUE; /* Yes */
173 2 } else {
174 2 tasks_waiting = FALSE; /* No */
175 2 }
176 1 switch (opt) {
177 2 case OS_DEL_NO_PEND: /* Delete semaphore only if no task waiting */
178 2 if (tasks_waiting == FALSE) {
179 3 pevent->OSEventType = OS_EVENT_TYPE_UNUSED;
C51 COMPILER V6.23a OS_SEM 12/09/2004 16:50:26 PAGE 4
180 3 pevent->OSEventPtr = OSEventFreeList; /* Return Event Control Block to free list */
181 3 OSEventFreeList = pevent; /* Get next free event control block */
182 3 OS_EXIT_CRITICAL();
183 3 *err = OS_NO_ERR;
184 3 return ((OS_EVENT *)0); /* Semaphore has been deleted */
185 3 } else {
186 3 OS_EXIT_CRITICAL();
187 3 *err = OS_ERR_TASK_WAITING;
188 3 return (pevent);
189 3 }
190 2
191 2 case OS_DEL_ALWAYS: /* Always delete the semaphore */
192 2 while (pevent->OSEventGrp != 0x00) { /* Ready ALL tasks waiting for semaphore */
193 3 OS_EventTaskRdy(pevent, (void *)0, OS_STAT_SEM);
194 3 }
195 2 pevent->OSEventType = OS_EVENT_TYPE_UNUSED;
196 2 pevent->OSEventPtr = OSEventFreeList; /* Return Event Control Block to free list */
197 2 OSEventFreeList = pevent; /* Get next free event control block */
198 2 OS_EXIT_CRITICAL();
199 2 if (tasks_waiting == TRUE) { /* Reschedule only if task(s) were waiting */
200 3 OS_Sched(); /* Find highest priority task ready to run */
201 3 }
202 2 *err = OS_NO_ERR;
203 2 return ((OS_EVENT *)0); /* Semaphore has been deleted */
204 2
205 2 default:
206 2 OS_EXIT_CRITICAL();
207 2 *err = OS_ERR_INVALID_OPT;
208 2 return (pevent);
209 2 }
210 1 }
211 #endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -