summaryrefslogtreecommitdiff
path: root/cesar/mac/sar/src/thread.c
blob: 485eee1a09992d6322c9b9fbc00f8d579089cdb4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/* Cesar project {{{
 *
 * Copyright (C) 2011 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    mac/sar/src/thead.c
 * \brief   Handles the Thread process.
 * \ingroup mac_sar
 */
#include "common/std.h"
#include "mac/sar/inc/sar_context.h"
#include "mac/sar/inc/sar_expiration.h"
#include "mac/sar/inc/sar.h"
#include "mac/sar/inc/thread.h"

/** Time SAR thread sleeps. */
#define SAR_THREAD_DELAY_RTC 10

/** Global thread context. */
static sar_thread_t sar_thread_global;

/** Main Thread function.
 * \data  the word to the sar thread context.
 */
static void
sar_thread_process (cyg_addrword_t data)
{
    while (true)
    {
        sar_thread_t *ctx = (sar_thread_t *) data;
        dbg_assert (ctx);
        cyg_thread_delay (SAR_THREAD_DELAY_RTC);
        sar_expiration_mfs (ctx->sar);
        if (ctx->sar->pbs_missing_for_pbproc)
        {
            /* Refill the PB pool if missing block were registered. */
            arch_dsr_lock ();
            sar_pb_pool_refill (ctx->sar, 0);
            arch_dsr_unlock ();
        }
    }
}

void
sar_thread_init (sar_t *sar)
{
    sar_thread_t *ctx = &sar_thread_global;
    ctx->sar = sar;
#ifndef SAR_UNIT_TEST
    /* Create the Thread for the SAR. */
    cyg_thread_create (MAC_SAR_THREAD_PRIORITY,
                       &sar_thread_process,
                       (cyg_addrword_t) ctx,
                       "MAC_SAR",
                       ctx->thread_stack,
                       MAC_SAR_THREAD_STACK_SIZE,
                       &ctx->thread_handle, &ctx->thread);
    cyg_thread_resume (ctx->thread_handle);
#endif
}

void
sar_thread_uninit (void)
{
    sar_thread_t *ctx = &sar_thread_global;
#ifndef SAR_UNIT_TEST
    cyg_thread_suspend (ctx->thread_handle);
    cyg_thread_delete (ctx->thread_handle);
#endif
}