Skip to content

Commit

Permalink
core/sched: added sched_change_priority() function
Browse files Browse the repository at this point in the history
  • Loading branch information
haukepetersen committed Jun 14, 2018
1 parent 9a8b9f4 commit 976d17f
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 9 deletions.
18 changes: 17 additions & 1 deletion core/include/sched.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2014 Freie Universität Berlin
* Copyright (C) 2014-2017 Freie Universität Berlin
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
Expand Down Expand Up @@ -75,6 +75,7 @@
* @brief Scheduler API definition
*
* @author Kaspar Schleiser <kaspar@schleiser.de>
* @author Hauke Petersen <hauke.petersen@fu-berlin.de>
*/

#ifndef SCHED_H
Expand Down Expand Up @@ -174,6 +175,21 @@ extern clist_node_t sched_runqueues[SCHED_PRIO_LEVELS];
*/
NORETURN void sched_task_exit(void);

#ifdef MODULE_CORE_PRIORITY_INHERITANCE
/**
* @brief Change the priority of the given thread
*
* @note This functions expects interrupts to be disabled when called!
*
* @pre (thread != NULL)
* @pre (priority < SCHED_PRIO_LEVELS)
*
* @param[in,out] thread target thread
* @param[in] priority new priority to assign to @p thread
*/
void sched_change_priority(thread_t *thread, uint8_t priority);
#endif

#ifdef MODULE_SCHEDSTATISTICS
/**
* Scheduler statistics
Expand Down
42 changes: 34 additions & 8 deletions core/sched.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2014 Freie Universität Berlin
* Copyright (C) 2014-2017 Freie Universität Berlin
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
Expand All @@ -15,6 +15,7 @@
*
* @author Kaspar Schleiser <kaspar@schleiser.de>
* @author René Kijewski <rene.kijewski@fu-berlin.de>
* @author Hauke Petersen <hauke.petersen@fu-berlin.de>
*
* @}
*/
Expand Down Expand Up @@ -79,6 +80,18 @@ static void (*sched_cb) (uint32_t timestamp, uint32_t value) = NULL;
schedstat sched_pidlist[KERNEL_PID_LAST + 1];
#endif

static inline void runqueue_push(thread_t *thread, uint8_t priority) {
clist_rpush(&sched_runqueues[priority], &(thread->rq_entry));
runqueue_bitcache |= 1 << priority;
}

static inline void runqueue_pop(thread_t *thread) {
clist_lpop(&sched_runqueues[thread->priority]);
if (!sched_runqueues[thread->priority].next) {
runqueue_bitcache &= ~(1 << thread->priority);
}
}

int __attribute__((used)) sched_run(void)
{
sched_context_switch_request = 0;
Expand Down Expand Up @@ -164,19 +177,14 @@ void sched_set_status(thread_t *process, unsigned int status)
if (!(process->status >= STATUS_ON_RUNQUEUE)) {
DEBUG("sched_set_status: adding thread %" PRIkernel_pid " to runqueue %" PRIu8 ".\n",
process->pid, process->priority);
clist_rpush(&sched_runqueues[process->priority], &(process->rq_entry));
runqueue_bitcache |= 1 << process->priority;
runqueue_push(process, process->priority);
}
}
else {
if (process->status >= STATUS_ON_RUNQUEUE) {
DEBUG("sched_set_status: removing thread %" PRIkernel_pid " to runqueue %" PRIu8 ".\n",
process->pid, process->priority);
clist_lpop(&sched_runqueues[process->priority]);

if (!sched_runqueues[process->priority].next) {
runqueue_bitcache &= ~(1 << process->priority);
}
runqueue_pop(process);
}
}

Expand Down Expand Up @@ -221,3 +229,21 @@ NORETURN void sched_task_exit(void)
sched_active_thread = NULL;
cpu_switch_context_exit();
}

#ifdef MODULE_CORE_PRIORITY_INHERITANCE
void sched_change_priority(thread_t *thread, uint8_t priority)
{
assert(thread && (priority < SCHED_PRIO_LEVELS));

if (thread->priority == priority) {
return;
}

if (thread->status >= STATUS_ON_RUNQUEUE) {
runqueue_pop(thread);
runqueue_push(thread, priority);
}

thread->priority = priority;
}
#endif

0 comments on commit 976d17f

Please sign in to comment.