| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- /*
- * Copyright (c) 2006-2022, RT-Thread Development Team
- *
- * SPDX-License-Identifier: Apache-2.0
- *
- * Change Logs:
- * Date Author Notes
- * 2018-08-24 yangjie the first version
- * 2020-10-17 Meco Man translate to English comment
- */
- /*
- * Demo: scheduler hook
- *
- * This demo sets a scheduler hook function and prints context-switching information.
- *
- * read more:
- * https://www.rt-thread.io/document/site/programming-manual/thread/thread/#set-the-scheduler-hook
- */
- #include <rtthread.h>
- #define THREAD_STACK_SIZE 1024
- #define THREAD_PRIORITY 20
- #define THREAD_TIMESLICE 10
- /* counter for each thread */
- volatile rt_uint32_t count[2];
- /* thread entry function */
- /* threads #1 and #2 share one entry, but the entry parameter is different */
- static void thread_entry(void *parameter)
- {
- rt_uint32_t value;
- value = (rt_uint32_t)parameter;
- while (1)
- {
- rt_kprintf("thread %d is running\n", value);
- rt_thread_mdelay(1000);
- }
- }
- /* thread(s) handler */
- static rt_thread_t tid1 = RT_NULL;
- static rt_thread_t tid2 = RT_NULL;
- static void hook_of_scheduler(struct rt_thread *from, struct rt_thread *to)
- {
- #if RT_VER_NUM >= 0x50001
- rt_kprintf("from: %s --> to: %s \n", from->parent.name, to->parent.name);
- #else
- rt_kprintf("from: %s --> to: %s \n", from->name, to->name);
- #endif
- }
- int scheduler_hook(void)
- {
- /* set a scheduler hook function */
- rt_scheduler_sethook(hook_of_scheduler);
- tid1 = rt_thread_create("thread1",
- thread_entry, (void *)1,
- THREAD_STACK_SIZE,
- THREAD_PRIORITY, THREAD_TIMESLICE);
- #ifdef RT_USING_SMP
- /* Bind threads to the same core to avoid messy log output when multiple cores are enabled */
- rt_thread_control(tid1, RT_THREAD_CTRL_BIND_CPU, (void*)0);
- #endif
- if (tid1 != RT_NULL)
- rt_thread_startup(tid1);
- tid2 = rt_thread_create("thread2",
- thread_entry, (void *)2,
- THREAD_STACK_SIZE,
- THREAD_PRIORITY, THREAD_TIMESLICE - 5);
- #ifdef RT_USING_SMP
- /* Bind threads to the same core to avoid messy log output when multiple cores are enabled */
- rt_thread_control(tid2, RT_THREAD_CTRL_BIND_CPU, (void*)0);
- #endif
- if (tid2 != RT_NULL)
- rt_thread_startup(tid2);
- return 0;
- }
- /* export the msh command */
- MSH_CMD_EXPORT(scheduler_hook, scheduler_hook sample);
|