<commit_before>/**
 * hardware timer abstraction
 *
 * Copyright (C) 2013 Freie Universität Berlin
 *
 * This file subject to the terms and conditions of the GNU Lesser General
 * Public License. See the file LICENSE in the top level directory for more
 * details.
 *
 * @ingroup kernel
 * @{
 * @file
 * @author      <NAME> <hwill@inf.fu-berlin.de>
 * @author      <NAME> <hillebra@inf.fu-berlin.de>
 * @author      <NAME> <kaspar.schleiser@fu-berlin.de>
 * @author      <NAME> <oliver.hahm@fu-berlin.de>
 * @}
 */

#include <stdio.h>

#include "kernel.h"
#include "thread.h"
#include "lifo.h"
#include "mutex.h"

#include "hwtimer.h"
#include "hwtimer_cpu.h"
#include "hwtimer_arch.h"

/*---------------------------------------------------------------------------*/

typedef struct hwtimer_t {
    void (*callback)(void*);
    void *data;
} hwtimer_t;

static hwtimer_t timer[ARCH_MAXTIMERS];
static int lifo[ARCH_MAXTIMERS + 1];

/*---------------------------------------------------------------------------*/

static void multiplexer(int source)
{
    lifo_insert(lifo, source);
    lpm_prevent_sleep--;

    timer[source].callback(timer[source].data);
}

static void hwtimer_releasemutex(void* mutex) {
    mutex_unlock((mutex_t*) mutex);
}

void hwtimer_spin(unsigned long ticks)
{
    unsigned long x = hwtimer_arch_now();

    /**
     * If hwtimer_arch_now + ticks results in an overflow,
     * hwtimer_arch_now needs to spin until it has overflowed as well.
     *
     * If it has overflowed, the result is smaller than ticks by at
     * least one.
     */
    if (x + ticks < ticks) {
        while (hwtimer_arch_now() > x);
    }

    x += ticks;

    while (hwtimer_arch_now() < x);
}

/*---------------------------------------------------------------------------*/

void hwtimer_init(void)
{
    hwtimer_init_comp(F_CPU);
}

/*---------------------------------------------------------------------------*/

void hwtimer_init_comp(uint32_t fcpu)
{
    hwtimer_arch_init(multiplexer, fcpu);

    lifo_init(lifo, ARCH_MAXTIMERS);

    for (int i = 0; i < ARCH_MAXTIMERS; i++) {
        lifo_insert(lifo, i);<commit_msg>improve comments, rename variable<commit_after>/**
 * hardware timer abstraction
 *
 * Copyright (C) 2013 Freie Universität Berlin
 *
 * This file subject to the terms and conditions of the GNU Lesser General
 * Public License. See the file LICENSE in the top level directory for more
 * details.
 *
 * @ingroup kernel
 * @{
 * @file
 * @author      <NAME> <hwill@inf.fu-berlin.de>
 * @author      <NAME> <hillebra@inf.fu-berlin.de>
 * @author      <NAME> <kaspar.schleiser@fu-berlin.de>
 * @author      <NAME> <oliver.hahm@fu-berlin.de>
 * @}
 */

#include <stdio.h>

#include "kernel.h"
#include "thread.h"
#include "lifo.h"
#include "mutex.h"

#include "hwtimer.h"
#include "hwtimer_cpu.h"
#include "hwtimer_arch.h"

/*---------------------------------------------------------------------------*/

typedef struct hwtimer_t {
    void (*callback)(void*);
    void *data;
} hwtimer_t;

static hwtimer_t timer[ARCH_MAXTIMERS];
static int lifo[ARCH_MAXTIMERS + 1];

/*---------------------------------------------------------------------------*/

static void multiplexer(int source)
{
    lifo_insert(lifo, source);
    lpm_prevent_sleep--;

    timer[source].callback(timer[source].data);
}

static void hwtimer_releasemutex(void* mutex) {
    mutex_unlock((mutex_t*) mutex);
}

void hwtimer_spin(unsigned long ticks)
{
    unsigned long t = hwtimer_arch_now();

    /**
     * If hwtimer_arch_now + ticks results in an overflow,
     * hwtimer_arch_now needs to spin until it has overflowed as well.
     *
     * If the destination time will result in an overflow, the result
     * is smaller than ticks by at least one.
     */
    if (t + ticks < ticks) {
        while (hwtimer_arch_now() > t);
    }

    /**
     * set t to destination time, possibly overflowing it
     */
    t += ticks;

    /**
     * wait until the present has past destination time t
     */
    while (hwtimer_arch_now() < t);
}

/*---------------------------------------------------------------------------*/

void hwtimer_init(void)
{
    hwtimer_init_comp(F_CPU);
}

/*---------------------------------------------------------------------------*/

void hwtimer_init_comp(uint32_t fcpu)
{
    hwtimer_arch_init(multiplexer, fcpu);

    lifo_init(lifo, ARCH_MAXTIMERS);

    for (int i = 0; i < ARCH_MAXTIMERS; i++) {
        lifo_insert(lifo, i);
{'fraction_non_alphanumeric': 0.24652622142536978, 'fraction_numerical': 0.003585835948005379, 'mean_word_length': 1.9400527009222661, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 8, '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': '2587280', 'n_tokens_mistral': 1447, 'n_tokens_neox': 1409, 'n_words': 358}