nuttx/sched/sched/sched_cpuselect.c
Alin Jerpelea 8935ac4cc3 sched: Author Gregory Nutt: update licenses to Apache
Gregory Nutt has submitted the SGA and we can mograte the licenses
to Apache.

Signed-off-by: Alin Jerpelea <alin.jerpelea@sony.com>
2021-02-09 01:21:53 -08:00

113 lines
3.4 KiB
C

/****************************************************************************
* sched/sched/sched_cpuselect.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 <sys/types.h>
#include <assert.h>
#include <nuttx/sched.h>
#include "sched/sched.h"
#ifdef CONFIG_SMP
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#define IMPOSSIBLE_CPU 0xff
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: nxsched_select_cpu
*
* Description:
* Return the index to the CPU with the lowest priority running task,
* possibly its IDLE task.
*
* Input Parameters:
* affinity - The set of CPUs on which the thread is permitted to run.
*
* Returned Value:
* Index of the CPU with the lowest priority running task
*
* Assumptions:
* Called from within a critical section.
*
****************************************************************************/
int nxsched_select_cpu(cpu_set_t affinity)
{
uint8_t minprio;
int cpu;
int i;
/* Otherwise, find the CPU that is executing the lowest priority task
* (possibly its IDLE task).
*/
minprio = SCHED_PRIORITY_MAX;
cpu = IMPOSSIBLE_CPU;
for (i = 0; i < CONFIG_SMP_NCPUS; i++)
{
/* If the thread permitted to run on this CPU? */
if ((affinity & (1 << i)) != 0)
{
FAR struct tcb_s *rtcb = (FAR struct tcb_s *)
g_assignedtasks[i].head;
/* If this thread is executing its IDLE task, the use it. The
* IDLE task is always the last task in the assigned task list.
*/
if (rtcb->flink == NULL)
{
/* The IDLE task should always be assigned to this CPU and have
* a priority of zero.
*/
DEBUGASSERT(rtcb->sched_priority == 0);
return i;
}
else if (rtcb->sched_priority < minprio)
{
DEBUGASSERT(rtcb->sched_priority > 0);
minprio = rtcb->sched_priority;
cpu = i;
}
}
}
DEBUGASSERT(cpu != IMPOSSIBLE_CPU);
return cpu;
}
#endif /* CONFIG_SMP */