Exemplo n.º 1
0
int sched_unlock(void)
{
  struct tcb_s *rtcb = (struct tcb_s*)g_readytorun.head;

  /* Check for some special cases:  (1) rtcb may be NULL only during
   * early boot-up phases, and (2) sched_unlock() should have no
   * effect if called from the interrupt level.
   */

  if (rtcb && !up_interrupt_context())
    {
      /* Prevent context switches throughout the following */

      irqstate_t flags = irqsave();

      /* Decrement the preemption lock counter */

      if (rtcb->lockcount)
        {
          rtcb->lockcount--;
        }

      /* Check if the lock counter has decremented to zero.  If so,
       * then pre-emption has been re-enabled.
       */

      if (rtcb->lockcount <= 0)
        {
          rtcb->lockcount = 0;

          /* Release any ready-to-run tasks that have collected in
           * g_pendingtasks.
           */

         if (g_pendingtasks.head)
           {
             up_release_pending();
           }
        }

      irqrestore(flags);
    }

  return OK;
}
Exemplo n.º 2
0
void leave_critical_section(irqstate_t flags)
{
  /* Do nothing if called from an interrupt handler */

  if (!up_interrupt_context())
    {
      FAR struct tcb_s *rtcb = this_task();
      DEBUGASSERT(rtcb != 0 && rtcb->irqcount > 0);

      /* Will we still have interrupts disabled after decrementing the
       * count?
       */

      if (rtcb->irqcount > 1)
        {
          /* Yes... make sure that the spinlock is set */

          DEBUGASSERT(g_cpu_irqlock == SP_LOCKED);
          rtcb->irqcount--;
        }
      else
        {
#ifdef CONFIG_SCHED_INSTRUMENTATION_CSECTION
          /* No.. Note that we have entered the critical section */

          sched_note_csection(rtcb, false);
#endif
          /* Decrement our count on the lock.  If all CPUs have released,
           * then unlock the spinlock.
           */

          rtcb->irqcount = 0;
          spin_clrbit(&g_cpu_irqset, this_cpu(), &g_cpu_irqsetlock,
                      &g_cpu_irqlock);

          /* Have all CPUs release the lock? */

          if (!spin_islocked(&g_cpu_irqlock))
            {
              /* Check if there are pending tasks and that pre-emption is
               * also enabled.
               */

              if (g_pendingtasks.head != NULL && !spin_islocked(&g_cpu_schedlock))
                {
                  /* Release any ready-to-run tasks that have collected in
                   * g_pendingtasks if the scheduler is not locked.
                   *
                   * NOTE: This operation has a very high likelihood of causing
                   * this task to be switched out!
                   */

                  up_release_pending();
                }
            }
        }

      /* Restore the previous interrupt state which may still be interrupts
       * disabled (but we don't have a mechanism to verify that now)
       */

      up_irq_restore(flags);
    }
}
Exemplo n.º 3
0
int sched_unlock(void)
{
  FAR struct tcb_s *rtcb = (FAR struct tcb_s *)g_readytorun.head;

  /* Check for some special cases:  (1) rtcb may be NULL only during
   * early boot-up phases, and (2) sched_unlock() should have no
   * effect if called from the interrupt level.
   */

  if (rtcb && !up_interrupt_context())
    {
      /* Prevent context switches throughout the following */

      irqstate_t flags = irqsave();

      /* Decrement the preemption lock counter */

      if (rtcb->lockcount)
        {
          rtcb->lockcount--;
        }

      /* Check if the lock counter has decremented to zero.  If so,
       * then pre-emption has been re-enabled.
       */

      if (rtcb->lockcount <= 0)
        {
          rtcb->lockcount = 0;

          /* Release any ready-to-run tasks that have collected in
           * g_pendingtasks.
           */

          if (g_pendingtasks.head)
            {
              up_release_pending();
            }

#if CONFIG_RR_INTERVAL > 0
          /* If (1) the task that was running supported round-robin
           * scheduling and (2) if its time slice has already expired, but
           * (3) it could not slice out because pre-emption was disabled,
           * then we need to swap the task out now and reassess the interval
           * timer for the next time slice.
           */

          if ((rtcb->flags & TCB_FLAG_POLICY_MASK) == TCB_FLAG_SCHED_RR &&
              rtcb->timeslice == 0)
            {
              /* Yes.. that is the situation.  But one more thing.  The call
               * to up_release_pending() above may have actually replaced
               * the task at the head of the read-to-run list.  In that case,
               * we need only to reset the timeslice value back to the
               * maximum.
               */

              if (rtcb != (FAR struct tcb_s *)g_readytorun.head)
                {
                  rtcb->timeslice = MSEC2TICK(CONFIG_RR_INTERVAL);
                }
#ifdef CONFIG_SCHED_TICKLESS
              else
                {
                  sched_timer_reassess();
                }
#endif
            }
#endif

#ifdef CONFIG_SCHED_SPORADIC
#if CONFIG_RR_INTERVAL > 0
          else
#endif
          /* If (1) the task that was running supported sporadic scheduling
           * and (2) if its budget slice has already expired, but (3) it
           * could not slice out because pre-emption was disabled, then we
           * need to swap the task out now and reassess the interval timer
           * for the next time slice.
           */

          if ((rtcb->flags & TCB_FLAG_POLICY_MASK) == TCB_FLAG_SCHED_SPORADIC &&
              rtcb->timeslice < 0)
            {
              /* Yes.. that is the situation.  Force the low-priority state
               * now
               */

              sched_sporadic_lowpriority(rtcb);

#ifdef CONFIG_SCHED_TICKLESS
              /* Make sure that the call to up_release_pending() did not
               * change the currently active task.
               */

              if (rtcb == (FAR struct tcb_s *)g_readytorun.head)
                {
                  sched_timer_reassess();
                }
#endif
            }
#endif
        }

      irqrestore(flags);
    }

  return OK;
}