test: kernel: sched: Add a test for nested scheduler lock

Add a new test for unlocking nested scheduler lock.  Make sure that
k_sched_unlock() isn't unconditionally a preemption point.

Signed-off-by: Yasushi SHOJI <y-shoji@ispace-inc.com>
This commit is contained in:
Yasushi SHOJI 2019-08-01 16:46:22 +09:00 committed by Carles Cufí
commit 1fe8269399
3 changed files with 51 additions and 0 deletions

View file

@ -65,6 +65,7 @@ void test_main(void)
ztest_unit_test(test_time_slicing_disable_preemptible),
ztest_unit_test(test_lock_preemptible),
ztest_unit_test(test_unlock_preemptible),
ztest_unit_test(test_unlock_nested_sched_lock),
ztest_unit_test(test_sched_is_preempt_thread),
ztest_unit_test(test_slice_reset),
ztest_unit_test(test_slice_scheduling),

View file

@ -37,6 +37,7 @@ void test_time_slicing_preemptible(void);
void test_time_slicing_disable_preemptible(void);
void test_lock_preemptible(void);
void test_unlock_preemptible(void);
void test_unlock_nested_sched_lock(void);
void test_sched_is_preempt_thread(void);
void test_slice_reset(void);
void test_slice_scheduling(void);

View file

@ -365,6 +365,55 @@ void test_unlock_preemptible(void)
teardown_threads();
}
/**
* @brief Validate nested k_sched_lock() and k_sched_unlock()
*
* @details In a preemptive thread, lock the scheduler twice and
* create a cooperative thread. Call k_sched_unlock() and check the
* cooperative thread haven't executed. Unlock it again to see the
* thread have executed this time.
*
* @see k_sched_lock(), k_sched_unlock()
*
* @ingroup kernel_sched_tests
*/
void test_unlock_nested_sched_lock(void)
{
/* set current thread to a preemptible priority */
init_prio = 0;
setup_threads();
/* take the scheduler lock twice */
k_sched_lock();
k_sched_lock();
/* spawn threads without wait */
spawn_threads(0);
/* do critical thing */
k_busy_wait(100000);
/* unlock once; this shouldn't let other threads to run */
k_sched_unlock();
/* checkpoint: no threads get executed */
for (int i = 0; i < THREADS_NUM; i++) {
zassert_true(tdata[i].executed == 0, NULL);
}
/* unlock another; this let the higher thread to run */
k_sched_unlock();
/* checkpoint: higher threads NOT get executed */
zassert_true(tdata[0].executed == 1, NULL);
for (int i = 1; i < THREADS_NUM; i++) {
zassert_true(tdata[i].executed == 0, NULL);
}
/* restore environment */
teardown_threads();
}
/**
* @brief validate k_wakeup() in some corner scenario
* @details trigger a timer and after expiration of timer