summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorShell <[email protected]>2023-12-23 17:59:18 +0800
committerMeco Man <[email protected]>2024-01-02 19:42:46 +0800
commite7880a9713ef3efed9ad133231981761bcc3e4eb (patch)
tree14909d6f79c1c39c89aa462a58779542e6f415d2 /examples
parent3a7903b799c9f379048368df029cac3e42c28be4 (diff)
[kernel] support for hook list
Signed-off-by: Shell <[email protected]>
Diffstat (limited to 'examples')
-rw-r--r--examples/utest/testcases/kernel/Kconfig7
-rw-r--r--examples/utest/testcases/kernel/SConscript3
-rw-r--r--examples/utest/testcases/kernel/hooklist_tc.c98
3 files changed, 107 insertions, 1 deletions
diff --git a/examples/utest/testcases/kernel/Kconfig b/examples/utest/testcases/kernel/Kconfig
index b984ca6c37..fc92292f0b 100644
--- a/examples/utest/testcases/kernel/Kconfig
+++ b/examples/utest/testcases/kernel/Kconfig
@@ -59,5 +59,10 @@ config UTEST_THREAD_TC
config UTEST_ATOMIC_TC
bool "atomic test"
default n
-
+
+config UTEST_HOOKLIST_TC
+ bool "hook list test"
+ select RT_USING_HOOKLIST
+ default n
+
endmenu
diff --git a/examples/utest/testcases/kernel/SConscript b/examples/utest/testcases/kernel/SConscript
index 5d560e076b..ea2780e4d7 100644
--- a/examples/utest/testcases/kernel/SConscript
+++ b/examples/utest/testcases/kernel/SConscript
@@ -44,6 +44,9 @@ if GetDepend(['UTEST_THREAD_TC']):
if GetDepend(['UTEST_ATOMIC_TC']):
src += ['atomic_tc.c']
+if GetDepend(['UTEST_HOOKLIST_TC']):
+ src += ['hooklist_tc.c']
+
group = DefineGroup('utestcases', src, depend = ['RT_USING_UTESTCASES'], CPPPATH = CPPPATH)
Return('group')
diff --git a/examples/utest/testcases/kernel/hooklist_tc.c b/examples/utest/testcases/kernel/hooklist_tc.c
new file mode 100644
index 0000000000..a513517782
--- /dev/null
+++ b/examples/utest/testcases/kernel/hooklist_tc.c
@@ -0,0 +1,98 @@
+/*
+ * Copyright (c) 2006-2023, RT-Thread Development Team
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Change Logs:
+ * Date Author Notes
+ * 2023-12-22 Shell Support hook list
+ */
+
+#include <rtthread.h>
+#include "rtconfig.h"
+#include "utest.h"
+#include "utest_assert.h"
+
+static int hooker1_ent_count;
+static int hooker2_ent_count;
+static struct rt_thread thr_tobe_inited;
+
+static void thread_inited_hooker1(rt_thread_t thread)
+{
+ LOG_D("%s: count %d", __func__, hooker1_ent_count);
+ hooker1_ent_count += 1;
+}
+RT_OBJECT_HOOKLIST_DEFINE_NODE(rt_thread_inited, hooker1_node, thread_inited_hooker1);
+
+static void thread_inited_hooker2(rt_thread_t thread)
+{
+ LOG_D("%s: count %d", __func__, hooker2_ent_count);
+ hooker2_ent_count += 1;
+}
+RT_OBJECT_HOOKLIST_DEFINE_NODE(rt_thread_inited, hooker2_node, thread_inited_hooker2);
+
+static char _thr_stack[UTEST_THR_STACK_SIZE];
+static void thr_tobe_inited_entry(void *param)
+{
+ rt_kprintf("Hello!\n");
+}
+
+static void hooklist_test(void)
+{
+ hooker1_ent_count = 0;
+ hooker2_ent_count = 0;
+ rt_thread_inited_sethook(&hooker1_node);
+ rt_thread_inited_sethook(&hooker2_node);
+
+ /* run 1 */
+ rt_thread_init(&thr_tobe_inited,
+ "thr_tobe_inited",
+ thr_tobe_inited_entry,
+ NULL,
+ _thr_stack,
+ sizeof(_thr_stack),
+ 25,
+ 100);
+
+ uassert_int_equal(hooker1_ent_count, 1);
+ uassert_int_equal(hooker2_ent_count, 1);
+
+ rt_thread_detach(&thr_tobe_inited);
+ rt_thread_mdelay(1); /* wait recycling done */
+
+ /* run 2 */
+ rt_thread_inited_rmhook(&hooker2_node);
+
+ rt_thread_init(&thr_tobe_inited,
+ "thr_tobe_inited",
+ thr_tobe_inited_entry,
+ NULL,
+ _thr_stack,
+ sizeof(_thr_stack),
+ 25,
+ 100);
+
+ uassert_int_equal(hooker1_ent_count, 2);
+ uassert_int_equal(hooker2_ent_count, 1);
+}
+
+static rt_err_t utest_tc_init(void)
+{
+ hooker1_ent_count = 0;
+ hooker2_ent_count = 0;
+ return RT_EOK;
+}
+
+static rt_err_t utest_tc_cleanup(void)
+{
+ rt_thread_detach(&thr_tobe_inited);
+ rt_thread_inited_rmhook(&hooker1_node);
+ rt_thread_inited_rmhook(&hooker2_node);
+ return RT_EOK;
+}
+
+static void testcase(void)
+{
+ UTEST_UNIT_RUN(hooklist_test);
+}
+UTEST_TC_EXPORT(testcase, "testcases.kernel.hooklist_tc", utest_tc_init, utest_tc_cleanup, 10);