amos 2465ffc7cf Add 4 tasks from batch 28: ESP ringbuf, RT-Thread dataqueue and cond wait, NuttX NSH quoting
Batch 28 commit 3847b17 gave ESP ringbuf reserve/publish (1 of 3 passed)
and RT-Thread dataqueue wrap reset (0 of 3) as HARD, and RT-Thread
condition wait transitions and NuttX NSH quoted script flow (2 of 3) as
MEDIUM. Each task comes from the commit Qwen measured, its digest
matches, and it is still present at the batch branch head.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-05 04:00:02 +08:00

2 lines
351 KiB
JSON
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

{"LICENSE": " Apache License\n Version 2.0, January 2004\n http://www.apache.org/licenses/\n\n TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION\n\n 1. Definitions.\n\n \"License\" shall mean the terms and conditions for use, reproduction,\n and distribution as defined by Sections 1 through 9 of this document.\n\n \"Licensor\" shall mean the copyright owner or entity authorized by\n the copyright owner that is granting the License.\n\n \"Legal Entity\" shall mean the union of the acting entity and all\n other entities that control, are controlled by, or are under common\n control with that entity. For the purposes of this definition,\n \"control\" means (i) the power, direct or indirect, to cause the\n direction or management of such entity, whether by contract or\n otherwise, or (ii) ownership of fifty percent (50%) or more of the\n outstanding shares, or (iii) beneficial ownership of such entity.\n\n \"You\" (or \"Your\") shall mean an individual or Legal Entity\n exercising permissions granted by this License.\n\n \"Source\" form shall mean the preferred form for making modifications,\n including but not limited to software source code, documentation\n source, and configuration files.\n\n \"Object\" form shall mean any form resulting from mechanical\n transformation or translation of a Source form, including but\n not limited to compiled object code, generated documentation,\n and conversions to other media types.\n\n \"Work\" shall mean the work of authorship, whether in Source or\n Object form, made available under the License, as indicated by a\n copyright notice that is included in or attached to the work\n (an example is provided in the Appendix below).\n\n \"Derivative Works\" shall mean any work, whether in Source or Object\n form, that is based on (or derived from) the Work and for which the\n editorial revisions, annotations, elaborations, or other modifications\n represent, as a whole, an original work of authorship. For the purposes\n of this License, Derivative Works shall not include works that remain\n separable from, or merely link (or bind by name) to the interfaces of,\n the Work and Derivative Works thereof.\n\n \"Contribution\" shall mean any work of authorship, including\n the original version of the Work and any modifications or additions\n to that Work or Derivative Works thereof, that is intentionally\n submitted to Licensor for inclusion in the Work by the copyright owner\n or by an individual or Legal Entity authorized to submit on behalf of\n the copyright owner. For the purposes of this definition, \"submitted\"\n means any form of electronic, verbal, or written communication sent\n to the Licensor or its representatives, including but not limited to\n communication on electronic mailing lists, source code control systems,\n and issue tracking systems that are managed by, or on behalf of, the\n Licensor for the purpose of discussing and improving the Work, but\n excluding communication that is conspicuously marked or otherwise\n designated in writing by the copyright owner as \"Not a Contribution.\"\n\n \"Contributor\" shall mean Licensor and any individual or Legal Entity\n on behalf of whom a Contribution has been received by Licensor and\n subsequently incorporated within the Work.\n\n 2. Grant of Copyright License. Subject to the terms and conditions of\n this License, each Contributor hereby grants to You a perpetual,\n worldwide, non-exclusive, no-charge, royalty-free, irrevocable\n copyright license to reproduce, prepare Derivative Works of,\n publicly display, publicly perform, sublicense, and distribute the\n Work and such Derivative Works in Source or Object form.\n\n 3. Grant of Patent License. Subject to the terms and conditions of\n this License, each Contributor hereby grants to You a perpetual,\n worldwide, non-exclusive, no-charge, royalty-free, irrevocable\n (except as stated in this section) patent license to make, have made,\n use, offer to sell, sell, import, and otherwise transfer the Work,\n where such license applies only to those patent claims licensable\n by such Contributor that are necessarily infringed by their\n Contribution(s) alone or by combination of their Contribution(s)\n with the Work to which such Contribution(s) was submitted. If You\n institute patent litigation against any entity (including a\n cross-claim or counterclaim in a lawsuit) alleging that the Work\n or a Contribution incorporated within the Work constitutes direct\n or contributory patent infringement, then any patent licenses\n granted to You under this License for that Work shall terminate\n as of the date such litigation is filed.\n\n 4. Redistribution. You may reproduce and distribute copies of the\n Work or Derivative Works thereof in any medium, with or without\n modifications, and in Source or Object form, provided that You\n meet the following conditions:\n\n (a) You must give any other recipients of the Work or\n Derivative Works a copy of this License; and\n\n (b) You must cause any modified files to carry prominent notices\n stating that You changed the files; and\n\n (c) You must retain, in the Source form of any Derivative Works\n that You distribute, all copyright, patent, trademark, and\n attribution notices from the Source form of the Work,\n excluding those notices that do not pertain to any part of\n the Derivative Works; and\n\n (d) If the Work includes a \"NOTICE\" text file as part of its\n distribution, then any Derivative Works that You distribute must\n include a readable copy of the attribution notices contained\n within such NOTICE file, excluding those notices that do not\n pertain to any part of the Derivative Works, in at least one\n of the following places: within a NOTICE text file distributed\n as part of the Derivative Works; within the Source form or\n documentation, if provided along with the Derivative Works; or,\n within a display generated by the Derivative Works, if and\n wherever such third-party notices normally appear. The contents\n of the NOTICE file are for informational purposes only and\n do not modify the License. You may add Your own attribution\n notices within Derivative Works that You distribute, alongside\n or as an addendum to the NOTICE text from the Work, provided\n that such additional attribution notices cannot be construed\n as modifying the License.\n\n You may add Your own copyright statement to Your modifications and\n may provide additional or different license terms and conditions\n for use, reproduction, or distribution of Your modifications, or\n for any such Derivative Works as a whole, provided Your use,\n reproduction, and distribution of the Work otherwise complies with\n the conditions stated in this License.\n\n 5. Submission of Contributions. Unless You explicitly state otherwise,\n any Contribution intentionally submitted for inclusion in the Work\n by You to the Licensor shall be under the terms and conditions of\n this License, without any additional terms or conditions.\n Notwithstanding the above, nothing herein shall supersede or modify\n the terms of any separate license agreement you may have executed\n with Licensor regarding such Contributions.\n\n 6. Trademarks. This License does not grant permission to use the trade\n names, trademarks, service marks, or product names of the Licensor,\n except as required for reasonable and customary use in describing the\n origin of the Work and reproducing the content of the NOTICE file.\n\n 7. Disclaimer of Warranty. Unless required by applicable law or\n agreed to in writing, Licensor provides the Work (and each\n Contributor provides its Contributions) on an \"AS IS\" BASIS,\n WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or\n implied, including, without limitation, any warranties or conditions\n of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A\n PARTICULAR PURPOSE. You are solely responsible for determining the\n appropriateness of using or redistributing the Work and assume any\n risks associated with Your exercise of permissions under this License.\n\n 8. Limitation of Liability. In no event and under no legal theory,\n whether in tort (including negligence), contract, or otherwise,\n unless required by applicable law (such as deliberate and grossly\n negligent acts) or agreed to in writing, shall any Contributor be\n liable to You for damages, including any direct, indirect, special,\n incidental, or consequential damages of any character arising as a\n result of this License or out of the use or inability to use the\n Work (including but not limited to damages for loss of goodwill,\n work stoppage, computer failure or malfunction, or any and all\n other commercial damages or losses), even if such Contributor\n has been advised of the possibility of such damages.\n\n 9. Accepting Warranty or Additional Liability. While redistributing\n the Work or Derivative Works thereof, You may choose to offer,\n and charge a fee for, acceptance of support, warranty, indemnity,\n or other liability obligations and/or rights consistent with this\n License. However, in accepting such obligations, You may act only\n on Your own behalf and on Your sole responsibility, not on behalf\n of any other Contributor, and only if You agree to indemnify,\n defend, and hold each Contributor harmless for any liability\n incurred by, or claims asserted against, such Contributor by reason\n of your accepting any such warranty or additional liability.\n\n END OF TERMS AND CONDITIONS\n\n APPENDIX: How to apply the Apache License to your work.\n\n To apply the Apache License to your work, attach the following\n boilerplate notice, with the fields enclosed by brackets \"[]\"\n replaced with your own identifying information. (Don't include\n the brackets!) The text should be enclosed in the appropriate\n comment syntax for the file format. We also recommend that a\n file or class name and description of purpose be included on the\n same \"printed page\" as the copyright notice for easier\n identification within third-party archives.\n\n Copyright [yyyy] [name of copyright owner]\n\n Licensed under the Apache License, Version 2.0 (the \"License\");\n you may not use this file except in compliance with the License.\n You may obtain a copy of the License at\n\n http://www.apache.org/licenses/LICENSE-2.0\n\n Unless required by applicable law or agreed to in writing, software\n distributed under the License is distributed on an \"AS IS\" BASIS,\n WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n See the License for the specific language governing permissions and\n limitations under the License.\n", "Makefile": "CC = gcc\nCFLAGS = -std=c11 -O1 -D_XOPEN_SOURCE=700 -I. -Iinclude -Icomponents/drivers/include\nSOURCES = components/drivers/src/dataqueue.c src/thread.c src/timer.c src/scheduler.c src/clock.c src/object.c src/irq.c src/ipc.c src/kservice.c src/idle.c host/port.c\n.PHONY: smoke clean\nsmoke: build/smoke\n\t./build/smoke\nbuild/smoke: $(SOURCES) host/smoke.c\n\tmkdir -p build\n\t$(CC) $(CFLAGS) $(SOURCES) host/smoke.c -o $@\nclean:\n\trm -f build/smoke\n", "README.md": "# 帧搬运服务支撑工程\n\n帧搬运服务以公开数据队列 API 交接稳定的帧描述符。工程保留 RT-Thread 的线程、调度器、定时器、信号量、对象与基础服务源码;`host/port.c` 提供 Linux 用户态 CPU 上下文、IRQ 屏蔽值、控制台和堆,不实现队列、调度策略或内核定时器。\n\n接口与交付行为见 `docs/queue-contract.md`。容量、低水位和等待 tick 来自调用方;输出指针不转移数据所有权。`make smoke` 构建并运行一次普通帧交接,检查工程能启动内核并完成正常路径。\n\n最终只提交并评分 `components/drivers/src/dataqueue.c` 和 `components/drivers/include/ipc/dataqueue.h`。允许调整这两个文件中的结构与辅助实现;新增文件不会被收集。\n\n冻结源码的 Apache-2.0 许可见 `LICENSE` 和各文件版权头。主机配置、移植层、构建与 smoke 是为本工作区补全的材料。\n", "components/drivers/include/ipc/completion.h": "/*\n * Copyright (c) 2006-2018, RT-Thread Development Team\n *\n * SPDX-License-Identifier: Apache-2.0\n *\n * Change Logs:\n * Date Author Notes\n */\n#ifndef COMPLETION_H_\n#define COMPLETION_H_\n\n#include <rtthread.h>\n\n/**\n * Completion\n */\n\nstruct rt_completion\n{\n rt_uint32_t flag;\n\n /* suspended list */\n rt_list_t suspended_list;\n};\n\nvoid rt_completion_init(struct rt_completion *completion);\nrt_err_t rt_completion_wait(struct rt_completion *completion,\n rt_int32_t timeout);\nvoid rt_completion_done(struct rt_completion *completion);\n\n#endif\n", "components/drivers/include/ipc/pipe.h": "/*\n * Copyright (c) 2006-2018, RT-Thread Development Team\n *\n * SPDX-License-Identifier: Apache-2.0\n *\n * Change Logs:\n * Date Author Notes\n */\n#ifndef PIPE_H__\n#define PIPE_H__\n\n/**\n * Pipe Device\n */\n#include <rtthread.h>\n#include <rtdevice.h>\n\n#ifndef RT_PIPE_BUFSZ\n#define PIPE_BUFSZ 512\n#else\n#define PIPE_BUFSZ RT_PIPE_BUFSZ\n#endif\n\nstruct rt_pipe_device\n{\n struct rt_device parent;\n rt_bool_t is_named;\n\n /* ring buffer in pipe device */\n struct rt_ringbuffer *fifo;\n rt_uint16_t bufsz;\n\n rt_uint8_t readers;\n rt_uint8_t writers;\n\n rt_wqueue_t reader_queue;\n rt_wqueue_t writer_queue;\n\n struct rt_mutex lock;\n};\ntypedef struct rt_pipe_device rt_pipe_t;\n\nrt_pipe_t *rt_pipe_create(const char *name, int bufsz);\nint rt_pipe_delete(const char *name);\n#endif /* PIPE_H__ */\n", "components/drivers/include/ipc/poll.h": "/*\n * Copyright (c) 2006-2018, RT-Thread Development Team\n *\n * SPDX-License-Identifier: Apache-2.0\n *\n * Change Logs:\n * Date Author Notes\n * 2016-09-19 Heyuanjie The first version.\n * 2016-12-26 Bernard Update poll interface\n */\n#ifndef IPC_POLL_H__\n#define IPC_POLL_H__\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\nstruct rt_pollreq;\ntypedef void (*poll_queue_proc)(rt_wqueue_t *, struct rt_pollreq *);\n\ntypedef struct rt_pollreq\n{\n poll_queue_proc _proc;\n short _key;\n} rt_pollreq_t;\n\nrt_inline void rt_poll_add(rt_wqueue_t *wq, rt_pollreq_t *req)\n{\n if (req && req->_proc && wq)\n {\n req->_proc(wq, req);\n }\n}\n\n#ifdef __cplusplus\n}\n#endif\n\n#endif\n", "components/drivers/include/ipc/ringblk_buf.h": "/*\n * Copyright (c) 2006-2018, RT-Thread Development Team\n *\n * SPDX-License-Identifier: Apache-2.0\n *\n * Change Logs:\n * Date Author Notes\n * 2018-08-25 armink the first version\n */\n\n#ifndef _RINGBLK_BUF_H_\n#define _RINGBLK_BUF_H_\n\n/*\n * Introduction:\n * The rbb is the ring buffer which is composed with many blocks. It is different from the ring buffer.\n * The ring buffer is only composed with chars. The rbb put and get supported zero copies. So the rbb\n * is very suitable for put block and get block by a certain order. Such as DMA block transmit,\n * communicate frame send/recv, and so on.\n */\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\nenum rt_rbb_status\n{\n /* unused status when first initialize or after blk_free() */\n RT_RBB_BLK_UNUSED,\n /* initialized status after blk_alloc() */\n RT_RBB_BLK_INITED,\n /* put status after blk_put() */\n RT_RBB_BLK_PUT,\n /* get status after blk_get() */\n RT_RBB_BLK_GET,\n};\ntypedef enum rt_rbb_status rt_rbb_status_t;\n\n/**\n * the block of rbb\n */\nstruct rt_rbb_blk\n{\n rt_rbb_status_t status :8;\n /* less then 2^24 */\n rt_size_t size :24;\n rt_uint8_t *buf;\n rt_slist_t list;\n};\ntypedef struct rt_rbb_blk *rt_rbb_blk_t;\n\n/**\n * Rbb block queue: the blocks (from block1->buf to blockn->buf) memory which on this queue is continuous.\n */\nstruct rt_rbb_blk_queue\n{\n rt_rbb_blk_t blocks;\n rt_size_t blk_num;\n};\ntypedef struct rt_rbb_blk_queue *rt_rbb_blk_queue_t;\n\n/**\n * ring block buffer\n */\nstruct rt_rbb\n{\n rt_uint8_t *buf;\n rt_size_t buf_size;\n /* all of blocks */\n rt_rbb_blk_t blk_set;\n rt_size_t blk_max_num;\n /* saved the initialized and put status blocks */\n rt_slist_t blk_list;\n};\ntypedef struct rt_rbb *rt_rbb_t;\n\n/* rbb (ring block buffer) API */\nvoid rt_rbb_init(rt_rbb_t rbb, rt_uint8_t *buf, rt_size_t buf_size, rt_rbb_blk_t block_set, rt_size_t blk_max_num);\nrt_rbb_t rt_rbb_create(rt_size_t buf_size, rt_size_t blk_max_num);\nvoid rt_rbb_destroy(rt_rbb_t rbb);\nrt_size_t rt_rbb_get_buf_size(rt_rbb_t rbb);\n\n/* rbb block API */\nrt_rbb_blk_t rt_rbb_blk_alloc(rt_rbb_t rbb, rt_size_t blk_size);\nvoid rt_rbb_blk_put(rt_rbb_blk_t block);\nrt_rbb_blk_t rt_rbb_blk_get(rt_rbb_t rbb);\nrt_size_t rt_rbb_blk_size(rt_rbb_blk_t block);\nrt_uint8_t *rt_rbb_blk_buf(rt_rbb_blk_t block);\nvoid rt_rbb_blk_free(rt_rbb_t rbb, rt_rbb_blk_t block);\n\n/* rbb block queue API */\nrt_size_t rt_rbb_blk_queue_get(rt_rbb_t rbb, rt_size_t queue_data_len, rt_rbb_blk_queue_t blk_queue);\nrt_size_t rt_rbb_blk_queue_len(rt_rbb_blk_queue_t blk_queue);\nrt_uint8_t *rt_rbb_blk_queue_buf(rt_rbb_blk_queue_t blk_queue);\nvoid rt_rbb_blk_queue_free(rt_rbb_t rbb, rt_rbb_blk_queue_t blk_queue);\nrt_size_t rt_rbb_next_blk_queue_len(rt_rbb_t rbb);\n\n\n#ifdef __cplusplus\n}\n#endif\n\n#endif /* _RINGBLK_BUF_H_ */\n", "components/drivers/include/ipc/ringbuffer.h": "/*\n * Copyright (c) 2006-2018, RT-Thread Development Team\n *\n * SPDX-License-Identifier: Apache-2.0\n *\n * Change Logs:\n * Date Author Notes\n */\n#ifndef RINGBUFFER_H__\n#define RINGBUFFER_H__\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n#include <rtthread.h>\n\n/* ring buffer */\nstruct rt_ringbuffer\n{\n rt_uint8_t *buffer_ptr;\n /* use the msb of the {read,write}_index as mirror bit. You can see this as\n * if the buffer adds a virtual mirror and the pointers point either to the\n * normal or to the mirrored buffer. If the write_index has the same value\n * with the read_index, but in a different mirror, the buffer is full.\n * While if the write_index and the read_index are the same and within the\n * same mirror, the buffer is empty. The ASCII art of the ringbuffer is:\n *\n * mirror = 0 mirror = 1\n * +---+---+---+---+---+---+---+|+~~~+~~~+~~~+~~~+~~~+~~~+~~~+\n * | 0 | 1 | 2 | 3 | 4 | 5 | 6 ||| 0 | 1 | 2 | 3 | 4 | 5 | 6 | Full\n * +---+---+---+---+---+---+---+|+~~~+~~~+~~~+~~~+~~~+~~~+~~~+\n * read_idx-^ write_idx-^\n *\n * +---+---+---+---+---+---+---+|+~~~+~~~+~~~+~~~+~~~+~~~+~~~+\n * | 0 | 1 | 2 | 3 | 4 | 5 | 6 ||| 0 | 1 | 2 | 3 | 4 | 5 | 6 | Empty\n * +---+---+---+---+---+---+---+|+~~~+~~~+~~~+~~~+~~~+~~~+~~~+\n * read_idx-^ ^-write_idx\n *\n * The tradeoff is we could only use 32KiB of buffer for 16 bit of index.\n * But it should be enough for most of the cases.\n *\n * Ref: http://en.wikipedia.org/wiki/Circular_buffer#Mirroring */\n rt_uint16_t read_mirror : 1;\n rt_uint16_t read_index : 15;\n rt_uint16_t write_mirror : 1;\n rt_uint16_t write_index : 15;\n /* as we use msb of index as mirror bit, the size should be signed and\n * could only be positive. */\n rt_int16_t buffer_size;\n};\n\nenum rt_ringbuffer_state\n{\n RT_RINGBUFFER_EMPTY,\n RT_RINGBUFFER_FULL,\n /* half full is neither full nor empty */\n RT_RINGBUFFER_HALFFULL,\n};\n\n/**\n * RingBuffer for DeviceDriver\n *\n * Please note that the ring buffer implementation of RT-Thread\n * has no thread wait or resume feature.\n */\nvoid rt_ringbuffer_init(struct rt_ringbuffer *rb, rt_uint8_t *pool, rt_int16_t size);\nvoid rt_ringbuffer_reset(struct rt_ringbuffer *rb);\nrt_size_t rt_ringbuffer_put(struct rt_ringbuffer *rb, const rt_uint8_t *ptr, rt_uint16_t length);\nrt_size_t rt_ringbuffer_put_force(struct rt_ringbuffer *rb, const rt_uint8_t *ptr, rt_uint16_t length);\nrt_size_t rt_ringbuffer_putchar(struct rt_ringbuffer *rb, const rt_uint8_t ch);\nrt_size_t rt_ringbuffer_putchar_force(struct rt_ringbuffer *rb, const rt_uint8_t ch);\nrt_size_t rt_ringbuffer_get(struct rt_ringbuffer *rb, rt_uint8_t *ptr, rt_uint16_t length);\nrt_size_t rt_ringbuffer_getchar(struct rt_ringbuffer *rb, rt_uint8_t *ch);\nrt_size_t rt_ringbuffer_data_len(struct rt_ringbuffer *rb);\n\n#ifdef RT_USING_HEAP\nstruct rt_ringbuffer* rt_ringbuffer_create(rt_uint16_t length);\nvoid rt_ringbuffer_destroy(struct rt_ringbuffer *rb);\n#endif\n\nrt_inline rt_uint16_t rt_ringbuffer_get_size(struct rt_ringbuffer *rb)\n{\n RT_ASSERT(rb != RT_NULL);\n return rb->buffer_size;\n}\n\n/** return the size of empty space in rb */\n#define rt_ringbuffer_space_len(rb) ((rb)->buffer_size - rt_ringbuffer_data_len(rb))\n\n\n#ifdef __cplusplus\n}\n#endif\n\n#endif\n", "components/drivers/include/ipc/waitqueue.h": "/*\n * Copyright (c) 2006-2018, RT-Thread Development Team\n *\n * SPDX-License-Identifier: Apache-2.0\n *\n * Change Logs:\n * Date Author Notes\n * 2018/06/26 Bernard Fix the wait queue issue when wakeup a soon \n * to blocked thread.\n */\n\n#ifndef WAITQUEUE_H__\n#define WAITQUEUE_H__\n\n#include <rtthread.h>\n\n#define RT_WQ_FLAG_CLEAN 0x00\n#define RT_WQ_FLAG_WAKEUP 0x01\n\nstruct rt_wqueue_node;\ntypedef int (*rt_wqueue_func_t)(struct rt_wqueue_node *wait, void *key);\n\nstruct rt_wqueue_node\n{\n rt_thread_t polling_thread;\n rt_list_t list;\n\n rt_wqueue_func_t wakeup;\n rt_uint32_t key;\n};\ntypedef struct rt_wqueue_node rt_wqueue_node_t;\n\nint __wqueue_default_wake(struct rt_wqueue_node *wait, void *key);\n\nrt_inline void rt_wqueue_init(rt_wqueue_t *queue)\n{\n RT_ASSERT(queue != RT_NULL);\n\n queue->flag = RT_WQ_FLAG_CLEAN;\n rt_list_init(&(queue->waiting_list));\n}\n\nvoid rt_wqueue_add(rt_wqueue_t *queue, struct rt_wqueue_node *node);\nvoid rt_wqueue_remove(struct rt_wqueue_node *node);\nint rt_wqueue_wait(rt_wqueue_t *queue, int condition, int timeout);\nvoid rt_wqueue_wakeup(rt_wqueue_t *queue, void *key);\n\n#define DEFINE_WAIT_FUNC(name, function) \\\n struct rt_wqueue_node name = { \\\n rt_current_thread, \\\n RT_LIST_OBJECT_INIT(((name).list)), \\\n \\\n function, \\\n 0 \\\n }\n\n#define DEFINE_WAIT(name) DEFINE_WAIT_FUNC(name, __wqueue_default_wake)\n\n#endif\n", "components/drivers/include/ipc/workqueue.h": "/*\n * Copyright (c) 2006-2018, RT-Thread Development Team\n *\n * SPDX-License-Identifier: Apache-2.0\n *\n * Change Logs:\n * Date Author Notes\n */\n#ifndef WORKQUEUE_H__\n#define WORKQUEUE_H__\n\n#include <rtthread.h>\n\nenum\n{\n RT_WORK_STATE_PENDING = 0x0001, /* Work item pending state */\n RT_WORK_STATE_SUBMITTING = 0x0002, /* Work item submitting state */\n};\n\n/**\n * work type defitions\n */\nenum\n{\n RT_WORK_TYPE_DELAYED = 0x0001,\n};\n\n/* workqueue implementation */\nstruct rt_workqueue\n{\n rt_list_t work_list;\n struct rt_work *work_current; /* current work */\n\n struct rt_semaphore sem;\n rt_thread_t work_thread;\n};\n\nstruct rt_work\n{\n rt_list_t list;\n\n void (*work_func)(struct rt_work *work, void *work_data);\n void *work_data;\n rt_uint16_t flags;\n rt_uint16_t type;\n struct rt_timer timer;\n struct rt_workqueue *workqueue;\n};\n\nstruct rt_delayed_work\n{\n struct rt_work work;\n};\n\n#ifdef RT_USING_HEAP\n/**\n * WorkQueue for DeviceDriver\n */\nstruct rt_workqueue *rt_workqueue_create(const char *name, rt_uint16_t stack_size, rt_uint8_t priority);\nrt_err_t rt_workqueue_destroy(struct rt_workqueue *queue);\nrt_err_t rt_workqueue_dowork(struct rt_workqueue *queue, struct rt_work *work);\nrt_err_t rt_workqueue_submit_work(struct rt_workqueue *queue, struct rt_work *work, rt_tick_t time);\nrt_err_t rt_workqueue_cancel_work(struct rt_workqueue *queue, struct rt_work *work);\nrt_err_t rt_workqueue_cancel_work_sync(struct rt_workqueue *queue, struct rt_work *work);\n\n#ifdef RT_USING_SYSTEM_WORKQUEUE\nrt_err_t rt_work_submit(struct rt_work *work, rt_tick_t time);\nrt_err_t rt_work_cancel(struct rt_work *work);\n#endif\n\nrt_inline void rt_work_init(struct rt_work *work, void (*work_func)(struct rt_work *work, void *work_data),\n void *work_data)\n{\n rt_list_init(&(work->list));\n work->work_func = work_func;\n work->work_data = work_data;\n work->workqueue = RT_NULL;\n work->flags = 0;\n work->type = 0;\n}\n\nvoid rt_delayed_work_init(struct rt_delayed_work *work, void (*work_func)(struct rt_work *work,\n void *work_data), void *work_data);\n\nint rt_work_sys_workqueue_init(void);\n#endif\n\n#endif\n", "components/drivers/include/rtdevice.h": "/*\n * Copyright (c) 2006-2018, RT-Thread Development Team\n *\n * SPDX-License-Identifier: Apache-2.0\n *\n * Change Logs:\n * Date Author Notes\n * 2012-01-08 bernard first version.\n * 2014-07-12 bernard Add workqueue implementation.\n */\n\n#ifndef __RT_DEVICE_H__\n#define __RT_DEVICE_H__\n\n#include <rtthread.h>\n\n#include \"ipc/ringbuffer.h\"\n#include \"ipc/completion.h\"\n#include \"ipc/dataqueue.h\"\n#include \"ipc/workqueue.h\"\n#include \"ipc/waitqueue.h\"\n#include \"ipc/pipe.h\"\n#include \"ipc/poll.h\"\n#include \"ipc/ringblk_buf.h\"\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n#define RT_DEVICE(device) ((rt_device_t)device)\n\n#ifdef RT_USING_RTC\n#include \"drivers/rtc.h\"\n#ifdef RT_USING_ALARM\n#include \"drivers/alarm.h\"\n#endif\n#endif /* RT_USING_RTC */\n\n#ifdef RT_USING_SPI\n#include \"drivers/spi.h\"\n#endif /* RT_USING_SPI */\n\n#ifdef RT_USING_MTD_NOR\n#include \"drivers/mtd_nor.h\"\n#endif /* RT_USING_MTD_NOR */\n\n#ifdef RT_USING_MTD_NAND\n#include \"drivers/mtd_nand.h\"\n#endif /* RT_USING_MTD_NAND */\n\n#ifdef RT_USING_USB_DEVICE\n#include \"drivers/usb_device.h\"\n#endif /* RT_USING_USB_DEVICE */\n\n#ifdef RT_USING_USB_HOST\n#include \"drivers/usb_host.h\"\n#endif /* RT_USING_USB_HOST */\n\n#ifdef RT_USING_SERIAL\n#include \"drivers/serial.h\"\n#endif /* RT_USING_SERIAL */\n\n#ifdef RT_USING_I2C\n#include \"drivers/i2c.h\"\n#include \"drivers/i2c_dev.h\"\n\n#ifdef RT_USING_I2C_BITOPS\n#include \"drivers/i2c-bit-ops.h\"\n#endif /* RT_USING_I2C_BITOPS */\n#endif /* RT_USING_I2C */\n\n#ifdef RT_USING_SDIO\n#include \"drivers/mmcsd_core.h\"\n#include \"drivers/sd.h\"\n#include \"drivers/sdio.h\"\n#endif\n\n#ifdef RT_USING_WDT\n#include \"drivers/watchdog.h\"\n#endif\n\n#ifdef RT_USING_PIN\n#include \"drivers/pin.h\"\n#endif\n\n#ifdef RT_USING_CAN\n#include \"drivers/can.h\"\n#endif\n\n#ifdef RT_USING_HWTIMER\n#include \"drivers/hwtimer.h\"\n#endif\n\n#ifdef RT_USING_AUDIO\n#include \"drivers/audio.h\"\n#endif\n\n#ifdef RT_USING_CPUTIME\n#include \"drivers/cputime.h\"\n#endif\n\n#ifdef RT_USING_ADC\n#include \"drivers/adc.h\"\n#endif\n\n#ifdef RT_USING_DAC\n#include \"drivers/dac.h\"\n#endif\n\n#ifdef RT_USING_PWM\n#include \"drivers/rt_drv_pwm.h\"\n#endif\n\n#ifdef RT_USING_PM\n#include \"drivers/pm.h\"\n#endif\n\n#ifdef RT_USING_WIFI\n#include \"drivers/wlan.h\"\n#endif\n\n#ifdef MTD_USING_NOR\n#include \"drivers/mtdnor.h\"\n#endif\n#ifdef MTD_USING_NAND\n#include \"drivers/mtdnand.h\"\n#endif\n\n#ifdef RT_USING_HWCRYPTO\n#include \"drivers/crypto.h\"\n#endif\n\n#ifdef RT_USING_PULSE_ENCODER\n#include \"drivers/pulse_encoder.h\"\n#endif\n\n#ifdef RT_USING_INPUT_CAPTURE\n#include \"drivers/rt_inputcapture.h\"\n#endif\n\n#ifdef __cplusplus\n}\n#endif\n\n#endif /* __RT_DEVICE_H__ */\n", "docs/queue-contract.md": "# 帧描述符队列合同\n\n本工作区使用 RT-Thread 的设备驱动数据队列和内核任务 API。下列条款是帧搬运服务在本次修复中依赖的接口合同,整理自队列公开声明和驱动实现。头文件给出参数类型,`include/rtdef.h` 给出错误码常量。\n\n**R01** 服务在任务上下文中使用已初始化的队列,容量为 1 到 1024,低水位在 0 到容量之间,事件回调传入 NULL;同一队列的 API 调用由任务串行执行,不涉及 ISR、SMP 或异步硬件时钟。\n\n**R02** `rt_data_queue_init(queue, capacity, low_watermark, NULL)` 在分配成功时返回 RT_EOK,建立一个空队列;释放后可以再次初始化该队列对象。\n\n**R03** `rt_data_queue_push` 成功返回 RT_EOK,并在队尾保存调用者给出的指针与长度;队列不复制或释放指针指向的数据,调用者在描述符使用期间保持数据有效,长度可以为零。\n\n**R04** `rt_data_queue_pop` 成功返回 RT_EOK,按成功入队的先后顺序取出一个指针与长度,且移除该项;任何有限长度的合法操作序列都保持这一语义。\n\n**R05** 满队列的 push 和空队列的 pop 在 timeout 为零时返回 -RT_ETIMEOUT,不改变队列已有项。\n\n**R06** `rt_data_queue_peak` 在队列非空时返回 RT_EOK 和队首指针、长度,且不移除该项;空队列返回 -RT_EEMPTY。\n\n**R07** `rt_data_queue_reset` 丢弃当前所有队列项,使队列为空并可继续使用;正在该队列上等待的 push 和 pop 任务以 -RT_ERROR 结束本次操作,已经取消的等待不会被旧定时器再次完成。\n\n**R08** 非零 timeout 允许任务等待:空队列的 pop 等待可用项;满队列的 push 等待占用量达到低水位或更低。满足条件后等待操作正常完成;正 timeout 在相应数量的内核 tick 后仍未满足条件时返回 -RT_ETIMEOUT。负 timeout 表示一直等待。\n\n**R09** `rt_data_queue_deinit` 以 -RT_ERROR 结束队列上的等待操作并释放队列存储,返回 RT_EOK;调用者之后只能重新初始化该对象,不能使用已释放队列。\n\n**R10** `rt_data_queue_len(queue)` 返回当前成功入队且尚未取出的项数,包括零和容量值;查询不改变队列项。\n\n调用者为输出参数提供有效地址,不依赖队列结构的字段、索引宽度、宏或内存布局。回调事件的次数与任务之间无关操作的次序不属于本场景交付标准。内核 tick 由软件显式推进,不以墙上时间判断超时。\n", "host/port.c": "/* SPDX-License-Identifier: Apache-2.0\n * User-space CPU context, interrupt-mask, console and heap support. */\n#define _XOPEN_SOURCE 700\n#include <ucontext.h>\n#include <stdlib.h>\n#include <stdio.h>\n#include <stdint.h>\n#include <rtthread.h>\n#include <rthw.h>\nstruct host_context { ucontext_t ctx; void (*entry)(void *); void *arg; void (*finish)(void); };\nstatic rt_base_t irq_mask;\nstatic void launch(uintptr_t raw) {\n struct host_context *c = (void *)raw;\n c->entry(c->arg);\n c->finish();\n abort();\n}\nrt_uint8_t *rt_hw_stack_init(void *entry, void *arg, rt_uint8_t *top, void *finish) {\n struct host_context *c = calloc(1, sizeof(*c));\n (void)top;\n if (!c) abort();\n c->entry = entry; c->arg = arg; c->finish = finish;\n if (getcontext(&c->ctx)) abort();\n c->ctx.uc_stack.ss_sp = malloc(131072);\n c->ctx.uc_stack.ss_size = 131072;\n c->ctx.uc_link = NULL;\n if (!c->ctx.uc_stack.ss_sp) abort();\n makecontext(&c->ctx, (void (*)(void))launch, 1, (uintptr_t)c);\n return (void *)c;\n}\nvoid rt_hw_context_switch(rt_ubase_t from, rt_ubase_t to) {\n struct host_context *a = *(void **)from, *b = *(void **)to;\n if (swapcontext(&a->ctx, &b->ctx)) abort();\n}\nvoid rt_hw_context_switch_to(rt_ubase_t to) {\n struct host_context *b = *(void **)to;\n if (setcontext(&b->ctx)) abort();\n}\nvoid rt_hw_context_switch_interrupt(rt_ubase_t from, rt_ubase_t to) { rt_hw_context_switch(from,to); }\nrt_base_t rt_hw_interrupt_disable(void) { rt_base_t old=irq_mask; irq_mask=1; return old; }\nvoid rt_hw_interrupt_enable(rt_base_t level) { irq_mask=level; }\nvoid rt_hw_console_output(const char *s) { fputs(s,stderr); }\nvoid *rt_malloc(rt_size_t n) { return malloc(n); }\nvoid rt_free(void *p) { free(p); }\nvoid *rt_realloc(void *p, rt_size_t n) { return realloc(p,n); }\nvoid *rt_calloc(rt_size_t a, rt_size_t b) { return calloc(a,b); }\n", "host/smoke.c": "/* SPDX-License-Identifier: Apache-2.0 */\n#include <stdio.h>\n#include <stdlib.h>\n#include <rtthread.h>\n#include <rtdevice.h>\nstatic struct rt_thread service;\nstatic unsigned char stack[65536];\nstatic int payload=42;\nstatic void run(void *unused) {\n struct rt_data_queue queue={0};\n const void *p=NULL;rt_size_t n=0;\n (void)unused;\n printf(\"init=%ld\\n\",rt_data_queue_init(&queue,4,0,NULL));\n printf(\"push=%ld\\n\",rt_data_queue_push(&queue,&payload,sizeof(payload),0));\n printf(\"peak=%ld\\n\",rt_data_queue_peak(&queue,&p,&n));\n printf(\"pop=%ld\\n\",rt_data_queue_pop(&queue,&p,&n,0));\n printf(\"received=%d size=%lu\\n\",p==&payload,(unsigned long)n);\n printf(\"deinit=%ld\\n\",rt_data_queue_deinit(&queue));\n exit(0);\n}\nint main(void) {\n rt_system_timer_init();rt_system_scheduler_init();rt_thread_idle_init();\n rt_thread_init(&service,\"service\",run,NULL,stack,sizeof(stack),10,100);\n rt_thread_startup(&service);rt_system_scheduler_start();return 0;\n}\n", "include/libc/libc_dirent.h": "/*\n * Copyright (c) 2006-2018, RT-Thread Development Team\n *\n * SPDX-License-Identifier: Apache-2.0\n */\n\n#ifndef LIBC_DIRENT_H__\n#define LIBC_DIRENT_H__\n\n#define DT_UNKNOWN 0x00\n#define DT_REG 0x01\n#define DT_DIR 0x02\n\n#endif\n", "include/libc/libc_errno.h": "/*\n * Copyright (c) 2006-2018, RT-Thread Development Team\n *\n * SPDX-License-Identifier: Apache-2.0\n *\n * Change Logs:\n * Date Author Notes\n * 2016-11-12 Bernard The first version\n */\n\n#ifndef LIBC_ERRNO_H__\n#define LIBC_ERRNO_H__\n\n#include <rtconfig.h>\n\n#if defined(RT_USING_NEWLIB) || defined(_WIN32) || defined( __GNUC__ )\n/* use errno.h file in toolchains */\n#include <errno.h>\n#endif\n\n#if defined(__CC_ARM)\n/*\ndefined in armcc/errno.h\n\n#define EDOM 1\n#define ERANGE 2\n#define EILSEQ 4\n#define ESIGNUM 3\n#define EINVAL 5\n#define ENOMEM 6\n*/\n#define ERROR_BASE_NO 7\n\n#elif defined(__IAR_SYSTEMS_ICC__)\n/* defined in iar/errno.h\n#define EDOM 33\n#define ERANGE 34\n#define EFPOS 35\n#define EILSEQ 36\n*/\n#define ERROR_BASE_NO 36\n\n#else\n\n#define ERROR_BASE_NO 0\n#endif\n\n#if !defined(RT_USING_NEWLIB) && !defined(_WIN32) && !defined(__GNUC__ )\n\n#define EPERM (ERROR_BASE_NO + 1)\n#define ENOENT (ERROR_BASE_NO + 2)\n#define ESRCH (ERROR_BASE_NO + 3)\n#define EINTR (ERROR_BASE_NO + 4)\n#define EIO (ERROR_BASE_NO + 5)\n#define ENXIO (ERROR_BASE_NO + 6)\n#define E2BIG (ERROR_BASE_NO + 7)\n#define ENOEXEC (ERROR_BASE_NO + 8)\n#define EBADF (ERROR_BASE_NO + 9)\n#define ECHILD (ERROR_BASE_NO + 10)\n#define EAGAIN (ERROR_BASE_NO + 11)\n\n#ifndef ENOMEM\n#define ENOMEM (ERROR_BASE_NO + 12)\n#endif\n\n#define EACCES (ERROR_BASE_NO + 13)\n#define EFAULT (ERROR_BASE_NO + 14)\n#define ENOTBLK (ERROR_BASE_NO + 15)\n#define EBUSY (ERROR_BASE_NO + 16)\n#define EEXIST (ERROR_BASE_NO + 17)\n#define EXDEV (ERROR_BASE_NO + 18)\n#define ENODEV (ERROR_BASE_NO + 19)\n#define ENOTDIR (ERROR_BASE_NO + 20)\n#define EISDIR (ERROR_BASE_NO + 21)\n\n#ifndef EINVAL\n#define EINVAL (ERROR_BASE_NO + 22)\n#endif\n\n#define ENFILE (ERROR_BASE_NO + 23)\n#define EMFILE (ERROR_BASE_NO + 24)\n#define ENOTTY (ERROR_BASE_NO + 25)\n#define ETXTBSY (ERROR_BASE_NO + 26)\n#define EFBIG (ERROR_BASE_NO + 27)\n#define ENOSPC (ERROR_BASE_NO + 28)\n#define ESPIPE (ERROR_BASE_NO + 29)\n#define EROFS (ERROR_BASE_NO + 30)\n#define EMLINK (ERROR_BASE_NO + 31)\n#define EPIPE (ERROR_BASE_NO + 32)\n\n#ifndef EDOM\n#define EDOM (ERROR_BASE_NO + 33)\n#endif\n\n#ifndef ERANGE\n#define ERANGE (ERROR_BASE_NO + 34)\n#endif\n\n#define EDEADLK (ERROR_BASE_NO + 35)\n#define ENAMETOOLONG (ERROR_BASE_NO + 36)\n#define ENOLCK (ERROR_BASE_NO + 37)\n#define ENOSYS (ERROR_BASE_NO + 38)\n#define ENOTEMPTY (ERROR_BASE_NO + 39)\n#define ELOOP (ERROR_BASE_NO + 40)\n#define EWOULDBLOCK EAGAIN\n#define ENOMSG (ERROR_BASE_NO + 42)\n#define EIDRM (ERROR_BASE_NO + 43)\n#define ECHRNG (ERROR_BASE_NO + 44)\n#define EL2NSYNC (ERROR_BASE_NO + 45)\n#define EL3HLT (ERROR_BASE_NO + 46)\n#define EL3RST (ERROR_BASE_NO + 47)\n#define ELNRNG (ERROR_BASE_NO + 48)\n#define EUNATCH (ERROR_BASE_NO + 49)\n#define ENOCSI (ERROR_BASE_NO + 50)\n#define EL2HLT (ERROR_BASE_NO + 51)\n#define EBADE (ERROR_BASE_NO + 52)\n#define EBADR (ERROR_BASE_NO + 53)\n#define EXFULL (ERROR_BASE_NO + 54)\n#define ENOANO (ERROR_BASE_NO + 55)\n#define EBADRQC (ERROR_BASE_NO + 56)\n#define EBADSLT (ERROR_BASE_NO + 57)\n#define EDEADLOCK EDEADLK\n#define EBFONT (ERROR_BASE_NO + 59)\n#define ENOSTR (ERROR_BASE_NO + 60)\n#define ENODATA (ERROR_BASE_NO + 61)\n#define ETIME (ERROR_BASE_NO + 62)\n#define ENOSR (ERROR_BASE_NO + 63)\n#define ENONET (ERROR_BASE_NO + 64)\n#define ENOPKG (ERROR_BASE_NO + 65)\n#define EREMOTE (ERROR_BASE_NO + 66)\n#define ENOLINK (ERROR_BASE_NO + 67)\n#define EADV (ERROR_BASE_NO + 68)\n#define ESRMNT (ERROR_BASE_NO + 69)\n#define ECOMM (ERROR_BASE_NO + 70)\n#define EPROTO (ERROR_BASE_NO + 71)\n#define EMULTIHOP (ERROR_BASE_NO + 72)\n#define EDOTDOT (ERROR_BASE_NO + 73)\n#define EBADMSG (ERROR_BASE_NO + 74)\n#define EOVERFLOW (ERROR_BASE_NO + 75)\n#define ENOTUNIQ (ERROR_BASE_NO + 76)\n#define EBADFD (ERROR_BASE_NO + 77)\n#define EREMCHG (ERROR_BASE_NO + 78)\n#define ELIBACC (ERROR_BASE_NO + 79)\n#define ELIBBAD (ERROR_BASE_NO + 80)\n#define ELIBSCN (ERROR_BASE_NO + 81)\n#define ELIBMAX (ERROR_BASE_NO + 82)\n#define ELIBEXEC (ERROR_BASE_NO + 83)\n\n#ifndef EILSEQ\n#define EILSEQ (ERROR_BASE_NO + 84)\n#endif\n\n#define ERESTART (ERROR_BASE_NO + 85)\n#define ESTRPIPE (ERROR_BASE_NO + 86)\n#define EUSERS (ERROR_BASE_NO + 87)\n#define ENOTSOCK (ERROR_BASE_NO + 88)\n#define EDESTADDRREQ (ERROR_BASE_NO + 89)\n#define EMSGSIZE (ERROR_BASE_NO + 90)\n#define EPROTOTYPE (ERROR_BASE_NO + 91)\n#define ENOPROTOOPT (ERROR_BASE_NO + 92)\n#define EPROTONOSUPPORT (ERROR_BASE_NO + 93)\n#define ESOCKTNOSUPPORT (ERROR_BASE_NO + 94)\n#define EOPNOTSUPP (ERROR_BASE_NO + 95)\n#define ENOTSUP EOPNOTSUPP \n#define EPFNOSUPPORT (ERROR_BASE_NO + 96)\n#define EAFNOSUPPORT (ERROR_BASE_NO + 97)\n#define EADDRINUSE (ERROR_BASE_NO + 98)\n#define EADDRNOTAVAIL (ERROR_BASE_NO + 99)\n#define ENETDOWN (ERROR_BASE_NO + 100)\n#define ENETUNREACH (ERROR_BASE_NO + 101)\n#define ENETRESET (ERROR_BASE_NO + 102)\n#define ECONNABORTED (ERROR_BASE_NO + 103)\n#define ECONNRESET (ERROR_BASE_NO + 104)\n#define ENOBUFS (ERROR_BASE_NO + 105)\n#define EISCONN (ERROR_BASE_NO + 106)\n#define ENOTCONN (ERROR_BASE_NO + 107)\n#define ESHUTDOWN (ERROR_BASE_NO + 108)\n#define ETOOMANYREFS (ERROR_BASE_NO + 109)\n#define ETIMEDOUT (ERROR_BASE_NO + 110)\n#define ECONNREFUSED (ERROR_BASE_NO + 111)\n#define EHOSTDOWN (ERROR_BASE_NO + 112)\n#define EHOSTUNREACH (ERROR_BASE_NO + 113)\n#define EALREADY (ERROR_BASE_NO + 114)\n#define EINPROGRESS (ERROR_BASE_NO + 115)\n#define ESTALE (ERROR_BASE_NO + 116)\n#define EUCLEAN (ERROR_BASE_NO + 117)\n#define ENOTNAM (ERROR_BASE_NO + 118)\n#define ENAVAIL (ERROR_BASE_NO + 119)\n#define EISNAM (ERROR_BASE_NO + 120)\n#define EREMOTEIO (ERROR_BASE_NO + 121)\n#define EDQUOT (ERROR_BASE_NO + 122)\n#define ENOMEDIUM (ERROR_BASE_NO + 123)\n#define EMEDIUMTYPE (ERROR_BASE_NO + 124)\n#define ECANCELED (ERROR_BASE_NO + 125)\n#define ENOKEY (ERROR_BASE_NO + 126)\n#define EKEYEXPIRED (ERROR_BASE_NO + 127)\n#define EKEYREVOKED (ERROR_BASE_NO + 128)\n#define EKEYREJECTED (ERROR_BASE_NO + 129)\n#define EOWNERDEAD (ERROR_BASE_NO + 130)\n#define ENOTRECOVERABLE (ERROR_BASE_NO + 131)\n#define ERFKILL (ERROR_BASE_NO + 132)\n#define EHWPOISON (ERROR_BASE_NO + 133)\n\n#endif\n\n#endif\n", "include/libc/libc_fcntl.h": "/*\n * Copyright (c) 2006-2018, RT-Thread Development Team\n *\n * SPDX-License-Identifier: Apache-2.0\n *\n * Change Logs:\n * Date Author Notes\n * 2018-02-07 Bernard Add O_DIRECTORY definition in NEWLIB mode.\n * 2018-02-09 Bernard Add O_BINARY definition\n */\n\n#ifndef LIBC_FCNTL_H__\n#define LIBC_FCNTL_H__\n\n#if defined(RT_USING_NEWLIB) || defined(_WIN32) || defined( __GNUC__ )\n#include <fcntl.h>\n\n#ifndef O_NONBLOCK\n#define O_NONBLOCK 0x4000\n#endif\n\n#if defined(_WIN32)\n#define O_ACCMODE (_O_RDONLY | _O_WRONLY | _O_RDWR)\n#endif\n\n#ifndef F_GETFL\n#define F_GETFL 3\n#endif\n#ifndef F_SETFL\n#define F_SETFL 4\n#endif\n\n#ifndef O_DIRECTORY\n#define O_DIRECTORY 0x200000\n#endif\n\n#ifndef O_BINARY\n#ifdef _O_BINARY\n#define O_BINARY _O_BINARY\n#else\n#define O_BINARY\t 0\n#endif\n#endif\n\n#else\n#define O_RDONLY 00\n#define O_WRONLY 01\n#define O_RDWR 02\n\n#define O_CREAT 0100\n#define O_EXCL 0200\n#define O_NOCTTY 0400\n#define O_TRUNC 01000\n#define O_APPEND 02000\n#define O_NONBLOCK 04000\n#define O_DSYNC 010000\n#define O_SYNC 04010000\n#define O_RSYNC 04010000\n#define O_BINARY 0100000\n#define O_DIRECTORY 0200000\n#define O_NOFOLLOW 0400000\n#define O_CLOEXEC 02000000\n\n#define O_ASYNC 020000\n#define O_DIRECT 040000\n#define O_LARGEFILE 0100000\n#define O_NOATIME 01000000\n#define O_PATH 010000000\n#define O_TMPFILE 020200000\n#define O_NDELAY O_NONBLOCK\n\n#define O_SEARCH O_PATH\n#define O_EXEC O_PATH\n\n#define O_ACCMODE (03|O_SEARCH)\n\n#define F_DUPFD 0\n#define F_GETFD 1\n#define F_SETFD 2\n#define F_GETFL 3\n#define F_SETFL 4\n\n#define F_SETOWN 8\n#define F_GETOWN 9\n#define F_SETSIG 10\n#define F_GETSIG 11\n\n#define F_GETLK 12\n#define F_SETLK 13\n#define F_SETLKW 14\n\n#define F_SETOWN_EX 15\n#define F_GETOWN_EX 16\n\n#define F_GETOWNER_UIDS 17\n#endif\n\n#endif\n", "include/libc/libc_fdset.h": "/*\n * Copyright (c) 2006-2018, RT-Thread Development Team\n *\n * SPDX-License-Identifier: Apache-2.0\n *\n * Change Logs:\n * Date Author Notes\n * 2017-10-30 Bernard The first version\n */\n\n#ifndef LIBC_FDSET_H__\n#define LIBC_FDSET_H__\n\n#include <rtconfig.h>\n\n#if defined(RT_USING_NEWLIB) || defined(_WIN32) || defined( __GNUC__ )\n#include <sys/types.h>\n#if defined(HAVE_SYS_SELECT_H)\n#include <sys/select.h>\n#endif\n\n#else\n\n#ifdef SAL_USING_POSIX\n\n#ifdef FD_SETSIZE\n#undef FD_SETSIZE\n#endif\n\n#define FD_SETSIZE DFS_FD_MAX\n#endif\n\n# ifndef FD_SETSIZE\n# define FD_SETSIZE 32\n# endif\n\n# define NBBY 8 /* number of bits in a byte */\n\ntypedef long fd_mask;\n# define NFDBITS (sizeof (fd_mask) * NBBY) /* bits per mask */\n# ifndef howmany\n# define howmany(x,y) (((x)+((y)-1))/(y))\n# endif\n\n/* We use a macro for fd_set so that including Sockets.h afterwards\n can work. */\ntypedef struct _types_fd_set {\n fd_mask fds_bits[howmany(FD_SETSIZE, NFDBITS)];\n} _types_fd_set;\n\n#define fd_set _types_fd_set\n\n# define FD_SET(n, p) ((p)->fds_bits[(n)/NFDBITS] |= (1L << ((n) % NFDBITS)))\n# define FD_CLR(n, p) ((p)->fds_bits[(n)/NFDBITS] &= ~(1L << ((n) % NFDBITS)))\n# define FD_ISSET(n, p) ((p)->fds_bits[(n)/NFDBITS] & (1L << ((n) % NFDBITS)))\n# define FD_ZERO(p) memset((void*)(p), 0, sizeof(*(p)))\n\n#endif\n\n#endif\n", "include/libc/libc_ioctl.h": "/*\n * Copyright (c) 2006-2018, RT-Thread Development Team\n *\n * SPDX-License-Identifier: Apache-2.0\n *\n * Change Logs:\n * Date Author Notes\n * 2017-01-21 Bernard the first version\n */\n\n#ifndef LIBC_IOCTL_H__\n#define LIBC_IOCTL_H__\n\n#define _IOC(a,b,c,d) ( ((a)<<30) | ((b)<<8) | (c) | ((d)<<16) )\n#define _IOC_NONE 0U\n#define _IOC_WRITE 1U\n#define _IOC_READ 2U\n\n#ifndef _WIN32\n#define _IO(a,b) _IOC(_IOC_NONE,(a),(b),0)\n#define _IOW(a,b,c) _IOC(_IOC_WRITE,(a),(b),sizeof(c))\n#define _IOR(a,b,c) _IOC(_IOC_READ,(a),(b),sizeof(c))\n#define _IOWR(a,b,c) _IOC(_IOC_READ|_IOC_WRITE,(a),(b),sizeof(c))\n\n#define FIONREAD _IOR('f', 127, int) /* get # bytes to read */\n#define FIONBIO _IOW('f', 126, int) /* set/clear non-blocking i/o */\n#define FIONWRITE _IOR('f', 121, int) /* get # bytes outstanding\n * in send queue. */\n#endif\n\n#define TCGETS\t\t0x5401\n#define TCSETS\t\t0x5402\n#define TCSETSW\t\t0x5403\n#define TCSETSF\t\t0x5404\n#define TCGETA\t\t0x5405\n#define TCSETA\t\t0x5406\n#define TCSETAW\t\t0x5407\n#define TCSETAF\t\t0x5408\n#define TCSBRK\t\t0x5409\n#define TCXONC\t\t0x540A\n#define TCFLSH\t\t0x540B\n#define TIOCEXCL\t0x540C\n#define TIOCNXCL\t0x540D\n#define TIOCSCTTY\t0x540E\n#define TIOCGPGRP\t0x540F\n#define TIOCSPGRP\t0x5410\n#define TIOCOUTQ\t0x5411\n#define TIOCSTI\t\t0x5412\n#define TIOCGWINSZ\t0x5413\n#define TIOCSWINSZ\t0x5414\n#define TIOCMGET\t0x5415\n#define TIOCMBIS\t0x5416\n#define TIOCMBIC\t0x5417\n#define TIOCMSET\t0x5418\n#define TIOCGSOFTCAR\t0x5419\n#define TIOCSSOFTCAR\t0x541A\n// #define FIONREAD\t0x541B\n#define TIOCINQ\t\tFIONREAD\n#define TIOCLINUX\t0x541C\n#define TIOCCONS\t0x541D\n#define TIOCGSERIAL\t0x541E\n#define TIOCSSERIAL\t0x541F\n#define TIOCPKT\t\t0x5420\n// #define FIONBIO\t\t0x5421\n#define TIOCNOTTY\t0x5422\n#define TIOCSETD\t0x5423\n#define TIOCGETD\t0x5424\n#define TCSBRKP\t\t0x5425\n#define TIOCSBRK\t0x5427\n#define TIOCCBRK\t0x5428\n#define TIOCGSID\t0x5429\n#define TIOCGRS485\t0x542E\n#define TIOCSRS485\t0x542F\n#define TIOCGPTN\t0x80045430\n#define TIOCSPTLCK\t0x40045431\n#define TIOCGDEV\t0x80045432\n#define TCGETX\t\t0x5432\n#define TCSETX\t\t0x5433\n#define TCSETXF\t\t0x5434\n#define TCSETXW\t\t0x5435\n#define TIOCSIG\t\t0x40045436\n#define TIOCVHANGUP\t0x5437\n#define TIOCGPKT\t0x80045438\n#define TIOCGPTLCK\t0x80045439\n#define TIOCGEXCL\t0x80045440\n\n#define FIONCLEX\t0x5450\n#define FIOCLEX\t\t0x5451\n\n#ifndef _WIN32\n#define FIOASYNC\t0x5452\n#endif\n\n#define TIOCSERCONFIG\t0x5453\n#define TIOCSERGWILD\t0x5454\n#define TIOCSERSWILD\t0x5455\n#define TIOCGLCKTRMIOS\t0x5456\n#define TIOCSLCKTRMIOS\t0x5457\n#define TIOCSERGSTRUCT\t0x5458\n#define TIOCSERGETLSR 0x5459\n#define TIOCSERGETMULTI 0x545A\n#define TIOCSERSETMULTI 0x545B\n\n#define TIOCMIWAIT\t0x545C\n#define TIOCGICOUNT\t0x545D\n#define FIOQSIZE\t0x5460\n\n#define TIOCPKT_DATA\t\t 0\n#define TIOCPKT_FLUSHREAD\t 1\n#define TIOCPKT_FLUSHWRITE\t 2\n#define TIOCPKT_STOP\t\t 4\n#define TIOCPKT_START\t\t 8\n#define TIOCPKT_NOSTOP\t\t16\n#define TIOCPKT_DOSTOP\t\t32\n#define TIOCPKT_IOCTL\t\t64\n\n#define TIOCSER_TEMT 0x01\n\nstruct winsize {\n\tunsigned short ws_row;\n\tunsigned short ws_col;\n\tunsigned short ws_xpixel;\n\tunsigned short ws_ypixel;\n};\n\n#define TIOCM_LE 0x001\n#define TIOCM_DTR 0x002\n#define TIOCM_RTS 0x004\n#define TIOCM_ST 0x008\n#define TIOCM_SR 0x010\n#define TIOCM_CTS 0x020\n#define TIOCM_CAR 0x040\n#define TIOCM_RNG 0x080\n#define TIOCM_DSR 0x100\n#define TIOCM_CD TIOCM_CAR\n#define TIOCM_RI TIOCM_RNG\n#define TIOCM_OUT1 0x2000\n#define TIOCM_OUT2 0x4000\n#define TIOCM_LOOP 0x8000\n\n#define N_TTY 0\n#define N_SLIP 1\n#define N_MOUSE 2\n#define N_PPP 3\n#define N_STRIP 4\n#define N_AX25 5\n#define N_X25 6\n#define N_6PACK 7\n#define N_MASC 8\n#define N_R3964 9\n#define N_PROFIBUS_FDL 10\n#define N_IRDA 11\n#define N_SMSBLOCK 12\n#define N_HDLC 13\n#define N_SYNC_PPP 14\n#define N_HCI 15\n\n#define FIOSETOWN 0x8901\n#define SIOCSPGRP 0x8902\n#define FIOGETOWN 0x8903\n#define SIOCGPGRP 0x8904\n// #define SIOCATMARK 0x8905\n#define SIOCGSTAMP 0x8906\n#define SIOCGSTAMPNS 0x8907\n\n#define SIOCADDRT 0x890B\n#define SIOCDELRT 0x890C\n#define SIOCRTMSG 0x890D\n\n#define SIOCGIFNAME 0x8910\n#define SIOCSIFLINK 0x8911\n#define SIOCGIFCONF 0x8912\n#define SIOCGIFFLAGS 0x8913\n#define SIOCSIFFLAGS 0x8914\n#define SIOCGIFADDR 0x8915\n#define SIOCSIFADDR 0x8916\n#define SIOCGIFDSTADDR 0x8917\n#define SIOCSIFDSTADDR 0x8918\n#define SIOCGIFBRDADDR 0x8919\n#define SIOCSIFBRDADDR 0x891a\n#define SIOCGIFNETMASK 0x891b\n#define SIOCSIFNETMASK 0x891c\n#define SIOCGIFMETRIC 0x891d\n#define SIOCSIFMETRIC 0x891e\n#define SIOCGIFMEM 0x891f\n#define SIOCSIFMEM 0x8920\n#define SIOCGIFMTU 0x8921\n#define SIOCSIFMTU 0x8922\n#define SIOCSIFNAME 0x8923\n#define SIOCSIFHWADDR 0x8924\n#define SIOCGIFENCAP 0x8925\n#define SIOCSIFENCAP 0x8926\n#define SIOCGIFHWADDR 0x8927\n#define SIOCGIFSLAVE 0x8929\n#define SIOCSIFSLAVE 0x8930\n#define SIOCADDMULTI 0x8931\n#define SIOCDELMULTI 0x8932\n#define SIOCGIFINDEX 0x8933\n#define SIOGIFINDEX SIOCGIFINDEX\n#define SIOCSIFPFLAGS 0x8934\n#define SIOCGIFPFLAGS 0x8935\n#define SIOCDIFADDR 0x8936\n#define SIOCSIFHWBROADCAST 0x8937\n#define SIOCGIFCOUNT 0x8938\n\n#define SIOCGIFBR 0x8940\n#define SIOCSIFBR 0x8941\n\n#define SIOCGIFTXQLEN 0x8942\n#define SIOCSIFTXQLEN 0x8943\n\n#define SIOCDARP 0x8953\n#define SIOCGARP 0x8954\n#define SIOCSARP 0x8955\n\n#define SIOCDRARP 0x8960\n#define SIOCGRARP 0x8961\n#define SIOCSRARP 0x8962\n\n#define SIOCGIFMAP 0x8970\n#define SIOCSIFMAP 0x8971\n\n#define SIOCADDDLCI 0x8980\n#define SIOCDELDLCI 0x8981\n\n#define SIOCDEVPRIVATE\t\t0x89F0\n#define SIOCPROTOPRIVATE\t0x89E0\n\n#endif\n\n", "include/libc/libc_limits.h": "/*\n * Copyright (c) 2006-2018, RT-Thread Development Team\n *\n * SPDX-License-Identifier: Apache-2.0\n *\n * Change Logs:\n * Date Author Notes\n * 2020-09-05 Meco Manthe first version\n */\n\n#ifndef LIBC_LIMITS_H__\n#define LIBC_LIMITS_H__\n\n#include <limits.h>\n#include <rtconfig.h>\n\n#ifdef RT_USING_POSIX\n\n#ifndef SSIZE_MAX\n# define SSIZE_MAX LONG_MAX\n#endif\n\n#endif\n\n#endif\n", "include/libc/libc_signal.h": "/*\n * Copyright (c) 2006-2018, RT-Thread Development Team\n *\n * SPDX-License-Identifier: Apache-2.0\n *\n * Change Logs:\n * Date Author Notes\n * 2017-09-12 Bernard The first version\n */\n\n#ifndef LIBC_SIGNAL_H__\n#define LIBC_SIGNAL_H__\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n#include <stdint.h>\n#ifdef HAVE_CCONFIG_H\n#include <cconfig.h>\n#endif\n\n#ifndef HAVE_SIGVAL\n/* Signal Generation and Delivery, P1003.1b-1993, p. 63\n NOTE: P1003.1c/D10, p. 34 adds sigev_notify_function and\n sigev_notify_attributes to the sigevent structure. */\n\nunion sigval\n{\n int sival_int; /* Integer signal value */\n void *sival_ptr; /* Pointer signal value */\n};\n#endif\n\n#ifndef HAVE_SIGEVENT\nstruct sigevent\n{\n int sigev_notify; /* Notification type */\n int sigev_signo; /* Signal number */\n union sigval sigev_value; /* Signal value */\n void (*sigev_notify_function)( union sigval );\n /* Notification function */\n void *sigev_notify_attributes; /* Notification Attributes, really pthread_attr_t */\n};\n#endif\n\n#ifndef HAVE_SIGINFO\nstruct siginfo\n{\n uint16_t si_signo;\n uint16_t si_code;\n\n union sigval si_value;\n};\ntypedef struct siginfo siginfo_t;\n#endif\n\n#define SI_USER 0x01 /* Signal sent by kill(). */\n#define SI_QUEUE 0x02 /* Signal sent by sigqueue(). */\n#define SI_TIMER 0x03 /* Signal generated by expiration of a \n timer set by timer_settime(). */\n#define SI_ASYNCIO 0x04 /* Signal generated by completion of an \n asynchronous I/O request. */\n#define SI_MESGQ 0x05 /* Signal generated by arrival of a \n message on an empty message queue. */\n\n#if !defined(RT_USING_NEWLIB)\ntypedef void (*_sig_func_ptr)(int);\ntypedef unsigned long sigset_t;\n#endif\n\n#include <signal.h>\n\n#if defined(__CC_ARM) || defined(__CLANG_ARM)\n\n#define SIGHUP 1\n/* #define SIGINT 2 */\n#define SIGQUIT 3\n/* #define SIGILL 4 */\n#define SIGTRAP 5\n/* #define SIGABRT 6 */\n#define SIGEMT 7\n/* #define SIGFPE 8 */\n#define SIGKILL 9\n#define SIGBUS 10\n/* #define SIGSEGV 11 */\n#define SIGSYS 12\n#define SIGPIPE 13\n#define SIGALRM 14\n/* #define SIGTERM 15 */\n#define SIGURG 16\n#define SIGSTOP 17\n#define SIGTSTP 18\n#define SIGCONT 19\n#define SIGCHLD 20\n#define SIGTTIN 21\n#define SIGTTOU 22\n#define SIGPOLL 23\n#define SIGWINCH 24\n/* #define SIGUSR1 25 */\n/* #define SIGUSR2 26 */\n#define SIGRTMIN 27\n#define SIGRTMAX 31\n#define NSIG 32\n\n#define SIG_SETMASK 0 /* set mask with sigprocmask() */\n#define SIG_BLOCK 1 /* set of signals to block */\n#define SIG_UNBLOCK 2 /* set of signals to, well, unblock */\n\nstruct sigaction\n{\n _sig_func_ptr sa_handler;\n sigset_t sa_mask;\n int sa_flags;\n};\n\n#define sigaddset(what,sig) (*(what) |= (1<<(sig)), 0)\n#define sigdelset(what,sig) (*(what) &= ~(1<<(sig)), 0)\n#define sigemptyset(what) (*(what) = 0, 0)\n#define sigfillset(what) (*(what) = ~(0), 0)\n#define sigismember(what,sig) (((*(what)) & (1<<(sig))) != 0)\n\nint sigprocmask (int how, const sigset_t *set, sigset_t *oset);\nint sigaction(int signum, const struct sigaction *act, struct sigaction *oldact);\n\n#elif defined(__IAR_SYSTEMS_ICC__)\n\n#define SIGHUP 1\n#define SIGINT 2\n#define SIGQUIT 3\n#define SIGILL 4\n#define SIGTRAP 5\n/* #define SIGABRT 6 */\n#define SIGEMT 7\n#define SIGFPE 8\n#define SIGKILL 9\n#define SIGBUS 10\n#define SIGSEGV 11\n#define SIGSYS 12\n#define SIGPIPE 13\n#define SIGALRM 14\n#define SIGTERM 15\n#define SIGURG 16\n#define SIGSTOP 17\n#define SIGTSTP 18\n#define SIGCONT 19\n#define SIGCHLD 20\n#define SIGTTIN 21\n#define SIGTTOU 22\n#define SIGPOLL 23\n#define SIGWINCH 24\n#define SIGUSR1 25\n#define SIGUSR2 26\n#define SIGRTMIN 27\n#define SIGRTMAX 31\n#define NSIG 32\n\n#define SIG_SETMASK 0 /* set mask with sigprocmask() */\n#define SIG_BLOCK 1 /* set of signals to block */\n#define SIG_UNBLOCK 2 /* set of signals to, well, unblock */\n\nstruct sigaction\n{\n _sig_func_ptr sa_handler;\n sigset_t sa_mask;\n int sa_flags;\n};\n\n#define sigaddset(what,sig) (*(what) |= (1<<(sig)), 0)\n#define sigdelset(what,sig) (*(what) &= ~(1<<(sig)), 0)\n#define sigemptyset(what) (*(what) = 0, 0)\n#define sigfillset(what) (*(what) = ~(0), 0)\n#define sigismember(what,sig) (((*(what)) & (1<<(sig))) != 0)\n\nint sigprocmask (int how, const sigset_t *set, sigset_t *oset);\nint sigaction(int signum, const struct sigaction *act, struct sigaction *oldact);\n#endif\n\n#ifdef __cplusplus\n}\n#endif\n\n#endif\n\n", "include/libc/libc_stat.h": "/*\n * Copyright (c) 2006-2018, RT-Thread Development Team\n *\n * SPDX-License-Identifier: Apache-2.0\n */\n\n#ifndef LIBC_STAT_H__\n#define LIBC_STAT_H__\n\n#include <rtconfig.h>\n\n#if defined(RT_USING_NEWLIB) || defined( __GNUC__ )\n/* use header file of newlib */\n#include <sys/stat.h>\n\n#elif defined(_WIN32)\n#include <sys/stat.h>\n\n#define S_IRWXU 00700\n#define S_IRUSR 00400\n#define S_IWUSR 00200\n#define S_IXUSR 00100\n\n#define S_IRWXG 00070\n#define S_IRGRP 00040\n#define S_IWGRP 00020\n#define S_IXGRP 00010\n\n#define S_IRWXO 00007\n#define S_IROTH 00004\n#define S_IWOTH 00002\n#define S_IXOTH 00001\n\n#define S_IFSOCK 0140000\n#define S_IFLNK 0120000\n#define S_IFBLK 0060000\n#define S_IFIFO 0010000\n#define S_ISUID 0004000\n#define S_ISGID 0002000\n#define S_ISVTX 0001000\n\n#define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)\n\n#else\n#define S_IFMT 00170000\n#define S_IFSOCK 0140000\n#define S_IFLNK 0120000\n#define S_IFREG 0100000\n#define S_IFBLK 0060000\n#define S_IFDIR 0040000\n#define S_IFCHR 0020000\n#define S_IFIFO 0010000\n#define S_ISUID 0004000\n#define S_ISGID 0002000\n#define S_ISVTX 0001000\n\n#define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)\n#define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)\n#define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)\n#define S_ISCHR(m) (((m) & S_IFMT) == S_IFCHR)\n#define S_ISBLK(m) (((m) & S_IFMT) == S_IFBLK)\n#define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO)\n#define S_ISSOCK(m) (((m) & S_IFMT) == S_IFSOCK)\n\n#define S_IRWXU 00700\n#define S_IRUSR 00400\n#define S_IWUSR 00200\n#define S_IXUSR 00100\n\n#define S_IRWXG 00070\n#define S_IRGRP 00040\n#define S_IWGRP 00020\n#define S_IXGRP 00010\n\n#define S_IRWXO 00007\n#define S_IROTH 00004\n#define S_IWOTH 00002\n#define S_IXOTH 00001\n\n/* stat structure */\n#include <stdint.h>\n#include <time.h>\n\nstruct stat\n{\n struct rt_device *st_dev;\n uint16_t st_ino;\n uint16_t st_mode;\n uint16_t st_nlink;\n uint16_t st_uid;\n uint16_t st_gid;\n struct rt_device *st_rdev;\n uint32_t st_size;\n time_t st_atime;\n long st_spare1;\n time_t st_mtime;\n long st_spare2;\n time_t st_ctime;\n long st_spare3;\n uint32_t st_blksize;\n uint32_t st_blocks;\n long st_spare4[2];\n};\n\n#endif\n\n#endif\n", "include/libc/libc_stdio.h": "/*\n * Copyright (c) 2006-2018, RT-Thread Development Team\n *\n * SPDX-License-Identifier: Apache-2.0\n *\n * Change Logs:\n * Date Author Notes\n * 2020-09-05 Meco Manthe first version\n */\n\n#ifndef LIBC_STDIO_H__\n#define LIBC_STDIO_H__\n\n#include <rtconfig.h>\n\n#ifdef RT_USING_POSIX_GETLINE\n\n#include <posix_getline.h>\n\n#endif\n\n#endif\n", "include/rtdbg.h": "/*\n * Copyright (c) 2006-2018, RT-Thread Development Team\n *\n * SPDX-License-Identifier: Apache-2.0\n *\n * Change Logs:\n * Date Author Notes\n * 2016-11-12 Bernard The first version\n * 2018-05-25 armink Add simple API, such as LOG_D, LOG_E\n */\n\n/*\n * The macro definitions for debug\n *\n * These macros are defined in static. If you want to use debug macro, you can\n * use as following code:\n *\n * In your C/C++ file, enable/disable DEBUG_ENABLE macro, and then include this\n * header file.\n *\n * #define DBG_TAG \"MOD_TAG\"\n * #define DBG_LVL DBG_INFO\n * #include <rtdbg.h> // must after of DBG_LVL, DBG_TAG or other options\n *\n * Then in your C/C++ file, you can use LOG_X macro to print out logs:\n * LOG_D(\"this is a debug log!\");\n * LOG_E(\"this is a error log!\");\n */\n\n#ifndef RT_DBG_H__\n#define RT_DBG_H__\n\n#include <rtconfig.h>\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n/* the debug log will force enable when RT_DEBUG macro is defined */\n#if defined(RT_DEBUG) && !defined(DBG_ENABLE)\n#define DBG_ENABLE\n#endif\n\n/* it will force output color log when RT_DEBUG_COLOR macro is defined */\n#if defined(RT_DEBUG_COLOR) && !defined(DBG_COLOR)\n#define DBG_COLOR\n#endif\n\n#if defined(RT_USING_ULOG)\n/* using ulog compatible with rtdbg */\n#include <ulog.h>\n#else\n\n/* DEBUG level */\n#define DBG_ERROR 0\n#define DBG_WARNING 1\n#define DBG_INFO 2\n#define DBG_LOG 3\n\n#ifdef DBG_TAG\n#ifndef DBG_SECTION_NAME\n#define DBG_SECTION_NAME DBG_TAG\n#endif\n#else\n/* compatible with old version */\n#ifndef DBG_SECTION_NAME\n#define DBG_SECTION_NAME \"DBG\"\n#endif\n#endif /* DBG_TAG */\n\n#ifdef DBG_ENABLE\n\n#ifdef DBG_LVL\n#ifndef DBG_LEVEL\n#define DBG_LEVEL DBG_LVL\n#endif\n#else\n/* compatible with old version */\n#ifndef DBG_LEVEL\n#define DBG_LEVEL DBG_WARNING\n#endif\n#endif /* DBG_LVL */\n\n/*\n * The color for terminal (foreground)\n * BLACK 30\n * RED 31\n * GREEN 32\n * YELLOW 33\n * BLUE 34\n * PURPLE 35\n * CYAN 36\n * WHITE 37\n */\n#ifdef DBG_COLOR\n#define _DBG_COLOR(n) rt_kprintf(\"\\033[\"#n\"m\")\n#define _DBG_LOG_HDR(lvl_name, color_n) \\\n rt_kprintf(\"\\033[\"#color_n\"m[\" lvl_name \"/\" DBG_SECTION_NAME \"] \")\n#define _DBG_LOG_X_END \\\n rt_kprintf(\"\\033[0m\\n\")\n#else\n#define _DBG_COLOR(n)\n#define _DBG_LOG_HDR(lvl_name, color_n) \\\n rt_kprintf(\"[\" lvl_name \"/\" DBG_SECTION_NAME \"] \")\n#define _DBG_LOG_X_END \\\n rt_kprintf(\"\\n\")\n#endif /* DBG_COLOR */\n\n/*\n * static debug routine\n * NOTE: This is a NOT RECOMMENDED API. Please using LOG_X API.\n * It will be DISCARDED later. Because it will take up more resources.\n */\n#define dbg_log(level, fmt, ...) \\\n if ((level) <= DBG_LEVEL) \\\n { \\\n switch(level) \\\n { \\\n case DBG_ERROR: _DBG_LOG_HDR(\"E\", 31); break; \\\n case DBG_WARNING: _DBG_LOG_HDR(\"W\", 33); break; \\\n case DBG_INFO: _DBG_LOG_HDR(\"I\", 32); break; \\\n case DBG_LOG: _DBG_LOG_HDR(\"D\", 0); break; \\\n default: break; \\\n } \\\n rt_kprintf(fmt, ##__VA_ARGS__); \\\n _DBG_COLOR(0); \\\n }\n\n#define dbg_here \\\n if ((DBG_LEVEL) <= DBG_LOG){ \\\n rt_kprintf(DBG_SECTION_NAME \" Here %s:%d\\n\", \\\n __FUNCTION__, __LINE__); \\\n }\n\n#define dbg_log_line(lvl, color_n, fmt, ...) \\\n do \\\n { \\\n _DBG_LOG_HDR(lvl, color_n); \\\n rt_kprintf(fmt, ##__VA_ARGS__); \\\n _DBG_LOG_X_END; \\\n } \\\n while (0)\n\n#define dbg_raw(...) rt_kprintf(__VA_ARGS__);\n\n#else\n#define dbg_log(level, fmt, ...)\n#define dbg_here\n#define dbg_enter\n#define dbg_exit\n#define dbg_log_line(lvl, color_n, fmt, ...)\n#define dbg_raw(...)\n#endif /* DBG_ENABLE */\n\n#if (DBG_LEVEL >= DBG_LOG)\n#define LOG_D(fmt, ...) dbg_log_line(\"D\", 0, fmt, ##__VA_ARGS__)\n#else\n#define LOG_D(...)\n#endif\n\n#if (DBG_LEVEL >= DBG_INFO)\n#define LOG_I(fmt, ...) dbg_log_line(\"I\", 32, fmt, ##__VA_ARGS__)\n#else\n#define LOG_I(...)\n#endif\n\n#if (DBG_LEVEL >= DBG_WARNING)\n#define LOG_W(fmt, ...) dbg_log_line(\"W\", 33, fmt, ##__VA_ARGS__)\n#else\n#define LOG_W(...)\n#endif\n\n#if (DBG_LEVEL >= DBG_ERROR)\n#define LOG_E(fmt, ...) dbg_log_line(\"E\", 31, fmt, ##__VA_ARGS__)\n#else\n#define LOG_E(...)\n#endif\n\n#define LOG_RAW(...) dbg_raw(__VA_ARGS__)\n\n#endif /* defined(RT_USING_ULOG) && define(DBG_ENABLE) */\n\n#ifdef __cplusplus\n}\n#endif\n\n#endif /* RT_DBG_H__ */\n", "include/rtdebug.h": "/*\n * Copyright (c) 2006-2018, RT-Thread Development Team\n *\n * SPDX-License-Identifier: Apache-2.0\n */\n\n#ifndef __RTDEBUG_H__\n#define __RTDEBUG_H__\n\n#include <rtconfig.h>\n\n/* settings depend check */\n#ifdef RT_USING_POSIX\n#if !defined(RT_USING_DFS) || !defined(RT_USING_DFS_DEVFS)\n#error \"POSIX poll/select, stdin need file system(RT_USING_DFS) and device file system(RT_USING_DFS_DEVFS)\"\n#endif\n\n#if !defined(RT_USING_LIBC)\n#error \"POSIX layer need standard C library(RT_USING_LIBC)\"\n#endif\n\n#endif\n\n#ifdef RT_USING_POSIX_TERMIOS\n#if !defined(RT_USING_POSIX)\n#error \"termios need POSIX layer(RT_USING_POSIX)\"\n#endif\n#endif\n\n/* Using this macro to control all kernel debug features. */\n#ifdef RT_DEBUG\n\n/* Turn on some of these (set to non-zero) to debug kernel */\n#ifndef RT_DEBUG_MEM\n#define RT_DEBUG_MEM 0\n#endif\n\n#ifndef RT_DEBUG_MEMHEAP\n#define RT_DEBUG_MEMHEAP 0\n#endif\n\n#ifndef RT_DEBUG_MODULE\n#define RT_DEBUG_MODULE 0\n#endif\n\n#ifndef RT_DEBUG_SCHEDULER\n#define RT_DEBUG_SCHEDULER 0\n#endif\n\n#ifndef RT_DEBUG_SLAB\n#define RT_DEBUG_SLAB 0\n#endif\n\n#ifndef RT_DEBUG_THREAD\n#define RT_DEBUG_THREAD 0\n#endif\n\n#ifndef RT_DEBUG_TIMER\n#define RT_DEBUG_TIMER 0\n#endif\n\n#ifndef RT_DEBUG_IRQ\n#define RT_DEBUG_IRQ 0\n#endif\n\n#ifndef RT_DEBUG_IPC\n#define RT_DEBUG_IPC 0\n#endif\n\n#ifndef RT_DEBUG_INIT\n#define RT_DEBUG_INIT 0\n#endif\n\n/* Turn on this to enable context check */\n#ifndef RT_DEBUG_CONTEXT_CHECK\n#define RT_DEBUG_CONTEXT_CHECK 1\n#endif\n\n#define RT_DEBUG_LOG(type, message) \\\ndo \\\n{ \\\n if (type) \\\n rt_kprintf message; \\\n} \\\nwhile (0)\n\n#define RT_ASSERT(EX) \\\nif (!(EX)) \\\n{ \\\n rt_assert_handler(#EX, __FUNCTION__, __LINE__); \\\n}\n\n/* Macro to check current context */\n#if RT_DEBUG_CONTEXT_CHECK\n#define RT_DEBUG_NOT_IN_INTERRUPT \\\ndo \\\n{ \\\n rt_base_t level; \\\n level = rt_hw_interrupt_disable(); \\\n if (rt_interrupt_get_nest() != 0) \\\n { \\\n rt_kprintf(\"Function[%s] shall not be used in ISR\\n\", __FUNCTION__); \\\n RT_ASSERT(0) \\\n } \\\n rt_hw_interrupt_enable(level); \\\n} \\\nwhile (0)\n\n/* \"In thread context\" means:\n * 1) the scheduler has been started\n * 2) not in interrupt context.\n */\n#define RT_DEBUG_IN_THREAD_CONTEXT \\\ndo \\\n{ \\\n rt_base_t level; \\\n level = rt_hw_interrupt_disable(); \\\n if (rt_thread_self() == RT_NULL) \\\n { \\\n rt_kprintf(\"Function[%s] shall not be used before scheduler start\\n\", \\\n __FUNCTION__); \\\n RT_ASSERT(0) \\\n } \\\n RT_DEBUG_NOT_IN_INTERRUPT; \\\n rt_hw_interrupt_enable(level); \\\n} \\\nwhile (0)\n#else\n#define RT_DEBUG_NOT_IN_INTERRUPT\n#define RT_DEBUG_IN_THREAD_CONTEXT\n#endif\n\n#else /* RT_DEBUG */\n\n#define RT_ASSERT(EX)\n#define RT_DEBUG_LOG(type, message)\n#define RT_DEBUG_NOT_IN_INTERRUPT\n#define RT_DEBUG_IN_THREAD_CONTEXT\n\n#endif /* RT_DEBUG */\n\n#endif /* __RTDEBUG_H__ */\n", "include/rtdef.h": "/*\n * Copyright (c) 2006-2018, RT-Thread Development Team\n *\n * SPDX-License-Identifier: Apache-2.0\n *\n * Change Logs:\n * Date Author Notes\n * 2007-01-10 Bernard the first version\n * 2008-07-12 Bernard remove all rt_int8, rt_uint32_t etc typedef\n * 2010-10-26 yi.qiu add module support\n * 2010-11-10 Bernard add cleanup callback function in thread exit.\n * 2011-05-09 Bernard use builtin va_arg in GCC 4.x\n * 2012-11-16 Bernard change RT_NULL from ((void*)0) to 0.\n * 2012-12-29 Bernard change the RT_USING_MEMPOOL location and add\n * RT_USING_MEMHEAP condition.\n * 2012-12-30 Bernard add more control command for graphic.\n * 2013-01-09 Bernard change version number.\n * 2015-02-01 Bernard change version number to v2.1.0\n * 2017-08-31 Bernard change version number to v3.0.0\n * 2017-11-30 Bernard change version number to v3.0.1\n * 2017-12-27 Bernard change version number to v3.0.2\n * 2018-02-24 Bernard change version number to v3.0.3\n * 2018-04-25 Bernard change version number to v3.0.4\n * 2018-05-31 Bernard change version number to v3.1.0\n * 2018-09-04 Bernard change version number to v3.1.1\n * 2018-09-14 Bernard apply Apache License v2.0 to RT-Thread Kernel\n * 2018-10-13 Bernard change version number to v4.0.0\n * 2018-10-02 Bernard add 64bit arch support\n * 2018-11-22 Jesven add smp member to struct rt_thread\n * add struct rt_cpu\n * add smp relevant macros\n * 2019-01-27 Bernard change version number to v4.0.1\n * 2019-05-17 Bernard change version number to v4.0.2\n * 2019-12-20 Bernard change version number to v4.0.3\n * 2020-08-10 Meco Man add macro for struct rt_device_ops\n */\n\n#ifndef __RT_DEF_H__\n#define __RT_DEF_H__\n\n/* include rtconfig header to import configuration */\n#include <rtconfig.h>\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n/**\n * @addtogroup BasicDef\n */\n\n/**@{*/\n\n/* RT-Thread version information */\n#define RT_VERSION 4L /**< major version number */\n#define RT_SUBVERSION 0L /**< minor version number */\n#define RT_REVISION 3L /**< revise version number */\n\n/* RT-Thread version */\n#define RTTHREAD_VERSION ((RT_VERSION * 10000) + \\\n (RT_SUBVERSION * 100) + RT_REVISION)\n\n/* RT-Thread basic data type definitions */\n#ifndef RT_USING_ARCH_DATA_TYPE\ntypedef signed char rt_int8_t; /**< 8bit integer type */\ntypedef signed short rt_int16_t; /**< 16bit integer type */\ntypedef signed int rt_int32_t; /**< 32bit integer type */\ntypedef unsigned char rt_uint8_t; /**< 8bit unsigned integer type */\ntypedef unsigned short rt_uint16_t; /**< 16bit unsigned integer type */\ntypedef unsigned int rt_uint32_t; /**< 32bit unsigned integer type */\n\n#ifdef ARCH_CPU_64BIT\ntypedef signed long rt_int64_t; /**< 64bit integer type */\ntypedef unsigned long rt_uint64_t; /**< 64bit unsigned integer type */\n#else\ntypedef signed long long rt_int64_t; /**< 64bit integer type */\ntypedef unsigned long long rt_uint64_t; /**< 64bit unsigned integer type */\n#endif\n#endif\n\ntypedef int rt_bool_t; /**< boolean type */\ntypedef long rt_base_t; /**< Nbit CPU related date type */\ntypedef unsigned long rt_ubase_t; /**< Nbit unsigned CPU related data type */\n\ntypedef rt_base_t rt_err_t; /**< Type for error number */\ntypedef rt_uint32_t rt_time_t; /**< Type for time stamp */\ntypedef rt_uint32_t rt_tick_t; /**< Type for tick count */\ntypedef rt_base_t rt_flag_t; /**< Type for flags */\ntypedef rt_ubase_t rt_size_t; /**< Type for size number */\ntypedef rt_ubase_t rt_dev_t; /**< Type for device */\ntypedef rt_base_t rt_off_t; /**< Type for offset */\n\n/* boolean type definitions */\n#define RT_TRUE 1 /**< boolean true */\n#define RT_FALSE 0 /**< boolean fails */\n\n/**@}*/\n\n/* maximum value of base type */\n#define RT_UINT8_MAX 0xff /**< Maxium number of UINT8 */\n#define RT_UINT16_MAX 0xffff /**< Maxium number of UINT16 */\n#define RT_UINT32_MAX 0xffffffff /**< Maxium number of UINT32 */\n#define RT_TICK_MAX RT_UINT32_MAX /**< Maxium number of tick */\n\n#if defined (__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050)\n#define __CLANG_ARM\n#endif\n\n/* Compiler Related Definitions */\n#if defined(__CC_ARM) || defined(__CLANG_ARM) /* ARM Compiler */\n #include <stdarg.h>\n #define SECTION(x) __attribute__((section(x)))\n #define RT_UNUSED __attribute__((unused))\n #define RT_USED __attribute__((used))\n #define ALIGN(n) __attribute__((aligned(n)))\n\n #define RT_WEAK __attribute__((weak))\n #define rt_inline static __inline\n /* module compiling */\n #ifdef RT_USING_MODULE\n #define RTT_API __declspec(dllimport)\n #else\n #define RTT_API __declspec(dllexport)\n #endif\n\n#elif defined (__IAR_SYSTEMS_ICC__) /* for IAR Compiler */\n #include <stdarg.h>\n #define SECTION(x) @ x\n #define RT_UNUSED\n #define RT_USED __root\n #define PRAGMA(x) _Pragma(#x)\n #define ALIGN(n) PRAGMA(data_alignment=n)\n #define RT_WEAK __weak\n #define rt_inline static inline\n #define RTT_API\n\n#elif defined (__GNUC__) /* GNU GCC Compiler */\n #ifdef RT_USING_NEWLIB\n #include <stdarg.h>\n #else\n /* the version of GNU GCC must be greater than 4.x */\n typedef __builtin_va_list __gnuc_va_list;\n typedef __gnuc_va_list va_list;\n #define va_start(v,l) __builtin_va_start(v,l)\n #define va_end(v) __builtin_va_end(v)\n #define va_arg(v,l) __builtin_va_arg(v,l)\n #endif\n\n #define SECTION(x) __attribute__((section(x)))\n #define RT_UNUSED __attribute__((unused))\n #define RT_USED __attribute__((used))\n #define ALIGN(n) __attribute__((aligned(n)))\n #define RT_WEAK __attribute__((weak))\n #define rt_inline static __inline\n #define RTT_API\n#elif defined (__ADSPBLACKFIN__) /* for VisualDSP++ Compiler */\n #include <stdarg.h>\n #define SECTION(x) __attribute__((section(x)))\n #define RT_UNUSED __attribute__((unused))\n #define RT_USED __attribute__((used))\n #define ALIGN(n) __attribute__((aligned(n)))\n #define RT_WEAK __attribute__((weak))\n #define rt_inline static inline\n #define RTT_API\n#elif defined (_MSC_VER)\n #include <stdarg.h>\n #define SECTION(x)\n #define RT_UNUSED\n #define RT_USED\n #define ALIGN(n) __declspec(align(n))\n #define RT_WEAK\n #define rt_inline static __inline\n #define RTT_API\n#elif defined (__TI_COMPILER_VERSION__)\n #include <stdarg.h>\n /* The way that TI compiler set section is different from other(at least\n * GCC and MDK) compilers. See ARM Optimizing C/C++ Compiler 5.9.3 for more\n * details. */\n #define SECTION(x)\n #define RT_UNUSED\n #define RT_USED\n #define PRAGMA(x) _Pragma(#x)\n #define ALIGN(n)\n #define RT_WEAK\n #define rt_inline static inline\n #define RTT_API\n#else\n #error not supported tool chain\n#endif\n\n/* initialization export */\n#ifdef RT_USING_COMPONENTS_INIT\ntypedef int (*init_fn_t)(void);\n#ifdef _MSC_VER /* we do not support MS VC++ compiler */\n #define INIT_EXPORT(fn, level)\n#else\n #if RT_DEBUG_INIT\n struct rt_init_desc\n {\n const char* fn_name;\n const init_fn_t fn;\n };\n #define INIT_EXPORT(fn, level) \\\n const char __rti_##fn##_name[] = #fn; \\\n RT_USED const struct rt_init_desc __rt_init_desc_##fn SECTION(\".rti_fn.\" level) = \\\n { __rti_##fn##_name, fn};\n #else\n #define INIT_EXPORT(fn, level) \\\n RT_USED const init_fn_t __rt_init_##fn SECTION(\".rti_fn.\" level) = fn\n #endif\n#endif\n#else\n#define INIT_EXPORT(fn, level)\n#endif\n\n/* board init routines will be called in board_init() function */\n#define INIT_BOARD_EXPORT(fn) INIT_EXPORT(fn, \"1\")\n\n/* pre/device/component/env/app init routines will be called in init_thread */\n/* components pre-initialization (pure software initilization) */\n#define INIT_PREV_EXPORT(fn) INIT_EXPORT(fn, \"2\")\n/* device initialization */\n#define INIT_DEVICE_EXPORT(fn) INIT_EXPORT(fn, \"3\")\n/* components initialization (dfs, lwip, ...) */\n#define INIT_COMPONENT_EXPORT(fn) INIT_EXPORT(fn, \"4\")\n/* environment initialization (mount disk, ...) */\n#define INIT_ENV_EXPORT(fn) INIT_EXPORT(fn, \"5\")\n/* appliation initialization (rtgui application etc ...) */\n#define INIT_APP_EXPORT(fn) INIT_EXPORT(fn, \"6\")\n\n#if !defined(RT_USING_FINSH)\n/* define these to empty, even if not include finsh.h file */\n#define FINSH_FUNCTION_EXPORT(name, desc)\n#define FINSH_FUNCTION_EXPORT_ALIAS(name, alias, desc)\n#define FINSH_VAR_EXPORT(name, type, desc)\n\n#define MSH_CMD_EXPORT(command, desc)\n#define MSH_CMD_EXPORT_ALIAS(command, alias, desc)\n#elif !defined(FINSH_USING_SYMTAB)\n#define FINSH_FUNCTION_EXPORT_CMD(name, cmd, desc)\n#endif\n\n/* event length */\n#define RT_EVENT_LENGTH 32\n\n/* memory management option */\n#define RT_MM_PAGE_SIZE 4096\n#define RT_MM_PAGE_MASK (RT_MM_PAGE_SIZE - 1)\n#define RT_MM_PAGE_BITS 12\n\n/* kernel malloc definitions */\n#ifndef RT_KERNEL_MALLOC\n#define RT_KERNEL_MALLOC(sz) rt_malloc(sz)\n#endif\n\n#ifndef RT_KERNEL_FREE\n#define RT_KERNEL_FREE(ptr) rt_free(ptr)\n#endif\n\n#ifndef RT_KERNEL_REALLOC\n#define RT_KERNEL_REALLOC(ptr, size) rt_realloc(ptr, size)\n#endif\n\n/**\n * @addtogroup Error\n */\n\n/**@{*/\n\n/* RT-Thread error code definitions */\n#define RT_EOK 0 /**< There is no error */\n#define RT_ERROR 1 /**< A generic error happens */\n#define RT_ETIMEOUT 2 /**< Timed out */\n#define RT_EFULL 3 /**< The resource is full */\n#define RT_EEMPTY 4 /**< The resource is empty */\n#define RT_ENOMEM 5 /**< No memory */\n#define RT_ENOSYS 6 /**< No system */\n#define RT_EBUSY 7 /**< Busy */\n#define RT_EIO 8 /**< IO error */\n#define RT_EINTR 9 /**< Interrupted system call */\n#define RT_EINVAL 10 /**< Invalid argument */\n\n/**@}*/\n\n/**\n * @ingroup BasicDef\n *\n * @def RT_ALIGN(size, align)\n * Return the most contiguous size aligned at specified width. RT_ALIGN(13, 4)\n * would return 16.\n */\n#define RT_ALIGN(size, align) (((size) + (align) - 1) & ~((align) - 1))\n\n/**\n * @ingroup BasicDef\n *\n * @def RT_ALIGN_DOWN(size, align)\n * Return the down number of aligned at specified width. RT_ALIGN_DOWN(13, 4)\n * would return 12.\n */\n#define RT_ALIGN_DOWN(size, align) ((size) & ~((align) - 1))\n\n/**\n * @ingroup BasicDef\n *\n * @def RT_NULL\n * Similar as the \\c NULL in C library.\n */\n#define RT_NULL (0)\n\n/**\n * Double List structure\n */\nstruct rt_list_node\n{\n struct rt_list_node *next; /**< point to next node. */\n struct rt_list_node *prev; /**< point to prev node. */\n};\ntypedef struct rt_list_node rt_list_t; /**< Type for lists. */\n\n/**\n * Single List structure\n */\nstruct rt_slist_node\n{\n struct rt_slist_node *next; /**< point to next node. */\n};\ntypedef struct rt_slist_node rt_slist_t; /**< Type for single list. */\n\n/**\n * @addtogroup KernelObject\n */\n\n/**@{*/\n\n/*\n * kernel object macros\n */\n#define RT_OBJECT_FLAG_MODULE 0x80 /**< is module object. */\n\n/**\n * Base structure of Kernel object\n */\nstruct rt_object\n{\n char name[RT_NAME_MAX]; /**< name of kernel object */\n rt_uint8_t type; /**< type of kernel object */\n rt_uint8_t flag; /**< flag of kernel object */\n\n#ifdef RT_USING_MODULE\n void *module_id; /**< id of application module */\n#endif\n rt_list_t list; /**< list node of kernel object */\n};\ntypedef struct rt_object *rt_object_t; /**< Type for kernel objects. */\n\n/**\n * The object type can be one of the follows with specific\n * macros enabled:\n * - Thread\n * - Semaphore\n * - Mutex\n * - Event\n * - MailBox\n * - MessageQueue\n * - MemHeap\n * - MemPool\n * - Device\n * - Timer\n * - Module\n * - Unknown\n * - Static\n */\nenum rt_object_class_type\n{\n RT_Object_Class_Null = 0x00, /**< The object is not used. */\n RT_Object_Class_Thread = 0x01, /**< The object is a thread. */\n RT_Object_Class_Semaphore = 0x02, /**< The object is a semaphore. */\n RT_Object_Class_Mutex = 0x03, /**< The object is a mutex. */\n RT_Object_Class_Event = 0x04, /**< The object is a event. */\n RT_Object_Class_MailBox = 0x05, /**< The object is a mail box. */\n RT_Object_Class_MessageQueue = 0x06, /**< The object is a message queue. */\n RT_Object_Class_MemHeap = 0x07, /**< The object is a memory heap. */\n RT_Object_Class_MemPool = 0x08, /**< The object is a memory pool. */\n RT_Object_Class_Device = 0x09, /**< The object is a device. */\n RT_Object_Class_Timer = 0x0a, /**< The object is a timer. */\n RT_Object_Class_Module = 0x0b, /**< The object is a module. */\n RT_Object_Class_Unknown = 0x0c, /**< The object is unknown. */\n RT_Object_Class_Static = 0x80 /**< The object is a static object. */\n};\n\n/**\n * The information of the kernel object\n */\nstruct rt_object_information\n{\n enum rt_object_class_type type; /**< object class type */\n rt_list_t object_list; /**< object list */\n rt_size_t object_size; /**< object size */\n};\n\n/**\n * The hook function call macro\n */\n#ifdef RT_USING_HOOK\n#define RT_OBJECT_HOOK_CALL(func, argv) \\\n do { if ((func) != RT_NULL) func argv; } while (0)\n#else\n#define RT_OBJECT_HOOK_CALL(func, argv)\n#endif\n\n/**@}*/\n\n/**\n * @addtogroup Clock\n */\n\n/**@{*/\n\n/**\n * clock & timer macros\n */\n#define RT_TIMER_FLAG_DEACTIVATED 0x0 /**< timer is deactive */\n#define RT_TIMER_FLAG_ACTIVATED 0x1 /**< timer is active */\n#define RT_TIMER_FLAG_ONE_SHOT 0x0 /**< one shot timer */\n#define RT_TIMER_FLAG_PERIODIC 0x2 /**< periodic timer */\n\n#define RT_TIMER_FLAG_HARD_TIMER 0x0 /**< hard timer,the timer's callback function will be called in tick isr. */\n#define RT_TIMER_FLAG_SOFT_TIMER 0x4 /**< soft timer,the timer's callback function will be called in timer thread. */\n\n#define RT_TIMER_CTRL_SET_TIME 0x0 /**< set timer control command */\n#define RT_TIMER_CTRL_GET_TIME 0x1 /**< get timer control command */\n#define RT_TIMER_CTRL_SET_ONESHOT 0x2 /**< change timer to one shot */\n#define RT_TIMER_CTRL_SET_PERIODIC 0x3 /**< change timer to periodic */\n#define RT_TIMER_CTRL_GET_STATE 0x4 /**< get timer run state active or deactive*/\n\n#ifndef RT_TIMER_SKIP_LIST_LEVEL\n#define RT_TIMER_SKIP_LIST_LEVEL 1\n#endif\n\n/* 1 or 3 */\n#ifndef RT_TIMER_SKIP_LIST_MASK\n#define RT_TIMER_SKIP_LIST_MASK 0x3\n#endif\n\n/**\n * timer structure\n */\nstruct rt_timer\n{\n struct rt_object parent; /**< inherit from rt_object */\n\n rt_list_t row[RT_TIMER_SKIP_LIST_LEVEL];\n\n void (*timeout_func)(void *parameter); /**< timeout function */\n void *parameter; /**< timeout function's parameter */\n\n rt_tick_t init_tick; /**< timer timeout tick */\n rt_tick_t timeout_tick; /**< timeout tick */\n};\ntypedef struct rt_timer *rt_timer_t;\n\n/**@}*/\n\n/**\n * @addtogroup Signal\n */\n#ifdef RT_USING_SIGNALS\n#include <libc/libc_signal.h>\ntypedef unsigned long rt_sigset_t;\ntypedef void (*rt_sighandler_t)(int signo);\ntypedef siginfo_t rt_siginfo_t;\n\n#define RT_SIG_MAX 32\n#endif\n/**@}*/\n\n/**\n * @addtogroup Thread\n */\n\n/**@{*/\n\n/*\n * Thread\n */\n\n/*\n * thread state definitions\n */\n#define RT_THREAD_INIT 0x00 /**< Initialized status */\n#define RT_THREAD_READY 0x01 /**< Ready status */\n#define RT_THREAD_SUSPEND 0x02 /**< Suspend status */\n#define RT_THREAD_RUNNING 0x03 /**< Running status */\n#define RT_THREAD_BLOCK RT_THREAD_SUSPEND /**< Blocked status */\n#define RT_THREAD_CLOSE 0x04 /**< Closed status */\n#define RT_THREAD_STAT_MASK 0x07\n\n#define RT_THREAD_STAT_YIELD 0x08 /**< indicate whether remaining_tick has been reloaded since last schedule */\n#define RT_THREAD_STAT_YIELD_MASK RT_THREAD_STAT_YIELD\n\n#define RT_THREAD_STAT_SIGNAL 0x10 /**< task hold signals */\n#define RT_THREAD_STAT_SIGNAL_READY (RT_THREAD_STAT_SIGNAL | RT_THREAD_READY)\n#define RT_THREAD_STAT_SIGNAL_WAIT 0x20 /**< task is waiting for signals */\n#define RT_THREAD_STAT_SIGNAL_PENDING 0x40 /**< signals is held and it has not been procressed */\n#define RT_THREAD_STAT_SIGNAL_MASK 0xf0\n\n/**\n * thread control command definitions\n */\n#define RT_THREAD_CTRL_STARTUP 0x00 /**< Startup thread. */\n#define RT_THREAD_CTRL_CLOSE 0x01 /**< Close thread. */\n#define RT_THREAD_CTRL_CHANGE_PRIORITY 0x02 /**< Change thread priority. */\n#define RT_THREAD_CTRL_INFO 0x03 /**< Get thread information. */\n#define RT_THREAD_CTRL_BIND_CPU 0x04 /**< Set thread bind cpu. */\n\n#ifdef RT_USING_SMP\n\n#define RT_CPU_DETACHED RT_CPUS_NR /**< The thread not running on cpu. */\n#define RT_CPU_MASK ((1 << RT_CPUS_NR) - 1) /**< All CPUs mask bit. */\n\n#ifndef RT_SCHEDULE_IPI\n#define RT_SCHEDULE_IPI 0\n#endif\n\n/**\n * CPUs definitions\n *\n */\nstruct rt_cpu\n{\n struct rt_thread *current_thread;\n\n rt_uint16_t irq_nest;\n rt_uint8_t irq_switch_flag;\n\n rt_uint8_t current_priority;\n rt_list_t priority_table[RT_THREAD_PRIORITY_MAX];\n#if RT_THREAD_PRIORITY_MAX > 32\n rt_uint32_t priority_group;\n rt_uint8_t ready_table[32];\n#else\n rt_uint32_t priority_group;\n#endif\n\n rt_tick_t tick;\n};\n\n#endif\n\n/**\n * Thread structure\n */\nstruct rt_thread\n{\n /* rt object */\n char name[RT_NAME_MAX]; /**< the name of thread */\n rt_uint8_t type; /**< type of object */\n rt_uint8_t flags; /**< thread's flags */\n\n#ifdef RT_USING_MODULE\n void *module_id; /**< id of application module */\n#endif\n\n rt_list_t list; /**< the object list */\n rt_list_t tlist; /**< the thread list */\n\n /* stack point and entry */\n void *sp; /**< stack point */\n void *entry; /**< entry */\n void *parameter; /**< parameter */\n void *stack_addr; /**< stack address */\n rt_uint32_t stack_size; /**< stack size */\n\n /* error code */\n rt_err_t error; /**< error code */\n\n rt_uint8_t stat; /**< thread status */\n\n#ifdef RT_USING_SMP\n rt_uint8_t bind_cpu; /**< thread is bind to cpu */\n rt_uint8_t oncpu; /**< process on cpu` */\n\n rt_uint16_t scheduler_lock_nest; /**< scheduler lock count */\n rt_uint16_t cpus_lock_nest; /**< cpus lock count */\n rt_uint16_t critical_lock_nest; /**< critical lock count */\n#endif /*RT_USING_SMP*/\n\n /* priority */\n rt_uint8_t current_priority; /**< current priority */\n rt_uint8_t init_priority; /**< initialized priority */\n#if RT_THREAD_PRIORITY_MAX > 32\n rt_uint8_t number;\n rt_uint8_t high_mask;\n#endif\n rt_uint32_t number_mask;\n\n#if defined(RT_USING_EVENT)\n /* thread event */\n rt_uint32_t event_set;\n rt_uint8_t event_info;\n#endif\n\n#if defined(RT_USING_SIGNALS)\n rt_sigset_t sig_pending; /**< the pending signals */\n rt_sigset_t sig_mask; /**< the mask bits of signal */\n\n#ifndef RT_USING_SMP\n void *sig_ret; /**< the return stack pointer from signal */\n#endif\n rt_sighandler_t *sig_vectors; /**< vectors of signal handler */\n void *si_list; /**< the signal infor list */\n#endif\n\n rt_ubase_t init_tick; /**< thread's initialized tick */\n rt_ubase_t remaining_tick; /**< remaining tick */\n\n struct rt_timer thread_timer; /**< built-in thread timer */\n\n void (*cleanup)(struct rt_thread *tid); /**< cleanup function when thread exit */\n\n /* light weight process if present */\n#ifdef RT_USING_LWP\n void *lwp;\n#endif\n\n rt_ubase_t user_data; /**< private user data beyond this thread */\n};\ntypedef struct rt_thread *rt_thread_t;\n\n/**@}*/\n\n/**\n * @addtogroup IPC\n */\n\n/**@{*/\n\n/**\n * IPC flags and control command definitions\n */\n#define RT_IPC_FLAG_FIFO 0x00 /**< FIFOed IPC. @ref IPC. */\n#define RT_IPC_FLAG_PRIO 0x01 /**< PRIOed IPC. @ref IPC. */\n\n#define RT_IPC_CMD_UNKNOWN 0x00 /**< unknown IPC command */\n#define RT_IPC_CMD_RESET 0x01 /**< reset IPC object */\n\n#define RT_WAITING_FOREVER -1 /**< Block forever until get resource. */\n#define RT_WAITING_NO 0 /**< Non-block. */\n\n/**\n * Base structure of IPC object\n */\nstruct rt_ipc_object\n{\n struct rt_object parent; /**< inherit from rt_object */\n\n rt_list_t suspend_thread; /**< threads pended on this resource */\n};\n\n#ifdef RT_USING_SEMAPHORE\n/**\n * Semaphore structure\n */\nstruct rt_semaphore\n{\n struct rt_ipc_object parent; /**< inherit from ipc_object */\n\n rt_uint16_t value; /**< value of semaphore. */\n rt_uint16_t reserved; /**< reserved field */\n};\ntypedef struct rt_semaphore *rt_sem_t;\n#endif\n\n#ifdef RT_USING_MUTEX\n/**\n * Mutual exclusion (mutex) structure\n */\nstruct rt_mutex\n{\n struct rt_ipc_object parent; /**< inherit from ipc_object */\n\n rt_uint16_t value; /**< value of mutex */\n\n rt_uint8_t original_priority; /**< priority of last thread hold the mutex */\n rt_uint8_t hold; /**< numbers of thread hold the mutex */\n\n struct rt_thread *owner; /**< current owner of mutex */\n};\ntypedef struct rt_mutex *rt_mutex_t;\n#endif\n\n#ifdef RT_USING_EVENT\n/**\n * flag defintions in event\n */\n#define RT_EVENT_FLAG_AND 0x01 /**< logic and */\n#define RT_EVENT_FLAG_OR 0x02 /**< logic or */\n#define RT_EVENT_FLAG_CLEAR 0x04 /**< clear flag */\n\n/*\n * event structure\n */\nstruct rt_event\n{\n struct rt_ipc_object parent; /**< inherit from ipc_object */\n\n rt_uint32_t set; /**< event set */\n};\ntypedef struct rt_event *rt_event_t;\n#endif\n\n#ifdef RT_USING_MAILBOX\n/**\n * mailbox structure\n */\nstruct rt_mailbox\n{\n struct rt_ipc_object parent; /**< inherit from ipc_object */\n\n rt_ubase_t *msg_pool; /**< start address of message buffer */\n\n rt_uint16_t size; /**< size of message pool */\n\n rt_uint16_t entry; /**< index of messages in msg_pool */\n rt_uint16_t in_offset; /**< input offset of the message buffer */\n rt_uint16_t out_offset; /**< output offset of the message buffer */\n\n rt_list_t suspend_sender_thread; /**< sender thread suspended on this mailbox */\n};\ntypedef struct rt_mailbox *rt_mailbox_t;\n#endif\n\n#ifdef RT_USING_MESSAGEQUEUE\n/**\n * message queue structure\n */\nstruct rt_messagequeue\n{\n struct rt_ipc_object parent; /**< inherit from ipc_object */\n\n void *msg_pool; /**< start address of message queue */\n\n rt_uint16_t msg_size; /**< message size of each message */\n rt_uint16_t max_msgs; /**< max number of messages */\n\n rt_uint16_t entry; /**< index of messages in the queue */\n\n void *msg_queue_head; /**< list head */\n void *msg_queue_tail; /**< list tail */\n void *msg_queue_free; /**< pointer indicated the free node of queue */\n\n rt_list_t suspend_sender_thread; /**< sender thread suspended on this message queue */\n};\ntypedef struct rt_messagequeue *rt_mq_t;\n#endif\n\n/**@}*/\n\n/**\n * @addtogroup MM\n */\n\n/**@{*/\n\n/*\n * memory management\n * heap & partition\n */\n\n#ifdef RT_USING_MEMHEAP\n/**\n * memory item on the heap\n */\nstruct rt_memheap_item\n{\n rt_uint32_t magic; /**< magic number for memheap */\n struct rt_memheap *pool_ptr; /**< point of pool */\n\n struct rt_memheap_item *next; /**< next memheap item */\n struct rt_memheap_item *prev; /**< prev memheap item */\n\n struct rt_memheap_item *next_free; /**< next free memheap item */\n struct rt_memheap_item *prev_free; /**< prev free memheap item */\n};\n\n/**\n * Base structure of memory heap object\n */\nstruct rt_memheap\n{\n struct rt_object parent; /**< inherit from rt_object */\n\n void *start_addr; /**< pool start address and size */\n\n rt_uint32_t pool_size; /**< pool size */\n rt_uint32_t available_size; /**< available size */\n rt_uint32_t max_used_size; /**< maximum allocated size */\n\n struct rt_memheap_item *block_list; /**< used block list */\n\n struct rt_memheap_item *free_list; /**< free block list */\n struct rt_memheap_item free_header; /**< free block list header */\n\n struct rt_semaphore lock; /**< semaphore lock */\n};\n#endif\n\n#ifdef RT_USING_MEMPOOL\n/**\n * Base structure of Memory pool object\n */\nstruct rt_mempool\n{\n struct rt_object parent; /**< inherit from rt_object */\n\n void *start_address; /**< memory pool start */\n rt_size_t size; /**< size of memory pool */\n\n rt_size_t block_size; /**< size of memory blocks */\n rt_uint8_t *block_list; /**< memory blocks list */\n\n rt_size_t block_total_count; /**< numbers of memory block */\n rt_size_t block_free_count; /**< numbers of free memory block */\n\n rt_list_t suspend_thread; /**< threads pended on this resource */\n};\ntypedef struct rt_mempool *rt_mp_t;\n#endif\n\n/**@}*/\n\n#ifdef RT_USING_DEVICE\n/**\n * @addtogroup Device\n */\n\n/**@{*/\n\n/**\n * device (I/O) class type\n */\nenum rt_device_class_type\n{\n RT_Device_Class_Char = 0, /**< character device */\n RT_Device_Class_Block, /**< block device */\n RT_Device_Class_NetIf, /**< net interface */\n RT_Device_Class_MTD, /**< memory device */\n RT_Device_Class_CAN, /**< CAN device */\n RT_Device_Class_RTC, /**< RTC device */\n RT_Device_Class_Sound, /**< Sound device */\n RT_Device_Class_Graphic, /**< Graphic device */\n RT_Device_Class_I2CBUS, /**< I2C bus device */\n RT_Device_Class_USBDevice, /**< USB slave device */\n RT_Device_Class_USBHost, /**< USB host bus */\n RT_Device_Class_SPIBUS, /**< SPI bus device */\n RT_Device_Class_SPIDevice, /**< SPI device */\n RT_Device_Class_SDIO, /**< SDIO bus device */\n RT_Device_Class_PM, /**< PM pseudo device */\n RT_Device_Class_Pipe, /**< Pipe device */\n RT_Device_Class_Portal, /**< Portal device */\n RT_Device_Class_Timer, /**< Timer device */\n RT_Device_Class_Miscellaneous, /**< Miscellaneous device */\n RT_Device_Class_Sensor, /**< Sensor device */\n RT_Device_Class_Touch, /**< Touch device */\n RT_Device_Class_Unknown /**< unknown device */\n};\n\n/**\n * device flags defitions\n */\n#define RT_DEVICE_FLAG_DEACTIVATE 0x000 /**< device is not not initialized */\n\n#define RT_DEVICE_FLAG_RDONLY 0x001 /**< read only */\n#define RT_DEVICE_FLAG_WRONLY 0x002 /**< write only */\n#define RT_DEVICE_FLAG_RDWR 0x003 /**< read and write */\n\n#define RT_DEVICE_FLAG_REMOVABLE 0x004 /**< removable device */\n#define RT_DEVICE_FLAG_STANDALONE 0x008 /**< standalone device */\n#define RT_DEVICE_FLAG_ACTIVATED 0x010 /**< device is activated */\n#define RT_DEVICE_FLAG_SUSPENDED 0x020 /**< device is suspended */\n#define RT_DEVICE_FLAG_STREAM 0x040 /**< stream mode */\n\n#define RT_DEVICE_FLAG_INT_RX 0x100 /**< INT mode on Rx */\n#define RT_DEVICE_FLAG_DMA_RX 0x200 /**< DMA mode on Rx */\n#define RT_DEVICE_FLAG_INT_TX 0x400 /**< INT mode on Tx */\n#define RT_DEVICE_FLAG_DMA_TX 0x800 /**< DMA mode on Tx */\n\n#define RT_DEVICE_OFLAG_CLOSE 0x000 /**< device is closed */\n#define RT_DEVICE_OFLAG_RDONLY 0x001 /**< read only access */\n#define RT_DEVICE_OFLAG_WRONLY 0x002 /**< write only access */\n#define RT_DEVICE_OFLAG_RDWR 0x003 /**< read and write */\n#define RT_DEVICE_OFLAG_OPEN 0x008 /**< device is opened */\n#define RT_DEVICE_OFLAG_MASK 0xf0f /**< mask of open flag */\n\n/**\n * general device commands\n */\n#define RT_DEVICE_CTRL_RESUME 0x01 /**< resume device */\n#define RT_DEVICE_CTRL_SUSPEND 0x02 /**< suspend device */\n#define RT_DEVICE_CTRL_CONFIG 0x03 /**< configure device */\n#define RT_DEVICE_CTRL_CLOSE 0x04 /**< close device */\n\n#define RT_DEVICE_CTRL_SET_INT 0x10 /**< set interrupt */\n#define RT_DEVICE_CTRL_CLR_INT 0x11 /**< clear interrupt */\n#define RT_DEVICE_CTRL_GET_INT 0x12 /**< get interrupt status */\n\n/**\n * special device commands\n */\n#define RT_DEVICE_CTRL_CHAR_STREAM 0x10 /**< stream mode on char device */\n#define RT_DEVICE_CTRL_BLK_GETGEOME 0x10 /**< get geometry information */\n#define RT_DEVICE_CTRL_BLK_SYNC 0x11 /**< flush data to block device */\n#define RT_DEVICE_CTRL_BLK_ERASE 0x12 /**< erase block on block device */\n#define RT_DEVICE_CTRL_BLK_AUTOREFRESH 0x13 /**< block device : enter/exit auto refresh mode */\n#define RT_DEVICE_CTRL_NETIF_GETMAC 0x10 /**< get mac address */\n#define RT_DEVICE_CTRL_MTD_FORMAT 0x10 /**< format a MTD device */\n#define RT_DEVICE_CTRL_RTC_GET_TIME 0x10 /**< get time */\n#define RT_DEVICE_CTRL_RTC_SET_TIME 0x11 /**< set time */\n#define RT_DEVICE_CTRL_RTC_GET_ALARM 0x12 /**< get alarm */\n#define RT_DEVICE_CTRL_RTC_SET_ALARM 0x13 /**< set alarm */\n\ntypedef struct rt_device *rt_device_t;\n\n#ifdef RT_USING_DEVICE_OPS\n/**\n * operations set for device object\n */\nstruct rt_device_ops\n{\n /* common device interface */\n rt_err_t (*init) (rt_device_t dev);\n rt_err_t (*open) (rt_device_t dev, rt_uint16_t oflag);\n rt_err_t (*close) (rt_device_t dev);\n rt_size_t (*read) (rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size);\n rt_size_t (*write) (rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size);\n rt_err_t (*control)(rt_device_t dev, int cmd, void *args);\n};\n#endif\n\n/**\n * WaitQueue structure\n */\nstruct rt_wqueue\n{\n rt_uint32_t flag;\n rt_list_t waiting_list;\n};\ntypedef struct rt_wqueue rt_wqueue_t;\n\n/**\n * Device structure\n */\nstruct rt_device\n{\n struct rt_object parent; /**< inherit from rt_object */\n\n enum rt_device_class_type type; /**< device type */\n rt_uint16_t flag; /**< device flag */\n rt_uint16_t open_flag; /**< device open flag */\n\n rt_uint8_t ref_count; /**< reference count */\n rt_uint8_t device_id; /**< 0 - 255 */\n\n /* device call back */\n rt_err_t (*rx_indicate)(rt_device_t dev, rt_size_t size);\n rt_err_t (*tx_complete)(rt_device_t dev, void *buffer);\n\n#ifdef RT_USING_DEVICE_OPS\n const struct rt_device_ops *ops;\n#else\n /* common device interface */\n rt_err_t (*init) (rt_device_t dev);\n rt_err_t (*open) (rt_device_t dev, rt_uint16_t oflag);\n rt_err_t (*close) (rt_device_t dev);\n rt_size_t (*read) (rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size);\n rt_size_t (*write) (rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size);\n rt_err_t (*control)(rt_device_t dev, int cmd, void *args);\n#endif\n\n#if defined(RT_USING_POSIX)\n const struct dfs_file_ops *fops;\n struct rt_wqueue wait_queue;\n#endif\n\n void *user_data; /**< device private data */\n};\n\n/**\n * block device geometry structure\n */\nstruct rt_device_blk_geometry\n{\n rt_uint32_t sector_count; /**< count of sectors */\n rt_uint32_t bytes_per_sector; /**< number of bytes per sector */\n rt_uint32_t block_size; /**< number of bytes to erase one block */\n};\n\n/**\n * sector arrange struct on block device\n */\nstruct rt_device_blk_sectors\n{\n rt_uint32_t sector_begin; /**< begin sector */\n rt_uint32_t sector_end; /**< end sector */\n};\n\n/**\n * cursor control command\n */\n#define RT_DEVICE_CTRL_CURSOR_SET_POSITION 0x10\n#define RT_DEVICE_CTRL_CURSOR_SET_TYPE 0x11\n\n/**\n * graphic device control command\n */\n#define RTGRAPHIC_CTRL_RECT_UPDATE 0\n#define RTGRAPHIC_CTRL_POWERON 1\n#define RTGRAPHIC_CTRL_POWEROFF 2\n#define RTGRAPHIC_CTRL_GET_INFO 3\n#define RTGRAPHIC_CTRL_SET_MODE 4\n#define RTGRAPHIC_CTRL_GET_EXT 5\n\n/* graphic deice */\nenum\n{\n RTGRAPHIC_PIXEL_FORMAT_MONO = 0,\n RTGRAPHIC_PIXEL_FORMAT_GRAY4,\n RTGRAPHIC_PIXEL_FORMAT_GRAY16,\n RTGRAPHIC_PIXEL_FORMAT_RGB332,\n RTGRAPHIC_PIXEL_FORMAT_RGB444,\n RTGRAPHIC_PIXEL_FORMAT_RGB565,\n RTGRAPHIC_PIXEL_FORMAT_RGB565P,\n RTGRAPHIC_PIXEL_FORMAT_BGR565 = RTGRAPHIC_PIXEL_FORMAT_RGB565P,\n RTGRAPHIC_PIXEL_FORMAT_RGB666,\n RTGRAPHIC_PIXEL_FORMAT_RGB888,\n RTGRAPHIC_PIXEL_FORMAT_ARGB888,\n RTGRAPHIC_PIXEL_FORMAT_ABGR888,\n RTGRAPHIC_PIXEL_FORMAT_ARGB565,\n RTGRAPHIC_PIXEL_FORMAT_ALPHA,\n RTGRAPHIC_PIXEL_FORMAT_COLOR,\n};\n\n/**\n * build a pixel position according to (x, y) coordinates.\n */\n#define RTGRAPHIC_PIXEL_POSITION(x, y) ((x << 16) | y)\n\n/**\n * graphic device information structure\n */\nstruct rt_device_graphic_info\n{\n rt_uint8_t pixel_format; /**< graphic format */\n rt_uint8_t bits_per_pixel; /**< bits per pixel */\n rt_uint16_t reserved; /**< reserved field */\n\n rt_uint16_t width; /**< width of graphic device */\n rt_uint16_t height; /**< height of graphic device */\n\n rt_uint8_t *framebuffer; /**< frame buffer */\n};\n\n/**\n * rectangle information structure\n */\nstruct rt_device_rect_info\n{\n rt_uint16_t x; /**< x coordinate */\n rt_uint16_t y; /**< y coordinate */\n rt_uint16_t width; /**< width */\n rt_uint16_t height; /**< height */\n};\n\n/**\n * graphic operations\n */\nstruct rt_device_graphic_ops\n{\n void (*set_pixel) (const char *pixel, int x, int y);\n void (*get_pixel) (char *pixel, int x, int y);\n\n void (*draw_hline)(const char *pixel, int x1, int x2, int y);\n void (*draw_vline)(const char *pixel, int x, int y1, int y2);\n\n void (*blit_line) (const char *pixel, int x, int y, rt_size_t size);\n};\n#define rt_graphix_ops(device) ((struct rt_device_graphic_ops *)(device->user_data))\n\n/**@}*/\n#endif\n\n/* definitions for libc */\n#include \"rtlibc.h\"\n\n#ifdef __cplusplus\n}\n#endif\n\n#ifdef __cplusplus\n/* RT-Thread definitions for C++ */\nnamespace rtthread {\n\nenum TICK_WAIT {\n WAIT_NONE = 0,\n WAIT_FOREVER = -1,\n};\n\n}\n\n#endif /* end of __cplusplus */\n\n#endif\n", "include/rthw.h": "/*\n * Copyright (c) 2006-2018, RT-Thread Development Team\n *\n * SPDX-License-Identifier: Apache-2.0\n *\n * Change Logs:\n * Date Author Notes\n * 2006-03-18 Bernard the first version\n * 2006-04-25 Bernard add rt_hw_context_switch_interrupt declaration\n * 2006-09-24 Bernard add rt_hw_context_switch_to declaration\n * 2012-12-29 Bernard add rt_hw_exception_install declaration\n * 2017-10-17 Hichard add some micros\n * 2018-11-17 Jesven add rt_hw_spinlock_t\n * add smp support\n */\n\n#ifndef __RT_HW_H__\n#define __RT_HW_H__\n\n#include <rtthread.h>\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n/*\n * Some macros define\n */\n#ifndef HWREG32\n#define HWREG32(x) (*((volatile rt_uint32_t *)(x)))\n#endif\n#ifndef HWREG16\n#define HWREG16(x) (*((volatile rt_uint16_t *)(x)))\n#endif\n#ifndef HWREG8\n#define HWREG8(x) (*((volatile rt_uint8_t *)(x)))\n#endif\n\n#ifndef RT_CPU_CACHE_LINE_SZ\n#define RT_CPU_CACHE_LINE_SZ 32\n#endif\n\nenum RT_HW_CACHE_OPS\n{\n RT_HW_CACHE_FLUSH = 0x01,\n RT_HW_CACHE_INVALIDATE = 0x02,\n};\n\n/*\n * CPU interfaces\n */\nvoid rt_hw_cpu_icache_enable(void);\nvoid rt_hw_cpu_icache_disable(void);\nrt_base_t rt_hw_cpu_icache_status(void);\nvoid rt_hw_cpu_icache_ops(int ops, void* addr, int size);\n\nvoid rt_hw_cpu_dcache_enable(void);\nvoid rt_hw_cpu_dcache_disable(void);\nrt_base_t rt_hw_cpu_dcache_status(void);\nvoid rt_hw_cpu_dcache_ops(int ops, void* addr, int size);\n\nvoid rt_hw_cpu_reset(void);\nvoid rt_hw_cpu_shutdown(void);\n\nrt_uint8_t *rt_hw_stack_init(void *entry,\n void *parameter,\n rt_uint8_t *stack_addr,\n void *exit);\n\n/*\n * Interrupt handler definition\n */\ntypedef void (*rt_isr_handler_t)(int vector, void *param);\n\nstruct rt_irq_desc\n{\n rt_isr_handler_t handler;\n void *param;\n\n#ifdef RT_USING_INTERRUPT_INFO\n char name[RT_NAME_MAX];\n rt_uint32_t counter;\n#endif\n};\n\n/*\n * Interrupt interfaces\n */\nvoid rt_hw_interrupt_init(void);\nvoid rt_hw_interrupt_mask(int vector);\nvoid rt_hw_interrupt_umask(int vector);\nrt_isr_handler_t rt_hw_interrupt_install(int vector,\n rt_isr_handler_t handler,\n void *param,\n const char *name);\n\n#ifdef RT_USING_SMP\nrt_base_t rt_hw_local_irq_disable();\nvoid rt_hw_local_irq_enable(rt_base_t level);\n\n#define rt_hw_interrupt_disable rt_cpus_lock\n#define rt_hw_interrupt_enable rt_cpus_unlock\n\n#else\nrt_base_t rt_hw_interrupt_disable(void);\nvoid rt_hw_interrupt_enable(rt_base_t level);\n#endif /*RT_USING_SMP*/\n\n/*\n * Context interfaces\n */\n#ifdef RT_USING_SMP\nvoid rt_hw_context_switch(rt_ubase_t from, rt_ubase_t to, struct rt_thread *to_thread);\nvoid rt_hw_context_switch_to(rt_ubase_t to, struct rt_thread *to_thread);\nvoid rt_hw_context_switch_interrupt(void *context, rt_ubase_t from, rt_ubase_t to, struct rt_thread *to_thread);\n#else\nvoid rt_hw_context_switch(rt_ubase_t from, rt_ubase_t to);\nvoid rt_hw_context_switch_to(rt_ubase_t to);\nvoid rt_hw_context_switch_interrupt(rt_ubase_t from, rt_ubase_t to);\n#endif /*RT_USING_SMP*/\n\nvoid rt_hw_console_output(const char *str);\n\nvoid rt_hw_backtrace(rt_uint32_t *fp, rt_ubase_t thread_entry);\nvoid rt_hw_show_memory(rt_uint32_t addr, rt_size_t size);\n\n/*\n * Exception interfaces\n */\nvoid rt_hw_exception_install(rt_err_t (*exception_handle)(void *context));\n\n/*\n * delay interfaces\n */\nvoid rt_hw_us_delay(rt_uint32_t us);\n\n#ifdef RT_USING_SMP\ntypedef union {\n unsigned long slock;\n struct __arch_tickets {\n unsigned short owner;\n unsigned short next;\n } tickets;\n} rt_hw_spinlock_t;\n\nstruct rt_spinlock\n{\n rt_hw_spinlock_t lock;\n};\n\nvoid rt_hw_spin_lock_init(rt_hw_spinlock_t *lock);\nvoid rt_hw_spin_lock(rt_hw_spinlock_t *lock);\nvoid rt_hw_spin_unlock(rt_hw_spinlock_t *lock);\n\nint rt_hw_cpu_id(void);\n\nextern rt_hw_spinlock_t _cpus_lock;\nextern rt_hw_spinlock_t _rt_critical_lock;\n\n#define __RT_HW_SPIN_LOCK_INITIALIZER(lockname) {0}\n\n#define __RT_HW_SPIN_LOCK_UNLOCKED(lockname) \\\n (rt_hw_spinlock_t) __RT_HW_SPIN_LOCK_INITIALIZER(lockname)\n\n#define RT_DEFINE_SPINLOCK(x) rt_hw_spinlock_t x = __RT_HW_SPIN_LOCK_UNLOCKED(x)\n#define RT_DECLARE_SPINLOCK(x)\n\n/**\n * ipi function\n */\nvoid rt_hw_ipi_send(int ipi_vector, unsigned int cpu_mask);\n\n/**\n * boot secondary cpu\n */\nvoid rt_hw_secondary_cpu_up(void);\n\n/**\n * secondary cpu idle function\n */\nvoid rt_hw_secondary_cpu_idle_exec(void);\n#else\n\n#define RT_DEFINE_SPINLOCK(x) \n#define RT_DECLARE_SPINLOCK(x) rt_ubase_t x\n\n#define rt_hw_spin_lock(lock) *(lock) = rt_hw_interrupt_disable()\n#define rt_hw_spin_unlock(lock) rt_hw_interrupt_enable(*(lock))\n\n#endif\n\n#ifdef __cplusplus\n}\n#endif\n\n#endif\n", "include/rtlibc.h": "/*\n * Copyright (c) 2006-2018, RT-Thread Development Team\n *\n * SPDX-License-Identifier: Apache-2.0\n *\n * Change Logs:\n * Date Author Notes\n * 2017-01-21 Bernard the first version\n */\n\n#ifndef RTLIBC_H__\n#define RTLIBC_H__\n\n/* definitions for libc if toolchain has no these definitions */\n#include \"libc/libc_stat.h\"\n#include \"libc/libc_errno.h\"\n\n#include \"libc/libc_fcntl.h\"\n#include \"libc/libc_ioctl.h\"\n#include \"libc/libc_dirent.h\"\n#include \"libc/libc_signal.h\"\n#include \"libc/libc_fdset.h\"\n#include \"libc/libc_limits.h\"\n#include \"libc/libc_stdio.h\"\n\n#if defined(__CC_ARM) || defined(__CLANG_ARM) || defined(__IAR_SYSTEMS_ICC__)\ntypedef signed long off_t;\ntypedef int mode_t;\n#endif\n\n#if defined(__MINGW32__) || defined(_WIN32)\ntypedef signed long off_t;\ntypedef int mode_t;\n#endif\n\n#endif\n\n", "include/rtm.h": "/*\n * Copyright (c) 2006-2018, RT-Thread Development Team\n *\n * SPDX-License-Identifier: Apache-2.0\n */\n\n#ifndef __RTM_H__\n#define __RTM_H__\n\n#include <rtdef.h>\n#include <rtthread.h>\n\n#ifdef RT_USING_MODULE\nstruct rt_module_symtab\n{\n void *addr;\n const char *name;\n};\n\n#if defined(_MSC_VER)\n#pragma section(\"RTMSymTab$f\",read)\n#define RTM_EXPORT(symbol) \\\n__declspec(allocate(\"RTMSymTab$f\"))const char __rtmsym_##symbol##_name[] = \"__vs_rtm_\"#symbol;\n#pragma comment(linker, \"/merge:RTMSymTab=mytext\")\n\n#elif defined(__MINGW32__)\n#define RTM_EXPORT(symbol)\n\n#else\n#define RTM_EXPORT(symbol) \\\nconst char __rtmsym_##symbol##_name[] SECTION(\".rodata.name\") = #symbol; \\\nconst struct rt_module_symtab __rtmsym_##symbol SECTION(\"RTMSymTab\")= \\\n{ \\\n (void *)&symbol, \\\n __rtmsym_##symbol##_name \\\n};\n#endif\n\n#else\n#define RTM_EXPORT(symbol)\n#endif\n\n#endif\n", "include/rtservice.h": "/*\n * Copyright (c) 2006-2018, RT-Thread Development Team\n *\n * SPDX-License-Identifier: Apache-2.0\n *\n * Change Logs:\n * Date Author Notes\n * 2006-03-16 Bernard the first version\n * 2006-09-07 Bernard move the kservice APIs to rtthread.h\n * 2007-06-27 Bernard fix the rt_list_remove bug\n * 2012-03-22 Bernard rename kservice.h to rtservice.h\n * 2017-11-15 JasonJia Modify rt_slist_foreach to rt_slist_for_each_entry.\n * Make code cleanup.\n */\n\n#ifndef __RT_SERVICE_H__\n#define __RT_SERVICE_H__\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n/**\n * @addtogroup KernelService\n */\n\n/**@{*/\n\n/**\n * rt_container_of - return the member address of ptr, if the type of ptr is the\n * struct type.\n */\n#define rt_container_of(ptr, type, member) \\\n ((type *)((char *)(ptr) - (unsigned long)(&((type *)0)->member)))\n\n\n/**\n * @brief initialize a list object\n */\n#define RT_LIST_OBJECT_INIT(object) { &(object), &(object) }\n\n/**\n * @brief initialize a list\n *\n * @param l list to be initialized\n */\nrt_inline void rt_list_init(rt_list_t *l)\n{\n l->next = l->prev = l;\n}\n\n/**\n * @brief insert a node after a list\n *\n * @param l list to insert it\n * @param n new node to be inserted\n */\nrt_inline void rt_list_insert_after(rt_list_t *l, rt_list_t *n)\n{\n l->next->prev = n;\n n->next = l->next;\n\n l->next = n;\n n->prev = l;\n}\n\n/**\n * @brief insert a node before a list\n *\n * @param n new node to be inserted\n * @param l list to insert it\n */\nrt_inline void rt_list_insert_before(rt_list_t *l, rt_list_t *n)\n{\n l->prev->next = n;\n n->prev = l->prev;\n\n l->prev = n;\n n->next = l;\n}\n\n/**\n * @brief remove node from list.\n * @param n the node to remove from the list.\n */\nrt_inline void rt_list_remove(rt_list_t *n)\n{\n n->next->prev = n->prev;\n n->prev->next = n->next;\n\n n->next = n->prev = n;\n}\n\n/**\n * @brief tests whether a list is empty\n * @param l the list to test.\n */\nrt_inline int rt_list_isempty(const rt_list_t *l)\n{\n return l->next == l;\n}\n\n/**\n * @brief get the list length\n * @param l the list to get.\n */\nrt_inline unsigned int rt_list_len(const rt_list_t *l)\n{\n unsigned int len = 0;\n const rt_list_t *p = l;\n while (p->next != l)\n {\n p = p->next;\n len ++;\n }\n\n return len;\n}\n\n/**\n * @brief get the struct for this entry\n * @param node the entry point\n * @param type the type of structure\n * @param member the name of list in structure\n */\n#define rt_list_entry(node, type, member) \\\n rt_container_of(node, type, member)\n\n/**\n * rt_list_for_each - iterate over a list\n * @pos: the rt_list_t * to use as a loop cursor.\n * @head: the head for your list.\n */\n#define rt_list_for_each(pos, head) \\\n for (pos = (head)->next; pos != (head); pos = pos->next)\n\n/**\n * rt_list_for_each_safe - iterate over a list safe against removal of list entry\n * @pos: the rt_list_t * to use as a loop cursor.\n * @n: another rt_list_t * to use as temporary storage\n * @head: the head for your list.\n */\n#define rt_list_for_each_safe(pos, n, head) \\\n for (pos = (head)->next, n = pos->next; pos != (head); \\\n pos = n, n = pos->next)\n\n/**\n * rt_list_for_each_entry - iterate over list of given type\n * @pos: the type * to use as a loop cursor.\n * @head: the head for your list.\n * @member: the name of the list_struct within the struct.\n */\n#define rt_list_for_each_entry(pos, head, member) \\\n for (pos = rt_list_entry((head)->next, typeof(*pos), member); \\\n &pos->member != (head); \\\n pos = rt_list_entry(pos->member.next, typeof(*pos), member))\n\n/**\n * rt_list_for_each_entry_safe - iterate over list of given type safe against removal of list entry\n * @pos: the type * to use as a loop cursor.\n * @n: another type * to use as temporary storage\n * @head: the head for your list.\n * @member: the name of the list_struct within the struct.\n */\n#define rt_list_for_each_entry_safe(pos, n, head, member) \\\n for (pos = rt_list_entry((head)->next, typeof(*pos), member), \\\n n = rt_list_entry(pos->member.next, typeof(*pos), member); \\\n &pos->member != (head); \\\n pos = n, n = rt_list_entry(n->member.next, typeof(*n), member))\n\n/**\n * rt_list_first_entry - get the first element from a list\n * @ptr: the list head to take the element from.\n * @type: the type of the struct this is embedded in.\n * @member: the name of the list_struct within the struct.\n *\n * Note, that list is expected to be not empty.\n */\n#define rt_list_first_entry(ptr, type, member) \\\n rt_list_entry((ptr)->next, type, member)\n\n#define RT_SLIST_OBJECT_INIT(object) { RT_NULL }\n\n/**\n * @brief initialize a single list\n *\n * @param l the single list to be initialized\n */\nrt_inline void rt_slist_init(rt_slist_t *l)\n{\n l->next = RT_NULL;\n}\n\nrt_inline void rt_slist_append(rt_slist_t *l, rt_slist_t *n)\n{\n struct rt_slist_node *node;\n\n node = l;\n while (node->next) node = node->next;\n\n /* append the node to the tail */\n node->next = n;\n n->next = RT_NULL;\n}\n\nrt_inline void rt_slist_insert(rt_slist_t *l, rt_slist_t *n)\n{\n n->next = l->next;\n l->next = n;\n}\n\nrt_inline unsigned int rt_slist_len(const rt_slist_t *l)\n{\n unsigned int len = 0;\n const rt_slist_t *list = l->next;\n while (list != RT_NULL)\n {\n list = list->next;\n len ++;\n }\n\n return len;\n}\n\nrt_inline rt_slist_t *rt_slist_remove(rt_slist_t *l, rt_slist_t *n)\n{\n /* remove slist head */\n struct rt_slist_node *node = l;\n while (node->next && node->next != n) node = node->next;\n\n /* remove node */\n if (node->next != (rt_slist_t *)0) node->next = node->next->next;\n\n return l;\n}\n\nrt_inline rt_slist_t *rt_slist_first(rt_slist_t *l)\n{\n return l->next;\n}\n\nrt_inline rt_slist_t *rt_slist_tail(rt_slist_t *l)\n{\n while (l->next) l = l->next;\n\n return l;\n}\n\nrt_inline rt_slist_t *rt_slist_next(rt_slist_t *n)\n{\n return n->next;\n}\n\nrt_inline int rt_slist_isempty(rt_slist_t *l)\n{\n return l->next == RT_NULL;\n}\n\n/**\n * @brief get the struct for this single list node\n * @param node the entry point\n * @param type the type of structure\n * @param member the name of list in structure\n */\n#define rt_slist_entry(node, type, member) \\\n rt_container_of(node, type, member)\n\n/**\n * rt_slist_for_each - iterate over a single list\n * @pos: the rt_slist_t * to use as a loop cursor.\n * @head: the head for your single list.\n */\n#define rt_slist_for_each(pos, head) \\\n for (pos = (head)->next; pos != RT_NULL; pos = pos->next)\n\n/**\n * rt_slist_for_each_entry - iterate over single list of given type\n * @pos: the type * to use as a loop cursor.\n * @head: the head for your single list.\n * @member: the name of the list_struct within the struct.\n */\n#define rt_slist_for_each_entry(pos, head, member) \\\n for (pos = rt_slist_entry((head)->next, typeof(*pos), member); \\\n &pos->member != (RT_NULL); \\\n pos = rt_slist_entry(pos->member.next, typeof(*pos), member))\n\n/**\n * rt_slist_first_entry - get the first element from a slist\n * @ptr: the slist head to take the element from.\n * @type: the type of the struct this is embedded in.\n * @member: the name of the slist_struct within the struct.\n *\n * Note, that slist is expected to be not empty.\n */\n#define rt_slist_first_entry(ptr, type, member) \\\n rt_slist_entry((ptr)->next, type, member)\n\n/**\n * rt_slist_tail_entry - get the tail element from a slist\n * @ptr: the slist head to take the element from.\n * @type: the type of the struct this is embedded in.\n * @member: the name of the slist_struct within the struct.\n *\n * Note, that slist is expected to be not empty.\n */\n#define rt_slist_tail_entry(ptr, type, member) \\\n rt_slist_entry(rt_slist_tail(ptr), type, member)\n\n/**@}*/\n\n#ifdef __cplusplus\n}\n#endif\n\n#endif\n", "include/rtthread.h": "/*\n * Copyright (c) 2006-2018, RT-Thread Development Team\n *\n * SPDX-License-Identifier: Apache-2.0\n *\n * Change Logs:\n * Date Author Notes\n * 2006-03-18 Bernard the first version\n * 2006-04-26 Bernard add semaphore APIs\n * 2006-08-10 Bernard add version information\n * 2007-01-28 Bernard rename RT_OBJECT_Class_Static to RT_Object_Class_Static\n * 2007-03-03 Bernard clean up the definitions to rtdef.h\n * 2010-04-11 yi.qiu add module feature\n * 2013-06-24 Bernard add rt_kprintf re-define when not use RT_USING_CONSOLE.\n * 2016-08-09 ArdaFu add new thread and interrupt hook.\n * 2018-11-22 Jesven add all cpu's lock and ipi handler\n */\n\n#ifndef __RT_THREAD_H__\n#define __RT_THREAD_H__\n\n#include <rtconfig.h>\n#include <rtdebug.h>\n#include <rtdef.h>\n#include <rtservice.h>\n#include <rtm.h>\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n/**\n * @addtogroup KernelObject\n */\n\n/**@{*/\n\n/*\n * kernel object interface\n */\nvoid rt_system_object_init(void);\nstruct rt_object_information *\nrt_object_get_information(enum rt_object_class_type type);\nint rt_object_get_length(enum rt_object_class_type type);\nint rt_object_get_pointers(enum rt_object_class_type type, rt_object_t *pointers, int maxlen);\n\nvoid rt_object_init(struct rt_object *object,\n enum rt_object_class_type type,\n const char *name);\nvoid rt_object_detach(rt_object_t object);\nrt_object_t rt_object_allocate(enum rt_object_class_type type,\n const char *name);\nvoid rt_object_delete(rt_object_t object);\nrt_bool_t rt_object_is_systemobject(rt_object_t object);\nrt_uint8_t rt_object_get_type(rt_object_t object);\nrt_object_t rt_object_find(const char *name, rt_uint8_t type);\n\n#ifdef RT_USING_HOOK\nvoid rt_object_attach_sethook(void (*hook)(struct rt_object *object));\nvoid rt_object_detach_sethook(void (*hook)(struct rt_object *object));\nvoid rt_object_trytake_sethook(void (*hook)(struct rt_object *object));\nvoid rt_object_take_sethook(void (*hook)(struct rt_object *object));\nvoid rt_object_put_sethook(void (*hook)(struct rt_object *object));\n#endif\n\n/**@}*/\n\n/**\n * @addtogroup Clock\n */\n\n/**@{*/\n\n/*\n * clock & timer interface\n */\nvoid rt_system_tick_init(void);\nrt_tick_t rt_tick_get(void);\nvoid rt_tick_set(rt_tick_t tick);\nvoid rt_tick_increase(void);\nrt_tick_t rt_tick_from_millisecond(rt_int32_t ms);\n\nvoid rt_system_timer_init(void);\nvoid rt_system_timer_thread_init(void);\n\nvoid rt_timer_init(rt_timer_t timer,\n const char *name,\n void (*timeout)(void *parameter),\n void *parameter,\n rt_tick_t time,\n rt_uint8_t flag);\nrt_err_t rt_timer_detach(rt_timer_t timer);\nrt_timer_t rt_timer_create(const char *name,\n void (*timeout)(void *parameter),\n void *parameter,\n rt_tick_t time,\n rt_uint8_t flag);\nrt_err_t rt_timer_delete(rt_timer_t timer);\nrt_err_t rt_timer_start(rt_timer_t timer);\nrt_err_t rt_timer_stop(rt_timer_t timer);\nrt_err_t rt_timer_control(rt_timer_t timer, int cmd, void *arg);\n\nrt_tick_t rt_timer_next_timeout_tick(void);\nvoid rt_timer_check(void);\n\n#ifdef RT_USING_HOOK\nvoid rt_timer_enter_sethook(void (*hook)(struct rt_timer *timer));\nvoid rt_timer_exit_sethook(void (*hook)(struct rt_timer *timer));\n#endif\n\n/**@}*/\n\n/**\n * @addtogroup Thread\n */\n\n/**@{*/\n\n/*\n * thread interface\n */\nrt_err_t rt_thread_init(struct rt_thread *thread,\n const char *name,\n void (*entry)(void *parameter),\n void *parameter,\n void *stack_start,\n rt_uint32_t stack_size,\n rt_uint8_t priority,\n rt_uint32_t tick);\nrt_err_t rt_thread_detach(rt_thread_t thread);\nrt_thread_t rt_thread_create(const char *name,\n void (*entry)(void *parameter),\n void *parameter,\n rt_uint32_t stack_size,\n rt_uint8_t priority,\n rt_uint32_t tick);\nrt_thread_t rt_thread_self(void);\nrt_thread_t rt_thread_find(char *name);\nrt_err_t rt_thread_startup(rt_thread_t thread);\nrt_err_t rt_thread_delete(rt_thread_t thread);\n\nrt_err_t rt_thread_yield(void);\nrt_err_t rt_thread_delay(rt_tick_t tick);\nrt_err_t rt_thread_delay_until(rt_tick_t *tick, rt_tick_t inc_tick);\nrt_err_t rt_thread_mdelay(rt_int32_t ms);\nrt_err_t rt_thread_control(rt_thread_t thread, int cmd, void *arg);\nrt_err_t rt_thread_suspend(rt_thread_t thread);\nrt_err_t rt_thread_resume(rt_thread_t thread);\nvoid rt_thread_timeout(void *parameter);\n\n#ifdef RT_USING_SIGNALS\nvoid rt_thread_alloc_sig(rt_thread_t tid);\nvoid rt_thread_free_sig(rt_thread_t tid);\nint rt_thread_kill(rt_thread_t tid, int sig);\n#endif\n\n#ifdef RT_USING_HOOK\nvoid rt_thread_suspend_sethook(void (*hook)(rt_thread_t thread));\nvoid rt_thread_resume_sethook (void (*hook)(rt_thread_t thread));\nvoid rt_thread_inited_sethook (void (*hook)(rt_thread_t thread));\n#endif\n\n/*\n * idle thread interface\n */\nvoid rt_thread_idle_init(void);\n#if defined(RT_USING_HOOK) || defined(RT_USING_IDLE_HOOK)\nrt_err_t rt_thread_idle_sethook(void (*hook)(void));\nrt_err_t rt_thread_idle_delhook(void (*hook)(void));\n#endif\nvoid rt_thread_idle_excute(void);\nrt_thread_t rt_thread_idle_gethandler(void);\n\n/*\n * schedule service\n */\nvoid rt_system_scheduler_init(void);\nvoid rt_system_scheduler_start(void);\n\nvoid rt_schedule(void);\nvoid rt_schedule_insert_thread(struct rt_thread *thread);\nvoid rt_schedule_remove_thread(struct rt_thread *thread);\n\nvoid rt_enter_critical(void);\nvoid rt_exit_critical(void);\nrt_uint16_t rt_critical_level(void);\n\n#ifdef RT_USING_HOOK\nvoid rt_scheduler_sethook(void (*hook)(rt_thread_t from, rt_thread_t to));\n#endif\n\n#ifdef RT_USING_SMP\nvoid rt_scheduler_ipi_handler(int vector, void *param);\n#endif\n\n/**@}*/\n\n/**\n * @addtogroup Signals\n * @{\n */\n#ifdef RT_USING_SIGNALS\nvoid rt_signal_mask(int signo);\nvoid rt_signal_unmask(int signo);\nrt_sighandler_t rt_signal_install(int signo, rt_sighandler_t handler);\nint rt_signal_wait(const rt_sigset_t *set, rt_siginfo_t *si, rt_int32_t timeout);\n\nint rt_system_signal_init(void);\n#endif\n/*@}*/\n\n/**\n * @addtogroup MM\n */\n\n/**@{*/\n\n/*\n * memory management interface\n */\n#ifdef RT_USING_MEMPOOL\n/*\n * memory pool interface\n */\nrt_err_t rt_mp_init(struct rt_mempool *mp,\n const char *name,\n void *start,\n rt_size_t size,\n rt_size_t block_size);\nrt_err_t rt_mp_detach(struct rt_mempool *mp);\nrt_mp_t rt_mp_create(const char *name,\n rt_size_t block_count,\n rt_size_t block_size);\nrt_err_t rt_mp_delete(rt_mp_t mp);\n\nvoid *rt_mp_alloc(rt_mp_t mp, rt_int32_t time);\nvoid rt_mp_free(void *block);\n\n#ifdef RT_USING_HOOK\nvoid rt_mp_alloc_sethook(void (*hook)(struct rt_mempool *mp, void *block));\nvoid rt_mp_free_sethook(void (*hook)(struct rt_mempool *mp, void *block));\n#endif\n\n#endif\n\n#ifdef RT_USING_HEAP\n/*\n * heap memory interface\n */\nvoid rt_system_heap_init(void *begin_addr, void *end_addr);\n\nvoid *rt_malloc(rt_size_t nbytes);\nvoid rt_free(void *ptr);\nvoid *rt_realloc(void *ptr, rt_size_t nbytes);\nvoid *rt_calloc(rt_size_t count, rt_size_t size);\nvoid *rt_malloc_align(rt_size_t size, rt_size_t align);\nvoid rt_free_align(void *ptr);\n\nvoid rt_memory_info(rt_uint32_t *total,\n rt_uint32_t *used,\n rt_uint32_t *max_used);\n\n#ifdef RT_USING_SLAB\nvoid *rt_page_alloc(rt_size_t npages);\nvoid rt_page_free(void *addr, rt_size_t npages);\n#endif\n\n#ifdef RT_USING_HOOK\nvoid rt_malloc_sethook(void (*hook)(void *ptr, rt_size_t size));\nvoid rt_free_sethook(void (*hook)(void *ptr));\n#endif\n\n#endif\n\n#ifdef RT_USING_MEMHEAP\n/**\n * memory heap object interface\n */\nrt_err_t rt_memheap_init(struct rt_memheap *memheap,\n const char *name,\n void *start_addr,\n rt_size_t size);\nrt_err_t rt_memheap_detach(struct rt_memheap *heap);\nvoid *rt_memheap_alloc(struct rt_memheap *heap, rt_size_t size);\nvoid *rt_memheap_realloc(struct rt_memheap *heap, void *ptr, rt_size_t newsize);\nvoid rt_memheap_free(void *ptr);\n#endif\n\n/**@}*/\n\n/**\n * @addtogroup IPC\n */\n\n/**@{*/\n\n#ifdef RT_USING_SEMAPHORE\n/*\n * semaphore interface\n */\nrt_err_t rt_sem_init(rt_sem_t sem,\n const char *name,\n rt_uint32_t value,\n rt_uint8_t flag);\nrt_err_t rt_sem_detach(rt_sem_t sem);\nrt_sem_t rt_sem_create(const char *name, rt_uint32_t value, rt_uint8_t flag);\nrt_err_t rt_sem_delete(rt_sem_t sem);\n\nrt_err_t rt_sem_take(rt_sem_t sem, rt_int32_t time);\nrt_err_t rt_sem_trytake(rt_sem_t sem);\nrt_err_t rt_sem_release(rt_sem_t sem);\nrt_err_t rt_sem_control(rt_sem_t sem, int cmd, void *arg);\n#endif\n\n#ifdef RT_USING_MUTEX\n/*\n * mutex interface\n */\nrt_err_t rt_mutex_init(rt_mutex_t mutex, const char *name, rt_uint8_t flag);\nrt_err_t rt_mutex_detach(rt_mutex_t mutex);\nrt_mutex_t rt_mutex_create(const char *name, rt_uint8_t flag);\nrt_err_t rt_mutex_delete(rt_mutex_t mutex);\n\nrt_err_t rt_mutex_take(rt_mutex_t mutex, rt_int32_t time);\nrt_err_t rt_mutex_release(rt_mutex_t mutex);\nrt_err_t rt_mutex_control(rt_mutex_t mutex, int cmd, void *arg);\n#endif\n\n#ifdef RT_USING_EVENT\n/*\n * event interface\n */\nrt_err_t rt_event_init(rt_event_t event, const char *name, rt_uint8_t flag);\nrt_err_t rt_event_detach(rt_event_t event);\nrt_event_t rt_event_create(const char *name, rt_uint8_t flag);\nrt_err_t rt_event_delete(rt_event_t event);\n\nrt_err_t rt_event_send(rt_event_t event, rt_uint32_t set);\nrt_err_t rt_event_recv(rt_event_t event,\n rt_uint32_t set,\n rt_uint8_t opt,\n rt_int32_t timeout,\n rt_uint32_t *recved);\nrt_err_t rt_event_control(rt_event_t event, int cmd, void *arg);\n#endif\n\n#ifdef RT_USING_MAILBOX\n/*\n * mailbox interface\n */\nrt_err_t rt_mb_init(rt_mailbox_t mb,\n const char *name,\n void *msgpool,\n rt_size_t size,\n rt_uint8_t flag);\nrt_err_t rt_mb_detach(rt_mailbox_t mb);\nrt_mailbox_t rt_mb_create(const char *name, rt_size_t size, rt_uint8_t flag);\nrt_err_t rt_mb_delete(rt_mailbox_t mb);\n\nrt_err_t rt_mb_send(rt_mailbox_t mb, rt_ubase_t value);\nrt_err_t rt_mb_send_wait(rt_mailbox_t mb,\n rt_ubase_t value,\n rt_int32_t timeout);\nrt_err_t rt_mb_recv(rt_mailbox_t mb, rt_ubase_t *value, rt_int32_t timeout);\nrt_err_t rt_mb_control(rt_mailbox_t mb, int cmd, void *arg);\n#endif\n\n#ifdef RT_USING_MESSAGEQUEUE\n/*\n * message queue interface\n */\nrt_err_t rt_mq_init(rt_mq_t mq,\n const char *name,\n void *msgpool,\n rt_size_t msg_size,\n rt_size_t pool_size,\n rt_uint8_t flag);\nrt_err_t rt_mq_detach(rt_mq_t mq);\nrt_mq_t rt_mq_create(const char *name,\n rt_size_t msg_size,\n rt_size_t max_msgs,\n rt_uint8_t flag);\nrt_err_t rt_mq_delete(rt_mq_t mq);\n\nrt_err_t rt_mq_send(rt_mq_t mq, const void *buffer, rt_size_t size);\nrt_err_t rt_mq_send_wait(rt_mq_t mq,\n const void *buffer,\n rt_size_t size,\n rt_int32_t timeout);\nrt_err_t rt_mq_urgent(rt_mq_t mq, const void *buffer, rt_size_t size);\nrt_err_t rt_mq_recv(rt_mq_t mq,\n void *buffer,\n rt_size_t size,\n rt_int32_t timeout);\nrt_err_t rt_mq_control(rt_mq_t mq, int cmd, void *arg);\n#endif\n\n/*\n * spinlock\n */\n#ifdef RT_USING_SMP\nstruct rt_spinlock;\n\nvoid rt_spin_lock_init(struct rt_spinlock *lock);\nvoid rt_spin_lock(struct rt_spinlock *lock);\nvoid rt_spin_unlock(struct rt_spinlock *lock);\nrt_base_t rt_spin_lock_irqsave(struct rt_spinlock *lock);\nvoid rt_spin_unlock_irqrestore(struct rt_spinlock *lock, rt_base_t level);\n\n#else\n#define rt_spin_lock_init(lock) /* nothing */\n#define rt_spin_lock(lock) rt_enter_critical()\n#define rt_spin_unlock(lock) rt_exit_critical()\n#define rt_spin_lock_irqsave(lock) rt_hw_interrupt_disable()\n#define rt_spin_unlock_irqrestore(lock, level) rt_hw_interrupt_enable(level)\n\n#endif\n\n/**@}*/\n\n#ifdef RT_USING_DEVICE\n/**\n * @addtogroup Device\n */\n\n/**@{*/\n\n/*\n * device (I/O) system interface\n */\nrt_device_t rt_device_find(const char *name);\n\nrt_err_t rt_device_register(rt_device_t dev,\n const char *name,\n rt_uint16_t flags);\nrt_err_t rt_device_unregister(rt_device_t dev);\n\nrt_device_t rt_device_create(int type, int attach_size);\nvoid rt_device_destroy(rt_device_t device);\n\nrt_err_t rt_device_init_all(void);\n\nrt_err_t\nrt_device_set_rx_indicate(rt_device_t dev,\n rt_err_t (*rx_ind)(rt_device_t dev, rt_size_t size));\nrt_err_t\nrt_device_set_tx_complete(rt_device_t dev,\n rt_err_t (*tx_done)(rt_device_t dev, void *buffer));\n\nrt_err_t rt_device_init (rt_device_t dev);\nrt_err_t rt_device_open (rt_device_t dev, rt_uint16_t oflag);\nrt_err_t rt_device_close(rt_device_t dev);\nrt_size_t rt_device_read (rt_device_t dev,\n rt_off_t pos,\n void *buffer,\n rt_size_t size);\nrt_size_t rt_device_write(rt_device_t dev,\n rt_off_t pos,\n const void *buffer,\n rt_size_t size);\nrt_err_t rt_device_control(rt_device_t dev, int cmd, void *arg);\n\n/**@}*/\n#endif\n\n/*\n * interrupt service\n */\n\n/*\n * rt_interrupt_enter and rt_interrupt_leave only can be called by BSP\n */\nvoid rt_interrupt_enter(void);\nvoid rt_interrupt_leave(void);\n\n#ifdef RT_USING_SMP\n\n/*\n * smp cpus lock service\n */\n\nrt_base_t rt_cpus_lock(void);\nvoid rt_cpus_unlock(rt_base_t level);\n\nstruct rt_cpu *rt_cpu_self(void);\nstruct rt_cpu *rt_cpu_index(int index);\n\n#endif\n\n/*\n * the number of nested interrupts.\n */\nrt_uint8_t rt_interrupt_get_nest(void);\n\n#ifdef RT_USING_HOOK\nvoid rt_interrupt_enter_sethook(void (*hook)(void));\nvoid rt_interrupt_leave_sethook(void (*hook)(void));\n#endif\n\n#ifdef RT_USING_COMPONENTS_INIT\nvoid rt_components_init(void);\nvoid rt_components_board_init(void);\n#endif\n\n/**\n * @addtogroup KernelService\n */\n\n/**@{*/\n\n/*\n * general kernel service\n */\n#ifndef RT_USING_CONSOLE\n#define rt_kprintf(...)\n#define rt_kputs(str)\n#else\nvoid rt_kprintf(const char *fmt, ...);\nvoid rt_kputs(const char *str);\n#endif\nrt_int32_t rt_vsprintf(char *dest, const char *format, va_list arg_ptr);\nrt_int32_t rt_vsnprintf(char *buf, rt_size_t size, const char *fmt, va_list args);\nrt_int32_t rt_sprintf(char *buf, const char *format, ...);\nrt_int32_t rt_snprintf(char *buf, rt_size_t size, const char *format, ...);\n\n#if defined(RT_USING_DEVICE) && defined(RT_USING_CONSOLE)\nrt_device_t rt_console_set_device(const char *name);\nrt_device_t rt_console_get_device(void);\n#endif\n\nrt_err_t rt_get_errno(void);\nvoid rt_set_errno(rt_err_t no);\nint *_rt_errno(void);\n#if !defined(RT_USING_NEWLIB) && !defined(_WIN32)\n#ifndef errno\n#define errno *_rt_errno()\n#endif\n#endif\n\nint __rt_ffs(int value);\n\nvoid *rt_memset(void *src, int c, rt_ubase_t n);\nvoid *rt_memcpy(void *dest, const void *src, rt_ubase_t n);\n\nrt_int32_t rt_strncmp(const char *cs, const char *ct, rt_ubase_t count);\nrt_int32_t rt_strcmp(const char *cs, const char *ct);\nrt_size_t rt_strlen(const char *src);\nrt_size_t rt_strnlen(const char *s, rt_ubase_t maxlen);\nchar *rt_strdup(const char *s);\n#if defined(__CC_ARM) || defined(__CLANG_ARM)\n/* leak strdup interface */\nchar* strdup(const char* str);\n#endif\n\nchar *rt_strstr(const char *str1, const char *str2);\nrt_int32_t rt_sscanf(const char *buf, const char *fmt, ...);\nchar *rt_strncpy(char *dest, const char *src, rt_ubase_t n);\nvoid *rt_memmove(void *dest, const void *src, rt_ubase_t n);\nrt_int32_t rt_memcmp(const void *cs, const void *ct, rt_ubase_t count);\nrt_int32_t rt_strcasecmp(const char *a, const char *b);\n\nvoid rt_show_version(void);\n\n#ifdef RT_DEBUG\nextern void (*rt_assert_hook)(const char *ex, const char *func, rt_size_t line);\nvoid rt_assert_set_hook(void (*hook)(const char *ex, const char *func, rt_size_t line));\n\nvoid rt_assert_handler(const char *ex, const char *func, rt_size_t line);\n#endif /* RT_DEBUG */\n\n#ifdef RT_USING_FINSH\n#include <finsh_api.h>\n#endif\n\n/**@}*/\n\n#ifdef __cplusplus\n}\n#endif\n\n#endif\n", "rtconfig.h": "#ifndef RTCONFIG_H\n#define RTCONFIG_H\n#include <signal.h>\n#define HAVE_SIGVAL\n#define HAVE_SIGEVENT\n#define HAVE_SIGINFO\n#define RT_USING_NEWLIB\n#define RT_USING_DEVICE\n#define RT_USING_MUTEX\n#define ARCH_CPU_64BIT\n#define RT_NAME_MAX 16\n#define RT_ALIGN_SIZE 8\n#define RT_THREAD_PRIORITY_MAX 32\n#define RT_TICK_PER_SECOND 1000\n#define RT_USING_HEAP\n#define RT_USING_SEMAPHORE\n#define RT_DEBUG\n#define RT_CONSOLEBUF_SIZE 256\n#define IDLE_THREAD_STACK_SIZE 65536\n#endif\n", "src/clock.c": "/*\n * Copyright (c) 2006-2018, RT-Thread Development Team\n *\n * SPDX-License-Identifier: Apache-2.0\n *\n * Change Logs:\n * Date Author Notes\n * 2006-03-12 Bernard first version\n * 2006-05-27 Bernard add support for same priority thread schedule\n * 2006-08-10 Bernard remove the last rt_schedule in rt_tick_increase\n * 2010-03-08 Bernard remove rt_passed_second\n * 2010-05-20 Bernard fix the tick exceeds the maximum limits\n * 2010-07-13 Bernard fix rt_tick_from_millisecond issue found by kuronca\n * 2011-06-26 Bernard add rt_tick_set function.\n * 2018-11-22 Jesven add per cpu tick\n */\n\n#include <rthw.h>\n#include <rtthread.h>\n\n#ifdef RT_USING_SMP\n#define rt_tick rt_cpu_index(0)->tick\n#else\nstatic rt_tick_t rt_tick = 0;\n#endif\n\n/**\n * This function will initialize system tick and set it to zero.\n * @ingroup SystemInit\n *\n * @deprecated since 1.1.0, this function does not need to be invoked\n * in the system initialization.\n */\nvoid rt_system_tick_init(void)\n{\n}\n\n/**\n * @addtogroup Clock\n */\n\n/**@{*/\n\n/**\n * This function will return current tick from operating system startup\n *\n * @return current tick\n */\nrt_tick_t rt_tick_get(void)\n{\n /* return the global tick */\n return rt_tick;\n}\nRTM_EXPORT(rt_tick_get);\n\n/**\n * This function will set current tick\n */\nvoid rt_tick_set(rt_tick_t tick)\n{\n rt_base_t level;\n\n level = rt_hw_interrupt_disable();\n rt_tick = tick;\n rt_hw_interrupt_enable(level);\n}\n\n/**\n * This function will notify kernel there is one tick passed. Normally,\n * this function is invoked by clock ISR.\n */\nvoid rt_tick_increase(void)\n{\n struct rt_thread *thread;\n\n /* increase the global tick */\n#ifdef RT_USING_SMP\n rt_cpu_self()->tick ++;\n#else\n ++ rt_tick;\n#endif\n\n /* check time slice */\n thread = rt_thread_self();\n\n -- thread->remaining_tick;\n if (thread->remaining_tick == 0)\n {\n /* change to initialized tick */\n thread->remaining_tick = thread->init_tick;\n\n thread->stat |= RT_THREAD_STAT_YIELD;\n\n /* yield */\n rt_thread_yield();\n }\n\n /* check timer */\n rt_timer_check();\n}\n\n/**\n * This function will calculate the tick from millisecond.\n *\n * @param ms the specified millisecond\n * - Negative Number wait forever\n * - Zero not wait\n * - Max 0x7fffffff\n *\n * @return the calculated tick\n */\nrt_tick_t rt_tick_from_millisecond(rt_int32_t ms)\n{\n rt_tick_t tick;\n\n if (ms < 0)\n {\n tick = (rt_tick_t)RT_WAITING_FOREVER;\n }\n else\n {\n tick = RT_TICK_PER_SECOND * (ms / 1000);\n tick += (RT_TICK_PER_SECOND * (ms % 1000) + 999) / 1000;\n }\n \n /* return the calculated tick */\n return tick;\n}\nRTM_EXPORT(rt_tick_from_millisecond);\n\n/**@}*/\n\n", "src/idle.c": "/*\n * Copyright (c) 2006-2018, RT-Thread Development Team\n *\n * SPDX-License-Identifier: Apache-2.0\n *\n * Change Logs:\n * Date Author Notes\n * 2006-03-23 Bernard the first version\n * 2010-11-10 Bernard add cleanup callback function in thread exit.\n * 2012-12-29 Bernard fix compiling warning.\n * 2013-12-21 Grissiom let rt_thread_idle_excute loop until there is no\n * dead thread.\n * 2016-08-09 ArdaFu add method to get the handler of the idle thread.\n * 2018-02-07 Bernard lock scheduler to protect tid->cleanup.\n * 2018-07-14 armink add idle hook list\n * 2018-11-22 Jesven add per cpu idle task\n * combine the code of primary and secondary cpu\n */\n\n#include <rthw.h>\n#include <rtthread.h>\n\n#ifdef RT_USING_MODULE\n#include <dlmodule.h>\n#endif\n\n#if defined (RT_USING_HOOK)\n#ifndef RT_USING_IDLE_HOOK\n#define RT_USING_IDLE_HOOK\n#endif\n#endif\n\n#ifndef IDLE_THREAD_STACK_SIZE\n#if defined (RT_USING_IDLE_HOOK) || defined(RT_USING_HEAP)\n#define IDLE_THREAD_STACK_SIZE 256\n#else\n#define IDLE_THREAD_STACK_SIZE 128\n#endif\n#endif\n\n#ifdef RT_USING_SMP\n#define _CPUS_NR RT_CPUS_NR\n#else\n#define _CPUS_NR 1\n#endif\n\nextern rt_list_t rt_thread_defunct;\n\nstatic struct rt_thread idle[_CPUS_NR];\nALIGN(RT_ALIGN_SIZE)\nstatic rt_uint8_t rt_thread_stack[_CPUS_NR][IDLE_THREAD_STACK_SIZE];\n\n#ifdef RT_USING_IDLE_HOOK\n#ifndef RT_IDLE_HOOK_LIST_SIZE\n#define RT_IDLE_HOOK_LIST_SIZE 4\n#endif\n\nstatic void (*idle_hook_list[RT_IDLE_HOOK_LIST_SIZE])(void);\n\n/**\n * @ingroup Hook\n * This function sets a hook function to idle thread loop. When the system performs\n * idle loop, this hook function should be invoked.\n *\n * @param hook the specified hook function\n *\n * @return RT_EOK: set OK\n * -RT_EFULL: hook list is full\n *\n * @note the hook function must be simple and never be blocked or suspend.\n */\nrt_err_t rt_thread_idle_sethook(void (*hook)(void))\n{\n rt_size_t i;\n rt_base_t level;\n rt_err_t ret = -RT_EFULL;\n\n /* disable interrupt */\n level = rt_hw_interrupt_disable();\n\n for (i = 0; i < RT_IDLE_HOOK_LIST_SIZE; i++)\n {\n if (idle_hook_list[i] == RT_NULL)\n {\n idle_hook_list[i] = hook;\n ret = RT_EOK;\n break;\n }\n }\n /* enable interrupt */\n rt_hw_interrupt_enable(level);\n\n return ret;\n}\n\n/**\n * delete the idle hook on hook list\n *\n * @param hook the specified hook function\n *\n * @return RT_EOK: delete OK\n * -RT_ENOSYS: hook was not found\n */\nrt_err_t rt_thread_idle_delhook(void (*hook)(void))\n{\n rt_size_t i;\n rt_base_t level;\n rt_err_t ret = -RT_ENOSYS;\n\n /* disable interrupt */\n level = rt_hw_interrupt_disable();\n\n for (i = 0; i < RT_IDLE_HOOK_LIST_SIZE; i++)\n {\n if (idle_hook_list[i] == hook)\n {\n idle_hook_list[i] = RT_NULL;\n ret = RT_EOK;\n break;\n }\n }\n /* enable interrupt */\n rt_hw_interrupt_enable(level);\n\n return ret;\n}\n\n#endif\n\n/* Return whether there is defunctional thread to be deleted. */\nrt_inline int _has_defunct_thread(void)\n{\n /* The rt_list_isempty has prototype of \"int rt_list_isempty(const rt_list_t *l)\".\n * So the compiler has a good reason that the rt_thread_defunct list does\n * not change within rt_thread_idle_excute thus optimize the \"while\" loop\n * into a \"if\".\n *\n * So add the volatile qualifier here. */\n const volatile rt_list_t *l = (const volatile rt_list_t *)&rt_thread_defunct;\n\n return l->next != l;\n}\n\n/**\n * @ingroup Thread\n *\n * This function will perform system background job when system idle.\n */\nvoid rt_thread_idle_excute(void)\n{\n /* Loop until there is no dead thread. So one call to rt_thread_idle_excute\n * will do all the cleanups. */\n while (_has_defunct_thread())\n {\n rt_base_t lock;\n rt_thread_t thread;\n#ifdef RT_USING_MODULE\n struct rt_dlmodule *module = RT_NULL;\n#endif\n RT_DEBUG_NOT_IN_INTERRUPT;\n\n /* disable interrupt */\n lock = rt_hw_interrupt_disable();\n\n /* re-check whether list is empty */\n if (_has_defunct_thread())\n {\n /* get defunct thread */\n thread = rt_list_entry(rt_thread_defunct.next,\n struct rt_thread,\n tlist);\n#ifdef RT_USING_MODULE\n module = (struct rt_dlmodule*)thread->module_id;\n if (module)\n {\n dlmodule_destroy(module);\n }\n#endif\n /* remove defunct thread */\n rt_list_remove(&(thread->tlist));\n\n /* lock scheduler to prevent scheduling in cleanup function. */\n rt_enter_critical();\n\n /* invoke thread cleanup */\n if (thread->cleanup != RT_NULL)\n thread->cleanup(thread);\n\n#ifdef RT_USING_SIGNALS\n rt_thread_free_sig(thread);\n#endif\n\n /* if it's a system object, not delete it */\n if (rt_object_is_systemobject((rt_object_t)thread) == RT_TRUE)\n {\n /* detach this object */\n rt_object_detach((rt_object_t)thread);\n /* unlock scheduler */\n rt_exit_critical();\n\n /* enable interrupt */\n rt_hw_interrupt_enable(lock);\n\n return;\n }\n\n /* unlock scheduler */\n rt_exit_critical();\n }\n else\n {\n /* enable interrupt */\n rt_hw_interrupt_enable(lock);\n\n /* may the defunct thread list is removed by others, just return */\n return;\n }\n\n /* enable interrupt */\n rt_hw_interrupt_enable(lock);\n\n#ifdef RT_USING_HEAP\n /* release thread's stack */\n RT_KERNEL_FREE(thread->stack_addr);\n /* delete thread object */\n rt_object_delete((rt_object_t)thread);\n#endif\n }\n}\n\nextern void rt_system_power_manager(void);\nstatic void rt_thread_idle_entry(void *parameter)\n{\n#ifdef RT_USING_SMP\n if (rt_hw_cpu_id() != 0)\n {\n while (1)\n {\n rt_hw_secondary_cpu_idle_exec();\n }\n }\n#endif\n\n while (1)\n {\n#ifdef RT_USING_IDLE_HOOK\n rt_size_t i;\n\n for (i = 0; i < RT_IDLE_HOOK_LIST_SIZE; i++)\n {\n if (idle_hook_list[i] != RT_NULL)\n {\n idle_hook_list[i]();\n }\n }\n#endif\n\n rt_thread_idle_excute();\n#ifdef RT_USING_PM \n rt_system_power_manager();\n#endif\n }\n}\n\n/**\n * @ingroup SystemInit\n *\n * This function will initialize idle thread, then start it.\n *\n * @note this function must be invoked when system init.\n */\nvoid rt_thread_idle_init(void)\n{\n rt_ubase_t i;\n char tidle_name[RT_NAME_MAX];\n\n for (i = 0; i < _CPUS_NR; i++)\n {\n rt_sprintf(tidle_name, \"tidle%d\", i);\n rt_thread_init(&idle[i],\n tidle_name,\n rt_thread_idle_entry,\n RT_NULL,\n &rt_thread_stack[i][0],\n sizeof(rt_thread_stack[i]),\n RT_THREAD_PRIORITY_MAX - 1,\n 32);\n#ifdef RT_USING_SMP\n rt_thread_control(&idle[i], RT_THREAD_CTRL_BIND_CPU, (void*)i);\n#endif\n /* startup */\n rt_thread_startup(&idle[i]);\n }\n}\n\n/**\n * @ingroup Thread\n *\n * This function will get the handler of the idle thread.\n *\n */\nrt_thread_t rt_thread_idle_gethandler(void)\n{\n#ifdef RT_USING_SMP\n register int id = rt_hw_cpu_id();\n#else\n register int id = 0;\n#endif\n\n return (rt_thread_t)(&idle[id]);\n}\n", "src/ipc.c": "/*\n * Copyright (c) 2006-2018, RT-Thread Development Team\n *\n * SPDX-License-Identifier: Apache-2.0\n *\n * Change Logs:\n * Date Author Notes\n * 2006-03-14 Bernard the first version\n * 2006-04-25 Bernard implement semaphore\n * 2006-05-03 Bernard add RT_IPC_DEBUG\n * modify the type of IPC waiting time to rt_int32_t\n * 2006-05-10 Bernard fix the semaphore take bug and add IPC object\n * 2006-05-12 Bernard implement mailbox and message queue\n * 2006-05-20 Bernard implement mutex\n * 2006-05-23 Bernard implement fast event\n * 2006-05-24 Bernard implement event\n * 2006-06-03 Bernard fix the thread timer init bug\n * 2006-06-05 Bernard fix the mutex release bug\n * 2006-06-07 Bernard fix the message queue send bug\n * 2006-08-04 Bernard add hook support\n * 2009-05-21 Yi.qiu fix the sem release bug\n * 2009-07-18 Bernard fix the event clear bug\n * 2009-09-09 Bernard remove fast event and fix ipc release bug\n * 2009-10-10 Bernard change semaphore and mutex value to unsigned value\n * 2009-10-25 Bernard change the mb/mq receive timeout to 0 if the\n * re-calculated delta tick is a negative number.\n * 2009-12-16 Bernard fix the rt_ipc_object_suspend issue when IPC flag\n * is RT_IPC_FLAG_PRIO\n * 2010-01-20 mbbill remove rt_ipc_object_decrease function.\n * 2010-04-20 Bernard move memcpy outside interrupt disable in mq\n * 2010-10-26 yi.qiu add module support in rt_mp_delete and rt_mq_delete\n * 2010-11-10 Bernard add IPC reset command implementation.\n * 2011-12-18 Bernard add more parameter checking in message queue\n * 2013-09-14 Grissiom add an option check in rt_event_recv\n * 2018-10-02 Bernard add 64bit support for mailbox\n * 2019-09-16 tyx add send wait support for message queue\n * 2020-07-29 Meco Man fix thread->event_set/event_info when received an \n event without pending\n */\n\n#include <rtthread.h>\n#include <rthw.h>\n\n#ifdef RT_USING_HOOK\nextern void (*rt_object_trytake_hook)(struct rt_object *object);\nextern void (*rt_object_take_hook)(struct rt_object *object);\nextern void (*rt_object_put_hook)(struct rt_object *object);\n#endif\n\n/**\n * @addtogroup IPC\n */\n\n/**@{*/\n\n/**\n * This function will initialize an IPC object\n *\n * @param ipc the IPC object\n *\n * @return the operation status, RT_EOK on successful\n */\nrt_inline rt_err_t rt_ipc_object_init(struct rt_ipc_object *ipc)\n{\n /* initialize ipc object */\n rt_list_init(&(ipc->suspend_thread));\n\n return RT_EOK;\n}\n\n/**\n * This function will suspend a thread to a specified list. IPC object or some\n * double-queue object (mailbox etc.) contains this kind of list.\n *\n * @param list the IPC suspended thread list\n * @param thread the thread object to be suspended\n * @param flag the IPC object flag,\n * which shall be RT_IPC_FLAG_FIFO/RT_IPC_FLAG_PRIO.\n *\n * @return the operation status, RT_EOK on successful\n */\nrt_inline rt_err_t rt_ipc_list_suspend(rt_list_t *list,\n struct rt_thread *thread,\n rt_uint8_t flag)\n{\n /* suspend thread */\n rt_thread_suspend(thread);\n\n switch (flag)\n {\n case RT_IPC_FLAG_FIFO:\n rt_list_insert_before(list, &(thread->tlist));\n break;\n\n case RT_IPC_FLAG_PRIO:\n {\n struct rt_list_node *n;\n struct rt_thread *sthread;\n\n /* find a suitable position */\n for (n = list->next; n != list; n = n->next)\n {\n sthread = rt_list_entry(n, struct rt_thread, tlist);\n\n /* find out */\n if (thread->current_priority < sthread->current_priority)\n {\n /* insert this thread before the sthread */\n rt_list_insert_before(&(sthread->tlist), &(thread->tlist));\n break;\n }\n }\n\n /*\n * not found a suitable position,\n * append to the end of suspend_thread list\n */\n if (n == list)\n rt_list_insert_before(list, &(thread->tlist));\n }\n break;\n\n default:\n break; \n }\n\n return RT_EOK;\n}\n\n/**\n * This function will resume the first thread in the list of a IPC object:\n * - remove the thread from suspend queue of IPC object\n * - put the thread into system ready queue\n *\n * @param list the thread list\n *\n * @return the operation status, RT_EOK on successful\n */\nrt_inline rt_err_t rt_ipc_list_resume(rt_list_t *list)\n{\n struct rt_thread *thread;\n\n /* get thread entry */\n thread = rt_list_entry(list->next, struct rt_thread, tlist);\n\n RT_DEBUG_LOG(RT_DEBUG_IPC, (\"resume thread:%s\\n\", thread->name));\n\n /* resume it */\n rt_thread_resume(thread);\n\n return RT_EOK;\n}\n\n/**\n * This function will resume all suspended threads in a list, including\n * suspend list of IPC object and private list of mailbox etc.\n *\n * @param list of the threads to resume\n *\n * @return the operation status, RT_EOK on successful\n */\nrt_inline rt_err_t rt_ipc_list_resume_all(rt_list_t *list)\n{\n struct rt_thread *thread;\n register rt_ubase_t temp;\n\n /* wakeup all suspended threads */\n while (!rt_list_isempty(list))\n {\n /* disable interrupt */\n temp = rt_hw_interrupt_disable();\n\n /* get next suspended thread */\n thread = rt_list_entry(list->next, struct rt_thread, tlist);\n /* set error code to RT_ERROR */\n thread->error = -RT_ERROR;\n\n /*\n * resume thread\n * In rt_thread_resume function, it will remove current thread from\n * suspended list\n */\n rt_thread_resume(thread);\n\n /* enable interrupt */\n rt_hw_interrupt_enable(temp);\n }\n\n return RT_EOK;\n}\n\n#ifdef RT_USING_SEMAPHORE\n/**\n * This function will initialize a semaphore and put it under control of\n * resource management.\n *\n * @param sem the semaphore object\n * @param name the name of semaphore\n * @param value the initial value of semaphore\n * @param flag the flag of semaphore\n *\n * @return the operation status, RT_EOK on successful\n */\nrt_err_t rt_sem_init(rt_sem_t sem,\n const char *name,\n rt_uint32_t value,\n rt_uint8_t flag)\n{\n RT_ASSERT(sem != RT_NULL);\n RT_ASSERT(value < 0x10000U);\n\n /* initialize object */\n rt_object_init(&(sem->parent.parent), RT_Object_Class_Semaphore, name);\n\n /* initialize ipc object */\n rt_ipc_object_init(&(sem->parent));\n\n /* set initial value */\n sem->value = (rt_uint16_t)value;\n\n /* set parent */\n sem->parent.parent.flag = flag;\n\n return RT_EOK;\n}\nRTM_EXPORT(rt_sem_init);\n\n/**\n * This function will detach a semaphore from resource management\n *\n * @param sem the semaphore object\n *\n * @return the operation status, RT_EOK on successful\n *\n * @see rt_sem_delete\n */\nrt_err_t rt_sem_detach(rt_sem_t sem)\n{\n /* parameter check */\n RT_ASSERT(sem != RT_NULL);\n RT_ASSERT(rt_object_get_type(&sem->parent.parent) == RT_Object_Class_Semaphore);\n RT_ASSERT(rt_object_is_systemobject(&sem->parent.parent));\n\n /* wakeup all suspended threads */\n rt_ipc_list_resume_all(&(sem->parent.suspend_thread));\n\n /* detach semaphore object */\n rt_object_detach(&(sem->parent.parent));\n\n return RT_EOK;\n}\nRTM_EXPORT(rt_sem_detach);\n\n#ifdef RT_USING_HEAP\n/**\n * This function will create a semaphore from system resource\n *\n * @param name the name of semaphore\n * @param value the initial value of semaphore\n * @param flag the flag of semaphore\n *\n * @return the created semaphore, RT_NULL on error happen\n *\n * @see rt_sem_init\n */\nrt_sem_t rt_sem_create(const char *name, rt_uint32_t value, rt_uint8_t flag)\n{\n rt_sem_t sem;\n\n RT_DEBUG_NOT_IN_INTERRUPT;\n RT_ASSERT(value < 0x10000U);\n\n /* allocate object */\n sem = (rt_sem_t)rt_object_allocate(RT_Object_Class_Semaphore, name);\n if (sem == RT_NULL)\n return sem;\n\n /* initialize ipc object */\n rt_ipc_object_init(&(sem->parent));\n\n /* set initial value */\n sem->value = value;\n\n /* set parent */\n sem->parent.parent.flag = flag;\n\n return sem;\n}\nRTM_EXPORT(rt_sem_create);\n\n/**\n * This function will delete a semaphore object and release the memory\n *\n * @param sem the semaphore object\n *\n * @return the error code\n *\n * @see rt_sem_detach\n */\nrt_err_t rt_sem_delete(rt_sem_t sem)\n{\n RT_DEBUG_NOT_IN_INTERRUPT;\n\n /* parameter check */\n RT_ASSERT(sem != RT_NULL);\n RT_ASSERT(rt_object_get_type(&sem->parent.parent) == RT_Object_Class_Semaphore);\n RT_ASSERT(rt_object_is_systemobject(&sem->parent.parent) == RT_FALSE);\n\n /* wakeup all suspended threads */\n rt_ipc_list_resume_all(&(sem->parent.suspend_thread));\n\n /* delete semaphore object */\n rt_object_delete(&(sem->parent.parent));\n\n return RT_EOK;\n}\nRTM_EXPORT(rt_sem_delete);\n#endif\n\n/**\n * This function will take a semaphore, if the semaphore is unavailable, the\n * thread shall wait for a specified time.\n *\n * @param sem the semaphore object\n * @param time the waiting time\n *\n * @return the error code\n */\nrt_err_t rt_sem_take(rt_sem_t sem, rt_int32_t time)\n{\n register rt_base_t temp;\n struct rt_thread *thread;\n\n /* parameter check */\n RT_ASSERT(sem != RT_NULL);\n RT_ASSERT(rt_object_get_type(&sem->parent.parent) == RT_Object_Class_Semaphore);\n\n RT_OBJECT_HOOK_CALL(rt_object_trytake_hook, (&(sem->parent.parent)));\n\n /* disable interrupt */\n temp = rt_hw_interrupt_disable();\n\n RT_DEBUG_LOG(RT_DEBUG_IPC, (\"thread %s take sem:%s, which value is: %d\\n\",\n rt_thread_self()->name,\n ((struct rt_object *)sem)->name,\n sem->value));\n\n if (sem->value > 0)\n {\n /* semaphore is available */\n sem->value --;\n\n /* enable interrupt */\n rt_hw_interrupt_enable(temp);\n }\n else\n {\n /* no waiting, return with timeout */\n if (time == 0)\n {\n rt_hw_interrupt_enable(temp);\n\n return -RT_ETIMEOUT;\n }\n else\n {\n /* current context checking */\n RT_DEBUG_IN_THREAD_CONTEXT;\n\n /* semaphore is unavailable, push to suspend list */\n /* get current thread */\n thread = rt_thread_self();\n\n /* reset thread error number */\n thread->error = RT_EOK;\n\n RT_DEBUG_LOG(RT_DEBUG_IPC, (\"sem take: suspend thread - %s\\n\",\n thread->name));\n\n /* suspend thread */\n rt_ipc_list_suspend(&(sem->parent.suspend_thread),\n thread,\n sem->parent.parent.flag);\n\n /* has waiting time, start thread timer */\n if (time > 0)\n {\n RT_DEBUG_LOG(RT_DEBUG_IPC, (\"set thread:%s to timer list\\n\",\n thread->name));\n\n /* reset the timeout of thread timer and start it */\n rt_timer_control(&(thread->thread_timer),\n RT_TIMER_CTRL_SET_TIME,\n &time);\n rt_timer_start(&(thread->thread_timer));\n }\n\n /* enable interrupt */\n rt_hw_interrupt_enable(temp);\n\n /* do schedule */\n rt_schedule();\n\n if (thread->error != RT_EOK)\n {\n return thread->error;\n }\n }\n }\n\n RT_OBJECT_HOOK_CALL(rt_object_take_hook, (&(sem->parent.parent)));\n\n return RT_EOK;\n}\nRTM_EXPORT(rt_sem_take);\n\n/**\n * This function will try to take a semaphore and immediately return\n *\n * @param sem the semaphore object\n *\n * @return the error code\n */\nrt_err_t rt_sem_trytake(rt_sem_t sem)\n{\n return rt_sem_take(sem, 0);\n}\nRTM_EXPORT(rt_sem_trytake);\n\n/**\n * This function will release a semaphore, if there are threads suspended on\n * semaphore, it will be waked up.\n *\n * @param sem the semaphore object\n *\n * @return the error code\n */\nrt_err_t rt_sem_release(rt_sem_t sem)\n{\n register rt_base_t temp;\n register rt_bool_t need_schedule;\n\n /* parameter check */\n RT_ASSERT(sem != RT_NULL);\n RT_ASSERT(rt_object_get_type(&sem->parent.parent) == RT_Object_Class_Semaphore);\n\n RT_OBJECT_HOOK_CALL(rt_object_put_hook, (&(sem->parent.parent)));\n\n need_schedule = RT_FALSE;\n\n /* disable interrupt */\n temp = rt_hw_interrupt_disable();\n\n RT_DEBUG_LOG(RT_DEBUG_IPC, (\"thread %s releases sem:%s, which value is: %d\\n\",\n rt_thread_self()->name,\n ((struct rt_object *)sem)->name,\n sem->value));\n\n if (!rt_list_isempty(&sem->parent.suspend_thread))\n {\n /* resume the suspended thread */\n rt_ipc_list_resume(&(sem->parent.suspend_thread));\n need_schedule = RT_TRUE;\n }\n else\n sem->value ++; /* increase value */\n\n /* enable interrupt */\n rt_hw_interrupt_enable(temp);\n\n /* resume a thread, re-schedule */\n if (need_schedule == RT_TRUE)\n rt_schedule();\n\n return RT_EOK;\n}\nRTM_EXPORT(rt_sem_release);\n\n/**\n * This function can get or set some extra attributions of a semaphore object.\n *\n * @param sem the semaphore object\n * @param cmd the execution command\n * @param arg the execution argument\n *\n * @return the error code\n */\nrt_err_t rt_sem_control(rt_sem_t sem, int cmd, void *arg)\n{\n rt_ubase_t level;\n\n /* parameter check */\n RT_ASSERT(sem != RT_NULL);\n RT_ASSERT(rt_object_get_type(&sem->parent.parent) == RT_Object_Class_Semaphore);\n\n if (cmd == RT_IPC_CMD_RESET)\n {\n rt_ubase_t value;\n\n /* get value */\n value = (rt_ubase_t)arg;\n /* disable interrupt */\n level = rt_hw_interrupt_disable();\n\n /* resume all waiting thread */\n rt_ipc_list_resume_all(&sem->parent.suspend_thread);\n\n /* set new value */\n sem->value = (rt_uint16_t)value;\n\n /* enable interrupt */\n rt_hw_interrupt_enable(level);\n\n rt_schedule();\n\n return RT_EOK;\n }\n\n return -RT_ERROR;\n}\nRTM_EXPORT(rt_sem_control);\n#endif /* end of RT_USING_SEMAPHORE */\n\n#ifdef RT_USING_MUTEX\n/**\n * This function will initialize a mutex and put it under control of resource\n * management.\n *\n * @param mutex the mutex object\n * @param name the name of mutex\n * @param flag the flag of mutex\n *\n * @return the operation status, RT_EOK on successful\n */\nrt_err_t rt_mutex_init(rt_mutex_t mutex, const char *name, rt_uint8_t flag)\n{\n /* parameter check */\n RT_ASSERT(mutex != RT_NULL);\n\n /* initialize object */\n rt_object_init(&(mutex->parent.parent), RT_Object_Class_Mutex, name);\n\n /* initialize ipc object */\n rt_ipc_object_init(&(mutex->parent));\n\n mutex->value = 1;\n mutex->owner = RT_NULL;\n mutex->original_priority = 0xFF;\n mutex->hold = 0;\n\n /* set flag */\n mutex->parent.parent.flag = flag;\n\n return RT_EOK;\n}\nRTM_EXPORT(rt_mutex_init);\n\n/**\n * This function will detach a mutex from resource management\n *\n * @param mutex the mutex object\n *\n * @return the operation status, RT_EOK on successful\n *\n * @see rt_mutex_delete\n */\nrt_err_t rt_mutex_detach(rt_mutex_t mutex)\n{\n /* parameter check */\n RT_ASSERT(mutex != RT_NULL);\n RT_ASSERT(rt_object_get_type(&mutex->parent.parent) == RT_Object_Class_Mutex);\n RT_ASSERT(rt_object_is_systemobject(&mutex->parent.parent));\n\n /* wakeup all suspended threads */\n rt_ipc_list_resume_all(&(mutex->parent.suspend_thread));\n\n /* detach semaphore object */\n rt_object_detach(&(mutex->parent.parent));\n\n return RT_EOK;\n}\nRTM_EXPORT(rt_mutex_detach);\n\n#ifdef RT_USING_HEAP\n/**\n * This function will create a mutex from system resource\n *\n * @param name the name of mutex\n * @param flag the flag of mutex\n *\n * @return the created mutex, RT_NULL on error happen\n *\n * @see rt_mutex_init\n */\nrt_mutex_t rt_mutex_create(const char *name, rt_uint8_t flag)\n{\n struct rt_mutex *mutex;\n\n RT_DEBUG_NOT_IN_INTERRUPT;\n\n /* allocate object */\n mutex = (rt_mutex_t)rt_object_allocate(RT_Object_Class_Mutex, name);\n if (mutex == RT_NULL)\n return mutex;\n\n /* initialize ipc object */\n rt_ipc_object_init(&(mutex->parent));\n\n mutex->value = 1;\n mutex->owner = RT_NULL;\n mutex->original_priority = 0xFF;\n mutex->hold = 0;\n\n /* set flag */\n mutex->parent.parent.flag = flag;\n\n return mutex;\n}\nRTM_EXPORT(rt_mutex_create);\n\n/**\n * This function will delete a mutex object and release the memory\n *\n * @param mutex the mutex object\n *\n * @return the error code\n *\n * @see rt_mutex_detach\n */\nrt_err_t rt_mutex_delete(rt_mutex_t mutex)\n{\n RT_DEBUG_NOT_IN_INTERRUPT;\n\n /* parameter check */\n RT_ASSERT(mutex != RT_NULL);\n RT_ASSERT(rt_object_get_type(&mutex->parent.parent) == RT_Object_Class_Mutex);\n RT_ASSERT(rt_object_is_systemobject(&mutex->parent.parent) == RT_FALSE);\n\n /* wakeup all suspended threads */\n rt_ipc_list_resume_all(&(mutex->parent.suspend_thread));\n\n /* delete mutex object */\n rt_object_delete(&(mutex->parent.parent));\n\n return RT_EOK;\n}\nRTM_EXPORT(rt_mutex_delete);\n#endif\n\n/**\n * This function will take a mutex, if the mutex is unavailable, the\n * thread shall wait for a specified time.\n *\n * @param mutex the mutex object\n * @param time the waiting time\n *\n * @return the error code\n */\nrt_err_t rt_mutex_take(rt_mutex_t mutex, rt_int32_t time)\n{\n register rt_base_t temp;\n struct rt_thread *thread;\n\n /* this function must not be used in interrupt even if time = 0 */\n RT_DEBUG_IN_THREAD_CONTEXT;\n\n /* parameter check */\n RT_ASSERT(mutex != RT_NULL);\n RT_ASSERT(rt_object_get_type(&mutex->parent.parent) == RT_Object_Class_Mutex);\n\n /* get current thread */\n thread = rt_thread_self();\n\n /* disable interrupt */\n temp = rt_hw_interrupt_disable();\n\n RT_OBJECT_HOOK_CALL(rt_object_trytake_hook, (&(mutex->parent.parent)));\n\n RT_DEBUG_LOG(RT_DEBUG_IPC,\n (\"mutex_take: current thread %s, mutex value: %d, hold: %d\\n\",\n thread->name, mutex->value, mutex->hold));\n\n /* reset thread error */\n thread->error = RT_EOK;\n\n if (mutex->owner == thread)\n {\n /* it's the same thread */\n mutex->hold ++;\n }\n else\n {\n#ifdef RT_USING_SIGNALS\n__again:\n#endif /* end of RT_USING_SIGNALS */\n /* The value of mutex is 1 in initial status. Therefore, if the\n * value is great than 0, it indicates the mutex is avaible.\n */\n if (mutex->value > 0)\n {\n /* mutex is available */\n mutex->value --;\n\n /* set mutex owner and original priority */\n mutex->owner = thread;\n mutex->original_priority = thread->current_priority;\n mutex->hold ++;\n }\n else\n {\n /* no waiting, return with timeout */\n if (time == 0)\n {\n /* set error as timeout */\n thread->error = -RT_ETIMEOUT;\n\n /* enable interrupt */\n rt_hw_interrupt_enable(temp);\n\n return -RT_ETIMEOUT;\n }\n else\n {\n /* mutex is unavailable, push to suspend list */\n RT_DEBUG_LOG(RT_DEBUG_IPC, (\"mutex_take: suspend thread: %s\\n\",\n thread->name));\n\n /* change the owner thread priority of mutex */\n if (thread->current_priority < mutex->owner->current_priority)\n {\n /* change the owner thread priority */\n rt_thread_control(mutex->owner,\n RT_THREAD_CTRL_CHANGE_PRIORITY,\n &thread->current_priority);\n }\n\n /* suspend current thread */\n rt_ipc_list_suspend(&(mutex->parent.suspend_thread),\n thread,\n mutex->parent.parent.flag);\n\n /* has waiting time, start thread timer */\n if (time > 0)\n {\n RT_DEBUG_LOG(RT_DEBUG_IPC,\n (\"mutex_take: start the timer of thread:%s\\n\",\n thread->name));\n\n /* reset the timeout of thread timer and start it */\n rt_timer_control(&(thread->thread_timer),\n RT_TIMER_CTRL_SET_TIME,\n &time);\n rt_timer_start(&(thread->thread_timer));\n }\n\n /* enable interrupt */\n rt_hw_interrupt_enable(temp);\n\n /* do schedule */\n rt_schedule();\n\n if (thread->error != RT_EOK)\n {\n#ifdef RT_USING_SIGNALS\n /* interrupt by signal, try it again */\n if (thread->error == -RT_EINTR) goto __again;\n#endif /* end of RT_USING_SIGNALS */\n\n /* return error */\n return thread->error;\n }\n else\n {\n /* the mutex is taken successfully. */\n /* disable interrupt */\n temp = rt_hw_interrupt_disable();\n }\n }\n }\n }\n\n /* enable interrupt */\n rt_hw_interrupt_enable(temp);\n\n RT_OBJECT_HOOK_CALL(rt_object_take_hook, (&(mutex->parent.parent)));\n\n return RT_EOK;\n}\nRTM_EXPORT(rt_mutex_take);\n\n/**\n * This function will release a mutex, if there are threads suspended on mutex,\n * it will be waked up.\n *\n * @param mutex the mutex object\n *\n * @return the error code\n */\nrt_err_t rt_mutex_release(rt_mutex_t mutex)\n{\n register rt_base_t temp;\n struct rt_thread *thread;\n rt_bool_t need_schedule;\n\n /* parameter check */\n RT_ASSERT(mutex != RT_NULL);\n RT_ASSERT(rt_object_get_type(&mutex->parent.parent) == RT_Object_Class_Mutex);\n\n need_schedule = RT_FALSE;\n\n /* only thread could release mutex because we need test the ownership */\n RT_DEBUG_IN_THREAD_CONTEXT;\n\n /* get current thread */\n thread = rt_thread_self();\n\n /* disable interrupt */\n temp = rt_hw_interrupt_disable();\n\n RT_DEBUG_LOG(RT_DEBUG_IPC,\n (\"mutex_release:current thread %s, mutex value: %d, hold: %d\\n\",\n thread->name, mutex->value, mutex->hold));\n\n RT_OBJECT_HOOK_CALL(rt_object_put_hook, (&(mutex->parent.parent)));\n\n /* mutex only can be released by owner */\n if (thread != mutex->owner)\n {\n thread->error = -RT_ERROR;\n\n /* enable interrupt */\n rt_hw_interrupt_enable(temp);\n\n return -RT_ERROR;\n }\n\n /* decrease hold */\n mutex->hold --;\n /* if no hold */\n if (mutex->hold == 0)\n {\n /* change the owner thread to original priority */\n if (mutex->original_priority != mutex->owner->current_priority)\n {\n rt_thread_control(mutex->owner,\n RT_THREAD_CTRL_CHANGE_PRIORITY,\n &(mutex->original_priority));\n }\n\n /* wakeup suspended thread */\n if (!rt_list_isempty(&mutex->parent.suspend_thread))\n {\n /* get suspended thread */\n thread = rt_list_entry(mutex->parent.suspend_thread.next,\n struct rt_thread,\n tlist);\n\n RT_DEBUG_LOG(RT_DEBUG_IPC, (\"mutex_release: resume thread: %s\\n\",\n thread->name));\n\n /* set new owner and priority */\n mutex->owner = thread;\n mutex->original_priority = thread->current_priority;\n mutex->hold ++;\n\n /* resume thread */\n rt_ipc_list_resume(&(mutex->parent.suspend_thread));\n\n need_schedule = RT_TRUE;\n }\n else\n {\n /* increase value */\n mutex->value ++;\n\n /* clear owner */\n mutex->owner = RT_NULL;\n mutex->original_priority = 0xff;\n }\n }\n\n /* enable interrupt */\n rt_hw_interrupt_enable(temp);\n\n /* perform a schedule */\n if (need_schedule == RT_TRUE)\n rt_schedule();\n\n return RT_EOK;\n}\nRTM_EXPORT(rt_mutex_release);\n\n/**\n * This function can get or set some extra attributions of a mutex object.\n *\n * @param mutex the mutex object\n * @param cmd the execution command\n * @param arg the execution argument\n *\n * @return the error code\n */\nrt_err_t rt_mutex_control(rt_mutex_t mutex, int cmd, void *arg)\n{\n /* parameter check */\n RT_ASSERT(mutex != RT_NULL);\n RT_ASSERT(rt_object_get_type(&mutex->parent.parent) == RT_Object_Class_Mutex);\n\n return -RT_ERROR;\n}\nRTM_EXPORT(rt_mutex_control);\n#endif /* end of RT_USING_MUTEX */\n\n#ifdef RT_USING_EVENT\n/**\n * This function will initialize an event and put it under control of resource\n * management.\n *\n * @param event the event object\n * @param name the name of event\n * @param flag the flag of event\n *\n * @return the operation status, RT_EOK on successful\n */\nrt_err_t rt_event_init(rt_event_t event, const char *name, rt_uint8_t flag)\n{\n /* parameter check */\n RT_ASSERT(event != RT_NULL);\n\n /* initialize object */\n rt_object_init(&(event->parent.parent), RT_Object_Class_Event, name);\n\n /* set parent flag */\n event->parent.parent.flag = flag;\n\n /* initialize ipc object */\n rt_ipc_object_init(&(event->parent));\n\n /* initialize event */\n event->set = 0;\n\n return RT_EOK;\n}\nRTM_EXPORT(rt_event_init);\n\n/**\n * This function will detach an event object from resource management\n *\n * @param event the event object\n *\n * @return the operation status, RT_EOK on successful\n */\nrt_err_t rt_event_detach(rt_event_t event)\n{\n /* parameter check */\n RT_ASSERT(event != RT_NULL);\n RT_ASSERT(rt_object_get_type(&event->parent.parent) == RT_Object_Class_Event);\n RT_ASSERT(rt_object_is_systemobject(&event->parent.parent));\n\n /* resume all suspended thread */\n rt_ipc_list_resume_all(&(event->parent.suspend_thread));\n\n /* detach event object */\n rt_object_detach(&(event->parent.parent));\n\n return RT_EOK;\n}\nRTM_EXPORT(rt_event_detach);\n\n#ifdef RT_USING_HEAP\n/**\n * This function will create an event object from system resource\n *\n * @param name the name of event\n * @param flag the flag of event\n *\n * @return the created event, RT_NULL on error happen\n */\nrt_event_t rt_event_create(const char *name, rt_uint8_t flag)\n{\n rt_event_t event;\n\n RT_DEBUG_NOT_IN_INTERRUPT;\n\n /* allocate object */\n event = (rt_event_t)rt_object_allocate(RT_Object_Class_Event, name);\n if (event == RT_NULL)\n return event;\n\n /* set parent */\n event->parent.parent.flag = flag;\n\n /* initialize ipc object */\n rt_ipc_object_init(&(event->parent));\n\n /* initialize event */\n event->set = 0;\n\n return event;\n}\nRTM_EXPORT(rt_event_create);\n\n/**\n * This function will delete an event object and release the memory\n *\n * @param event the event object\n *\n * @return the error code\n */\nrt_err_t rt_event_delete(rt_event_t event)\n{\n /* parameter check */\n RT_ASSERT(event != RT_NULL);\n RT_ASSERT(rt_object_get_type(&event->parent.parent) == RT_Object_Class_Event);\n RT_ASSERT(rt_object_is_systemobject(&event->parent.parent) == RT_FALSE);\n\n RT_DEBUG_NOT_IN_INTERRUPT;\n\n /* resume all suspended thread */\n rt_ipc_list_resume_all(&(event->parent.suspend_thread));\n\n /* delete event object */\n rt_object_delete(&(event->parent.parent));\n\n return RT_EOK;\n}\nRTM_EXPORT(rt_event_delete);\n#endif\n\n/**\n * This function will send an event to the event object, if there are threads\n * suspended on event object, it will be waked up.\n *\n * @param event the event object\n * @param set the event set\n *\n * @return the error code\n */\nrt_err_t rt_event_send(rt_event_t event, rt_uint32_t set)\n{\n struct rt_list_node *n;\n struct rt_thread *thread;\n register rt_ubase_t level;\n register rt_base_t status;\n rt_bool_t need_schedule;\n\n /* parameter check */\n RT_ASSERT(event != RT_NULL);\n RT_ASSERT(rt_object_get_type(&event->parent.parent) == RT_Object_Class_Event);\n\n if (set == 0)\n return -RT_ERROR;\n\n need_schedule = RT_FALSE;\n\n /* disable interrupt */\n level = rt_hw_interrupt_disable();\n\n /* set event */\n event->set |= set;\n\n RT_OBJECT_HOOK_CALL(rt_object_put_hook, (&(event->parent.parent)));\n\n if (!rt_list_isempty(&event->parent.suspend_thread))\n {\n /* search thread list to resume thread */\n n = event->parent.suspend_thread.next;\n while (n != &(event->parent.suspend_thread))\n {\n /* get thread */\n thread = rt_list_entry(n, struct rt_thread, tlist);\n\n status = -RT_ERROR;\n if (thread->event_info & RT_EVENT_FLAG_AND)\n {\n if ((thread->event_set & event->set) == thread->event_set)\n {\n /* received an AND event */\n status = RT_EOK;\n }\n }\n else if (thread->event_info & RT_EVENT_FLAG_OR)\n {\n if (thread->event_set & event->set)\n {\n /* save the received event set */\n thread->event_set = thread->event_set & event->set;\n\n /* received an OR event */\n status = RT_EOK;\n }\n }\n else\n {\n /* enable interrupt */\n rt_hw_interrupt_enable(level);\n\n return -RT_EINVAL;\n }\n\n /* move node to the next */\n n = n->next;\n\n /* condition is satisfied, resume thread */\n if (status == RT_EOK)\n {\n /* clear event */\n if (thread->event_info & RT_EVENT_FLAG_CLEAR)\n event->set &= ~thread->event_set;\n\n /* resume thread, and thread list breaks out */\n rt_thread_resume(thread);\n\n /* need do a scheduling */\n need_schedule = RT_TRUE;\n }\n }\n }\n\n /* enable interrupt */\n rt_hw_interrupt_enable(level);\n\n /* do a schedule */\n if (need_schedule == RT_TRUE)\n rt_schedule();\n\n return RT_EOK;\n}\nRTM_EXPORT(rt_event_send);\n\n/**\n * This function will receive an event from event object, if the event is\n * unavailable, the thread shall wait for a specified time.\n *\n * @param event the fast event object\n * @param set the interested event set\n * @param option the receive option, either RT_EVENT_FLAG_AND or\n * RT_EVENT_FLAG_OR should be set.\n * @param timeout the waiting time\n * @param recved the received event, if you don't care, RT_NULL can be set.\n *\n * @return the error code\n */\nrt_err_t rt_event_recv(rt_event_t event,\n rt_uint32_t set,\n rt_uint8_t option,\n rt_int32_t timeout,\n rt_uint32_t *recved)\n{\n struct rt_thread *thread;\n register rt_ubase_t level;\n register rt_base_t status;\n\n RT_DEBUG_IN_THREAD_CONTEXT;\n\n /* parameter check */\n RT_ASSERT(event != RT_NULL);\n RT_ASSERT(rt_object_get_type(&event->parent.parent) == RT_Object_Class_Event);\n\n if (set == 0)\n return -RT_ERROR;\n\n /* initialize status */\n status = -RT_ERROR;\n /* get current thread */\n thread = rt_thread_self();\n /* reset thread error */\n thread->error = RT_EOK;\n\n RT_OBJECT_HOOK_CALL(rt_object_trytake_hook, (&(event->parent.parent)));\n\n /* disable interrupt */\n level = rt_hw_interrupt_disable();\n\n /* check event set */\n if (option & RT_EVENT_FLAG_AND)\n {\n if ((event->set & set) == set)\n status = RT_EOK;\n }\n else if (option & RT_EVENT_FLAG_OR)\n {\n if (event->set & set)\n status = RT_EOK;\n }\n else\n {\n /* either RT_EVENT_FLAG_AND or RT_EVENT_FLAG_OR should be set */\n RT_ASSERT(0);\n }\n\n if (status == RT_EOK)\n {\n /* set received event */\n if (recved)\n *recved = (event->set & set);\n \n /* fill thread event info */ \n thread->event_set = (event->set & set);\n thread->event_info = option;\n \n /* received event */\n if (option & RT_EVENT_FLAG_CLEAR)\n event->set &= ~set;\n }\n else if (timeout == 0)\n {\n /* no waiting */\n thread->error = -RT_ETIMEOUT;\n\n /* enable interrupt */\n rt_hw_interrupt_enable(level);\n\n return -RT_ETIMEOUT;\n }\n else\n {\n /* fill thread event info */\n thread->event_set = set;\n thread->event_info = option;\n\n /* put thread to suspended thread list */\n rt_ipc_list_suspend(&(event->parent.suspend_thread),\n thread,\n event->parent.parent.flag);\n\n /* if there is a waiting timeout, active thread timer */\n if (timeout > 0)\n {\n /* reset the timeout of thread timer and start it */\n rt_timer_control(&(thread->thread_timer),\n RT_TIMER_CTRL_SET_TIME,\n &timeout);\n rt_timer_start(&(thread->thread_timer));\n }\n\n /* enable interrupt */\n rt_hw_interrupt_enable(level);\n\n /* do a schedule */\n rt_schedule();\n\n if (thread->error != RT_EOK)\n {\n /* return error */\n return thread->error;\n }\n\n /* received an event, disable interrupt to protect */\n level = rt_hw_interrupt_disable();\n\n /* set received event */\n if (recved)\n *recved = thread->event_set;\n }\n\n /* enable interrupt */\n rt_hw_interrupt_enable(level);\n\n RT_OBJECT_HOOK_CALL(rt_object_take_hook, (&(event->parent.parent)));\n\n return thread->error;\n}\nRTM_EXPORT(rt_event_recv);\n\n/**\n * This function can get or set some extra attributions of an event object.\n *\n * @param event the event object\n * @param cmd the execution command\n * @param arg the execution argument\n *\n * @return the error code\n */\nrt_err_t rt_event_control(rt_event_t event, int cmd, void *arg)\n{\n rt_ubase_t level;\n\n /* parameter check */\n RT_ASSERT(event != RT_NULL);\n RT_ASSERT(rt_object_get_type(&event->parent.parent) == RT_Object_Class_Event);\n\n if (cmd == RT_IPC_CMD_RESET)\n {\n /* disable interrupt */\n level = rt_hw_interrupt_disable();\n\n /* resume all waiting thread */\n rt_ipc_list_resume_all(&event->parent.suspend_thread);\n\n /* initialize event set */\n event->set = 0;\n\n /* enable interrupt */\n rt_hw_interrupt_enable(level);\n\n rt_schedule();\n\n return RT_EOK;\n }\n\n return -RT_ERROR;\n}\nRTM_EXPORT(rt_event_control);\n#endif /* end of RT_USING_EVENT */\n\n#ifdef RT_USING_MAILBOX\n/**\n * This function will initialize a mailbox and put it under control of resource\n * management.\n *\n * @param mb the mailbox object\n * @param name the name of mailbox\n * @param msgpool the begin address of buffer to save received mail\n * @param size the size of mailbox\n * @param flag the flag of mailbox\n *\n * @return the operation status, RT_EOK on successful\n */\nrt_err_t rt_mb_init(rt_mailbox_t mb,\n const char *name,\n void *msgpool,\n rt_size_t size,\n rt_uint8_t flag)\n{\n RT_ASSERT(mb != RT_NULL);\n\n /* initialize object */\n rt_object_init(&(mb->parent.parent), RT_Object_Class_MailBox, name);\n\n /* set parent flag */\n mb->parent.parent.flag = flag;\n\n /* initialize ipc object */\n rt_ipc_object_init(&(mb->parent));\n\n /* initialize mailbox */\n mb->msg_pool = (rt_ubase_t *)msgpool;\n mb->size = size;\n mb->entry = 0;\n mb->in_offset = 0;\n mb->out_offset = 0;\n\n /* initialize an additional list of sender suspend thread */\n rt_list_init(&(mb->suspend_sender_thread));\n\n return RT_EOK;\n}\nRTM_EXPORT(rt_mb_init);\n\n/**\n * This function will detach a mailbox from resource management\n *\n * @param mb the mailbox object\n *\n * @return the operation status, RT_EOK on successful\n */\nrt_err_t rt_mb_detach(rt_mailbox_t mb)\n{\n /* parameter check */\n RT_ASSERT(mb != RT_NULL);\n RT_ASSERT(rt_object_get_type(&mb->parent.parent) == RT_Object_Class_MailBox);\n RT_ASSERT(rt_object_is_systemobject(&mb->parent.parent));\n\n /* resume all suspended thread */\n rt_ipc_list_resume_all(&(mb->parent.suspend_thread));\n /* also resume all mailbox private suspended thread */\n rt_ipc_list_resume_all(&(mb->suspend_sender_thread));\n\n /* detach mailbox object */\n rt_object_detach(&(mb->parent.parent));\n\n return RT_EOK;\n}\nRTM_EXPORT(rt_mb_detach);\n\n#ifdef RT_USING_HEAP\n/**\n * This function will create a mailbox object from system resource\n *\n * @param name the name of mailbox\n * @param size the size of mailbox\n * @param flag the flag of mailbox\n *\n * @return the created mailbox, RT_NULL on error happen\n */\nrt_mailbox_t rt_mb_create(const char *name, rt_size_t size, rt_uint8_t flag)\n{\n rt_mailbox_t mb;\n\n RT_DEBUG_NOT_IN_INTERRUPT;\n\n /* allocate object */\n mb = (rt_mailbox_t)rt_object_allocate(RT_Object_Class_MailBox, name);\n if (mb == RT_NULL)\n return mb;\n\n /* set parent */\n mb->parent.parent.flag = flag;\n\n /* initialize ipc object */\n rt_ipc_object_init(&(mb->parent));\n\n /* initialize mailbox */\n mb->size = size;\n mb->msg_pool = (rt_ubase_t *)RT_KERNEL_MALLOC(mb->size * sizeof(rt_ubase_t));\n if (mb->msg_pool == RT_NULL)\n {\n /* delete mailbox object */\n rt_object_delete(&(mb->parent.parent));\n\n return RT_NULL;\n }\n mb->entry = 0;\n mb->in_offset = 0;\n mb->out_offset = 0;\n\n /* initialize an additional list of sender suspend thread */\n rt_list_init(&(mb->suspend_sender_thread));\n\n return mb;\n}\nRTM_EXPORT(rt_mb_create);\n\n/**\n * This function will delete a mailbox object and release the memory\n *\n * @param mb the mailbox object\n *\n * @return the error code\n */\nrt_err_t rt_mb_delete(rt_mailbox_t mb)\n{\n RT_DEBUG_NOT_IN_INTERRUPT;\n\n /* parameter check */\n RT_ASSERT(mb != RT_NULL);\n RT_ASSERT(rt_object_get_type(&mb->parent.parent) == RT_Object_Class_MailBox);\n RT_ASSERT(rt_object_is_systemobject(&mb->parent.parent) == RT_FALSE);\n\n /* resume all suspended thread */\n rt_ipc_list_resume_all(&(mb->parent.suspend_thread));\n\n /* also resume all mailbox private suspended thread */\n rt_ipc_list_resume_all(&(mb->suspend_sender_thread));\n\n /* free mailbox pool */\n RT_KERNEL_FREE(mb->msg_pool);\n\n /* delete mailbox object */\n rt_object_delete(&(mb->parent.parent));\n\n return RT_EOK;\n}\nRTM_EXPORT(rt_mb_delete);\n#endif\n\n/**\n * This function will send a mail to mailbox object. If the mailbox is full,\n * current thread will be suspended until timeout.\n *\n * @param mb the mailbox object\n * @param value the mail\n * @param timeout the waiting time\n *\n * @return the error code\n */\nrt_err_t rt_mb_send_wait(rt_mailbox_t mb,\n rt_ubase_t value,\n rt_int32_t timeout)\n{\n struct rt_thread *thread;\n register rt_ubase_t temp;\n rt_uint32_t tick_delta;\n\n /* parameter check */\n RT_ASSERT(mb != RT_NULL);\n RT_ASSERT(rt_object_get_type(&mb->parent.parent) == RT_Object_Class_MailBox);\n\n /* initialize delta tick */\n tick_delta = 0;\n /* get current thread */\n thread = rt_thread_self();\n\n RT_OBJECT_HOOK_CALL(rt_object_put_hook, (&(mb->parent.parent)));\n\n /* disable interrupt */\n temp = rt_hw_interrupt_disable();\n\n /* for non-blocking call */\n if (mb->entry == mb->size && timeout == 0)\n {\n rt_hw_interrupt_enable(temp);\n\n return -RT_EFULL;\n }\n\n /* mailbox is full */\n while (mb->entry == mb->size)\n {\n /* reset error number in thread */\n thread->error = RT_EOK;\n\n /* no waiting, return timeout */\n if (timeout == 0)\n {\n /* enable interrupt */\n rt_hw_interrupt_enable(temp);\n\n return -RT_EFULL;\n }\n\n RT_DEBUG_IN_THREAD_CONTEXT;\n /* suspend current thread */\n rt_ipc_list_suspend(&(mb->suspend_sender_thread),\n thread,\n mb->parent.parent.flag);\n\n /* has waiting time, start thread timer */\n if (timeout > 0)\n {\n /* get the start tick of timer */\n tick_delta = rt_tick_get();\n\n RT_DEBUG_LOG(RT_DEBUG_IPC, (\"mb_send_wait: start timer of thread:%s\\n\",\n thread->name));\n\n /* reset the timeout of thread timer and start it */\n rt_timer_control(&(thread->thread_timer),\n RT_TIMER_CTRL_SET_TIME,\n &timeout);\n rt_timer_start(&(thread->thread_timer));\n }\n\n /* enable interrupt */\n rt_hw_interrupt_enable(temp);\n\n /* re-schedule */\n rt_schedule();\n\n /* resume from suspend state */\n if (thread->error != RT_EOK)\n {\n /* return error */\n return thread->error;\n }\n\n /* disable interrupt */\n temp = rt_hw_interrupt_disable();\n\n /* if it's not waiting forever and then re-calculate timeout tick */\n if (timeout > 0)\n {\n tick_delta = rt_tick_get() - tick_delta;\n timeout -= tick_delta;\n if (timeout < 0)\n timeout = 0;\n }\n }\n\n /* set ptr */\n mb->msg_pool[mb->in_offset] = value;\n /* increase input offset */\n ++ mb->in_offset;\n if (mb->in_offset >= mb->size)\n mb->in_offset = 0;\n /* increase message entry */\n mb->entry ++;\n\n /* resume suspended thread */\n if (!rt_list_isempty(&mb->parent.suspend_thread))\n {\n rt_ipc_list_resume(&(mb->parent.suspend_thread));\n\n /* enable interrupt */\n rt_hw_interrupt_enable(temp);\n\n rt_schedule();\n\n return RT_EOK;\n }\n\n /* enable interrupt */\n rt_hw_interrupt_enable(temp);\n\n return RT_EOK;\n}\nRTM_EXPORT(rt_mb_send_wait);\n\n/**\n * This function will send a mail to mailbox object, if there are threads\n * suspended on mailbox object, it will be waked up. This function will return\n * immediately, if you want blocking send, use rt_mb_send_wait instead.\n *\n * @param mb the mailbox object\n * @param value the mail\n *\n * @return the error code\n */\nrt_err_t rt_mb_send(rt_mailbox_t mb, rt_ubase_t value)\n{\n return rt_mb_send_wait(mb, value, 0);\n}\nRTM_EXPORT(rt_mb_send);\n\n/**\n * This function will receive a mail from mailbox object, if there is no mail\n * in mailbox object, the thread shall wait for a specified time.\n *\n * @param mb the mailbox object\n * @param value the received mail will be saved in\n * @param timeout the waiting time\n *\n * @return the error code\n */\nrt_err_t rt_mb_recv(rt_mailbox_t mb, rt_ubase_t *value, rt_int32_t timeout)\n{\n struct rt_thread *thread;\n register rt_ubase_t temp;\n rt_uint32_t tick_delta;\n\n /* parameter check */\n RT_ASSERT(mb != RT_NULL);\n RT_ASSERT(rt_object_get_type(&mb->parent.parent) == RT_Object_Class_MailBox);\n\n /* initialize delta tick */\n tick_delta = 0;\n /* get current thread */\n thread = rt_thread_self();\n\n RT_OBJECT_HOOK_CALL(rt_object_trytake_hook, (&(mb->parent.parent)));\n\n /* disable interrupt */\n temp = rt_hw_interrupt_disable();\n\n /* for non-blocking call */\n if (mb->entry == 0 && timeout == 0)\n {\n rt_hw_interrupt_enable(temp);\n\n return -RT_ETIMEOUT;\n }\n\n /* mailbox is empty */\n while (mb->entry == 0)\n {\n /* reset error number in thread */\n thread->error = RT_EOK;\n\n /* no waiting, return timeout */\n if (timeout == 0)\n {\n /* enable interrupt */\n rt_hw_interrupt_enable(temp);\n\n thread->error = -RT_ETIMEOUT;\n\n return -RT_ETIMEOUT;\n }\n\n RT_DEBUG_IN_THREAD_CONTEXT;\n /* suspend current thread */\n rt_ipc_list_suspend(&(mb->parent.suspend_thread),\n thread,\n mb->parent.parent.flag);\n\n /* has waiting time, start thread timer */\n if (timeout > 0)\n {\n /* get the start tick of timer */\n tick_delta = rt_tick_get();\n\n RT_DEBUG_LOG(RT_DEBUG_IPC, (\"mb_recv: start timer of thread:%s\\n\",\n thread->name));\n\n /* reset the timeout of thread timer and start it */\n rt_timer_control(&(thread->thread_timer),\n RT_TIMER_CTRL_SET_TIME,\n &timeout);\n rt_timer_start(&(thread->thread_timer));\n }\n\n /* enable interrupt */\n rt_hw_interrupt_enable(temp);\n\n /* re-schedule */\n rt_schedule();\n\n /* resume from suspend state */\n if (thread->error != RT_EOK)\n {\n /* return error */\n return thread->error;\n }\n\n /* disable interrupt */\n temp = rt_hw_interrupt_disable();\n\n /* if it's not waiting forever and then re-calculate timeout tick */\n if (timeout > 0)\n {\n tick_delta = rt_tick_get() - tick_delta;\n timeout -= tick_delta;\n if (timeout < 0)\n timeout = 0;\n }\n }\n\n /* fill ptr */\n *value = mb->msg_pool[mb->out_offset];\n\n /* increase output offset */\n ++ mb->out_offset;\n if (mb->out_offset >= mb->size)\n mb->out_offset = 0;\n /* decrease message entry */\n mb->entry --;\n\n /* resume suspended thread */\n if (!rt_list_isempty(&(mb->suspend_sender_thread)))\n {\n rt_ipc_list_resume(&(mb->suspend_sender_thread));\n\n /* enable interrupt */\n rt_hw_interrupt_enable(temp);\n\n RT_OBJECT_HOOK_CALL(rt_object_take_hook, (&(mb->parent.parent)));\n\n rt_schedule();\n\n return RT_EOK;\n }\n\n /* enable interrupt */\n rt_hw_interrupt_enable(temp);\n\n RT_OBJECT_HOOK_CALL(rt_object_take_hook, (&(mb->parent.parent)));\n\n return RT_EOK;\n}\nRTM_EXPORT(rt_mb_recv);\n\n/**\n * This function can get or set some extra attributions of a mailbox object.\n *\n * @param mb the mailbox object\n * @param cmd the execution command\n * @param arg the execution argument\n *\n * @return the error code\n */\nrt_err_t rt_mb_control(rt_mailbox_t mb, int cmd, void *arg)\n{\n rt_ubase_t level;\n\n /* parameter check */\n RT_ASSERT(mb != RT_NULL);\n RT_ASSERT(rt_object_get_type(&mb->parent.parent) == RT_Object_Class_MailBox);\n\n if (cmd == RT_IPC_CMD_RESET)\n {\n /* disable interrupt */\n level = rt_hw_interrupt_disable();\n\n /* resume all waiting thread */\n rt_ipc_list_resume_all(&(mb->parent.suspend_thread));\n /* also resume all mailbox private suspended thread */\n rt_ipc_list_resume_all(&(mb->suspend_sender_thread));\n\n /* re-init mailbox */\n mb->entry = 0;\n mb->in_offset = 0;\n mb->out_offset = 0;\n\n /* enable interrupt */\n rt_hw_interrupt_enable(level);\n\n rt_schedule();\n\n return RT_EOK;\n }\n\n return -RT_ERROR;\n}\nRTM_EXPORT(rt_mb_control);\n#endif /* end of RT_USING_MAILBOX */\n\n#ifdef RT_USING_MESSAGEQUEUE\nstruct rt_mq_message\n{\n struct rt_mq_message *next;\n};\n\n/**\n * This function will initialize a message queue and put it under control of\n * resource management.\n *\n * @param mq the message object\n * @param name the name of message queue\n * @param msgpool the beginning address of buffer to save messages\n * @param msg_size the maximum size of message\n * @param pool_size the size of buffer to save messages\n * @param flag the flag of message queue\n *\n * @return the operation status, RT_EOK on successful\n */\nrt_err_t rt_mq_init(rt_mq_t mq,\n const char *name,\n void *msgpool,\n rt_size_t msg_size,\n rt_size_t pool_size,\n rt_uint8_t flag)\n{\n struct rt_mq_message *head;\n register rt_base_t temp;\n\n /* parameter check */\n RT_ASSERT(mq != RT_NULL);\n\n /* initialize object */\n rt_object_init(&(mq->parent.parent), RT_Object_Class_MessageQueue, name);\n\n /* set parent flag */\n mq->parent.parent.flag = flag;\n\n /* initialize ipc object */\n rt_ipc_object_init(&(mq->parent));\n\n /* set message pool */\n mq->msg_pool = msgpool;\n\n /* get correct message size */\n mq->msg_size = RT_ALIGN(msg_size, RT_ALIGN_SIZE);\n mq->max_msgs = pool_size / (mq->msg_size + sizeof(struct rt_mq_message));\n\n /* initialize message list */\n mq->msg_queue_head = RT_NULL;\n mq->msg_queue_tail = RT_NULL;\n\n /* initialize message empty list */\n mq->msg_queue_free = RT_NULL;\n for (temp = 0; temp < mq->max_msgs; temp ++)\n {\n head = (struct rt_mq_message *)((rt_uint8_t *)mq->msg_pool +\n temp * (mq->msg_size + sizeof(struct rt_mq_message)));\n head->next = (struct rt_mq_message *)mq->msg_queue_free;\n mq->msg_queue_free = head;\n }\n\n /* the initial entry is zero */\n mq->entry = 0;\n\n /* initialize an additional list of sender suspend thread */\n rt_list_init(&(mq->suspend_sender_thread));\n\n return RT_EOK;\n}\nRTM_EXPORT(rt_mq_init);\n\n/**\n * This function will detach a message queue object from resource management\n *\n * @param mq the message queue object\n *\n * @return the operation status, RT_EOK on successful\n */\nrt_err_t rt_mq_detach(rt_mq_t mq)\n{\n /* parameter check */\n RT_ASSERT(mq != RT_NULL);\n RT_ASSERT(rt_object_get_type(&mq->parent.parent) == RT_Object_Class_MessageQueue);\n RT_ASSERT(rt_object_is_systemobject(&mq->parent.parent));\n\n /* resume all suspended thread */\n rt_ipc_list_resume_all(&mq->parent.suspend_thread);\n /* also resume all message queue private suspended thread */\n rt_ipc_list_resume_all(&(mq->suspend_sender_thread));\n\n /* detach message queue object */\n rt_object_detach(&(mq->parent.parent));\n\n return RT_EOK;\n}\nRTM_EXPORT(rt_mq_detach);\n\n#ifdef RT_USING_HEAP\n/**\n * This function will create a message queue object from system resource\n *\n * @param name the name of message queue\n * @param msg_size the size of message\n * @param max_msgs the maximum number of message in queue\n * @param flag the flag of message queue\n *\n * @return the created message queue, RT_NULL on error happen\n */\nrt_mq_t rt_mq_create(const char *name,\n rt_size_t msg_size,\n rt_size_t max_msgs,\n rt_uint8_t flag)\n{\n struct rt_messagequeue *mq;\n struct rt_mq_message *head;\n register rt_base_t temp;\n\n RT_DEBUG_NOT_IN_INTERRUPT;\n\n /* allocate object */\n mq = (rt_mq_t)rt_object_allocate(RT_Object_Class_MessageQueue, name);\n if (mq == RT_NULL)\n return mq;\n\n /* set parent */\n mq->parent.parent.flag = flag;\n\n /* initialize ipc object */\n rt_ipc_object_init(&(mq->parent));\n\n /* initialize message queue */\n\n /* get correct message size */\n mq->msg_size = RT_ALIGN(msg_size, RT_ALIGN_SIZE);\n mq->max_msgs = max_msgs;\n\n /* allocate message pool */\n mq->msg_pool = RT_KERNEL_MALLOC((mq->msg_size + sizeof(struct rt_mq_message)) * mq->max_msgs);\n if (mq->msg_pool == RT_NULL)\n {\n rt_object_delete(&(mq->parent.parent));\n\n return RT_NULL;\n }\n\n /* initialize message list */\n mq->msg_queue_head = RT_NULL;\n mq->msg_queue_tail = RT_NULL;\n\n /* initialize message empty list */\n mq->msg_queue_free = RT_NULL;\n for (temp = 0; temp < mq->max_msgs; temp ++)\n {\n head = (struct rt_mq_message *)((rt_uint8_t *)mq->msg_pool +\n temp * (mq->msg_size + sizeof(struct rt_mq_message)));\n head->next = (struct rt_mq_message *)mq->msg_queue_free;\n mq->msg_queue_free = head;\n }\n\n /* the initial entry is zero */\n mq->entry = 0;\n\n /* initialize an additional list of sender suspend thread */\n rt_list_init(&(mq->suspend_sender_thread));\n\n return mq;\n}\nRTM_EXPORT(rt_mq_create);\n\n/**\n * This function will delete a message queue object and release the memory\n *\n * @param mq the message queue object\n *\n * @return the error code\n */\nrt_err_t rt_mq_delete(rt_mq_t mq)\n{\n RT_DEBUG_NOT_IN_INTERRUPT;\n\n /* parameter check */\n RT_ASSERT(mq != RT_NULL);\n RT_ASSERT(rt_object_get_type(&mq->parent.parent) == RT_Object_Class_MessageQueue);\n RT_ASSERT(rt_object_is_systemobject(&mq->parent.parent) == RT_FALSE);\n\n /* resume all suspended thread */\n rt_ipc_list_resume_all(&(mq->parent.suspend_thread));\n /* also resume all message queue private suspended thread */\n rt_ipc_list_resume_all(&(mq->suspend_sender_thread));\n\n /* free message queue pool */\n RT_KERNEL_FREE(mq->msg_pool);\n\n /* delete message queue object */\n rt_object_delete(&(mq->parent.parent));\n\n return RT_EOK;\n}\nRTM_EXPORT(rt_mq_delete);\n#endif\n\n/**\n * This function will send a message to message queue object. If the message queue is full,\n * current thread will be suspended until timeout.\n *\n * @param mq the message queue object\n * @param buffer the message\n * @param size the size of buffer\n * @param timeout the waiting time\n *\n * @return the error code\n */\nrt_err_t rt_mq_send_wait(rt_mq_t mq,\n const void *buffer,\n rt_size_t size,\n rt_int32_t timeout)\n{\n register rt_ubase_t temp;\n struct rt_mq_message *msg;\n rt_uint32_t tick_delta;\n struct rt_thread *thread;\n\n /* parameter check */\n RT_ASSERT(mq != RT_NULL);\n RT_ASSERT(rt_object_get_type(&mq->parent.parent) == RT_Object_Class_MessageQueue);\n RT_ASSERT(buffer != RT_NULL);\n RT_ASSERT(size != 0);\n\n /* greater than one message size */\n if (size > mq->msg_size)\n return -RT_ERROR;\n\n /* initialize delta tick */\n tick_delta = 0;\n /* get current thread */\n thread = rt_thread_self();\n\n RT_OBJECT_HOOK_CALL(rt_object_put_hook, (&(mq->parent.parent)));\n\n /* disable interrupt */\n temp = rt_hw_interrupt_disable();\n\n /* get a free list, there must be an empty item */\n msg = (struct rt_mq_message *)mq->msg_queue_free;\n /* for non-blocking call */\n if (msg == RT_NULL && timeout == 0)\n {\n /* enable interrupt */\n rt_hw_interrupt_enable(temp);\n\n return -RT_EFULL;\n }\n\n /* message queue is full */\n while ((msg = mq->msg_queue_free) == RT_NULL)\n {\n /* reset error number in thread */\n thread->error = RT_EOK;\n\n /* no waiting, return timeout */\n if (timeout == 0)\n {\n /* enable interrupt */\n rt_hw_interrupt_enable(temp);\n\n return -RT_EFULL;\n }\n\n RT_DEBUG_IN_THREAD_CONTEXT;\n /* suspend current thread */\n rt_ipc_list_suspend(&(mq->suspend_sender_thread),\n thread,\n mq->parent.parent.flag);\n\n /* has waiting time, start thread timer */\n if (timeout > 0)\n {\n /* get the start tick of timer */\n tick_delta = rt_tick_get();\n\n RT_DEBUG_LOG(RT_DEBUG_IPC, (\"mq_send_wait: start timer of thread:%s\\n\",\n thread->name));\n\n /* reset the timeout of thread timer and start it */\n rt_timer_control(&(thread->thread_timer),\n RT_TIMER_CTRL_SET_TIME,\n &timeout);\n rt_timer_start(&(thread->thread_timer));\n }\n\n /* enable interrupt */\n rt_hw_interrupt_enable(temp);\n\n /* re-schedule */\n rt_schedule();\n\n /* resume from suspend state */\n if (thread->error != RT_EOK)\n {\n /* return error */\n return thread->error;\n }\n\n /* disable interrupt */\n temp = rt_hw_interrupt_disable();\n\n /* if it's not waiting forever and then re-calculate timeout tick */\n if (timeout > 0)\n {\n tick_delta = rt_tick_get() - tick_delta;\n timeout -= tick_delta;\n if (timeout < 0)\n timeout = 0;\n }\n }\n\n /* move free list pointer */\n mq->msg_queue_free = msg->next;\n\n /* enable interrupt */\n rt_hw_interrupt_enable(temp);\n\n /* the msg is the new tailer of list, the next shall be NULL */\n msg->next = RT_NULL;\n /* copy buffer */\n rt_memcpy(msg + 1, buffer, size);\n\n /* disable interrupt */\n temp = rt_hw_interrupt_disable();\n /* link msg to message queue */\n if (mq->msg_queue_tail != RT_NULL)\n {\n /* if the tail exists, */\n ((struct rt_mq_message *)mq->msg_queue_tail)->next = msg;\n }\n\n /* set new tail */\n mq->msg_queue_tail = msg;\n /* if the head is empty, set head */\n if (mq->msg_queue_head == RT_NULL)\n mq->msg_queue_head = msg;\n\n /* increase message entry */\n mq->entry ++;\n\n /* resume suspended thread */\n if (!rt_list_isempty(&mq->parent.suspend_thread))\n {\n rt_ipc_list_resume(&(mq->parent.suspend_thread));\n\n /* enable interrupt */\n rt_hw_interrupt_enable(temp);\n\n rt_schedule();\n\n return RT_EOK;\n }\n\n /* enable interrupt */\n rt_hw_interrupt_enable(temp);\n\n return RT_EOK;\n}\nRTM_EXPORT(rt_mq_send_wait)\n\n/**\n * This function will send a message to message queue object, if there are\n * threads suspended on message queue object, it will be waked up.\n *\n * @param mq the message queue object\n * @param buffer the message\n * @param size the size of buffer\n *\n * @return the error code\n */\nrt_err_t rt_mq_send(rt_mq_t mq, const void *buffer, rt_size_t size)\n{\n return rt_mq_send_wait(mq, buffer, size, 0);\n}\nRTM_EXPORT(rt_mq_send);\n\n/**\n * This function will send an urgent message to message queue object, which\n * means the message will be inserted to the head of message queue. If there\n * are threads suspended on message queue object, it will be waked up.\n *\n * @param mq the message queue object\n * @param buffer the message\n * @param size the size of buffer\n *\n * @return the error code\n */\nrt_err_t rt_mq_urgent(rt_mq_t mq, const void *buffer, rt_size_t size)\n{\n register rt_ubase_t temp;\n struct rt_mq_message *msg;\n\n /* parameter check */\n RT_ASSERT(mq != RT_NULL);\n RT_ASSERT(rt_object_get_type(&mq->parent.parent) == RT_Object_Class_MessageQueue);\n RT_ASSERT(buffer != RT_NULL);\n RT_ASSERT(size != 0);\n\n /* greater than one message size */\n if (size > mq->msg_size)\n return -RT_ERROR;\n\n RT_OBJECT_HOOK_CALL(rt_object_put_hook, (&(mq->parent.parent)));\n\n /* disable interrupt */\n temp = rt_hw_interrupt_disable();\n\n /* get a free list, there must be an empty item */\n msg = (struct rt_mq_message *)mq->msg_queue_free;\n /* message queue is full */\n if (msg == RT_NULL)\n {\n /* enable interrupt */\n rt_hw_interrupt_enable(temp);\n\n return -RT_EFULL;\n }\n /* move free list pointer */\n mq->msg_queue_free = msg->next;\n\n /* enable interrupt */\n rt_hw_interrupt_enable(temp);\n\n /* copy buffer */\n rt_memcpy(msg + 1, buffer, size);\n\n /* disable interrupt */\n temp = rt_hw_interrupt_disable();\n\n /* link msg to the beginning of message queue */\n msg->next = (struct rt_mq_message *)mq->msg_queue_head;\n mq->msg_queue_head = msg;\n\n /* if there is no tail */\n if (mq->msg_queue_tail == RT_NULL)\n mq->msg_queue_tail = msg;\n\n /* increase message entry */\n mq->entry ++;\n\n /* resume suspended thread */\n if (!rt_list_isempty(&mq->parent.suspend_thread))\n {\n rt_ipc_list_resume(&(mq->parent.suspend_thread));\n\n /* enable interrupt */\n rt_hw_interrupt_enable(temp);\n\n rt_schedule();\n\n return RT_EOK;\n }\n\n /* enable interrupt */\n rt_hw_interrupt_enable(temp);\n\n return RT_EOK;\n}\nRTM_EXPORT(rt_mq_urgent);\n\n/**\n * This function will receive a message from message queue object, if there is\n * no message in message queue object, the thread shall wait for a specified\n * time.\n *\n * @param mq the message queue object\n * @param buffer the received message will be saved in\n * @param size the size of buffer\n * @param timeout the waiting time\n *\n * @return the error code\n */\nrt_err_t rt_mq_recv(rt_mq_t mq,\n void *buffer,\n rt_size_t size,\n rt_int32_t timeout)\n{\n struct rt_thread *thread;\n register rt_ubase_t temp;\n struct rt_mq_message *msg;\n rt_uint32_t tick_delta;\n\n /* parameter check */\n RT_ASSERT(mq != RT_NULL);\n RT_ASSERT(rt_object_get_type(&mq->parent.parent) == RT_Object_Class_MessageQueue);\n RT_ASSERT(buffer != RT_NULL);\n RT_ASSERT(size != 0);\n\n /* initialize delta tick */\n tick_delta = 0;\n /* get current thread */\n thread = rt_thread_self();\n RT_OBJECT_HOOK_CALL(rt_object_trytake_hook, (&(mq->parent.parent)));\n\n /* disable interrupt */\n temp = rt_hw_interrupt_disable();\n\n /* for non-blocking call */\n if (mq->entry == 0 && timeout == 0)\n {\n rt_hw_interrupt_enable(temp);\n\n return -RT_ETIMEOUT;\n }\n\n /* message queue is empty */\n while (mq->entry == 0)\n {\n RT_DEBUG_IN_THREAD_CONTEXT;\n\n /* reset error number in thread */\n thread->error = RT_EOK;\n\n /* no waiting, return timeout */\n if (timeout == 0)\n {\n /* enable interrupt */\n rt_hw_interrupt_enable(temp);\n\n thread->error = -RT_ETIMEOUT;\n\n return -RT_ETIMEOUT;\n }\n\n /* suspend current thread */\n rt_ipc_list_suspend(&(mq->parent.suspend_thread),\n thread,\n mq->parent.parent.flag);\n\n /* has waiting time, start thread timer */\n if (timeout > 0)\n {\n /* get the start tick of timer */\n tick_delta = rt_tick_get();\n\n RT_DEBUG_LOG(RT_DEBUG_IPC, (\"set thread:%s to timer list\\n\",\n thread->name));\n\n /* reset the timeout of thread timer and start it */\n rt_timer_control(&(thread->thread_timer),\n RT_TIMER_CTRL_SET_TIME,\n &timeout);\n rt_timer_start(&(thread->thread_timer));\n }\n\n /* enable interrupt */\n rt_hw_interrupt_enable(temp);\n\n /* re-schedule */\n rt_schedule();\n\n /* recv message */\n if (thread->error != RT_EOK)\n {\n /* return error */\n return thread->error;\n }\n\n /* disable interrupt */\n temp = rt_hw_interrupt_disable();\n\n /* if it's not waiting forever and then re-calculate timeout tick */\n if (timeout > 0)\n {\n tick_delta = rt_tick_get() - tick_delta;\n timeout -= tick_delta;\n if (timeout < 0)\n timeout = 0;\n }\n }\n\n /* get message from queue */\n msg = (struct rt_mq_message *)mq->msg_queue_head;\n\n /* move message queue head */\n mq->msg_queue_head = msg->next;\n /* reach queue tail, set to NULL */\n if (mq->msg_queue_tail == msg)\n mq->msg_queue_tail = RT_NULL;\n\n /* decrease message entry */\n mq->entry --;\n\n /* enable interrupt */\n rt_hw_interrupt_enable(temp);\n\n /* copy message */\n rt_memcpy(buffer, msg + 1, size > mq->msg_size ? mq->msg_size : size);\n\n /* disable interrupt */\n temp = rt_hw_interrupt_disable();\n /* put message to free list */\n msg->next = (struct rt_mq_message *)mq->msg_queue_free;\n mq->msg_queue_free = msg;\n\n /* resume suspended thread */\n if (!rt_list_isempty(&(mq->suspend_sender_thread)))\n {\n rt_ipc_list_resume(&(mq->suspend_sender_thread));\n\n /* enable interrupt */\n rt_hw_interrupt_enable(temp);\n\n RT_OBJECT_HOOK_CALL(rt_object_take_hook, (&(mq->parent.parent)));\n\n rt_schedule();\n\n return RT_EOK;\n }\n\n /* enable interrupt */\n rt_hw_interrupt_enable(temp);\n\n RT_OBJECT_HOOK_CALL(rt_object_take_hook, (&(mq->parent.parent)));\n\n return RT_EOK;\n}\nRTM_EXPORT(rt_mq_recv);\n\n/**\n * This function can get or set some extra attributions of a message queue\n * object.\n *\n * @param mq the message queue object\n * @param cmd the execution command\n * @param arg the execution argument\n *\n * @return the error code\n */\nrt_err_t rt_mq_control(rt_mq_t mq, int cmd, void *arg)\n{\n rt_ubase_t level;\n struct rt_mq_message *msg;\n\n /* parameter check */\n RT_ASSERT(mq != RT_NULL);\n RT_ASSERT(rt_object_get_type(&mq->parent.parent) == RT_Object_Class_MessageQueue);\n\n if (cmd == RT_IPC_CMD_RESET)\n {\n /* disable interrupt */\n level = rt_hw_interrupt_disable();\n\n /* resume all waiting thread */\n rt_ipc_list_resume_all(&mq->parent.suspend_thread);\n /* also resume all message queue private suspended thread */\n rt_ipc_list_resume_all(&(mq->suspend_sender_thread));\n\n /* release all message in the queue */\n while (mq->msg_queue_head != RT_NULL)\n {\n /* get message from queue */\n msg = (struct rt_mq_message *)mq->msg_queue_head;\n\n /* move message queue head */\n mq->msg_queue_head = msg->next;\n /* reach queue tail, set to NULL */\n if (mq->msg_queue_tail == msg)\n mq->msg_queue_tail = RT_NULL;\n\n /* put message to free list */\n msg->next = (struct rt_mq_message *)mq->msg_queue_free;\n mq->msg_queue_free = msg;\n }\n\n /* clean entry */\n mq->entry = 0;\n\n /* enable interrupt */\n rt_hw_interrupt_enable(level);\n\n rt_schedule();\n\n return RT_EOK;\n }\n\n return -RT_ERROR;\n}\nRTM_EXPORT(rt_mq_control);\n#endif /* end of RT_USING_MESSAGEQUEUE */\n\n/**@}*/\n", "src/irq.c": "/*\n * Copyright (c) 2006-2018, RT-Thread Development Team\n *\n * SPDX-License-Identifier: Apache-2.0\n *\n * Change Logs:\n * Date Author Notes\n * 2006-02-24 Bernard first version\n * 2006-05-03 Bernard add IRQ_DEBUG\n * 2016-08-09 ArdaFu add interrupt enter and leave hook.\n * 2018-11-22 Jesven rt_interrupt_get_nest function add disable irq\n */\n\n#include <rthw.h>\n#include <rtthread.h>\n\n#ifdef RT_USING_HOOK\n\nstatic void (*rt_interrupt_enter_hook)(void);\nstatic void (*rt_interrupt_leave_hook)(void);\n\n/**\n * @ingroup Hook\n * This function set a hook function when the system enter a interrupt \n *\n * @note the hook function must be simple and never be blocked or suspend.\n */\nvoid rt_interrupt_enter_sethook(void (*hook)(void))\n{\n rt_interrupt_enter_hook = hook;\n}\n/**\n * @ingroup Hook\n * This function set a hook function when the system exit a interrupt. \n *\n * @note the hook function must be simple and never be blocked or suspend.\n */\nvoid rt_interrupt_leave_sethook(void (*hook)(void))\n{\n rt_interrupt_leave_hook = hook;\n}\n#endif\n\n/* #define IRQ_DEBUG */\n\n/**\n * @addtogroup Kernel\n */\n\n/**@{*/\n\n#ifdef RT_USING_SMP\n#define rt_interrupt_nest rt_cpu_self()->irq_nest\n#else\nvolatile rt_uint8_t rt_interrupt_nest = 0;\n#endif\n\n/**\n * This function will be invoked by BSP, when enter interrupt service routine\n *\n * @note please don't invoke this routine in application\n *\n * @see rt_interrupt_leave\n */\nvoid rt_interrupt_enter(void)\n{\n rt_base_t level;\n\n RT_DEBUG_LOG(RT_DEBUG_IRQ, (\"irq coming..., irq nest:%d\\n\",\n rt_interrupt_nest));\n\n level = rt_hw_interrupt_disable();\n rt_interrupt_nest ++;\n RT_OBJECT_HOOK_CALL(rt_interrupt_enter_hook,());\n rt_hw_interrupt_enable(level);\n}\nRTM_EXPORT(rt_interrupt_enter);\n\n/**\n * This function will be invoked by BSP, when leave interrupt service routine\n *\n * @note please don't invoke this routine in application\n *\n * @see rt_interrupt_enter\n */\nvoid rt_interrupt_leave(void)\n{\n rt_base_t level;\n\n RT_DEBUG_LOG(RT_DEBUG_IRQ, (\"irq leave, irq nest:%d\\n\",\n rt_interrupt_nest));\n\n level = rt_hw_interrupt_disable();\n rt_interrupt_nest --;\n RT_OBJECT_HOOK_CALL(rt_interrupt_leave_hook,());\n rt_hw_interrupt_enable(level);\n}\nRTM_EXPORT(rt_interrupt_leave);\n\n/**\n * This function will return the nest of interrupt.\n *\n * User application can invoke this function to get whether current\n * context is interrupt context.\n *\n * @return the number of nested interrupts.\n */\nRT_WEAK rt_uint8_t rt_interrupt_get_nest(void)\n{\n rt_uint8_t ret;\n rt_base_t level;\n\n level = rt_hw_interrupt_disable();\n ret = rt_interrupt_nest;\n rt_hw_interrupt_enable(level);\n return ret;\n}\nRTM_EXPORT(rt_interrupt_get_nest);\n\nRTM_EXPORT(rt_hw_interrupt_disable);\nRTM_EXPORT(rt_hw_interrupt_enable);\n\n/**@}*/\n\n", "src/kservice.c": "/*\n * Copyright (c) 2006-2018, RT-Thread Development Team\n *\n * SPDX-License-Identifier: Apache-2.0\n *\n * Change Logs:\n * Date Author Notes\n * 2006-03-16 Bernard the first version\n * 2006-05-25 Bernard rewrite vsprintf\n * 2006-08-10 Bernard add rt_show_version\n * 2010-03-17 Bernard remove rt_strlcpy function\n * fix gcc compiling issue.\n * 2010-04-15 Bernard remove weak definition on ICCM16C compiler\n * 2012-07-18 Arda add the alignment display for signed integer\n * 2012-11-23 Bernard fix IAR compiler error.\n * 2012-12-22 Bernard fix rt_kprintf issue, which found by Grissiom.\n * 2013-06-24 Bernard remove rt_kprintf if RT_USING_CONSOLE is not defined.\n * 2013-09-24 aozima make sure the device is in STREAM mode when used by rt_kprintf.\n * 2015-07-06 Bernard Add rt_assert_handler routine.\n */\n\n#include <rtthread.h>\n#include <rthw.h>\n\n#ifdef RT_USING_MODULE\n#include <dlmodule.h>\n#endif\n\n/* use precision */\n#define RT_PRINTF_PRECISION\n\n/**\n * @addtogroup KernelService\n */\n\n/**@{*/\n\n/* global errno in RT-Thread */\nstatic volatile int __rt_errno;\n\n#if defined(RT_USING_DEVICE) && defined(RT_USING_CONSOLE)\nstatic rt_device_t _console_device = RT_NULL;\n#endif\n\n/*\n * This function will get errno\n *\n * @return errno\n */\nrt_err_t rt_get_errno(void)\n{\n rt_thread_t tid;\n\n if (rt_interrupt_get_nest() != 0)\n {\n /* it's in interrupt context */\n return __rt_errno;\n }\n\n tid = rt_thread_self();\n if (tid == RT_NULL)\n return __rt_errno;\n\n return tid->error;\n}\nRTM_EXPORT(rt_get_errno);\n\n/*\n * This function will set errno\n *\n * @param error the errno shall be set\n */\nvoid rt_set_errno(rt_err_t error)\n{\n rt_thread_t tid;\n\n if (rt_interrupt_get_nest() != 0)\n {\n /* it's in interrupt context */\n __rt_errno = error;\n\n return;\n }\n\n tid = rt_thread_self();\n if (tid == RT_NULL)\n {\n __rt_errno = error;\n\n return;\n }\n\n tid->error = error;\n}\nRTM_EXPORT(rt_set_errno);\n\n/**\n * This function returns errno.\n *\n * @return the errno in the system\n */\nint *_rt_errno(void)\n{\n rt_thread_t tid;\n\n if (rt_interrupt_get_nest() != 0)\n return (int *)&__rt_errno;\n\n tid = rt_thread_self();\n if (tid != RT_NULL)\n return (int *) & (tid->error);\n\n return (int *)&__rt_errno;\n}\nRTM_EXPORT(_rt_errno);\n\n/**\n * This function will set the content of memory to specified value\n *\n * @param s the address of source memory\n * @param c the value shall be set in content\n * @param count the copied length\n *\n * @return the address of source memory\n */\nvoid *rt_memset(void *s, int c, rt_ubase_t count)\n{\n#ifdef RT_USING_TINY_SIZE\n char *xs = (char *)s;\n\n while (count--)\n *xs++ = c;\n\n return s;\n#else\n#define LBLOCKSIZE (sizeof(long))\n#define UNALIGNED(X) ((long)X & (LBLOCKSIZE - 1))\n#define TOO_SMALL(LEN) ((LEN) < LBLOCKSIZE)\n\n unsigned int i;\n char *m = (char *)s;\n unsigned long buffer;\n unsigned long *aligned_addr;\n unsigned int d = c & 0xff; /* To avoid sign extension, copy C to an\n unsigned variable. */\n\n if (!TOO_SMALL(count) && !UNALIGNED(s))\n {\n /* If we get this far, we know that n is large and m is word-aligned. */\n aligned_addr = (unsigned long *)s;\n\n /* Store D into each char sized location in BUFFER so that\n * we can set large blocks quickly.\n */\n if (LBLOCKSIZE == 4)\n {\n buffer = (d << 8) | d;\n buffer |= (buffer << 16);\n }\n else\n {\n buffer = 0;\n for (i = 0; i < LBLOCKSIZE; i ++)\n buffer = (buffer << 8) | d;\n }\n\n while (count >= LBLOCKSIZE * 4)\n {\n *aligned_addr++ = buffer;\n *aligned_addr++ = buffer;\n *aligned_addr++ = buffer;\n *aligned_addr++ = buffer;\n count -= 4 * LBLOCKSIZE;\n }\n\n while (count >= LBLOCKSIZE)\n {\n *aligned_addr++ = buffer;\n count -= LBLOCKSIZE;\n }\n\n /* Pick up the remainder with a bytewise loop. */\n m = (char *)aligned_addr;\n }\n\n while (count--)\n {\n *m++ = (char)d;\n }\n\n return s;\n\n#undef LBLOCKSIZE\n#undef UNALIGNED\n#undef TOO_SMALL\n#endif\n}\nRTM_EXPORT(rt_memset);\n\n/**\n * This function will copy memory content from source address to destination\n * address.\n *\n * @param dst the address of destination memory\n * @param src the address of source memory\n * @param count the copied length\n *\n * @return the address of destination memory\n */\nvoid *rt_memcpy(void *dst, const void *src, rt_ubase_t count)\n{\n#ifdef RT_USING_TINY_SIZE\n char *tmp = (char *)dst, *s = (char *)src;\n rt_ubase_t len;\n\n if (tmp <= s || tmp > (s + count))\n {\n while (count--)\n *tmp ++ = *s ++;\n }\n else\n {\n for (len = count; len > 0; len --)\n tmp[len - 1] = s[len - 1];\n }\n\n return dst;\n#else\n\n#define UNALIGNED(X, Y) \\\n (((long)X & (sizeof (long) - 1)) | ((long)Y & (sizeof (long) - 1)))\n#define BIGBLOCKSIZE (sizeof (long) << 2)\n#define LITTLEBLOCKSIZE (sizeof (long))\n#define TOO_SMALL(LEN) ((LEN) < BIGBLOCKSIZE)\n\n char *dst_ptr = (char *)dst;\n char *src_ptr = (char *)src;\n long *aligned_dst;\n long *aligned_src;\n int len = count;\n\n /* If the size is small, or either SRC or DST is unaligned,\n then punt into the byte copy loop. This should be rare. */\n if (!TOO_SMALL(len) && !UNALIGNED(src_ptr, dst_ptr))\n {\n aligned_dst = (long *)dst_ptr;\n aligned_src = (long *)src_ptr;\n\n /* Copy 4X long words at a time if possible. */\n while (len >= BIGBLOCKSIZE)\n {\n *aligned_dst++ = *aligned_src++;\n *aligned_dst++ = *aligned_src++;\n *aligned_dst++ = *aligned_src++;\n *aligned_dst++ = *aligned_src++;\n len -= BIGBLOCKSIZE;\n }\n\n /* Copy one long word at a time if possible. */\n while (len >= LITTLEBLOCKSIZE)\n {\n *aligned_dst++ = *aligned_src++;\n len -= LITTLEBLOCKSIZE;\n }\n\n /* Pick up any residual with a byte copier. */\n dst_ptr = (char *)aligned_dst;\n src_ptr = (char *)aligned_src;\n }\n\n while (len--)\n *dst_ptr++ = *src_ptr++;\n\n return dst;\n#undef UNALIGNED\n#undef BIGBLOCKSIZE\n#undef LITTLEBLOCKSIZE\n#undef TOO_SMALL\n#endif\n}\nRTM_EXPORT(rt_memcpy);\n\n/**\n * This function will move memory content from source address to destination\n * address.\n *\n * @param dest the address of destination memory\n * @param src the address of source memory\n * @param n the copied length\n *\n * @return the address of destination memory\n */\nvoid *rt_memmove(void *dest, const void *src, rt_ubase_t n)\n{\n char *tmp = (char *)dest, *s = (char *)src;\n\n if (s < tmp && tmp < s + n)\n {\n tmp += n;\n s += n;\n\n while (n--)\n *(--tmp) = *(--s);\n }\n else\n {\n while (n--)\n *tmp++ = *s++;\n }\n\n return dest;\n}\nRTM_EXPORT(rt_memmove);\n\n/**\n * This function will compare two areas of memory\n *\n * @param cs one area of memory\n * @param ct another area of memory\n * @param count the size of the area\n *\n * @return the result\n */\nrt_int32_t rt_memcmp(const void *cs, const void *ct, rt_ubase_t count)\n{\n const unsigned char *su1, *su2;\n int res = 0;\n\n for (su1 = (const unsigned char *)cs, su2 = (const unsigned char *)ct; 0 < count; ++su1, ++su2, count--)\n if ((res = *su1 - *su2) != 0)\n break;\n\n return res;\n}\nRTM_EXPORT(rt_memcmp);\n\n/**\n * This function will return the first occurrence of a string.\n *\n * @param s1 the source string\n * @param s2 the find string\n *\n * @return the first occurrence of a s2 in s1, or RT_NULL if no found.\n */\nchar *rt_strstr(const char *s1, const char *s2)\n{\n int l1, l2;\n\n l2 = rt_strlen(s2);\n if (!l2)\n return (char *)s1;\n l1 = rt_strlen(s1);\n while (l1 >= l2)\n {\n l1 --;\n if (!rt_memcmp(s1, s2, l2))\n return (char *)s1;\n s1 ++;\n }\n\n return RT_NULL;\n}\nRTM_EXPORT(rt_strstr);\n\n/**\n * This function will compare two strings while ignoring differences in case\n *\n * @param a the string to be compared\n * @param b the string to be compared\n *\n * @return the result\n */\nrt_int32_t rt_strcasecmp(const char *a, const char *b)\n{\n int ca, cb;\n\n do\n {\n ca = *a++ & 0xff;\n cb = *b++ & 0xff;\n if (ca >= 'A' && ca <= 'Z')\n ca += 'a' - 'A';\n if (cb >= 'A' && cb <= 'Z')\n cb += 'a' - 'A';\n }\n while (ca == cb && ca != '\\0');\n\n return ca - cb;\n}\nRTM_EXPORT(rt_strcasecmp);\n\n/**\n * This function will copy string no more than n bytes.\n *\n * @param dst the string to copy\n * @param src the string to be copied\n * @param n the maximum copied length\n *\n * @return the result\n */\nchar *rt_strncpy(char *dst, const char *src, rt_ubase_t n)\n{\n if (n != 0)\n {\n char *d = dst;\n const char *s = src;\n\n do\n {\n if ((*d++ = *s++) == 0)\n {\n /* NUL pad the remaining n-1 bytes */\n while (--n != 0)\n *d++ = 0;\n break;\n }\n } while (--n != 0);\n }\n\n return (dst);\n}\nRTM_EXPORT(rt_strncpy);\n\n/**\n * This function will compare two strings with specified maximum length\n *\n * @param cs the string to be compared\n * @param ct the string to be compared\n * @param count the maximum compare length\n *\n * @return the result\n */\nrt_int32_t rt_strncmp(const char *cs, const char *ct, rt_ubase_t count)\n{\n register signed char __res = 0;\n\n while (count)\n {\n if ((__res = *cs - *ct++) != 0 || !*cs++)\n break;\n count --;\n }\n\n return __res;\n}\nRTM_EXPORT(rt_strncmp);\n\n/**\n * This function will compare two strings without specified length\n *\n * @param cs the string to be compared\n * @param ct the string to be compared\n *\n * @return the result\n */\nrt_int32_t rt_strcmp(const char *cs, const char *ct)\n{\n while (*cs && *cs == *ct)\n { \n cs++;\n ct++;\n }\n\n return (*cs - *ct);\n}\nRTM_EXPORT(rt_strcmp);\n\n/**\n * The strnlen() function returns the number of characters in the\n * string pointed to by s, excluding the terminating null byte ('\\0'),\n * but at most maxlen. In doing this, strnlen() looks only at the\n * first maxlen characters in the string pointed to by s and never\n * beyond s+maxlen.\n *\n * @param s the string\n * @param maxlen the max size\n * @return the length of string\n */\nrt_size_t rt_strnlen(const char *s, rt_ubase_t maxlen)\n{\n const char *sc;\n\n for (sc = s; *sc != '\\0' && (rt_ubase_t)(sc - s) < maxlen; ++sc) /* nothing */\n ;\n\n return sc - s;\n}\nRTM_EXPORT(rt_strnlen);\n\n/**\n * This function will return the length of a string, which terminate will\n * null character.\n *\n * @param s the string\n *\n * @return the length of string\n */\nrt_size_t rt_strlen(const char *s)\n{\n const char *sc;\n\n for (sc = s; *sc != '\\0'; ++sc) /* nothing */\n ;\n\n return sc - s;\n}\nRTM_EXPORT(rt_strlen);\n\n#ifdef RT_USING_HEAP\n/**\n * This function will duplicate a string.\n *\n * @param s the string to be duplicated\n *\n * @return the duplicated string pointer\n */\nchar *rt_strdup(const char *s)\n{\n rt_size_t len = rt_strlen(s) + 1;\n char *tmp = (char *)rt_malloc(len);\n\n if (!tmp)\n return RT_NULL;\n\n rt_memcpy(tmp, s, len);\n\n return tmp;\n}\nRTM_EXPORT(rt_strdup);\n#if defined(__CC_ARM) || defined(__CLANG_ARM)\nchar *strdup(const char *s) __attribute__((alias(\"rt_strdup\")));\n#endif\n#endif\n\n/**\n * This function will show the version of rt-thread rtos\n */\nvoid rt_show_version(void)\n{\n rt_kprintf(\"\\n \\\\ | /\\n\");\n rt_kprintf(\"- RT - Thread Operating System\\n\");\n rt_kprintf(\" / | \\\\ %d.%d.%d build %s\\n\",\n RT_VERSION, RT_SUBVERSION, RT_REVISION, __DATE__);\n rt_kprintf(\" 2006 - 2020 Copyright by rt-thread team\\n\");\n}\nRTM_EXPORT(rt_show_version);\n\n/* private function */\n#define _ISDIGIT(c) ((unsigned)((c) - '0') < 10)\n\n#ifdef RT_PRINTF_LONGLONG\nrt_inline int divide(long long *n, int base)\n{\n int res;\n\n /* optimized for processor which does not support divide instructions. */\n if (base == 10)\n {\n res = (int)(((unsigned long long)*n) % 10U);\n *n = (long long)(((unsigned long long)*n) / 10U);\n }\n else\n {\n res = (int)(((unsigned long long)*n) % 16U);\n *n = (long long)(((unsigned long long)*n) / 16U);\n }\n\n return res;\n}\n#else\nrt_inline int divide(long *n, int base)\n{\n int res;\n\n /* optimized for processor which does not support divide instructions. */\n if (base == 10)\n {\n res = (int)(((unsigned long)*n) % 10U);\n *n = (long)(((unsigned long)*n) / 10U);\n }\n else\n {\n res = (int)(((unsigned long)*n) % 16U);\n *n = (long)(((unsigned long)*n) / 16U);\n }\n\n return res;\n}\n#endif\n\nrt_inline int skip_atoi(const char **s)\n{\n register int i = 0;\n while (_ISDIGIT(**s))\n i = i * 10 + *((*s)++) - '0';\n\n return i;\n}\n\n#define ZEROPAD (1 << 0) /* pad with zero */\n#define SIGN (1 << 1) /* unsigned/signed long */\n#define PLUS (1 << 2) /* show plus */\n#define SPACE (1 << 3) /* space if plus */\n#define LEFT (1 << 4) /* left justified */\n#define SPECIAL (1 << 5) /* 0x */\n#define LARGE (1 << 6) /* use 'ABCDEF' instead of 'abcdef' */\n\n#ifdef RT_PRINTF_PRECISION\nstatic char *print_number(char *buf,\n char *end,\n#ifdef RT_PRINTF_LONGLONG\n long long num,\n#else\n long num,\n#endif\n int base,\n int s,\n int precision,\n int type)\n#else\nstatic char *print_number(char *buf,\n char *end,\n#ifdef RT_PRINTF_LONGLONG\n long long num,\n#else\n long num,\n#endif\n int base,\n int s,\n int type)\n#endif\n{\n char c, sign;\n#ifdef RT_PRINTF_LONGLONG\n char tmp[32];\n#else\n char tmp[16];\n#endif\n int precision_bak = precision;\n const char *digits;\n static const char small_digits[] = \"0123456789abcdef\";\n static const char large_digits[] = \"0123456789ABCDEF\";\n register int i;\n register int size;\n\n size = s;\n\n digits = (type & LARGE) ? large_digits : small_digits;\n if (type & LEFT)\n type &= ~ZEROPAD;\n\n c = (type & ZEROPAD) ? '0' : ' ';\n\n /* get sign */\n sign = 0;\n if (type & SIGN)\n {\n if (num < 0)\n {\n sign = '-';\n num = -num;\n }\n else if (type & PLUS)\n sign = '+';\n else if (type & SPACE)\n sign = ' ';\n }\n\n#ifdef RT_PRINTF_SPECIAL\n if (type & SPECIAL)\n {\n if (base == 16)\n size -= 2;\n else if (base == 8)\n size--;\n }\n#endif\n\n i = 0;\n if (num == 0)\n tmp[i++] = '0';\n else\n {\n while (num != 0)\n tmp[i++] = digits[divide(&num, base)];\n }\n\n#ifdef RT_PRINTF_PRECISION\n if (i > precision)\n precision = i;\n size -= precision;\n#else\n size -= i;\n#endif\n\n if (!(type & (ZEROPAD | LEFT)))\n {\n if ((sign) && (size > 0))\n size--;\n\n while (size-- > 0)\n {\n if (buf < end)\n *buf = ' ';\n ++ buf;\n }\n }\n\n if (sign)\n {\n if (buf < end)\n {\n *buf = sign;\n }\n -- size;\n ++ buf;\n }\n\n#ifdef RT_PRINTF_SPECIAL\n if (type & SPECIAL)\n {\n if (base == 8)\n {\n if (buf < end)\n *buf = '0';\n ++ buf;\n }\n else if (base == 16)\n {\n if (buf < end)\n *buf = '0';\n ++ buf;\n if (buf < end)\n {\n *buf = type & LARGE ? 'X' : 'x';\n }\n ++ buf;\n }\n }\n#endif\n\n /* no align to the left */\n if (!(type & LEFT))\n {\n while (size-- > 0)\n {\n if (buf < end)\n *buf = c;\n ++ buf;\n }\n }\n\n#ifdef RT_PRINTF_PRECISION\n while (i < precision--)\n {\n if (buf < end)\n *buf = '0';\n ++ buf;\n }\n#endif\n\n /* put number in the temporary buffer */\n while (i-- > 0 && (precision_bak != 0))\n {\n if (buf < end)\n *buf = tmp[i];\n ++ buf;\n }\n\n while (size-- > 0)\n {\n if (buf < end)\n *buf = ' ';\n ++ buf;\n }\n\n return buf;\n}\n\nrt_int32_t rt_vsnprintf(char *buf,\n rt_size_t size,\n const char *fmt,\n va_list args)\n{\n#ifdef RT_PRINTF_LONGLONG\n unsigned long long num;\n#else\n rt_uint32_t num;\n#endif\n int i, len;\n char *str, *end, c;\n const char *s;\n\n rt_uint8_t base; /* the base of number */\n rt_uint8_t flags; /* flags to print number */\n rt_uint8_t qualifier; /* 'h', 'l', or 'L' for integer fields */\n rt_int32_t field_width; /* width of output field */\n\n#ifdef RT_PRINTF_PRECISION\n int precision; /* min. # of digits for integers and max for a string */\n#endif\n\n str = buf;\n end = buf + size;\n\n /* Make sure end is always >= buf */\n if (end < buf)\n {\n end = ((char *) - 1);\n size = end - buf;\n }\n\n for (; *fmt ; ++fmt)\n {\n if (*fmt != '%')\n {\n if (str < end)\n *str = *fmt;\n ++ str;\n continue;\n }\n\n /* process flags */\n flags = 0;\n\n while (1)\n {\n /* skips the first '%' also */\n ++ fmt;\n if (*fmt == '-') flags |= LEFT;\n else if (*fmt == '+') flags |= PLUS;\n else if (*fmt == ' ') flags |= SPACE;\n else if (*fmt == '#') flags |= SPECIAL;\n else if (*fmt == '0') flags |= ZEROPAD;\n else break;\n }\n\n /* get field width */\n field_width = -1;\n if (_ISDIGIT(*fmt)) field_width = skip_atoi(&fmt);\n else if (*fmt == '*')\n {\n ++ fmt;\n /* it's the next argument */\n field_width = va_arg(args, int);\n if (field_width < 0)\n {\n field_width = -field_width;\n flags |= LEFT;\n }\n }\n\n#ifdef RT_PRINTF_PRECISION\n /* get the precision */\n precision = -1;\n if (*fmt == '.')\n {\n ++ fmt;\n if (_ISDIGIT(*fmt)) precision = skip_atoi(&fmt);\n else if (*fmt == '*')\n {\n ++ fmt;\n /* it's the next argument */\n precision = va_arg(args, int);\n }\n if (precision < 0) precision = 0;\n }\n#endif\n /* get the conversion qualifier */\n qualifier = 0;\n#ifdef RT_PRINTF_LONGLONG\n if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L')\n#else\n if (*fmt == 'h' || *fmt == 'l')\n#endif\n {\n qualifier = *fmt;\n ++ fmt;\n#ifdef RT_PRINTF_LONGLONG\n if (qualifier == 'l' && *fmt == 'l')\n {\n qualifier = 'L';\n ++ fmt;\n }\n#endif\n }\n\n /* the default base */\n base = 10;\n\n switch (*fmt)\n {\n case 'c':\n if (!(flags & LEFT))\n {\n while (--field_width > 0)\n {\n if (str < end) *str = ' ';\n ++ str;\n }\n }\n\n /* get character */\n c = (rt_uint8_t)va_arg(args, int);\n if (str < end) *str = c;\n ++ str;\n\n /* put width */\n while (--field_width > 0)\n {\n if (str < end) *str = ' ';\n ++ str;\n }\n continue;\n\n case 's':\n s = va_arg(args, char *);\n if (!s) s = \"(NULL)\";\n\n len = rt_strlen(s);\n#ifdef RT_PRINTF_PRECISION\n if (precision > 0 && len > precision) len = precision;\n#endif\n\n if (!(flags & LEFT))\n {\n while (len < field_width--)\n {\n if (str < end) *str = ' ';\n ++ str;\n }\n }\n\n for (i = 0; i < len; ++i)\n {\n if (str < end) *str = *s;\n ++ str;\n ++ s;\n }\n\n while (len < field_width--)\n {\n if (str < end) *str = ' ';\n ++ str;\n }\n continue;\n\n case 'p':\n if (field_width == -1)\n {\n field_width = sizeof(void *) << 1;\n flags |= ZEROPAD;\n }\n#ifdef RT_PRINTF_PRECISION\n str = print_number(str, end,\n (long)va_arg(args, void *),\n 16, field_width, precision, flags);\n#else\n str = print_number(str, end,\n (long)va_arg(args, void *),\n 16, field_width, flags);\n#endif\n continue;\n\n case '%':\n if (str < end) *str = '%';\n ++ str;\n continue;\n\n /* integer number formats - set up the flags and \"break\" */\n case 'o':\n base = 8;\n break;\n\n case 'X':\n flags |= LARGE;\n case 'x':\n base = 16;\n break;\n\n case 'd':\n case 'i':\n flags |= SIGN;\n case 'u':\n break;\n\n default:\n if (str < end) *str = '%';\n ++ str;\n\n if (*fmt)\n {\n if (str < end) *str = *fmt;\n ++ str;\n }\n else\n {\n -- fmt;\n }\n continue;\n }\n\n#ifdef RT_PRINTF_LONGLONG\n if (qualifier == 'L') num = va_arg(args, long long);\n else if (qualifier == 'l')\n#else\n if (qualifier == 'l')\n#endif\n {\n num = va_arg(args, rt_uint32_t);\n if (flags & SIGN) num = (rt_int32_t)num;\n }\n else if (qualifier == 'h')\n {\n num = (rt_uint16_t)va_arg(args, rt_int32_t);\n if (flags & SIGN) num = (rt_int16_t)num;\n }\n else\n {\n num = va_arg(args, rt_uint32_t);\n if (flags & SIGN) num = (rt_int32_t)num;\n }\n#ifdef RT_PRINTF_PRECISION\n str = print_number(str, end, num, base, field_width, precision, flags);\n#else\n str = print_number(str, end, num, base, field_width, flags);\n#endif\n }\n\n if (size > 0)\n {\n if (str < end) *str = '\\0';\n else\n {\n end[-1] = '\\0';\n }\n }\n\n /* the trailing null byte doesn't count towards the total\n * ++str;\n */\n return str - buf;\n}\nRTM_EXPORT(rt_vsnprintf);\n\n/**\n * This function will fill a formatted string to buffer\n *\n * @param buf the buffer to save formatted string\n * @param size the size of buffer\n * @param fmt the format\n */\nrt_int32_t rt_snprintf(char *buf, rt_size_t size, const char *fmt, ...)\n{\n rt_int32_t n;\n va_list args;\n\n va_start(args, fmt);\n n = rt_vsnprintf(buf, size, fmt, args);\n va_end(args);\n\n return n;\n}\nRTM_EXPORT(rt_snprintf);\n\n/**\n * This function will fill a formatted string to buffer\n *\n * @param buf the buffer to save formatted string\n * @param arg_ptr the arg_ptr\n * @param format the format\n */\nrt_int32_t rt_vsprintf(char *buf, const char *format, va_list arg_ptr)\n{\n return rt_vsnprintf(buf, (rt_size_t) - 1, format, arg_ptr);\n}\nRTM_EXPORT(rt_vsprintf);\n\n/**\n * This function will fill a formatted string to buffer\n *\n * @param buf the buffer to save formatted string\n * @param format the format\n */\nrt_int32_t rt_sprintf(char *buf, const char *format, ...)\n{\n rt_int32_t n;\n va_list arg_ptr;\n\n va_start(arg_ptr, format);\n n = rt_vsprintf(buf, format, arg_ptr);\n va_end(arg_ptr);\n\n return n;\n}\nRTM_EXPORT(rt_sprintf);\n\n#ifdef RT_USING_CONSOLE\n\n#ifdef RT_USING_DEVICE\n/**\n * This function returns the device using in console.\n *\n * @return the device using in console or RT_NULL\n */\nrt_device_t rt_console_get_device(void)\n{\n return _console_device;\n}\nRTM_EXPORT(rt_console_get_device);\n\n/**\n * This function will set a device as console device.\n * After set a device to console, all output of rt_kprintf will be\n * redirected to this new device.\n *\n * @param name the name of new console device\n *\n * @return the old console device handler\n */\nrt_device_t rt_console_set_device(const char *name)\n{\n rt_device_t new_device, old_device;\n\n /* save old device */\n old_device = _console_device;\n\n /* find new console device */\n new_device = rt_device_find(name);\n if (new_device != RT_NULL)\n {\n if (_console_device != RT_NULL)\n {\n /* close old console device */\n rt_device_close(_console_device);\n }\n\n /* set new console device */\n rt_device_open(new_device, RT_DEVICE_OFLAG_RDWR | RT_DEVICE_FLAG_STREAM);\n _console_device = new_device;\n }\n\n return old_device;\n}\nRTM_EXPORT(rt_console_set_device);\n#endif\n\nRT_WEAK void rt_hw_console_output(const char *str)\n{\n /* empty console output */\n}\nRTM_EXPORT(rt_hw_console_output);\n\n/**\n * This function will put string to the console.\n *\n * @param str the string output to the console.\n */\nvoid rt_kputs(const char *str)\n{\n if (!str) return;\n\n#ifdef RT_USING_DEVICE\n if (_console_device == RT_NULL)\n {\n rt_hw_console_output(str);\n }\n else\n {\n rt_uint16_t old_flag = _console_device->open_flag;\n\n _console_device->open_flag |= RT_DEVICE_FLAG_STREAM;\n rt_device_write(_console_device, 0, str, rt_strlen(str));\n _console_device->open_flag = old_flag;\n }\n#else\n rt_hw_console_output(str);\n#endif\n}\n\n/**\n * This function will print a formatted string on system console\n *\n * @param fmt the format\n */\nvoid rt_kprintf(const char *fmt, ...)\n{\n va_list args;\n rt_size_t length;\n static char rt_log_buf[RT_CONSOLEBUF_SIZE];\n\n va_start(args, fmt);\n /* the return value of vsnprintf is the number of bytes that would be\n * written to buffer had if the size of the buffer been sufficiently\n * large excluding the terminating null byte. If the output string\n * would be larger than the rt_log_buf, we have to adjust the output\n * length. */\n length = rt_vsnprintf(rt_log_buf, sizeof(rt_log_buf) - 1, fmt, args);\n if (length > RT_CONSOLEBUF_SIZE - 1)\n length = RT_CONSOLEBUF_SIZE - 1;\n#ifdef RT_USING_DEVICE\n if (_console_device == RT_NULL)\n {\n rt_hw_console_output(rt_log_buf);\n }\n else\n {\n rt_uint16_t old_flag = _console_device->open_flag;\n\n _console_device->open_flag |= RT_DEVICE_FLAG_STREAM;\n rt_device_write(_console_device, 0, rt_log_buf, length);\n _console_device->open_flag = old_flag;\n }\n#else\n rt_hw_console_output(rt_log_buf);\n#endif\n va_end(args);\n}\nRTM_EXPORT(rt_kprintf);\n#endif\n\n#ifdef RT_USING_HEAP\n/**\n * This function allocates a memory block, which address is aligned to the\n * specified alignment size.\n *\n * @param size the allocated memory block size\n * @param align the alignment size\n *\n * @return the allocated memory block on successful, otherwise returns RT_NULL\n */\nvoid *rt_malloc_align(rt_size_t size, rt_size_t align)\n{\n void *ptr;\n void *align_ptr;\n int uintptr_size;\n rt_size_t align_size;\n\n /* sizeof pointer */\n uintptr_size = sizeof(void*);\n uintptr_size -= 1;\n\n /* align the alignment size to uintptr size byte */\n align = ((align + uintptr_size) & ~uintptr_size);\n\n /* get total aligned size */\n align_size = ((size + uintptr_size) & ~uintptr_size) + align;\n /* allocate memory block from heap */\n ptr = rt_malloc(align_size);\n if (ptr != RT_NULL)\n {\n /* the allocated memory block is aligned */\n if (((rt_ubase_t)ptr & (align - 1)) == 0)\n {\n align_ptr = (void *)((rt_ubase_t)ptr + align);\n }\n else\n {\n align_ptr = (void *)(((rt_ubase_t)ptr + (align - 1)) & ~(align - 1));\n }\n\n /* set the pointer before alignment pointer to the real pointer */\n *((rt_ubase_t *)((rt_ubase_t)align_ptr - sizeof(void *))) = (rt_ubase_t)ptr;\n\n ptr = align_ptr;\n }\n\n return ptr;\n}\nRTM_EXPORT(rt_malloc_align);\n\n/**\n * This function release the memory block, which is allocated by\n * rt_malloc_align function and address is aligned.\n *\n * @param ptr the memory block pointer\n */\nvoid rt_free_align(void *ptr)\n{\n void *real_ptr;\n\n real_ptr = (void *) * (rt_ubase_t *)((rt_ubase_t)ptr - sizeof(void *));\n rt_free(real_ptr);\n}\nRTM_EXPORT(rt_free_align);\n#endif\n\n#ifndef RT_USING_CPU_FFS\nconst rt_uint8_t __lowest_bit_bitmap[] =\n{\n /* 00 */ 0, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,\n /* 10 */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,\n /* 20 */ 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,\n /* 30 */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,\n /* 40 */ 6, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,\n /* 50 */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,\n /* 60 */ 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,\n /* 70 */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,\n /* 80 */ 7, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,\n /* 90 */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,\n /* A0 */ 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,\n /* B0 */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,\n /* C0 */ 6, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,\n /* D0 */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,\n /* E0 */ 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,\n /* F0 */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0\n};\n\n/**\n * This function finds the first bit set (beginning with the least significant bit)\n * in value and return the index of that bit.\n *\n * Bits are numbered starting at 1 (the least significant bit). A return value of\n * zero from any of these functions means that the argument was zero.\n *\n * @return return the index of the first bit set. If value is 0, then this function\n * shall return 0.\n */\nint __rt_ffs(int value)\n{\n if (value == 0) return 0;\n\n if (value & 0xff)\n return __lowest_bit_bitmap[value & 0xff] + 1;\n\n if (value & 0xff00)\n return __lowest_bit_bitmap[(value & 0xff00) >> 8] + 9;\n\n if (value & 0xff0000)\n return __lowest_bit_bitmap[(value & 0xff0000) >> 16] + 17;\n\n return __lowest_bit_bitmap[(value & 0xff000000) >> 24] + 25;\n}\n#endif\n\n#ifdef RT_DEBUG\n/* RT_ASSERT(EX)'s hook */\n\nvoid (*rt_assert_hook)(const char *ex, const char *func, rt_size_t line);\n\n/**\n * This function will set a hook function to RT_ASSERT(EX). It will run when the expression is false.\n *\n * @param hook the hook function\n */\nvoid rt_assert_set_hook(void (*hook)(const char *ex, const char *func, rt_size_t line))\n{\n rt_assert_hook = hook;\n}\n\n/**\n * The RT_ASSERT function.\n *\n * @param ex the assertion condition string\n * @param func the function name when assertion.\n * @param line the file line number when assertion.\n */\nvoid rt_assert_handler(const char *ex_string, const char *func, rt_size_t line)\n{\n volatile char dummy = 0;\n\n if (rt_assert_hook == RT_NULL)\n {\n#ifdef RT_USING_MODULE\n if (dlmodule_self())\n {\n /* close assertion module */\n dlmodule_exit(-1);\n }\n else\n#endif\n {\n rt_kprintf(\"(%s) assertion failed at function:%s, line number:%d \\n\", ex_string, func, line);\n while (dummy == 0);\n }\n }\n else\n {\n rt_assert_hook(ex_string, func, line);\n }\n}\nRTM_EXPORT(rt_assert_handler);\n#endif /* RT_DEBUG */\n\n#if !defined (RT_USING_NEWLIB) && defined (RT_USING_MINILIBC) && defined (__GNUC__)\n#include <sys/types.h>\nvoid *memcpy(void *dest, const void *src, size_t n) __attribute__((weak, alias(\"rt_memcpy\")));\nvoid *memset(void *s, int c, size_t n) __attribute__((weak, alias(\"rt_memset\")));\nvoid *memmove(void *dest, const void *src, size_t n) __attribute__((weak, alias(\"rt_memmove\")));\nint memcmp(const void *s1, const void *s2, size_t n) __attribute__((weak, alias(\"rt_memcmp\")));\n\nsize_t strlen(const char *s) __attribute__((weak, alias(\"rt_strlen\")));\nchar *strstr(const char *s1, const char *s2) __attribute__((weak, alias(\"rt_strstr\")));\nint strcasecmp(const char *a, const char *b) __attribute__((weak, alias(\"rt_strcasecmp\")));\nchar *strncpy(char *dest, const char *src, size_t n) __attribute__((weak, alias(\"rt_strncpy\")));\nint strncmp(const char *cs, const char *ct, size_t count) __attribute__((weak, alias(\"rt_strncmp\")));\n#ifdef RT_USING_HEAP\nchar *strdup(const char *s) __attribute__((weak, alias(\"rt_strdup\")));\n#endif\n\nint sprintf(char *buf, const char *format, ...) __attribute__((weak, alias(\"rt_sprintf\")));\nint snprintf(char *buf, rt_size_t size, const char *fmt, ...) __attribute__((weak, alias(\"rt_snprintf\")));\nint vsprintf(char *buf, const char *format, va_list arg_ptr) __attribute__((weak, alias(\"rt_vsprintf\")));\n\n#endif\n\n/**@}*/\n", "src/object.c": "/*\n * Copyright (c) 2006-2018, RT-Thread Development Team\n *\n * SPDX-License-Identifier: Apache-2.0\n *\n * Change Logs:\n * Date Author Notes\n * 2006-03-14 Bernard the first version\n * 2006-04-21 Bernard change the scheduler lock to interrupt lock\n * 2006-05-18 Bernard fix the object init bug\n * 2006-08-03 Bernard add hook support\n * 2007-01-28 Bernard rename RT_OBJECT_Class_Static to RT_Object_Class_Static\n * 2010-10-26 yi.qiu add module support in rt_object_allocate and rt_object_free\n * 2017-12-10 Bernard Add object_info enum.\n * 2018-01-25 Bernard Fix the object find issue when enable MODULE.\n */\n\n#include <rtthread.h>\n#include <rthw.h>\n\n#ifdef RT_USING_MODULE\n#include <dlmodule.h>\n#endif\n\n/*\n * define object_info for the number of rt_object_container items.\n */\nenum rt_object_info_type\n{\n RT_Object_Info_Thread = 0, /**< The object is a thread. */\n#ifdef RT_USING_SEMAPHORE\n RT_Object_Info_Semaphore, /**< The object is a semaphore. */\n#endif\n#ifdef RT_USING_MUTEX\n RT_Object_Info_Mutex, /**< The object is a mutex. */\n#endif\n#ifdef RT_USING_EVENT\n RT_Object_Info_Event, /**< The object is a event. */\n#endif\n#ifdef RT_USING_MAILBOX\n RT_Object_Info_MailBox, /**< The object is a mail box. */\n#endif\n#ifdef RT_USING_MESSAGEQUEUE\n RT_Object_Info_MessageQueue, /**< The object is a message queue. */\n#endif\n#ifdef RT_USING_MEMHEAP\n RT_Object_Info_MemHeap, /**< The object is a memory heap */\n#endif\n#ifdef RT_USING_MEMPOOL\n RT_Object_Info_MemPool, /**< The object is a memory pool. */\n#endif\n#ifdef RT_USING_DEVICE\n RT_Object_Info_Device, /**< The object is a device */\n#endif\n RT_Object_Info_Timer, /**< The object is a timer. */\n#ifdef RT_USING_MODULE\n RT_Object_Info_Module, /**< The object is a module. */\n#endif\n RT_Object_Info_Unknown, /**< The object is unknown. */\n};\n\n#define _OBJ_CONTAINER_LIST_INIT(c) \\\n {&(rt_object_container[c].object_list), &(rt_object_container[c].object_list)}\nstatic struct rt_object_information rt_object_container[RT_Object_Info_Unknown] =\n{\n /* initialize object container - thread */\n {RT_Object_Class_Thread, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_Thread), sizeof(struct rt_thread)},\n#ifdef RT_USING_SEMAPHORE\n /* initialize object container - semaphore */\n {RT_Object_Class_Semaphore, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_Semaphore), sizeof(struct rt_semaphore)},\n#endif\n#ifdef RT_USING_MUTEX\n /* initialize object container - mutex */\n {RT_Object_Class_Mutex, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_Mutex), sizeof(struct rt_mutex)},\n#endif\n#ifdef RT_USING_EVENT\n /* initialize object container - event */\n {RT_Object_Class_Event, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_Event), sizeof(struct rt_event)},\n#endif\n#ifdef RT_USING_MAILBOX\n /* initialize object container - mailbox */\n {RT_Object_Class_MailBox, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_MailBox), sizeof(struct rt_mailbox)},\n#endif\n#ifdef RT_USING_MESSAGEQUEUE\n /* initialize object container - message queue */\n {RT_Object_Class_MessageQueue, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_MessageQueue), sizeof(struct rt_messagequeue)},\n#endif\n#ifdef RT_USING_MEMHEAP\n /* initialize object container - memory heap */\n {RT_Object_Class_MemHeap, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_MemHeap), sizeof(struct rt_memheap)},\n#endif\n#ifdef RT_USING_MEMPOOL\n /* initialize object container - memory pool */\n {RT_Object_Class_MemPool, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_MemPool), sizeof(struct rt_mempool)},\n#endif\n#ifdef RT_USING_DEVICE\n /* initialize object container - device */\n {RT_Object_Class_Device, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_Device), sizeof(struct rt_device)},\n#endif\n /* initialize object container - timer */\n {RT_Object_Class_Timer, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_Timer), sizeof(struct rt_timer)},\n#ifdef RT_USING_MODULE\n /* initialize object container - module */\n {RT_Object_Class_Module, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_Module), sizeof(struct rt_dlmodule)},\n#endif\n};\n\n#ifdef RT_USING_HOOK\nstatic void (*rt_object_attach_hook)(struct rt_object *object);\nstatic void (*rt_object_detach_hook)(struct rt_object *object);\nvoid (*rt_object_trytake_hook)(struct rt_object *object);\nvoid (*rt_object_take_hook)(struct rt_object *object);\nvoid (*rt_object_put_hook)(struct rt_object *object);\n\n/**\n * @addtogroup Hook\n */\n\n/**@{*/\n\n/**\n * This function will set a hook function, which will be invoked when object\n * attaches to kernel object system.\n *\n * @param hook the hook function\n */\nvoid rt_object_attach_sethook(void (*hook)(struct rt_object *object))\n{\n rt_object_attach_hook = hook;\n}\n\n/**\n * This function will set a hook function, which will be invoked when object\n * detaches from kernel object system.\n *\n * @param hook the hook function\n */\nvoid rt_object_detach_sethook(void (*hook)(struct rt_object *object))\n{\n rt_object_detach_hook = hook;\n}\n\n/**\n * This function will set a hook function, which will be invoked when object\n * is taken from kernel object system.\n *\n * The object is taken means:\n * semaphore - semaphore is taken by thread\n * mutex - mutex is taken by thread\n * event - event is received by thread\n * mailbox - mail is received by thread\n * message queue - message is received by thread\n *\n * @param hook the hook function\n */\nvoid rt_object_trytake_sethook(void (*hook)(struct rt_object *object))\n{\n rt_object_trytake_hook = hook;\n}\n\n/**\n * This function will set a hook function, which will be invoked when object\n * have been taken from kernel object system.\n *\n * The object have been taken means:\n * semaphore - semaphore have been taken by thread\n * mutex - mutex have been taken by thread\n * event - event have been received by thread\n * mailbox - mail have been received by thread\n * message queue - message have been received by thread\n * timer - timer is started\n *\n * @param hook the hook function\n */\nvoid rt_object_take_sethook(void (*hook)(struct rt_object *object))\n{\n rt_object_take_hook = hook;\n}\n\n/**\n * This function will set a hook function, which will be invoked when object\n * is put to kernel object system.\n *\n * @param hook the hook function\n */\nvoid rt_object_put_sethook(void (*hook)(struct rt_object *object))\n{\n rt_object_put_hook = hook;\n}\n\n/**@}*/\n#endif\n\n/**\n * @ingroup SystemInit\n *\n * This function will initialize system object management.\n *\n * @deprecated since 0.3.0, this function does not need to be invoked\n * in the system initialization.\n */\nvoid rt_system_object_init(void)\n{\n}\n\n/**\n * @addtogroup KernelObject\n */\n\n/**@{*/\n\n/**\n * This function will return the specified type of object information.\n *\n * @param type the type of object, which can be \n * RT_Object_Class_Thread/Semaphore/Mutex... etc\n *\n * @return the object type information or RT_NULL\n */\nstruct rt_object_information *\nrt_object_get_information(enum rt_object_class_type type)\n{\n int index;\n\n for (index = 0; index < RT_Object_Info_Unknown; index ++)\n if (rt_object_container[index].type == type) return &rt_object_container[index];\n\n return RT_NULL;\n}\nRTM_EXPORT(rt_object_get_information);\n\n/**\n * This function will return the length of object list in object container.\n *\n * @param type the type of object, which can be \n * RT_Object_Class_Thread/Semaphore/Mutex... etc\n * @return the length of object list\n */\nint rt_object_get_length(enum rt_object_class_type type)\n{\n int count = 0;\n rt_ubase_t level;\n struct rt_list_node *node = RT_NULL;\n struct rt_object_information *information = RT_NULL;\n\n information = rt_object_get_information((enum rt_object_class_type)type);\n if (information == RT_NULL) return 0;\n\n level = rt_hw_interrupt_disable();\n /* get the count of objects */\n rt_list_for_each(node, &(information->object_list))\n {\n count ++;\n }\n rt_hw_interrupt_enable(level);\n\n return count;\n}\nRTM_EXPORT(rt_object_get_length);\n\n/**\n * This function will copy the object pointer of the specified type, \n * with the maximum size specified by maxlen.\n *\n * @param type the type of object, which can be \n * RT_Object_Class_Thread/Semaphore/Mutex... etc\n * @param pointers the pointers will be saved to\n * @param maxlen the maximum number of pointers can be saved\n *\n * @return the copied number of object pointers\n */\nint rt_object_get_pointers(enum rt_object_class_type type, rt_object_t *pointers, int maxlen)\n{\n int index = 0;\n rt_ubase_t level;\n\n struct rt_object *object;\n struct rt_list_node *node = RT_NULL;\n struct rt_object_information *information = RT_NULL;\n\n if (maxlen <= 0) return 0;\n\n information = rt_object_get_information((enum rt_object_class_type)type);\n if (information == RT_NULL) return 0;\n\n level = rt_hw_interrupt_disable();\n /* retrieve pointer of object */\n rt_list_for_each(node, &(information->object_list))\n {\n object = rt_list_entry(node, struct rt_object, list);\n\n pointers[index] = object;\n index ++;\n\n if (index >= maxlen) break;\n }\n rt_hw_interrupt_enable(level);\n\n return index;\n}\nRTM_EXPORT(rt_object_get_pointers);\n\n/**\n * This function will initialize an object and add it to object system\n * management.\n *\n * @param object the specified object to be initialized.\n * @param type the object type.\n * @param name the object name. In system, the object's name must be unique.\n */\nvoid rt_object_init(struct rt_object *object,\n enum rt_object_class_type type,\n const char *name)\n{\n register rt_base_t temp;\n struct rt_list_node *node = RT_NULL;\n struct rt_object_information *information;\n#ifdef RT_USING_MODULE\n struct rt_dlmodule *module = dlmodule_self();\n#endif\n\n /* get object information */\n information = rt_object_get_information(type);\n RT_ASSERT(information != RT_NULL);\n\n /* check object type to avoid re-initialization */\n\n /* enter critical */\n rt_enter_critical();\n /* try to find object */\n for (node = information->object_list.next;\n node != &(information->object_list);\n node = node->next)\n {\n struct rt_object *obj;\n\n obj = rt_list_entry(node, struct rt_object, list);\n if (obj) /* skip warning when disable debug */\n {\n RT_ASSERT(obj != object);\n }\n }\n /* leave critical */\n rt_exit_critical();\n\n /* initialize object's parameters */\n /* set object type to static */\n object->type = type | RT_Object_Class_Static;\n /* copy name */\n rt_strncpy(object->name, name, RT_NAME_MAX);\n\n RT_OBJECT_HOOK_CALL(rt_object_attach_hook, (object));\n\n /* lock interrupt */\n temp = rt_hw_interrupt_disable();\n\n#ifdef RT_USING_MODULE\n if (module)\n {\n rt_list_insert_after(&(module->object_list), &(object->list));\n object->module_id = (void *)module;\n }\n else\n#endif\n {\n /* insert object into information object list */\n rt_list_insert_after(&(information->object_list), &(object->list));\n }\n\n /* unlock interrupt */\n rt_hw_interrupt_enable(temp);\n}\n\n/**\n * This function will detach a static object from object system,\n * and the memory of static object is not freed.\n *\n * @param object the specified object to be detached.\n */\nvoid rt_object_detach(rt_object_t object)\n{\n register rt_base_t temp;\n\n /* object check */\n RT_ASSERT(object != RT_NULL);\n\n RT_OBJECT_HOOK_CALL(rt_object_detach_hook, (object));\n\n /* reset object type */\n object->type = 0;\n\n /* lock interrupt */\n temp = rt_hw_interrupt_disable();\n\n /* remove from old list */\n rt_list_remove(&(object->list));\n\n /* unlock interrupt */\n rt_hw_interrupt_enable(temp);\n}\n\n#ifdef RT_USING_HEAP\n/**\n * This function will allocate an object from object system\n *\n * @param type the type of object\n * @param name the object name. In system, the object's name must be unique.\n *\n * @return object\n */\nrt_object_t rt_object_allocate(enum rt_object_class_type type, const char *name)\n{\n struct rt_object *object;\n register rt_base_t temp;\n struct rt_object_information *information;\n#ifdef RT_USING_MODULE\n struct rt_dlmodule *module = dlmodule_self();\n#endif\n\n RT_DEBUG_NOT_IN_INTERRUPT;\n\n /* get object information */\n information = rt_object_get_information(type);\n RT_ASSERT(information != RT_NULL);\n\n object = (struct rt_object *)RT_KERNEL_MALLOC(information->object_size);\n if (object == RT_NULL)\n {\n /* no memory can be allocated */\n return RT_NULL;\n }\n\n /* clean memory data of object */\n rt_memset(object, 0x0, information->object_size);\n\n /* initialize object's parameters */\n\n /* set object type */\n object->type = type;\n\n /* set object flag */\n object->flag = 0;\n\n /* copy name */\n rt_strncpy(object->name, name, RT_NAME_MAX);\n\n RT_OBJECT_HOOK_CALL(rt_object_attach_hook, (object));\n\n /* lock interrupt */\n temp = rt_hw_interrupt_disable();\n\n#ifdef RT_USING_MODULE\n if (module)\n {\n rt_list_insert_after(&(module->object_list), &(object->list));\n object->module_id = (void *)module;\n }\n else\n#endif\n {\n /* insert object into information object list */\n rt_list_insert_after(&(information->object_list), &(object->list));\n }\n\n /* unlock interrupt */\n rt_hw_interrupt_enable(temp);\n\n /* return object */\n return object;\n}\n\n/**\n * This function will delete an object and release object memory.\n *\n * @param object the specified object to be deleted.\n */\nvoid rt_object_delete(rt_object_t object)\n{\n register rt_base_t temp;\n\n /* object check */\n RT_ASSERT(object != RT_NULL);\n RT_ASSERT(!(object->type & RT_Object_Class_Static));\n\n RT_OBJECT_HOOK_CALL(rt_object_detach_hook, (object));\n\n /* reset object type */\n object->type = 0;\n\n /* lock interrupt */\n temp = rt_hw_interrupt_disable();\n\n /* remove from old list */\n rt_list_remove(&(object->list));\n\n /* unlock interrupt */\n rt_hw_interrupt_enable(temp);\n\n /* free the memory of object */\n RT_KERNEL_FREE(object);\n}\n#endif\n\n/**\n * This function will judge the object is system object or not.\n * Normally, the system object is a static object and the type\n * of object set to RT_Object_Class_Static.\n *\n * @param object the specified object to be judged.\n *\n * @return RT_TRUE if a system object, RT_FALSE for others.\n */\nrt_bool_t rt_object_is_systemobject(rt_object_t object)\n{\n /* object check */\n RT_ASSERT(object != RT_NULL);\n\n if (object->type & RT_Object_Class_Static)\n return RT_TRUE;\n\n return RT_FALSE;\n}\n\n/**\n * This function will return the type of object without\n * RT_Object_Class_Static flag.\n *\n * @param object the specified object to be get type.\n *\n * @return the type of object.\n */\nrt_uint8_t rt_object_get_type(rt_object_t object)\n{\n /* object check */\n RT_ASSERT(object != RT_NULL);\n\n return object->type & ~RT_Object_Class_Static;\n}\n\n/**\n * This function will find specified name object from object\n * container.\n *\n * @param name the specified name of object.\n * @param type the type of object\n *\n * @return the found object or RT_NULL if there is no this object\n * in object container.\n *\n * @note this function shall not be invoked in interrupt status.\n */\nrt_object_t rt_object_find(const char *name, rt_uint8_t type)\n{\n struct rt_object *object = RT_NULL;\n struct rt_list_node *node = RT_NULL;\n struct rt_object_information *information = RT_NULL;\n\n information = rt_object_get_information((enum rt_object_class_type)type);\n\n /* parameter check */\n if ((name == RT_NULL) || (information == RT_NULL)) return RT_NULL;\n\n /* which is invoke in interrupt status */\n RT_DEBUG_NOT_IN_INTERRUPT;\n\n /* enter critical */\n rt_enter_critical();\n\n /* try to find object */\n rt_list_for_each(node, &(information->object_list))\n {\n object = rt_list_entry(node, struct rt_object, list);\n if (rt_strncmp(object->name, name, RT_NAME_MAX) == 0)\n {\n /* leave critical */\n rt_exit_critical();\n\n return object;\n }\n }\n\n /* leave critical */\n rt_exit_critical();\n\n return RT_NULL;\n}\n\n/**@}*/\n", "src/scheduler.c": "/*\n * Copyright (c) 2006-2018, RT-Thread Development Team\n *\n * SPDX-License-Identifier: Apache-2.0\n *\n * Change Logs:\n * Date Author Notes\n * 2006-03-17 Bernard the first version\n * 2006-04-28 Bernard fix the scheduler algorthm\n * 2006-04-30 Bernard add SCHEDULER_DEBUG\n * 2006-05-27 Bernard fix the scheduler algorthm for same priority\n * thread schedule\n * 2006-06-04 Bernard rewrite the scheduler algorithm\n * 2006-08-03 Bernard add hook support\n * 2006-09-05 Bernard add 32 priority level support\n * 2006-09-24 Bernard add rt_system_scheduler_start function\n * 2009-09-16 Bernard fix _rt_scheduler_stack_check\n * 2010-04-11 yi.qiu add module feature\n * 2010-07-13 Bernard fix the maximal number of rt_scheduler_lock_nest\n * issue found by kuronca\n * 2010-12-13 Bernard add defunct list initialization even if not use heap.\n * 2011-05-10 Bernard clean scheduler debug log.\n * 2013-12-21 Grissiom add rt_critical_level\n * 2018-11-22 Jesven remove the current task from ready queue\n * add per cpu ready queue\n * add _get_highest_priority_thread to find highest priority task\n * rt_schedule_insert_thread won't insert current task to ready queue\n * in smp version, rt_hw_context_switch_interrupt maybe switch to\n * new task directly\n *\n */\n\n#include <rtthread.h>\n#include <rthw.h>\n\nrt_list_t rt_thread_priority_table[RT_THREAD_PRIORITY_MAX];\nrt_uint32_t rt_thread_ready_priority_group;\n#if RT_THREAD_PRIORITY_MAX > 32\n/* Maximum priority level, 256 */\nrt_uint8_t rt_thread_ready_table[32];\n#endif\n\n#ifndef RT_USING_SMP\nextern volatile rt_uint8_t rt_interrupt_nest;\nstatic rt_int16_t rt_scheduler_lock_nest;\nstruct rt_thread *rt_current_thread;\nrt_uint8_t rt_current_priority;\n#endif /*RT_USING_SMP*/\n\nrt_list_t rt_thread_defunct;\n\n#ifdef RT_USING_HOOK\nstatic void (*rt_scheduler_hook)(struct rt_thread *from, struct rt_thread *to);\n\n/**\n * @addtogroup Hook\n */\n\n/**@{*/\n\n/**\n * This function will set a hook function, which will be invoked when thread\n * switch happens.\n *\n * @param hook the hook function\n */\nvoid\nrt_scheduler_sethook(void (*hook)(struct rt_thread *from, struct rt_thread *to))\n{\n rt_scheduler_hook = hook;\n}\n\n/**@}*/\n#endif\n\n#ifdef RT_USING_OVERFLOW_CHECK\nstatic void _rt_scheduler_stack_check(struct rt_thread *thread)\n{\n RT_ASSERT(thread != RT_NULL);\n\n#if defined(ARCH_CPU_STACK_GROWS_UPWARD)\n if (*((rt_uint8_t *)((rt_ubase_t)thread->stack_addr + thread->stack_size - 1)) != '#' ||\n#else\n if (*((rt_uint8_t *)thread->stack_addr) != '#' ||\n#endif\n (rt_ubase_t)thread->sp <= (rt_ubase_t)thread->stack_addr ||\n (rt_ubase_t)thread->sp >\n (rt_ubase_t)thread->stack_addr + (rt_ubase_t)thread->stack_size)\n {\n rt_ubase_t level;\n\n rt_kprintf(\"thread:%s stack overflow\\n\", thread->name);\n#ifdef RT_USING_FINSH\n {\n extern long list_thread(void);\n list_thread();\n }\n#endif\n level = rt_hw_interrupt_disable();\n while (level);\n }\n#if defined(ARCH_CPU_STACK_GROWS_UPWARD)\n else if ((rt_ubase_t)thread->sp > ((rt_ubase_t)thread->stack_addr + thread->stack_size))\n {\n rt_kprintf(\"warning: %s stack is close to the top of stack address.\\n\",\n thread->name);\n }\n#else\n else if ((rt_ubase_t)thread->sp <= ((rt_ubase_t)thread->stack_addr + 32))\n {\n rt_kprintf(\"warning: %s stack is close to end of stack address.\\n\",\n thread->name);\n }\n#endif\n}\n#endif\n\n/*\n * get the highest priority thread in ready queue\n */\n#ifdef RT_USING_SMP\nstatic struct rt_thread* _get_highest_priority_thread(rt_ubase_t *highest_prio)\n{\n register struct rt_thread *highest_priority_thread;\n register rt_ubase_t highest_ready_priority, local_highest_ready_priority;\n struct rt_cpu* pcpu = rt_cpu_self();\n#if RT_THREAD_PRIORITY_MAX > 32\n register rt_ubase_t number;\n\n number = __rt_ffs(rt_thread_ready_priority_group) - 1;\n highest_ready_priority = (number << 3) + __rt_ffs(rt_thread_ready_table[number]) - 1;\n number = __rt_ffs(pcpu->priority_group) - 1;\n local_highest_ready_priority = (number << 3) + __rt_ffs(pcpu->ready_table[number]) - 1;\n#else\n highest_ready_priority = __rt_ffs(rt_thread_ready_priority_group) - 1;\n local_highest_ready_priority = __rt_ffs(pcpu->priority_group) - 1;\n#endif\n\n /* get highest ready priority thread */\n if (highest_ready_priority < local_highest_ready_priority)\n {\n *highest_prio = highest_ready_priority;\n highest_priority_thread = rt_list_entry(rt_thread_priority_table[highest_ready_priority].next,\n struct rt_thread,\n tlist);\n }\n else\n {\n *highest_prio = local_highest_ready_priority;\n highest_priority_thread = rt_list_entry(pcpu->priority_table[local_highest_ready_priority].next,\n struct rt_thread,\n tlist);\n }\n\n return highest_priority_thread;\n}\n#else\nstatic struct rt_thread* _get_highest_priority_thread(rt_ubase_t *highest_prio)\n{\n register struct rt_thread *highest_priority_thread;\n register rt_ubase_t highest_ready_priority;\n\n#if RT_THREAD_PRIORITY_MAX > 32\n register rt_ubase_t number;\n\n number = __rt_ffs(rt_thread_ready_priority_group) - 1;\n highest_ready_priority = (number << 3) + __rt_ffs(rt_thread_ready_table[number]) - 1;\n#else\n highest_ready_priority = __rt_ffs(rt_thread_ready_priority_group) - 1;\n#endif\n\n /* get highest ready priority thread */\n highest_priority_thread = rt_list_entry(rt_thread_priority_table[highest_ready_priority].next,\n struct rt_thread,\n tlist);\n\n *highest_prio = highest_ready_priority;\n\n return highest_priority_thread;\n}\n#endif\n\n/**\n * @ingroup SystemInit\n * This function will initialize the system scheduler\n */\nvoid rt_system_scheduler_init(void)\n{\n#ifdef RT_USING_SMP\n int cpu;\n#endif /*RT_USING_SMP*/\n register rt_base_t offset;\n\n#ifndef RT_USING_SMP\n rt_scheduler_lock_nest = 0;\n#endif /*RT_USING_SMP*/\n\n RT_DEBUG_LOG(RT_DEBUG_SCHEDULER, (\"start scheduler: max priority 0x%02x\\n\",\n RT_THREAD_PRIORITY_MAX));\n\n for (offset = 0; offset < RT_THREAD_PRIORITY_MAX; offset ++)\n {\n rt_list_init(&rt_thread_priority_table[offset]);\n }\n#ifdef RT_USING_SMP\n for (cpu = 0; cpu < RT_CPUS_NR; cpu++)\n {\n struct rt_cpu *pcpu = rt_cpu_index(cpu);\n for (offset = 0; offset < RT_THREAD_PRIORITY_MAX; offset ++)\n {\n rt_list_init(&pcpu->priority_table[offset]);\n }\n\n pcpu->irq_switch_flag = 0;\n pcpu->current_priority = RT_THREAD_PRIORITY_MAX - 1;\n pcpu->current_thread = RT_NULL;\n pcpu->priority_group = 0;\n\n#if RT_THREAD_PRIORITY_MAX > 32\n rt_memset(pcpu->ready_table, 0, sizeof(pcpu->ready_table));\n#endif\n }\n#endif /*RT_USING_SMP*/\n\n /* initialize ready priority group */\n rt_thread_ready_priority_group = 0;\n\n#if RT_THREAD_PRIORITY_MAX > 32\n /* initialize ready table */\n rt_memset(rt_thread_ready_table, 0, sizeof(rt_thread_ready_table));\n#endif\n\n /* initialize thread defunct */\n rt_list_init(&rt_thread_defunct);\n}\n\n/**\n * @ingroup SystemInit\n * This function will startup scheduler. It will select one thread\n * with the highest priority level, then switch to it.\n */\nvoid rt_system_scheduler_start(void)\n{\n register struct rt_thread *to_thread;\n rt_ubase_t highest_ready_priority;\n\n to_thread = _get_highest_priority_thread(&highest_ready_priority);\n\n#ifdef RT_USING_SMP\n to_thread->oncpu = rt_hw_cpu_id();\n#else\n rt_current_thread = to_thread;\n#endif /*RT_USING_SMP*/\n\n rt_schedule_remove_thread(to_thread);\n to_thread->stat = RT_THREAD_RUNNING;\n\n /* switch to new thread */\n#ifdef RT_USING_SMP\n rt_hw_context_switch_to((rt_ubase_t)&to_thread->sp, to_thread);\n#else\n rt_hw_context_switch_to((rt_ubase_t)&to_thread->sp);\n#endif /*RT_USING_SMP*/\n\n /* never come back */\n}\n\n/**\n * @addtogroup Thread\n */\n\n/**@{*/\n\n\n#ifdef RT_USING_SMP\n/**\n * This function will handle IPI interrupt and do a scheduling in system;\n * \n * @param vector, the number of IPI interrupt for system scheduling\n * @param param, use RT_NULL\n * \n * NOTE: this function should be invoke or register as ISR in BSP.\n */\nvoid rt_scheduler_ipi_handler(int vector, void *param)\n{\n rt_schedule();\n}\n\n/**\n * This function will perform one scheduling. It will select one thread\n * with the highest priority level in global ready queue or local ready queue, \n * then switch to it.\n */\nvoid rt_schedule(void)\n{\n rt_base_t level;\n struct rt_thread *to_thread;\n struct rt_thread *current_thread;\n struct rt_cpu *pcpu;\n int cpu_id;\n\n /* disable interrupt */\n level = rt_hw_interrupt_disable();\n\n cpu_id = rt_hw_cpu_id();\n pcpu = rt_cpu_index(cpu_id);\n current_thread = pcpu->current_thread;\n\n /* whether do switch in interrupt */\n if (pcpu->irq_nest)\n {\n pcpu->irq_switch_flag = 1;\n rt_hw_interrupt_enable(level);\n goto __exit;\n }\n\n#ifdef RT_USING_SIGNALS\n if ((current_thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_SUSPEND)\n {\n /* if current_thread signal is in pending */\n\n if ((current_thread->stat & RT_THREAD_STAT_SIGNAL_MASK) & RT_THREAD_STAT_SIGNAL_PENDING)\n {\n rt_thread_resume(current_thread);\n }\n }\n#endif\n\n if (current_thread->scheduler_lock_nest == 1) /* whether lock scheduler */\n {\n rt_ubase_t highest_ready_priority;\n\n if (rt_thread_ready_priority_group != 0 || pcpu->priority_group != 0)\n {\n to_thread = _get_highest_priority_thread(&highest_ready_priority);\n current_thread->oncpu = RT_CPU_DETACHED;\n if ((current_thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_RUNNING)\n {\n if (current_thread->current_priority < highest_ready_priority)\n {\n to_thread = current_thread;\n }\n else if (current_thread->current_priority == highest_ready_priority && (current_thread->stat & RT_THREAD_STAT_YIELD_MASK) == 0)\n {\n to_thread = current_thread;\n }\n else\n {\n current_thread->stat &= ~RT_THREAD_STAT_YIELD_MASK;\n rt_schedule_insert_thread(current_thread);\n }\n }\n to_thread->oncpu = cpu_id;\n if (to_thread != current_thread)\n {\n /* if the destination thread is not the same as current thread */\n pcpu->current_priority = (rt_uint8_t)highest_ready_priority;\n\n RT_OBJECT_HOOK_CALL(rt_scheduler_hook, (current_thread, to_thread));\n\n rt_schedule_remove_thread(to_thread);\n to_thread->stat = RT_THREAD_RUNNING | (to_thread->stat & ~RT_THREAD_STAT_MASK);\n\n /* switch to new thread */\n RT_DEBUG_LOG(RT_DEBUG_SCHEDULER,\n (\"[%d]switch to priority#%d \"\n \"thread:%.*s(sp:0x%08x), \"\n \"from thread:%.*s(sp: 0x%08x)\\n\",\n pcpu->irq_nest, highest_ready_priority,\n RT_NAME_MAX, to_thread->name, to_thread->sp,\n RT_NAME_MAX, current_thread->name, current_thread->sp));\n\n#ifdef RT_USING_OVERFLOW_CHECK\n _rt_scheduler_stack_check(to_thread);\n#endif\n\n rt_hw_context_switch((rt_ubase_t)&current_thread->sp,\n (rt_ubase_t)&to_thread->sp, to_thread);\n }\n }\n }\n\n /* enable interrupt */\n rt_hw_interrupt_enable(level);\n\n#ifdef RT_USING_SIGNALS\n /* check stat of thread for signal */\n level = rt_hw_interrupt_disable();\n if (current_thread->stat & RT_THREAD_STAT_SIGNAL_PENDING)\n {\n extern void rt_thread_handle_sig(rt_bool_t clean_state);\n\n current_thread->stat &= ~RT_THREAD_STAT_SIGNAL_PENDING;\n\n rt_hw_interrupt_enable(level);\n\n /* check signal status */\n rt_thread_handle_sig(RT_TRUE);\n }\n else\n {\n rt_hw_interrupt_enable(level);\n }\n#endif\n\n__exit:\n return ;\n}\n#else\n/**\n * This function will perform one schedule. It will select one thread\n * with the highest priority level, then switch to it.\n */\nvoid rt_schedule(void)\n{\n rt_base_t level;\n struct rt_thread *to_thread;\n struct rt_thread *from_thread;\n\n /* disable interrupt */\n level = rt_hw_interrupt_disable();\n\n /* check the scheduler is enabled or not */\n if (rt_scheduler_lock_nest == 0)\n {\n rt_ubase_t highest_ready_priority;\n\n if (rt_thread_ready_priority_group != 0)\n {\n /* need_insert_from_thread: need to insert from_thread to ready queue */\n int need_insert_from_thread = 0;\n\n to_thread = _get_highest_priority_thread(&highest_ready_priority);\n\n if ((rt_current_thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_RUNNING)\n {\n if (rt_current_thread->current_priority < highest_ready_priority)\n {\n to_thread = rt_current_thread;\n }\n else if (rt_current_thread->current_priority == highest_ready_priority && (rt_current_thread->stat & RT_THREAD_STAT_YIELD_MASK) == 0)\n {\n to_thread = rt_current_thread;\n }\n else\n {\n rt_current_thread->stat &= ~RT_THREAD_STAT_YIELD_MASK;\n need_insert_from_thread = 1;\n }\n }\n\n if (to_thread != rt_current_thread)\n {\n /* if the destination thread is not the same as current thread */\n rt_current_priority = (rt_uint8_t)highest_ready_priority;\n from_thread = rt_current_thread;\n rt_current_thread = to_thread;\n\n RT_OBJECT_HOOK_CALL(rt_scheduler_hook, (from_thread, to_thread));\n\n if (need_insert_from_thread)\n {\n rt_schedule_insert_thread(from_thread);\n }\n\n rt_schedule_remove_thread(to_thread);\n to_thread->stat = RT_THREAD_RUNNING | (to_thread->stat & ~RT_THREAD_STAT_MASK);\n\n /* switch to new thread */\n RT_DEBUG_LOG(RT_DEBUG_SCHEDULER,\n (\"[%d]switch to priority#%d \"\n \"thread:%.*s(sp:0x%08x), \"\n \"from thread:%.*s(sp: 0x%08x)\\n\",\n rt_interrupt_nest, highest_ready_priority,\n RT_NAME_MAX, to_thread->name, to_thread->sp,\n RT_NAME_MAX, from_thread->name, from_thread->sp));\n\n#ifdef RT_USING_OVERFLOW_CHECK\n _rt_scheduler_stack_check(to_thread);\n#endif\n\n if (rt_interrupt_nest == 0)\n {\n extern void rt_thread_handle_sig(rt_bool_t clean_state);\n\n rt_hw_context_switch((rt_ubase_t)&from_thread->sp,\n (rt_ubase_t)&to_thread->sp);\n\n /* enable interrupt */\n rt_hw_interrupt_enable(level);\n\n#ifdef RT_USING_SIGNALS\n /* check stat of thread for signal */\n level = rt_hw_interrupt_disable();\n if (rt_current_thread->stat & RT_THREAD_STAT_SIGNAL_PENDING)\n {\n extern void rt_thread_handle_sig(rt_bool_t clean_state);\n\n rt_current_thread->stat &= ~RT_THREAD_STAT_SIGNAL_PENDING;\n\n rt_hw_interrupt_enable(level);\n\n /* check signal status */\n rt_thread_handle_sig(RT_TRUE);\n }\n else\n {\n rt_hw_interrupt_enable(level);\n }\n#endif\n goto __exit;\n }\n else\n {\n RT_DEBUG_LOG(RT_DEBUG_SCHEDULER, (\"switch in interrupt\\n\"));\n\n rt_hw_context_switch_interrupt((rt_ubase_t)&from_thread->sp,\n (rt_ubase_t)&to_thread->sp);\n }\n }\n else\n {\n rt_schedule_remove_thread(rt_current_thread);\n rt_current_thread->stat = RT_THREAD_RUNNING | (rt_current_thread->stat & ~RT_THREAD_STAT_MASK);\n }\n }\n }\n\n /* enable interrupt */\n rt_hw_interrupt_enable(level);\n\n__exit:\n return;\n}\n#endif /*RT_USING_SMP*/\n\n/**\n * This function checks if a scheduling is needed after IRQ context. If yes,\n * it will select one thread with the highest priority level, and then switch\n * to it.\n */\n#ifdef RT_USING_SMP\nvoid rt_scheduler_do_irq_switch(void *context)\n{\n int cpu_id;\n rt_base_t level;\n struct rt_cpu* pcpu;\n struct rt_thread *to_thread;\n struct rt_thread *current_thread;\n\n level = rt_hw_interrupt_disable();\n\n cpu_id = rt_hw_cpu_id();\n pcpu = rt_cpu_index(cpu_id);\n current_thread = pcpu->current_thread;\n\n#ifdef RT_USING_SIGNALS\n if ((current_thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_SUSPEND)\n {\n /* if current_thread signal is in pending */\n\n if ((current_thread->stat & RT_THREAD_STAT_SIGNAL_MASK) & RT_THREAD_STAT_SIGNAL_PENDING)\n {\n rt_thread_resume(current_thread);\n }\n }\n#endif\n\n if (pcpu->irq_switch_flag == 0)\n {\n rt_hw_interrupt_enable(level);\n return;\n }\n\n if (current_thread->scheduler_lock_nest == 1 && pcpu->irq_nest == 0)\n {\n rt_ubase_t highest_ready_priority;\n\n /* clear irq switch flag */\n pcpu->irq_switch_flag = 0;\n\n if (rt_thread_ready_priority_group != 0 || pcpu->priority_group != 0)\n {\n to_thread = _get_highest_priority_thread(&highest_ready_priority);\n current_thread->oncpu = RT_CPU_DETACHED;\n if ((current_thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_RUNNING)\n {\n if (current_thread->current_priority < highest_ready_priority)\n {\n to_thread = current_thread;\n }\n else if (current_thread->current_priority == highest_ready_priority && (current_thread->stat & RT_THREAD_STAT_YIELD_MASK) == 0)\n {\n to_thread = current_thread;\n }\n else\n {\n current_thread->stat &= ~RT_THREAD_STAT_YIELD_MASK;\n rt_schedule_insert_thread(current_thread);\n }\n }\n to_thread->oncpu = cpu_id;\n if (to_thread != current_thread)\n {\n /* if the destination thread is not the same as current thread */\n\n pcpu->current_priority = (rt_uint8_t)highest_ready_priority;\n\n RT_OBJECT_HOOK_CALL(rt_scheduler_hook, (current_thread, to_thread));\n\n rt_schedule_remove_thread(to_thread);\n to_thread->stat = RT_THREAD_RUNNING | (to_thread->stat & ~RT_THREAD_STAT_MASK);\n\n#ifdef RT_USING_OVERFLOW_CHECK\n _rt_scheduler_stack_check(to_thread);\n#endif\n RT_DEBUG_LOG(RT_DEBUG_SCHEDULER, (\"switch in interrupt\\n\"));\n\n current_thread->cpus_lock_nest--;\n current_thread->scheduler_lock_nest--;\n\n rt_hw_context_switch_interrupt(context, (rt_ubase_t)&current_thread->sp,\n (rt_ubase_t)&to_thread->sp, to_thread);\n }\n }\n }\n rt_hw_interrupt_enable(level);\n}\n#endif /*RT_USING_SMP*/\n\n/*\n * This function will insert a thread to system ready queue. The state of\n * thread will be set as READY and remove from suspend queue.\n *\n * @param thread the thread to be inserted\n * @note Please do not invoke this function in user application.\n */\n#ifdef RT_USING_SMP\nvoid rt_schedule_insert_thread(struct rt_thread *thread)\n{\n int cpu_id;\n int bind_cpu;\n rt_uint32_t cpu_mask;\n register rt_base_t level;\n\n RT_ASSERT(thread != RT_NULL);\n\n /* disable interrupt */\n level = rt_hw_interrupt_disable();\n\n /* it should be RUNNING thread */\n if (thread->oncpu != RT_CPU_DETACHED)\n {\n thread->stat = RT_THREAD_RUNNING | (thread->stat & ~RT_THREAD_STAT_MASK);\n goto __exit;\n }\n\n /* READY thread, insert to ready queue */\n thread->stat = RT_THREAD_READY | (thread->stat & ~RT_THREAD_STAT_MASK);\n\n cpu_id = rt_hw_cpu_id();\n bind_cpu = thread->bind_cpu ;\n\n /* insert thread to ready list */\n if (bind_cpu == RT_CPUS_NR)\n {\n#if RT_THREAD_PRIORITY_MAX > 32\n rt_thread_ready_table[thread->number] |= thread->high_mask;\n#endif\n rt_thread_ready_priority_group |= thread->number_mask;\n\n rt_list_insert_before(&(rt_thread_priority_table[thread->current_priority]),\n &(thread->tlist));\n cpu_mask = RT_CPU_MASK ^ (1 << cpu_id);\n rt_hw_ipi_send(RT_SCHEDULE_IPI, cpu_mask);\n }\n else\n {\n struct rt_cpu *pcpu = rt_cpu_index(bind_cpu);\n\n#if RT_THREAD_PRIORITY_MAX > 32\n pcpu->ready_table[thread->number] |= thread->high_mask;\n#endif\n pcpu->priority_group |= thread->number_mask;\n\n rt_list_insert_before(&(rt_cpu_index(bind_cpu)->priority_table[thread->current_priority]),\n &(thread->tlist));\n\n if (cpu_id != bind_cpu)\n {\n cpu_mask = 1 << bind_cpu;\n rt_hw_ipi_send(RT_SCHEDULE_IPI, cpu_mask);\n }\n }\n\n RT_DEBUG_LOG(RT_DEBUG_SCHEDULER, (\"insert thread[%.*s], the priority: %d\\n\",\n RT_NAME_MAX, thread->name, thread->current_priority));\n\n__exit:\n /* enable interrupt */\n rt_hw_interrupt_enable(level);\n}\n#else\nvoid rt_schedule_insert_thread(struct rt_thread *thread)\n{\n register rt_base_t temp;\n\n RT_ASSERT(thread != RT_NULL);\n\n /* disable interrupt */\n temp = rt_hw_interrupt_disable();\n\n /* it's current thread, it should be RUNNING thread */\n if (thread == rt_current_thread)\n {\n thread->stat = RT_THREAD_RUNNING | (thread->stat & ~RT_THREAD_STAT_MASK);\n goto __exit;\n }\n\n /* READY thread, insert to ready queue */\n thread->stat = RT_THREAD_READY | (thread->stat & ~RT_THREAD_STAT_MASK);\n /* insert thread to ready list */\n rt_list_insert_before(&(rt_thread_priority_table[thread->current_priority]),\n &(thread->tlist));\n\n RT_DEBUG_LOG(RT_DEBUG_SCHEDULER, (\"insert thread[%.*s], the priority: %d\\n\",\n RT_NAME_MAX, thread->name, thread->current_priority));\n\n /* set priority mask */\n#if RT_THREAD_PRIORITY_MAX > 32\n rt_thread_ready_table[thread->number] |= thread->high_mask;\n#endif\n rt_thread_ready_priority_group |= thread->number_mask;\n\n__exit:\n /* enable interrupt */\n rt_hw_interrupt_enable(temp);\n}\n#endif /*RT_USING_SMP*/\n\n/*\n * This function will remove a thread from system ready queue.\n *\n * @param thread the thread to be removed\n *\n * @note Please do not invoke this function in user application.\n */\n#ifdef RT_USING_SMP\nvoid rt_schedule_remove_thread(struct rt_thread *thread)\n{\n register rt_base_t level;\n\n RT_ASSERT(thread != RT_NULL);\n\n /* disable interrupt */\n level = rt_hw_interrupt_disable();\n\n RT_DEBUG_LOG(RT_DEBUG_SCHEDULER, (\"remove thread[%.*s], the priority: %d\\n\",\n RT_NAME_MAX, thread->name,\n thread->current_priority));\n\n /* remove thread from ready list */\n rt_list_remove(&(thread->tlist));\n if (thread->bind_cpu == RT_CPUS_NR)\n {\n if (rt_list_isempty(&(rt_thread_priority_table[thread->current_priority])))\n {\n#if RT_THREAD_PRIORITY_MAX > 32\n rt_thread_ready_table[thread->number] &= ~thread->high_mask;\n if (rt_thread_ready_table[thread->number] == 0)\n {\n rt_thread_ready_priority_group &= ~thread->number_mask;\n }\n#else\n rt_thread_ready_priority_group &= ~thread->number_mask;\n#endif\n }\n }\n else\n {\n struct rt_cpu *pcpu = rt_cpu_index(thread->bind_cpu);\n\n if (rt_list_isempty(&(pcpu->priority_table[thread->current_priority])))\n {\n#if RT_THREAD_PRIORITY_MAX > 32\n pcpu->ready_table[thread->number] &= ~thread->high_mask;\n if (pcpu->ready_table[thread->number] == 0)\n {\n pcpu->priority_group &= ~thread->number_mask;\n }\n#else\n pcpu->priority_group &= ~thread->number_mask;\n#endif\n }\n }\n\n /* enable interrupt */\n rt_hw_interrupt_enable(level);\n}\n#else\nvoid rt_schedule_remove_thread(struct rt_thread *thread)\n{\n register rt_base_t level;\n\n RT_ASSERT(thread != RT_NULL);\n\n /* disable interrupt */\n level = rt_hw_interrupt_disable();\n\n RT_DEBUG_LOG(RT_DEBUG_SCHEDULER, (\"remove thread[%.*s], the priority: %d\\n\",\n RT_NAME_MAX, thread->name,\n thread->current_priority));\n\n /* remove thread from ready list */\n rt_list_remove(&(thread->tlist));\n if (rt_list_isempty(&(rt_thread_priority_table[thread->current_priority])))\n {\n#if RT_THREAD_PRIORITY_MAX > 32\n rt_thread_ready_table[thread->number] &= ~thread->high_mask;\n if (rt_thread_ready_table[thread->number] == 0)\n {\n rt_thread_ready_priority_group &= ~thread->number_mask;\n }\n#else\n rt_thread_ready_priority_group &= ~thread->number_mask;\n#endif\n }\n\n /* enable interrupt */\n rt_hw_interrupt_enable(level);\n}\n#endif /*RT_USING_SMP*/\n\n/**\n * This function will lock the thread scheduler.\n */\n#ifdef RT_USING_SMP\nvoid rt_enter_critical(void)\n{\n register rt_base_t level;\n struct rt_thread *current_thread;\n\n /* disable interrupt */\n level = rt_hw_local_irq_disable();\n\n current_thread = rt_cpu_self()->current_thread;\n if (!current_thread)\n {\n rt_hw_local_irq_enable(level);\n return;\n }\n\n /*\n * the maximal number of nest is RT_UINT16_MAX, which is big\n * enough and does not check here\n */\n\n {\n register rt_uint16_t lock_nest = current_thread->cpus_lock_nest;\n current_thread->cpus_lock_nest++;\n if (lock_nest == 0)\n {\n current_thread->scheduler_lock_nest ++;\n rt_hw_spin_lock(&_cpus_lock);\n }\n }\n /* critical for local cpu */\n current_thread->critical_lock_nest ++;\n\n /* lock scheduler for local cpu */\n current_thread->scheduler_lock_nest ++;\n\n /* enable interrupt */\n rt_hw_local_irq_enable(level);\n}\n#else\nvoid rt_enter_critical(void)\n{\n register rt_base_t level;\n\n /* disable interrupt */\n level = rt_hw_interrupt_disable();\n\n /*\n * the maximal number of nest is RT_UINT16_MAX, which is big\n * enough and does not check here\n */\n rt_scheduler_lock_nest ++;\n\n /* enable interrupt */\n rt_hw_interrupt_enable(level);\n}\n#endif /*RT_USING_SMP*/\nRTM_EXPORT(rt_enter_critical);\n\n/**\n * This function will unlock the thread scheduler.\n */\n#ifdef RT_USING_SMP\nvoid rt_exit_critical(void)\n{\n register rt_base_t level;\n struct rt_thread *current_thread;\n\n /* disable interrupt */\n level = rt_hw_local_irq_disable();\n\n current_thread = rt_cpu_self()->current_thread;\n if (!current_thread)\n {\n rt_hw_local_irq_enable(level);\n return;\n }\n\n current_thread->scheduler_lock_nest --;\n\n current_thread->critical_lock_nest --;\n\n current_thread->cpus_lock_nest--;\n if (current_thread->cpus_lock_nest == 0)\n {\n current_thread->scheduler_lock_nest --;\n rt_hw_spin_unlock(&_cpus_lock);\n }\n\n if (current_thread->scheduler_lock_nest <= 0)\n {\n current_thread->scheduler_lock_nest = 0;\n /* enable interrupt */\n rt_hw_local_irq_enable(level);\n\n rt_schedule();\n }\n else\n {\n /* enable interrupt */\n rt_hw_local_irq_enable(level);\n }\n}\n#else\nvoid rt_exit_critical(void)\n{\n register rt_base_t level;\n\n /* disable interrupt */\n level = rt_hw_interrupt_disable();\n\n rt_scheduler_lock_nest --;\n if (rt_scheduler_lock_nest <= 0)\n {\n rt_scheduler_lock_nest = 0;\n /* enable interrupt */\n rt_hw_interrupt_enable(level);\n\n if (rt_current_thread)\n {\n /* if scheduler is started, do a schedule */\n rt_schedule();\n }\n }\n else\n {\n /* enable interrupt */\n rt_hw_interrupt_enable(level);\n }\n}\n#endif /*RT_USING_SMP*/\nRTM_EXPORT(rt_exit_critical);\n\n/**\n * Get the scheduler lock level\n *\n * @return the level of the scheduler lock. 0 means unlocked.\n */\nrt_uint16_t rt_critical_level(void)\n{\n#ifdef RT_USING_SMP\n struct rt_thread *current_thread = rt_cpu_self()->current_thread;\n\n return current_thread->critical_lock_nest;\n#else\n return rt_scheduler_lock_nest;\n#endif /*RT_USING_SMP*/\n}\nRTM_EXPORT(rt_critical_level);\n\n/**@}*/\n", "src/thread.c": "/*\n * Copyright (c) 2006-2018, RT-Thread Development Team\n *\n * SPDX-License-Identifier: Apache-2.0\n *\n * Change Logs:\n * Date Author Notes\n * 2006-03-28 Bernard first version\n * 2006-04-29 Bernard implement thread timer\n * 2006-04-30 Bernard added THREAD_DEBUG\n * 2006-05-27 Bernard fixed the rt_thread_yield bug\n * 2006-06-03 Bernard fixed the thread timer init bug\n * 2006-08-10 Bernard fixed the timer bug in thread_sleep\n * 2006-09-03 Bernard changed rt_timer_delete to rt_timer_detach\n * 2006-09-03 Bernard implement rt_thread_detach\n * 2008-02-16 Bernard fixed the rt_thread_timeout bug\n * 2010-03-21 Bernard change the errno of rt_thread_delay/sleep to\n * RT_EOK.\n * 2010-11-10 Bernard add cleanup callback function in thread exit.\n * 2011-09-01 Bernard fixed rt_thread_exit issue when the current\n * thread preempted, which reported by Jiaxing Lee.\n * 2011-09-08 Bernard fixed the scheduling issue in rt_thread_startup.\n * 2012-12-29 Bernard fixed compiling warning.\n * 2016-08-09 ArdaFu add thread suspend and resume hook.\n * 2017-04-10 armink fixed the rt_thread_delete and rt_thread_detach\n * bug when thread has not startup.\n * 2018-11-22 Jesven yield is same to rt_schedule\n * add support for tasks bound to cpu\n */\n\n#include <rthw.h>\n#include <rtthread.h>\n\nextern rt_list_t rt_thread_defunct;\n\n#ifdef RT_USING_HOOK\nstatic void (*rt_thread_suspend_hook)(rt_thread_t thread);\nstatic void (*rt_thread_resume_hook) (rt_thread_t thread);\nstatic void (*rt_thread_inited_hook) (rt_thread_t thread);\n\n/**\n * @ingroup Hook\n * This function sets a hook function when the system suspend a thread.\n *\n * @param hook the specified hook function\n *\n * @note the hook function must be simple and never be blocked or suspend.\n */\nvoid rt_thread_suspend_sethook(void (*hook)(rt_thread_t thread))\n{\n rt_thread_suspend_hook = hook;\n}\n\n/**\n * @ingroup Hook\n * This function sets a hook function when the system resume a thread.\n *\n * @param hook the specified hook function\n *\n * @note the hook function must be simple and never be blocked or suspend.\n */\nvoid rt_thread_resume_sethook(void (*hook)(rt_thread_t thread))\n{\n rt_thread_resume_hook = hook;\n}\n\n/**\n * @ingroup Hook\n * This function sets a hook function when a thread is initialized.\n *\n * @param hook the specified hook function\n */\nvoid rt_thread_inited_sethook(void (*hook)(rt_thread_t thread))\n{\n rt_thread_inited_hook = hook;\n}\n\n#endif\n\nvoid rt_thread_exit(void)\n{\n struct rt_thread *thread;\n register rt_base_t level;\n\n /* get current thread */\n thread = rt_thread_self();\n\n /* disable interrupt */\n level = rt_hw_interrupt_disable();\n\n /* remove from schedule */\n rt_schedule_remove_thread(thread);\n /* change stat */\n thread->stat = RT_THREAD_CLOSE;\n\n /* remove it from timer list */\n rt_timer_detach(&thread->thread_timer);\n\n if ((rt_object_is_systemobject((rt_object_t)thread) == RT_TRUE) &&\n thread->cleanup == RT_NULL)\n {\n rt_object_detach((rt_object_t)thread);\n }\n else\n {\n /* insert to defunct thread list */\n rt_list_insert_after(&rt_thread_defunct, &(thread->tlist));\n }\n\n /* switch to next task */\n rt_schedule();\n\n /* enable interrupt */\n rt_hw_interrupt_enable(level);\n}\n\nstatic rt_err_t _rt_thread_init(struct rt_thread *thread,\n const char *name,\n void (*entry)(void *parameter),\n void *parameter,\n void *stack_start,\n rt_uint32_t stack_size,\n rt_uint8_t priority,\n rt_uint32_t tick)\n{\n /* init thread list */\n rt_list_init(&(thread->tlist));\n\n thread->entry = (void *)entry;\n thread->parameter = parameter;\n\n /* stack init */\n thread->stack_addr = stack_start;\n thread->stack_size = stack_size;\n\n /* init thread stack */\n rt_memset(thread->stack_addr, '#', thread->stack_size);\n#ifdef ARCH_CPU_STACK_GROWS_UPWARD\n thread->sp = (void *)rt_hw_stack_init(thread->entry, thread->parameter,\n (void *)((char *)thread->stack_addr),\n (void *)rt_thread_exit);\n#else\n thread->sp = (void *)rt_hw_stack_init(thread->entry, thread->parameter,\n (rt_uint8_t *)((char *)thread->stack_addr + thread->stack_size - sizeof(rt_ubase_t)),\n (void *)rt_thread_exit);\n#endif\n\n /* priority init */\n RT_ASSERT(priority < RT_THREAD_PRIORITY_MAX);\n thread->init_priority = priority;\n thread->current_priority = priority;\n\n thread->number_mask = 0;\n#if RT_THREAD_PRIORITY_MAX > 32\n thread->number = 0;\n thread->high_mask = 0;\n#endif\n\n /* tick init */\n thread->init_tick = tick;\n thread->remaining_tick = tick;\n\n /* error and flags */\n thread->error = RT_EOK;\n thread->stat = RT_THREAD_INIT;\n\n#ifdef RT_USING_SMP\n /* not bind on any cpu */\n thread->bind_cpu = RT_CPUS_NR;\n thread->oncpu = RT_CPU_DETACHED;\n\n /* lock init */\n thread->scheduler_lock_nest = 0;\n thread->cpus_lock_nest = 0;\n thread->critical_lock_nest = 0;\n#endif /*RT_USING_SMP*/\n\n /* initialize cleanup function and user data */\n thread->cleanup = 0;\n thread->user_data = 0;\n\n /* initialize thread timer */\n rt_timer_init(&(thread->thread_timer),\n thread->name,\n rt_thread_timeout,\n thread,\n 0,\n RT_TIMER_FLAG_ONE_SHOT);\n\n /* initialize signal */\n#ifdef RT_USING_SIGNALS\n thread->sig_mask = 0x00;\n thread->sig_pending = 0x00;\n\n#ifndef RT_USING_SMP\n thread->sig_ret = RT_NULL;\n#endif\n thread->sig_vectors = RT_NULL;\n thread->si_list = RT_NULL;\n#endif\n\n#ifdef RT_USING_LWP\n thread->lwp = RT_NULL;\n#endif\n\n RT_OBJECT_HOOK_CALL(rt_thread_inited_hook, (thread));\n\n return RT_EOK;\n}\n\n/**\n * @addtogroup Thread\n */\n\n/**@{*/\n\n/**\n * This function will initialize a thread, normally it's used to initialize a\n * static thread object.\n *\n * @param thread the static thread object\n * @param name the name of thread, which shall be unique\n * @param entry the entry function of thread\n * @param parameter the parameter of thread enter function\n * @param stack_start the start address of thread stack\n * @param stack_size the size of thread stack\n * @param priority the priority of thread\n * @param tick the time slice if there are same priority thread\n *\n * @return the operation status, RT_EOK on OK, -RT_ERROR on error\n */\nrt_err_t rt_thread_init(struct rt_thread *thread,\n const char *name,\n void (*entry)(void *parameter),\n void *parameter,\n void *stack_start,\n rt_uint32_t stack_size,\n rt_uint8_t priority,\n rt_uint32_t tick)\n{\n /* thread check */\n RT_ASSERT(thread != RT_NULL);\n RT_ASSERT(stack_start != RT_NULL);\n\n /* initialize thread object */\n rt_object_init((rt_object_t)thread, RT_Object_Class_Thread, name);\n\n return _rt_thread_init(thread,\n name,\n entry,\n parameter,\n stack_start,\n stack_size,\n priority,\n tick);\n}\nRTM_EXPORT(rt_thread_init);\n\n/**\n * This function will return self thread object\n *\n * @return the self thread object\n */\nrt_thread_t rt_thread_self(void)\n{\n#ifdef RT_USING_SMP\n rt_base_t lock;\n rt_thread_t self;\n\n lock = rt_hw_local_irq_disable();\n self = rt_cpu_self()->current_thread;\n rt_hw_local_irq_enable(lock);\n return self;\n#else\n extern rt_thread_t rt_current_thread;\n\n return rt_current_thread;\n#endif\n}\nRTM_EXPORT(rt_thread_self);\n\n/**\n * This function will start a thread and put it to system ready queue\n *\n * @param thread the thread to be started\n *\n * @return the operation status, RT_EOK on OK, -RT_ERROR on error\n */\nrt_err_t rt_thread_startup(rt_thread_t thread)\n{\n /* thread check */\n RT_ASSERT(thread != RT_NULL);\n RT_ASSERT((thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_INIT);\n RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);\n\n /* set current priority to initialize priority */\n thread->current_priority = thread->init_priority;\n\n /* calculate priority attribute */\n#if RT_THREAD_PRIORITY_MAX > 32\n thread->number = thread->current_priority >> 3; /* 5bit */\n thread->number_mask = 1L << thread->number;\n thread->high_mask = 1L << (thread->current_priority & 0x07); /* 3bit */\n#else\n thread->number_mask = 1L << thread->current_priority;\n#endif\n\n RT_DEBUG_LOG(RT_DEBUG_THREAD, (\"startup a thread:%s with priority:%d\\n\",\n thread->name, thread->init_priority));\n /* change thread stat */\n thread->stat = RT_THREAD_SUSPEND;\n /* then resume it */\n rt_thread_resume(thread);\n if (rt_thread_self() != RT_NULL)\n {\n /* do a scheduling */\n rt_schedule();\n }\n\n return RT_EOK;\n}\nRTM_EXPORT(rt_thread_startup);\n\n/**\n * This function will detach a thread. The thread object will be removed from\n * thread queue and detached/deleted from system object management.\n *\n * @param thread the thread to be deleted\n *\n * @return the operation status, RT_EOK on OK, -RT_ERROR on error\n */\nrt_err_t rt_thread_detach(rt_thread_t thread)\n{\n rt_base_t lock;\n\n /* thread check */\n RT_ASSERT(thread != RT_NULL);\n RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);\n RT_ASSERT(rt_object_is_systemobject((rt_object_t)thread));\n\n if ((thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_CLOSE)\n return RT_EOK;\n\n if ((thread->stat & RT_THREAD_STAT_MASK) != RT_THREAD_INIT)\n {\n /* remove from schedule */\n rt_schedule_remove_thread(thread);\n }\n\n /* release thread timer */\n rt_timer_detach(&(thread->thread_timer));\n\n /* change stat */\n thread->stat = RT_THREAD_CLOSE;\n\n if ((rt_object_is_systemobject((rt_object_t)thread) == RT_TRUE) &&\n thread->cleanup == RT_NULL)\n {\n rt_object_detach((rt_object_t)thread);\n }\n else\n {\n /* disable interrupt */\n lock = rt_hw_interrupt_disable();\n /* insert to defunct thread list */\n rt_list_insert_after(&rt_thread_defunct, &(thread->tlist));\n /* enable interrupt */\n rt_hw_interrupt_enable(lock);\n }\n\n return RT_EOK;\n}\nRTM_EXPORT(rt_thread_detach);\n\n#ifdef RT_USING_HEAP\n/**\n * This function will create a thread object and allocate thread object memory\n * and stack.\n *\n * @param name the name of thread, which shall be unique\n * @param entry the entry function of thread\n * @param parameter the parameter of thread enter function\n * @param stack_size the size of thread stack\n * @param priority the priority of thread\n * @param tick the time slice if there are same priority thread\n *\n * @return the created thread object\n */\nrt_thread_t rt_thread_create(const char *name,\n void (*entry)(void *parameter),\n void *parameter,\n rt_uint32_t stack_size,\n rt_uint8_t priority,\n rt_uint32_t tick)\n{\n struct rt_thread *thread;\n void *stack_start;\n\n thread = (struct rt_thread *)rt_object_allocate(RT_Object_Class_Thread,\n name);\n if (thread == RT_NULL)\n return RT_NULL;\n\n stack_start = (void *)RT_KERNEL_MALLOC(stack_size);\n if (stack_start == RT_NULL)\n {\n /* allocate stack failure */\n rt_object_delete((rt_object_t)thread);\n\n return RT_NULL;\n }\n\n _rt_thread_init(thread,\n name,\n entry,\n parameter,\n stack_start,\n stack_size,\n priority,\n tick);\n\n return thread;\n}\nRTM_EXPORT(rt_thread_create);\n\n/**\n * This function will delete a thread. The thread object will be removed from\n * thread queue and deleted from system object management in the idle thread.\n *\n * @param thread the thread to be deleted\n *\n * @return the operation status, RT_EOK on OK, -RT_ERROR on error\n */\nrt_err_t rt_thread_delete(rt_thread_t thread)\n{\n rt_base_t lock;\n\n /* thread check */\n RT_ASSERT(thread != RT_NULL);\n RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);\n RT_ASSERT(rt_object_is_systemobject((rt_object_t)thread) == RT_FALSE);\n\n if ((thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_CLOSE)\n return RT_EOK;\n\n if ((thread->stat & RT_THREAD_STAT_MASK) != RT_THREAD_INIT)\n {\n /* remove from schedule */\n rt_schedule_remove_thread(thread);\n }\n\n /* release thread timer */\n rt_timer_detach(&(thread->thread_timer));\n\n /* disable interrupt */\n lock = rt_hw_interrupt_disable();\n\n /* change stat */\n thread->stat = RT_THREAD_CLOSE;\n\n /* insert to defunct thread list */\n rt_list_insert_after(&rt_thread_defunct, &(thread->tlist));\n\n /* enable interrupt */\n rt_hw_interrupt_enable(lock);\n\n return RT_EOK;\n}\nRTM_EXPORT(rt_thread_delete);\n#endif\n\n/**\n * This function will let current thread yield processor, and scheduler will\n * choose a highest thread to run. After yield processor, the current thread\n * is still in READY state.\n *\n * @return RT_EOK\n */\nrt_err_t rt_thread_yield(void)\n{\n rt_schedule();\n\n return RT_EOK;\n}\nRTM_EXPORT(rt_thread_yield);\n\n/**\n * This function will let current thread sleep for some ticks.\n *\n * @param tick the sleep ticks\n *\n * @return RT_EOK\n */\nrt_err_t rt_thread_sleep(rt_tick_t tick)\n{\n register rt_base_t temp;\n struct rt_thread *thread;\n\n /* set to current thread */\n thread = rt_thread_self();\n RT_ASSERT(thread != RT_NULL);\n RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);\n\n /* disable interrupt */\n temp = rt_hw_interrupt_disable();\n\n /* suspend thread */\n rt_thread_suspend(thread);\n\n /* reset the timeout of thread timer and start it */\n rt_timer_control(&(thread->thread_timer), RT_TIMER_CTRL_SET_TIME, &tick);\n rt_timer_start(&(thread->thread_timer));\n\n /* enable interrupt */\n rt_hw_interrupt_enable(temp);\n\n rt_schedule();\n\n /* clear error number of this thread to RT_EOK */\n if (thread->error == -RT_ETIMEOUT)\n thread->error = RT_EOK;\n\n return RT_EOK;\n}\n\n/**\n * This function will let current thread delay for some ticks.\n *\n * @param tick the delay ticks\n *\n * @return RT_EOK\n */\nrt_err_t rt_thread_delay(rt_tick_t tick)\n{\n return rt_thread_sleep(tick);\n}\nRTM_EXPORT(rt_thread_delay);\n\n/**\n * This function will let current thread delay until (*tick + inc_tick).\n *\n * @param tick the tick of last wakeup.\n * @param inc_tick the increment tick\n *\n * @return RT_EOK\n */\nrt_err_t rt_thread_delay_until(rt_tick_t *tick, rt_tick_t inc_tick)\n{\n register rt_base_t level;\n struct rt_thread *thread;\n\n RT_ASSERT(tick != RT_NULL);\n\n /* set to current thread */\n thread = rt_thread_self();\n RT_ASSERT(thread != RT_NULL);\n RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);\n\n /* disable interrupt */\n level = rt_hw_interrupt_disable();\n\n if (rt_tick_get() - *tick < inc_tick)\n {\n *tick = rt_tick_get() - *tick + inc_tick;\n\n /* suspend thread */\n rt_thread_suspend(thread);\n\n /* reset the timeout of thread timer and start it */\n rt_timer_control(&(thread->thread_timer), RT_TIMER_CTRL_SET_TIME, tick);\n rt_timer_start(&(thread->thread_timer));\n\n /* enable interrupt */\n rt_hw_interrupt_enable(level);\n\n rt_schedule();\n\n /* clear error number of this thread to RT_EOK */\n if (thread->error == -RT_ETIMEOUT)\n {\n thread->error = RT_EOK;\n }\n }\n else\n {\n rt_hw_interrupt_enable(level);\n }\n\n /* get the wakeup tick */\n *tick = rt_tick_get();\n\n return RT_EOK;\n}\nRTM_EXPORT(rt_thread_delay_until);\n\n/**\n * This function will let current thread delay for some milliseconds.\n *\n * @param ms the delay ms time\n *\n * @return RT_EOK\n */\nrt_err_t rt_thread_mdelay(rt_int32_t ms)\n{\n rt_tick_t tick;\n\n tick = rt_tick_from_millisecond(ms);\n\n return rt_thread_sleep(tick);\n}\nRTM_EXPORT(rt_thread_mdelay);\n\n/**\n * This function will control thread behaviors according to control command.\n *\n * @param thread the specified thread to be controlled\n * @param cmd the control command, which includes\n * RT_THREAD_CTRL_CHANGE_PRIORITY for changing priority level of thread;\n * RT_THREAD_CTRL_STARTUP for starting a thread;\n * RT_THREAD_CTRL_CLOSE for delete a thread;\n * RT_THREAD_CTRL_BIND_CPU for bind the thread to a CPU.\n * @param arg the argument of control command\n *\n * @return RT_EOK\n */\nrt_err_t rt_thread_control(rt_thread_t thread, int cmd, void *arg)\n{\n register rt_base_t temp;\n\n /* thread check */\n RT_ASSERT(thread != RT_NULL);\n RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);\n\n switch (cmd)\n {\n case RT_THREAD_CTRL_CHANGE_PRIORITY:\n /* disable interrupt */\n temp = rt_hw_interrupt_disable();\n\n /* for ready thread, change queue */\n if ((thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_READY)\n {\n /* remove thread from schedule queue first */\n rt_schedule_remove_thread(thread);\n\n /* change thread priority */\n thread->current_priority = *(rt_uint8_t *)arg;\n\n /* recalculate priority attribute */\n#if RT_THREAD_PRIORITY_MAX > 32\n thread->number = thread->current_priority >> 3; /* 5bit */\n thread->number_mask = 1 << thread->number;\n thread->high_mask = 1 << (thread->current_priority & 0x07); /* 3bit */\n#else\n thread->number_mask = 1 << thread->current_priority;\n#endif\n\n /* insert thread to schedule queue again */\n rt_schedule_insert_thread(thread);\n }\n else\n {\n thread->current_priority = *(rt_uint8_t *)arg;\n\n /* recalculate priority attribute */\n#if RT_THREAD_PRIORITY_MAX > 32\n thread->number = thread->current_priority >> 3; /* 5bit */\n thread->number_mask = 1 << thread->number;\n thread->high_mask = 1 << (thread->current_priority & 0x07); /* 3bit */\n#else\n thread->number_mask = 1 << thread->current_priority;\n#endif\n }\n\n /* enable interrupt */\n rt_hw_interrupt_enable(temp);\n break;\n\n case RT_THREAD_CTRL_STARTUP:\n return rt_thread_startup(thread);\n\n case RT_THREAD_CTRL_CLOSE:\n\n if (rt_object_is_systemobject((rt_object_t)thread) == RT_TRUE)\n {\n return rt_thread_detach(thread);\n }\n#ifdef RT_USING_HEAP\n else\n {\n return rt_thread_delete(thread);\n }\n#endif\n\n#ifdef RT_USING_SMP\n case RT_THREAD_CTRL_BIND_CPU:\n {\n rt_uint8_t cpu;\n\n if ((thread->stat & RT_THREAD_STAT_MASK) != RT_THREAD_INIT)\n {\n /* we only support bind cpu before started phase. */\n return RT_ERROR;\n }\n\n cpu = (rt_uint8_t)(size_t)arg;\n thread->bind_cpu = cpu > RT_CPUS_NR? RT_CPUS_NR : cpu;\n break;\n }\n#endif /*RT_USING_SMP*/\n default:\n break;\n }\n\n return RT_EOK;\n}\nRTM_EXPORT(rt_thread_control);\n\n/**\n * This function will suspend the specified thread.\n *\n * @param thread the thread to be suspended\n *\n * @return the operation status, RT_EOK on OK, -RT_ERROR on error\n *\n * @note if suspend self thread, after this function call, the\n * rt_schedule() must be invoked.\n */\nrt_err_t rt_thread_suspend(rt_thread_t thread)\n{\n register rt_base_t stat;\n register rt_base_t temp;\n\n /* thread check */\n RT_ASSERT(thread != RT_NULL);\n RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);\n\n RT_DEBUG_LOG(RT_DEBUG_THREAD, (\"thread suspend: %s\\n\", thread->name));\n\n stat = thread->stat & RT_THREAD_STAT_MASK;\n if ((stat != RT_THREAD_READY) && (stat != RT_THREAD_RUNNING))\n {\n RT_DEBUG_LOG(RT_DEBUG_THREAD, (\"thread suspend: thread disorder, 0x%2x\\n\",\n thread->stat));\n return -RT_ERROR;\n }\n\n /* disable interrupt */\n temp = rt_hw_interrupt_disable();\n if (stat == RT_THREAD_RUNNING)\n {\n /* not suspend running status thread on other core */\n RT_ASSERT(thread == rt_thread_self());\n }\n\n /* change thread stat */\n rt_schedule_remove_thread(thread);\n thread->stat = RT_THREAD_SUSPEND | (thread->stat & ~RT_THREAD_STAT_MASK);\n\n /* stop thread timer anyway */\n rt_timer_stop(&(thread->thread_timer));\n\n /* enable interrupt */\n rt_hw_interrupt_enable(temp);\n\n RT_OBJECT_HOOK_CALL(rt_thread_suspend_hook, (thread));\n return RT_EOK;\n}\nRTM_EXPORT(rt_thread_suspend);\n\n/**\n * This function will resume a thread and put it to system ready queue.\n *\n * @param thread the thread to be resumed\n *\n * @return the operation status, RT_EOK on OK, -RT_ERROR on error\n */\nrt_err_t rt_thread_resume(rt_thread_t thread)\n{\n register rt_base_t temp;\n\n /* thread check */\n RT_ASSERT(thread != RT_NULL);\n RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);\n\n RT_DEBUG_LOG(RT_DEBUG_THREAD, (\"thread resume: %s\\n\", thread->name));\n\n if ((thread->stat & RT_THREAD_STAT_MASK) != RT_THREAD_SUSPEND)\n {\n RT_DEBUG_LOG(RT_DEBUG_THREAD, (\"thread resume: thread disorder, %d\\n\",\n thread->stat));\n\n return -RT_ERROR;\n }\n\n /* disable interrupt */\n temp = rt_hw_interrupt_disable();\n\n /* remove from suspend list */\n rt_list_remove(&(thread->tlist));\n\n rt_timer_stop(&thread->thread_timer);\n\n /* enable interrupt */\n rt_hw_interrupt_enable(temp);\n\n /* insert to schedule ready list */\n rt_schedule_insert_thread(thread);\n\n RT_OBJECT_HOOK_CALL(rt_thread_resume_hook, (thread));\n return RT_EOK;\n}\nRTM_EXPORT(rt_thread_resume);\n\n/**\n * This function is the timeout function for thread, normally which is invoked\n * when thread is timeout to wait some resource.\n *\n * @param parameter the parameter of thread timeout function\n */\nvoid rt_thread_timeout(void *parameter)\n{\n struct rt_thread *thread;\n\n thread = (struct rt_thread *)parameter;\n\n /* thread check */\n RT_ASSERT(thread != RT_NULL);\n RT_ASSERT((thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_SUSPEND);\n RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);\n\n /* set error number */\n thread->error = -RT_ETIMEOUT;\n\n /* remove from suspend list */\n rt_list_remove(&(thread->tlist));\n\n /* insert to schedule ready list */\n rt_schedule_insert_thread(thread);\n\n /* do schedule */\n rt_schedule();\n}\nRTM_EXPORT(rt_thread_timeout);\n\n/**\n * This function will find the specified thread.\n *\n * @param name the name of thread finding\n *\n * @return the found thread\n *\n * @note please don't invoke this function in interrupt status.\n */\nrt_thread_t rt_thread_find(char *name)\n{\n struct rt_object_information *information;\n struct rt_object *object;\n struct rt_list_node *node;\n\n /* enter critical */\n if (rt_thread_self() != RT_NULL)\n rt_enter_critical();\n\n /* try to find device object */\n information = rt_object_get_information(RT_Object_Class_Thread);\n RT_ASSERT(information != RT_NULL);\n for (node = information->object_list.next;\n node != &(information->object_list);\n node = node->next)\n {\n object = rt_list_entry(node, struct rt_object, list);\n if (rt_strncmp(object->name, name, RT_NAME_MAX) == 0)\n {\n /* leave critical */\n if (rt_thread_self() != RT_NULL)\n rt_exit_critical();\n\n return (rt_thread_t)object;\n }\n }\n\n /* leave critical */\n if (rt_thread_self() != RT_NULL)\n rt_exit_critical();\n\n /* not found */\n return RT_NULL;\n}\nRTM_EXPORT(rt_thread_find);\n\n/**@}*/\n", "src/timer.c": "/*\n * Copyright (c) 2006-2018, RT-Thread Development Team\n *\n * SPDX-License-Identifier: Apache-2.0\n *\n * Change Logs:\n * Date Author Notes\n * 2006-03-12 Bernard first version\n * 2006-04-29 Bernard implement thread timer\n * 2006-06-04 Bernard implement rt_timer_control\n * 2006-08-10 Bernard fix the periodic timer bug\n * 2006-09-03 Bernard implement rt_timer_detach\n * 2009-11-11 LiJin add soft timer\n * 2010-05-12 Bernard fix the timer check bug.\n * 2010-11-02 Charlie re-implement tick overflow issue\n * 2012-12-15 Bernard fix the next timeout issue in soft timer\n * 2014-07-12 Bernard does not lock scheduler when invoking soft-timer\n * timeout function.\n */\n\n#include <rtthread.h>\n#include <rthw.h>\n\n/* hard timer list */\nstatic rt_list_t rt_timer_list[RT_TIMER_SKIP_LIST_LEVEL];\n\n#ifdef RT_USING_TIMER_SOFT\n#ifndef RT_TIMER_THREAD_STACK_SIZE\n#define RT_TIMER_THREAD_STACK_SIZE 512\n#endif\n\n#ifndef RT_TIMER_THREAD_PRIO\n#define RT_TIMER_THREAD_PRIO 0\n#endif\n\n/* soft timer list */\nstatic rt_list_t rt_soft_timer_list[RT_TIMER_SKIP_LIST_LEVEL];\nstatic struct rt_thread timer_thread;\nALIGN(RT_ALIGN_SIZE)\nstatic rt_uint8_t timer_thread_stack[RT_TIMER_THREAD_STACK_SIZE];\n#endif\n\n#ifdef RT_USING_HOOK\nextern void (*rt_object_take_hook)(struct rt_object *object);\nextern void (*rt_object_put_hook)(struct rt_object *object);\nstatic void (*rt_timer_enter_hook)(struct rt_timer *timer);\nstatic void (*rt_timer_exit_hook)(struct rt_timer *timer);\n\n/**\n * @addtogroup Hook\n */\n\n/**@{*/\n\n/**\n * This function will set a hook function, which will be invoked when enter\n * timer timeout callback function.\n *\n * @param hook the hook function\n */\nvoid rt_timer_enter_sethook(void (*hook)(struct rt_timer *timer))\n{\n rt_timer_enter_hook = hook;\n}\n\n/**\n * This function will set a hook function, which will be invoked when exit\n * timer timeout callback function.\n *\n * @param hook the hook function\n */\nvoid rt_timer_exit_sethook(void (*hook)(struct rt_timer *timer))\n{\n rt_timer_exit_hook = hook;\n}\n\n/**@}*/\n#endif\n\nstatic void _rt_timer_init(rt_timer_t timer,\n void (*timeout)(void *parameter),\n void *parameter,\n rt_tick_t time,\n rt_uint8_t flag)\n{\n int i;\n\n /* set flag */\n timer->parent.flag = flag;\n\n /* set deactivated */\n timer->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;\n\n timer->timeout_func = timeout;\n timer->parameter = parameter;\n\n timer->timeout_tick = 0;\n timer->init_tick = time;\n\n /* initialize timer list */\n for (i = 0; i < RT_TIMER_SKIP_LIST_LEVEL; i++)\n {\n rt_list_init(&(timer->row[i]));\n }\n}\n\n/* the fist timer always in the last row */\nstatic rt_tick_t rt_timer_list_next_timeout(rt_list_t timer_list[])\n{\n struct rt_timer *timer;\n register rt_base_t level;\n rt_tick_t timeout_tick = RT_TICK_MAX;\n\n /* disable interrupt */\n level = rt_hw_interrupt_disable();\n\n if (!rt_list_isempty(&timer_list[RT_TIMER_SKIP_LIST_LEVEL - 1]))\n {\n timer = rt_list_entry(timer_list[RT_TIMER_SKIP_LIST_LEVEL - 1].next,\n struct rt_timer, row[RT_TIMER_SKIP_LIST_LEVEL - 1]);\n timeout_tick = timer->timeout_tick;\n }\n\n /* enable interrupt */\n rt_hw_interrupt_enable(level);\n\n return timeout_tick;\n}\n\nrt_inline void _rt_timer_remove(rt_timer_t timer)\n{\n int i;\n\n for (i = 0; i < RT_TIMER_SKIP_LIST_LEVEL; i++)\n {\n rt_list_remove(&timer->row[i]);\n }\n}\n\n#if RT_DEBUG_TIMER\nstatic int rt_timer_count_height(struct rt_timer *timer)\n{\n int i, cnt = 0;\n\n for (i = 0; i < RT_TIMER_SKIP_LIST_LEVEL; i++)\n {\n if (!rt_list_isempty(&timer->row[i]))\n cnt++;\n }\n return cnt;\n}\n\nvoid rt_timer_dump(rt_list_t timer_heads[])\n{\n rt_list_t *list;\n\n for (list = timer_heads[RT_TIMER_SKIP_LIST_LEVEL - 1].next;\n list != &timer_heads[RT_TIMER_SKIP_LIST_LEVEL - 1];\n list = list->next)\n {\n struct rt_timer *timer = rt_list_entry(list,\n struct rt_timer,\n row[RT_TIMER_SKIP_LIST_LEVEL - 1]);\n rt_kprintf(\"%d\", rt_timer_count_height(timer));\n }\n rt_kprintf(\"\\n\");\n}\n#endif\n\n/**\n * @addtogroup Clock\n */\n\n/**@{*/\n\n/**\n * This function will initialize a timer, normally this function is used to\n * initialize a static timer object.\n *\n * @param timer the static timer object\n * @param name the name of timer\n * @param timeout the timeout function\n * @param parameter the parameter of timeout function\n * @param time the tick of timer\n * @param flag the flag of timer\n */\nvoid rt_timer_init(rt_timer_t timer,\n const char *name,\n void (*timeout)(void *parameter),\n void *parameter,\n rt_tick_t time,\n rt_uint8_t flag)\n{\n /* timer check */\n RT_ASSERT(timer != RT_NULL);\n\n /* timer object initialization */\n rt_object_init((rt_object_t)timer, RT_Object_Class_Timer, name);\n\n _rt_timer_init(timer, timeout, parameter, time, flag);\n}\nRTM_EXPORT(rt_timer_init);\n\n/**\n * This function will detach a timer from timer management.\n *\n * @param timer the static timer object\n *\n * @return the operation status, RT_EOK on OK; RT_ERROR on error\n */\nrt_err_t rt_timer_detach(rt_timer_t timer)\n{\n register rt_base_t level;\n\n /* timer check */\n RT_ASSERT(timer != RT_NULL);\n RT_ASSERT(rt_object_get_type(&timer->parent) == RT_Object_Class_Timer);\n RT_ASSERT(rt_object_is_systemobject(&timer->parent));\n\n /* disable interrupt */\n level = rt_hw_interrupt_disable();\n\n _rt_timer_remove(timer);\n\n /* enable interrupt */\n rt_hw_interrupt_enable(level);\n\n rt_object_detach((rt_object_t)timer);\n\n return RT_EOK;\n}\nRTM_EXPORT(rt_timer_detach);\n\n#ifdef RT_USING_HEAP\n/**\n * This function will create a timer\n *\n * @param name the name of timer\n * @param timeout the timeout function\n * @param parameter the parameter of timeout function\n * @param time the tick of timer\n * @param flag the flag of timer\n *\n * @return the created timer object\n */\nrt_timer_t rt_timer_create(const char *name,\n void (*timeout)(void *parameter),\n void *parameter,\n rt_tick_t time,\n rt_uint8_t flag)\n{\n struct rt_timer *timer;\n\n /* allocate a object */\n timer = (struct rt_timer *)rt_object_allocate(RT_Object_Class_Timer, name);\n if (timer == RT_NULL)\n {\n return RT_NULL;\n }\n\n _rt_timer_init(timer, timeout, parameter, time, flag);\n\n return timer;\n}\nRTM_EXPORT(rt_timer_create);\n\n/**\n * This function will delete a timer and release timer memory\n *\n * @param timer the timer to be deleted\n *\n * @return the operation status, RT_EOK on OK; RT_ERROR on error\n */\nrt_err_t rt_timer_delete(rt_timer_t timer)\n{\n register rt_base_t level;\n\n /* timer check */\n RT_ASSERT(timer != RT_NULL);\n RT_ASSERT(rt_object_get_type(&timer->parent) == RT_Object_Class_Timer);\n RT_ASSERT(rt_object_is_systemobject(&timer->parent) == RT_FALSE);\n\n /* disable interrupt */\n level = rt_hw_interrupt_disable();\n\n _rt_timer_remove(timer);\n\n /* enable interrupt */\n rt_hw_interrupt_enable(level);\n\n rt_object_delete((rt_object_t)timer);\n\n return RT_EOK;\n}\nRTM_EXPORT(rt_timer_delete);\n#endif\n\n/**\n * This function will start the timer\n *\n * @param timer the timer to be started\n *\n * @return the operation status, RT_EOK on OK, -RT_ERROR on error\n */\nrt_err_t rt_timer_start(rt_timer_t timer)\n{\n unsigned int row_lvl;\n rt_list_t *timer_list;\n register rt_base_t level;\n rt_list_t *row_head[RT_TIMER_SKIP_LIST_LEVEL];\n unsigned int tst_nr;\n static unsigned int random_nr;\n\n /* timer check */\n RT_ASSERT(timer != RT_NULL);\n RT_ASSERT(rt_object_get_type(&timer->parent) == RT_Object_Class_Timer);\n\n /* stop timer firstly */\n level = rt_hw_interrupt_disable();\n /* remove timer from list */\n _rt_timer_remove(timer);\n /* change status of timer */\n timer->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;\n rt_hw_interrupt_enable(level);\n\n RT_OBJECT_HOOK_CALL(rt_object_take_hook, (&(timer->parent)));\n\n /*\n * get timeout tick,\n * the max timeout tick shall not great than RT_TICK_MAX/2\n */\n RT_ASSERT(timer->init_tick < RT_TICK_MAX / 2);\n timer->timeout_tick = rt_tick_get() + timer->init_tick;\n\n /* disable interrupt */\n level = rt_hw_interrupt_disable();\n\n#ifdef RT_USING_TIMER_SOFT\n if (timer->parent.flag & RT_TIMER_FLAG_SOFT_TIMER)\n {\n /* insert timer to soft timer list */\n timer_list = rt_soft_timer_list;\n }\n else\n#endif\n {\n /* insert timer to system timer list */\n timer_list = rt_timer_list;\n }\n\n row_head[0] = &timer_list[0];\n for (row_lvl = 0; row_lvl < RT_TIMER_SKIP_LIST_LEVEL; row_lvl++)\n {\n for (; row_head[row_lvl] != timer_list[row_lvl].prev;\n row_head[row_lvl] = row_head[row_lvl]->next)\n {\n struct rt_timer *t;\n rt_list_t *p = row_head[row_lvl]->next;\n\n /* fix up the entry pointer */\n t = rt_list_entry(p, struct rt_timer, row[row_lvl]);\n\n /* If we have two timers that timeout at the same time, it's\n * preferred that the timer inserted early get called early.\n * So insert the new timer to the end the the some-timeout timer\n * list.\n */\n if ((t->timeout_tick - timer->timeout_tick) == 0)\n {\n continue;\n }\n else if ((t->timeout_tick - timer->timeout_tick) < RT_TICK_MAX / 2)\n {\n break;\n }\n }\n if (row_lvl != RT_TIMER_SKIP_LIST_LEVEL - 1)\n row_head[row_lvl + 1] = row_head[row_lvl] + 1;\n }\n\n /* Interestingly, this super simple timer insert counter works very very\n * well on distributing the list height uniformly. By means of \"very very\n * well\", I mean it beats the randomness of timer->timeout_tick very easily\n * (actually, the timeout_tick is not random and easy to be attacked). */\n random_nr++;\n tst_nr = random_nr;\n\n rt_list_insert_after(row_head[RT_TIMER_SKIP_LIST_LEVEL - 1],\n &(timer->row[RT_TIMER_SKIP_LIST_LEVEL - 1]));\n for (row_lvl = 2; row_lvl <= RT_TIMER_SKIP_LIST_LEVEL; row_lvl++)\n {\n if (!(tst_nr & RT_TIMER_SKIP_LIST_MASK))\n rt_list_insert_after(row_head[RT_TIMER_SKIP_LIST_LEVEL - row_lvl],\n &(timer->row[RT_TIMER_SKIP_LIST_LEVEL - row_lvl]));\n else\n break;\n /* Shift over the bits we have tested. Works well with 1 bit and 2\n * bits. */\n tst_nr >>= (RT_TIMER_SKIP_LIST_MASK + 1) >> 1;\n }\n\n timer->parent.flag |= RT_TIMER_FLAG_ACTIVATED;\n\n /* enable interrupt */\n rt_hw_interrupt_enable(level);\n\n#ifdef RT_USING_TIMER_SOFT\n if (timer->parent.flag & RT_TIMER_FLAG_SOFT_TIMER)\n {\n /* check whether timer thread is ready */\n if ((timer_thread.stat & RT_THREAD_STAT_MASK) == RT_THREAD_SUSPEND)\n {\n /* resume timer thread to check soft timer */\n rt_thread_resume(&timer_thread);\n rt_schedule();\n }\n }\n#endif\n\n return RT_EOK;\n}\nRTM_EXPORT(rt_timer_start);\n\n/**\n * This function will stop the timer\n *\n * @param timer the timer to be stopped\n *\n * @return the operation status, RT_EOK on OK, -RT_ERROR on error\n */\nrt_err_t rt_timer_stop(rt_timer_t timer)\n{\n register rt_base_t level;\n\n /* timer check */\n RT_ASSERT(timer != RT_NULL);\n RT_ASSERT(rt_object_get_type(&timer->parent) == RT_Object_Class_Timer);\n\n if (!(timer->parent.flag & RT_TIMER_FLAG_ACTIVATED))\n return -RT_ERROR;\n\n RT_OBJECT_HOOK_CALL(rt_object_put_hook, (&(timer->parent)));\n\n /* disable interrupt */\n level = rt_hw_interrupt_disable();\n\n _rt_timer_remove(timer);\n\n /* enable interrupt */\n rt_hw_interrupt_enable(level);\n\n /* change stat */\n timer->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;\n\n return RT_EOK;\n}\nRTM_EXPORT(rt_timer_stop);\n\n/**\n * This function will get or set some options of the timer\n *\n * @param timer the timer to be get or set\n * @param cmd the control command\n * @param arg the argument\n *\n * @return RT_EOK\n */\nrt_err_t rt_timer_control(rt_timer_t timer, int cmd, void *arg)\n{\n /* timer check */\n RT_ASSERT(timer != RT_NULL);\n RT_ASSERT(rt_object_get_type(&timer->parent) == RT_Object_Class_Timer);\n\n switch (cmd)\n {\n case RT_TIMER_CTRL_GET_TIME:\n *(rt_tick_t *)arg = timer->init_tick;\n break;\n\n case RT_TIMER_CTRL_SET_TIME:\n timer->init_tick = *(rt_tick_t *)arg;\n break;\n\n case RT_TIMER_CTRL_SET_ONESHOT:\n timer->parent.flag &= ~RT_TIMER_FLAG_PERIODIC;\n break;\n\n case RT_TIMER_CTRL_SET_PERIODIC:\n timer->parent.flag |= RT_TIMER_FLAG_PERIODIC;\n break;\n\n case RT_TIMER_CTRL_GET_STATE:\n if(timer->parent.flag & RT_TIMER_FLAG_ACTIVATED)\n {\n /*timer is start and run*/\n *(rt_tick_t *)arg = RT_TIMER_FLAG_ACTIVATED;\n }\n else\n {\n /*timer is stop*/\n *(rt_tick_t *)arg = RT_TIMER_FLAG_DEACTIVATED;\n }\n break;\n\n default:\n break;\n }\n\n return RT_EOK;\n}\nRTM_EXPORT(rt_timer_control);\n\n/**\n * This function will check timer list, if a timeout event happens, the\n * corresponding timeout function will be invoked.\n *\n * @note this function shall be invoked in operating system timer interrupt.\n */\nvoid rt_timer_check(void)\n{\n struct rt_timer *t;\n rt_tick_t current_tick;\n register rt_base_t level;\n\n RT_DEBUG_LOG(RT_DEBUG_TIMER, (\"timer check enter\\n\"));\n\n current_tick = rt_tick_get();\n\n /* disable interrupt */\n level = rt_hw_interrupt_disable();\n\n while (!rt_list_isempty(&rt_timer_list[RT_TIMER_SKIP_LIST_LEVEL - 1]))\n {\n t = rt_list_entry(rt_timer_list[RT_TIMER_SKIP_LIST_LEVEL - 1].next,\n struct rt_timer, row[RT_TIMER_SKIP_LIST_LEVEL - 1]);\n\n /*\n * It supposes that the new tick shall less than the half duration of\n * tick max.\n */\n if ((current_tick - t->timeout_tick) < RT_TICK_MAX / 2)\n {\n RT_OBJECT_HOOK_CALL(rt_timer_enter_hook, (t));\n\n /* remove timer from timer list firstly */\n _rt_timer_remove(t);\n\n /* call timeout function */\n t->timeout_func(t->parameter);\n\n /* re-get tick */\n current_tick = rt_tick_get();\n\n RT_OBJECT_HOOK_CALL(rt_timer_exit_hook, (t));\n RT_DEBUG_LOG(RT_DEBUG_TIMER, (\"current tick: %d\\n\", current_tick));\n\n if ((t->parent.flag & RT_TIMER_FLAG_PERIODIC) &&\n (t->parent.flag & RT_TIMER_FLAG_ACTIVATED))\n {\n /* start it */\n t->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;\n rt_timer_start(t);\n }\n else\n {\n /* stop timer */\n t->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;\n }\n }\n else\n break;\n }\n\n /* enable interrupt */\n rt_hw_interrupt_enable(level);\n\n RT_DEBUG_LOG(RT_DEBUG_TIMER, (\"timer check leave\\n\"));\n}\n\n/**\n * This function will return the next timeout tick in the system.\n *\n * @return the next timeout tick in the system\n */\nrt_tick_t rt_timer_next_timeout_tick(void)\n{\n return rt_timer_list_next_timeout(rt_timer_list);\n}\n\n#ifdef RT_USING_TIMER_SOFT\n/**\n * This function will check timer list, if a timeout event happens, the\n * corresponding timeout function will be invoked.\n */\nvoid rt_soft_timer_check(void)\n{\n rt_tick_t current_tick;\n struct rt_timer *t;\n\n RT_DEBUG_LOG(RT_DEBUG_TIMER, (\"software timer check enter\\n\"));\n\n /* lock scheduler */\n rt_enter_critical();\n\n while (!rt_list_isempty(&rt_soft_timer_list[RT_TIMER_SKIP_LIST_LEVEL - 1]))\n {\n t = rt_list_entry(rt_soft_timer_list[RT_TIMER_SKIP_LIST_LEVEL - 1].next,\n struct rt_timer, row[RT_TIMER_SKIP_LIST_LEVEL - 1]);\n\n current_tick = rt_tick_get();\n\n /*\n * It supposes that the new tick shall less than the half duration of\n * tick max.\n */\n if ((current_tick - t->timeout_tick) < RT_TICK_MAX / 2)\n {\n RT_OBJECT_HOOK_CALL(rt_timer_enter_hook, (t));\n\n /* remove timer from timer list firstly */\n _rt_timer_remove(t);\n\n /* not lock scheduler when performing timeout function */\n rt_exit_critical();\n /* call timeout function */\n t->timeout_func(t->parameter);\n\n RT_OBJECT_HOOK_CALL(rt_timer_exit_hook, (t));\n RT_DEBUG_LOG(RT_DEBUG_TIMER, (\"current tick: %d\\n\", current_tick));\n\n /* lock scheduler */\n rt_enter_critical();\n\n if ((t->parent.flag & RT_TIMER_FLAG_PERIODIC) &&\n (t->parent.flag & RT_TIMER_FLAG_ACTIVATED))\n {\n /* start it */\n t->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;\n rt_timer_start(t);\n }\n else\n {\n /* stop timer */\n t->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;\n }\n }\n else break; /* not check anymore */\n }\n\n /* unlock scheduler */\n rt_exit_critical();\n\n RT_DEBUG_LOG(RT_DEBUG_TIMER, (\"software timer check leave\\n\"));\n}\n\n/* system timer thread entry */\nstatic void rt_thread_timer_entry(void *parameter)\n{\n rt_tick_t next_timeout;\n\n while (1)\n {\n /* get the next timeout tick */\n next_timeout = rt_timer_list_next_timeout(rt_soft_timer_list);\n if (next_timeout == RT_TICK_MAX)\n {\n /* no software timer exist, suspend self. */\n rt_thread_suspend(rt_thread_self());\n rt_schedule();\n }\n else\n {\n rt_tick_t current_tick;\n\n /* get current tick */\n current_tick = rt_tick_get();\n\n if ((next_timeout - current_tick) < RT_TICK_MAX / 2)\n {\n /* get the delta timeout tick */\n next_timeout = next_timeout - current_tick;\n rt_thread_delay(next_timeout);\n }\n }\n\n /* check software timer */\n rt_soft_timer_check();\n }\n}\n#endif\n\n/**\n * @ingroup SystemInit\n *\n * This function will initialize system timer\n */\nvoid rt_system_timer_init(void)\n{\n int i;\n\n for (i = 0; i < sizeof(rt_timer_list) / sizeof(rt_timer_list[0]); i++)\n {\n rt_list_init(rt_timer_list + i);\n }\n}\n\n/**\n * @ingroup SystemInit\n *\n * This function will initialize system timer thread\n */\nvoid rt_system_timer_thread_init(void)\n{\n#ifdef RT_USING_TIMER_SOFT\n int i;\n\n for (i = 0;\n i < sizeof(rt_soft_timer_list) / sizeof(rt_soft_timer_list[0]);\n i++)\n {\n rt_list_init(rt_soft_timer_list + i);\n }\n\n /* start software timer thread */\n rt_thread_init(&timer_thread,\n \"timer\",\n rt_thread_timer_entry,\n RT_NULL,\n &timer_thread_stack[0],\n sizeof(timer_thread_stack),\n RT_TIMER_THREAD_PRIO,\n 10);\n\n /* startup */\n rt_thread_startup(&timer_thread);\n#endif\n}\n\n/**@}*/\n"}