2015-10-02 17:43:18 -06:00
|
|
|
/****************************************************************************
|
2015-07-24 11:49:28 -06:00
|
|
|
* sched/sched/sched_roundrobin.c
|
|
|
|
*
|
2020-05-09 14:08:44 -06:00
|
|
|
* 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
|
2015-07-24 11:49:28 -06:00
|
|
|
*
|
2020-05-09 14:08:44 -06:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2015-07-24 11:49:28 -06:00
|
|
|
*
|
2020-05-09 14:08:44 -06:00
|
|
|
* 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.
|
2015-07-24 11:49:28 -06:00
|
|
|
*
|
2015-10-02 17:43:18 -06:00
|
|
|
****************************************************************************/
|
2015-07-24 11:49:28 -06:00
|
|
|
|
2015-10-02 17:43:18 -06:00
|
|
|
/****************************************************************************
|
2015-07-24 11:49:28 -06:00
|
|
|
* Included Files
|
2015-10-02 17:43:18 -06:00
|
|
|
****************************************************************************/
|
2015-07-24 11:49:28 -06:00
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <sched.h>
|
2023-02-01 10:41:12 -03:00
|
|
|
#include <sys/param.h>
|
2015-07-24 11:49:28 -06:00
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
#include <nuttx/sched.h>
|
|
|
|
#include <nuttx/clock.h>
|
|
|
|
|
|
|
|
#include "sched/sched.h"
|
|
|
|
|
|
|
|
#if CONFIG_RR_INTERVAL > 0
|
|
|
|
|
2015-10-02 17:43:18 -06:00
|
|
|
/****************************************************************************
|
2015-07-24 11:49:28 -06:00
|
|
|
* Public Functions
|
2015-10-02 17:43:18 -06:00
|
|
|
****************************************************************************/
|
2015-07-24 11:49:28 -06:00
|
|
|
|
2015-10-02 17:43:18 -06:00
|
|
|
/****************************************************************************
|
2020-05-09 12:40:14 -06:00
|
|
|
* Name: nxsched_process_roundrobin
|
2015-07-24 11:49:28 -06:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Check if the currently executing task has exceeded its time slice.
|
|
|
|
*
|
2018-02-01 10:00:02 -06:00
|
|
|
* Input Parameters:
|
2015-07-24 11:49:28 -06:00
|
|
|
* tcb - The TCB of the currently executing task
|
|
|
|
* ticks - The number of ticks that have elapsed on the interval timer.
|
|
|
|
* noswitches - True: Can't do context switches now.
|
|
|
|
*
|
2018-02-01 10:00:02 -06:00
|
|
|
* Returned Value:
|
2015-07-24 11:49:28 -06:00
|
|
|
* The number if ticks remaining until the next time slice expires.
|
|
|
|
* Zero is returned if there is no time slicing (i.e., the task at the
|
|
|
|
* head of the ready-to-run list does not support round robin
|
|
|
|
* scheduling).
|
|
|
|
*
|
|
|
|
* The value one may returned under certain circumstances that probably
|
|
|
|
* can't happen. The value one is the minimal timer setup and it means
|
|
|
|
* that a context switch is needed now, but cannot be performed because
|
|
|
|
* noswitches == true.
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* - Interrupts are disabled
|
|
|
|
* - The task associated with TCB uses the round robin scheduling
|
|
|
|
* policy
|
|
|
|
*
|
2015-10-02 17:43:18 -06:00
|
|
|
****************************************************************************/
|
2015-07-24 11:49:28 -06:00
|
|
|
|
2020-05-09 12:40:14 -06:00
|
|
|
uint32_t nxsched_process_roundrobin(FAR struct tcb_s *tcb, uint32_t ticks,
|
2020-05-09 14:08:44 -06:00
|
|
|
bool noswitches)
|
2015-07-24 11:49:28 -06:00
|
|
|
{
|
|
|
|
uint32_t ret;
|
|
|
|
int decr;
|
|
|
|
|
|
|
|
/* How much can we decrement the timeslice delay? If 'ticks' is greater
|
|
|
|
* than the timeslice value, then we ignore any excess amount.
|
|
|
|
*
|
2016-11-21 14:43:56 -06:00
|
|
|
* 'ticks' should not be greater than the remaining timeslice. But that
|
|
|
|
* event seems to be possible, perhaps in cases where pre-emption has been
|
|
|
|
* disabled or the noswitches flag is set. This might cause jitter of a
|
|
|
|
* few ticks in the slicing because the excess amount is not handled.
|
2015-07-24 11:49:28 -06:00
|
|
|
*/
|
|
|
|
|
2016-11-21 14:43:56 -06:00
|
|
|
DEBUGASSERT(tcb != NULL);
|
2015-07-24 11:49:28 -06:00
|
|
|
decr = MIN(tcb->timeslice, ticks);
|
|
|
|
|
|
|
|
/* Decrement the timeslice counter */
|
|
|
|
|
|
|
|
tcb->timeslice -= decr;
|
|
|
|
|
2016-11-21 14:43:56 -06:00
|
|
|
/* Did decrementing the timeslice counter cause the timeslice to expire? */
|
2015-07-24 11:49:28 -06:00
|
|
|
|
|
|
|
ret = tcb->timeslice;
|
2020-05-09 12:40:14 -06:00
|
|
|
if (tcb->timeslice <= 0 && !nxsched_islocked_tcb(tcb))
|
2015-07-24 11:49:28 -06:00
|
|
|
{
|
|
|
|
/* We will also suppress context switches if we were called via one
|
2020-05-09 12:40:14 -06:00
|
|
|
* of the unusual cases handled by nxsched_reassess_timer(). In that
|
2015-07-24 11:49:28 -06:00
|
|
|
* case, we will return a value of one so that the timer will expire
|
|
|
|
* as soon as possible and we can perform this action in the normal
|
|
|
|
* timer expiration context.
|
|
|
|
*
|
|
|
|
* This is kind of kludge, but I am not to concerned because I hope
|
|
|
|
* that the situation is impossible or at least could only occur on
|
|
|
|
* rare corner-cases.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (noswitches)
|
|
|
|
{
|
|
|
|
ret = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Reset the timeslice. */
|
|
|
|
|
|
|
|
tcb->timeslice = MSEC2TICK(CONFIG_RR_INTERVAL);
|
|
|
|
ret = tcb->timeslice;
|
|
|
|
|
|
|
|
/* We know we are at the head of the ready to run prioritized
|
|
|
|
* list. We must be the highest priority task eligible for
|
|
|
|
* execution. Check the next task in the ready to run list. If
|
|
|
|
* it is the same priority, then we need to relinquish the CPU and
|
|
|
|
* give that task a shot.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (tcb->flink &&
|
|
|
|
tcb->flink->sched_priority >= tcb->sched_priority)
|
|
|
|
{
|
2022-11-15 10:05:50 +08:00
|
|
|
FAR struct tcb_s *rtcb = this_task();
|
|
|
|
|
2015-07-24 11:49:28 -06:00
|
|
|
/* Just resetting the task priority to its current value.
|
Sources and Docs: Fix typos and nxstyle issues
Documentation/contributing/coding_style.rst:
* Fix repeated words: ("this this").
* Remove trailing spaces.
boards/z80/z80/z80sim/README.txt:
* Fix repeated words: ("this this") and rewrap lines.
graphics/Kconfig,
libs/libc/math/Kconfig:
* Fix repeated words: ("this this").
arch/arm/src/armv7-a/arm_assert.c,
arch/arm/src/armv7-r/arm_assert.c,
arch/arm/src/imxrt/imxrt_enet.c,
arch/arm/src/kinetis/kinetis_enet.c,
arch/arm/src/kinetis/kinetis_flexcan.c,
arch/arm/src/s32k1xx/s32k1xx_enet.c,
arch/arm/src/s32k1xx/s32k1xx_flexcan.c,
arch/arm/src/stm32/stm32_pwm.c,
arch/arm/src/stm32h7/stm32_pwm.c,
arch/arm/src/stm32l4/stm32l4_pwm.c,
arch/renesas/src/rx65n/rx65n_usbdev.c,
binfmt/libnxflat/libnxflat_bind.c,
drivers/pipes/pipe_common.c,
net/igmp/igmp_input.c,
net/tcp/tcp_conn.c,
sched/sched/sched_roundrobin.c:
* Fix typo in comment ("this this").
arch/arm/src/cxd56xx/cxd56_usbdev.c,
arch/arm/src/lc823450/lc823450_usbdev.c:
* Fix typo in comment and rewrap lines.
arch/arm/src/imxrt/imxrt_usbdev.c,
arch/arm/src/stm32/stm32_dac.c,
arch/arm/src/stm32f0l0g0/stm32_pwm.c,
arch/arm/src/stm32f7/stm32_pwm.c,
arch/arm/src/tiva/lm/lm4f_gpio.h,
fs/nxffs/nxffs_write.c,
include/nuttx/analog/pga11x.h,
include/nuttx/usb/usbdev.h,
net/mld/mld_join.c:
* Fix typo in comment ("this this").
* Fix nxstyle issues.
2020-10-01 19:29:35 -04:00
|
|
|
* This will cause the task to be rescheduled behind any
|
2015-07-24 11:49:28 -06:00
|
|
|
* other tasks at the same priority.
|
|
|
|
*/
|
|
|
|
|
2022-11-15 10:05:50 +08:00
|
|
|
if (nxsched_reprioritize_rtr(tcb, tcb->sched_priority))
|
|
|
|
{
|
2022-11-15 16:52:40 +08:00
|
|
|
up_switch_context(this_task(), rtcb);
|
2022-11-15 10:05:50 +08:00
|
|
|
}
|
2015-07-24 11:49:28 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* CONFIG_RR_INTERVAL > 0 */
|