1    /* 
     2     * Copyright (c) 2011, Texas Instruments Incorporated
     3     * All rights reserved.
     4     *
     5     * Redistribution and use in source and binary forms, with or without
     6     * modification, are permitted provided that the following conditions
     7     * are met:
     8     *
     9     * *  Redistributions of source code must retain the above copyright
    10     *    notice, this list of conditions and the following disclaimer.
    11     *
    12     * *  Redistributions in binary form must reproduce the above copyright
    13     *    notice, this list of conditions and the following disclaimer in the
    14     *    documentation and/or other materials provided with the distribution.
    15     *
    16     * *  Neither the name of Texas Instruments Incorporated nor the names of
    17     *    its contributors may be used to endorse or promote products derived
    18     *    from this software without specific prior written permission.
    19     *
    20     * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    21     * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
    22     * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
    23     * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
    24     * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
    25     * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
    26     * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
    27     * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
    28     * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
    29     * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
    30     * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    31     * */
    32    /*
    33     *  ======== Task.xdc ========
    34     *
    35     */
    36    
    37    package ti.sysbios.knl;
    38    
    39    import xdc.rov.ViewInfo;
    40    
    41    import xdc.runtime.Error;
    42    import xdc.runtime.Assert;
    43    import xdc.runtime.Diags;
    44    import xdc.runtime.Log;
    45    import xdc.runtime.IHeap;
    46    
    47    import ti.sysbios.knl.Queue;
    48    
    49    /*!
    50     *  ======== Task ========
    51     *  Task Manager.
    52     *
    53     *  The Task module makes available a set of functions that manipulate task
    54     *  objects accessed through pointers of type {@link #Handle}. Tasks represent
    55     *  independent threads of control that conceptually execute functions in
    56     *  parallel within a single C program; in reality, concurrency is achieved
    57     *  by switching the processor from one task to another.
    58     *
    59     *  When you create a task, it is provided with its own run-time stack,
    60     *  used for storing local variables as well as for further nesting of
    61     *  function calls. Each stack must be large enough to handle normal
    62     *  subroutine calls and one task preemption context.
    63     *  A task preemption context is the context that gets saved when one task
    64     *  preempts another as a result of an interrupt thread readying
    65     *  a higher-priority task.
    66     *
    67     *  All tasks executing within a single program share a common set of
    68     *  global variables, accessed according to the standard rules of scope
    69     *  defined for C functions.
    70     *
    71     *  Each task is in one of five modes of execution at any point in time:
    72     *  running, ready, blocked, terminated, or inactive. By design, there is 
    73     *  always one
    74     *  (and only one) task currently running, even if it is only the idle task
    75     *  managed internally by Task. The current task can be suspended from
    76     *  execution by calling certain Task functions, as well as functions
    77     *  provided by other modules like the Semaphore or Event Modules. 
    78     *  The current task
    79     *  can also terminate its own execution. In either case, the processor
    80     *  is switched to the highest priority task that is ready to run.
    81     *
    82     *  You can assign numeric priorities to tasks. Tasks are
    83     *  readied for execution in strict priority order; tasks of the same
    84     *  priority are scheduled on a first-come, first-served basis. 
    85     *  The priority of the currently running task is never lower
    86     *  than the priority of any ready task. Conversely, the running task
    87     *  is preempted and re-scheduled for execution whenever there exists
    88     *  some ready task of higher priority.
    89     *
    90     *  @a(Task Deletion)
    91     *
    92     *  Any dynamically created task that is not in the Task_Mode_RUNNING 
    93     *  state (ie not the currently running task) can be deleted using the 
    94     *  {@link #delete} API. 
    95     *
    96     *  Task_delete() removes the task from all internal queues and calls
    97     *  Memory_free() to free the task object and stack.
    98     *  Memory_free() must acquire a lock to the memory before proceeding. 
    99     *  If another task already holds a lock to the memory, then there is 
   100     *  a context switch.
   101     *  
   102     *  You can specify application-wide Delete hook functions that
   103     *  run whenever a task is deleted. See the discussion of Hook Functions
   104     *  below for details.
   105     *  
   106     *  Note: Unless the mode of the deleted task is Task_Mode_TERMINATED,
   107     *  Task_delete should be called with care. For example, if the task has
   108     *  obtained exclusive access to a resource, deleting the task makes the
   109     *  resource unavailable.
   110     *  
   111     *  Task_delete() constraints:
   112    
   113     *  @p(blist)
   114     *  -The task cannot be the currently executing task (Task_self()).
   115     *  -Task_delete cannot be called from a Swi or Hwi.
   116     *  -No check is performed to prevent Task_delete from being used on a
   117     *  statically-created object. If a program attempts to delete a task object
   118     *  that was created statically, the Memory_free() call will result in an
   119     *  assertion failure in its corresponding Heap manager, causing the 
   120     *  application to exit.
   121     *  @p
   122     *  
   123     *  @a(Stack Alignment)
   124     *  
   125     *  Stack size parameters for both static and dynamic tasks are rounded
   126     *  up to the nearest integer multiple of a target-specific alignment
   127     *  requirement.
   128     *  
   129     *  In the case of Task's which are created with a user-provided stack,
   130     *  both the base address and the stackSize are aligned. The base address
   131     *  is increased to the nearest aligned address. The stack size is decreased
   132     *  accordingly and then rounded down to the nearest integer multiple of the 
   133     *  target-specific required alignment.
   134     *
   135     *  @p(html)
   136     *  <a name="hookfunc"></a>
   137     *  @p
   138     *
   139     *  @a(Hook Functions)
   140     *
   141     *  Sets of hook functions can be specified for the Task module.  Each
   142     *  set can contains these hook functions:
   143     *  @p(blist)
   144     *  -Register: A function called before any statically created tasks
   145     *      are initialized at runtime.  The register hook is called at boot time
   146     *      before main() and before interrupts are enabled.
   147     *  -Create: A function that is called when a task is created. 
   148     *      This includes tasks that are created statically and those
   149     *      created dynamically using {@link #create} or {@link #construct}.
   150     *      The create hook is called outside of a Task_disable/enable block and
   151     *   before the task has been added to the ready list.
   152     *  -Ready: A function that is called when a task becomes ready to run.
   153     *   The ready hook is called from within a Task_disable/enable block with
   154     *   interrupts enabled.
   155     *  -Switch: A function that is called just before a task switch
   156     *      occurs. The 'prev' and 'next' task handles are passed to the Switch
   157     *      hook. 'prev' is set to NULL for the initial task switch that occurs
   158     *      during SYS/BIOS startup.  The Switch hook is called from within a
   159     *      Task_disable/enable block with interrupts enabled.
   160     *  -Exit:      A function that is called when a task exits using
   161     *      {@link #exit}.  The exit hook is passed the handle of the exiting
   162     *      task.  The exit hook is called outside of a Task_disable/enable block
   163     *      and before the task has been removed from the kernel lists.
   164     *  -Delete: A function that is called when any task is deleted at
   165     *      run-time with {@link #delete}.  The delete hook is called outside
   166     *      of a Task_disable/enable block.
   167     *  @p
   168     *  Hook functions can only be configured statically.
   169     *
   170     *  If you define more than one set of hook functions, all the functions
   171     *  of a particular type will be run when a Swi triggers that type of 
   172     *  hook.
   173     *
   174     *  @p(html)
   175     *  <B>Register Function</B>
   176     *  @p
   177     *
   178     *  The Register function is provided to allow a hook set to store its
   179     *  hookset ID.  This id can be passed to {@link #setHookContext} and 
   180     *  {@link #getHookContext} to set or get hookset-specific context.  The
   181     *  Register function must be specified if the hook implementation
   182     *  needs to use {@link #setHookContext} or {@link #getHookContext}.
   183     *  The registerFxn hook function is called during system initialization
   184     *  before interrupts have been enabled. 
   185     *
   186     *  @p(code)
   187     *  Void myRegisterFxn(Int id);
   188     *  @p
   189     *
   190     *  @p(html)
   191     *  <B>Create and Delete Functions</B>
   192     *  @p
   193     *
   194     *  The create and delete functions are called whenever a Task is created
   195     *  or deleted.  They are called with interrupts enabled (unless called 
   196     *  at boot time or from main()).
   197     *
   198     *  @p(code)
   199     *  Void myCreateFxn(Task_Handle task, Error_Block *eb);
   200     *  @p
   201     *
   202     *  @p(code)
   203     *  Void myDeleteFxn(Task_Handle task);
   204     *  @p
   205     *
   206     *  @p(html)
   207     *  <B>Switch Function</B>
   208     *  @p
   209     *
   210     *  If a switch function is specified, it is invoked just before the new task
   211     *  is switched to.  The switch function is called with interrupts enabled.
   212     *
   213     *  This function can be used to save/restore additional task context (for
   214     *  example, external hardware registers), to check for task stack overflow,
   215     *  to monitor the time used by each task, etc.
   216     *
   217     *  @p(code)
   218     *  Void mySwitchFxn(Task_Handle prev, Task_Handle next);
   219     *  @p
   220     *
   221     *  To properly handle the switch to the first task your switchFxn should
   222     *  check for "prev == NULL" before using prev:
   223     *
   224     *  @p(code)
   225     *  Void mySwitchFxn(Task_Handle prev, Task_Handle next)
   226     *  {
   227     *      if (prev != NULL) {
   228     *          ...
   229     *      }
   230     *      ...
   231     *  }
   232     *  @p
   233     *
   234     *  @p(html)
   235     *  <B>Ready Function</B>
   236     *  @p
   237     *
   238     *  If a ready function is specified, it is invoked whenever a task is made
   239     *  ready to run.   The ready function is called  with interrupts enabled
   240     *  (unless called at boot time or from main()).
   241     *
   242     *  @p(code)
   243     *  Void myReadyFxn(Task_Handle task);
   244     *  @p
   245     * 
   246     *  @p(html)
   247     *  <B>Exit Function</B>
   248     *  @p
   249     *
   250     *  If an exit function is specified, it is invoked when a task exits (via
   251     *  call to Task_exit() or when a task returns from its' main function).
   252     *  The Exit Function is called with interrupts enabled. 
   253     *
   254     *  @p(code)
   255     *  Void myExitFxn(Task_Handle task);
   256     *  @p
   257     *
   258     *  @p(html)
   259     *  <h3> Calling Context </h3>
   260     *  <table border="1" cellpadding="3">
   261     *    <colgroup span="1"></colgroup> <colgroup span="5" align="center">
   262     *  </colgroup>
   263     *    
   264     *    <tr><th> Function                 </th><th>  Hwi   </th><th>  Swi   </th>
   265     *  <th>  Task  </th><th>  Main  </th><th>  Startup  </th></tr>
   266     *    <!--                                                       -->
   267     *    <tr><td> {@link #create}          </td><td>   N    </td><td>   N    </td>
   268     *  <td>   Y    </td><td>   Y    </td><td>   N    </td></tr>
   269     *    <tr><td> {@link #disable}         </td><td>   Y    </td><td>   Y    </td>
   270     *  <td>   Y    </td><td>   Y    </td><td>   N    </td></tr>
   271     *    <tr><td> {@link #exit}            </td><td>   N    </td><td>   N    </td>
   272     *  <td>   Y    </td><td>   N    </td><td>   N    </td></tr>
   273     *    <tr><td> {@link #getIdleTask}     </td><td>   Y    </td><td>   Y    </td>
   274     *  <td>   Y    </td><td>   Y    </td><td>   N    </td></tr>
   275     *    <tr><td> {@link #Params_init}     </td><td>   Y    </td><td>   Y    </td>
   276     *  <td>   Y    </td><td>   Y    </td><td>   Y    </td></tr>
   277     *    <tr><td> {@link #restore}         </td><td>   Y    </td><td>   Y    </td>
   278     *  <td>   Y    </td><td>   Y    </td><td>   N    </td></tr>
   279     *    <tr><td> {@link #self}            </td><td>   Y    </td><td>   Y    </td>
   280     *  <td>   Y    </td><td>   Y    </td><td>   N    </td></tr>
   281     *    <tr><td> {@link #sleep}           </td><td>   N    </td><td>   N    </td>
   282     *  <td>   Y    </td><td>   N    </td><td>   N    </td></tr>
   283     *    <tr><td> {@link #yield}           </td><td>   Y    </td><td>   Y    </td>
   284     *  <td>   Y    </td><td>   N    </td><td>   N    </td></tr>
   285     *    <tr><td> {@link #construct}       </td><td>   N    </td><td>   N    </td>
   286     *  <td>   Y    </td><td>   Y    </td><td>   N    </td></tr>
   287     *    <tr><td> {@link #delete}          </td><td>   N    </td><td>   N    </td>
   288     *  <td>   Y    </td><td>   Y    </td><td>   N    </td></tr>
   289     *    <tr><td> {@link #destruct}        </td><td>   N    </td><td>   N    </td>
   290     *  <td>   Y    </td><td>   Y    </td><td>   N    </td></tr>
   291     *    <tr><td> {@link #getEnv}          </td><td>   Y    </td><td>   Y    </td>
   292     *  <td>   Y    </td><td>   Y    </td><td>   N    </td></tr>
   293     *    <tr><td> {@link #getHookContext}  </td><td>   Y    </td><td>   Y    </td>
   294     *  <td>   Y    </td><td>   Y    </td><td>   N    </td></tr>
   295     *    <tr><td> {@link #getMode}         </td><td>   Y    </td><td>   Y    </td>
   296     *  <td>   Y    </td><td>   Y    </td><td>   N    </td></tr>
   297     *    <tr><td> {@link #getPri}          </td><td>   Y    </td><td>   Y    </td>
   298     *  <td>   Y    </td><td>   Y    </td><td>   N    </td></tr>
   299     *    <tr><td> {@link #setEnv}          </td><td>   Y    </td><td>   Y    </td>
   300     *  <td>   Y    </td><td>   Y    </td><td>   N    </td></tr>
   301     *    <tr><td> {@link #setHookContext}  </td><td>   Y    </td><td>   Y    </td>
   302     *  <td>   Y    </td><td>   Y    </td><td>   N    </td></tr>
   303     *    <tr><td> {@link #setPri}          </td><td>   Y    </td><td>   Y    </td>
   304     *  <td>   Y    </td><td>   N    </td><td>   N    </td></tr>
   305     *    <tr><td> {@link #stat}            </td><td>   Y    </td><td>   Y    </td>
   306     *  <td>   Y    </td><td>   Y    </td><td>   N    </td></tr>
   307     *    <tr><td colspan="6"> Definitions: <br />
   308     *       <ul>
   309     *         <li> <b>Hwi</b>: API is callable from a Hwi thread. </li>
   310     *         <li> <b>Swi</b>: API is callable from a Swi thread. </li>
   311     *         <li> <b>Task</b>: API is callable from a Task thread. </li>
   312     *         <li> <b>Main</b>: API is callable during any of these phases: </li>
   313     *           <ul>
   314     *             <li> In your module startup after this module is started 
   315     *  (e.g. Task_Module_startupDone() returns TRUE). </li>
   316     *             <li> During xdc.runtime.Startup.lastFxns. </li>
   317     *             <li> During main().</li>
   318     *             <li> During BIOS.startupFxns.</li>
   319     *           </ul>
   320     *         <li> <b>Startup</b>: API is callable during any of these phases:</li>
   321     *           <ul>
   322     *             <li> During xdc.runtime.Startup.firstFxns.</li>
   323     *             <li> In your module startup before this module is started 
   324     *  (e.g. Task_Module_startupDone() returns FALSE).</li>
   325     *           </ul>
   326     *       </ul>
   327     *    </td></tr>
   328     *  
   329     *  </table>
   330     *  @p
   331     */
   332    
   333    @ModuleStartup      /* generate a call to Task_Module_startup at startup */
   334    @InstanceFinalize   /* generate call to Task_Instance_finalize on delete */
   335    @InstanceInitError  /* instance init can fail */
   336    
   337    module Task
   338    {
   339    
   340        // -------- Module Constants --------
   341    
   342        // -------- Module Types --------
   343    
   344        /*! Task function type definition. */
   345        typedef Void (*FuncPtr)(UArg, UArg);
   346    
   347        /*! "All Task Blocked" function type definition. */
   348        typedef Void (*AllBlockedFuncPtr)(Void);
   349    
   350        /*! 
   351         *  Task execution modes. 
   352         *
   353         *  These enumerations are the range of modes or states that 
   354         *  a task can be in. A task's current mode can be gotten using
   355         *  {@link #stat}.
   356         */
   357        enum Mode {
   358            Mode_RUNNING,           /*! Task is currently executing. */
   359            Mode_READY,             /*! Task is scheduled for execution. */
   360            Mode_BLOCKED,           /*! Task is suspended from execution. */
   361            Mode_TERMINATED,        /*! Task is terminated from execution. */
   362            Mode_INACTIVE           /*! Task is on inactive task list */
   363        };
   364    
   365        /*! 
   366         *  Task Status Buffer. 
   367         *
   368         *  Passed to and filled in by {@link #stat};
   369         */
   370        struct Stat {
   371            Int     priority;       /*! Task priority. */
   372            Ptr     stack;          /*! Task stack. */
   373            SizeT   stackSize;      /*! Task stack size. */
   374            IHeap.Handle stackHeap; /*! Heap used to alloc stack. */
   375            Ptr     env;            /*! Global environment struct. */
   376            Mode    mode;           /*! Task's current mode. */
   377            Ptr     sp;             /*! Task's current stack pointer. */
   378            SizeT   used;           /*! max # of words used on stack. */
   379        };
   380    
   381        /*! 
   382         *  Task hook set type definition.
   383         * 
   384         *  Sets of hook functions can be specified for the Task module. 
   385         *  See {@link #hookfunc Hook Functions} for details. 
   386         */
   387        struct HookSet {
   388            Void (*registerFxn)(Int);
   389            Void (*createFxn)(Handle, Error.Block *);       
   390            Void (*readyFxn)(Handle);
   391            Void (*switchFxn)(Handle, Handle);
   392            Void (*exitFxn)(Handle);
   393            Void (*deleteFxn)(Handle);
   394        };
   395    
   396        /*!  @_nodoc */
   397        metaonly struct BasicView {
   398            String      label;
   399            Int         priority;
   400            String      mode;
   401            String      fxn[]; 
   402            UArg        arg0; 
   403            UArg        arg1; 
   404            SizeT       stackSize;
   405            Ptr         stackBase; 
   406        }
   407        
   408        /*!  @_nodoc */
   409        metaonly struct DetailedView {
   410            String      label;
   411            Int         priority;
   412            String      mode;
   413            String      fxn[]; 
   414            UArg        arg0; 
   415            UArg        arg1; 
   416            SizeT       stackPeak;
   417            SizeT       stackSize;
   418            Ptr         stackBase; 
   419            String      blockedOn;
   420        }
   421    
   422        /*!  @_nodoc */
   423        metaonly struct ModuleView {
   424            String      schedulerState;
   425            String      readyQMask;
   426            Bool        workPending;
   427            UInt        numVitalTasks;
   428            Ptr         currentTask;
   429            SizeT       hwiStackPeak;
   430            SizeT       hwiStackSize;
   431            Ptr         hwiStackBase;
   432        }
   433           
   434        /*! @_nodoc */
   435        @Facet
   436        metaonly config ViewInfo.Instance rovViewInfo = 
   437            ViewInfo.create({
   438                viewMap: [
   439                    ['Basic',    {type: ViewInfo.INSTANCE, viewInitFxn: 'viewInitBasic',    structName: 'BasicView'}],
   440                    ['Detailed', {type: ViewInfo.INSTANCE, viewInitFxn: 'viewInitDetailed', structName: 'DetailedView'}],
   441                    ['Module',   {type: ViewInfo.MODULE,   viewInitFxn: 'viewInitModule',   structName: 'ModuleView'}],
   442                ]
   443            });
   444    
   445        // -------- Module Parameters --------
   446    
   447        // Logs
   448        
   449        /*! Logged on every task switch */
   450        config Log.Event LM_switch = {
   451            mask: Diags.USER1 | Diags.USER2,
   452            msg: "LM_switch: oldtsk: 0x%x, oldfunc: 0x%x, newtsk: 0x%x, newfunc: 0x%x"
   453        };
   454    
   455        /*! Logged on calls to Task_sleep */
   456        config Log.Event LM_sleep = {
   457            mask: Diags.USER1 | Diags.USER2,
   458            msg: "LM_sleep: tsk: 0x%x, func: 0x%x, timeout: %d"
   459        };
   460    
   461        /*! Logged when a task is made ready to run (ie Semaphore_post()) */
   462        config Log.Event LD_ready = {
   463            mask: Diags.USER2,
   464            msg: "LD_ready: tsk: 0x%x, func: 0x%x, pri: %d"
   465        };
   466    
   467        /*! Logged when a task is blocked (ie Semaphore_pend()) */
   468        config Log.Event LD_block = {
   469            mask: Diags.USER2,
   470            msg: "LD_block: tsk: 0x%x, func: 0x%x"
   471        };
   472    
   473        /*! Logged on calls to Task_yield */
   474        config Log.Event LM_yield = {
   475            mask: Diags.USER1 | Diags.USER2,
   476            msg: "LM_yield: tsk: 0x%x, func: 0x%x, currThread: %d"
   477        };
   478    
   479        /*! Logged on calls to Task_setPri */
   480        config Log.Event LM_setPri = {
   481            mask: Diags.USER1 | Diags.USER2,
   482            msg: "LM_setPri: tsk: 0x%x, func: 0x%x, oldPri: %d, newPri %d"
   483        };
   484    
   485        /*! 
   486         *  Logged when Task functions fall thru the bottom 
   487         *  or when Task_exit() is explicitly called.
   488         */
   489        config Log.Event LD_exit = {
   490            mask: Diags.USER2,
   491            msg: "LD_exit: tsk: 0x%x, func: 0x%x"
   492        };
   493    
   494        // Errors
   495    
   496        /*! 
   497         *  Error raised when a stack overflow (or corruption) is detected.
   498         *
   499         *  This error is raised by kernel's stack checking function.  This
   500         *  function checks the stacks before every task switch to make sure
   501         *  that reserved word at top of stack has not been modified.
   502         *
   503         *  The stack checking logic is enabled by the {@link #initStackFlag} and
   504         *  {@link #checkStackFlag} configuration parameters.  If both of these
   505         *  flags are set to true, the kernel will validate the stacks.
   506         */
   507        config Error.Id E_stackOverflow  = {
   508            msg: "E_stackOverflow: Task 0x%x stack overflow."
   509        };
   510    
   511        /*! 
   512         *  Error raised when a task's stack pointer (SP) does not point
   513         *  somewhere within the task's stack.
   514         *
   515         *  This error is raised by kernel's stack checking function.  This
   516         *  function checks the SPs before every task switch to make sure
   517         *  they point within the task's stack.
   518         *
   519         *  The stack checking logic is enabled by the {@link #initStackFlag} and
   520         *  {@link #checkStackFlag} configuration parameters.  If both of these
   521         *  flags are set to true, the kernel will validate the stack pointers.
   522         */
   523        config Error.Id E_spOutOfBounds  = {
   524            msg: "E_spOutOfBounds: Task 0x%x stack error, SP = 0x%x."
   525        };
   526    
   527        // Asserts
   528    
   529        /*! Asserted in Task_create and Task_delete */
   530        config Assert.Id A_badThreadType = {
   531            msg: "A_badThreadType: Cannot create/delete a task from Hwi or Swi thread."
   532        };
   533    
   534        /*! Asserted in Task_delete */
   535        config Assert.Id A_badTaskState = {
   536            msg: "A_badTaskState: Can't delete a task in RUNNING state."
   537        };
   538    
   539        /*! Asserted in Task_delete */
   540        config Assert.Id A_noPendElem = {
   541            msg: "A_noPendElem: Not enough info to delete BLOCKED task."
   542        };
   543    
   544        /*! Asserted in Task_create */
   545        config Assert.Id A_taskDisabled = {
   546            msg: "A_taskDisabled: Cannot create a task when tasking is disabled."
   547        };
   548    
   549        /*! Asserted in Task_create */
   550        config Assert.Id A_badPriority = {
   551            msg: "A_badPriority: An invalid task priority was used."
   552        };
   553    
   554        /*! Asserted in Task_sleep */
   555        config Assert.Id A_badTimeout = {
   556            msg: "A_badTimeout: Can't sleep FOREVER."
   557        };
   558    
   559        /*! 
   560         *  Number of Task priorities supported. Default is 16.
   561         *
   562         *  The maximum number of priorities supported is
   563         *  target specific and depends on the number of 
   564         *  bits in a UInt data type. For 6x and ARM devices
   565         *  the maximum number of priorities is therefore 32.
   566         *  For 28x, 55x, and MSP430 devices, the maximum number of
   567         *  priorities is 16.
   568         */
   569        config UInt numPriorities = 16;
   570    
   571        /*!
   572         *  Default stack size (in MAUs) used for all tasks.
   573         *
   574         *  Default is obtained from the family-specific TaskSupport module 
   575          *  (e.g. {@link ti.sysbios.family.arm.m3.TaskSupport}, 
   576          *  {@link ti.sysbios.family.c62.TaskSupport}).
   577         */
   578        config SizeT defaultStackSize;
   579    
   580        /*!
   581         *  Default memory section used for all statically created task stacks.
   582         *
   583         *  The default stack section name is ".taskStackSection" which gets placed 
   584         *  into the platform's stackMemory (ie Program.platform.stackMemory).
   585         *
   586         *  To place all task stacks into a different memory segment, 
   587         *  add the following to your config script:
   588         *
   589         *  @p(code)
   590         *  Program.sectMap[Task.defaultStackSection] = new Program.SectionSpec();
   591         *  Program.sectMap[Task.defaultStackSection].loadSegment = 
   592         *                   "yourMemorySegment";
   593         *  @p
   594         *
   595         *  To place all task stacks into a different section AND memory segment, 
   596         *  add the following to your config script:
   597         *
   598         *  @p(code)
   599         *  Task.defaultStackSection = ".yourSectionName";
   600         *  Program.sectMap[Task.defaultStackSection] = new Program.SectionSpec();
   601         *  Program.sectMap[Task.defaultStackSection].loadSegment =
   602         *                   "yourMemorySegment";
   603         *  @p
   604         *
   605         *  Where "yourSectionName" can be just about anything, and
   606         *                   "yourMemorySegment" 
   607         *  must be a memory segment defined within the 
   608         *  {@link xdc.cfg.Program#platform Program.platform} 
   609         *  the application is being built for.
   610         */
   611        metaonly config String defaultStackSection = ".taskStackSection";
   612    
   613        /*!
   614         *  Default Mem heap used for all dynamically created task stacks.
   615         *
   616         *  Default is null.
   617         */
   618        config IHeap.Handle defaultStackHeap;
   619    
   620        /*! 
   621         *  Create a task (of priority 0) to run the Idle functions in.
   622         *
   623         *  When set to true, a task is created that continuously calls the 
   624         *  {@link Idle#run Idle_run()} function, which, in turn calls each of
   625         *  the configured Idle functions. 
   626         *
   627         *  When set to false, no Idle Task is created and it is up to the
   628         *  user to call the Idle_run() function if the configured Idle 
   629         *  functions need to be run. Or, by adding the following lines to
   630         *  the config script, the Idle functions will run whenever all
   631         *  tasks are blocked ({@link #allBlockedFunc Task.allBlockedFunc}):
   632         *
   633         *  @p(code)
   634         *  Task.enableIdleTask = false;
   635         *  Task.allBlockedFunc = Idle.run;
   636         *  @p
   637         *
   638         *  Default is true.
   639         *
   640         *  @see #idleTaskStackSize
   641         *  @see #idleTaskStackSection
   642         *  @see #idleTaskVitalTaskFlag
   643         *  @see #allBlockedFunc
   644         */
   645        metaonly config Bool enableIdleTask = true;
   646    
   647        /*! 
   648         *  Idle task stack size in MAUs. 
   649         *
   650         *  Default is inherited from module config defaultStackSize.
   651         */
   652        metaonly config SizeT idleTaskStackSize;
   653    
   654        /*! 
   655         *  Idle task stack section
   656         *
   657         *  Default is inherited from module config defaultStackSection;
   658         */
   659        metaonly config String idleTaskStackSection;
   660    
   661        /*! 
   662         *  Idle task's vitalTaskFlag.
   663         *  (see {@link #vitalTaskFlag}).
   664         *
   665         *  Default is true.
   666         */
   667        metaonly config Bool idleTaskVitalTaskFlag = true;
   668    
   669        /*! 
   670         *  Function to call while all tasks are blocked.
   671         *
   672         *  This function will be called repeatedly while no tasks are
   673         *  ready to run.
   674         *
   675         *  Ordinarily (in applications that have tasks ready to run at startup),
   676         *  the function will run in the context of the last task to block. 
   677         *
   678         *  In an application where there are no tasks ready to run 
   679         *  when BIOS_start() is called, the allBlockedFunc function is
   680         *  called within the BIOS_start() thread which runs on the system/ISR 
   681         *  stack.
   682         *
   683         *  By default, allBlockedFunc is initialized to point to an internal
   684         *  function that simply returns.
   685         *
   686         *  By adding the following lines to the config script, the Idle 
   687         *  functions will run whenever all tasks are blocked:
   688         *
   689         *  @p(code)
   690         *  Task.enableIdleTask = false;
   691         *  Task.allBlockedFunc = Idle.run;
   692         *  @p
   693         *
   694         *  @see #enableIdleTask
   695         *
   696         *  @a(constraints)
   697         *  The configured allBlockedFunc function is called with interrupts
   698         *  disabled. If your function must run with interrupts enabled, 
   699         *  surround the body of your code with  Hwi_enable()/Hwi_restore() 
   700         *  function calls per the following example:
   701         *
   702         *  The Task scheduler is also disabled if Swi_disable is called.
   703         *  Swi_restore re-enables and invokes the Task
   704         *  scheduler if the Task scheduler was enabled prior
   705         *  to invoking Swi_disable().
   706         *
   707         *  @p(code)
   708         *  Void yourFunc() {
   709         *      UInt hwiKey;
   710         *
   711         *      hwiKey = Hwi_enable();
   712         *
   713         *      ...         // your code here
   714         *
   715         *      Hwi_restore(hwiKey);
   716         *  }
   717         *  @p
   718         */
   719        config AllBlockedFuncPtr allBlockedFunc;
   720    
   721        /*! 
   722         *  Initialize stack with known value for stack checking at runtime
   723         *  (see {@link #checkStackFlag}).
   724         *
   725         *  This is also useful for inspection of stack in debugger or core
   726         *  dump utilities.
   727         *  Default is true.
   728         */
   729        config Bool initStackFlag = true;
   730    
   731        /*! 
   732         *  Check 'from' and 'to' task stacks before task context switch.
   733         *
   734         *  The check consists of testing the top of stack value against
   735         *  its initial value (see {@link #initStackFlag}). If it is no
   736         *  longer at this value, the assumption is that the task has
   737         *  overrun its stack. If the test fails, then the 
   738         *  {@link #E_stackOverflow} error is raised.
   739         *
   740         *  Runtime stack checking is only performed if {@link #initStackFlag} is
   741         *  also true.
   742         *
   743         *  Default is true.
   744         *
   745         *  To enable or disable full stack checking, you should set both this
   746         *  flag and the {@link ti.sysbios.hal.Hwi#checkStackFlag}.
   747         */
   748        metaonly config Bool checkStackFlag = true;
   749    
   750        /*! 
   751         *  Automatically delete terminated tasks.
   752         *
   753         *  If this feature is enabled, an Idle function is installed that
   754         *  deletes dynamically created Tasks that have terminated either
   755         *  by falling through their task function or by explicitly calling
   756         *  Task_exit().
   757         *
   758         *  A list of terminated Tasks that were created dynmically is 
   759         *  maintained internally. Each invocation of the installed Idle function
   760         *  deletes the first Task on this list. This one-at-a-time process 
   761         *  continues until the list is empty.
   762         *
   763         *  @a(Note)
   764         *  This feature is disabled by default.
   765         *
   766         *  If this feature is enable, the user's application must not 
   767         *  also delete terminated tasks or undefined and/or potentially 
   768         *  catastrophic behavior will result.
   769         */
   770        config Bool deleteTerminatedTasks = false;
   771    
   772        /*!
   773         *  Const array that holds the HookSet objects.
   774         *
   775         *  See {@link #hookfunc Hook Functions} for details about HookSets. 
   776         */
   777        config HookSet hooks[length] = [];
   778    
   779        // -------- Module Functions --------
   780    
   781        /*!
   782         *  ======== addHookSet ========
   783         *  addHookSet is used in a config file to add a hook set.
   784         *
   785         *  Configures a set of hook functions for the 
   786         *  Task module. Each set contains these hook functions:
   787         *
   788         *  @p(blist)
   789         *  -Register: A function called before any statically created tasks
   790         *  are initialized at runtime.  The register hook is called at boot time
   791         *  before main() and before interrupts are enabled.
   792         *  -Create: A function that is called when a task is created. 
   793         *  This includes tasks that are created statically and those
   794         *  created dynamically using {@link #create} or {@link #construct}.
   795         *  The create hook is called outside of a Task_disable/enable block and
   796         *   before the task has been added to the ready list.
   797         *  -Ready: A function that is called when a task becomes ready to run.
   798         *   The ready hook is called from within a Task_disable/enable block with
   799         *   interrupts enabled.
   800         *  -Switch: A function that is called just before a task switch
   801         *  occurs. The 'prev' and 'next' task handles are passed to the Switch
   802         *  hook. 'prev' is set to NULL for the initial task switch that occurs
   803         *  during SYS/BIOS startup.  The Switch hook is called from within a
   804         *  Task_disable/enable block with interrupts enabled.
   805         *  -Exit:  A function that is called when a task exits using
   806         *  {@link #exit}.  The exit hook is passed the handle of the exiting
   807         *  task.  The exit hook is called outside of a Task_disable/enable block
   808         *  and before the task has been removed from the kernel lists.
   809         *  -Delete: A function that is called when any task is deleted at
   810         *  run-time with {@link #delete}.  The delete hook is called outside
   811         *  of a Task_disable/enable block.
   812         *  @p
   813         *  Hook functions can only be configured statically.
   814         *
   815         *  See {@link #hookfunc Hook Functions} for more details. 
   816         *
   817         *  HookSet structure elements may be omitted, in which case those
   818         *  elements will not exist.
   819         *
   820         *  For example, the following configuration code defines a HookSet:
   821         *
   822         *  @p(code)
   823         *  // Hook Set 1 
   824         *  Task.addHookSet({
   825         *     registerFxn: '&myRegister1',
   826         *     createFxn:   '&myCreate1',
   827         *     readyFxn:    '&myReady1',
   828         *     switchFxn:   '&mySwitch1',
   829         *     exitFxn:     '&myExit1',
   830         *     deleteFxn:   '&myDelete1'
   831         *  });
   832         *  @p
   833         *
   834         *  @param(hook)    structure of type HookSet
   835         */
   836        metaonly Void addHookSet(HookSet hook);
   837    
   838        /*!
   839         *  @_nodoc
   840         *  ======== Task_startup ========
   841         *  Start the task scheduler.
   842         *
   843         *  Task_startup signals the end of boot operations, enables
   844         *  the Task scheduler and schedules the highest priority ready
   845         *  task for execution.
   846         *
   847         *  Task_startup is called by BIOS_start() after Hwi_enable() 
   848         *  and Swi_enable(). There is no return from this function as the 
   849         *  execution thread is handed to the highest priority ready task.
   850         */
   851        @DirectCall
   852        Void startup();
   853    
   854        /*!
   855         *  ======== Task_disable ========
   856         *  Disable the task scheduler.
   857         *
   858         *  {@link #disable} and {@link #restore} control Task scheduling.
   859         *  {@link #disable} disables all other Tasks from running until
   860         *  {@link #restore} is called. Hardware and Software interrupts
   861         *  can still run.
   862         *
   863         *  {@link #disable} and {@link #restore} allow you to ensure that 
   864         *  statements
   865         *  that must be performed together during critical processing are not
   866         *  preempted by other Tasks.
   867         *
   868         *  The value of the key returned is opaque to applications and is meant
   869         *  to be passed to Task_restore().
   870         *
   871         *  In the following example, the critical section is
   872         *  not preempted by any Tasks.
   873         *
   874         *  @p(code)
   875         *  key = Task_disable();
   876         *      `critical section`
   877         *  Task_restore(key);
   878         *  @p
   879         *
   880         *  You can also use {@link #disable} and {@link #restore} to 
   881         *  create several Tasks and allow them to be invoked in 
   882         *  priority order.
   883         *
   884         *  {@link #disable} calls can be nested.
   885         *
   886         *  @b(returns)     key for use with {@link #restore}
   887         *
   888         *  @a(constraints)
   889         *  Do not call any function that can cause the current task to block 
   890         *  within a {@link #disable}/{@link #restore} block. For example, 
   891         *  {@link ti.sysbios.knl.Semaphore#pend Semaphore_pend} 
   892         *  (if timeout is non-zero), 
   893         *  {@link #sleep}, {@link #yield}, and Memory_alloc can all
   894         *  cause blocking. 
   895         */
   896        @DirectCall
   897        UInt disable();
   898    
   899        /*!
   900         *  @_nodoc
   901         *  ======== enable ========
   902         *  Enable the task scheduler.
   903         *
   904         *  {@link #enable} unconditionally enables the Task scheduler and
   905         *  schedules the highest priority ready task for execution.
   906         *
   907         *  This function is called by {@link #startup} (which is called by
   908         *  {@link ti.sysbios.BIOS#start BIOS_start}) to begin multi-tasking
   909         *  operations.
   910         */
   911        @DirectCall
   912        Void enable();
   913    
   914        /*!
   915         *  ======== restore ========
   916         *  Restore Task scheduling state.
   917         *
   918         *  {@link #disable} and {@link #restore} control Task scheduling
   919         *  {@link #disable} disables all other Tasks from running until
   920         *  {@link #restore} is called. Hardware and Software interrupts
   921         *  can still run.
   922         *
   923         *  {@link #disable} and {@link #restore} allow you to ensure that 
   924         *  statements
   925         *  that must be performed together during critical processing are not
   926         *  preempted.
   927    
   928         *  In the following example, the critical section is not preempted
   929         *  by any Tasks.
   930         *
   931         *  @p(code)
   932         *  key = Task_disable();
   933         *      `critical section`
   934         *  Task_restore(key);
   935         *  @p
   936         *
   937         *  You can also use {@link #disable} and {@link #restore} to create 
   938         *  several Tasks and allow them to be performed in priority order.
   939         *
   940         *  {@link #disable} calls can be nested.
   941         *
   942         *  {@link #restore} returns with interrupts enabled if the key unlocks
   943         *  the scheduler
   944         *
   945         *  @param(key)     key to restore previous Task scheduler state
   946         *
   947         *  @a(constraints)
   948         *  Do not call any function that can cause the current task to block 
   949         *  within a {@link #disable}/{@link #restore} block. For example, 
   950         *  {@link ti.sysbios.knl.Semaphore#pend Semaphore_pend()} 
   951         *  (if timeout is non-zero), 
   952         *  {@link #sleep}, {@link #yield}, and Memory_alloc can all
   953         *  cause blocking. 
   954         *
   955         *  {@link #restore} internally calls Hwi_enable() if the key passed
   956         *  to it results in the unlocking of the Task scheduler (ie if this
   957         *  is root Task_disable/Task_restore pair).
   958         */
   959        @DirectCall
   960        Void restore(UInt key);
   961    
   962        /*!
   963         *  @_nodoc
   964         *  ======== restoreHwi ========
   965         *  Restore Task scheduling state.
   966         *  Used by dispatcher. Does not re-enable Ints.
   967         */
   968        @DirectCall
   969        Void restoreHwi(UInt key);
   970    
   971        /*!
   972         *  ======== self ========
   973         *  Returns a handle to the currently executing Task object.
   974         *
   975         *  Task_self returns the object handle for the currently executing task. 
   976         *  This function is useful when inspecting the object or when the current 
   977         *  task changes its own priority through {@link #setPri}.
   978         *  
   979         *  No task switch occurs when calling Task_self.
   980         *  
   981         *  Task_self will return NULL until Tasking is initiated at the end of 
   982         *  BIOS_start().
   983         *
   984         *  @b(returns)     address of currently executing task object
   985         */
   986        @DirectCall
   987        Handle self();
   988    
   989        /*!
   990         *  ======== selfMacro ========
   991         *  Returns a handle to the currently executing Task object.
   992         *
   993         *  Task_selfMacro is identical to {@link #self} but is implemented as
   994         *  and inline macro.
   995         *
   996         *  @b(returns)     address of currently executing task object
   997         */
   998        @Macro
   999        Handle selfMacro();
  1000    
  1001        /*!
  1002         *  @_nodoc
  1003         *  ======== checkStacks ========
  1004         *  Check for stack overflow.
  1005         *
  1006         *  This function is usually called by the {@link #HookSet} switchFxn to 
  1007         *  make sure task stacks are valid before performing the context 
  1008         *  switch.
  1009         *
  1010         *  If a stack overflow is detected on either the oldTask or the
  1011         *  newTask, a {@link #E_stackOverflow} Error is raised and the system 
  1012         *  exited.
  1013         *
  1014         *  In order to work properly, {@link #checkStacks} requires that the
  1015         *  {@link #initStackFlag} set to true, which it is by default.
  1016         *
  1017         *  You can call {@link #checkStacks} directly from your application. 
  1018         *  For example, you can check the current task's stack integrity 
  1019         *  at any time with a call like the following:
  1020         *
  1021         *  @p(code)
  1022         *  Task_checkStacks(Task_self(), Task_self());
  1023         *  @p
  1024         *
  1025         *  @param(oldTask)  leaving Task Object Ptr
  1026         *  @param(newTask)  entering Task Object Ptr
  1027         */
  1028        @DirectCall
  1029        Void checkStacks(Handle oldTask, Handle newTask);
  1030    
  1031        /*!
  1032         *  ======== exit ========
  1033         *  Terminate execution of the current task.
  1034         *
  1035         *  Task_exit terminates execution of the current task, changing its mode
  1036         *  from {@link #Mode_RUNNING} to {@link #Mode_TERMINATED}. If all tasks 
  1037         *  have been terminated, or if all remaining tasks have their 
  1038         *  vitalTaskFlag attribute set to FALSE, then SYS/BIOS terminates the 
  1039         *  program as a whole by calling the function System_exit with a status 
  1040         *  code of 0.
  1041         * 
  1042         *  Task_exit is automatically called whenever a task returns from its 
  1043         *  top-level function.
  1044         * 
  1045         *  Exit Hooks (see exitFxn in {@link #HookSet}) can be used to provide 
  1046         *  functions that run whenever a task is terminated. The exitFxn Hooks 
  1047         *  are called before the task has been blocked and marked 
  1048         *  {@link #Mode_TERMINATED}.
  1049         *  See {@link #hookfunc Hook Functions} for more information. 
  1050         *
  1051         *  Any SYS/BIOS function can be called from an Exit Hook function. 
  1052         *
  1053         *  Calling {@link #self} within an Exit function returns the task
  1054         *  being exited. Your Exit function declaration should be similar to 
  1055         *  the following:
  1056         *  @p(code)
  1057         *  Void myExitFxn(Void);
  1058         *  @p
  1059         * 
  1060         *  A task switch occurs when calling Task_exit unless the program as a
  1061         *  whole is terminated
  1062         *
  1063         *  @a(constraints)
  1064         *  Task_exit cannot be called from a Swi or Hwi.
  1065         *
  1066         *  Task_exit cannot be called from the program's main() function.
  1067         */
  1068        @DirectCall
  1069        Void exit();
  1070    
  1071        /*!
  1072         *  ======== sleep ========
  1073         *  Delay execution of the current task.
  1074         *
  1075         *  Task_sleep changes the current task's mode from {@link #Mode_RUNNING} 
  1076         *  to {@link #Mode_BLOCKED}, and delays its execution for nticks 
  1077         *  increments of the system clock. The actual time delayed can be up to 
  1078         *  1 system clock tick less than nticks due to granularity in system 
  1079         *  timekeeping.
  1080         *  
  1081         *  After the specified period of time has elapsed, the task reverts to 
  1082         *  the {@link #Mode_READY} mode and is scheduled for execution.
  1083         *  
  1084         *  A task switch always occurs when calling Task_sleep if nticks > 0.
  1085         *
  1086         *  @param(nticks)  number of system clock ticks to sleep
  1087         *
  1088         *  @a(constraints)
  1089         *  Task_sleep cannot be called from a Swi or Hwi, or within a
  1090         *  {@link #disable} / {@link #restore} block.
  1091         *
  1092         *  Task_sleep cannot be called from the program's main() function.
  1093         *
  1094         *  Task_sleep should not be called from within an Idle function. Doing 
  1095         *  so prevents analysis tools from gathering run-time information.
  1096         *
  1097         *  nticks cannot be {@link ti.sysbios.BIOS#WAIT_FOREVER BIOS_WAIT_FOREVER}.
  1098         */
  1099        @DirectCall
  1100        Void sleep(UInt nticks);
  1101    
  1102        /*!
  1103         *  ======== yield ========
  1104         *  Yield processor to equal priority task.
  1105         *
  1106         *  Task_yield yields the processor to another task of equal priority.
  1107         *  
  1108         *  A task switch occurs when you call Task_yield if there is an equal 
  1109         *  priority task ready to run.
  1110         *  
  1111         *  Tasks of higher priority preempt the currently running task without 
  1112         *  the need for a call to Task_yield. If only lower-priority tasks are 
  1113         *  ready to run when you call Task_yield, the current task continues to 
  1114         *  run. Control does not pass to a lower-priority task.
  1115         *
  1116         *  @a(constraints)
  1117         *  When called within an Hwi, the code sequence calling Task_yield
  1118         *  must be invoked by the Hwi dispatcher.
  1119         *
  1120         *  Task_yield cannot be called from the program's main() function.
  1121         */
  1122        @DirectCall
  1123        Void yield();
  1124    
  1125        /*!
  1126         *  ======== getIdleTask ========
  1127         *  returns a handle to idle task object
  1128         */
  1129        @DirectCall
  1130        Handle getIdleTask();
  1131        
  1132        /*!
  1133         *  ======== getNickName ========
  1134         *  
  1135         */
  1136        metaonly String getNickName(Any tskView);
  1137    
  1138    instance:
  1139    
  1140        /*!
  1141         *  ======== create ========
  1142         *  Create a Task.
  1143         *
  1144         *  Task_create creates a new task object. If successful, Task_create
  1145         *  returns the handle of the new task object. If unsuccessful, 
  1146         *  Task_create returns NULL unless it aborts.
  1147         *
  1148         *  The fxn parameter uses the {@link #FuncPtr} type to pass a pointer to 
  1149         *  the function the Task object should run. For example, if myFxn is a 
  1150         *  function in your program, your C code can create a Task object 
  1151         *  to call that 
  1152         *  function as follows: 
  1153         *  
  1154         *  @p(code)
  1155         *  Task_Params taskParams;
  1156         *  
  1157         *  // Create task with priority 15 
  1158         *  Task_Params_init(&taskParams);
  1159         *  taskParams.stackSize = 512;
  1160         *  taskParams.priority = 15;
  1161         *  Task_create((Task_FuncPtr)myFxn, &taskParams, &eb);
  1162         *  @p
  1163         *
  1164         *  The following statements statically create a task in the 
  1165         *  configuration file: 
  1166         *  
  1167         *  @p(code)
  1168         *  var params = new Task.Params;
  1169         *  params.instance.name = "tsk0";
  1170         *  params.arg0 = 1;
  1171         *  params.arg1 = 2;
  1172         *  params.priority = 1;
  1173         *  Task.create('&tsk0_func', params);
  1174         *  @p
  1175         *
  1176         *  If NULL is passed instead of a pointer to an actual Task_Params 
  1177         *  struct, a
  1178         *  default set of parameters is used. The "eb" is an error block that 
  1179         *  you can use
  1180         *  to handle errors that may occur during Task object creation.
  1181         *
  1182         *  The newly created task is placed in {@link #Mode_READY} mode, and is 
  1183         *  scheduled to begin concurrent execution of the following function 
  1184         *  call:
  1185         *
  1186         *  @p(code)
  1187         *  (*fxn)(arg1, arg2);
  1188         *  @p
  1189         *
  1190         *  As a result of being made ready to run, the task runs any
  1191         *  application-wide Ready functions that have been specified.
  1192         *
  1193         *  Task_exit is automatically called if and when the task returns 
  1194         *  from fxn. 
  1195         *  
  1196         *  @p(html)
  1197         *  <B>Create Hook Functions</B>
  1198         *  @p
  1199         *  
  1200         *  You can specify application-wide Create hook functions in your config
  1201         *  file that run whenever a task is created. This includes tasks that 
  1202         *  are created statically and those created dynamically using 
  1203         *  Task_create.
  1204         *
  1205         *  For Task objects created statically, Create functions are called
  1206         *  during the Task module initialization phase of the program startup 
  1207         *  process prior to main().
  1208         *
  1209         *  For Task objects created dynamically, Create functions 
  1210         *  are called after the task handle has been initialized but before the 
  1211         *  task has been placed on its ready queue.
  1212         *
  1213         *  Any SYS/BIOS function can be called from Create functions.
  1214         *  SYS/BIOS passes the task handle of the task being created to each of 
  1215         *  the Create functions. 
  1216         *
  1217         *  All Create function declarations should be similar to this:
  1218         *  @p(code)
  1219         *  Void myCreateFxn(Task_Handle task);
  1220         *  @p
  1221         *
  1222         *  @param(fxn)     Task Function
  1223         *  
  1224         *  @a(constraints)
  1225         *  @p(blist)
  1226         *  - The fxn parameter and the name attribute cannot be NULL.
  1227         *  - The priority attribute must be less than or equal to 
  1228         *  ({@link #numPriorities} - 1) and greater than or equal to one (1)
  1229         *  (priority 0 is owned by the Idle task). 
  1230         *  - The priority can be less than zero (0) for tasks that should not 
  1231         *  execute.
  1232         *  - The stackHeap attribute must identify a valid memory Heap.
  1233         *  @p
  1234         */
  1235        @DirectCall
  1236        create(FuncPtr fxn);
  1237    
  1238        // -------- Handle Parameters --------
  1239    
  1240        /*! Task function argument. Default is 0 */
  1241        config UArg arg0 = 0;
  1242    
  1243        /*! Task function argument. Default is 0 */
  1244        config UArg arg1 = 0;
  1245    
  1246        /*!
  1247         *  Task priority (0 to numPriorities-1 or -1).
  1248         *  Default is 1.
  1249         */
  1250        config Int priority = 1;
  1251    
  1252        /*!
  1253         *  Task stack pointer. Default = null.
  1254         *
  1255         *  Null indicates that the stack is to be allocated by create().
  1256         *
  1257         *  Example: To statically initialize "tsk0"'s stack to a literal
  1258         *  address, use the following syntax:
  1259         *
  1260         *  @p(code)
  1261         *      Program.global.tsk0.stack = $addr(literal);
  1262         *  @p
  1263         *
  1264         */
  1265        config Ptr stack = null;
  1266    
  1267        /*! 
  1268         *  Task stack size in MAUs. 
  1269         *
  1270         *  The default value of 0 means that the module config 
  1271         *  {@link #defaultStackSize} is used.
  1272         */
  1273        config SizeT stackSize = 0;
  1274    
  1275        /*!
  1276         *  Mem section used for statically created task stacks.
  1277         *
  1278         *  Default is inherited from module config defaultStackSection.
  1279         */
  1280        metaonly config String stackSection;
  1281    
  1282        /*!
  1283         *  Mem heap used for dynamically created task stack.
  1284         *
  1285         *  The default value of NULL means that the module config 
  1286         *  {@link #defaultStackHeap} is used.
  1287         */
  1288        config IHeap.Handle stackHeap = null;
  1289    
  1290        /*! Environment data struct. */
  1291        config Ptr env = null;
  1292    
  1293        /*!
  1294         *  Exit system immediately when the last task with this
  1295         *  flag set to TRUE has terminated. 
  1296         *
  1297         *  Default is true.
  1298         */
  1299        config Bool vitalTaskFlag = true;
  1300    
  1301        // -------- Handle Functions --------
  1302    
  1303        /*!
  1304         *  @_nodoc
  1305         *  ======== getArg0 ========
  1306         *  Returns arg0 passed via params to create.
  1307         *
  1308         *  @b(returns)     task's arg0
  1309         */
  1310        @DirectCall
  1311        UArg getArg0();
  1312    
  1313        /*!
  1314         *  @_nodoc
  1315         *  ======== getArg1 ========
  1316         *  Returns arg1 passed via params to create.
  1317         *
  1318         *  @b(returns)     task's arg1
  1319         */
  1320        @DirectCall
  1321        UArg getArg1();
  1322    
  1323        /*!
  1324         *  ======== getEnv ========
  1325         *  Get task environment pointer.
  1326         *
  1327         *  Task_getEnv returns the environment pointer of the specified task. The
  1328         *  environment pointer references an arbitrary application-defined data 
  1329         *  structure.
  1330         *  
  1331         *  If your program uses multiple hook sets, {@link #getHookContext} 
  1332         *  allows you to get environment pointers you have set for a particular 
  1333         *  hook set and Task object combination.
  1334         *
  1335         *  @b(returns)     task environment pointer
  1336         */
  1337        @DirectCall
  1338        Ptr getEnv();
  1339    
  1340        /*!
  1341         *  ======== getHookContext ========
  1342         *  Get hook set's context for a task.
  1343         *
  1344         *  For example, this C code gets the HookContext, prints it,
  1345         *  and sets a new value for the HookContext.
  1346         *
  1347         *  @p(code)
  1348         *  Ptr pEnv;
  1349         *  Task_Handle myTask;
  1350         *  Int myHookSetId1;
  1351         * 
  1352         *  pEnv = Task_getHookContext(task, myHookSetId1);
  1353         * 
  1354         *  System_printf("myEnd1: pEnv = 0x%lx, time = %ld\n", 
  1355         *                (ULong)pEnv, (ULong)Timestamp_get32());
  1356         * 
  1357         *  Task_setHookContext(task, myHookSetId1, (Ptr)0xc0de1);
  1358         *  @p
  1359         *
  1360         *  See {@link #hookfunc Hook Functions} for more details. 
  1361         *
  1362         *  @param(id)      hook set ID
  1363         *  @b(returns)     hook set context for task
  1364         */
  1365        @DirectCall
  1366        Ptr getHookContext(Int id);
  1367    
  1368        /*!
  1369         *  ======== getPri ========
  1370         *  Get task priority.
  1371         *
  1372         *  Task_getPri returns the priority of the referenced task.
  1373         *
  1374         *  @b(returns)     task priority
  1375         */
  1376        @DirectCall
  1377        Int getPri();
  1378    
  1379        /*!
  1380         *  @_nodoc
  1381         *  ======== setArg0 ========
  1382         *  Set arg0 (used primarily for legacy support)
  1383         */
  1384        @DirectCall
  1385        Void setArg0(UArg arg);
  1386    
  1387        /*!
  1388         *  @_nodoc
  1389         *  ======== setArg1 ========
  1390         *  Set arg1 (used primarily for legacy support)
  1391         */
  1392        @DirectCall
  1393        Void setArg1(UArg arg);
  1394    
  1395        /*!
  1396         *  ======== setEnv ========
  1397         *  Set task environment.
  1398         *
  1399         *  Task_setEnv sets the task environment pointer to env. The
  1400         *  environment pointer references an arbitrary application-defined
  1401         *  data structure.
  1402         *
  1403         *  If your program uses multiple hook sets, {@link #setHookContext} 
  1404         *  allows you to set environment pointers for any
  1405         *  hook set and Task object combination.
  1406         *
  1407         *  @param(env)     task environment pointer
  1408         */
  1409        @DirectCall
  1410        Void setEnv(Ptr env);
  1411    
  1412        /*!
  1413         *  ======== setHookContext ========
  1414         *  Set hook instance's context for a task.
  1415         *
  1416         *  For example, this C code gets the HookContext, prints it,
  1417         *  and sets a new value for the HookContext.
  1418         *
  1419         *  @p(code)
  1420         *  Ptr pEnv;
  1421         *  Task_Handle myTask;
  1422         *  Int myHookSetId1;
  1423         * 
  1424         *  pEnv = Task_getHookContext(task, myHookSetId1);
  1425         * 
  1426         *  System_printf("myEnd1: pEnv = 0x%lx, time = %ld\n", 
  1427         *                (ULong)pEnv, (ULong)Timestamp_get32());
  1428         * 
  1429         *  Task_setHookContext(task, myHookSetId1, (Ptr)0xc0de1);
  1430         *  @p
  1431         *
  1432         *  See {@link #hookfunc Hook Functions} for more details. 
  1433         *
  1434         *  @param(id)              hook set ID
  1435         *  @param(hookContext)     value to write to context
  1436         */
  1437        @DirectCall
  1438        Void setHookContext(Int id, Ptr hookContext);
  1439    
  1440        /*!
  1441         *  ======== setPri ========
  1442         *  Set a task's priority
  1443         *
  1444         *  Task_setpri sets the execution priority of task to newpri, and returns
  1445         *  that task's old priority value. Raising or lowering a task's priority
  1446         *  does not necessarily force preemption and re-scheduling of the caller:
  1447         *  tasks in the {@link #Mode_BLOCKED} mode remain suspended despite a
  1448         *  change in priority; and tasks in the {@link #Mode_READY} mode gain
  1449         *  control only if their new priority is greater than that of the
  1450         *  currently executing task.
  1451         *  
  1452         *  The maximum value of newpri is ({@link #numPriorities} - 1). 
  1453         *  The minimum value of newpri is one (1) (The Idle task owns priority 0).
  1454         *  If newpri is less than 0, the task is barred from further execution 
  1455         *  until its priority is raised at a later time by another task; if newpri
  1456         *  equals ({@link #numPriorities} - 1), execution of the task effectively
  1457         *  locks out all other program activity, except for the handling of 
  1458         *  interrupts.
  1459         *  
  1460         *  The current task can change its own priority (and possibly preempt its
  1461         *  execution) by passing the output of {@link #self} as the value of the 
  1462         *  task parameter.
  1463         *  
  1464         *  A context switch occurs when calling Task_setpri if a currently 
  1465         *  running task priority is set lower than the priority of another 
  1466         *  currently ready task, or if another ready task is made to have a
  1467         *  higher priority than the currently running task.
  1468         *
  1469         *  Task_setpri can be used for mutual exclusion.
  1470         *
  1471         *  If a task's new priority is different than its previous priority, 
  1472         *  then its relative placement in its new ready task priority 
  1473         *  queue can be different than the one it was removed from. This can 
  1474         *  effect the relative order in which it becomes the running task.
  1475         *
  1476         *  The effected task is placed at the head of its new priority queue
  1477         *  if it is the currently running task. Otherwise it is placed at
  1478         *  at the end of its new task priority queue.
  1479         *
  1480         *  @param(newpri) task's new priority
  1481         *  @b(returns)     task's old priority
  1482         *
  1483         *  @a(constraints)
  1484         *  newpri must be less than or equal to ({@link #numPriorities} - 1).
  1485         *
  1486         *  The task cannot be in the {@link #Mode_TERMINATED} mode.
  1487         *
  1488         *  The new priority should not be zero (0). This priority level is 
  1489         *  reserved for the Idle task.
  1490         */
  1491        @DirectCall
  1492        UInt setPri(Int newpri);
  1493    
  1494        /*!
  1495         *  ======== stat ========
  1496         *  Retrieve the status of a task.
  1497         *
  1498         *  Task_stat retrieves attribute values and status information about a 
  1499         *  task.
  1500         *  
  1501         *  Status information is returned through statbuf, which references a
  1502         *  structure of type {@link #Stat}.
  1503         *  
  1504         *  When a task is preempted by a software or hardware interrupt, the task
  1505         *  execution mode returned for that task by Task_stat is still 
  1506         *  {@link #Mode_RUNNING}  because the task runs when the preemption ends.
  1507         *
  1508         *  The current task can inquire about itself by passing the output of
  1509         *  {@link #self} as the first argument to Task_stat. However, the task 
  1510         *  stack pointer (sp) in the {@link #Stat} structure is the value from 
  1511         *  the previous context switch.
  1512         *
  1513         *  Task_stat has a non-deterministic execution time. As such, it is not
  1514         *  recommended to call this API from Swis or Hwis.
  1515         *
  1516         *  @param(statbuf) pointer to task status structure
  1517         *
  1518         *  @a(constraints)
  1519         *  statbuf cannot be NULL;
  1520         */
  1521        @DirectCall
  1522        Void stat(Stat *statbuf);
  1523    
  1524        /*!
  1525         *  ======== getMode ========
  1526         *  Retrieve the {@link #Mode} of a task.
  1527         */
  1528        @DirectCall
  1529        Mode getMode();
  1530    
  1531        /*!
  1532         *  @_nodoc
  1533         *  ======== block ========
  1534         *  Block a task.
  1535         *
  1536         *  Remove a task from its ready list.
  1537         *  The effect of this API is manifest the next time the internal
  1538         *  Task scheduler is invoked. 
  1539         *  This can be done directly by embedding the call within a 
  1540         *  {@link #disable}/{@link #restore} block.
  1541         *  Otherwise, the effect will be manifest as a result of processing
  1542         *  the next dispatched interrupt, or by posting a Swi, or by falling
  1543         *  through the task function.
  1544         *
  1545         *  @a(constraints)
  1546         *  If called from within a Hwi or a Swi, or main(), there is no need 
  1547         *  to embed the call within a {@link #disable}/{@link #restore} block.
  1548         */
  1549        @DirectCall
  1550        Void block();
  1551    
  1552        /*!
  1553         *  @_nodoc
  1554         *  ======== unblock ========
  1555         *  Unblock a task.
  1556         *
  1557         *  Place task in its ready list.
  1558         *  The effect of this API is manifest the next time the internal
  1559         *  Task scheduler is invoked. 
  1560         *  This can be done directly by embedding the call within a 
  1561         *  {@link #disable}/{@link #restore} block.
  1562         *  Otherwise, the effect will be manifest as a result of processing
  1563         *  the next dispatched interrupt, or by posting a Swi, or by falling
  1564         *  through the task function.
  1565         *
  1566         *  @a(constraints)
  1567         *  If called from within a Hwi or a Swi, or main(), there is no need 
  1568         *  to embed the call within a {@link #disable}/{@link #restore} block.
  1569         */
  1570        @DirectCall
  1571        Void unblock();
  1572    
  1573        /*!
  1574         *  @_nodoc
  1575         *  ======== blockI ========
  1576         *  Block a task.
  1577         *
  1578         *  Remove a task from its ready list.
  1579         *  Must be called within Task_disable/Task_restore block
  1580         *  with interrupts disabled.
  1581         *  This API is meant to be used internally.
  1582         */
  1583        @DirectCall
  1584        Void blockI();
  1585    
  1586        /*!
  1587         *  @_nodoc
  1588         *  ======== unblockI ========
  1589         *  Unblock a task.
  1590         *
  1591         *  Place task in its ready list.
  1592         *  Must be called within Task_disable/Task_restore block
  1593         *  with interrupts disabled.
  1594         *  This API is meant to be used internally.
  1595         *
  1596         *  @param(hwiKey) key returned from Hwi_disable()
  1597         */
  1598        @DirectCall
  1599        Void unblockI(UInt hwiKey);
  1600    
  1601    
  1602    internal:   /* not for client use */
  1603    
  1604        /*! Target-specific support functions. */
  1605        proxy SupportProxy inherits ti.sysbios.interfaces.ITaskSupport;
  1606    
  1607        /*
  1608         *  ======== schedule ========
  1609         *  Find highest priority ready task and invoke it.
  1610         *
  1611         *  Must be called with interrupts disabled.
  1612         */
  1613        Void schedule();
  1614    
  1615        /*
  1616         *  ======== enter ========
  1617         *  Task's initial entry point before entering task function.
  1618         */
  1619        Void enter();
  1620    
  1621        /*
  1622         *  ======== sleepTimeout ========
  1623         *  This function is the clock event handler for sleep.
  1624         */
  1625        Void sleepTimeout(UArg arg);
  1626    
  1627        /*
  1628         *  ======== postInit ========
  1629         *  finish initializing static and dynamic Tasks
  1630         */
  1631        Int postInit(Object *swi, Error.Block *eb);
  1632    
  1633        /*
  1634         *  Number of statically constructed Task objects.
  1635         *  Shouldn't be set directly by the user's
  1636         *  config (it gets set by instance$static$init).
  1637         */
  1638        config UInt numConstructedTasks = 0;
  1639    
  1640        /*
  1641         *  ======== allBlockedFunction ========
  1642         *  default function to be called
  1643         */
  1644        Void allBlockedFunction();
  1645    
  1646        /*
  1647         *  ======== deleteTerminatedTasksFunc ========
  1648         *  Idle func that deletes the first terminated task it finds
  1649         *  in the queue of dynamically created tasks
  1650         */
  1651        Void deleteTerminatedTasksFunc();
  1652    
  1653        /* 
  1654         *  Common object used by all blocked tasks to enable Task_delete()
  1655         *  to remove a task from any pend Q it is placed on while blocked.
  1656         */
  1657        struct PendElem {
  1658            Queue.Elem              qElem;
  1659            Task.Handle             task;
  1660            Clock.Handle            clock;
  1661        };
  1662    
  1663        struct Instance_State {
  1664            Queue.Elem      qElem;      // Task's readyQ element
  1665            volatile Int    priority;   // Task priority
  1666            UInt            mask;       // curSet mask = 1 << priority
  1667            Ptr             context;    // ptr to Task's saved context
  1668                                        // while not in RUNNING mode.
  1669            Mode            mode;       // READY, BLOCKED, RUNNING, etc
  1670            PendElem        *pendElem;  // ptr to Task, Semaphore, Event,
  1671                                        // or GateMutexPri PendElem
  1672            SizeT           stackSize;  // Task's stack buffer size
  1673            Char            stack[];    // buffer used for Task's stack
  1674            IHeap.Handle    stackHeap;  // Heap to allocate stack from
  1675            FuncPtr         fxn;        // Task function
  1676            UArg            arg0;       // Task function 1st arg
  1677            UArg            arg1;       // Task function 2nd arg
  1678            Ptr             env;        // Task environment pointer
  1679            Ptr             hookEnv[];  // ptr to Task's hook env array
  1680            Bool            vitalTaskFlag; // TRUE = shutdown system if
  1681                                        // last task like this exits
  1682            Queue.Handle    readyQ;     // This Task's readyQ
  1683        };
  1684    
  1685        struct Module_State {
  1686            Bool            locked;     // Task scheduler locked flag
  1687            volatile UInt   curSet;     // Bitmask reflects readyQ states
  1688            Bool            workFlag;   // Scheduler work is pending.
  1689                                        // Optimization. Must be set
  1690                                        // whenever readyQs are modified.
  1691            UInt            vitalTasks; // number of tasks with
  1692                                        // vitalTaskFlag = true
  1693            Handle          curTask;    // current Task instance
  1694            Queue.Handle    curQ;       // current Task's readyQ
  1695            Queue.Object    readyQ[];   // Task ready queues
  1696            Queue.Object    inactiveQ;  // Task's with -1 priority
  1697            Queue.Object    terminatedQ;// terminated dynamically created Tasks
  1698            Handle          idleTask;   // Idle Task
  1699            Handle          constructedTasks[]; // array of statically 
  1700                                        // constructed Tasks
  1701        };
  1702    }
  1703    
  1704    /*
  1705     *  @(#) ti.sysbios.knl; 2, 0, 0, 0,451; 2-2-2011 15:07:15; /db/vtree/library/trees/avala/avala-o27x/src/ xlibrary
  1706    
  1707     */
  1708