<commit_before>/*
 * Copyright (C) 2014 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
 * directory for more details.
 */

/**
 * @ingroup     core_sched
 * @{
 *
 * @file        sched.c
 * @brief       Scheduler implementation
 *
 * @author      <NAME> <<EMAIL>>
 *
 * @}
 */

#include <stdint.h>

#include "sched.h"
#include "kernel.h"
#include "kernel_internal.h"
#include "clist.h"
#include "bitarithm.h"
#include "irq.h"
#include "thread.h"
#include "irq.h"

#if SCHEDSTATISTICS
#include "hwtimer.h"
#endif

#define ENABLE_DEBUG (0)
#include "debug.h"

volatile int sched_num_threads = 0;

volatile unsigned int sched_context_switch_request;

volatile tcb_t *sched_threads[KERNEL_PID_LAST + 1];
volatile tcb_t *sched_active_thread;

volatile kernel_pid_t sched_active_pid = KERNEL_PID_UNDEF;

static clist_node_t *sched_runqueues[SCHED_PRIO_LEVELS];
static uint32_t runqueue_bitcache = 0;

#if SCHEDSTATISTICS
static void (*sched_cb) (uint32_t timestamp, uint32_t value) = NULL;
schedstat sched_pidlist[KERNEL_PID_LAST + 1];
#endif

void sched_run(void)
{
    sched_context_switch_request = 0;

#ifdef SCHEDSTATISTICS
    unsigned long time = hwtimer_now();
#endif

    tcb_t *my_active_thread = (tcb_t *)sched_active_thread;

    if (my_active_thread) {
        if (my_active_thread->status == STATUS_RUNNING) {
            my_active_thread->status = STATUS_PENDING;
        }

#ifdef SCHED_TEST_STACK
        if (*((unsigned int *)my_active_thread->stack_start) != (unsigned int) my_active_thread->stack_start) {
            printf("scheduler(): stack overflow detected, task=%s pid=%" PRIkernel_pid "\n", my_active_thread->name, my_active_thread->pid);
        }
#endif

#ifdef SCHEDSTATISTICS
        if (sched_pidlist[my_active_thread->pid].laststart) {
            sched_pidlist[my_active_thread->pid].runtime_ticks += time - sched_pidlist[my_active_thread->pid].laststart;
        }
#endif
    }

    DEBUG("\nscheduler: previous task: %s\n", (my_active_thread == NULL) ? "none" : my_active_thread->name);

    /* The bitmask in runqueue_bitcache is never empty,
     * since the threading should not be started before at least the idle thread was started.
     */
    int nextrq = bitarithm_lsb(runqueue_bitcache);
    my_active_thread = clist_get_container(sched_runqueues[nextrq], tcb_t, rq_entry);
    clist_advance(&(sched_runqueues[nextrq]));
    DEBUG("scheduler: first in queue: %s\n", my_active_thread->name);
    sched_active_pid = (volatile int) my_active_thread->pid;

    kernel_pid_t my_next_pid = my_active_thread->pid;
<commit_msg>cppcheck: don't needlessly assign sched_active_pid<commit_after>/*
 * Copyright (C) 2014 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
 * directory for more details.
 */

/**
 * @ingroup     core_sched
 * @{
 *
 * @file        sched.c
 * @brief       Scheduler implementation
 *
 * @author      <NAME> <<EMAIL>>
 *
 * @}
 */

#include <stdint.h>

#include "sched.h"
#include "kernel.h"
#include "kernel_internal.h"
#include "clist.h"
#include "bitarithm.h"
#include "irq.h"
#include "thread.h"
#include "irq.h"

#if SCHEDSTATISTICS
#include "hwtimer.h"
#endif

#define ENABLE_DEBUG (0)
#include "debug.h"

volatile int sched_num_threads = 0;

volatile unsigned int sched_context_switch_request;

volatile tcb_t *sched_threads[KERNEL_PID_LAST + 1];
volatile tcb_t *sched_active_thread;

volatile kernel_pid_t sched_active_pid = KERNEL_PID_UNDEF;

static clist_node_t *sched_runqueues[SCHED_PRIO_LEVELS];
static uint32_t runqueue_bitcache = 0;

#if SCHEDSTATISTICS
static void (*sched_cb) (uint32_t timestamp, uint32_t value) = NULL;
schedstat sched_pidlist[KERNEL_PID_LAST + 1];
#endif

void sched_run(void)
{
    sched_context_switch_request = 0;

#ifdef SCHEDSTATISTICS
    unsigned long time = hwtimer_now();
#endif

    tcb_t *my_active_thread = (tcb_t *)sched_active_thread;

    if (my_active_thread) {
        if (my_active_thread->status == STATUS_RUNNING) {
            my_active_thread->status = STATUS_PENDING;
        }

#ifdef SCHED_TEST_STACK
        if (*((unsigned int *)my_active_thread->stack_start) != (unsigned int) my_active_thread->stack_start) {
            printf("scheduler(): stack overflow detected, task=%s pid=%" PRIkernel_pid "\n", my_active_thread->name, my_active_thread->pid);
        }
#endif

#ifdef SCHEDSTATISTICS
        if (sched_pidlist[my_active_thread->pid].laststart) {
            sched_pidlist[my_active_thread->pid].runtime_ticks += time - sched_pidlist[my_active_thread->pid].laststart;
        }
#endif
    }

    DEBUG("\nscheduler: previous task: %s\n", (my_active_thread == NULL) ? "none" : my_active_thread->name);

    /* The bitmask in runqueue_bitcache is never empty,
     * since the threading should not be started before at least the idle thread was started.
     */
    int nextrq = bitarithm_lsb(runqueue_bitcache);
    my_active_thread = clist_get_container(sched_runqueues[nextrq], tcb_t, rq_entry);
    clist_advance(&(sched_runqueues[nextrq]));
    DEBUG("scheduler: first in queue: %s\n", my_active_thread->name);

    kernel_pid_t my_next_pid = my_active_thread->pid;

{'fraction_non_alphanumeric': 0.11056971514242879, 'fraction_numerical': 0.006746626686656672, 'mean_word_length': 4.217008797653959, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 36, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5032095', 'n_tokens_mistral': 1881, 'n_tokens_neox': 1832, 'n_words': 430}