os_task.ls1

来自「在51单片机上移植成功的UCOS-II操作系统源代码,包括源代码及相关注释」· LS1 代码 · 共 1,019 行 · 第 1/5 页

LS1
1,019
字号

                                           */
                     699     ;                 for (i = 0; i < stk_size; i++) {
                     700     ;                     #if OS_STK_GROWTH == 1
                     701     ;                     *pfill++ = (OS_STK)0;
                     702     ;                     #else
                     703     ;                     *pfill-- = (OS_STK)0;
                     704     ;                     #endif
                     705     ;                 }
                     706     ;             }
                     707     ;         }
                     708     ;         
                     709     ;         psp = (void *)OSTaskStkInit(task, ppdata, ptos, opt); /* Initialize the task's st
                             ack            */
                     710     ;         err = OSTCBInit(prio, psp, pbos, id, stk_size, pext, opt);         
                     711     ;         if (err == OS_NO_ERR) {
                     712     ;             OS_ENTER_CRITICAL();
                     713     ;             OSTaskCtr++;                                     /* Increment the #tasks coun
                             ter           */
                     714     ;             OSTaskCreateHook(OSTCBPrioTbl[prio]);            /* Call user defined hook   
                                           */
                     715     ;             OS_EXIT_CRITICAL();
                     716     ;             if (OSRunning) {                 /* Find highest priority task if multitaskin
                             g has started */
                     717     ;                 OSSched();
                     718     ;             }
                     719     ;         } else {
                     720     ;             OS_ENTER_CRITICAL();
                     721     ;             OSTCBPrioTbl[prio] = (OS_TCB *)0;/* Make this priority available to others   
                                           */
                     722     ;             OS_EXIT_CRITICAL();
                     723     ;         }
                     724     ;         return (err);
                     725     ;     } else {
                     726     ;         OS_EXIT_CRITICAL();
                     727     ;         return (OS_PRIO_EXIST);
                     728     ;     }
                     729     ; }
                     730     ; #endif
                     731     ; /*$PAGE*/
                     732     ; /*
                     733     ; *****************************************************************************************
                             ****************
                     734     ; *                                            DELETE A TASK
                     735     ; *
                     736     ; * Description: This function allows you to delete a task.  The calling task can delete it
                             self by 
                     737     ; *              its own priority number.  The deleted task is returned to the dormant stat
                             e and can be
                     738     ; *              re-activated by creating the deleted task again.
                     739     ; *
                     740     ; * Arguments  : prio    is the priority of the task to delete.  Note that you can explicit
                             ely delete
                     741     ; *                      the current task without knowing its priority level by setting 'pr
                             io' to
                     742     ; *                      OS_PRIO_SELF.
                     743     ; *
                     744     ; * Returns    : OS_NO_ERR           if the call is successful
                     745     ; *              OS_TASK_DEL_IDLE    if you attempted to delete uC/OS-II's idle task
                     746     ; *              OS_PRIO_INVALID     if the priority you specify is higher that the maximum
                              allowed 
                     747     ; *                                  (i.e. >= OS_LOWEST_PRIO) or, you have not specified OS
                             _PRIO_SELF.
                     748     ; *              OS_TASK_DEL_ERR     if the task you want to delete does not exist
                     749     ; *              OS_TASK_DEL_ISR     if you tried to delete a task from an ISR
                     750     ; *
                     751     ; * Notes      : 1) To reduce interrupt latency, OSTaskDel() 'disables' the task:
A51 MACRO ASSEMBLER  OS_TASK                                                              09/09/2007 21:13:11 PAGE    14

                     752     ; *                    a) by making it not ready
                     753     ; *                    b) by removing it from any wait lists
                     754     ; *                    c) by preventing OSTimeTick() from making the task ready to run.
                     755     ; *                 The task can then be 'unlinked' from the miscellaneous structures in uC
                             /OS-II.
                     756     ; *              2) The function OSDummy() is called after OS_EXIT_CRITICAL() because, on m
                             ost processors, 
                     757     ; *                 the next instruction following the enable interrupt instruction is igno
                             red.  You can 
                     758     ; *                 replace OSDummy() with a macro that basically executes a NO OP (i.e. OS
                             _NOP()).  The 
                     759     ; *                 NO OP macro would avoid the execution time of the function call and ret
                             urn.
                     760     ; *              3) An ISR cannot delete a task.
                     761     ; *              4) The lock nesting counter is incremented because, for a brief instant, i
                             f the current
                     762     ; *                 task is being deleted, the current task would not be able to be resched
                             uled because it
                     763     ; *                 is removed from the ready list.  Incrementing the nesting counter preve
                             nts another task
                     764     ; *                 from being schedule.  This means that the ISR would return to the curre
                             nt task which is
                     765     ; *                 being deleted.  The rest of the deletion would thus be able to be compl
                             eted.
                     766     ; *****************************************************************************************
                             ****************
                     767     ; */
                     768     ; /*$PAGE*/
                     769     ; #if OS_TASK_DEL_EN
                     770     ; INT8U OSTaskDel (INT8U prio) reentrant
                     771     ; {
                     772     ;     OS_TCB   *ptcb;
                     773     ;     OS_EVENT *pevent;
                     774     ; 
                     775     ; 
                     776     ;     if (prio == OS_IDLE_PRIO) {                                 /* Not allowed to delete 
                             idle task     */
                     777     ;         return (OS_TASK_DEL_IDLE);
                     778     ;     }
                     779     ;     if (prio >= OS_LOWEST_PRIO && prio != OS_PRIO_SELF) {       /* Task priority valid ? 
                                           */
                     780     ;         return (OS_PRIO_INVALID);
                     781     ;     }
                     782     ;     OS_ENTER_CRITICAL();
                     783     ;     if (OSIntNesting > 0) {                                     /* See if trying to delet
                             e from ISR    */
                     784     ;         OS_EXIT_CRITICAL();
                     785     ;         return (OS_TASK_DEL_ISR);
                     786     ;     }
                     787     ;     if (prio == OS_PRIO_SELF) {                                 /* See if requesting to d
                             elete self    */
                     788     ;         prio = OSTCBCur->OSTCBPrio;                             /* Set priority to delete
                              to current   */
                     789     ;     }
                     790     ;     if ((ptcb = OSTCBPrioTbl[prio]) != (OS_TCB *)0) {           /* Task to delete must ex
                             ist           */
                     791     ;         if ((OSRdyTbl[ptcb->OSTCBY] &= ~ptcb->OSTCBBitX) == 0) {/* Make task not ready   
                                           */
                     792     ;             OSRdyGrp &= ~ptcb->OSTCBBitY;
                     793     ;         }
                     794     ;         if ((pevent = ptcb->OSTCBEventPtr) != (OS_EVENT *)0) {  /* If task is waiting on 
                             event         */
                     795     ;             if ((pevent->OSEventTbl[ptcb->OSTCBY] &= ~ptcb->OSTCBBitX) == 0) { /* ... rem
                             ove task from */
                     796     ;                 pevent->OSEventGrp &= ~ptcb->OSTCBBitY;                        /* ... eve
                             nt ctrl block */
A51 MACRO ASSEMBLER  OS_TASK                                                              09/09/2007 21:13:11 PAGE    15

                     797     ;             }
                     798     ;         }
                     799     ;         ptcb->OSTCBDly  = 0;                                    /* Prevent OSTimeTick() f
                             rom updating  */
                     800     ;         ptcb->OSTCBStat = OS_STAT_RDY;                          /* Prevent task from bein
                             g resumed     */
                     801     ;         OSLockNesting++;
                     802     ;         OS_EXIT_CRITICAL();                                     /* Enabling INT. ignores 
                             next instruc. */
                     803     ;         OSDummy();                                              /* ... Dummy ensures that
                              INTs will be */
                     804     ;         OS_ENTER_CRITICAL();                                    /* ... disabled HERE!    
                                           */
                     805     ;         OSLockNesting--;
                     806     ;         OSTaskDelHook(ptcb);                                    /* Call user defined hook
                                           */
                     807     ;         OSTaskCtr--;                                            /* One less task being ma
                             naged         */
                     808     ;         OSTCBPrioTbl[prio] = (OS_TCB *)0;                       /* Clear old priority ent
                             ry            */
                     809     ;         if (ptcb->OSTCBPrev == (OS_TCB *)0) {                   /* Remove from TCB chain 
                                           */
                     810     ;             ptcb->OSTCBNext->OSTCBPrev = (OS_TCB *)0;
                     811     ;             OSTCBList                  = ptcb->OSTCBNext;
                     812     ;         } else {
                     813     ;             ptcb->OSTCBPrev->OSTCBNext = ptcb->OSTCBNext;
                     814     ;             ptcb->OSTCBNext->OSTCBPrev = ptcb->OSTCBPrev;
                     815     ;         }
                     816     ;         ptcb->OSTCBNext = OSTCBFreeList;                        /* Return TCB to free TCB
                              list         */
                     817     ;         OSTCBFreeList   = ptcb;
                     818     ;         OS_EXIT_CRITICAL();
                     819     ;         OSSched();                                              /* Find new highest prior
                             ity task      */
                     820     ;         return (OS_NO_ERR);
                     821     ;     } else {
                     822     ;         OS_EXIT_CRITICAL();
                     823     ;         return (OS_TASK_DEL_ERR);
                     824     ;     }
                     825     ; }
                     826     ; #endif
                     827     ; /*$PAGE*/
                     828     ; /*
                     829     ; *****************************************************************************************
                             ****************
                     830     ; *                                    REQUEST THAT A TASK DELETE ITSELF
                     831     ; *
                     832     ; * Description: This function is used to:
                     833     ; *                   a) notify a task to delete itself.
                     834     ; *                   b) to see if a task requested that the current task delete itself.
                     835     ; *              This function is a little tricky to understand.  Basically, you have a tas
                             k that needs
                     836     ; *              to be deleted however, this task has resources that it has allocated (memo
                             ry buffers,
                     837     ; *              semaphores, mailboxes, queues etc.).  The task cannot be deleted otherwise
                              these
                     838     ; *              resources would not be freed.  The requesting task calls OSTaskDelReq() to
                              indicate that
                     839     ; *              the task needs to be deleted.  Deleting of the task is however, deferred t
                             o the task to
                     840     ; *              be deleted.  For example, suppose that task #10 needs to be deleted.  The 
                             requesting task
                     841     ; *              example, task #5, would call OSTaskDelReq(10).  When task #10 gets to exec
                             ute, it calls
                     842     ; *              this function by specifying OS_PRIO_SELF and monitors the returned value. 
            

⌨️ 快捷键说明

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