thread_priority.c 2.1 KB
Newer Older
B
Bernard Xiong 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#include <rtthread.h>
#include "tc_comm.h"

struct rt_thread thread1;
struct rt_thread thread2;
static char thread1_stack[THREAD_STACK_SIZE];
static char thread2_stack[THREAD_STACK_SIZE];
static rt_uint32_t count = 0;

/*
 * the priority of thread1 > the priority of thread2
 */
static void thread1_entry(void* parameter)
{
G
Grissiom 已提交
15 16 17 18
    while (1)
    {
        count ++;
        rt_kprintf("count = %d\n", count);
B
Bernard Xiong 已提交
19

G
Grissiom 已提交
20 21
        rt_thread_delay(10);
    }
B
Bernard Xiong 已提交
22 23 24 25
}

static void thread2_entry(void* parameter)
{
G
Grissiom 已提交
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
    rt_tick_t tick;

    tick = rt_tick_get();
    while (1)
    {
        if (rt_tick_get() - tick >= 50)
        {
            if (count == 0)
                tc_done(TC_STAT_FAILED);
            else
                tc_done(TC_STAT_PASSED);

            break;
        }
    }
B
Bernard Xiong 已提交
41 42 43 44
}

int thread_priority_init()
{
G
Grissiom 已提交
45 46 47
    rt_err_t result;

    result = rt_thread_init(&thread1,
G
Grissiom 已提交
48 49 50 51 52
                            "t1",
                            thread1_entry, RT_NULL,
                            &thread1_stack[0], sizeof(thread1_stack),
                            THREAD_PRIORITY - 1, THREAD_TIMESLICE);

G
Grissiom 已提交
53 54 55 56 57 58
    if (result == RT_EOK)
        rt_thread_startup(&thread1);
    else
        tc_stat(TC_STAT_FAILED);

    rt_thread_init(&thread2,
G
Grissiom 已提交
59 60 61 62
                   "t2",
                   thread2_entry, RT_NULL,
                   &thread2_stack[0], sizeof(thread2_stack),
                   THREAD_PRIORITY + 1, THREAD_TIMESLICE);
G
Grissiom 已提交
63 64 65 66 67 68 69

    if (result == RT_EOK)
        rt_thread_startup(&thread2);
    else
        tc_stat(TC_STAT_FAILED);

    return 0;
B
Bernard Xiong 已提交
70 71 72 73 74
}

#ifdef RT_USING_TC
static void _tc_cleanup()
{
G
Grissiom 已提交
75 76
    /* lock scheduler */
    rt_enter_critical();
B
Bernard Xiong 已提交
77

G
Grissiom 已提交
78 79 80 81
    if (thread1.stat != RT_THREAD_CLOSE)
        rt_thread_detach(&thread1);
    if (thread2.stat != RT_THREAD_CLOSE)
        rt_thread_detach(&thread2);
B
Bernard Xiong 已提交
82

G
Grissiom 已提交
83 84
    /* unlock scheduler */
    rt_exit_critical();
B
Bernard Xiong 已提交
85 86 87
}
int _tc_thread_priority()
{
G
Grissiom 已提交
88
    count = 0;
B
Bernard Xiong 已提交
89

G
Grissiom 已提交
90 91 92
    /* set tc cleanup */
    tc_cleanup(_tc_cleanup);
    thread_priority_init();
B
Bernard Xiong 已提交
93

G
Grissiom 已提交
94
    return RT_TICK_PER_SECOND;
B
Bernard Xiong 已提交
95 96 97 98 99
}
FINSH_FUNCTION_EXPORT(_tc_thread_priority, a priority thread test);
#else
int rt_application_init()
{
G
Grissiom 已提交
100
    thread_priority_init();
B
Bernard Xiong 已提交
101

G
Grissiom 已提交
102
    return 0;
B
Bernard Xiong 已提交
103 104 105
}
#endif