├── .gitignore ├── LICENSE ├── README.md ├── README_zh.md ├── SConscript ├── en ├── SConscript ├── dynmem_sample.c ├── event_sample.c ├── idlehook_sample.c ├── interrupt_sample.c ├── mailbox_sample.c ├── memp_sample.c ├── msgq_sample.c ├── mutex_sample.c ├── priority_inversion.c ├── producer_consumer.c ├── scheduler_hook.c ├── semaphore_sample.c ├── signal_sample.c ├── thread_sample.c ├── timer_sample.c └── timeslice_sample.c └── zh ├── SConscript ├── dynmem_sample.c ├── event_sample.c ├── idlehook_sample.c ├── interrupt_sample.c ├── mailbox_sample.c ├── mailbox_urgent_sample.c ├── memp_sample.c ├── msgq_sample.c ├── mutex_sample.c ├── priority_inversion.c ├── producer_consumer.c ├── scheduler_hook.c ├── semaphore_sample.c ├── signal_sample.c ├── thread_sample.c ├── timer_sample.c └── timeslice_sample.c /.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Object files 5 | *.o 6 | *.ko 7 | *.obj 8 | *.elf 9 | 10 | # Linker output 11 | *.ilk 12 | *.map 13 | *.exp 14 | 15 | # Precompiled Headers 16 | *.gch 17 | *.pch 18 | 19 | # Libraries 20 | *.lib 21 | *.a 22 | *.la 23 | *.lo 24 | 25 | # Shared objects (inc. Windows DLLs) 26 | *.dll 27 | *.so 28 | *.so.* 29 | *.dylib 30 | 31 | # Executables 32 | *.exe 33 | *.out 34 | *.app 35 | *.i*86 36 | *.x86_64 37 | *.hex 38 | 39 | # Debug files 40 | *.dSYM/ 41 | *.su 42 | *.idb 43 | *.pdb 44 | 45 | # Kernel Module Compile Results 46 | *.mod* 47 | *.cmd 48 | .tmp_versions/ 49 | modules.order 50 | Module.symvers 51 | Mkfile.old 52 | dkms.conf 53 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | 203 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # kernel samples package 2 | 3 | ## 1. Introduction 4 | 5 | This package contains sample code related to the RT-Thread kernel. 6 | 7 | | 文件 | 说明 | 8 | | ---- | ---- | 9 | | dynmem_sample.c | dynamic memory allocation & management | 10 | | event_sample.c | event | 11 | | idlehook_sample.c | idle thread hook function | 12 | | interrupt_sample.c | disable / enable interrupt | 13 | | mailbox_sample.c | mailbox | 14 | | memp_sample.c | memory pool | 15 | | msgq_sample.c | message queue | 16 | | mutex_sample.c | mutex | 17 | | priority_inversion.c | prevent priority inversions | 18 | | producer_consumer.c | producer & consumer problem | 19 | | scheduler_hook.c | scheduler hook function | 20 | | semaphore_sample.c | semaphore | 21 | | signal_sample.c | signal | 22 | | thread_sample.c | thread | 23 | | timer_sample.c | timer | 24 | | timeslice_sample.c | time-slicing | 25 | 26 | 27 | 28 | ## 2. How to use kernel samples package 29 | 30 | You can use [ENV tool](https://www.rt-thread.io/download.html?download=Env) or [RT-Studio IDE](https://www.rt-thread.io/studio.html) to activate this package: 31 | 32 | ``` 33 | RT-Thread online packages 34 | miscellaneous packages ---> 35 | samples: kernel and components samples ---> 36 | a kernel_samples package for rt-thread ---> 37 | ``` 38 | 39 | 40 | 41 | ## 3. License 42 | 43 | This package is an open source software and has been licensed under Apache License Version 2.0. 44 | 45 | 46 | 47 | ## 4. Maintained by 48 | 49 | * Yang Jie: https://github.com/yangjie11 50 | * Meco Man: https://github.com/mysterywolf 51 | * https://github.com/RT-Thread-packages/kernel-sample 52 | -------------------------------------------------------------------------------- /README_zh.md: -------------------------------------------------------------------------------- 1 | # kernel samples 2 | 3 | ## 1、介绍 4 | 5 | 这个软件包包含了内核相关的使用示例代码。 6 | 7 | ### 1.1 例程说明 8 | 9 | | 文件 | 说明 | 10 | | ---- | ---- | 11 | | dynmem_sample.c | 动态堆内存的使用 | 12 | | event_sample.c | 事件的使用 | 13 | | idlehook_sample.c | 空闲任务钩子的使用 | 14 | | interrupt_sample.c | 使用开关中断进行线程间同步 | 15 | | mailbox_sample.c | 邮箱的使用 | 16 | | memp_sample.c | 内存池的使用 | 17 | | msgq_sample.c | 消息队列的使用 | 18 | | mutex_sample.c | 互斥量的使用 | 19 | | priority_inversion.c | 互斥量解决优先级翻转问题 | 20 | | producer_consumer.c | 生产者消费者模型 | 21 | | scheduler_hook.c | 调度器钩子的使用 | 22 | | semaphore_sample.c | 信号量的使用| 23 | | signal_sample.c | 信号的使用 | 24 | | thread_sample.c | 线程的使用 | 25 | | timer_sample.c | 定时器的使用 | 26 | | timeslice_sample.c | 线程时间片 | 27 | 28 | ### 1.2 许可证 29 | 30 | kernel samples package 遵循 Apache license v2.0 许可,详见 `LICENSE` 文件。 31 | 32 | ### 1.3 依赖 33 | 34 | 依赖系统内核对应的模块。 35 | 36 | ## 2、如何打开 kernel samples 37 | 38 | 使用 kernel samples package 需要在 RT-Thread 的 menuconfig 配置菜单中选择它,具体路径如下: 39 | 40 | ``` 41 | RT-Thread online packages 42 | miscellaneous packages ---> 43 | samples: kernel and components samples ---> 44 | a kernel_samples package for rt-thread ---> 45 | 46 | ``` 47 | 48 | 然后让 RT-Thread 的包管理器自动更新,或者使用 `pkgs --update` 命令更新包到 BSP 中。 49 | 50 | ## 3、使用 kernel samples 51 | 52 | 在打开 kernel samples package 后,当进行 BSP 编译时,选择的软件包相关源代码会被加入到 BSP 工程中进行编译。 53 | 54 | ## 4、注意事项 55 | 56 | 暂无。 57 | 58 | ## 5、联系方式 & 感谢 59 | 60 | * 维护:yangjie11 61 | * 主页:https://github.com/RT-Thread-packages/kernel-sample.git 62 | -------------------------------------------------------------------------------- /SConscript: -------------------------------------------------------------------------------- 1 | import os 2 | from building import * 3 | 4 | # get current dir path 5 | cwd = GetCurrentDir() 6 | 7 | # traversal subscript 8 | objs = [] 9 | list = os.listdir(cwd) 10 | for d in list: 11 | path = os.path.join(cwd, d) 12 | if os.path.isfile(os.path.join(path, 'SConscript')): 13 | objs = objs + SConscript(os.path.join(d, 'SConscript')) 14 | 15 | Return('objs') 16 | -------------------------------------------------------------------------------- /en/SConscript: -------------------------------------------------------------------------------- 1 | from building import * 2 | 3 | src = [] 4 | cwd = GetCurrentDir() 5 | include_path = [cwd] 6 | 7 | # add kernel samples. 8 | if GetDepend('KERNEL_SAMPLES_USING_THREAD'): 9 | src += ['thread_sample.c'] 10 | 11 | if GetDepend('KERNEL_SAMPLES_USING_SEMAPHORE'): 12 | src += ['semaphore_sample.c'] 13 | 14 | if GetDepend('KERNEL_SAMPLES_USING_MUTEX'): 15 | src += ['mutex_sample.c'] 16 | 17 | if GetDepend('KERNEL_SAMPLES_USING_MAILBOX'): 18 | src += ['mailbox_sample.c'] 19 | 20 | if GetDepend('KERNEL_SAMPLES_USING_EVENT'): 21 | src += ['event_sample.c'] 22 | 23 | if GetDepend('KERNEL_SAMPLES_USING_MESSAGEQUEUE'): 24 | src += ['msgq_sample.c'] 25 | 26 | if GetDepend('KERNEL_SAMPLES_USING_TIMER'): 27 | src += ['timer_sample.c'] 28 | 29 | if GetDepend('KERNEL_SAMPLES_USING_HEAP'): 30 | src += ['dynmem_sample.c'] 31 | 32 | if GetDepend('KERNEL_SAMPLES_USING_MEMPOOL'): 33 | src += ['memp_sample.c'] 34 | 35 | if GetDepend('KERNEL_SAMPLES_USING_IDLEHOOK'): 36 | src += ['idlehook_sample.c'] 37 | 38 | if GetDepend('KERNEL_SAMPLES_USING_SIGNAL'): 39 | src += ['signal_sample.c'] 40 | 41 | if GetDepend('KERNEL_SAMPLES_USING_INTERRUPT'): 42 | src += ['interrupt_sample.c'] 43 | 44 | if GetDepend('KERNEL_SAMPLES_USING_PRI_INVERSION'): 45 | src += ['priority_inversion.c'] 46 | 47 | if GetDepend('KERNEL_SAMPLES_USING_TIME_SLICE'): 48 | src += ['timeslice_sample.c'] 49 | 50 | if GetDepend('KERNEL_SAMPLES_USING_SCHEDULER_HOOK'): 51 | src += ['scheduler_hook.c'] 52 | 53 | if GetDepend('KERNEL_SAMPLES_USING_PRODUCER_CONSUMER'): 54 | src += ['producer_consumer.c'] 55 | 56 | group = DefineGroup('kernel-samples', src, depend = ['PKG_USING_KERNEL_SAMPLES_EN'], CPPPATH = include_path) 57 | 58 | Return('group') 59 | -------------------------------------------------------------------------------- /en/dynmem_sample.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006-2022, RT-Thread Development Team 3 | * 4 | * SPDX-License-Identifier: Apache-2.0 5 | * 6 | * Change Logs: 7 | * Date Author Notes 8 | * 2018-08-24 yangjie the first version 9 | * 2020-10-17 Meco Man translate to English comment 10 | */ 11 | 12 | /* 13 | * Demo: dynamic memory management 14 | * 15 | * This demo creates a dynamic thread to allocate and free memory. 16 | * Each time it allocates more memory, and it will end when it can't allocate any memory. 17 | * 18 | * read more: 19 | * https://www.rt-thread.io/document/site/programming-manual/memory/memory/ 20 | */ 21 | 22 | #include 23 | 24 | #define THREAD_PRIORITY 25 25 | #define THREAD_STACK_SIZE 512 26 | #define THREAD_TIMESLICE 5 27 | 28 | /* thread #1 entry function*/ 29 | void thread1_entry(void *parameter) 30 | { 31 | int i; 32 | char *ptr = RT_NULL; /* memory's pointer */ 33 | 34 | for (i = 0; ; i++) 35 | { 36 | /* allocate memory of (1 << i) bytes */ 37 | ptr = rt_malloc(1 << i); 38 | 39 | if (ptr != RT_NULL) 40 | { 41 | /* if memory allocated successfully */ 42 | rt_kprintf("get memory :%d byte\n", (1 << i)); 43 | rt_free(ptr); /* free memory */ 44 | rt_kprintf("free memory :%d byte\n", (1 << i)); 45 | ptr = RT_NULL; 46 | } 47 | else 48 | { 49 | rt_kprintf("try to get %d byte memory failed!\n", (1 << i)); 50 | return; 51 | } 52 | } 53 | } 54 | 55 | int dynmem_sample(void) 56 | { 57 | rt_thread_t tid = RT_NULL; 58 | 59 | /* create thread #1 */ 60 | tid = rt_thread_create("thread1", 61 | thread1_entry, RT_NULL, 62 | THREAD_STACK_SIZE, 63 | THREAD_PRIORITY, 64 | THREAD_TIMESLICE); 65 | /*start thread #1 */ 66 | if (tid != RT_NULL) 67 | rt_thread_startup(tid); 68 | 69 | return 0; 70 | } 71 | 72 | /* export the msh command */ 73 | MSH_CMD_EXPORT(dynmem_sample, dynmem sample); 74 | -------------------------------------------------------------------------------- /en/event_sample.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006-2022, RT-Thread Development Team 3 | * 4 | * SPDX-License-Identifier: Apache-2.0 5 | * 6 | * Change Logs: 7 | * Date Author Notes 8 | * 2018-08-24 yangjie the first version 9 | * 2020-10-17 Meco Man translate to English comment 10 | */ 11 | 12 | /* 13 | * Demo: event(s) 14 | * 15 | * This demo creates two threads and one statical event: 16 | * 1) thread #1: pend and receive events 17 | * 2) thread #2: sent events (Event3 and Event5) 18 | * 19 | * read more: 20 | * https://www.rt-thread.io/document/site/programming-manual/ipc1/ipc1/#event 21 | */ 22 | 23 | #include 24 | 25 | #define THREAD_PRIORITY 9 26 | #define THREAD_TIMESLICE 5 27 | 28 | #define EVENT_FLAG3 (1 << 3) 29 | #define EVENT_FLAG5 (1 << 5) 30 | 31 | /* ECB (Event Control Block) */ 32 | static struct rt_event event; 33 | 34 | #ifdef rt_align 35 | rt_align(RT_ALIGN_SIZE) 36 | #else 37 | ALIGN(RT_ALIGN_SIZE) 38 | #endif 39 | static char thread1_stack[1024]; /* thread stack 1024 Byte*/ 40 | static struct rt_thread thread1; /* TCB (Thread Control Block) */ 41 | 42 | /* thread #1 entry function */ 43 | static void thread1_recv_event(void *param) 44 | { 45 | rt_uint32_t e; 46 | 47 | /* 48 | first time to receive event(s): 49 | EITHER Event3 OR Event5 happened can resume thread1 50 | and then clear conrresponding event(s)' flag 51 | */ 52 | if (rt_event_recv(&event, (EVENT_FLAG3 | EVENT_FLAG5), 53 | RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR, 54 | RT_WAITING_FOREVER, &e) == RT_EOK) 55 | { 56 | rt_kprintf("thread1: OR recv event 0x%x\n", e); 57 | } 58 | 59 | rt_kprintf("thread1: delay 1s to prepare the second event\n"); 60 | rt_thread_mdelay(1000); 61 | 62 | /* 63 | second time to receive event(s): 64 | BOTH Event3 AND Event5 happened can resume thread1 65 | and then clear conrresponding event(s)' flag 66 | */ 67 | if (rt_event_recv(&event, (EVENT_FLAG3 | EVENT_FLAG5), 68 | RT_EVENT_FLAG_AND | RT_EVENT_FLAG_CLEAR, 69 | RT_WAITING_FOREVER, &e) == RT_EOK) 70 | { 71 | rt_kprintf("thread1: AND recv event 0x%x\n", e); 72 | } 73 | rt_kprintf("thread1 leave.\n"); 74 | } 75 | 76 | #ifdef rt_align 77 | rt_align(RT_ALIGN_SIZE) 78 | #else 79 | ALIGN(RT_ALIGN_SIZE) 80 | #endif 81 | static char thread2_stack[1024]; /* thread stack 1024 Byte*/ 82 | static struct rt_thread thread2; /* TCB (Thread Control Block) */ 83 | 84 | /* thread #2 entry function */ 85 | static void thread2_send_event(void *param) 86 | { 87 | rt_kprintf("thread2: send event3\n"); 88 | rt_event_send(&event, EVENT_FLAG3); 89 | rt_thread_mdelay(200); 90 | 91 | rt_kprintf("thread2: send event5\n"); 92 | rt_event_send(&event, EVENT_FLAG5); 93 | rt_thread_mdelay(200); 94 | 95 | rt_kprintf("thread2: send event3\n"); 96 | rt_event_send(&event, EVENT_FLAG3); 97 | rt_kprintf("thread2 leave.\n"); 98 | } 99 | 100 | int event_sample(void) 101 | { 102 | rt_err_t result; 103 | 104 | /* initiate the event (statically) */ 105 | result = rt_event_init(&event, "event", RT_IPC_FLAG_PRIO); 106 | if (result != RT_EOK) 107 | { 108 | rt_kprintf("init event failed.\n"); 109 | return -1; 110 | } 111 | 112 | /* initiate the thread #1 (statically) */ 113 | rt_thread_init(&thread1, 114 | "thread1", 115 | thread1_recv_event, 116 | RT_NULL, 117 | &thread1_stack[0], 118 | sizeof(thread1_stack), 119 | THREAD_PRIORITY - 1, THREAD_TIMESLICE); 120 | #ifdef RT_USING_SMP 121 | /* Bind threads to the same core to avoid messy log output when multiple cores are enabled */ 122 | rt_thread_control(&thread1, RT_THREAD_CTRL_BIND_CPU, (void*)0); 123 | #endif 124 | rt_thread_startup(&thread1); /* start thread #1 */ 125 | 126 | /* initiate the thread #2 (statically) */ 127 | rt_thread_init(&thread2, 128 | "thread2", 129 | thread2_send_event, 130 | RT_NULL, 131 | &thread2_stack[0], 132 | sizeof(thread2_stack), 133 | THREAD_PRIORITY, THREAD_TIMESLICE); 134 | #ifdef RT_USING_SMP 135 | /* Bind threads to the same core to avoid messy log output when multiple cores are enabled */ 136 | rt_thread_control(&thread2, RT_THREAD_CTRL_BIND_CPU, (void*)0); 137 | #endif 138 | rt_thread_startup(&thread2); /* start thread #2 */ 139 | 140 | return 0; 141 | } 142 | 143 | /* export the msh command */ 144 | MSH_CMD_EXPORT(event_sample, event sample); 145 | -------------------------------------------------------------------------------- /en/idlehook_sample.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006-2022, RT-Thread Development Team 3 | * 4 | * SPDX-License-Identifier: Apache-2.0 5 | * 6 | * Change Logs: 7 | * Date Author Notes 8 | * 2018-08-24 yangjie the first version 9 | * 2020-10-17 Meco Man translate to English comment 10 | */ 11 | 12 | /* 13 | * Demo: idle hook 14 | * 15 | * This demo creates a thread and set an idle thread hook function. The idle 16 | * thread hook function will be invoked when thread #1 is delaying. 17 | * 18 | * read more: 19 | * https://www.rt-thread.io/document/site/programming-manual/thread/thread/#set-and-delete-idle-hooks 20 | * https://www.rt-thread.io/document/site/programming-manual/thread/thread/#idle-thread 21 | */ 22 | 23 | #include 24 | 25 | #define THREAD_PRIORITY 20 26 | #define THREAD_STACK_SIZE 1024 27 | #define THREAD_TIMESLICE 5 28 | 29 | /* thread handler */ 30 | static rt_thread_t tid = RT_NULL; 31 | 32 | /* idle thread hook function running number of times */ 33 | volatile static int hook_times = 0; 34 | 35 | /* idle thread hook function */ 36 | static void idle_hook() 37 | { 38 | if (0 == (hook_times % 10000)) 39 | { 40 | rt_kprintf("enter idle hook %d times.\n", hook_times); 41 | } 42 | 43 | rt_enter_critical(); 44 | hook_times++; 45 | rt_exit_critical(); 46 | } 47 | 48 | /* thread entry function */ 49 | static void thread_entry(void *parameter) 50 | { 51 | int i = 5; 52 | while (i--) 53 | { 54 | rt_kprintf("enter thread1.\n"); 55 | rt_enter_critical(); 56 | hook_times = 0; 57 | rt_exit_critical(); 58 | 59 | /* sleep for 500ms */ 60 | rt_kprintf("thread1 delay 500ms.\n"); 61 | rt_thread_mdelay(500); 62 | } 63 | rt_kprintf("delete idle hook.\n"); 64 | 65 | /* remove idle thread hook function */ 66 | rt_thread_idle_delhook(idle_hook); 67 | rt_kprintf("thread1 finish.\n"); 68 | } 69 | 70 | int idle_hook_sample(void) 71 | { 72 | /* set idle thread hook function */ 73 | rt_thread_idle_sethook(idle_hook); 74 | 75 | /* create thread #1 */ 76 | tid = rt_thread_create("thread1", 77 | thread_entry, RT_NULL, 78 | THREAD_STACK_SIZE, 79 | THREAD_PRIORITY, THREAD_TIMESLICE); 80 | if (tid != RT_NULL) 81 | rt_thread_startup(tid); 82 | 83 | return 0; 84 | } 85 | 86 | /* export the msh command */ 87 | MSH_CMD_EXPORT(idle_hook_sample, idle hook sample); 88 | -------------------------------------------------------------------------------- /en/interrupt_sample.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006-2022, RT-Thread Development Team 3 | * 4 | * SPDX-License-Identifier: Apache-2.0 5 | * 6 | * Change Logs: 7 | * Date Author Notes 8 | * 2018-08-24 yangjie the first version 9 | * 2020-10-17 Meco Man translate to English comment 10 | */ 11 | 12 | /* 13 | * Demo: interrupt 14 | * 15 | * This demo demonstrates how to disable and enable interrupt to access global variable. 16 | * 17 | * read more: 18 | * https://www.rt-thread.io/document/site/programming-manual/interrupt/interrupt/ 19 | */ 20 | 21 | #include 22 | #include 23 | 24 | #define THREAD_PRIORITY 20 25 | #define THREAD_STACK_SIZE 512 26 | #define THREAD_TIMESLICE 5 27 | 28 | /* global variable */ 29 | static volatile rt_uint32_t cnt; 30 | 31 | /* thread entry function */ 32 | /* threads #1 and #2 share one entry, but the entry parameter is different */ 33 | static void thread_entry(void *parameter) 34 | { 35 | rt_uint32_t no; 36 | rt_uint32_t level; 37 | 38 | no = (rt_uint32_t) parameter; 39 | 40 | while (1) 41 | { 42 | /* disable interrupt */ 43 | level = rt_hw_interrupt_disable(); 44 | 45 | cnt += no; /* critical sections (or critical region) */ 46 | 47 | /* enable interrupt */ 48 | rt_hw_interrupt_enable(level); 49 | 50 | rt_kprintf("protect thread[%d]'s counter is %d\n", no, cnt); 51 | rt_thread_mdelay(no * 10); 52 | } 53 | } 54 | 55 | int interrupt_sample(void) 56 | { 57 | rt_thread_t thread; 58 | 59 | /* create thread #1 */ 60 | thread = rt_thread_create("thread1", thread_entry, (void *)10, 61 | THREAD_STACK_SIZE, 62 | THREAD_PRIORITY, THREAD_TIMESLICE); 63 | #ifdef RT_USING_SMP 64 | /* Bind threads to the same core to avoid messy log output when multiple cores are enabled */ 65 | rt_thread_control(thread, RT_THREAD_CTRL_BIND_CPU, (void*)0); 66 | #endif 67 | if (thread != RT_NULL) 68 | rt_thread_startup(thread); /* start thread #1 */ 69 | 70 | /* create thread #2 */ 71 | thread = rt_thread_create("thread2", thread_entry, (void *)20, 72 | THREAD_STACK_SIZE, 73 | THREAD_PRIORITY, THREAD_TIMESLICE); 74 | #ifdef RT_USING_SMP 75 | /* Bind threads to the same core to avoid messy log output when multiple cores are enabled */ 76 | rt_thread_control(thread, RT_THREAD_CTRL_BIND_CPU, (void*)0); 77 | #endif 78 | if (thread != RT_NULL) 79 | rt_thread_startup(thread); /* start thread #2 */ 80 | 81 | return 0; 82 | } 83 | 84 | /* export the msh command */ 85 | MSH_CMD_EXPORT(interrupt_sample, interrupt sample); 86 | -------------------------------------------------------------------------------- /en/mailbox_sample.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006-2022, RT-Thread Development Team 3 | * 4 | * SPDX-License-Identifier: Apache-2.0 5 | * 6 | * Change Logs: 7 | * Date Author Notes 8 | * 2018-08-24 yangjie the first version 9 | * 2020-10-17 Meco Man translate to English comment 10 | */ 11 | 12 | /* 13 | * Demo: 14 | * 15 | * This demo creates two threads and one boxmail (static): 16 | * 1) thread #1: receive mails 17 | * 2) thread #2: send mails 18 | * 19 | * read more: 20 | * https://www.rt-thread.io/document/site/programming-manual/ipc2/ipc2/#mailbox 21 | */ 22 | 23 | #include 24 | 25 | #define THREAD_PRIORITY 10 26 | #define THREAD_TIMESLICE 5 27 | 28 | /* mailbox control block */ 29 | static struct rt_mailbox mb; 30 | 31 | /* memory pool for mails storage */ 32 | static char mb_pool[128]; 33 | 34 | static char mb_str1[] = "I'm a mail!"; 35 | static char mb_str2[] = "this is another mail!"; 36 | static char mb_str3[] = "over"; 37 | 38 | #ifdef rt_align 39 | rt_align(RT_ALIGN_SIZE) 40 | #else 41 | ALIGN(RT_ALIGN_SIZE) 42 | #endif 43 | static char thread1_stack[1024]; 44 | static struct rt_thread thread1; 45 | 46 | /* thread #1 entry function */ 47 | static void thread1_entry(void *parameter) 48 | { 49 | char *str; 50 | 51 | while (1) 52 | { 53 | rt_kprintf("thread1: try to recv a mail\n"); 54 | 55 | /* pend and receive mail(s) from mailbox */ 56 | if (rt_mb_recv(&mb, (rt_ubase_t *)&str, RT_WAITING_FOREVER) == RT_EOK) 57 | { 58 | rt_kprintf("thread1: get a mail from mailbox, the content:%s\n", str); 59 | if (str == mb_str3) 60 | break; 61 | 62 | /* delay for 100ms */ 63 | rt_thread_mdelay(100); 64 | } 65 | } 66 | /* detach mailbox */ 67 | rt_mb_detach(&mb); 68 | } 69 | 70 | #ifdef rt_align 71 | rt_align(RT_ALIGN_SIZE) 72 | #else 73 | ALIGN(RT_ALIGN_SIZE) 74 | #endif 75 | static char thread2_stack[1024]; 76 | static struct rt_thread thread2; 77 | 78 | /* thread #2 entry function */ 79 | static void thread2_entry(void *parameter) 80 | { 81 | rt_uint8_t count; 82 | 83 | count = 0; 84 | while (count < 10) 85 | { 86 | count ++; 87 | if (count & 0x1) 88 | { 89 | /* send the 'mb_str1' variable's address to the mailbox */ 90 | rt_mb_send(&mb, (rt_uint32_t)&mb_str1); 91 | } 92 | else 93 | { 94 | /* send the 'mb_str2' variable's address to the mailbox */ 95 | rt_mb_send(&mb, (rt_uint32_t)&mb_str2); 96 | } 97 | 98 | /* delay for 200ms */ 99 | rt_thread_mdelay(200); 100 | } 101 | 102 | /* send the 'mb_str3' variable's address to the mailbox */ 103 | /* to inform thread #1 that thread #2 has finished running */ 104 | rt_mb_send(&mb, (rt_uint32_t)&mb_str3); 105 | } 106 | 107 | /* mailbox(s) demo */ 108 | int mailbox_sample(void) 109 | { 110 | rt_err_t result; 111 | 112 | /* initiate a mailbox */ 113 | result = rt_mb_init(&mb, 114 | "mbt", 115 | &mb_pool[0], 116 | sizeof(mb_pool) / sizeof(rt_ubase_t), /* size of mails */ 117 | RT_IPC_FLAG_PRIO); 118 | if (result != RT_EOK) 119 | { 120 | rt_kprintf("init mailbox failed.\n"); 121 | return -1; 122 | } 123 | 124 | /* initiate thread #1 */ 125 | rt_thread_init(&thread1, 126 | "thread1", 127 | thread1_entry, 128 | RT_NULL, 129 | &thread1_stack[0], 130 | sizeof(thread1_stack), 131 | THREAD_PRIORITY, THREAD_TIMESLICE); 132 | #ifdef RT_USING_SMP 133 | /* Bind threads to the same core to avoid messy log output when multiple cores are enabled */ 134 | rt_thread_control(&thread1, RT_THREAD_CTRL_BIND_CPU, (void*)0); 135 | #endif 136 | rt_thread_startup(&thread1); /* start thread #1 */ 137 | 138 | /*initiate thread #2 */ 139 | rt_thread_init(&thread2, 140 | "thread2", 141 | thread2_entry, 142 | RT_NULL, 143 | &thread2_stack[0], 144 | sizeof(thread2_stack), 145 | THREAD_PRIORITY, THREAD_TIMESLICE); 146 | #ifdef RT_USING_SMP 147 | /* Bind threads to the same core to avoid messy log output when multiple cores are enabled */ 148 | rt_thread_control(&thread2, RT_THREAD_CTRL_BIND_CPU, (void*)0); 149 | #endif 150 | rt_thread_startup(&thread2); /* start thread #2 */ 151 | 152 | return 0; 153 | } 154 | 155 | /* export the msh command */ 156 | MSH_CMD_EXPORT(mailbox_sample, mailbox sample); 157 | -------------------------------------------------------------------------------- /en/memp_sample.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006-2022, RT-Thread Development Team 3 | * 4 | * SPDX-License-Identifier: Apache-2.0 5 | * 6 | * Change Logs: 7 | * Date Author Notes 8 | * 2018-08-24 yangjie the first version 9 | * 2020-10-17 Meco Man translate to English comment 10 | */ 11 | 12 | /* 13 | * Demo: memory pool 14 | * 15 | * This demo creates one memory pool and two threads: 16 | * 1) thread #1: allocate memory pool 17 | * 2) thread #2: free memory pool 18 | * 19 | * read more: 20 | * https://www.rt-thread.io/document/site/programming-manual/memory/memory/#memory-pool 21 | */ 22 | 23 | #include 24 | 25 | static rt_uint8_t *ptr[50]; 26 | static rt_uint8_t mempool[4096]; 27 | static struct rt_mempool mp; 28 | 29 | #define THREAD_PRIORITY 25 30 | #define THREAD_STACK_SIZE 512 31 | #define THREAD_TIMESLICE 5 32 | 33 | /* thread handler */ 34 | static rt_thread_t tid1 = RT_NULL; 35 | static rt_thread_t tid2 = RT_NULL; 36 | 37 | /* thread #1 entry function */ 38 | static void thread1_mp_alloc(void *parameter) 39 | { 40 | int i; 41 | for (i = 0 ; i < 50 ; i++) 42 | { 43 | if (ptr[i] == RT_NULL) 44 | { 45 | /* allocate memory from the memory pool */ 46 | ptr[i] = rt_mp_alloc(&mp, RT_WAITING_FOREVER); 47 | if (ptr[i] != RT_NULL) 48 | rt_kprintf("allocate No.%d\n", i); 49 | } 50 | } 51 | } 52 | 53 | /* thread #2 entry function */ 54 | static void thread2_mp_release(void *parameter) 55 | { 56 | int i; 57 | 58 | rt_kprintf("thread2 try to release block\n"); 59 | for (i = 0; i < 50 ; i++) 60 | { 61 | /* free all memory */ 62 | if (ptr[i] != RT_NULL) 63 | { 64 | rt_kprintf("release block %d\n", i); 65 | rt_mp_free(ptr[i]); 66 | ptr[i] = RT_NULL; 67 | } 68 | } 69 | } 70 | 71 | int mempool_sample(void) 72 | { 73 | int i; 74 | for (i = 0; i < 50; i ++) ptr[i] = RT_NULL; 75 | 76 | /* initiate memory pool */ 77 | rt_mp_init(&mp, "mp1", &mempool[0], sizeof(mempool), 80); 78 | 79 | /* create thread #1 */ 80 | tid1 = rt_thread_create("thread1", thread1_mp_alloc, RT_NULL, 81 | THREAD_STACK_SIZE, 82 | THREAD_PRIORITY, THREAD_TIMESLICE); 83 | #ifdef RT_USING_SMP 84 | /* Bind threads to the same core to avoid messy log output when multiple cores are enabled */ 85 | rt_thread_control(tid1, RT_THREAD_CTRL_BIND_CPU, (void*)0); 86 | #endif 87 | if (tid1 != RT_NULL) 88 | rt_thread_startup(tid1); /* start thread #1 */ 89 | 90 | /* create thread #2 */ 91 | tid2 = rt_thread_create("thread2", thread2_mp_release, RT_NULL, 92 | THREAD_STACK_SIZE, 93 | THREAD_PRIORITY + 1, THREAD_TIMESLICE); 94 | #ifdef RT_USING_SMP 95 | /* Bind threads to the same core to avoid messy log output when multiple cores are enabled */ 96 | rt_thread_control(tid2, RT_THREAD_CTRL_BIND_CPU, (void*)0); 97 | #endif 98 | if (tid2 != RT_NULL) 99 | rt_thread_startup(tid2); /* start thread #2 */ 100 | 101 | return 0; 102 | } 103 | 104 | /* export the msh command */ 105 | MSH_CMD_EXPORT(mempool_sample, mempool sample); 106 | -------------------------------------------------------------------------------- /en/msgq_sample.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006-2022, RT-Thread Development Team 3 | * 4 | * SPDX-License-Identifier: Apache-2.0 5 | * 6 | * Change Logs: 7 | * Date Author Notes 8 | * 2018-08-24 yangjie the first version 9 | * 2020-10-17 Meco Man translate to English comment 10 | */ 11 | 12 | /* 13 | * Demo: message queue 14 | * 15 | * This demo creates two threads and one message queue: 16 | * 1) thread #1: receive message(s) from message queue 17 | * 2) thread #2: send normal and urgent messages to message queue 18 | * 19 | * read more: 20 | * https://www.rt-thread.io/document/site/programming-manual/ipc2/ipc2/#message-queue 21 | */ 22 | 23 | #include 24 | 25 | #define THREAD_PRIORITY 25 26 | #define THREAD_TIMESLICE 5 27 | 28 | /* message queue control block */ 29 | static struct rt_messagequeue mq; 30 | /* the memory pool used to place messages in the message queue */ 31 | static rt_uint8_t msg_pool[2048]; 32 | 33 | #ifdef rt_align 34 | rt_align(RT_ALIGN_SIZE) 35 | #else 36 | ALIGN(RT_ALIGN_SIZE) 37 | #endif 38 | static char thread1_stack[1024]; 39 | static struct rt_thread thread1; 40 | 41 | /* thread #1 entry function */ 42 | static void thread1_entry(void *parameter) 43 | { 44 | char buf = 0; 45 | rt_uint8_t cnt = 0; 46 | 47 | while (1) 48 | { 49 | /* pend and receive message(s) from message queue */ 50 | #if (RTTHREAD_VERSION >= RT_VERSION_CHECK(5, 0, 1)) 51 | if (rt_mq_recv(&mq, &buf, sizeof(buf), RT_WAITING_FOREVER) > 0) 52 | #else 53 | if (rt_mq_recv(&mq, &buf, sizeof(buf), RT_WAITING_FOREVER) == RT_EOK) 54 | #endif 55 | { 56 | rt_kprintf("thread1: recv msg from msg queue, the content:%c\n", buf); 57 | if (cnt == 19) 58 | { 59 | break; 60 | } 61 | } 62 | cnt++; 63 | 64 | /* delay for 50ms */ 65 | rt_thread_mdelay(50); 66 | } 67 | rt_kprintf("thread1: detach mq \n"); 68 | rt_mq_detach(&mq); 69 | } 70 | 71 | #ifdef rt_align 72 | rt_align(RT_ALIGN_SIZE) 73 | #else 74 | ALIGN(RT_ALIGN_SIZE) 75 | #endif 76 | static char thread2_stack[1024]; 77 | static struct rt_thread thread2; 78 | 79 | /* thread #2 entry function */ 80 | static void thread2_entry(void *parameter) 81 | { 82 | int result; 83 | char buf = 'A'; 84 | rt_uint8_t cnt = 0; 85 | 86 | while (1) 87 | { 88 | if (cnt == 8) 89 | { 90 | /* send one URGENT message to the message queue */ 91 | result = rt_mq_urgent(&mq, &buf, 1); 92 | if (result != RT_EOK) 93 | { 94 | rt_kprintf("rt_mq_urgent ERR\n"); 95 | } 96 | else 97 | { 98 | rt_kprintf("thread2: send urgent message - %c\n", buf); 99 | } 100 | } 101 | else if (cnt >= 20) /* exit */ 102 | { 103 | rt_kprintf("message queue stop send, thread2 quit\n"); 104 | break; 105 | } 106 | else 107 | { 108 | /* send one message to the message queue */ 109 | result = rt_mq_send(&mq, &buf, 1); 110 | if (result != RT_EOK) 111 | { 112 | rt_kprintf("rt_mq_send ERR\n"); 113 | } 114 | 115 | rt_kprintf("thread2: send message - %c\n", buf); 116 | } 117 | buf++; 118 | cnt++; 119 | 120 | /* delay for 5ms */ 121 | rt_thread_mdelay(5); 122 | } 123 | } 124 | 125 | int msgq_sample(void) 126 | { 127 | rt_err_t result; 128 | 129 | /* initiate a message queue */ 130 | result = rt_mq_init(&mq, 131 | "mqt", 132 | &msg_pool[0], /* msg_pool's address */ 133 | 1, /* the size of each message is 1 byte */ 134 | sizeof(msg_pool), /* The size of the memory pool is the size of msg_pool */ 135 | RT_IPC_FLAG_PRIO); 136 | 137 | if (result != RT_EOK) 138 | { 139 | rt_kprintf("init message queue failed.\n"); 140 | return -1; 141 | } 142 | 143 | rt_thread_init(&thread1, 144 | "thread1", 145 | thread1_entry, 146 | RT_NULL, 147 | &thread1_stack[0], 148 | sizeof(thread1_stack), 149 | THREAD_PRIORITY, THREAD_TIMESLICE); 150 | #ifdef RT_USING_SMP 151 | /* Bind threads to the same core to avoid messy log output when multiple cores are enabled */ 152 | rt_thread_control(&thread1, RT_THREAD_CTRL_BIND_CPU, (void*)0); 153 | #endif 154 | rt_thread_startup(&thread1); 155 | 156 | rt_thread_init(&thread2, 157 | "thread2", 158 | thread2_entry, 159 | RT_NULL, 160 | &thread2_stack[0], 161 | sizeof(thread2_stack), 162 | THREAD_PRIORITY, THREAD_TIMESLICE); 163 | #ifdef RT_USING_SMP 164 | /* Bind threads to the same core to avoid messy log output when multiple cores are enabled */ 165 | rt_thread_control(&thread2, RT_THREAD_CTRL_BIND_CPU, (void*)0); 166 | #endif 167 | rt_thread_startup(&thread2); 168 | 169 | return 0; 170 | } 171 | 172 | /* export the msh command */ 173 | MSH_CMD_EXPORT(msgq_sample, msgq sample); 174 | -------------------------------------------------------------------------------- /en/mutex_sample.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006-2022, RT-Thread Development Team 3 | * 4 | * SPDX-License-Identifier: Apache-2.0 5 | * 6 | * Change Logs: 7 | * Date Author Notes 8 | * 2018-08-24 yangjie the first version 9 | * 2020-10-17 Meco Man translate to English comment 10 | */ 11 | 12 | /* 13 | * Demo: mutex(es) 14 | * 15 | * This demo demonstrates how the mutex manage the shared resource. 16 | * 17 | * read more: 18 | * https://www.rt-thread.io/document/site/programming-manual/ipc1/ipc1/#mutex 19 | */ 20 | 21 | #include 22 | 23 | #define THREAD_PRIORITY 8 24 | #define THREAD_TIMESLICE 5 25 | 26 | /* mutex handler */ 27 | static rt_mutex_t dynamic_mutex = RT_NULL; 28 | static rt_uint8_t number1, number2 = 0; 29 | 30 | #ifdef rt_align 31 | rt_align(RT_ALIGN_SIZE) 32 | #else 33 | ALIGN(RT_ALIGN_SIZE) 34 | #endif 35 | static char thread1_stack[1024]; 36 | static struct rt_thread thread1; 37 | static void rt_thread_entry1(void *parameter) 38 | { 39 | while (1) 40 | { 41 | /* pending the mutex */ 42 | rt_mutex_take(dynamic_mutex, RT_WAITING_FOREVER); 43 | 44 | /* protect and deal with public variables */ 45 | number1++; 46 | rt_thread_mdelay(10); 47 | number2++; 48 | 49 | /* release the mutex */ 50 | rt_mutex_release(dynamic_mutex); 51 | } 52 | } 53 | 54 | #ifdef rt_align 55 | rt_align(RT_ALIGN_SIZE) 56 | #else 57 | ALIGN(RT_ALIGN_SIZE) 58 | #endif 59 | static char thread2_stack[1024]; 60 | static struct rt_thread thread2; 61 | static void rt_thread_entry2(void *parameter) 62 | { 63 | while (1) 64 | { 65 | rt_mutex_take(dynamic_mutex, RT_WAITING_FOREVER); 66 | if (number1 != number2) 67 | { 68 | rt_kprintf("not protect.number1 = %d, mumber2 = %d \n", number1, number2); 69 | } 70 | else 71 | { 72 | rt_kprintf("mutex protect ,number1 = mumber2 is %d\n", number1); 73 | } 74 | 75 | number1++; 76 | number2++; 77 | rt_mutex_release(dynamic_mutex); 78 | 79 | if (number1 >= 50) 80 | return; 81 | } 82 | } 83 | 84 | int mutex_sample(void) 85 | { 86 | /* create mutex */ 87 | dynamic_mutex = rt_mutex_create("dmutex", RT_IPC_FLAG_PRIO); 88 | if (dynamic_mutex == RT_NULL) 89 | { 90 | rt_kprintf("create dynamic mutex failed.\n"); 91 | return -1; 92 | } 93 | 94 | rt_thread_init(&thread1, 95 | "thread1", 96 | rt_thread_entry1, 97 | RT_NULL, 98 | &thread1_stack[0], 99 | sizeof(thread1_stack), 100 | THREAD_PRIORITY, THREAD_TIMESLICE); 101 | #ifdef RT_USING_SMP 102 | /* Bind threads to the same core to avoid messy log output when multiple cores are enabled */ 103 | rt_thread_control(&thread1, RT_THREAD_CTRL_BIND_CPU, (void*)0); 104 | #endif 105 | rt_thread_startup(&thread1); 106 | 107 | rt_thread_init(&thread2, 108 | "thread2", 109 | rt_thread_entry2, 110 | RT_NULL, 111 | &thread2_stack[0], 112 | sizeof(thread2_stack), 113 | THREAD_PRIORITY - 1, THREAD_TIMESLICE); 114 | #ifdef RT_USING_SMP 115 | /* Bind threads to the same core to avoid messy log output when multiple cores are enabled */ 116 | rt_thread_control(&thread2, RT_THREAD_CTRL_BIND_CPU, (void*)0); 117 | #endif 118 | rt_thread_startup(&thread2); 119 | return 0; 120 | } 121 | 122 | /* export the msh command */ 123 | MSH_CMD_EXPORT(mutex_sample, mutex sample); 124 | -------------------------------------------------------------------------------- /en/priority_inversion.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006-2022, RT-Thread Development Team 3 | * 4 | * SPDX-License-Identifier: Apache-2.0 5 | * 6 | * Change Logs: 7 | * Date Author Notes 8 | * 2018-08-24 yangjie the first version 9 | * 2020-10-17 Meco Man translate to English comment 10 | */ 11 | 12 | /* 13 | * Demo: prevent priority inversions 14 | * 15 | * This demo creates 3 threads and 1 mutex to 16 | * demonstrate how the mutex prevents priority inversions. 17 | * 18 | * read more: 19 | * https://www.rt-thread.io/document/site/programming-manual/ipc1/ipc1/#mutex 20 | */ 21 | 22 | #include 23 | 24 | /* thread(s) handler */ 25 | static rt_thread_t tid1 = RT_NULL; 26 | static rt_thread_t tid2 = RT_NULL; 27 | static rt_thread_t tid3 = RT_NULL; 28 | static rt_mutex_t mutex = RT_NULL; 29 | 30 | #define THREAD_PRIORITY 10 31 | #define THREAD_STACK_SIZE 512 32 | #define THREAD_TIMESLICE 5 33 | 34 | /* thread #1 entry function */ 35 | static void thread1_entry(void *parameter) 36 | { 37 | /* let the lower priority thread run first */ 38 | rt_thread_mdelay(100); 39 | 40 | /* at this point, thread #3 holds the mutex and thread #2 is pending on holding the mutex */ 41 | 42 | /* check the priority level of thread #2 and thread #3 */ 43 | #if (RTTHREAD_VERSION >= RT_VERSION_CHECK(5, 1, 0)) 44 | if (RT_SCHED_PRIV(tid2).current_priority != RT_SCHED_PRIV(tid3).current_priority) 45 | { 46 | /* failure */ 47 | rt_kprintf("the priority of thread2 is: %d\n", RT_SCHED_PRIV(tid2).current_priority); 48 | rt_kprintf("the priority of thread3 is: %d\n", RT_SCHED_PRIV(tid3).current_priority); 49 | rt_kprintf("test failed.\n"); 50 | return; 51 | } 52 | else 53 | { 54 | rt_kprintf("the priority of thread2 is: %d\n", RT_SCHED_PRIV(tid2).current_priority); 55 | rt_kprintf("the priority of thread3 is: %d\n", RT_SCHED_PRIV(tid3).current_priority); 56 | rt_kprintf("test OK.\n"); 57 | } 58 | #else 59 | if (tid2->current_priority != tid3->current_priority) 60 | { 61 | /* failure */ 62 | rt_kprintf("the priority of thread2 is: %d\n", tid2->current_priority); 63 | rt_kprintf("the priority of thread3 is: %d\n", tid3->current_priority); 64 | rt_kprintf("test failed.\n"); 65 | return; 66 | } 67 | else 68 | { 69 | rt_kprintf("the priority of thread2 is: %d\n", tid2->current_priority); 70 | rt_kprintf("the priority of thread3 is: %d\n", tid3->current_priority); 71 | rt_kprintf("test OK.\n"); 72 | } 73 | #endif 74 | } 75 | 76 | /* thread #2 entry function */ 77 | static void thread2_entry(void *parameter) 78 | { 79 | rt_err_t result; 80 | 81 | #if (RTTHREAD_VERSION >= RT_VERSION_CHECK(5, 1, 0)) 82 | rt_kprintf("the priority of thread2 is: %d\n", RT_SCHED_PRIV(tid2).current_priority); 83 | #else 84 | rt_kprintf("the priority of thread2 is: %d\n", tid2->current_priority); 85 | #endif 86 | /* let the lower priority thread run first */ 87 | rt_thread_mdelay(50); 88 | 89 | /* 90 | * pending the mutex 91 | * At this time, kernel raises the priority of thread #3 to same priority 92 | * as thread #2 93 | */ 94 | result = rt_mutex_take(mutex, RT_WAITING_FOREVER); 95 | 96 | if (result == RT_EOK) 97 | { 98 | /* release the mutex */ 99 | rt_mutex_release(mutex); 100 | } 101 | } 102 | 103 | /* thread #3 entry function */ 104 | static void thread3_entry(void *parameter) 105 | { 106 | rt_tick_t tick; 107 | rt_err_t result; 108 | 109 | #if (RTTHREAD_VERSION >= RT_VERSION_CHECK(5, 1, 0)) 110 | rt_kprintf("the priority of thread3 is: %d\n", RT_SCHED_PRIV(tid3).current_priority); 111 | #else 112 | rt_kprintf("the priority of thread3 is: %d\n", tid3->current_priority); 113 | #endif 114 | 115 | result = rt_mutex_take(mutex, RT_WAITING_FOREVER); 116 | if (result != RT_EOK) 117 | { 118 | rt_kprintf("thread3 take a mutex, failed.\n"); 119 | } 120 | 121 | /* delay for 500ms without scheduling */ 122 | tick = rt_tick_get(); 123 | while (rt_tick_get() - tick < (RT_TICK_PER_SECOND / 2)) ; 124 | 125 | /* release the mutex */ 126 | rt_mutex_release(mutex); 127 | } 128 | 129 | int pri_inversion(void) 130 | { 131 | /* create mutex */ 132 | mutex = rt_mutex_create("mutex", RT_IPC_FLAG_PRIO); 133 | if (mutex == RT_NULL) 134 | { 135 | rt_kprintf("create dynamic mutex failed.\n"); 136 | return -1; 137 | } 138 | 139 | /* create thread #1 */ 140 | tid1 = rt_thread_create("thread1", 141 | thread1_entry, 142 | RT_NULL, 143 | THREAD_STACK_SIZE, 144 | THREAD_PRIORITY - 1, THREAD_TIMESLICE); 145 | #ifdef RT_USING_SMP 146 | /* Bind threads to the same core to avoid messy log output when multiple cores are enabled */ 147 | rt_thread_control(tid1, RT_THREAD_CTRL_BIND_CPU, (void*)0); 148 | #endif 149 | if (tid1 != RT_NULL) 150 | rt_thread_startup(tid1); 151 | 152 | /* create thread #2 */ 153 | tid2 = rt_thread_create("thread2", 154 | thread2_entry, 155 | RT_NULL, 156 | THREAD_STACK_SIZE, 157 | THREAD_PRIORITY, THREAD_TIMESLICE); 158 | #ifdef RT_USING_SMP 159 | /* Bind threads to the same core to avoid messy log output when multiple cores are enabled */ 160 | rt_thread_control(tid2, RT_THREAD_CTRL_BIND_CPU, (void*)0); 161 | #endif 162 | if (tid2 != RT_NULL) 163 | rt_thread_startup(tid2); 164 | 165 | /* create thread #3 */ 166 | tid3 = rt_thread_create("thread3", 167 | thread3_entry, 168 | RT_NULL, 169 | THREAD_STACK_SIZE, 170 | THREAD_PRIORITY + 1, THREAD_TIMESLICE); 171 | #ifdef RT_USING_SMP 172 | /* Bind threads to the same core to avoid messy log output when multiple cores are enabled */ 173 | rt_thread_control(tid3, RT_THREAD_CTRL_BIND_CPU, (void*)0); 174 | #endif 175 | if (tid3 != RT_NULL) 176 | rt_thread_startup(tid3); 177 | 178 | return 0; 179 | } 180 | 181 | /* export the msh command */ 182 | MSH_CMD_EXPORT(pri_inversion, pri_inversion sample); 183 | -------------------------------------------------------------------------------- /en/producer_consumer.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006-2022, RT-Thread Development Team 3 | * 4 | * SPDX-License-Identifier: Apache-2.0 5 | * 6 | * Change Logs: 7 | * Date Author Notes 8 | * 2018-08-24 yangjie the first version 9 | * 2020-10-17 Meco Man translate to English comment 10 | */ 11 | 12 | /* 13 | * Demo: producer-consumer problem (or bounded-buffer problem) 14 | * 15 | * this demo creates two threads to demonstrate producer-consumer problem: 16 | * 1) producer thread: adds 1 to the variable "cnt" and stores it into the array. 17 | * 2) consumer thread: prints out the value and adds it up 18 | * 19 | * read more: 20 | * https://www.rt-thread.io/document/site/programming-manual/ipc1/ipc1/#semaphores 21 | */ 22 | 23 | #include 24 | 25 | #define THREAD_PRIORITY 6 26 | #define THREAD_STACK_SIZE 512 27 | #define THREAD_TIMESLICE 5 28 | 29 | /* define the maximum 5 elements can be produced */ 30 | #define MAXSEM 5 31 | 32 | rt_uint32_t array[MAXSEM]; 33 | 34 | /* the pointers of producer and consumer's position in the array */ 35 | static rt_uint32_t set, get; 36 | 37 | /* thread handler */ 38 | static rt_thread_t producer_tid = RT_NULL; 39 | static rt_thread_t consumer_tid = RT_NULL; 40 | 41 | struct rt_semaphore sem_lock; 42 | struct rt_semaphore sem_empty, sem_full; 43 | 44 | /* producer thread entry function */ 45 | void producer_thread_entry(void *parameter) 46 | { 47 | int cnt = 0; 48 | 49 | while (cnt < 10) 50 | { 51 | /* get a "empty" mark */ 52 | rt_sem_take(&sem_empty, RT_WAITING_FOREVER); 53 | 54 | /* protect the critial section */ 55 | rt_sem_take(&sem_lock, RT_WAITING_FOREVER); 56 | array[set % MAXSEM] = cnt + 1; 57 | rt_kprintf("the producer generates a number: %d\n", array[set % MAXSEM]); 58 | set++; 59 | rt_sem_release(&sem_lock); 60 | 61 | /* release a "full" mark */ 62 | rt_sem_release(&sem_full); 63 | cnt++; 64 | 65 | rt_thread_mdelay(20); 66 | } 67 | 68 | rt_kprintf("the producer exit!\n"); 69 | } 70 | 71 | /* consumer thread entry function */ 72 | void consumer_thread_entry(void *parameter) 73 | { 74 | rt_uint32_t sum = 0; 75 | 76 | while (1) 77 | { 78 | /* get a "full" mark */ 79 | rt_sem_take(&sem_full, RT_WAITING_FOREVER); 80 | 81 | /* protect the critial section */ 82 | rt_sem_take(&sem_lock, RT_WAITING_FOREVER); 83 | sum += array[get % MAXSEM]; 84 | rt_kprintf("the consumer[%d] get a number: %d\n", (get % MAXSEM), array[get % MAXSEM]); 85 | get++; 86 | rt_sem_release(&sem_lock); 87 | 88 | /* release a "empty" mark */ 89 | rt_sem_release(&sem_empty); 90 | 91 | if (get == 10) break; 92 | 93 | rt_thread_mdelay(50); 94 | } 95 | 96 | rt_kprintf("the consumer sum is: %d\n", sum); 97 | rt_kprintf("the consumer exit!\n"); 98 | } 99 | 100 | int producer_consumer(void) 101 | { 102 | set = 0; 103 | get = 0; 104 | 105 | rt_sem_init(&sem_lock, "lock", 1, RT_IPC_FLAG_PRIO); 106 | rt_sem_init(&sem_empty, "empty", MAXSEM, RT_IPC_FLAG_PRIO); 107 | rt_sem_init(&sem_full, "full", 0, RT_IPC_FLAG_PRIO); 108 | 109 | producer_tid = rt_thread_create("producer", 110 | producer_thread_entry, RT_NULL, 111 | THREAD_STACK_SIZE, 112 | THREAD_PRIORITY - 1, THREAD_TIMESLICE); 113 | #ifdef RT_USING_SMP 114 | /* Bind threads to the same core to avoid messy log output when multiple cores are enabled */ 115 | rt_thread_control(producer_tid, RT_THREAD_CTRL_BIND_CPU, (void*)0); 116 | #endif 117 | if (producer_tid != RT_NULL) 118 | rt_thread_startup(producer_tid); 119 | 120 | 121 | consumer_tid = rt_thread_create("consumer", 122 | consumer_thread_entry, RT_NULL, 123 | THREAD_STACK_SIZE, 124 | THREAD_PRIORITY + 1, THREAD_TIMESLICE); 125 | #ifdef RT_USING_SMP 126 | /* Bind threads to the same core to avoid messy log output when multiple cores are enabled */ 127 | rt_thread_control(consumer_tid, RT_THREAD_CTRL_BIND_CPU, (void*)0); 128 | #endif 129 | if (consumer_tid != RT_NULL) 130 | rt_thread_startup(consumer_tid); 131 | 132 | return 0; 133 | } 134 | 135 | /* export the msh command */ 136 | MSH_CMD_EXPORT(producer_consumer, producer_consumer sample); 137 | -------------------------------------------------------------------------------- /en/scheduler_hook.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006-2022, RT-Thread Development Team 3 | * 4 | * SPDX-License-Identifier: Apache-2.0 5 | * 6 | * Change Logs: 7 | * Date Author Notes 8 | * 2018-08-24 yangjie the first version 9 | * 2020-10-17 Meco Man translate to English comment 10 | */ 11 | 12 | /* 13 | * Demo: scheduler hook 14 | * 15 | * This demo sets a scheduler hook function and prints context-switching information. 16 | * 17 | * read more: 18 | * https://www.rt-thread.io/document/site/programming-manual/thread/thread/#set-the-scheduler-hook 19 | */ 20 | 21 | #include 22 | 23 | #define THREAD_STACK_SIZE 1024 24 | #define THREAD_PRIORITY 20 25 | #define THREAD_TIMESLICE 10 26 | 27 | /* counter for each thread */ 28 | volatile rt_uint32_t count[2]; 29 | 30 | /* thread entry function */ 31 | /* threads #1 and #2 share one entry, but the entry parameter is different */ 32 | static void thread_entry(void *parameter) 33 | { 34 | rt_uint32_t value; 35 | 36 | value = (rt_uint32_t)parameter; 37 | while (1) 38 | { 39 | rt_kprintf("thread %d is running\n", value); 40 | rt_thread_mdelay(1000); 41 | } 42 | } 43 | 44 | /* thread(s) handler */ 45 | static rt_thread_t tid1 = RT_NULL; 46 | static rt_thread_t tid2 = RT_NULL; 47 | 48 | static void hook_of_scheduler(struct rt_thread *from, struct rt_thread *to) 49 | { 50 | #if RT_VER_NUM >= 0x50001 51 | rt_kprintf("from: %s --> to: %s \n", from->parent.name, to->parent.name); 52 | #else 53 | rt_kprintf("from: %s --> to: %s \n", from->name, to->name); 54 | #endif 55 | } 56 | 57 | int scheduler_hook(void) 58 | { 59 | /* set a scheduler hook function */ 60 | rt_scheduler_sethook(hook_of_scheduler); 61 | 62 | tid1 = rt_thread_create("thread1", 63 | thread_entry, (void *)1, 64 | THREAD_STACK_SIZE, 65 | THREAD_PRIORITY, THREAD_TIMESLICE); 66 | #ifdef RT_USING_SMP 67 | /* Bind threads to the same core to avoid messy log output when multiple cores are enabled */ 68 | rt_thread_control(tid1, RT_THREAD_CTRL_BIND_CPU, (void*)0); 69 | #endif 70 | if (tid1 != RT_NULL) 71 | rt_thread_startup(tid1); 72 | 73 | tid2 = rt_thread_create("thread2", 74 | thread_entry, (void *)2, 75 | THREAD_STACK_SIZE, 76 | THREAD_PRIORITY, THREAD_TIMESLICE - 5); 77 | #ifdef RT_USING_SMP 78 | /* Bind threads to the same core to avoid messy log output when multiple cores are enabled */ 79 | rt_thread_control(tid2, RT_THREAD_CTRL_BIND_CPU, (void*)0); 80 | #endif 81 | if (tid2 != RT_NULL) 82 | rt_thread_startup(tid2); 83 | return 0; 84 | } 85 | 86 | /* export the msh command */ 87 | MSH_CMD_EXPORT(scheduler_hook, scheduler_hook sample); 88 | -------------------------------------------------------------------------------- /en/semaphore_sample.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006-2022, RT-Thread Development Team 3 | * 4 | * SPDX-License-Identifier: Apache-2.0 5 | * 6 | * Change Logs: 7 | * Date Author Notes 8 | * 2018-08-24 yangjie the first version 9 | * 2020-10-17 Meco Man translate to English comment 10 | */ 11 | 12 | /* 13 | * Demo: semaphore 14 | * This demo creates one semaphore and two threads: 15 | * 1) thread #1: release the semaphore 16 | * 2) thread #2: receive the semaphore 17 | * 18 | * read more: 19 | * https://www.rt-thread.io/document/site/programming-manual/ipc1/ipc1/#semaphores 20 | */ 21 | 22 | #include 23 | 24 | #define THREAD_PRIORITY 25 25 | #define THREAD_TIMESLICE 5 26 | 27 | /* semaphore handler */ 28 | static rt_sem_t dynamic_sem = RT_NULL; 29 | 30 | #ifdef rt_align 31 | rt_align(RT_ALIGN_SIZE) 32 | #else 33 | ALIGN(RT_ALIGN_SIZE) 34 | #endif 35 | static char thread1_stack[1024]; 36 | static struct rt_thread thread1; 37 | static void rt_thread1_entry(void *parameter) 38 | { 39 | static rt_uint8_t count = 0; 40 | 41 | while (1) 42 | { 43 | if (count <= 100) 44 | { 45 | count++; 46 | } 47 | else 48 | return; 49 | 50 | /* count release semaphore every 10 counts */ 51 | if (0 == (count % 10)) 52 | { 53 | rt_kprintf("thread1 release a dynamic semaphore.\n"); 54 | rt_sem_release(dynamic_sem); 55 | } 56 | } 57 | } 58 | 59 | #ifdef rt_align 60 | rt_align(RT_ALIGN_SIZE) 61 | #else 62 | ALIGN(RT_ALIGN_SIZE) 63 | #endif 64 | static char thread2_stack[1024]; 65 | static struct rt_thread thread2; 66 | static void rt_thread2_entry(void *parameter) 67 | { 68 | static rt_err_t result; 69 | static rt_uint8_t number = 0; 70 | while (1) 71 | { 72 | /* permanently wait for the semaphore; once obtain the semaphore, perform the number self-add operation */ 73 | result = rt_sem_take(dynamic_sem, RT_WAITING_FOREVER); 74 | if (result != RT_EOK) 75 | { 76 | rt_kprintf("thread2 take a dynamic semaphore, failed.\n"); 77 | rt_sem_delete(dynamic_sem); 78 | return; 79 | } 80 | else 81 | { 82 | number++; 83 | rt_kprintf("thread2 take a dynamic semaphore. number = %d\n", number); 84 | } 85 | } 86 | } 87 | 88 | int semaphore_sample() 89 | { 90 | /* create semaphtore and its initial value is 0 */ 91 | dynamic_sem = rt_sem_create("dsem", 0, RT_IPC_FLAG_PRIO); 92 | if (dynamic_sem == RT_NULL) 93 | { 94 | rt_kprintf("create dynamic semaphore failed.\n"); 95 | return -1; 96 | } 97 | else 98 | { 99 | rt_kprintf("create done. dynamic semaphore value = 0.\n"); 100 | } 101 | 102 | rt_thread_init(&thread1, 103 | "thread1", 104 | rt_thread1_entry, 105 | RT_NULL, 106 | &thread1_stack[0], 107 | sizeof(thread1_stack), 108 | THREAD_PRIORITY, THREAD_TIMESLICE); 109 | #ifdef RT_USING_SMP 110 | /* Bind threads to the same core to avoid messy log output when multiple cores are enabled */ 111 | rt_thread_control(&thread1, RT_THREAD_CTRL_BIND_CPU, (void*)0); 112 | #endif 113 | rt_thread_startup(&thread1); 114 | 115 | rt_thread_init(&thread2, 116 | "thread2", 117 | rt_thread2_entry, 118 | RT_NULL, 119 | &thread2_stack[0], 120 | sizeof(thread2_stack), 121 | THREAD_PRIORITY - 1, THREAD_TIMESLICE); 122 | #ifdef RT_USING_SMP 123 | /* Bind threads to the same core to avoid messy log output when multiple cores are enabled */ 124 | rt_thread_control(&thread2, RT_THREAD_CTRL_BIND_CPU, (void*)0); 125 | #endif 126 | rt_thread_startup(&thread2); 127 | 128 | return 0; 129 | } 130 | 131 | /* export the msh command */ 132 | MSH_CMD_EXPORT(semaphore_sample, semaphore sample); 133 | -------------------------------------------------------------------------------- /en/signal_sample.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006-2022, RT-Thread Development Team 3 | * 4 | * SPDX-License-Identifier: Apache-2.0 5 | * 6 | * Change Logs: 7 | * Date Author Notes 8 | * 2018-08-24 yangjie the first version 9 | * 2020-10-17 Meco Man translate to English comment 10 | */ 11 | 12 | /* 13 | * Demo: signal 14 | * 15 | * This demo creates one thread. When the signal is installed, the signal 16 | * processing mode is set to custom processing. The processing function of 17 | * the signal is defined to be thread1_signal_handler(). After the thread is 18 | * running and the signal is installed, a signal is sent to this thread. 19 | * This thread will receive the signal and print the message. 20 | * 21 | * read more: 22 | * https://www.rt-thread.io/document/site/programming-manual/ipc2/ipc2/#signal 23 | */ 24 | 25 | #include 26 | 27 | #define THREAD_PRIORITY 25 28 | #define THREAD_STACK_SIZE 512 29 | #define THREAD_TIMESLICE 5 30 | 31 | static rt_thread_t tid1 = RT_NULL; 32 | 33 | /* callback function of thread #1's signal */ 34 | void thread1_signal_handler(int sig) 35 | { 36 | rt_kprintf("thread1 received signal %d\n", sig); 37 | } 38 | 39 | /* thread #1 entry function */ 40 | static void thread1_entry(void *parameter) 41 | { 42 | int cnt = 0; 43 | 44 | /* install signal*/ 45 | rt_signal_install(SIGUSR1, thread1_signal_handler); 46 | rt_signal_unmask(SIGUSR1); 47 | 48 | while (cnt < 10) 49 | { 50 | rt_kprintf("thread1 count : %d\n", cnt); 51 | cnt++; 52 | rt_thread_mdelay(100); 53 | } 54 | } 55 | 56 | int signal_sample(void) 57 | { 58 | tid1 = rt_thread_create("thread1", 59 | thread1_entry, RT_NULL, 60 | THREAD_STACK_SIZE, 61 | THREAD_PRIORITY, THREAD_TIMESLICE); 62 | 63 | if (tid1 != RT_NULL) 64 | rt_thread_startup(tid1); 65 | 66 | rt_thread_mdelay(300); 67 | 68 | /* send the signal "SIGUSR1" to thread #1 */ 69 | rt_thread_kill(tid1, SIGUSR1); 70 | 71 | return 0; 72 | } 73 | 74 | /* export the msh command */ 75 | MSH_CMD_EXPORT(signal_sample, signal sample); 76 | -------------------------------------------------------------------------------- /en/thread_sample.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006-2022, RT-Thread Development Team 3 | * 4 | * SPDX-License-Identifier: Apache-2.0 5 | * 6 | * Change Logs: 7 | * Date Author Notes 8 | * 2018-08-24 yangjie the first version 9 | * 2020-10-17 Meco Man translate to English comment 10 | */ 11 | 12 | /* 13 | * Demo: thread(s) 14 | * 15 | * This demo creates two threads: 16 | * 1) create thread #1 dynamically, and delete automatically when the thread #1 finished; 17 | * 2) create thread #2 statically, and print counting numbers continuously. 18 | * 19 | * read more: 20 | * https://www.rt-thread.io/document/site/programming-manual/thread/thread/ 21 | */ 22 | 23 | #include 24 | 25 | #define THREAD_PRIORITY 25 26 | #define THREAD_STACK_SIZE 512 27 | #define THREAD_TIMESLICE 5 28 | 29 | /* thread handler */ 30 | static rt_thread_t tid1 = RT_NULL; 31 | 32 | /* thread #1 entry function */ 33 | static void thread1_entry(void *parameter) 34 | { 35 | rt_uint32_t count = 0; 36 | 37 | while (1) 38 | { 39 | /* thread #1 occupies low priority and prints counting numbers continuously */ 40 | rt_kprintf("thread1 count: %d\n", count ++); 41 | rt_thread_mdelay(500); 42 | } 43 | } 44 | 45 | #ifdef rt_align 46 | rt_align(RT_ALIGN_SIZE) 47 | #else 48 | ALIGN(RT_ALIGN_SIZE) 49 | #endif 50 | static char thread2_stack[1024]; 51 | static struct rt_thread thread2; 52 | 53 | /* thread #2 entry function */ 54 | static void thread2_entry(void *param) 55 | { 56 | rt_uint32_t count = 0; 57 | 58 | /* thread #2 occupies higher priority than that of thread #1 */ 59 | for (count = 0; count < 10 ; count++) 60 | { 61 | /* thread #2 prints counting numbers */ 62 | rt_kprintf("thread2 count: %d\n", count); 63 | } 64 | rt_kprintf("thread2 exit\n"); 65 | 66 | /* RT-Thread allows thread entry function to return directly */ 67 | /* thread #2 will be deleted automatically by idle thread once it finishes */ 68 | } 69 | 70 | int thread_sample(void) 71 | { 72 | /* create thread #1 dynamically */ 73 | tid1 = rt_thread_create("thread1", 74 | thread1_entry, RT_NULL, 75 | THREAD_STACK_SIZE, 76 | THREAD_PRIORITY, THREAD_TIMESLICE); 77 | #ifdef RT_USING_SMP 78 | /* Bind threads to the same core to avoid messy log output when multiple cores are enabled */ 79 | rt_thread_control(tid1, RT_THREAD_CTRL_BIND_CPU, (void*)0); 80 | #endif 81 | /* start thread #1 */ 82 | if (tid1 != RT_NULL) 83 | rt_thread_startup(tid1); 84 | 85 | /* create thread #2 statically */ 86 | rt_thread_init(&thread2, 87 | "thread2", 88 | thread2_entry, 89 | RT_NULL, 90 | &thread2_stack[0], 91 | sizeof(thread2_stack), 92 | THREAD_PRIORITY - 1, THREAD_TIMESLICE); 93 | #ifdef RT_USING_SMP 94 | /* Bind threads to the same core to avoid messy log output when multiple cores are enabled */ 95 | rt_thread_control(&thread2, RT_THREAD_CTRL_BIND_CPU, (void*)0); 96 | #endif 97 | rt_thread_startup(&thread2); /* start thread #2 */ 98 | 99 | return 0; 100 | } 101 | 102 | /* export the msh command */ 103 | MSH_CMD_EXPORT(thread_sample, thread sample); 104 | -------------------------------------------------------------------------------- /en/timer_sample.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006-2022, RT-Thread Development Team 3 | * 4 | * SPDX-License-Identifier: Apache-2.0 5 | * 6 | * Change Logs: 7 | * Date Author Notes 8 | * 2018-08-24 yangjie the first version 9 | * 2020-10-17 Meco Man translate to English comment 10 | */ 11 | 12 | /* 13 | * Demo: timer(s) 14 | * 15 | * This demo creates two dynamic timers: 16 | * 1) timer #1: one-shot mode timer 17 | * 2) timer #2: periodic mode timer 18 | * 19 | * read more: 20 | * https://www.rt-thread.io/document/site/programming-manual/timer/timer/#timer-management 21 | */ 22 | 23 | #include 24 | 25 | /* timer handle */ 26 | static rt_timer_t timer1; 27 | static rt_timer_t timer2; 28 | static int cnt = 0; 29 | 30 | /* timer #1 timeout callback function */ 31 | static void timeout1(void *parameter) 32 | { 33 | rt_kprintf("periodic timer is timeout %d\n", cnt); 34 | 35 | if (cnt++ >= 9) 36 | { 37 | rt_timer_stop(timer1); 38 | rt_kprintf("periodic timer was stopped! \n"); 39 | } 40 | } 41 | 42 | /* timer #2 timeout callback function */ 43 | static void timeout2(void *parameter) 44 | { 45 | rt_kprintf("one shot timer is timeout\n"); 46 | } 47 | 48 | int timer_sample(void) 49 | { 50 | /* create timer #1, periodic mode */ 51 | timer1 = rt_timer_create("timer1", timeout1, 52 | RT_NULL, 10, 53 | RT_TIMER_FLAG_PERIODIC); 54 | 55 | /* start timer #1 */ 56 | if (timer1 != RT_NULL) 57 | rt_timer_start(timer1); 58 | 59 | /* create timer #2, one-shot mode */ 60 | timer2 = rt_timer_create("timer2", timeout2, 61 | RT_NULL, 30, 62 | RT_TIMER_FLAG_ONE_SHOT); 63 | 64 | /* start timer #2 */ 65 | if (timer2 != RT_NULL) 66 | rt_timer_start(timer2); 67 | 68 | return 0; 69 | } 70 | 71 | /* export the msh command */ 72 | MSH_CMD_EXPORT(timer_sample, timer sample); 73 | -------------------------------------------------------------------------------- /en/timeslice_sample.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006-2022, RT-Thread Development Team 3 | * 4 | * SPDX-License-Identifier: Apache-2.0 5 | * 6 | * Change Logs: 7 | * Date Author Notes 8 | * 2018-08-24 yangjie the first version 9 | * 2020-10-17 Meco Man translate to English comment 10 | */ 11 | 12 | /* 13 | * Demo: Time-Slicing (or Round-Robin Scheduling) 14 | * 15 | * This demo creates two threads to show how Time-Slicing works. 16 | * 17 | * read more: 18 | * https://www.rt-thread.io/document/site/programming-manual/thread/thread/#time-slice 19 | */ 20 | 21 | #include 22 | 23 | #define THREAD_STACK_SIZE 1024 24 | #define THREAD_PRIORITY 20 25 | #define THREAD_TIMESLICE 10 26 | 27 | /* thread entry function */ 28 | /* threads #1 and #2 share one entry, but the entry parameter is different */ 29 | static void thread_entry(void *parameter) 30 | { 31 | rt_uint32_t value; 32 | rt_uint32_t count = 0; 33 | 34 | value = (rt_uint32_t)parameter; 35 | while (1) 36 | { 37 | if (0 == (count % 5)) 38 | { 39 | rt_kprintf("thread %d is running ,thread %d count = %d\n", value, value, count); 40 | 41 | if (count > 200) 42 | return; 43 | } 44 | count++; 45 | } 46 | } 47 | 48 | int timeslice_sample(void) 49 | { 50 | rt_thread_t tid = RT_NULL; /* thread handle */ 51 | 52 | /* create thread #1 */ 53 | tid = rt_thread_create("thread1", 54 | thread_entry, (void *)1, 55 | THREAD_STACK_SIZE, 56 | THREAD_PRIORITY, THREAD_TIMESLICE); 57 | #ifdef RT_USING_SMP 58 | /* Bind threads to the same core to avoid messy log output when multiple cores are enabled */ 59 | rt_thread_control(tid, RT_THREAD_CTRL_BIND_CPU, (void*)0); 60 | #endif 61 | if (tid != RT_NULL) 62 | rt_thread_startup(tid); /* start thread #1 */ 63 | 64 | /* create thread #2 */ 65 | tid = rt_thread_create("thread2", 66 | thread_entry, (void *)2, 67 | THREAD_STACK_SIZE, 68 | THREAD_PRIORITY, THREAD_TIMESLICE - 5); 69 | #ifdef RT_USING_SMP 70 | /* Bind threads to the same core to avoid messy log output when multiple cores are enabled */ 71 | rt_thread_control(tid, RT_THREAD_CTRL_BIND_CPU, (void*)0); 72 | #endif 73 | if (tid != RT_NULL) 74 | rt_thread_startup(tid); /* start thread #2 */ 75 | 76 | return 0; 77 | } 78 | 79 | /* export the msh command */ 80 | MSH_CMD_EXPORT(timeslice_sample, timeslice sample); 81 | -------------------------------------------------------------------------------- /zh/SConscript: -------------------------------------------------------------------------------- 1 | from building import * 2 | 3 | src = [] 4 | cwd = GetCurrentDir() 5 | include_path = [cwd] 6 | 7 | # add kernel samples. 8 | if GetDepend('KERNEL_SAMPLES_USING_THREAD'): 9 | src += ['thread_sample.c'] 10 | 11 | if GetDepend('KERNEL_SAMPLES_USING_SEMAPHORE'): 12 | src += ['semaphore_sample.c'] 13 | 14 | if GetDepend('KERNEL_SAMPLES_USING_MUTEX'): 15 | src += ['mutex_sample.c'] 16 | 17 | if GetDepend('KERNEL_SAMPLES_USING_MAILBOX'): 18 | src += ['mailbox_sample.c'] 19 | 20 | if GetDepend('KERNEL_SAMPLES_USING_EVENT'): 21 | src += ['event_sample.c'] 22 | 23 | if GetDepend('KERNEL_SAMPLES_USING_MESSAGEQUEUE'): 24 | src += ['msgq_sample.c'] 25 | 26 | if GetDepend('KERNEL_SAMPLES_USING_TIMER'): 27 | src += ['timer_sample.c'] 28 | 29 | if GetDepend('KERNEL_SAMPLES_USING_HEAP'): 30 | src += ['dynmem_sample.c'] 31 | 32 | if GetDepend('KERNEL_SAMPLES_USING_MEMPOOL'): 33 | src += ['memp_sample.c'] 34 | 35 | if GetDepend('KERNEL_SAMPLES_USING_IDLEHOOK'): 36 | src += ['idlehook_sample.c'] 37 | 38 | if GetDepend('KERNEL_SAMPLES_USING_SIGNAL'): 39 | src += ['signal_sample.c'] 40 | 41 | if GetDepend('KERNEL_SAMPLES_USING_INTERRUPT'): 42 | src += ['interrupt_sample.c'] 43 | 44 | if GetDepend('KERNEL_SAMPLES_USING_PRI_INVERSION'): 45 | src += ['priority_inversion.c'] 46 | 47 | if GetDepend('KERNEL_SAMPLES_USING_TIME_SLICE'): 48 | src += ['timeslice_sample.c'] 49 | 50 | if GetDepend('KERNEL_SAMPLES_USING_SCHEDULER_HOOK'): 51 | src += ['scheduler_hook.c'] 52 | 53 | if GetDepend('KERNEL_SAMPLES_USING_PRODUCER_CONSUMER'): 54 | src += ['producer_consumer.c'] 55 | 56 | group = DefineGroup('kernel-samples', src, depend = ['PKG_USING_KERNEL_SAMPLES_ZH'], CPPPATH = include_path) 57 | 58 | Return('group') 59 | -------------------------------------------------------------------------------- /zh/dynmem_sample.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006-2022, RT-Thread Development Team 3 | * 4 | * SPDX-License-Identifier: Apache-2.0 5 | * 6 | * Change Logs: 7 | * Date Author Notes 8 | * 2018-08-24 yangjie the first version 9 | */ 10 | 11 | /* 12 | * 程序清单:动态内存管理例程 13 | * 14 | * 这个程序会创建一个动态的线程,这个线程会动态申请内存并释放 15 | * 每次申请更大的内存,当申请不到的时候就结束 16 | */ 17 | #include 18 | 19 | #define THREAD_PRIORITY 25 20 | #define THREAD_STACK_SIZE 512 21 | #define THREAD_TIMESLICE 5 22 | 23 | /* 线程入口 */ 24 | void thread1_entry(void *parameter) 25 | { 26 | int i; 27 | char *ptr = RT_NULL; /* 内存块的指针 */ 28 | 29 | for (i = 0; ; i++) 30 | { 31 | /* 每次分配 (1 << i) 大小字节数的内存空间 */ 32 | ptr = rt_malloc(1 << i); 33 | 34 | /* 如果分配成功 */ 35 | if (ptr != RT_NULL) 36 | { 37 | rt_kprintf("get memory :%d byte\n", (1 << i)); 38 | /* 释放内存块 */ 39 | rt_free(ptr); 40 | rt_kprintf("free memory :%d byte\n", (1 << i)); 41 | ptr = RT_NULL; 42 | } 43 | else 44 | { 45 | rt_kprintf("try to get %d byte memory failed!\n", (1 << i)); 46 | return; 47 | } 48 | } 49 | } 50 | 51 | int dynmem_sample(void) 52 | { 53 | rt_thread_t tid = RT_NULL; 54 | 55 | /* 创建线程1 */ 56 | tid = rt_thread_create("thread1", 57 | thread1_entry, RT_NULL, 58 | THREAD_STACK_SIZE, 59 | THREAD_PRIORITY, 60 | THREAD_TIMESLICE); 61 | if (tid != RT_NULL) 62 | rt_thread_startup(tid); 63 | 64 | return 0; 65 | } 66 | 67 | /* 导出到 msh 命令列表中 */ 68 | MSH_CMD_EXPORT(dynmem_sample, dynmem sample); 69 | -------------------------------------------------------------------------------- /zh/event_sample.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006-2022, RT-Thread Development Team 3 | * 4 | * SPDX-License-Identifier: Apache-2.0 5 | * 6 | * Change Logs: 7 | * Date Author Notes 8 | * 2018-08-24 yangjie the first version 9 | */ 10 | 11 | /* 12 | * 程序清单:事件例程 13 | * 14 | * 程序会初始化2个线程及初始化一个静态事件对象 15 | * 一个线程等待于事件对象上,以接收事件; 16 | * 一个线程发送事件 (事件3/事件5) 17 | */ 18 | #include 19 | 20 | #define THREAD_PRIORITY 9 21 | #define THREAD_TIMESLICE 5 22 | 23 | #define EVENT_FLAG3 (1 << 3) 24 | #define EVENT_FLAG5 (1 << 5) 25 | 26 | /* 事件控制块 */ 27 | static struct rt_event event; 28 | 29 | #ifdef rt_align 30 | rt_align(RT_ALIGN_SIZE) 31 | #else 32 | ALIGN(RT_ALIGN_SIZE) 33 | #endif 34 | static char thread1_stack[1024]; 35 | static struct rt_thread thread1; 36 | 37 | /* 线程1入口函数 */ 38 | static void thread1_recv_event(void *param) 39 | { 40 | rt_uint32_t e; 41 | 42 | /* 第一次接收事件,事件3或事件5任意一个可以触发线程1,接收完后清除事件标志 */ 43 | if (rt_event_recv(&event, (EVENT_FLAG3 | EVENT_FLAG5), 44 | RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR, 45 | RT_WAITING_FOREVER, &e) == RT_EOK) 46 | { 47 | rt_kprintf("thread1: OR recv event 0x%x\n", e); 48 | } 49 | 50 | rt_kprintf("thread1: delay 1s to prepare the second event\n"); 51 | rt_thread_mdelay(1000); 52 | 53 | /* 第二次接收事件,事件3和事件5均发生时才可以触发线程1,接收完后清除事件标志 */ 54 | if (rt_event_recv(&event, (EVENT_FLAG3 | EVENT_FLAG5), 55 | RT_EVENT_FLAG_AND | RT_EVENT_FLAG_CLEAR, 56 | RT_WAITING_FOREVER, &e) == RT_EOK) 57 | { 58 | rt_kprintf("thread1: AND recv event 0x%x\n", e); 59 | } 60 | rt_kprintf("thread1 leave.\n"); 61 | } 62 | 63 | #ifdef rt_align 64 | rt_align(RT_ALIGN_SIZE) 65 | #else 66 | ALIGN(RT_ALIGN_SIZE) 67 | #endif 68 | static char thread2_stack[1024]; 69 | static struct rt_thread thread2; 70 | 71 | /* 线程2入口 */ 72 | static void thread2_send_event(void *param) 73 | { 74 | rt_kprintf("thread2: send event3\n"); 75 | rt_event_send(&event, EVENT_FLAG3); 76 | rt_thread_mdelay(200); 77 | 78 | rt_kprintf("thread2: send event5\n"); 79 | rt_event_send(&event, EVENT_FLAG5); 80 | rt_thread_mdelay(200); 81 | 82 | rt_kprintf("thread2: send event3\n"); 83 | rt_event_send(&event, EVENT_FLAG3); 84 | rt_kprintf("thread2 leave.\n"); 85 | } 86 | 87 | int event_sample(void) 88 | { 89 | rt_err_t result; 90 | 91 | /* 初始化事件对象 */ 92 | result = rt_event_init(&event, "event", RT_IPC_FLAG_PRIO); 93 | if (result != RT_EOK) 94 | { 95 | rt_kprintf("init event failed.\n"); 96 | return -1; 97 | } 98 | 99 | rt_thread_init(&thread1, 100 | "thread1", 101 | thread1_recv_event, 102 | RT_NULL, 103 | &thread1_stack[0], 104 | sizeof(thread1_stack), 105 | THREAD_PRIORITY - 1, THREAD_TIMESLICE); 106 | #ifdef RT_USING_SMP 107 | /* 绑定线程到同一个核上,避免启用多核时的输出混乱 */ 108 | rt_thread_control(&thread1, RT_THREAD_CTRL_BIND_CPU, (void*)0); 109 | #endif 110 | rt_thread_startup(&thread1); 111 | 112 | rt_thread_init(&thread2, 113 | "thread2", 114 | thread2_send_event, 115 | RT_NULL, 116 | &thread2_stack[0], 117 | sizeof(thread2_stack), 118 | THREAD_PRIORITY, THREAD_TIMESLICE); 119 | #ifdef RT_USING_SMP 120 | /* 绑定线程到同一个核上,避免启用多核时的输出混乱 */ 121 | rt_thread_control(&thread2, RT_THREAD_CTRL_BIND_CPU, (void*)0); 122 | #endif 123 | rt_thread_startup(&thread2); 124 | 125 | return 0; 126 | } 127 | 128 | /* 导出到 msh 命令列表中 */ 129 | MSH_CMD_EXPORT(event_sample, event sample); 130 | -------------------------------------------------------------------------------- /zh/idlehook_sample.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006-2022, RT-Thread Development Team 3 | * 4 | * SPDX-License-Identifier: Apache-2.0 5 | * 6 | * Change Logs: 7 | * Date Author Notes 8 | * 2018-08-24 yangjie the first version 9 | */ 10 | 11 | /* 12 | * 程序清单:空闲任务钩子例程 13 | * 14 | * 这个例程创建一个线程,通过延时进入空闲任务钩子,用于打印进入空闲钩子的次数 15 | */ 16 | 17 | #include 18 | #include 19 | 20 | #define THREAD_PRIORITY 20 21 | #define THREAD_STACK_SIZE 1024 22 | #define THREAD_TIMESLICE 5 23 | 24 | /* 指向线程控制块的指针 */ 25 | static rt_thread_t tid = RT_NULL; 26 | 27 | /* 空闲函数钩子函数执行次数 */ 28 | volatile static int hook_times = 0; 29 | 30 | /* 空闲任务钩子函数 */ 31 | static void idle_hook() 32 | { 33 | if (0 == (hook_times % 10000)) 34 | { 35 | rt_kprintf("enter idle hook %d times.\n", hook_times); 36 | } 37 | 38 | rt_enter_critical(); 39 | hook_times++; 40 | rt_exit_critical(); 41 | } 42 | 43 | /* 线程入口 */ 44 | static void thread_entry(void *parameter) 45 | { 46 | int i = 5; 47 | while (i--) 48 | { 49 | rt_kprintf("enter thread1.\n"); 50 | rt_enter_critical(); 51 | hook_times = 0; 52 | rt_exit_critical(); 53 | 54 | /* 休眠500ms */ 55 | rt_kprintf("thread1 delay 500ms.\n"); 56 | rt_thread_mdelay(500); 57 | } 58 | rt_kprintf("delete idle hook.\n"); 59 | 60 | /* 删除空闲钩子函数 */ 61 | rt_thread_idle_delhook(idle_hook); 62 | rt_kprintf("thread1 finish.\n"); 63 | } 64 | 65 | int idle_hook_sample(void) 66 | { 67 | /* 设置空闲线程钩子 */ 68 | rt_thread_idle_sethook(idle_hook); 69 | 70 | /* 创建线程 */ 71 | tid = rt_thread_create("thread1", 72 | thread_entry, RT_NULL, 73 | THREAD_STACK_SIZE, 74 | THREAD_PRIORITY, THREAD_TIMESLICE); 75 | if (tid != RT_NULL) 76 | rt_thread_startup(tid); 77 | 78 | return 0; 79 | } 80 | 81 | /* 导出到 msh 命令列表中 */ 82 | MSH_CMD_EXPORT(idle_hook_sample, idle hook sample); 83 | -------------------------------------------------------------------------------- /zh/interrupt_sample.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006-2022, RT-Thread Development Team 3 | * 4 | * SPDX-License-Identifier: Apache-2.0 5 | * 6 | * Change Logs: 7 | * Date Author Notes 8 | * 2018-08-24 yangjie the first version 9 | */ 10 | 11 | /* 程序清单:关闭中断进行全局变量的访问 */ 12 | #include 13 | #include 14 | 15 | #define THREAD_PRIORITY 20 16 | #define THREAD_STACK_SIZE 512 17 | #define THREAD_TIMESLICE 5 18 | 19 | /* 同时访问的全局变量 */ 20 | static rt_uint32_t cnt; 21 | static void thread_entry(void *parameter) 22 | { 23 | rt_uint32_t no; 24 | rt_uint32_t level; 25 | 26 | no = (rt_uint32_t) parameter; 27 | while (1) 28 | { 29 | /* 关闭中断 */ 30 | level = rt_hw_interrupt_disable(); 31 | cnt += no; 32 | /* 恢复中断 */ 33 | rt_hw_interrupt_enable(level); 34 | 35 | rt_kprintf("protect thread[%d]'s counter is %d\n", no, cnt); 36 | rt_thread_mdelay(no * 10); 37 | } 38 | } 39 | 40 | int interrupt_sample(void) 41 | { 42 | rt_thread_t thread; 43 | 44 | /* 创建thread1线程 */ 45 | thread = rt_thread_create("thread1", thread_entry, (void *)10, 46 | THREAD_STACK_SIZE, 47 | THREAD_PRIORITY, THREAD_TIMESLICE); 48 | #ifdef RT_USING_SMP 49 | /* 绑定线程到同一个核上,避免启用多核时的输出混乱 */ 50 | rt_thread_control(thread, RT_THREAD_CTRL_BIND_CPU, (void*)0); 51 | #endif 52 | if (thread != RT_NULL) 53 | rt_thread_startup(thread); 54 | 55 | /* 创建thread2线程 */ 56 | thread = rt_thread_create("thread2", thread_entry, (void *)20, 57 | THREAD_STACK_SIZE, 58 | THREAD_PRIORITY, THREAD_TIMESLICE); 59 | #ifdef RT_USING_SMP 60 | /* 绑定线程到同一个核上,避免启用多核时的输出混乱 */ 61 | rt_thread_control(thread, RT_THREAD_CTRL_BIND_CPU, (void*)0); 62 | #endif 63 | if (thread != RT_NULL) 64 | rt_thread_startup(thread); 65 | 66 | return 0; 67 | } 68 | 69 | /* 导出到 msh 命令列表中 */ 70 | MSH_CMD_EXPORT(interrupt_sample, interrupt sample); 71 | -------------------------------------------------------------------------------- /zh/mailbox_sample.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006-2022, RT-Thread Development Team 3 | * 4 | * SPDX-License-Identifier: Apache-2.0 5 | * 6 | * Change Logs: 7 | * Date Author Notes 8 | * 2018-08-24 yangjie the first version 9 | */ 10 | 11 | /* 12 | * 程序清单:邮箱例程 13 | * 14 | * 这个程序会创建2个动态线程,一个静态的邮箱对象,其中一个线程往邮箱中发送邮件, 15 | * 一个线程往邮箱中收取邮件。 16 | */ 17 | #include 18 | 19 | #define THREAD_PRIORITY 10 20 | #define THREAD_TIMESLICE 5 21 | 22 | /* 邮箱控制块 */ 23 | static struct rt_mailbox mb; 24 | /* 用于放邮件的内存池 */ 25 | static char mb_pool[128]; 26 | 27 | static char mb_str1[] = "I'm a mail!"; 28 | static char mb_str2[] = "this is another mail!"; 29 | static char mb_str3[] = "over"; 30 | 31 | #ifdef rt_align 32 | rt_align(RT_ALIGN_SIZE) 33 | #else 34 | ALIGN(RT_ALIGN_SIZE) 35 | #endif 36 | static char thread1_stack[1024]; 37 | static struct rt_thread thread1; 38 | 39 | /* 线程1入口 */ 40 | static void thread1_entry(void *parameter) 41 | { 42 | char *str; 43 | 44 | while (1) 45 | { 46 | rt_kprintf("thread1: try to recv a mail\n"); 47 | 48 | /* 从邮箱中收取邮件 */ 49 | if (rt_mb_recv(&mb, (rt_ubase_t *)&str, RT_WAITING_FOREVER) == RT_EOK) 50 | { 51 | rt_kprintf("thread1: get a mail from mailbox, the content:%s\n", str); 52 | if (str == mb_str3) 53 | break; 54 | 55 | /* 延时100ms */ 56 | rt_thread_mdelay(100); 57 | } 58 | } 59 | /* 执行邮箱对象脱离 */ 60 | rt_mb_detach(&mb); 61 | } 62 | 63 | #ifdef rt_align 64 | rt_align(RT_ALIGN_SIZE) 65 | #else 66 | ALIGN(RT_ALIGN_SIZE) 67 | #endif 68 | static char thread2_stack[1024]; 69 | static struct rt_thread thread2; 70 | 71 | /* 线程2入口 */ 72 | static void thread2_entry(void *parameter) 73 | { 74 | rt_uint8_t count; 75 | 76 | count = 0; 77 | while (count < 10) 78 | { 79 | count ++; 80 | if (count & 0x1) 81 | { 82 | /* 发送mb_str1地址到邮箱中 */ 83 | rt_mb_send(&mb, (rt_uint32_t)&mb_str1); 84 | } 85 | else 86 | { 87 | /* 发送mb_str2地址到邮箱中 */ 88 | rt_mb_send(&mb, (rt_uint32_t)&mb_str2); 89 | } 90 | 91 | /* 延时200ms */ 92 | rt_thread_mdelay(200); 93 | } 94 | 95 | /* 发送邮件告诉线程1,线程2已经运行结束 */ 96 | rt_mb_send(&mb, (rt_uint32_t)&mb_str3); 97 | } 98 | 99 | int mailbox_sample(void) 100 | { 101 | rt_err_t result; 102 | 103 | /* 初始化一个mailbox */ 104 | result = rt_mb_init(&mb, 105 | "mbt", /* 名称是mbt */ 106 | &mb_pool[0], /* 邮箱用到的内存池是mb_pool */ 107 | sizeof(mb_pool) / sizeof(rt_ubase_t), /* 邮箱中的邮件数目,sizeof(rt_ubase_t)表示指针大小 */ 108 | RT_IPC_FLAG_PRIO); /* 采用PRIO方式进行线程等待 */ 109 | if (result != RT_EOK) 110 | { 111 | rt_kprintf("init mailbox failed.\n"); 112 | return -1; 113 | } 114 | 115 | rt_thread_init(&thread1, 116 | "thread1", 117 | thread1_entry, 118 | RT_NULL, 119 | &thread1_stack[0], 120 | sizeof(thread1_stack), 121 | THREAD_PRIORITY, THREAD_TIMESLICE); 122 | #ifdef RT_USING_SMP 123 | /* 绑定线程到同一个核上,避免启用多核时的输出混乱 */ 124 | rt_thread_control(&thread1, RT_THREAD_CTRL_BIND_CPU, (void*)0); 125 | #endif 126 | rt_thread_startup(&thread1); 127 | 128 | rt_thread_init(&thread2, 129 | "thread2", 130 | thread2_entry, 131 | RT_NULL, 132 | &thread2_stack[0], 133 | sizeof(thread2_stack), 134 | THREAD_PRIORITY, THREAD_TIMESLICE); 135 | #ifdef RT_USING_SMP 136 | /* 绑定线程到同一个核上,避免启用多核时的输出混乱 */ 137 | rt_thread_control(&thread2, RT_THREAD_CTRL_BIND_CPU, (void*)0); 138 | #endif 139 | rt_thread_startup(&thread2); 140 | return 0; 141 | } 142 | 143 | /* 导出到 msh 命令列表中 */ 144 | MSH_CMD_EXPORT(mailbox_sample, mailbox sample); 145 | -------------------------------------------------------------------------------- /zh/mailbox_urgent_sample.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006-2022, RT-Thread Development Team 3 | * 4 | * SPDX-License-Identifier: Apache-2.0 5 | * 6 | * Change Logs: 7 | * Date Author Notes 8 | * 2018-08-24 yangjie the first version 9 | * 2021-01-03 Meco Man add the demo of rt_mb_urgent() 10 | */ 11 | 12 | /* 13 | * 程序清单:邮箱例程 14 | * 15 | * 这个程序会创建2个动态线程,一个静态的邮箱对象,其中一个线程往邮箱中发送邮件, 16 | * 一个线程往邮箱中收取邮件。 17 | * 本例程也会展示紧急邮件和正常邮件的区别。 18 | */ 19 | #include 20 | 21 | #define THREAD_PRIORITY 10 22 | #define THREAD_TIMESLICE 5 23 | 24 | /* 邮箱控制块 */ 25 | static struct rt_mailbox mb; 26 | /* 用于放邮件的内存池 */ 27 | static char mb_pool[128]; 28 | 29 | static char mb_urgent[] = "I'm a urgent mail!"; 30 | static char mb_normal[] = "I'm a normal mail!"; 31 | static char mb_str3[] = "over"; 32 | 33 | #ifdef rt_align 34 | rt_align(RT_ALIGN_SIZE) 35 | #else 36 | ALIGN(RT_ALIGN_SIZE) 37 | #endif 38 | static char thread1_stack[1024]; 39 | static struct rt_thread thread1; 40 | 41 | /* 线程1入口 */ 42 | static void thread1_entry(void *parameter) 43 | { 44 | char *str; 45 | 46 | while (1) 47 | { 48 | rt_kprintf("thread1: try to recv a mail\n"); 49 | 50 | /* 从邮箱中收取邮件 */ 51 | if (rt_mb_recv(&mb, (rt_ubase_t *)&str, RT_WAITING_FOREVER) == RT_EOK) 52 | { 53 | rt_kprintf("thread1: get a mail from mailbox, the content:%s\n", str); 54 | if (str == mb_str3) 55 | break; 56 | 57 | /* 延时100ms */ 58 | rt_thread_mdelay(100); 59 | } 60 | } 61 | /* 执行邮箱对象脱离 */ 62 | rt_mb_detach(&mb); 63 | } 64 | 65 | #ifdef rt_align 66 | rt_align(RT_ALIGN_SIZE) 67 | #else 68 | ALIGN(RT_ALIGN_SIZE) 69 | #endif 70 | static char thread2_stack[1024]; 71 | static struct rt_thread thread2; 72 | 73 | /* 线程2入口 */ 74 | static void thread2_entry(void *parameter) 75 | { 76 | rt_uint8_t count; 77 | 78 | count = 0; 79 | while (count < 10) 80 | { 81 | count ++; 82 | 83 | /* 分别发送正常邮件和紧急邮件到邮箱中 */ 84 | /* 关调度器是为了同时给线程1两封邮件,观察线程1如何处理紧急邮件和正常邮件 */ 85 | rt_enter_critical(); 86 | rt_mb_send(&mb, (rt_uint32_t)&mb_normal); 87 | rt_mb_urgent(&mb, (rt_uint32_t)&mb_urgent); 88 | rt_exit_critical(); 89 | /* 可以看到虽然正常邮件和紧急优先同时发送,但是邮箱选择优先处理紧急邮件 */ 90 | 91 | /* 延时200ms */ 92 | rt_thread_mdelay(200); 93 | } 94 | 95 | /* 发送邮件告诉线程1,线程2已经运行结束 */ 96 | rt_mb_send(&mb, (rt_uint32_t)&mb_str3); 97 | } 98 | 99 | int mailbox_urgent_sample(void) 100 | { 101 | rt_err_t result; 102 | 103 | /* 初始化一个mailbox */ 104 | result = rt_mb_init(&mb, 105 | "mbt", /* 名称是mbt */ 106 | &mb_pool[0], /* 邮箱用到的内存池是mb_pool */ 107 | sizeof(mb_pool) / sizeof(rt_ubase_t), /* 邮箱中的邮件数目,sizeof(rt_ubase_t)表示指针大小 */ 108 | RT_IPC_FLAG_PRIO); /* 采用PRIO方式进行线程等待 */ 109 | if (result != RT_EOK) 110 | { 111 | rt_kprintf("init mailbox failed.\n"); 112 | return -1; 113 | } 114 | 115 | rt_thread_init(&thread1, 116 | "thread1", 117 | thread1_entry, 118 | RT_NULL, 119 | &thread1_stack[0], 120 | sizeof(thread1_stack), 121 | THREAD_PRIORITY, THREAD_TIMESLICE); 122 | #ifdef RT_USING_SMP 123 | /* 绑定线程到同一个核上,避免启用多核时的输出混乱 */ 124 | rt_thread_control(&thread1, RT_THREAD_CTRL_BIND_CPU, (void*)0); 125 | #endif 126 | rt_thread_startup(&thread1); 127 | 128 | rt_thread_init(&thread2, 129 | "thread2", 130 | thread2_entry, 131 | RT_NULL, 132 | &thread2_stack[0], 133 | sizeof(thread2_stack), 134 | THREAD_PRIORITY, THREAD_TIMESLICE); 135 | #ifdef RT_USING_SMP 136 | /* 绑定线程到同一个核上,避免启用多核时的输出混乱 */ 137 | rt_thread_control(&thread2, RT_THREAD_CTRL_BIND_CPU, (void*)0); 138 | #endif 139 | rt_thread_startup(&thread2); 140 | return 0; 141 | } 142 | 143 | /* 导出到 msh 命令列表中 */ 144 | MSH_CMD_EXPORT(mailbox_urgent_sample, mailbox urgent sample); 145 | -------------------------------------------------------------------------------- /zh/memp_sample.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006-2022, RT-Thread Development Team 3 | * 4 | * SPDX-License-Identifier: Apache-2.0 5 | * 6 | * Change Logs: 7 | * Date Author Notes 8 | * 2018-08-24 yangjie the first version 9 | */ 10 | 11 | /* 12 | * 程序清单:内存池例程 13 | * 14 | * 这个程序会创建一个静态的内存池对象,2个动态线程。 15 | * 一个线程会试图从内存池中获得内存块,另一个线程释放内存块 16 | * 内存块 17 | */ 18 | #include 19 | 20 | static rt_uint8_t *ptr[50]; 21 | static rt_uint8_t mempool[4096]; 22 | static struct rt_mempool mp; 23 | 24 | #define THREAD_PRIORITY 25 25 | #define THREAD_STACK_SIZE 512 26 | #define THREAD_TIMESLICE 5 27 | 28 | /* 指向线程控制块的指针 */ 29 | static rt_thread_t tid1 = RT_NULL; 30 | static rt_thread_t tid2 = RT_NULL; 31 | 32 | /* 线程1入口 */ 33 | static void thread1_mp_alloc(void *parameter) 34 | { 35 | int i; 36 | for (i = 0 ; i < 50 ; i++) 37 | { 38 | if (ptr[i] == RT_NULL) 39 | { 40 | /* 试图申请内存块50次,当申请不到内存块时, 41 | 线程1挂起,转至线程2运行 */ 42 | ptr[i] = rt_mp_alloc(&mp, RT_WAITING_FOREVER); 43 | if (ptr[i] != RT_NULL) 44 | rt_kprintf("allocate No.%d\n", i); 45 | } 46 | } 47 | } 48 | 49 | /* 线程2入口,线程2的优先级比线程1低,应该线程1先获得执行。*/ 50 | static void thread2_mp_release(void *parameter) 51 | { 52 | int i; 53 | 54 | rt_kprintf("thread2 try to release block\n"); 55 | for (i = 0; i < 50 ; i++) 56 | { 57 | /* 释放所有分配成功的内存块 */ 58 | if (ptr[i] != RT_NULL) 59 | { 60 | rt_kprintf("release block %d\n", i); 61 | rt_mp_free(ptr[i]); 62 | ptr[i] = RT_NULL; 63 | } 64 | } 65 | } 66 | 67 | int mempool_sample(void) 68 | { 69 | int i; 70 | for (i = 0; i < 50; i ++) ptr[i] = RT_NULL; 71 | 72 | /* 初始化内存池对象 */ 73 | rt_mp_init(&mp, "mp1", &mempool[0], sizeof(mempool), 80); 74 | 75 | /* 创建线程1:申请内存池 */ 76 | tid1 = rt_thread_create("thread1", thread1_mp_alloc, RT_NULL, 77 | THREAD_STACK_SIZE, 78 | THREAD_PRIORITY, THREAD_TIMESLICE); 79 | #ifdef RT_USING_SMP 80 | /* 绑定线程到同一个核上,避免启用多核时的输出混乱 */ 81 | rt_thread_control(tid1, RT_THREAD_CTRL_BIND_CPU, (void*)0); 82 | #endif 83 | if (tid1 != RT_NULL) 84 | rt_thread_startup(tid1); 85 | 86 | /* 创建线程2:释放内存池*/ 87 | tid2 = rt_thread_create("thread2", thread2_mp_release, RT_NULL, 88 | THREAD_STACK_SIZE, 89 | THREAD_PRIORITY + 1, THREAD_TIMESLICE); 90 | #ifdef RT_USING_SMP 91 | /* 绑定线程到同一个核上,避免启用多核时的输出混乱 */ 92 | rt_thread_control(tid2, RT_THREAD_CTRL_BIND_CPU, (void*)0); 93 | #endif 94 | if (tid2 != RT_NULL) 95 | rt_thread_startup(tid2); 96 | 97 | return 0; 98 | } 99 | 100 | /* 导出到 msh 命令列表中 */ 101 | MSH_CMD_EXPORT(mempool_sample, mempool sample); 102 | -------------------------------------------------------------------------------- /zh/msgq_sample.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006-2022, RT-Thread Development Team 3 | * 4 | * SPDX-License-Identifier: Apache-2.0 5 | * 6 | * Change Logs: 7 | * Date Author Notes 8 | * 2018-08-24 yangjie the first version 9 | */ 10 | 11 | /* 12 | * 程序清单:消息队列例程 13 | * 14 | * 这个程序会创建2个动态线程,一个线程会从消息队列中收取消息;一个线程会定时给消 15 | * 息队列发送 普通消息和紧急消息。 16 | */ 17 | #include 18 | 19 | #define THREAD_PRIORITY 25 20 | #define THREAD_TIMESLICE 5 21 | 22 | /* 消息队列控制块 */ 23 | static struct rt_messagequeue mq; 24 | /* 消息队列中用到的放置消息的内存池 */ 25 | static rt_uint8_t msg_pool[2048]; 26 | 27 | #ifdef rt_align 28 | rt_align(RT_ALIGN_SIZE) 29 | #else 30 | ALIGN(RT_ALIGN_SIZE) 31 | #endif 32 | static char thread1_stack[1024]; 33 | static struct rt_thread thread1; 34 | 35 | /* 线程1入口函数 */ 36 | static void thread1_entry(void *parameter) 37 | { 38 | char buf = 0; 39 | rt_uint8_t cnt = 0; 40 | 41 | while (1) 42 | { 43 | /* 从消息队列中接收消息 */ 44 | #if (RTTHREAD_VERSION >= RT_VERSION_CHECK(5, 0, 1)) 45 | if (rt_mq_recv(&mq, &buf, sizeof(buf), RT_WAITING_FOREVER) > 0) 46 | #else 47 | if (rt_mq_recv(&mq, &buf, sizeof(buf), RT_WAITING_FOREVER) == RT_EOK) 48 | #endif 49 | { 50 | rt_kprintf("thread1: recv msg from msg queue, the content:%c\n", buf); 51 | if (cnt == 19) 52 | { 53 | break; 54 | } 55 | } 56 | /* 延时50ms */ 57 | cnt++; 58 | rt_thread_mdelay(50); 59 | } 60 | rt_kprintf("thread1: detach mq \n"); 61 | rt_mq_detach(&mq); 62 | } 63 | 64 | #ifdef rt_align 65 | rt_align(RT_ALIGN_SIZE) 66 | #else 67 | ALIGN(RT_ALIGN_SIZE) 68 | #endif 69 | static char thread2_stack[1024]; 70 | static struct rt_thread thread2; 71 | 72 | /* 线程2入口 */ 73 | static void thread2_entry(void *parameter) 74 | { 75 | int result; 76 | char buf = 'A'; 77 | rt_uint8_t cnt = 0; 78 | 79 | while (1) 80 | { 81 | if (cnt == 8) 82 | { 83 | /* 发送紧急消息到消息队列中 */ 84 | result = rt_mq_urgent(&mq, &buf, 1); 85 | if (result != RT_EOK) 86 | { 87 | rt_kprintf("rt_mq_urgent ERR\n"); 88 | } 89 | else 90 | { 91 | rt_kprintf("thread2: send urgent message - %c\n", buf); 92 | } 93 | } 94 | else if (cnt >= 20)/* 发送20次消息之后退出 */ 95 | { 96 | rt_kprintf("message queue stop send, thread2 quit\n"); 97 | break; 98 | } 99 | else 100 | { 101 | /* 发送消息到消息队列中 */ 102 | result = rt_mq_send(&mq, &buf, 1); 103 | if (result != RT_EOK) 104 | { 105 | rt_kprintf("rt_mq_send ERR\n"); 106 | } 107 | 108 | rt_kprintf("thread2: send message - %c\n", buf); 109 | } 110 | buf++; 111 | cnt++; 112 | /* 延时5ms */ 113 | rt_thread_mdelay(5); 114 | } 115 | } 116 | 117 | /* 消息队列示例的初始化 */ 118 | int msgq_sample(void) 119 | { 120 | rt_err_t result; 121 | 122 | /* 初始化消息队列 */ 123 | result = rt_mq_init(&mq, 124 | "mqt", 125 | &msg_pool[0], /* 内存池指向msg_pool */ 126 | 1, /* 每个消息的大小是 1 字节 */ 127 | sizeof(msg_pool), /* 内存池的大小是msg_pool的大小 */ 128 | RT_IPC_FLAG_PRIO); /* 如果有多个线程等待,按照先来先得到的方法分配消息 */ 129 | 130 | if (result != RT_EOK) 131 | { 132 | rt_kprintf("init message queue failed.\n"); 133 | return -1; 134 | } 135 | 136 | rt_thread_init(&thread1, 137 | "thread1", 138 | thread1_entry, 139 | RT_NULL, 140 | &thread1_stack[0], 141 | sizeof(thread1_stack), 142 | THREAD_PRIORITY, THREAD_TIMESLICE); 143 | #ifdef RT_USING_SMP 144 | /* 绑定线程到同一个核上,避免启用多核时的输出混乱 */ 145 | rt_thread_control(&thread1, RT_THREAD_CTRL_BIND_CPU, (void*)0); 146 | #endif 147 | rt_thread_startup(&thread1); 148 | 149 | rt_thread_init(&thread2, 150 | "thread2", 151 | thread2_entry, 152 | RT_NULL, 153 | &thread2_stack[0], 154 | sizeof(thread2_stack), 155 | THREAD_PRIORITY, THREAD_TIMESLICE); 156 | #ifdef RT_USING_SMP 157 | /* 绑定线程到同一个核上,避免启用多核时的输出混乱 */ 158 | rt_thread_control(&thread2, RT_THREAD_CTRL_BIND_CPU, (void*)0); 159 | #endif 160 | rt_thread_startup(&thread2); 161 | 162 | return 0; 163 | } 164 | 165 | /* 导出到 msh 命令列表中 */ 166 | MSH_CMD_EXPORT(msgq_sample, msgq sample); 167 | -------------------------------------------------------------------------------- /zh/mutex_sample.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006-2022, RT-Thread Development Team 3 | * 4 | * SPDX-License-Identifier: Apache-2.0 5 | * 6 | * Change Logs: 7 | * Date Author Notes 8 | * 2018-08-24 yangjie the first version 9 | */ 10 | 11 | /* 12 | * 程序清单:互斥锁例程 13 | * 14 | * 互斥锁是一种保护共享资源的方法。当一个线程拥有互斥锁的时候, 15 | * 可以保护共享资源不被其他线程破坏。线程1对2个number分别进行加1操作 16 | * 线程2也会对2个number分别进行加1操作。使用互斥量保证2个number值保持一致 17 | */ 18 | #include 19 | 20 | #define THREAD_PRIORITY 8 21 | #define THREAD_TIMESLICE 5 22 | 23 | /* 指向互斥量的指针 */ 24 | static rt_mutex_t dynamic_mutex = RT_NULL; 25 | static rt_uint8_t number1, number2 = 0; 26 | 27 | #ifdef rt_align 28 | rt_align(RT_ALIGN_SIZE) 29 | #else 30 | ALIGN(RT_ALIGN_SIZE) 31 | #endif 32 | static char thread1_stack[1024]; 33 | static struct rt_thread thread1; 34 | static void rt_thread_entry1(void *parameter) 35 | { 36 | while (1) 37 | { 38 | /* 线程1获取到互斥量后,先后对number1、number2进行加1操作,然后释放互斥量 */ 39 | rt_mutex_take(dynamic_mutex, RT_WAITING_FOREVER); 40 | number1++; 41 | rt_thread_mdelay(10); 42 | number2++; 43 | rt_mutex_release(dynamic_mutex); 44 | } 45 | } 46 | 47 | #ifdef rt_align 48 | rt_align(RT_ALIGN_SIZE) 49 | #else 50 | ALIGN(RT_ALIGN_SIZE) 51 | #endif 52 | static char thread2_stack[1024]; 53 | static struct rt_thread thread2; 54 | static void rt_thread_entry2(void *parameter) 55 | { 56 | while (1) 57 | { 58 | /* 线程2获取到互斥量后,检查number1、number2的值是否相同,相同则表示mutex起到了锁的作用 */ 59 | rt_mutex_take(dynamic_mutex, RT_WAITING_FOREVER); 60 | if (number1 != number2) 61 | { 62 | rt_kprintf("not protect.number1 = %d, mumber2 = %d \n", number1, number2); 63 | } 64 | else 65 | { 66 | rt_kprintf("mutex protect ,number1 = mumber2 is %d\n", number1); 67 | } 68 | 69 | number1++; 70 | number2++; 71 | rt_mutex_release(dynamic_mutex); 72 | 73 | if (number1 >= 50) 74 | return; 75 | } 76 | } 77 | 78 | /* 互斥量示例的初始化 */ 79 | int mutex_sample(void) 80 | { 81 | /* 创建一个动态互斥量 */ 82 | dynamic_mutex = rt_mutex_create("dmutex", RT_IPC_FLAG_PRIO); 83 | if (dynamic_mutex == RT_NULL) 84 | { 85 | rt_kprintf("create dynamic mutex failed.\n"); 86 | return -1; 87 | } 88 | 89 | rt_thread_init(&thread1, 90 | "thread1", 91 | rt_thread_entry1, 92 | RT_NULL, 93 | &thread1_stack[0], 94 | sizeof(thread1_stack), 95 | THREAD_PRIORITY, THREAD_TIMESLICE); 96 | #ifdef RT_USING_SMP 97 | /* 绑定线程到同一个核上,避免启用多核时的输出混乱 */ 98 | rt_thread_control(&thread1, RT_THREAD_CTRL_BIND_CPU, (void*)0); 99 | #endif 100 | rt_thread_startup(&thread1); 101 | 102 | rt_thread_init(&thread2, 103 | "thread2", 104 | rt_thread_entry2, 105 | RT_NULL, 106 | &thread2_stack[0], 107 | sizeof(thread2_stack), 108 | THREAD_PRIORITY - 1, THREAD_TIMESLICE); 109 | #ifdef RT_USING_SMP 110 | /* 绑定线程到同一个核上,避免启用多核时的输出混乱 */ 111 | rt_thread_control(&thread2, RT_THREAD_CTRL_BIND_CPU, (void*)0); 112 | #endif 113 | rt_thread_startup(&thread2); 114 | return 0; 115 | } 116 | 117 | /* 导出到 msh 命令列表中 */ 118 | MSH_CMD_EXPORT(mutex_sample, mutex sample); 119 | -------------------------------------------------------------------------------- /zh/priority_inversion.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006-2022, RT-Thread Development Team 3 | * 4 | * SPDX-License-Identifier: Apache-2.0 5 | * 6 | * Change Logs: 7 | * Date Author Notes 8 | * 2018-08-24 yangjie the first version 9 | */ 10 | 11 | /* 12 | * 程序清单:使用互斥量来防止优先级反转 13 | * 14 | * 这个例子将创建 3 个动态线程以检查持有互斥量时,持有的线程优先级是否 15 | * 被调整到等待线程优先级中的最高优先级。 16 | * 17 | * 线程 1,2,3 的优先级从高到低分别被创建, 18 | * 线程 3 先持有互斥量,而后线程 2 试图持有互斥量,此时线程 3 的优先级应该 19 | * 被提升为和线程 2 的优先级相同。线程 1 用于检查线程 3 的优先级是否被提升 20 | * 为与线程 2的优先级相同。 21 | */ 22 | #include 23 | 24 | /* 指向线程控制块的指针 */ 25 | static rt_thread_t tid1 = RT_NULL; 26 | static rt_thread_t tid2 = RT_NULL; 27 | static rt_thread_t tid3 = RT_NULL; 28 | static rt_mutex_t mutex = RT_NULL; 29 | 30 | #define THREAD_PRIORITY 10 31 | #define THREAD_STACK_SIZE 512 32 | #define THREAD_TIMESLICE 5 33 | 34 | /* 线程 1 入口 */ 35 | static void thread1_entry(void *parameter) 36 | { 37 | /* 先让低优先级线程运行 */ 38 | rt_thread_mdelay(100); 39 | 40 | /* 此时 thread3 持有 mutex,并且 thread2 等待持有 mutex */ 41 | #if (RTTHREAD_VERSION >= RT_VERSION_CHECK(5, 1, 0)) 42 | /* 检查 thread2 与 thread3 的优先级情况 */ 43 | if (RT_SCHED_PRIV(tid2).current_priority != RT_SCHED_PRIV(tid3).current_priority) 44 | { 45 | /* 优先级不相同,测试失败 */ 46 | rt_kprintf("the priority of thread2 is: %d\n", RT_SCHED_PRIV(tid2).current_priority); 47 | rt_kprintf("the priority of thread3 is: %d\n", RT_SCHED_PRIV(tid3).current_priority); 48 | rt_kprintf("test failed.\n"); 49 | return; 50 | } 51 | else 52 | { 53 | rt_kprintf("the priority of thread2 is: %d\n", RT_SCHED_PRIV(tid2).current_priority); 54 | rt_kprintf("the priority of thread3 is: %d\n", RT_SCHED_PRIV(tid3).current_priority); 55 | rt_kprintf("test OK.\n"); 56 | } 57 | #else 58 | if (tid2->current_priority != tid3->current_priority) 59 | { 60 | /* 优先级不相同,测试失败 */ 61 | rt_kprintf("the priority of thread2 is: %d\n", tid2->current_priority); 62 | rt_kprintf("the priority of thread3 is: %d\n", tid3->current_priority); 63 | rt_kprintf("test failed.\n"); 64 | return; 65 | } 66 | else 67 | { 68 | rt_kprintf("the priority of thread2 is: %d\n", tid2->current_priority); 69 | rt_kprintf("the priority of thread3 is: %d\n", tid3->current_priority); 70 | rt_kprintf("test OK.\n"); 71 | } 72 | #endif 73 | 74 | } 75 | 76 | /* 线程 2 入口 */ 77 | static void thread2_entry(void *parameter) 78 | { 79 | rt_err_t result; 80 | #if (RTTHREAD_VERSION >= RT_VERSION_CHECK(5, 1, 0)) 81 | rt_kprintf("the priority of thread2 is: %d\n", RT_SCHED_PRIV(tid2).current_priority); 82 | #else 83 | rt_kprintf("the priority of thread2 is: %d\n", tid2->current_priority); 84 | #endif 85 | 86 | /* 先让低优先级线程运行 */ 87 | rt_thread_mdelay(50); 88 | 89 | /* 90 | * 试图持有互斥锁,此时 thread3 持有,应把 thread3 的优先级提升 91 | * 到 thread2 相同的优先级 92 | */ 93 | result = rt_mutex_take(mutex, RT_WAITING_FOREVER); 94 | 95 | if (result == RT_EOK) 96 | { 97 | /* 释放互斥锁 */ 98 | rt_mutex_release(mutex); 99 | } 100 | } 101 | 102 | /* 线程 3 入口 */ 103 | static void thread3_entry(void *parameter) 104 | { 105 | rt_tick_t tick; 106 | rt_err_t result; 107 | #if (RTTHREAD_VERSION >= RT_VERSION_CHECK(5, 1, 0)) 108 | rt_kprintf("the priority of thread3 is: %d\n", RT_SCHED_PRIV(tid3).current_priority); 109 | #else 110 | rt_kprintf("the priority of thread3 is: %d\n", tid3->current_priority); 111 | #endif 112 | 113 | 114 | result = rt_mutex_take(mutex, RT_WAITING_FOREVER); 115 | if (result != RT_EOK) 116 | { 117 | rt_kprintf("thread3 take a mutex, failed.\n"); 118 | } 119 | 120 | /* 做一个长时间的循环,500ms */ 121 | tick = rt_tick_get(); 122 | while (rt_tick_get() - tick < (RT_TICK_PER_SECOND / 2)) ; 123 | 124 | rt_mutex_release(mutex); 125 | } 126 | 127 | int pri_inversion(void) 128 | { 129 | /* 创建互斥锁 */ 130 | mutex = rt_mutex_create("mutex", RT_IPC_FLAG_PRIO); 131 | if (mutex == RT_NULL) 132 | { 133 | rt_kprintf("create dynamic mutex failed.\n"); 134 | return -1; 135 | } 136 | 137 | /* 创建线程 1 */ 138 | tid1 = rt_thread_create("thread1", 139 | thread1_entry, 140 | RT_NULL, 141 | THREAD_STACK_SIZE, 142 | THREAD_PRIORITY - 1, THREAD_TIMESLICE); 143 | #ifdef RT_USING_SMP 144 | /* 绑定线程到同一个核上,避免启用多核时的输出混乱 */ 145 | rt_thread_control(tid1, RT_THREAD_CTRL_BIND_CPU, (void*)0); 146 | #endif 147 | if (tid1 != RT_NULL) 148 | rt_thread_startup(tid1); 149 | 150 | /* 创建线程 2 */ 151 | tid2 = rt_thread_create("thread2", 152 | thread2_entry, 153 | RT_NULL, 154 | THREAD_STACK_SIZE, 155 | THREAD_PRIORITY, THREAD_TIMESLICE); 156 | #ifdef RT_USING_SMP 157 | /* 绑定线程到同一个核上,避免启用多核时的输出混乱 */ 158 | rt_thread_control(tid2, RT_THREAD_CTRL_BIND_CPU, (void*)0); 159 | #endif 160 | if (tid2 != RT_NULL) 161 | rt_thread_startup(tid2); 162 | 163 | /* 创建线程 3 */ 164 | tid3 = rt_thread_create("thread3", 165 | thread3_entry, 166 | RT_NULL, 167 | THREAD_STACK_SIZE, 168 | THREAD_PRIORITY + 1, THREAD_TIMESLICE); 169 | #ifdef RT_USING_SMP 170 | /* 绑定线程到同一个核上,避免启用多核时的输出混乱 */ 171 | rt_thread_control(tid3, RT_THREAD_CTRL_BIND_CPU, (void*)0); 172 | #endif 173 | if (tid3 != RT_NULL) 174 | rt_thread_startup(tid3); 175 | 176 | return 0; 177 | } 178 | 179 | /* 导出到 msh 命令列表中 */ 180 | MSH_CMD_EXPORT(pri_inversion, pri_inversion sample); 181 | -------------------------------------------------------------------------------- /zh/producer_consumer.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006-2022, RT-Thread Development Team 3 | * 4 | * SPDX-License-Identifier: Apache-2.0 5 | * 6 | * Change Logs: 7 | * Date Author Notes 8 | * 2018-08-24 yangjie the first version 9 | */ 10 | 11 | /* 12 | * 程序清单:生产者消费者例子 13 | * 14 | * 这个例子中将创建两个线程用于实现生产者消费者问题 15 | *(1)生产者线程将cnt值每次加1并循环存入array数组的5个成员内; 16 | *(2)消费者线程将生产者中生产的数值打印出来,并累加求和 17 | */ 18 | #include 19 | 20 | #define THREAD_PRIORITY 6 21 | #define THREAD_STACK_SIZE 512 22 | #define THREAD_TIMESLICE 5 23 | 24 | /* 定义最大5个元素能够被产生 */ 25 | #define MAXSEM 5 26 | 27 | /* 用于放置生产的整数数组 */ 28 | rt_uint32_t array[MAXSEM]; 29 | 30 | /* 指向生产者、消费者在array数组中的读写位置 */ 31 | static rt_uint32_t set, get; 32 | 33 | /* 指向线程控制块的指针 */ 34 | static rt_thread_t producer_tid = RT_NULL; 35 | static rt_thread_t consumer_tid = RT_NULL; 36 | 37 | struct rt_semaphore sem_lock; 38 | struct rt_semaphore sem_empty, sem_full; 39 | 40 | /* 生产者线程入口 */ 41 | void producer_thread_entry(void *parameter) 42 | { 43 | int cnt = 0; 44 | 45 | /* 运行10次 */ 46 | while (cnt < 10) 47 | { 48 | /* 获取一个空位 */ 49 | rt_sem_take(&sem_empty, RT_WAITING_FOREVER); 50 | 51 | /* 修改array内容,上锁 */ 52 | rt_sem_take(&sem_lock, RT_WAITING_FOREVER); 53 | array[set % MAXSEM] = cnt + 1; 54 | rt_kprintf("the producer generates a number: %d\n", array[set % MAXSEM]); 55 | set++; 56 | rt_sem_release(&sem_lock); 57 | 58 | /* 发布一个满位 */ 59 | rt_sem_release(&sem_full); 60 | cnt++; 61 | 62 | /* 暂停一段时间 */ 63 | rt_thread_mdelay(20); 64 | } 65 | 66 | rt_kprintf("the producer exit!\n"); 67 | } 68 | 69 | /* 消费者线程入口 */ 70 | void consumer_thread_entry(void *parameter) 71 | { 72 | rt_uint32_t sum = 0; 73 | 74 | while (1) 75 | { 76 | /* 获取一个满位 */ 77 | rt_sem_take(&sem_full, RT_WAITING_FOREVER); 78 | 79 | /* 临界区,上锁进行操作 */ 80 | rt_sem_take(&sem_lock, RT_WAITING_FOREVER); 81 | sum += array[get % MAXSEM]; 82 | rt_kprintf("the consumer[%d] get a number: %d\n", (get % MAXSEM), array[get % MAXSEM]); 83 | get++; 84 | rt_sem_release(&sem_lock); 85 | 86 | /* 释放一个空位 */ 87 | rt_sem_release(&sem_empty); 88 | 89 | /* 生产者生产到10个数目,停止,消费者线程相应停止 */ 90 | if (get == 10) break; 91 | 92 | /* 暂停一小会时间 */ 93 | rt_thread_mdelay(50); 94 | } 95 | 96 | rt_kprintf("the consumer sum is: %d\n", sum); 97 | rt_kprintf("the consumer exit!\n"); 98 | } 99 | 100 | int producer_consumer(void) 101 | { 102 | set = 0; 103 | get = 0; 104 | 105 | /* 初始化3个信号量 */ 106 | rt_sem_init(&sem_lock, "lock", 1, RT_IPC_FLAG_PRIO); 107 | rt_sem_init(&sem_empty, "empty", MAXSEM, RT_IPC_FLAG_PRIO); 108 | rt_sem_init(&sem_full, "full", 0, RT_IPC_FLAG_PRIO); 109 | 110 | /* 创建生产者线程 */ 111 | producer_tid = rt_thread_create("producer", 112 | producer_thread_entry, RT_NULL, 113 | THREAD_STACK_SIZE, 114 | THREAD_PRIORITY - 1, THREAD_TIMESLICE); 115 | #ifdef RT_USING_SMP 116 | /* 绑定线程到同一个核上,避免启用多核时的输出混乱 */ 117 | rt_thread_control(producer_tid, RT_THREAD_CTRL_BIND_CPU, (void*)0); 118 | #endif 119 | if (producer_tid != RT_NULL) 120 | rt_thread_startup(producer_tid); 121 | 122 | /* 创建消费者线程 */ 123 | consumer_tid = rt_thread_create("consumer", 124 | consumer_thread_entry, RT_NULL, 125 | THREAD_STACK_SIZE, 126 | THREAD_PRIORITY + 1, THREAD_TIMESLICE); 127 | #ifdef RT_USING_SMP 128 | /* 绑定线程到同一个核上,避免启用多核时的输出混乱 */ 129 | rt_thread_control(consumer_tid, RT_THREAD_CTRL_BIND_CPU, (void*)0); 130 | #endif 131 | if (consumer_tid != RT_NULL) 132 | rt_thread_startup(consumer_tid); 133 | 134 | return 0; 135 | } 136 | 137 | /* 导出到 msh 命令列表中 */ 138 | MSH_CMD_EXPORT(producer_consumer, producer_consumer sample); 139 | -------------------------------------------------------------------------------- /zh/scheduler_hook.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006-2022, RT-Thread Development Team 3 | * 4 | * SPDX-License-Identifier: Apache-2.0 5 | * 6 | * Change Logs: 7 | * Date Author Notes 8 | * 2018-08-24 yangjie the first version 9 | */ 10 | 11 | /* 12 | * 程序清单:调度器钩子 13 | * 在调度器钩子中打印线程切换信息 14 | */ 15 | 16 | #include 17 | 18 | #define THREAD_STACK_SIZE 1024 19 | #define THREAD_PRIORITY 20 20 | #define THREAD_TIMESLICE 10 21 | 22 | /* 针对每个线程的计数器 */ 23 | volatile rt_uint32_t count[2]; 24 | 25 | /* 线程1、2共用一个入口,但入口参数不同 */ 26 | static void thread_entry(void *parameter) 27 | { 28 | rt_uint32_t value; 29 | 30 | value = (rt_uint32_t)parameter; 31 | while (1) 32 | { 33 | rt_kprintf("thread %d is running\n", value); 34 | rt_thread_mdelay(1000); //延时一段时间 35 | } 36 | } 37 | 38 | static rt_thread_t tid1 = RT_NULL; 39 | static rt_thread_t tid2 = RT_NULL; 40 | 41 | static void hook_of_scheduler(struct rt_thread *from, struct rt_thread *to) 42 | { 43 | #if RT_VER_NUM >= 0x50001 44 | rt_kprintf("from: %s --> to: %s \n", from->parent.name, to->parent.name); 45 | #else 46 | rt_kprintf("from: %s --> to: %s \n", from->name, to->name); 47 | #endif 48 | } 49 | 50 | int scheduler_hook(void) 51 | { 52 | /* 设置调度器钩子 */ 53 | rt_scheduler_sethook(hook_of_scheduler); 54 | 55 | /* 创建线程1 */ 56 | tid1 = rt_thread_create("thread1", 57 | thread_entry, (void *)1, 58 | THREAD_STACK_SIZE, 59 | THREAD_PRIORITY, THREAD_TIMESLICE); 60 | if (tid1 != RT_NULL) 61 | rt_thread_startup(tid1); 62 | 63 | /* 创建线程2 */ 64 | tid2 = rt_thread_create("thread2", 65 | thread_entry, (void *)2, 66 | THREAD_STACK_SIZE, 67 | THREAD_PRIORITY, THREAD_TIMESLICE - 5); 68 | if (tid2 != RT_NULL) 69 | rt_thread_startup(tid2); 70 | return 0; 71 | } 72 | 73 | /* 导出到 msh 命令列表中 */ 74 | MSH_CMD_EXPORT(scheduler_hook, scheduler_hook sample); 75 | -------------------------------------------------------------------------------- /zh/semaphore_sample.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006-2022, RT-Thread Development Team 3 | * 4 | * SPDX-License-Identifier: Apache-2.0 5 | * 6 | * Change Logs: 7 | * Date Author Notes 8 | * 2018-08-24 yangjie the first version 9 | */ 10 | 11 | /* 12 | * 程序清单:信号量例程 13 | * 14 | * 该例程创建了一个动态信号量,初始化两个线程,线程1在count每计数10次时, 15 | * 发送一个信号量,线程2在接收信号量后,对number进行加1操作 16 | */ 17 | #include 18 | 19 | #define THREAD_PRIORITY 25 20 | #define THREAD_TIMESLICE 5 21 | 22 | /* 指向信号量的指针 */ 23 | static rt_sem_t dynamic_sem = RT_NULL; 24 | 25 | #ifdef rt_align 26 | rt_align(RT_ALIGN_SIZE) 27 | #else 28 | ALIGN(RT_ALIGN_SIZE) 29 | #endif 30 | static char thread1_stack[1024]; 31 | static struct rt_thread thread1; 32 | static void rt_thread1_entry(void *parameter) 33 | { 34 | static rt_uint8_t count = 0; 35 | 36 | while (1) 37 | { 38 | if (count <= 100) 39 | { 40 | count++; 41 | } 42 | else 43 | return; 44 | 45 | /* count每计数10次,就释放一次信号量 */ 46 | if (0 == (count % 10)) 47 | { 48 | rt_kprintf("thread1 release a dynamic semaphore.\n"); 49 | rt_sem_release(dynamic_sem); 50 | } 51 | } 52 | } 53 | 54 | #ifdef rt_align 55 | rt_align(RT_ALIGN_SIZE) 56 | #else 57 | ALIGN(RT_ALIGN_SIZE) 58 | #endif 59 | static char thread2_stack[1024]; 60 | static struct rt_thread thread2; 61 | static void rt_thread2_entry(void *parameter) 62 | { 63 | static rt_err_t result; 64 | static rt_uint8_t number = 0; 65 | while (1) 66 | { 67 | /* 永久方式等待信号量,获取到信号量,则执行number自加的操作 */ 68 | result = rt_sem_take(dynamic_sem, RT_WAITING_FOREVER); 69 | if (result != RT_EOK) 70 | { 71 | rt_kprintf("thread2 take a dynamic semaphore, failed.\n"); 72 | rt_sem_delete(dynamic_sem); 73 | return; 74 | } 75 | else 76 | { 77 | number++; 78 | rt_kprintf("thread2 take a dynamic semaphore. number = %d\n", number); 79 | } 80 | } 81 | } 82 | 83 | /* 信号量示例的初始化 */ 84 | int semaphore_sample() 85 | { 86 | /* 创建一个动态信号量,初始值是0 */ 87 | dynamic_sem = rt_sem_create("dsem", 0, RT_IPC_FLAG_PRIO); 88 | if (dynamic_sem == RT_NULL) 89 | { 90 | rt_kprintf("create dynamic semaphore failed.\n"); 91 | return -1; 92 | } 93 | else 94 | { 95 | rt_kprintf("create done. dynamic semaphore value = 0.\n"); 96 | } 97 | 98 | rt_thread_init(&thread1, 99 | "thread1", 100 | rt_thread1_entry, 101 | RT_NULL, 102 | &thread1_stack[0], 103 | sizeof(thread1_stack), 104 | THREAD_PRIORITY, THREAD_TIMESLICE); 105 | #ifdef RT_USING_SMP 106 | /* 绑定线程到同一个核上,避免启用多核时的输出混乱 */ 107 | rt_thread_control(&thread1, RT_THREAD_CTRL_BIND_CPU, (void*)0); 108 | #endif 109 | rt_thread_startup(&thread1); 110 | 111 | rt_thread_init(&thread2, 112 | "thread2", 113 | rt_thread2_entry, 114 | RT_NULL, 115 | &thread2_stack[0], 116 | sizeof(thread2_stack), 117 | THREAD_PRIORITY - 1, THREAD_TIMESLICE); 118 | #ifdef RT_USING_SMP 119 | /* 绑定线程到同一个核上,避免启用多核时的输出混乱 */ 120 | rt_thread_control(&thread2, RT_THREAD_CTRL_BIND_CPU, (void*)0); 121 | #endif 122 | rt_thread_startup(&thread2); 123 | 124 | return 0; 125 | } 126 | 127 | /* 导出到 msh 命令列表中 */ 128 | MSH_CMD_EXPORT(semaphore_sample, semaphore sample); 129 | 130 | -------------------------------------------------------------------------------- /zh/signal_sample.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006-2022, RT-Thread Development Team 3 | * 4 | * SPDX-License-Identifier: Apache-2.0 5 | * 6 | * Change Logs: 7 | * Date Author Notes 8 | * 2018-08-24 yangjie the first version 9 | */ 10 | 11 | /* 12 | * 程序清单:信号例程 13 | * 14 | * 这个例子会创建一个线程,线程安装信号,然后给这个线程发送信号。 15 | * 16 | */ 17 | #include 18 | 19 | #define THREAD_PRIORITY 25 20 | #define THREAD_STACK_SIZE 512 21 | #define THREAD_TIMESLICE 5 22 | 23 | static rt_thread_t tid1 = RT_NULL; 24 | 25 | /* 线程1的信号处理函数 */ 26 | void thread1_signal_handler(int sig) 27 | { 28 | rt_kprintf("thread1 received signal %d\n", sig); 29 | } 30 | 31 | /* 线程1的入口函数 */ 32 | static void thread1_entry(void *parameter) 33 | { 34 | int cnt = 0; 35 | 36 | /* 安装信号 */ 37 | rt_signal_install(SIGUSR1, thread1_signal_handler); 38 | rt_signal_unmask(SIGUSR1); 39 | 40 | /* 运行10次 */ 41 | while (cnt < 10) 42 | { 43 | /* 线程1采用低优先级运行,一直打印计数值 */ 44 | rt_kprintf("thread1 count : %d\n", cnt); 45 | 46 | cnt++; 47 | rt_thread_mdelay(100); 48 | } 49 | } 50 | 51 | /* 信号示例的初始化 */ 52 | int signal_sample(void) 53 | { 54 | /* 创建线程1 */ 55 | tid1 = rt_thread_create("thread1", 56 | thread1_entry, RT_NULL, 57 | THREAD_STACK_SIZE, 58 | THREAD_PRIORITY, THREAD_TIMESLICE); 59 | 60 | if (tid1 != RT_NULL) 61 | rt_thread_startup(tid1); 62 | 63 | rt_thread_mdelay(300); 64 | 65 | /* 发送信号 SIGUSR1 给线程1 */ 66 | rt_thread_kill(tid1, SIGUSR1); 67 | 68 | return 0; 69 | } 70 | 71 | /* 导出到 msh 命令列表中 */ 72 | MSH_CMD_EXPORT(signal_sample, signal sample); 73 | -------------------------------------------------------------------------------- /zh/thread_sample.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006-2022, RT-Thread Development Team 3 | * 4 | * SPDX-License-Identifier: Apache-2.0 5 | * 6 | * Change Logs: 7 | * Date Author Notes 8 | * 2018-08-24 yangjie the first version 9 | */ 10 | 11 | /* 12 | * 程序清单:创建、初始化/脱离线程 13 | * 14 | * 这个例子会创建两个线程,一个动态线程,一个静态线程。 15 | * 静态线程在运行完毕后自动被系统脱离,动态线程一直打印计数。 16 | */ 17 | #include 18 | 19 | #define THREAD_PRIORITY 25 20 | #define THREAD_STACK_SIZE 512 21 | #define THREAD_TIMESLICE 5 22 | 23 | static rt_thread_t tid1 = RT_NULL; 24 | 25 | /* 线程1的入口函数 */ 26 | static void thread1_entry(void *parameter) 27 | { 28 | rt_uint32_t count = 0; 29 | 30 | while (1) 31 | { 32 | /* 线程1采用低优先级运行,一直打印计数值 */ 33 | rt_kprintf("thread1 count: %d\n", count ++); 34 | rt_thread_mdelay(500); 35 | } 36 | } 37 | 38 | #ifdef rt_align 39 | rt_align(RT_ALIGN_SIZE) 40 | #else 41 | ALIGN(RT_ALIGN_SIZE) 42 | #endif 43 | static char thread2_stack[1024]; 44 | static struct rt_thread thread2; 45 | 46 | /* 线程2入口 */ 47 | static void thread2_entry(void *param) 48 | { 49 | rt_uint32_t count = 0; 50 | 51 | /* 线程2拥有较高的优先级,以抢占线程1而获得执行 */ 52 | for (count = 0; count < 10 ; count++) 53 | { 54 | /* 线程2打印计数值 */ 55 | rt_kprintf("thread2 count: %d\n", count); 56 | } 57 | rt_kprintf("thread2 exit\n"); 58 | 59 | /* 线程2运行结束后也将自动被系统脱离 */ 60 | } 61 | 62 | /* 线程示例 */ 63 | int thread_sample(void) 64 | { 65 | /* 创建线程1,名称是thread1,入口是thread1_entry*/ 66 | tid1 = rt_thread_create("thread1", 67 | thread1_entry, RT_NULL, 68 | THREAD_STACK_SIZE, 69 | THREAD_PRIORITY, THREAD_TIMESLICE); 70 | #ifdef RT_USING_SMP 71 | /* 绑定线程到同一个核上,避免启用多核时的输出混乱 */ 72 | rt_thread_control(tid1, RT_THREAD_CTRL_BIND_CPU, (void*)0); 73 | #endif 74 | /* 如果获得线程控制块,启动这个线程 */ 75 | if (tid1 != RT_NULL) 76 | rt_thread_startup(tid1); 77 | 78 | /* 初始化线程2,名称是thread2,入口是thread2_entry */ 79 | rt_thread_init(&thread2, 80 | "thread2", 81 | thread2_entry, 82 | RT_NULL, 83 | &thread2_stack[0], 84 | sizeof(thread2_stack), 85 | THREAD_PRIORITY - 1, THREAD_TIMESLICE); 86 | #ifdef RT_USING_SMP 87 | /* 绑定线程到同一个核上,避免启用多核时的输出混乱 */ 88 | rt_thread_control(&thread2, RT_THREAD_CTRL_BIND_CPU, (void*)0); 89 | #endif 90 | rt_thread_startup(&thread2); 91 | 92 | return 0; 93 | } 94 | 95 | /* 导出到 msh 命令列表中 */ 96 | MSH_CMD_EXPORT(thread_sample, thread sample); 97 | -------------------------------------------------------------------------------- /zh/timer_sample.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006-2022, RT-Thread Development Team 3 | * 4 | * SPDX-License-Identifier: Apache-2.0 5 | * 6 | * Change Logs: 7 | * Date Author Notes 8 | * 2018-08-24 yangjie the first version 9 | */ 10 | 11 | /* 12 | * 程序清单:定时器例程 13 | * 14 | * 这个例程会创建两个动态定时器,一个是单次定时,一个是周期性定时 15 | * 并让周期定时器运行一段时间后停止运行 16 | */ 17 | #include 18 | 19 | /* 定时器的控制块 */ 20 | static rt_timer_t timer1; 21 | static rt_timer_t timer2; 22 | static int cnt = 0; 23 | 24 | /* 定时器1超时函数 */ 25 | static void timeout1(void *parameter) 26 | { 27 | rt_kprintf("periodic timer is timeout %d\n", cnt); 28 | 29 | /* 运行第10次,停止周期定时器 */ 30 | if (cnt++ >= 9) 31 | { 32 | rt_timer_stop(timer1); 33 | rt_kprintf("periodic timer was stopped! \n"); 34 | } 35 | } 36 | 37 | /* 定时器2超时函数 */ 38 | static void timeout2(void *parameter) 39 | { 40 | rt_kprintf("one shot timer is timeout\n"); 41 | } 42 | 43 | int timer_sample(void) 44 | { 45 | /* 创建定时器1 周期定时器 */ 46 | timer1 = rt_timer_create("timer1", timeout1, 47 | RT_NULL, 10, 48 | RT_TIMER_FLAG_PERIODIC); 49 | 50 | /* 启动定时器1 */ 51 | if (timer1 != RT_NULL) 52 | rt_timer_start(timer1); 53 | 54 | /* 创建定时器2 单次定时器 */ 55 | timer2 = rt_timer_create("timer2", timeout2, 56 | RT_NULL, 30, 57 | RT_TIMER_FLAG_ONE_SHOT); 58 | 59 | /* 启动定时器2 */ 60 | if (timer2 != RT_NULL) 61 | rt_timer_start(timer2); 62 | 63 | return 0; 64 | } 65 | 66 | /* 导出到 msh 命令列表中 */ 67 | MSH_CMD_EXPORT(timer_sample, timer sample); 68 | -------------------------------------------------------------------------------- /zh/timeslice_sample.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006-2022, RT-Thread Development Team 3 | * 4 | * SPDX-License-Identifier: Apache-2.0 5 | * 6 | * Change Logs: 7 | * Date Author Notes 8 | * 2018-08-24 yangjie the first version 9 | */ 10 | 11 | /* 12 | * 程序清单:相同优先级线程按照时间片轮转调度 13 | * 14 | * 这个例子中将创建两个线程,每一个线程都在打印信息 15 | * 16 | */ 17 | 18 | #include 19 | 20 | #define THREAD_STACK_SIZE 1024 21 | #define THREAD_PRIORITY 20 22 | #define THREAD_TIMESLICE 10 23 | 24 | /* 线程入口 */ 25 | static void thread_entry(void *parameter) 26 | { 27 | rt_uint32_t value; 28 | rt_uint32_t count = 0; 29 | 30 | value = (rt_uint32_t)parameter; 31 | while (1) 32 | { 33 | if (0 == (count % 5)) 34 | { 35 | rt_kprintf("thread %d is running ,thread %d count = %d\n", value, value, count); 36 | 37 | if (count > 200) 38 | return; 39 | } 40 | count++; 41 | } 42 | } 43 | 44 | int timeslice_sample(void) 45 | { 46 | rt_thread_t tid = RT_NULL; 47 | /* 创建线程1 */ 48 | tid = rt_thread_create("thread1", 49 | thread_entry, (void *)1, 50 | THREAD_STACK_SIZE, 51 | THREAD_PRIORITY, THREAD_TIMESLICE); 52 | #ifdef RT_USING_SMP 53 | /* 绑定线程到同一个核上,避免启用多核时的输出混乱 */ 54 | rt_thread_control(tid, RT_THREAD_CTRL_BIND_CPU, (void*)0); 55 | #endif 56 | if (tid != RT_NULL) 57 | rt_thread_startup(tid); 58 | 59 | /* 创建线程2 */ 60 | tid = rt_thread_create("thread2", 61 | thread_entry, (void *)2, 62 | THREAD_STACK_SIZE, 63 | THREAD_PRIORITY, THREAD_TIMESLICE - 5); 64 | #ifdef RT_USING_SMP 65 | /* 绑定线程到同一个核上,避免启用多核时的输出混乱 */ 66 | rt_thread_control(tid, RT_THREAD_CTRL_BIND_CPU, (void*)0); 67 | #endif 68 | if (tid != RT_NULL) 69 | rt_thread_startup(tid); 70 | 71 | return 0; 72 | } 73 | 74 | /* 导出到 msh 命令列表中 */ 75 | MSH_CMD_EXPORT(timeslice_sample, timeslice sample); 76 | 77 | --------------------------------------------------------------------------------