📄 os_sem.lst
字号:
C51 COMPILER V8.08 OS_SEM 02/25/2009 12:35:33 PAGE 1
C51 COMPILER V8.08, COMPILATION OF MODULE OS_SEM
OBJECT MODULE PLACED IN .\Output-files\OS_SEM.obj
COMPILER INVOKED BY: C:\Keil\C51\BIN\C51.EXE ucos-ii\OS_SEM.C LARGE BROWSE DEBUG OBJECTEXTEND PRINT(.\List-files\OS_SEM.
-lst) OBJECT(.\Output-files\OS_SEM.obj)
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 "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
INT16U OSSemAccept (OS_EVENT *pevent) reentrant
{
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();
cnt = pevent->OSEventCnt;
C51 COMPILER V8.08 OS_SEM 02/25/2009 12:35:33 PAGE 2
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
62
63 /*$PAGE*/
64 /*
65 *********************************************************************************************************
66 * CREATE A SEMAPHORE
67 *
68 * Description: This function creates a semaphore.
69 *
70 * Arguments : cnt is the initial value for the semaphore. If the value is 0, no resource is
71 * available (or no event has occurred). You initialize the semaphore to a
72 * non-zero value to specify how many resources are available (e.g. if you have
73 * 10 resources, you would initialize the semaphore to 10).
74 *
75 * Returns : != (void *)0 is a pointer to the event control clock (OS_EVENT) associated with the
76 * created semaphore
77 * == (void *)0 if no event control blocks were available
78 *********************************************************************************************************
79 */
80
81 OS_EVENT *OSSemCreate (INT16U cnt)reentrant
82 {
83 1
84 1 OS_EVENT *pevent;
85 1
86 1
87 1 if (OSIntNesting > 0) { /* See if called from ISR ... */
88 2 return ((OS_EVENT *)0); /* ... can't CREATE from an ISR */
89 2 }
90 1 OS_ENTER_CRITICAL();
91 1 pevent = OSEventFreeList; /* Get next free event control block */
92 1 if (OSEventFreeList != (OS_EVENT *)0) { /* See if pool of free ECB pool was empty */
93 2 OSEventFreeList = (OS_EVENT *)OSEventFreeList->OSEventPtr;
94 2 }
95 1 OS_EXIT_CRITICAL();
96 1 if (pevent != (OS_EVENT *)0) { /* Get an event control block */
97 2 pevent->OSEventType = OS_EVENT_TYPE_SEM;
98 2 pevent->OSEventCnt = cnt; /* Set semaphore value */
99 2 pevent->OSEventPtr = (void *)0; /* Unlink from ECB free list */
100 2 OS_EventWaitListInit(pevent); /* Initialize to 'nobody waiting' on sem. */
101 2 }
102 1 return (pevent);
103 1 }
104
105 /*$PAGE*/
106 /*
107 *********************************************************************************************************
108 * DELETE A SEMAPHORE
109 *
110 * Description: This function deletes a semaphore and readies all tasks pending on the semaphore.
111 *
112 * Arguments : pevent is a pointer to the event control block associated with the desired
113 * semaphore.
114 *
115 * opt determines delete options as follows:
116 * opt == OS_DEL_NO_PEND Delete semaphore ONLY if no task pending
C51 COMPILER V8.08 OS_SEM 02/25/2009 12:35:33 PAGE 3
117 * opt == OS_DEL_ALWAYS Deletes the semaphore even if tasks are waiting.
118 * In this case, all the tasks pending will be readied.
119 *
120 * err is a pointer to an error code that can contain one of the following values:
121 * OS_NO_ERR The call was successful and the semaphore was deleted
122 * OS_ERR_DEL_ISR If you attempted to delete the semaphore from an ISR
123 * OS_ERR_INVALID_OPT An invalid option was specified
124 * OS_ERR_TASK_WAITING One or more tasks were waiting on the semaphore
125 * OS_ERR_EVENT_TYPE If you didn't pass a pointer to a semaphore
126 * OS_ERR_PEVENT_NULL If 'pevent' is a NULL pointer.
127 *
128 * Returns : pevent upon error
129 * (OS_EVENT *)0 if the semaphore was successfully deleted.
130 *
131 * Note(s) : 1) This function must be used with care. Tasks that would normally expect the presence of
132 * the semaphore MUST check the return code of OSSemPend().
133 * 2) OSSemAccept() callers will not know that the intended semaphore has been deleted unless
134 * they check 'pevent' to see that it's a NULL pointer.
135 * 3) This call can potentially disable interrupts for a long time. The interrupt disable
136 * time is directly proportional to the number of tasks waiting on the semaphore.
137 * 4) Because ALL tasks pending on the semaphore will be readied, you MUST be careful in
138 * applications where the semaphore is used for mutual exclusion because the resource(s)
139 * will no longer be guarded by the semaphore.
140 *********************************************************************************************************
141 */
142
143 #if OS_SEM_DEL_EN > 0
OS_EVENT *OSSemDel (OS_EVENT *pevent, INT8U opt, INT8U *err) reentrant
{
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;
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 */
C51 COMPILER V8.08 OS_SEM 02/25/2009 12:35:33 PAGE 4
} 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);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -