?? os_sem.lst
字號:
C51 COMPILER V7.20 OS_SEM 06/14/2006 20:19:47 PAGE 1
C51 COMPILER V7.20, COMPILATION OF MODULE OS_SEM
OBJECT MODULE PLACED IN OS_SEM.OBJ
COMPILER INVOKED BY: d:\Keil\C51\BIN\C51.EXE OS_SEM.C LARGE BROWSE DEBUG OBJECTEXTEND
line 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 "ucos_ii.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
INT16U OSSemAccept (OS_EVENT *pevent) reentrant
{
#if OS_CRITICAL_METHOD == 3 /* Allocate storage for CPU status register */
OS_CPU_SR cpu_sr;
#endif
INT16U cnt;
#if OS_ARG_CHK_EN > 0
if (pevent == (OS_EVENT *)0) { /* Validate 'pevent' */
return (0);
}
if (pevent->OSEventType != OS_EVENT_TYPE_SEM) { /* Validate event block type */
return (0);
}
#endif
OS_ENTER_CRITICAL();
C51 COMPILER V7.20 OS_SEM 06/14/2006 20:19:47 PAGE 2
cnt = pevent->OSEventCnt;
if (cnt > 0) { /* See if resource is available */
pevent->OSEventCnt--; /* Yes, decrement semaphore and notify caller */
}
OS_EXIT_CRITICAL();
return (cnt); /* Return semaphore count */
}
#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 #if OS_SEM_CREATE_EN>0
83 OS_EVENT *OSSemCreate (INT16U cnt) reentrant
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 #endif
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 V7.20 OS_SEM 06/14/2006 20:19:47 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
OS_EVENT *OSSemDel (OS_EVENT *pevent, INT8U opt, INT8U *err) reentrant
{
#if OS_CRITICAL_METHOD == 3 /* Allocate storage for CPU status register */
OS_CPU_SR cpu_sr;
#endif
BOOLEAN tasks_waiting;
if (OSIntNesting > 0) { /* See if called from ISR ... */
*err = OS_ERR_DEL_ISR; /* ... can't DELETE from an ISR */
return (pevent);
}
#if OS_ARG_CHK_EN > 0
if (pevent == (OS_EVENT *)0) { /* Validate 'pevent' */
*err = OS_ERR_PEVENT_NULL;
return (pevent);
}
if (pevent->OSEventType != OS_EVENT_TYPE_SEM) { /* Validate event block type */
*err = OS_ERR_EVENT_TYPE;
return (pevent);
}
#endif
OS_ENTER_CRITICAL();
if (pevent->OSEventGrp != 0x00) { /* See if any tasks waiting on semaphore */
tasks_waiting = TRUE; /* Yes */
} else {
tasks_waiting = FALSE; /* No */
}
switch (opt) {
case OS_DEL_NO_PEND: /* Delete semaphore only if no task waiting */
if (tasks_waiting == FALSE) {
pevent->OSEventType = OS_EVENT_TYPE_UNUSED;
C51 COMPILER V7.20 OS_SEM 06/14/2006 20:19:47 PAGE 4
pevent->OSEventPtr = OSEventFreeList; /* Return Event Control Block to free list */
OSEventFreeList = pevent; /* Get next free event control block */
OS_EXIT_CRITICAL();
*err = OS_NO_ERR;
return ((OS_EVENT *)0); /* Semaphore has been deleted */
} else {
OS_EXIT_CRITICAL();
*err = OS_ERR_TASK_WAITING;
return (pevent);
}
case OS_DEL_ALWAYS: /* Always delete the semaphore */
while (pevent->OSEventGrp != 0x00) { /* Ready ALL tasks waiting for semaphore */
OS_EventTaskRdy(pevent, (void *)0, OS_STAT_SEM);
}
pevent->OSEventType = OS_EVENT_TYPE_UNUSED;
pevent->OSEventPtr = OSEventFreeList; /* Return Event Control Block to free list */
OSEventFreeList = pevent; /* Get next free event control block */
OS_EXIT_CRITICAL();
if (tasks_waiting == TRUE) { /* Reschedule only if task(s) were waiting */
OS_Sched(); /* Find highest priority task ready to run */
}
*err = OS_NO_ERR;
return ((OS_EVENT *)0); /* Semaphore has been deleted */
default:
OS_EXIT_CRITICAL();
*err = OS_ERR_INVALID_OPT;
return (pevent);
}
}
#endif
212
213 /*$PAGE*/
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -