Commit 983e8519 authored by 陈德's avatar 陈德
Browse files

feat:preliminary version of removereadytorun

No related merge requests found
Showing with 268 additions and 0 deletions
+268 -0
/****************************************************************************
* sched/sched/sched_removereadytorun.c
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <nuttx/sched.h>
#include <sched.h>
#include <stdbool.h>
#include <assert.h>
#include <nuttx/queue.h>
#include <nuttx/sched_note.h>
#include "irq/irq.h"
#include "sched/sched.h"
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: nxsched_remove_readytorun
*
* Description:
* This function removes a TCB from the ready to run list.
*
* Input Parameters:
* rtcb - Points to the TCB that is ready-to-run
* merge - Merge pending list or not
*
* Returned Value:
* true if the currently active task (the head of the ready-to-run list)
* has changed.
*
* Assumptions:
* - The caller has established a critical section before calling this
* function (calling sched_lock() first is NOT a good idea -- use
* enter_critical_section()).
* - The caller handles the condition that occurs if the head of the
* ready-to-run list is changed.
*
****************************************************************************/
#ifdef CONFIG_SMP
bool nxsched_remove_readytorun_new(FAR struct tcb_s *rtcb, bool merge)
{
FAR dq_queue_t *tasklist;
bool doswitch = false;
int cpu;
int me;
irqstate_t flags;
/*首先我们需要关闭中断
*/
flags=up_irq_save();
/* Which CPU (if any) is the task running on? Which task list holds the
* TCB?
*/
cpu = rtcb->cpu;
me=this_cpu();
tasklist = TLIST_HEAD(rtcb, cpu);
/* Check if the TCB to be removed is at the head of a ready-to-run list.
* For the case of SMP, there are two lists involved: (1) the
* g_readytorun list that holds non-running tasks that have not been
* assigned to a CPU, and (2) and the g_assignedtasks[] lists which hold
* tasks assigned a CPU, including the task that is currently running on
* that CPU. Only this latter list contains the currently active task
* only removing the head of that list can result in a context switch.
*
* rtcb->blink == NULL will tell us if the TCB is at the head of the
* ready-to-run list and, hence, a candidate for the new running task.
*
* If so, then the tasklist RUNNABLE attribute will inform us if the list
* holds the currently executing task and, hence, if a context switch
* should occur.
*/
/*如果我们删除的是当前运行的任务,我们需要做的额外操作是
* 1. 尝试从别的CPU队列拉取一个更高优先级的任务
* 2. 更新running_cpupri
* 否则删除的只是普通就绪任务,我们只需要
* 1. 更新ready_cpupri
*/
if (rtcb->blink == NULL && TLIST_ISRUNNABLE(rtcb->task_state))
{
FAR struct tcb_s *nxttcb;
FAR struct tcb_s *rtrtcb = NULL;
/* There must always be at least one task in the list (the IDLE task)
* after the TCB being removed.
*/
nxttcb = rtcb->flink;
DEBUGASSERT(nxttcb != NULL);
/* If we are modifying the head of some assigned task list other than
* our own, we will need to stop that CPU.
*/
if (cpu != me)
{
DEBUGVERIFY(up_cpu_pause(cpu));
}
/* The task is running but the CPU that it was running on has been
* paused. We can now safely remove its TCB from the ready-to-run
* task list. In the SMP case this may be either the g_readytorun()
* or the g_assignedtasks[cpu] list.
*/
dq_rem((FAR dq_entry_t *)rtcb, tasklist);
/* Which task will go at the head of the list? It will be either the
* next tcb in the assigned task list (nxttcb) or a TCB in the
* g_readytorun list. We can only select a task from that list if
* the affinity mask includes the current CPU.
*/
/*
*before Search next task to run,we must try to pull a possible higher priority task.
*
*/
sched_lock();
pull();
sched_unlock();
/* Search for the highest priority task that can run on this
* CPU.
*/
for (rtrtcb = (FAR struct tcb_s *)list_readytorun()->head;
rtrtcb != NULL && !CPU_ISSET(cpu, &rtrtcb->affinity);
rtrtcb = rtrtcb->flink);
/* Did we find a task in the g_readytorun list? Which task should
* we use? We decide strictly by the priority of the two tasks:
* Either (1) the task currently at the head of the
* g_assignedtasks[cpu] list (nexttcb) or (2) the highest priority
* task from the g_readytorun list with matching affinity (rtrtcb).
*/
if (rtrtcb != NULL && rtrtcb->sched_priority >= nxttcb->sched_priority)
{
/* The TCB rtrtcb has the higher priority and it can be run on
* target CPU. Remove that task (rtrtcb) from the g_readytorun
* list and add to the head of the g_assignedtasks[cpu] list.
*/
dq_rem((FAR dq_entry_t *)rtrtcb, list_readytorun());
dq_addfirst((FAR dq_entry_t *)rtrtcb, tasklist);
rtrtcb->cpu = cpu;
nxttcb = rtrtcb;
}
/* Will pre-emption be disabled after the switch? If the lockcount is
* greater than zero, then this task/this CPU holds the scheduler lock.
*/
if (nxttcb->lockcount > 0)
{
/* Yes... make sure that scheduling logic knows about this */
spin_setbit(&g_cpu_lockset, cpu, &g_cpu_locksetlock,
&g_cpu_schedlock);
}
else
{
/* No.. we may need to perform release our hold on the lock. */
spin_clrbit(&g_cpu_lockset, cpu, &g_cpu_locksetlock,
&g_cpu_schedlock);
}
/* NOTE: If the task runs on another CPU(cpu), adjusting global IRQ
* controls will be done in the pause handler on the new CPU(cpu).
* If the task is scheduled on this CPU(me), do nothing because
* this CPU already has a critical section
*/
nxttcb->task_state = TSTATE_TASK_RUNNING;
/* All done, restart the other CPU (if it was paused). */
doswitch = true;
if (cpu != me)
{
/* In this we will not want to report a context switch to this
* CPU. Only the other CPU is affected.
*/
DEBUGVERIFY(up_cpu_resume(cpu));
doswitch = false;
}
/*
* update running_cpupri
*/
enter_critical_section_cpupri(me);
preempt_running_cpupri(cpu, nxttcb->sched_priority);
leave_critical_section_cpupri(me);
}
else
{
/* The task is not running. Just remove its TCB from the ready-to-run
* list. In the SMP case this may be either the g_readytorun() or the
* g_assignedtasks[cpu] list.
*/
dq_rem((FAR dq_entry_t *)rtcb, tasklist);
enter_critical_section_cpupri(me);
/*using the head task of readytorun update ready_cpupri*/
struct tcb_s *ready_tcb = (struct tcb_s *)list_readytorun()->head;
replace_ready_cpupri(me, ready_tcb->sched_priority);
leave_critical_section_cpupri(me);
}
/* Since the TCB is no longer in any list, it is now invalid */
rtcb->task_state = TSTATE_TASK_INVALID;
if (list_pendingtasks()->head && merge)
{
doswitch |= nxsched_merge_pending();
}
return doswitch;
}
#endif /* CONFIG_SMP */
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment