<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 "Os.h"
#include "Os_Arch_Fibers.h"

#include <windows.h>

typedef struct Os_Arch_StateType {
    LPVOID fiber_old;
    LPVOID fiber;
} Os_Arch_StateType;

LPVOID Os_Arch_System;

Os_Arch_StateType Os_Arch_State[OS_TASK_COUNT];

VOID CALLBACK Os_Arch_FiberStart(LPVOID lpParameter)
{
    Os_TaskType task = (Os_TaskType)(intptr_t)lpParameter;
    if (Os_Arch_State[task].fiber_old) {
        DeleteFiber(Os_Arch_State[task].fiber_old);
        Os_Arch_State[task].fiber_old = NULL;
    }
    Os_TaskConfigs[task].entry();
}

void Os_Arch_Init(void)
{
    memset(&Os_Arch_State, 0, sizeof(Os_Arch_State));
    Os_Arch_System = ConvertThreadToFiber(NULL);
}

void Os_Arch_DisableAllInterrupts(void)
{

}

void Os_Arch_EnableAllInterrupts(void)
{

}

void Os_Arch_SwapState(Os_TaskType prev, Os_TaskType task)
{
    SwitchToFiber(Os_Arch_State[task].fiber);
}

static __inline void Os_Arch_DeleteFiber(PVOID* fiber)
{
    if(*fiber && (*fiber != GetCurrentFiber())) {
        DeleteFiber(*fiber);
        *fiber = NULL;
    }
}

void Os_Arch_PrepareState(Os_TaskType task)
{
    /* clean any prepared fiber */
    Os_Arch_DeleteFiber(&Os_Arch_State[task].fiber_old);<commit_msg>Use a timer to simulate ticks<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 "Os.h"
#include "Os_Arch_Fibers.h"

#include <Windows.h>
#include <WinBase.h>

typedef struct Os_Arch_StateType {
    LPVOID fiber_old;
    LPVOID fiber;
} Os_Arch_StateType;

LPVOID Os_Arch_System;

HANDLE Os_Arch_Timer;
CRITICAL_SECTION Os_Arch_Section;

Os_Arch_StateType Os_Arch_State[OS_TASK_COUNT];

VOID CALLBACK Os_Arch_FiberStart(LPVOID lpParameter)
{
    Os_TaskType task = (Os_TaskType)(intptr_t)lpParameter;
    if (Os_Arch_State[task].fiber_old) {
        DeleteFiber(Os_Arch_State[task].fiber_old);
        Os_Arch_State[task].fiber_old = NULL;
    }
    Os_TaskConfigs[task].entry();
}

VOID CALLBACK Os_Arch_TimerCallback(PVOID lpParameter, BOOLEAN TimerOrWaitFired)
{
    Os_Arch_DisableAllInterrupts();
    Os_Isr();
    Os_Arch_EnableAllInterrupts();
}

void Os_Arch_Init(void)
{
    memset(&Os_Arch_State, 0, sizeof(Os_Arch_State));
    Os_Arch_System = ConvertThreadToFiber(NULL);

    InitializeCriticalSection(&Os_Arch_Section);
    Os_Arch_DisableAllInterrupts();

    CreateTimerQueueTimer( &Os_Arch_Timer
                         , NULL
                         , Os_Arch_TimerCallback
                         , NULL
                         , 100
                         , 100
                         , WT_EXECUTEDEFAULT);

}

void Os_Arch_DisableAllInterrupts(void)
{
    EnterCriticalSection(&Os_Arch_Section);
}

void Os_Arch_EnableAllInterrupts(void)
{
    LeaveCriticalSection(&Os_Arch_Section);
}

void Os_Arch_SwapState(Os_TaskType task, Os_TaskType prev)
{
    SwitchToFiber(Os_Arch_State[task].fiber);
}

static __inline void Os_Arch_DeleteFiber(PVOID* fiber)
{
    if(*fiber && (*fiber != GetCurrentFiber())) {
        DeleteFiber(*fiber);
        *fiber = NULL;
    }
}

void Os_Arch_PrepareState(Os_TaskType task)
{
    /* clean any prepared fiber */
    Os_Arch_DeleteFiber(&Os_Arch_State[task].fiber_old);
{'fraction_non_alphanumeric': 0.08335091791517198, 'fraction_numerical': 0.008862629246676515, 'mean_word_length': 3.891640866873065, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, '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': '3987770', 'n_tokens_mistral': 1585, 'n_tokens_neox': 1520, 'n_words': 419}