summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorguozhanxin <[email protected]>2022-08-09 15:03:55 +0800
committerMan, Jianting (Meco) <[email protected]>2022-08-16 00:07:33 -0400
commitf6ac38a19b84f97f91db8ef020887f807281b5c0 (patch)
tree7a0ef77ccf596148492a7200ea42b7f834224f0d /examples
parent7750358bed31c6eab5d25a89c7b2f256c87f932d (diff)
[utestcases] add timeslice_test
Diffstat (limited to 'examples')
-rw-r--r--examples/utest/testcases/kernel/thread_tc.c100
1 files changed, 100 insertions, 0 deletions
diff --git a/examples/utest/testcases/kernel/thread_tc.c b/examples/utest/testcases/kernel/thread_tc.c
index e95c97c293..6c1a17cd04 100644
--- a/examples/utest/testcases/kernel/thread_tc.c
+++ b/examples/utest/testcases/kernel/thread_tc.c
@@ -455,6 +455,104 @@ static void test_delay_until(void)
uassert_int_equal(delta, 10);
}
+static rt_thread_t tidA, tidB1, tidB2;
+static uint32_t timeslice_cntA, timeslice_cntB1, timeslice_cntB2;
+
+static void test_timeslice_threadA_entry(void *parameter)
+{
+ while (1)
+ {
+ rt_thread_delay(2);
+ timeslice_cntA++;
+ if (timeslice_cntA > 10) return;
+ }
+}
+static void test_timeslice_threadB1_entry(void *parameter)
+{
+ while (1)
+ {
+ timeslice_cntB1++;
+ if (timeslice_cntA > 10) return;
+ }
+}
+static void test_timeslice_threadB2_entry(void *parameter)
+{
+ while (1)
+ {
+ timeslice_cntB2++;
+ if (timeslice_cntA > 10) return;
+ }
+}
+
+void test_timeslice(void)
+{
+ rt_err_t ret_startup = -RT_ERROR;
+ uint32_t diff;
+
+ timeslice_cntA = 0;
+ timeslice_cntB1 = 0;
+ timeslice_cntB2 = 0;
+
+ tidA = rt_thread_create("timeslice", test_timeslice_threadA_entry, RT_NULL,
+ 2048, __current_thread->current_priority + 1, 10);
+ if (!tidA)
+ {
+ LOG_E("rt_thread_create failed!");
+ return;
+ }
+
+ rt_thread_control(tidA, RT_THREAD_CTRL_BIND_CPU, (void *)1);
+ ret_startup = rt_thread_startup(tidA);
+ if (ret_startup != RT_EOK)
+ {
+ LOG_E("rt_thread_startup failed!");
+ uassert_false(1);
+ return ;
+ }
+
+ tidB1 = rt_thread_create("timeslice", test_timeslice_threadB1_entry, RT_NULL,
+ 2048, __current_thread->current_priority + 2, 2);
+ if (!tidB1)
+ {
+ LOG_E("rt_thread_create failed!");
+ return;
+ }
+
+ rt_thread_control(tidB1, RT_THREAD_CTRL_BIND_CPU, (void *)1);
+ ret_startup = rt_thread_startup(tidB1);
+ if (ret_startup != RT_EOK)
+ {
+ LOG_E("rt_thread_startup failed!");
+ uassert_false(1);
+ return ;
+ }
+
+ tidB2 = rt_thread_create("timeslice", test_timeslice_threadB2_entry, RT_NULL,
+ 2048, __current_thread->current_priority + 2, 2);
+ if (!tidB2)
+ {
+ LOG_E("rt_thread_create failed!");
+ return;
+ }
+
+ rt_thread_control(tidB2, RT_THREAD_CTRL_BIND_CPU, (void *)1);
+ ret_startup = rt_thread_startup(tidB2);
+ if (ret_startup != RT_EOK)
+ {
+ LOG_E("rt_thread_startup failed!");
+ uassert_false(1);
+ return ;
+ }
+ do{
+ rt_thread_delay(2 * 20);
+ }while(timeslice_cntA <= 10);
+
+ rt_kprintf("A:%d,B1:%d,B2:%d\n", timeslice_cntA, timeslice_cntB1, timeslice_cntB2);
+ diff = abs(timeslice_cntB1 - timeslice_cntB2);
+ uassert_true(diff * 100 / timeslice_cntB1 < 30);
+ uassert_true(timeslice_cntA == 11);
+}
+
#ifndef RT_USING_SMP
static volatile rt_uint32_t yield_count;
@@ -634,6 +732,8 @@ static void testcase(void)
UTEST_UNIT_RUN(test_thread_priority);
/* delay_until */
UTEST_UNIT_RUN(test_delay_until);
+ /* timeslice */
+ UTEST_UNIT_RUN(test_timeslice);
}