RT-Thread / RT-Thread/rt-thread

[Feature] 关于名字重复性的问题,是否需要改变 rt_object_init 接口

Open
#11,043 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C
Stars
12.2k
Forks
5.4k
Avg merge
4d 12h
Merged PRs (30d)
40

Description

Describe problem solved by the proposed feature

获取句柄的方式
方法一:作为线程参数传递

/* 定义全局句柄变量 */
rt_mq_t mq_handle = RT_NULL;

void thread_entry(void* parameter)
{
    /* 1. 将 void* 强转回 rt_mq_t */
    rt_mq_t mq = (rt_mq_t)parameter;
    char buf[10];

    while (1)
    {
        /* 2. 使用句柄接收消息 */
        if (rt_mq_recv(mq, &buf, sizeof(buf), RT_WAITING_FOREVER) == RT_EOK)
        {
            rt_kprintf("Received: %s\n", buf);
        }
    }
}

int main(void)
{
    /* 创建消息队列 */
    mq_handle = rt_mq_create("mq1", 10, 5, RT_IPC_FLAG_FIFO);

    /* 创建线程时,将 mq_handle 作为第4个参数传递 */
    rt_thread_t tid = rt_thread_create("t1", thread_entry, (void*)mq_handle, 
                                       1024, 25, 10);
    if (tid != RT_NULL) rt_thread_startup(tid);
    
    return 0;
}

方法二:使用 extern 全局变量

rt_mq_t global_mq = RT_NULL; // 定义

void system_init(void) {
    global_mq = rt_mq_create("mq_global", 10, 5, RT_IPC_FLAG_FIFO);
}

方法三:使用 rt_object_find

void thread_consumer_entry(void* parameter)
{
    rt_mq_t find_mq;

    /* 1. 通过名字查找对象 */
    /* 注意:rt_object_find 返回的是 rt_object_t,需要强转,且需要指定类型 */
    rt_object_t obj = rt_object_find("sensor_mq", RT_Object_Class_MessageQueue);

    if (obj == RT_NULL)
    {
        rt_kprintf("Error: MQ not found!\n");
        return;
    }
    
    /* 2. 强转为消息队列句柄 */
    find_mq = (rt_mq_t)obj;

    /* 3. 使用 */
    char buffer[10];
    rt_mq_recv(find_mq, buffer, 10, RT_WAITING_FOREVER);
}

如果大家工程上大部分采用find话,那是不是得考虑名字的唯一性问题,#10968 中提议修改rt_object_init 接口的方法是否可行?

Describe your preferred solution

No response

Describe possible alternatives

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading the rt_object_init and rt_object_find entry points and the discussion in issue #10968. Determine whether object names must be unique and clarify the intended interface change and completion criteria; this issue names no files or tests to run.

Written by the indexing model from the issue text.

Assessment

Tech stack
c
Domain
operating-systems
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.