<commit_before>/* OSEKOS Implementation of an OSEK Scheduler
 * Copyright (C) 2015 <NAME>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.

 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.

 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <string.h>

#include "Std_Types.h"
#include "Os_Types.h"
#include "Os.h"

Os_TaskControlType              Os_TaskControls        [OS_TASK_COUNT];
Os_ReadyListType                Os_TaskReady           [OS_PRIO_COUNT];
Os_TaskType                     Os_TaskRunning;
Os_ContextType                  Os_CallContext;
const Os_TaskConfigType *       Os_TaskConfigs;

void Os_ReadyListPushHead(Os_ReadyListType* list, Os_TaskType task)
{
    Os_TaskControls[task].next = list->head;
    list->head = task;
    if (list->tail == Os_TaskIdNone) {
        list->tail = task;
    }
}

void Os_ReadyListPopHead(Os_ReadyListType* list, Os_TaskType* task)
{
    if (list->tail == Os_TaskIdNone) {
        *task = Os_TaskIdNone;
    } else {
        *task = list->head;
        if (list->tail == *task) {
            list->tail = Os_TaskIdNone;
            list->head = Os_TaskIdNone;
        }
        list->head = Os_TaskControls[*task].next;
    }
    Os_TaskControls[*task].next = Os_TaskIdNone;
}

void Os_ReadyListPushTail(Os_ReadyListType* list, Os_TaskType task)
{
    Os_TaskControls[task].next = Os_TaskIdNone;
    if (list->head == Os_TaskIdNone) {
        list->head = task;
    } else {
        Os_TaskControls[list->tail].next = task;
    }
    list->tail = task;
}

void Os_ReadyListInit(Os_ReadyListType* list)
{
    list->head = Os_TaskIdNone;
    list->tail = Os_TaskIdNone;
}

void Os_TaskInit(Os_TaskType task)
{
    memset(&Os_TaskControls[task], 0, sizeof(Os_TaskControls[task]));
}

static __inline void Os_TaskSetReadyHead(Os_TaskType task)
{
    Os_PriorityType prio = Os_TaskConfigs[task].priority;
    Os_ReadyListPushHead(&Os_TaskReady[prio], task);
}

void Os_TaskPeek(Os_PriorityType min_priority, Os_TaskType* task)
{
    Os_PriorityType prio;
    *task = Os_TaskIdNone;
    for(prio = OS_PRIO_COUNT; prio > min_priority && *task == Os_TaskIdNone; --prio) {
        *task = Os_TaskReady[prio-1].head;
    }
}

static __inline void Os_State_Running_To_Suspended(Os_TaskType task)
{
    OS_ERRORCHECK(Os_TaskControls[task].state == OS_TASK_RUNNING, E_OS_STATE);
    Os_TaskControls[task].state = OS_TASK_SUSPENDED;
}

static __inline void Os_State_Running_To_Ready(Os_TaskType task)
{
    OS_ERRORCHECK(Os_TaskControls[task].state == OS_TASK_RUNNING, E_OS_STATE);
    Os_PriorityType prio = Os_TaskConfigs[task].priority;
    Os_ReadyListPushHead(&Os_TaskReady[prio], task);
    Os_TaskControls[task].state = OS_TASK_READY;
}

static __inline void Os_State_Suspended_To_Ready(Os_TaskType task)
{
    OS_ERRORCHECK(Os_TaskControls[task].state == OS_TASK_SUSPENDED, E_OS_STATE);
    Os_Arch_PrepareState(task);
    Os_PriorityType prio = Os_TaskConfigs[task].priority;
    Os_ReadyListPushTail(&Os_TaskReady[prio], task);
    Os_TaskControls[task].state = OS_TASK_READY;
}

static __inline void Os_State_Ready_To_Running(Os_TaskType task)
{
    OS_ERRORCHECK(Os_TaskControls[task].state == OS_TASK_READY   , E_OS_STATE);
    Os_PriorityType prio = Os_TaskConfigs[task].priority;
    Os_TaskType     task2;
    Os_ReadyListPopHead(&Os_TaskReady[prio], &task2);
    OS_ERRORCHECK(task2 == task, E_OS_STATE);
    Os_TaskControls[task].state = OS_TASK_RUNNING;
}

void Os_Start(void)
{
    Os_Arch_EnableAllInterrupts();
    while(1) {
        (void)Os_Schedule();
    }
}

StatusType Os_ScheduleInternal(void)
{
    Os_PriorityType prio;
    Os_TaskType     task;
    if (Os_TaskRunning == Os_TaskIdNone) {
        prio = 0u;
    } else {
        prio = Os_TaskConfigs[Os_TaskRunning].priority + 1;
    }

    while(1) {
        Os_TaskPeek(prio, &task);

        if (task == Os_TaskIdNone) {
            if (Os_TaskRunning != Os_TaskIdNone) {
                break;    /* continue with current */
            }
            if (Os_CallContext != OS_CONTEXT_TASK) {
                break;    /* no new task, and we are inside tick, so we need to return out */
            }

            /* idling in last task */
            continue;
        }

        if (Os_TaskRunning != Os_TaskIdNone) {
            /* put preempted task as first ready */
            Os_State_Running_To_Ready(Os_TaskRunning);
        }

        Os_TaskType prev = Os_TaskRunning;
        Os_TaskRunning = task;
        Os_State_Ready_To_Running(task);

        /* no direct return if called from task
         * if called from ISR, this will just prepare next task */
        Os_Arch_SwapState(task, prev);
        break;
    }
    return E_OK;
}

StatusType Os_Schedule(void)
{
    StatusType res;
    Os_Arch_DisableAllInterrupts();
    res = Os_ScheduleInternal();
    Os_Arch_EnableAllInterrupts();
    return res;
}

void Os_Isr(void)
{
    Os_CallContext = OS_CONTEXT_ISR;
    Os_ScheduleInternal();
    Os_CallContext = OS_CONTEXT_TASK;
}


StatusType Os_TerminateTask(void)
{
    StatusType res;
    Os_Arch_DisableAllInterrupts();

    OS_ERRORCHECK(Os_TaskControls[Os_TaskRunning].activation > 0, E_OS_LIMIT);
    OS_ERRORCHECK(Os_CallContext != OS_CONTEXT_ISR              , E_OS_CALLEVEL);

    Os_State_Running_To_Suspended(Os_TaskRunning);

    Os_TaskControls[Os_TaskRunning].activation--;
    if (Os_TaskControls[Os_TaskRunning].activation) {
        Os_State_Suspended_To_Ready(Os_TaskRunning);
    }

    Os_TaskRunning = Os_TaskIdNone;
    res = Os_ScheduleInternal();
    Os_Arch_EnableAllInterrupts();
    return res;
}

StatusType Os_ActivateTask(Os_TaskType task)
{
    StatusType res;
    Os_Arch_DisableAllInterrupts();

    OS_ERRORCHECK(task < OS_TASK_COUNT                   , E_OS_ID);
    OS_ERRORCHECK(Os_TaskControls[task].activation <  255, E_OS_LIMIT);

    Os_TaskControls[task].activation++;
    if (Os_TaskControls[task].activation == 1u) {
        Os_State_Suspended_To_Ready(task);
    }
    res = Os_ScheduleInternal();
    Os_Arch_EnableAllInterrupts();
    return res;
}

void Os_Init(const Os_ConfigType* config)
{
    Os_TaskType task;
    uint_least8_t prio;

    Os_TaskConfigs = *config->tasks;
    Os_CallContext = OS_CONTEXT_TASK;

    memset(&Os_TaskControls, 0u, sizeof(Os_TaskControls));

    for (prio = 0u; prio < OS_PRIO_COUNT; ++prio) {
        Os_ReadyListInit(&Os_TaskReady[prio]);
    }

    Os_Arch_Init();

    /* setup autostarting tasks */
    for (task = 0u; task < OS_TASK_COUNT; ++task) {
        Os_TaskInit(task);

        if (Os_TaskConfigs[task].autostart) {
            Os_TaskControls[task].activation = 1;
            Os_State_Suspended_To_Ready(task);
        }
    }

    /* there is no task running */
    Os_TaskRunning = Os_TaskIdNone;
}


<commit_msg>Drop unused function and some cosmetics<commit_after>/* OSEKOS Implementation of an OSEK Scheduler
 * Copyright (C) 2015 <NAME>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.

 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.

 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <string.h>

#include "Std_Types.h"
#include "Os_Types.h"
#include "Os.h"

Os_TaskControlType              Os_TaskControls        [OS_TASK_COUNT];
Os_ReadyListType                Os_TaskReady           [OS_PRIO_COUNT];
Os_TaskType                     Os_TaskRunning;
Os_ContextType                  Os_CallContext;
const Os_TaskConfigType *       Os_TaskConfigs;

void Os_ReadyListPushHead(Os_ReadyListType* list, Os_TaskType task)
{
    Os_TaskControls[task].next = list->head;
    list->head = task;
    if (list->tail == Os_TaskIdNone) {
        list->tail = task;
    }
}

void Os_ReadyListPopHead(Os_ReadyListType* list, Os_TaskType* task)
{
    if (list->tail == Os_TaskIdNone) {
        *task = Os_TaskIdNone;
    } else {
        *task = list->head;
        if (list->tail == *task) {
            list->tail = Os_TaskIdNone;
            list->head = Os_TaskIdNone;
        }
        list->head = Os_TaskControls[*task].next;
    }
    Os_TaskControls[*task].next = Os_TaskIdNone;
}

void Os_ReadyListPushTail(Os_ReadyListType* list, Os_TaskType task)
{
    Os_TaskControls[task].next = Os_TaskIdNone;
    if (list->head == Os_TaskIdNone) {
        list->head = task;
    } else {
        Os_TaskControls[list->tail].next = task;
    }
    list->tail = task;
}

void Os_ReadyListInit(Os_ReadyListType* list)
{
    list->head = Os_TaskIdNone;
    list->tail = Os_TaskIdNone;
}

void Os_TaskInit(Os_TaskType task)
{
    memset(&Os_TaskControls[task], 0, sizeof(Os_TaskControls[task]));
}

void Os_TaskPeek(Os_PriorityType min_priority, Os_TaskType* task)
{
    Os_PriorityType prio;
    *task = Os_TaskIdNone;
    for(prio = OS_PRIO_COUNT; prio > min_priority && *task == Os_TaskIdNone; --prio) {
        *task = Os_TaskReady[prio-1].head;
    }
}

static __inline void Os_State_Running_To_Suspended(Os_TaskType task)
{
    OS_ERRORCHECK(Os_TaskControls[task].state == OS_TASK_RUNNING, E_OS_STATE);

    Os_TaskControls[task].state = OS_TASK_SUSPENDED;
}

static __inline void Os_State_Running_To_Ready(Os_TaskType task)
{
    OS_ERRORCHECK(Os_TaskControls[task].state == OS_TASK_RUNNING, E_OS_STATE);

    Os_PriorityType prio = Os_TaskConfigs[task].priority;
    Os_ReadyListPushHead(&Os_TaskReady[prio], task);
    Os_TaskControls[task].state = OS_TASK_READY;
}

static __inline void Os_State_Suspended_To_Ready(Os_TaskType task)
{
    OS_ERRORCHECK(Os_TaskControls[task].state == OS_TASK_SUSPENDED, E_OS_STATE);

    Os_Arch_PrepareState(task);
    Os_PriorityType prio = Os_TaskConfigs[task].priority;
    Os_ReadyListPushTail(&Os_TaskReady[prio], task);
    Os_TaskControls[task].state = OS_TASK_READY;
}

static __inline void Os_State_Ready_To_Running(Os_TaskType task)
{
    OS_ERRORCHECK(Os_TaskControls[task].state == OS_TASK_READY   , E_OS_STATE);

    Os_PriorityType prio = Os_TaskConfigs[task].priority;
    Os_TaskType     task2;
    Os_ReadyListPopHead(&Os_TaskReady[prio], &task2);

    OS_ERRORCHECK(task2 == task, E_OS_STATE);

    Os_TaskControls[task].state = OS_TASK_RUNNING;
}

void Os_Start(void)
{
    Os_Arch_EnableAllInterrupts();
    while(1) {
        (void)Os_Schedule();
    }
}

StatusType Os_ScheduleInternal(void)
{
    Os_PriorityType prio;
    Os_TaskType     task;
    if (Os_TaskRunning == Os_TaskIdNone) {
        prio = 0u;
    } else {
        prio = Os_TaskConfigs[Os_TaskRunning].priority + 1;
    }

    while(1) {
        Os_TaskPeek(prio, &task);

        if (task == Os_TaskIdNone) {
            if (Os_TaskRunning != Os_TaskIdNone) {
                break;    /* continue with current */
            }
            if (Os_CallContext != OS_CONTEXT_TASK) {
                break;    /* no new task, and we are inside tick, so we need to return out */
            }

            /* idling in last task */
            continue;
        }

        if (Os_TaskRunning != Os_TaskIdNone) {
            /* put preempted task as first ready */
            Os_State_Running_To_Ready(Os_TaskRunning);
        }

        Os_TaskType prev = Os_TaskRunning;
        Os_TaskRunning = task;
        Os_State_Ready_To_Running(task);

        /* no direct return if called from task
         * if called from ISR, this will just prepare next task */
        Os_Arch_SwapState(task, prev);
        break;
    }
    return E_OK;
}

StatusType Os_Schedule(void)
{
    StatusType res;
    Os_Arch_DisableAllInterrupts();
    res = Os_ScheduleInternal();
    Os_Arch_EnableAllInterrupts();
    return res;
}

void Os_Isr(void)
{
    Os_CallContext = OS_CONTEXT_ISR;
    Os_ScheduleInternal();
    Os_CallContext = OS_CONTEXT_TASK;
}


StatusType Os_TerminateTask(void)
{
    StatusType res;
    Os_Arch_DisableAllInterrupts();

    OS_ERRORCHECK(Os_TaskControls[Os_TaskRunning].activation > 0, E_OS_LIMIT);
    OS_ERRORCHECK(Os_CallContext != OS_CONTEXT_ISR              , E_OS_CALLEVEL);

    Os_State_Running_To_Suspended(Os_TaskRunning);

    Os_TaskControls[Os_TaskRunning].activation--;
    if (Os_TaskControls[Os_TaskRunning].activation) {
        Os_State_Suspended_To_Ready(Os_TaskRunning);
    }

    Os_TaskRunning = Os_TaskIdNone;
    res = Os_ScheduleInternal();
    Os_Arch_EnableAllInterrupts();
    return res;
}

StatusType Os_ActivateTask(Os_TaskType task)
{
    StatusType res;
    Os_Arch_DisableAllInterrupts();

    OS_ERRORCHECK(task < OS_TASK_COUNT                   , E_OS_ID);
    OS_ERRORCHECK(Os_TaskControls[task].activation <  255, E_OS_LIMIT);

    Os_TaskControls[task].activation++;
    if (Os_TaskControls[task].activation == 1u) {
        Os_State_Suspended_To_Ready(task);
    }
    res = Os_ScheduleInternal();
    Os_Arch_EnableAllInterrupts();
    return res;
}

void Os_Init(const Os_ConfigType* config)
{
    Os_TaskType task;
    uint_least8_t prio;

    Os_TaskConfigs = *config->tasks;
    Os_CallContext = OS_CONTEXT_TASK;

    memset(&Os_TaskControls, 0u, sizeof(Os_TaskControls));

    for (prio = 0u; prio < OS_PRIO_COUNT; ++prio) {
        Os_ReadyListInit(&Os_TaskReady[prio]);
    }

    Os_Arch_Init();

    /* setup autostarting tasks */
    for (task = 0u; task < OS_TASK_COUNT; ++task) {
        Os_TaskInit(task);

        if (Os_TaskConfigs[task].autostart) {
            Os_TaskControls[task].activation = 1;
            Os_State_Suspended_To_Ready(task);
        }
    }

    /* there is no task running */
    Os_TaskRunning = Os_TaskIdNone;
}



{'fraction_non_alphanumeric': 0.09579359088725725, 'fraction_numerical': 0.004940643656076306, 'mean_word_length': 3.189134808853119, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 45, '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': '3757748', 'n_tokens_mistral': 5005, 'n_tokens_neox': 4870, 'n_words': 1007}