2021-03-08 07:18:51 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2006-2021, RT-Thread Development Team
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
|
|
|
* Change Logs:
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2013-01-08 21:05:02 +08:00
|
|
|
#include <rtthread.h>
|
|
|
|
#include "tc_comm.h"
|
|
|
|
|
|
|
|
static rt_sem_t sem;
|
|
|
|
static rt_uint8_t t1_count, t2_count;
|
|
|
|
static rt_thread_t t1, t2, worker;
|
|
|
|
static void thread1_entry(void* parameter)
|
|
|
|
{
|
2013-12-21 12:51:52 +08:00
|
|
|
rt_err_t result;
|
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
result = rt_sem_take(sem, RT_WAITING_FOREVER);
|
|
|
|
if (result != RT_EOK)
|
|
|
|
{
|
|
|
|
tc_done(TC_STAT_FAILED);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
t1_count ++;
|
|
|
|
rt_kprintf("thread1: got semaphore, count: %d\n", t1_count);
|
|
|
|
}
|
2013-01-08 21:05:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void thread2_entry(void* parameter)
|
|
|
|
{
|
2013-12-21 12:51:52 +08:00
|
|
|
rt_err_t result;
|
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
result = rt_sem_take(sem, RT_WAITING_FOREVER);
|
|
|
|
if (result != RT_EOK)
|
|
|
|
{
|
|
|
|
tc_done(TC_STAT_FAILED);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
t2_count ++;
|
|
|
|
rt_kprintf("thread2: got semaphore, count: %d\n", t2_count);
|
|
|
|
}
|
2013-01-08 21:05:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void worker_thread_entry(void* parameter)
|
|
|
|
{
|
2013-12-21 12:51:52 +08:00
|
|
|
rt_thread_delay(10);
|
2013-01-08 21:05:02 +08:00
|
|
|
|
2013-12-21 12:51:52 +08:00
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
rt_sem_release(sem);
|
|
|
|
rt_thread_delay(5);
|
|
|
|
}
|
2013-01-08 21:05:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int semaphore_priority_init()
|
|
|
|
{
|
2013-12-21 12:51:52 +08:00
|
|
|
sem = rt_sem_create("sem", 0, RT_IPC_FLAG_PRIO);
|
|
|
|
if (sem == RT_NULL)
|
|
|
|
{
|
|
|
|
tc_stat(TC_STAT_END | TC_STAT_FAILED);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
t1_count = t2_count = 0;
|
|
|
|
|
|
|
|
t1 = rt_thread_create("t1",
|
|
|
|
thread1_entry, RT_NULL,
|
|
|
|
THREAD_STACK_SIZE, THREAD_PRIORITY + 1, THREAD_TIMESLICE);
|
|
|
|
if (t1 != RT_NULL)
|
|
|
|
rt_thread_startup(t1);
|
|
|
|
else
|
|
|
|
tc_stat(TC_STAT_END | TC_STAT_FAILED);
|
|
|
|
|
|
|
|
t2 = rt_thread_create("t2",
|
|
|
|
thread2_entry, RT_NULL,
|
|
|
|
THREAD_STACK_SIZE, THREAD_PRIORITY - 1, THREAD_TIMESLICE);
|
|
|
|
if (t2 != RT_NULL)
|
|
|
|
rt_thread_startup(t2);
|
|
|
|
else
|
|
|
|
tc_stat(TC_STAT_END | TC_STAT_FAILED);
|
|
|
|
|
|
|
|
worker = rt_thread_create("worker",
|
|
|
|
worker_thread_entry, RT_NULL,
|
|
|
|
THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE);
|
|
|
|
if (worker != RT_NULL)
|
|
|
|
rt_thread_startup(worker);
|
|
|
|
else
|
|
|
|
tc_stat(TC_STAT_END | TC_STAT_FAILED);
|
|
|
|
|
|
|
|
return 0;
|
2013-01-08 21:05:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef RT_USING_TC
|
|
|
|
static void _tc_cleanup()
|
|
|
|
{
|
2013-12-21 12:51:52 +08:00
|
|
|
/* lock scheduler */
|
|
|
|
rt_enter_critical();
|
|
|
|
|
|
|
|
/* delete t1, t2 and worker thread */
|
|
|
|
rt_thread_delete(t1);
|
|
|
|
rt_thread_delete(t2);
|
|
|
|
rt_thread_delete(worker);
|
|
|
|
|
|
|
|
if (sem)
|
|
|
|
{
|
|
|
|
rt_sem_delete(sem);
|
|
|
|
sem = RT_NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (t1_count > t2_count)
|
|
|
|
tc_done(TC_STAT_FAILED);
|
|
|
|
else
|
|
|
|
tc_done(TC_STAT_PASSED);
|
|
|
|
|
|
|
|
/* unlock scheduler */
|
|
|
|
rt_exit_critical();
|
2013-01-08 21:05:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int _tc_semaphore_priority()
|
|
|
|
{
|
2013-12-21 12:51:52 +08:00
|
|
|
/* set tc cleanup */
|
|
|
|
tc_cleanup(_tc_cleanup);
|
|
|
|
semaphore_priority_init();
|
2013-01-08 21:05:02 +08:00
|
|
|
|
2013-12-21 12:51:52 +08:00
|
|
|
return 50;
|
2013-01-08 21:05:02 +08:00
|
|
|
}
|
|
|
|
FINSH_FUNCTION_EXPORT(_tc_semaphore_priority, a priority semaphore test);
|
|
|
|
#else
|
|
|
|
int rt_application_init()
|
|
|
|
{
|
2013-12-21 12:51:52 +08:00
|
|
|
semaphore_priority_init();
|
2013-01-08 21:05:02 +08:00
|
|
|
|
2013-12-21 12:51:52 +08:00
|
|
|
return 0;
|
2013-01-08 21:05:02 +08:00
|
|
|
}
|
|
|
|
#endif
|