@@ -455,6 +455,104 @@ static void test_delay_until(void)
455455 uassert_int_equal (delta , 10 );
456456}
457457
458+ static rt_thread_t tidA , tidB1 , tidB2 ;
459+ static uint32_t timeslice_cntA , timeslice_cntB1 , timeslice_cntB2 ;
460+
461+ static void test_timeslice_threadA_entry (void * parameter )
462+ {
463+ while (1 )
464+ {
465+ rt_thread_delay (2 );
466+ timeslice_cntA ++ ;
467+ if (timeslice_cntA > 10 ) return ;
468+ }
469+ }
470+ static void test_timeslice_threadB1_entry (void * parameter )
471+ {
472+ while (1 )
473+ {
474+ timeslice_cntB1 ++ ;
475+ if (timeslice_cntA > 10 ) return ;
476+ }
477+ }
478+ static void test_timeslice_threadB2_entry (void * parameter )
479+ {
480+ while (1 )
481+ {
482+ timeslice_cntB2 ++ ;
483+ if (timeslice_cntA > 10 ) return ;
484+ }
485+ }
486+
487+ void test_timeslice (void )
488+ {
489+ rt_err_t ret_startup = - RT_ERROR ;
490+ uint32_t diff ;
491+
492+ timeslice_cntA = 0 ;
493+ timeslice_cntB1 = 0 ;
494+ timeslice_cntB2 = 0 ;
495+
496+ tidA = rt_thread_create ("timeslice" , test_timeslice_threadA_entry , RT_NULL ,
497+ 2048 , __current_thread -> current_priority + 1 , 10 );
498+ if (!tidA )
499+ {
500+ LOG_E ("rt_thread_create failed!" );
501+ return ;
502+ }
503+
504+ rt_thread_control (tidA , RT_THREAD_CTRL_BIND_CPU , (void * )1 );
505+ ret_startup = rt_thread_startup (tidA );
506+ if (ret_startup != RT_EOK )
507+ {
508+ LOG_E ("rt_thread_startup failed!" );
509+ uassert_false (1 );
510+ return ;
511+ }
512+
513+ tidB1 = rt_thread_create ("timeslice" , test_timeslice_threadB1_entry , RT_NULL ,
514+ 2048 , __current_thread -> current_priority + 2 , 2 );
515+ if (!tidB1 )
516+ {
517+ LOG_E ("rt_thread_create failed!" );
518+ return ;
519+ }
520+
521+ rt_thread_control (tidB1 , RT_THREAD_CTRL_BIND_CPU , (void * )1 );
522+ ret_startup = rt_thread_startup (tidB1 );
523+ if (ret_startup != RT_EOK )
524+ {
525+ LOG_E ("rt_thread_startup failed!" );
526+ uassert_false (1 );
527+ return ;
528+ }
529+
530+ tidB2 = rt_thread_create ("timeslice" , test_timeslice_threadB2_entry , RT_NULL ,
531+ 2048 , __current_thread -> current_priority + 2 , 2 );
532+ if (!tidB2 )
533+ {
534+ LOG_E ("rt_thread_create failed!" );
535+ return ;
536+ }
537+
538+ rt_thread_control (tidB2 , RT_THREAD_CTRL_BIND_CPU , (void * )1 );
539+ ret_startup = rt_thread_startup (tidB2 );
540+ if (ret_startup != RT_EOK )
541+ {
542+ LOG_E ("rt_thread_startup failed!" );
543+ uassert_false (1 );
544+ return ;
545+ }
546+ do {
547+ rt_thread_delay (2 * 20 );
548+ }while (timeslice_cntA <= 10 );
549+
550+ rt_kprintf ("A:%d,B1:%d,B2:%d\n" , timeslice_cntA , timeslice_cntB1 , timeslice_cntB2 );
551+ diff = abs (timeslice_cntB1 - timeslice_cntB2 );
552+ uassert_true (diff * 100 / timeslice_cntB1 < 30 );
553+ uassert_true (timeslice_cntA == 11 );
554+ }
555+
458556#ifndef RT_USING_SMP
459557static volatile rt_uint32_t yield_count ;
460558
@@ -634,6 +732,8 @@ static void testcase(void)
634732 UTEST_UNIT_RUN (test_thread_priority );
635733 /* delay_until */
636734 UTEST_UNIT_RUN (test_delay_until );
735+ /* timeslice */
736+ UTEST_UNIT_RUN (test_timeslice );
637737}
638738
639739
0 commit comments