⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 os_sem.lst

📁 uCOS 嵌入式操作系统的改进版,增加了网络通讯.
💻 LST
📖 第 1 页 / 共 2 页
字号:
C51 COMPILER V7.06   OS_SEM                                                                07/18/2003 11:06:03 PAGE 1   


C51 COMPILER V7.06, COMPILATION OF MODULE OS_SEM
OBJECT MODULE PLACED IN .\os_sem.obj
COMPILER INVOKED BY: C:\KEIL\C51\BIN\C51.EXE ..\keilc51\os_sem.c LARGE BROWSE DEBUG OBJECTEXTEND PRINT(.\os_sem.lst) OBJ
                    -ECT(.\os_sem.obj)

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) reentrant //using 0
  40          {
  41   1      #if OS_CRITICAL_METHOD == 3                           /* Allocate storage for CPU status register      */
  42   1          OS_CPU_SR  cpu_sr;
  43   1      #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      #endif
  52   1          if (pevent->OSEventType != OS_EVENT_TYPE_SEM) {   /* Validate event block type                     */
  53   2              return (0);
  54   2          }
C51 COMPILER V7.06   OS_SEM                                                                07/18/2003 11:06:03 PAGE 2   

  55   1          OS_ENTER_CRITICAL();
  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) reentrant //using 0
  84          {
  85   1      #if OS_CRITICAL_METHOD == 3                                /* Allocate storage for CPU status register */
  86   1          OS_CPU_SR  cpu_sr;
  87   1      #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      #if OS_EVENT_NAME_SIZE > 0
 105   2              (void)strcpy(pevent->OSEventName, "?");
 106   2      #endif
 107   2              OS_EventWaitListInit(pevent);                      /* Initialize to 'nobody waiting' on sem.   */
 108   2          }
 109   1          return (pevent);
 110   1      }
 111          
 112          /*$PAGE*/
 113          /*
 114          *********************************************************************************************************
 115          *                                         DELETE A SEMAPHORE
 116          *
C51 COMPILER V7.06   OS_SEM                                                                07/18/2003 11:06:03 PAGE 3   

 117          * Description: This function deletes a semaphore and readies all tasks pending on the semaphore.
 118          *
 119          * Arguments  : pevent        is a pointer to the event control block associated with the desired
 120          *                            semaphore.
 121          *
 122          *              opt           determines delete options as follows:
 123          *                            opt == OS_DEL_NO_PEND   Delete semaphore ONLY if no task pending
 124          *                            opt == OS_DEL_ALWAYS    Deletes the semaphore even if tasks are waiting.
 125          *                                                    In this case, all the tasks pending will be readied.
 126          *
 127          *              err           is a pointer to an error code that can contain one of the following values:
 128          *                            OS_NO_ERR               The call was successful and the semaphore was deleted
 129          *                            OS_ERR_DEL_ISR          If you attempted to delete the semaphore from an ISR
 130          *                            OS_ERR_INVALID_OPT      An invalid option was specified
 131          *                            OS_ERR_TASK_WAITING     One or more tasks were waiting on the semaphore
 132          *                            OS_ERR_EVENT_TYPE       If you didn't pass a pointer to a semaphore
 133          *                            OS_ERR_PEVENT_NULL      If 'pevent' is a NULL pointer.
 134          *
 135          * Returns    : pevent        upon error
 136          *              (OS_EVENT *)0 if the semaphore was successfully deleted.
 137          *
 138          * Note(s)    : 1) This function must be used with care.  Tasks that would normally expect the presence of
 139          *                 the semaphore MUST check the return code of OSSemPend().
 140          *              2) OSSemAccept() callers will not know that the intended semaphore has been deleted unless
 141          *                 they check 'pevent' to see that it's a NULL pointer.
 142          *              3) This call can potentially disable interrupts for a long time.  The interrupt disable
 143          *                 time is directly proportional to the number of tasks waiting on the semaphore.
 144          *              4) Because ALL tasks pending on the semaphore will be readied, you MUST be careful in
 145          *                 applications where the semaphore is used for mutual exclusion because the resource(s)
 146          *                 will no longer be guarded by the semaphore.
 147          *********************************************************************************************************
 148          */
 149          
 150          #if OS_SEM_DEL_EN > 0
 151          OS_EVENT  *OSSemDel (OS_EVENT *pevent, INT8U opt, INT8U *err) reentrant //using 0
 152          {
 153   1      #if OS_CRITICAL_METHOD == 3                                /* Allocate storage for CPU status register */
 154   1          OS_CPU_SR  cpu_sr;
 155   1      #endif    
 156   1          BOOLEAN    tasks_waiting;
 157   1      
 158   1      
 159   1          if (OSIntNesting > 0) {                                /* See if called from ISR ...               */
 160   2              *err = OS_ERR_DEL_ISR;                             /* ... can't DELETE from an ISR             */
 161   2              return (pevent);
 162   2          }
 163   1      #if OS_ARG_CHK_EN > 0
 164   1          if (pevent == (OS_EVENT *)0) {                         /* Validate 'pevent'                        */
 165   2              *err = OS_ERR_PEVENT_NULL;
 166   2              return (pevent);
 167   2          }
 168   1      #endif
 169   1          if (pevent->OSEventType != OS_EVENT_TYPE_SEM) {        /* Validate event block type                */
 170   2              *err = OS_ERR_EVENT_TYPE;
 171   2              return (pevent);
 172   2          }
 173   1          OS_ENTER_CRITICAL();
 174   1          if (pevent->OSEventGrp != 0x00) {                      /* See if any tasks waiting on semaphore    */
 175   2              tasks_waiting = TRUE;                              /* Yes                                      */
 176   2          } else {
 177   2              tasks_waiting = FALSE;                             /* No                                       */
 178   2          }
C51 COMPILER V7.06   OS_SEM                                                                07/18/2003 11:06:03 PAGE 4   

 179   1          switch (opt) {
 180   2              case OS_DEL_NO_PEND:                               /* Delete semaphore only if no task waiting */
 181   2                   if (tasks_waiting == FALSE) {
 182   3      #if OS_EVENT_NAME_SIZE > 0
 183   3                       (void)strcpy(pevent->OSEventName, "?");   /* Unknown name                             */
 184   3      #endif
 185   3                       pevent->OSEventType = OS_EVENT_TYPE_UNUSED;
 186   3                       pevent->OSEventPtr  = OSEventFreeList;    /* Return Event Control Block to free list  */
 187   3                       pevent->OSEventCnt  = 0;
 188   3                       OSEventFreeList     = pevent;             /* Get next free event control block        */
 189   3                       OS_EXIT_CRITICAL();
 190   3                       *err                = OS_NO_ERR;
 191   3                       return ((OS_EVENT *)0);                   /* Semaphore has been deleted               */
 192   3                   } else {
 193   3                       OS_EXIT_CRITICAL();
 194   3                       *err                = OS_ERR_TASK_WAITING;
 195   3                       return (pevent);
 196   3                   }
 197   2      
 198   2              case OS_DEL_ALWAYS:                                /* Always delete the semaphore              */
 199   2                   while (pevent->OSEventGrp != 0x00) {          /* Ready ALL tasks waiting for semaphore    */
 200   3                       OS_EventTaskRdy(pevent, (void *)0, OS_STAT_SEM);
 201   3                   }
 202   2      #if OS_EVENT_NAME_SIZE > 0
 203   2                   (void)strcpy(pevent->OSEventName, "?");       /* Unknown name                             */
 204   2      #endif
 205   2                   OS_EventWaitListInit(pevent);                 /* Clear out the wait list                  */
 206   2                   pevent->OSEventType = OS_EVENT_TYPE_UNUSED;
 207   2                   pevent->OSEventPtr  = OSEventFreeList;        /* Return Event Control Block to free list  */
 208   2                   pevent->OSEventCnt  = 0;
 209   2                   OSEventFreeList     = pevent;                 /* Get next free event control block        */
 210   2                   OS_EXIT_CRITICAL();
 211   2                   if (tasks_waiting == TRUE) {                  /* Reschedule only if task(s) were waiting  */
 212   3                       OS_Sched();                               /* Find highest priority task ready to run  */
 213   3                   }
 214   2                   *err = OS_NO_ERR;
 215   2                   return ((OS_EVENT *)0);                       /* Semaphore has been deleted               */
 216   2      
 217   2              default:

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -