Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[src] fix mutex bug #10050

Merged
merged 3 commits into from
Apr 13, 2025
Merged

[src] fix mutex bug #10050

merged 3 commits into from
Apr 13, 2025

Conversation

wycwyhwyq
Copy link
Contributor

@wycwyhwyq wycwyhwyq commented Feb 27, 2025

拉取/合并请求描述:(PR description)

[

为什么提交这份PR (why to submit this PR)

#10049

你的解决方案是什么 (what is your solution)

请提供验证的bsp和config (provide the config and bsp)

  • BSP:
    risc-v/K230
  • .config:
  • action:

]

当前拉取/合并请求的状态 Intent for your PR

必须选择一项 Choose one (Mandatory):

  • 本拉取/合并请求是一个草稿版本 This PR is for a code-review and is intended to get feedback
  • 本拉取/合并请求是一个成熟版本 This PR is mature, and ready to be integrated into the repo

代码质量 Code Quality:

我在这个拉取/合并请求中已经考虑了 As part of this pull request, I've considered the following:

  • 已经仔细查看过代码改动的对比 Already check the difference between PR and old code
  • 代码风格正确,包括缩进空格,命名及其他风格 Style guide is adhered to, including spacing, naming and other styles
  • 没有垃圾代码,代码尽量精简,不包含#if 0代码,不包含已经被注释了的代码 All redundant code is removed and cleaned up
  • 所有变更均有原因及合理的,并且不会影响到其他软件组件代码或BSP All modifications are justified and not affect other components or BSP
  • 对难懂代码均提供对应的注释 I've commented appropriately where code is tricky
  • 代码是高质量的 Code in this PR is of high quality
  • 已经使用formatting 等源码格式化工具确保格式符合RT-Thread代码规范 This PR complies with RT-Thread code specification
  • 如果是新增bsp, 已经添加ci检查到.github/workflows/bsp_buildings.yml 详细请参考链接BSP自查

@github-actions github-actions bot added the Kernel PR has src relate code label Feb 27, 2025
src/thread.c Outdated
@@ -797,7 +797,7 @@ rt_err_t rt_thread_control(rt_thread_t thread, int cmd, void *arg)
rt_err_t error;
rt_sched_lock_level_t slvl;
rt_sched_lock(&slvl);
error = rt_sched_thread_change_priority(thread, *(rt_uint8_t *)arg);
error = rt_sched_thread_set_priority(thread, *(rt_uint8_t *)arg);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个API为什么不直接在 rt_sched_thread_change_priority 基础上修改呢?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个API为什么不直接在 rt_sched_thread_change_priority 基础上修改呢?
原API还有其它地方在调用,如_thread_update_priority_check_and_update_prio,这些地方不能调整init_priority。我理解init_priority用于设置task的正常优先级,而current_priority可能会因为使用mutex改变,是临时优先级,当使用完mutex后task会返回到init_priority

Copy link
Member

@Rbb666 Rbb666 Mar 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个API为什么不直接在 rt_sched_thread_change_priority 基础上修改呢?
原API还有其它地方在调用,如_thread_update_priority_check_and_update_prio,这些地方不能调整init_priority。我理解init_priority用于设置task的正常优先级,而current_priority可能会因为使用mutex改变,是临时优先级,当使用完mutex后task会返回到init_priority

建议在rt_thread_control中新增一个:RT_THREAD_CTRL_SET_PRIORITY的选项

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

建议在rt_thread_control中新增一个:RT_THREAD_CTRL_SET_PRIORITY的选项

新增涉及到更改rtdef.h,请为RT_THREAD_CTRL_SET_PRIORITY提供一个合适值,0x05?

#define RT_THREAD_CTRL_STARTUP          0x00                /**< Startup thread. */
#define RT_THREAD_CTRL_CLOSE            0x01                /**< Close thread. */
#define RT_THREAD_CTRL_CHANGE_PRIORITY  0x02                /**< Change thread priority. */
#define RT_THREAD_CTRL_INFO             0x03                /**< Get thread information. */
#define RT_THREAD_CTRL_BIND_CPU         0x04                /**< Set thread bind cpu. */

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

建议在rt_thread_control中新增一个:RT_THREAD_CTRL_SET_PRIORITY的选项

新增涉及到更改rtdef.h,请为RT_THREAD_CTRL_SET_PRIORITY提供一个合适值,0x05?

#define RT_THREAD_CTRL_STARTUP          0x00                /**< Startup thread. */
#define RT_THREAD_CTRL_CLOSE            0x01                /**< Close thread. */
#define RT_THREAD_CTRL_CHANGE_PRIORITY  0x02                /**< Change thread priority. */
#define RT_THREAD_CTRL_INFO             0x03                /**< Get thread information. */
#define RT_THREAD_CTRL_BIND_CPU         0x04                /**< Set thread bind cpu. */

可以

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已修改

@wycwyhwyq wycwyhwyq requested a review from BernardXiong as a code owner March 12, 2025 10:17
@github-actions github-actions bot added RT-Smart RT-Thread Smart related PR or issues component: lwp Component labels Mar 12, 2025
@mysterywolf
Copy link
Member

change prio 和 set prio这两个函数有什么区别呀,这个名字没有注释的话很不好区分

@wycwyhwyq
Copy link
Contributor Author

wycwyhwyq commented Mar 13, 2025

change prio 和 set prio这两个函数有什么区别呀,这个名字没有注释的话很不好区分

关于函数名字和注释,你们可以协商定一个告诉我或者直接修改。

@wycwyhwyq wycwyhwyq closed this Mar 13, 2025
@wycwyhwyq wycwyhwyq reopened this Mar 13, 2025
@Rbb666 Rbb666 requested a review from mysterywolf March 13, 2025 13:04
@Rbb666
Copy link
Member

Rbb666 commented Mar 17, 2025

@wycwyhwyq 看了下,rt_sched_thread_set_priorityrt_sched_thread_change_priority代码有大量雷同写法,建议规范下,可以参考下面的代码实现:

static void _rt_sched_update_priority(struct rt_thread *thread, rt_uint8_t priority, rt_bool_t update_init_prio)
{
    if (update_init_prio)
    {
        RT_SCHED_PRIV(thread).init_priority = priority;
    }
    /* change thread priority */
    RT_SCHED_PRIV(thread).current_priority = priority;

    /* recalculate priority attribute */
#if RT_THREAD_PRIORITY_MAX > 32
    RT_SCHED_PRIV(thread).number = RT_SCHED_PRIV(thread).current_priority >> 3;               /* 5bit */
    RT_SCHED_PRIV(thread).number_mask = 1 << RT_SCHED_PRIV(thread).number;
    RT_SCHED_PRIV(thread).high_mask = 1 << (RT_SCHED_PRIV(thread).current_priority & 0x07);   /* 3bit */
#else
    RT_SCHED_PRIV(thread).number_mask = 1 << RT_SCHED_PRIV(thread).current_priority;
#endif /* RT_THREAD_PRIORITY_MAX > 32 */
}

/**
 * @brief Reset priority of the target thread
 */
rt_err_t rt_sched_thread_reset_priority(struct rt_thread *thread, rt_uint8_t priority)
{
    RT_ASSERT(priority < RT_THREAD_PRIORITY_MAX);
    RT_SCHED_DEBUG_IS_LOCKED;

    /* for ready thread, change queue; otherwise simply update the priority */
    if ((RT_SCHED_CTX(thread).stat & RT_THREAD_STAT_MASK) == RT_THREAD_READY)
    {
        /* remove thread from schedule queue first */
        rt_sched_remove_thread(thread);
        _rt_sched_update_priority(thread, priority, RT_TRUE);
        RT_SCHED_CTX(thread).stat = RT_THREAD_INIT;
        /* insert thread to schedule queue again */
        rt_sched_insert_thread(thread);
    }
    else
    {
        _rt_sched_update_priority(thread, priority, RT_TRUE);
    }

    return RT_EOK;
}

/**
 * @brief Update priority of the target thread
 */
rt_err_t rt_sched_thread_change_priority(struct rt_thread *thread, rt_uint8_t priority)
{
    RT_ASSERT(priority < RT_THREAD_PRIORITY_MAX);
    RT_SCHED_DEBUG_IS_LOCKED;

    /* for ready thread, change queue; otherwise simply update the priority */
    if ((RT_SCHED_CTX(thread).stat & RT_THREAD_STAT_MASK) == RT_THREAD_READY)
    {
        /* remove thread from schedule queue first */
        rt_sched_remove_thread(thread);
        _rt_sched_update_priority(thread, priority, RT_FALSE);
        RT_SCHED_CTX(thread).stat = RT_THREAD_INIT;
        /* insert thread to schedule queue again */
        rt_sched_insert_thread(thread);
    }
    else
    {
        _rt_sched_update_priority(thread, priority, RT_FALSE);
    }

    return RT_EOK;
}

@Rbb666
Copy link
Member

Rbb666 commented Mar 17, 2025

change prio 和 set prio这两个函数有什么区别呀,这个名字没有注释的话很不好区分

关于函数名字和注释,你们可以协商定一个告诉我或者直接修改。

API命名上建议:rt_sched_thread_set_priority → rt_sched_thread_reset_priority,体现 "重置" 的含义,即既修改 init_priority 又修改 current_priority,表示线程的初始优先级和当前优先级都被更新。

注释这么写会好些
#define RT_THREAD_CTRL_RESET_PRIORITY 0x05 /** Reset thread initial and current priority. */

@mysterywolf
Copy link
Member

你好,我最近太忙了。我看了一下,rt_sched_thread_set_priority 和 rt_sched_thread_change_priority 很多逻辑是重复的,可否通过bool类型来区分一下逻辑。

unicornx pushed a commit to unicornx/rt-thread that referenced this pull request Apr 2, 2025
There is a PR submitted but has not been merged:
RT-Thread#10050.

The PR is a bit different against this patch.
unicornx pushed a commit to unicornx/rt-thread that referenced this pull request Apr 3, 2025
There is a PR submitted but has not been merged:
RT-Thread#10050.

The PR is a bit different against this patch.
unicornx pushed a commit to unicornx/rt-thread that referenced this pull request Apr 3, 2025
There is a PR submitted but has not been merged:
RT-Thread#10050.

The PR is a bit different against this patch.
@supperthomas supperthomas requested a review from Copilot April 10, 2025 03:55
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.

@Rbb666 Rbb666 closed this Apr 10, 2025
@Rbb666 Rbb666 reopened this Apr 10, 2025
@Rbb666 Rbb666 merged commit 12de72a into RT-Thread:master Apr 13, 2025
113 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: lwp Component Kernel PR has src relate code RT-Smart RT-Thread Smart related PR or issues
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants