Esempio n. 1
0
void up_unblock_task(FAR _TCB *tcb)
{
    /* Verify that the context switch can be performed */

    if ((tcb->task_state < FIRST_BLOCKED_STATE) ||
            (tcb->task_state > LAST_BLOCKED_STATE))
    {
        PANIC(OSERR_BADUNBLOCKSTATE);
    }
    else
    {
        FAR _TCB *rtcb = (FAR _TCB*)g_readytorun.head;

        dbg("Unblocking TCB=%p\n", tcb);

        /* Remove the task from the blocked task list */

        sched_removeblocked(tcb);

        /* Reset its timeslice.  This is only meaningful for round
         * robin tasks but it doesn't here to do it for everything
         */

#if CONFIG_RR_INTERVAL > 0
        tcb->timeslice = CONFIG_RR_INTERVAL / MSEC_PER_TICK;
#endif

        /* Add the task in the correct location in the prioritized
         * g_readytorun task list
         */

        if (sched_addreadytorun(tcb))
        {
            /* The currently active task has changed! We need to do
             * a context switch to the new task.
             *
             * Are we in an interrupt handler?
             */

            if (g_irqtos)
            {
                /* Yes, then we have to do things differently.
                 * Just copy the current stack into the OLD rtcb.
                 */

                up_saveirqcontext(&rtcb->xcp);

                /* Restore the exception context of the rtcb at the (new) head
                 * of the g_readytorun task list.
                 */

                rtcb = (FAR _TCB*)g_readytorun.head;
                dbg("New Active Task TCB=%p\n", rtcb);

                /* Then setup so that the context will be performed on exit
                 * from the interrupt.
                 */

                g_irqcontext = &rtcb->xcp;
            }

            /* We are not in an interrupt andler.  Copy the user C context
             * into the TCB of the task that was previously active.  if
             * up_savecontext returns a non-zero value, then this is really the
             * previously running task restarting!
             */

            else if (!up_savecontext(&rtcb->xcp))
            {
                /* Restore the exception context of the new task that is ready to
                 * run (probably tcb).  This is the new rtcb at the head of the
                 * g_readytorun task list.
                 */

                rtcb = (FAR _TCB*)g_readytorun.head;
                dbg("New Active Task TCB=%p\n", rtcb);

                /* Then switch contexts */

                up_restorecontext(&rtcb->xcp);
            }
        }
    }
}
void up_block_task(FAR struct tcb_s *tcb, tstate_t task_state)
{
  FAR struct tcb_s *rtcb = (FAR struct tcb_s*)g_readytorun.head;
  bool switch_needed;

  /* Verify that the context switch can be performed */

  ASSERT((tcb->task_state >= FIRST_READY_TO_RUN_STATE) &&
         (tcb->task_state <= LAST_READY_TO_RUN_STATE));

  dbg("Blocking TCB=%p\n", tcb);

  /* Remove the tcb task from the ready-to-run list.  If we
   * are blocking the task at the head of the task list (the
   * most likely case), then a context switch to the next
   * ready-to-run task is needed. In this case, it should
   * also be true that rtcb == tcb.
   */

  switch_needed = sched_removereadytorun(tcb);

  /* Add the task to the specified blocked task list */

  sched_addblocked(tcb, (tstate_t)task_state);

  /* If there are any pending tasks, then add them to the g_readytorun
   * task list now
   */

  if (g_pendingtasks.head)
    {
      switch_needed |= sched_mergepending();
    }

  /* Now, perform the context switch if one is needed */

  if (switch_needed)
    {
      /* Are we in an interrupt handler? */

      if (g_irqtos)
        {
          /* Yes, then we have to do things differently.
           * Just copy the current registers into the OLD rtcb.
           */

          up_saveirqcontext(&tcb->xcp);

          /* Restore the exception context of the rtcb at the (new) head
           * of the g_readytorun task list.
           */

          rtcb = (FAR struct tcb_s*)g_readytorun.head;
          dbg("New Active Task TCB=%p\n", rtcb);

          /* Then setup so that the context will be performed on exit
           * from the interrupt.
           */

          g_irqcontext = &rtcb->xcp;
        }

      /* Copy the user C context into the TCB at the (old) head of the
       * g_readytorun Task list. if up_savecontext returns a non-zero
       * value, then this is really the previously running task restarting!
       */

      else if (!up_savecontext(&rtcb->xcp))
        {
          /* Restore the exception context of the rtcb at the (new) head
           * of the g_readytorun task list.
           */

          rtcb = (FAR struct tcb_s*)g_readytorun.head;
          dbg("New Active Task TCB=%p\n", rtcb);

          /* Then switch contexts */

          up_restorecontext(&rtcb->xcp);
        }
    }
}