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

📄 os_task.lst

📁 在MC68HC908JB8上移植UCOSII成功
💻 LST
📖 第 1 页 / 共 4 页
字号:
  333:  }
  334:  #endif
  335:  /*$PAGE*/
  336:  /*
  337:  *********************************************************************************************************
  338:  *                                            DELETE A TASK
  339:  *
  340:  * Description: This function allows you to delete a task.  The calling task can delete itself by 
  341:  *              its own priority number.  The deleted task is returned to the dormant state and can be
  342:  *              re-activated by creating the deleted task again.
  343:  *
  344:  * Arguments  : prio    is the priority of the task to delete.  Note that you can explicitely delete
  345:  *                      the current task without knowing its priority level by setting 'prio' to
  346:  *                      OS_PRIO_SELF.
  347:  *
  348:  * Returns    : OS_NO_ERR           if the call is successful
  349:  *              OS_TASK_DEL_IDLE    if you attempted to delete uC/OS-II's idle task
  350:  *              OS_PRIO_INVALID     if the priority you specify is higher that the maximum allowed 
  351:  *                                  (i.e. >= OS_LOWEST_PRIO) or, you have not specified OS_PRIO_SELF.
  352:  *              OS_TASK_DEL_ERR     if the task you want to delete does not exist
  353:  *              OS_TASK_DEL_ISR     if you tried to delete a task from an ISR
  354:  *
  355:  * Notes      : 1) To reduce interrupt latency, OSTaskDel() 'disables' the task:
  356:  *                    a) by making it not ready
  357:  *                    b) by removing it from any wait lists
  358:  *                    c) by preventing OSTimeTick() from making the task ready to run.
  359:  *                 The task can then be 'unlinked' from the miscellaneous structures in uC/OS-II.
  360:  *              2) The function OSDummy() is called after OS_EXIT_CRITICAL() because, on most processors, 
  361:  *                 the next instruction following the enable interrupt instruction is ignored.  You can 
  362:  *                 replace OSDummy() with a macro that basically executes a NO OP (i.e. OS_NOP()).  The 
  363:  *                 NO OP macro would avoid the execution time of the function call and return.
  364:  *              3) An ISR cannot delete a task.
  365:  *              4) The lock nesting counter is incremented because, for a brief instant, if the current
  366:  *                 task is being deleted, the current task would not be able to be rescheduled because it
  367:  *                 is removed from the ready list.  Incrementing the nesting counter prevents another task
  368:  *                 from being schedule.  This means that the ISR would return to the current task which is
  369:  *                 being deleted.  The rest of the deletion would thus be able to be completed.
  370:  *********************************************************************************************************
  371:  */
  372:  /*$PAGE*/
  373:  #if OS_TASK_DEL_EN
  374:  INT8U OSTaskDel (INT8U prio)
  375:  {
  376:      OS_TCB   *ptcb;
  377:      OS_EVENT *pevent;
  378:  
  379:  
  380:      if (prio == OS_IDLE_PRIO) {                                 /* Not allowed to delete idle task     */
  381:          return (OS_TASK_DEL_IDLE);
  382:      }
  383:      if (prio >= OS_LOWEST_PRIO && prio != OS_PRIO_SELF) {       /* Task priority valid ?               */
  384:          return (OS_PRIO_INVALID);
  385:      }
  386:      OS_ENTER_CRITICAL();
  387:      if (OSIntNesting > 0) {                                     /* See if trying to delete from ISR    */
  388:          OS_EXIT_CRITICAL();
  389:          return (OS_TASK_DEL_ISR);
  390:      }
  391:      if (prio == OS_PRIO_SELF) {                                 /* See if requesting to delete self    */
  392:          prio = OSTCBCur->OSTCBPrio;                             /* Set priority to delete to current   */
  393:      }
  394:      if ((ptcb = OSTCBPrioTbl[prio]) != (OS_TCB *)0) {           /* Task to delete must exist           */
  395:          if ((OSRdyTbl[ptcb->OSTCBY] &= ~ptcb->OSTCBBitX) == 0) {/* Make task not ready                 */
  396:              OSRdyGrp &= ~ptcb->OSTCBBitY;
  397:          }
  398:          if ((pevent = ptcb->OSTCBEventPtr) != (OS_EVENT *)0) {  /* If task is waiting on event         */
  399:              if ((pevent->OSEventTbl[ptcb->OSTCBY] &= ~ptcb->OSTCBBitX) == 0) { /* ... remove task from */
  400:                  pevent->OSEventGrp &= ~ptcb->OSTCBBitY;                        /* ... event ctrl block */
  401:              }
  402:          }
  403:          ptcb->OSTCBDly  = 0;                                    /* Prevent OSTimeTick() from updating  */
  404:          ptcb->OSTCBStat = OS_STAT_RDY;                          /* Prevent task from being resumed     */
  405:          OSLockNesting++;
  406:          OS_EXIT_CRITICAL();                                     /* Enabling INT. ignores next instruc. */
  407:          OSDummy();                                              /* ... Dummy ensures that INTs will be */
  408:          OS_ENTER_CRITICAL();                                    /* ... disabled HERE!                  */
  409:          OSLockNesting--;
  410:          OSTaskDelHook(ptcb);                                    /* Call user defined hook              */
  411:          OSTaskCtr--;                                            /* One less task being managed         */
  412:          OSTCBPrioTbl[prio] = (OS_TCB *)0;                       /* Clear old priority entry            */
  413:          if (ptcb->OSTCBPrev == (OS_TCB *)0) {                   /* Remove from TCB chain               */
  414:              ptcb->OSTCBNext->OSTCBPrev = (OS_TCB *)0;
  415:              OSTCBList                  = ptcb->OSTCBNext;
  416:          } else {
  417:              ptcb->OSTCBPrev->OSTCBNext = ptcb->OSTCBNext;
  418:              ptcb->OSTCBNext->OSTCBPrev = ptcb->OSTCBPrev;
  419:          }
  420:          ptcb->OSTCBNext = OSTCBFreeList;                        /* Return TCB to free TCB list         */
  421:          OSTCBFreeList   = ptcb;
  422:          OS_EXIT_CRITICAL();
  423:          OSSched();                                              /* Find new highest priority task      */
  424:          return (OS_NO_ERR);
  425:      } else {
  426:          OS_EXIT_CRITICAL();
  427:          return (OS_TASK_DEL_ERR);
  428:      }
  429:  }
  430:  #endif
  431:  /*$PAGE*/
  432:  /*
  433:  *********************************************************************************************************
  434:  *                                    REQUEST THAT A TASK DELETE ITSELF
  435:  *
  436:  * Description: This function is used to:
  437:  *                   a) notify a task to delete itself.
  438:  *                   b) to see if a task requested that the current task delete itself.
  439:  *              This function is a little tricky to understand.  Basically, you have a task that needs
  440:  *              to be deleted however, this task has resources that it has allocated (memory buffers,
  441:  *              semaphores, mailboxes, queues etc.).  The task cannot be deleted otherwise these
  442:  *              resources would not be freed.  The requesting task calls OSTaskDelReq() to indicate that
  443:  *              the task needs to be deleted.  Deleting of the task is however, deferred to the task to
  444:  *              be deleted.  For example, suppose that task #10 needs to be deleted.  The requesting task
  445:  *              example, task #5, would call OSTaskDelReq(10).  When task #10 gets to execute, it calls
  446:  *              this function by specifying OS_PRIO_SELF and monitors the returned value.  If the return
  447:  *              value is OS_TASK_DEL_REQ, another task requested a task delete.  Task #10 would look like
  448:  *              this:
  449:  *
  450:  *                   void Task(void *data)
  451:  *                   {
  452:  *                       .
  453:  *                       .
  454:  *                       while (1) {
  455:  *                           OSTimeDly(1);
  456:  *                           if (OSTaskDelReq(OS_PRIO_SELF) == OS_TASK_DEL_REQ) {
  457:  *                               Release any owned resources;
  458:  *                               De-allocate any dynamic memory;
  459:  *                               OSTaskDel(OS_PRIO_SELF);
  460:  *                           }
  461:  *                       }
  462:  *                   }
  463:  *
  464:  * Arguments  : prio    is the priority of the task to request the delete from
  465:  *
  466:  * Returns    : OS_NO_ERR          if the task exist and the request has been registered
  467:  *              OS_TASK_NOT_EXIST  if the task has been deleted.  This allows the caller to know whether
  468:  *                                 the request has been executed.
  469:  *              OS_TASK_DEL_IDLE   if you requested to delete uC/OS-II's idle task
  470:  *              OS_PRIO_INVALID    if the priority you specify is higher that the maximum allowed 
  471:  *                                 (i.e. >= OS_LOWEST_PRIO) or, you have not specified OS_PRIO_SELF.
  472:  *              OS_TASK_DEL_REQ    if a task (possibly another task) requested that the running task be 
  473:  *                                 deleted.
  474:  *********************************************************************************************************
  475:  */
  476:  /*$PAGE*/
  477:  #if OS_TASK_DEL_EN
  478:  INT8U OSTaskDelReq (INT8U prio)
  479:  {
  480:      BOOLEAN  stat;
  481:      INT8U    err;
  482:      OS_TCB  *ptcb;
  483:  
  484:  
  485:      if (prio == OS_IDLE_PRIO) {                                 /* Not allowed to delete idle task     */
  486:          return (OS_TASK_DEL_IDLE);
  487:      }
  488:      if (prio >= OS_LOWEST_PRIO && prio != OS_PRIO_SELF) {       /* Task priority valid ?               */
  489:          return (OS_PRIO_INVALID);
  490:      }
  491:      if (prio == OS_PRIO_SELF) {                                 /* See if a task is requesting to ...  */
  492:          OS_ENTER_CRITICAL();                                    /* ... this task to delete itself      */
  493:          stat = OSTCBCur->OSTCBDelReq;                           /* Return request status to caller     */
  494:          OS_EXIT_CRITICAL();
  495:          return (stat);
  496:      } else {
  497:          OS_ENTER_CRITICAL();
  498:          if ((ptcb = OSTCBPrioTbl[prio]) != (OS_TCB *)0) {       /* Task to delete must exist           */
  499:              ptcb->OSTCBDelReq = OS_TASK_DEL_REQ;                /* Set flag indicating task to be DEL. */
  500:              err               = OS_NO_ERR;
  501:          } else {
  502:              err               = OS_TASK_NOT_EXIST;              /* Task must be deleted                */
  503:          }
  504:          OS_EXIT_CRITICAL();
  505:          return (err);
  506:      }
  507:  }
  508:  #endif
  509:  /*$PAGE*/
  510:  /*
  511:  *********************************************************************************************************
  512:  *                                        RESUME A SUSPENDED TASK
  513:  *
  514:  * Description: This function is called to resume a previously suspended task.  This is the only call that
  515:  *              will remove an explicit task suspension.
  516:  *
  517:  * Arguments  : prio     is the priority of the task to resume.
  518:  *
  519:  * Returns    : OS_NO_ERR                if the requested task is resumed
  520:  *              OS_PRIO_INVALID          if the priority you specify is higher that the maximum allowed 
  521:  *                                       (i.e. >= OS_LOWEST_PRIO)
  522:  *              OS_TASK_RESUME_PRIO      if the task to resume does not exist
  523:  *              OS_TASK_NOT_SUSPENDED    if the task to resume has not been suspended
  524:  *********************************************************************************************************
  525:  */
  526:  
  527:  #if OS_TASK_SUSPEND_EN
  528:  INT8U OSTaskResume (INT8U prio)
  529:  {
  530:      OS_TCB   *ptcb;
  531:  
  532:  
  533:      if (prio >= OS_LOWEST_PRIO) {                               /* Make sure task priority is valid    */
  534:          return (OS_PRIO_INVALID);
  535:      }
  536:      OS_ENTER_CRITICAL();
  537:      if ((ptcb = OSTCBPrioTbl[prio]) == (OS_TCB *)0) {           /* Task to suspend must exist          */
  538:          OS_EXIT_CRITICAL();
  539:          return (OS_TASK_RESUME_PRIO);
  540:      } else {
  541:          if (ptcb->OSTCBStat & OS_STAT_SUSPEND) {                           /* Task must be suspended   */
  542:              if (((ptcb->OSTCBStat &= ~OS_STAT_SUSPEND) == OS_STAT_RDY) &&  /* Remove suspension        */
  543:                   (ptcb->OSTCBDly  == 0)) {                                 /* Must not be delayed      */
  544:                  OSRdyGrp               |= ptcb->OSTCBBitY;                 /* Make task ready to run   */
  545:                  OSRdyTbl[ptcb->OSTCBY] |= ptcb->OSTCBBitX;
  546:                  OS_EXIT_CRITICAL();
  547:                  OSSched();
  548:              } else {
  549:                  OS_EXIT_CRITICAL();
  550:              }
  551:              return (OS_NO_ERR);
  552:          } else {
  553:              OS_EXIT_CRITICAL();
  554:              return (OS_TASK_NOT_SUSPENDED);

⌨️ 快捷键说明

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