Skip to content

SMP下中断与互斥锁同时使用存在死锁的场景 #8493

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

Closed
Yaochenger opened this issue Jan 16, 2024 · 1 comment · Fixed by #8537
Closed

SMP下中断与互斥锁同时使用存在死锁的场景 #8493

Yaochenger opened this issue Jan 16, 2024 · 1 comment · Fixed by #8537
Assignees
Labels
bug This PR/issue is a bug in the current code. v5.1.0 version 5.1.0 (planned to be released by the end of 2023)

Comments

@Yaochenger
Copy link
Contributor

  • [ 问题 ] SMP模式下中断与互斥锁同时使用的存在死锁,_cpus_lock死锁

  • [ 测试版本 ] 主线最新版本

  • [ 测试BSP ] qemu-vexpress-a9

  • [ 测试用例 ]

#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <rtthread.h>
#include <rthw.h>
#define NUM 1000000
#define TASK_NUM 3

static volatile unsigned long long count = 0;
static int ac = 0;
static rt_mutex_t test_mutex;

static void test_thread(void *param)
{
    for (int i = 0; i < NUM; i++) {
        rt_mutex_take(test_mutex, RT_WAITING_FOREVER);
        ++count;
        rt_mutex_release(test_mutex);
    }
    printf("count: %llu\n", count);
}

int main(void)
{
    rt_thread_t threads[TASK_NUM];
    test_mutex = rt_mutex_create("test_mutex", RT_IPC_FLAG_PRIO);

    for (int i = 0; i < TASK_NUM; i++) {
        char buf[64];
        rt_snprintf(buf, sizeof(buf), "test%d", i);
        threads[i] = rt_thread_create(buf, test_thread, 0, 2048, 20+i, 10u);
        rt_thread_startup(threads[i]);
    }

    while(1) {
        if (count == (NUM * TASK_NUM)) {
            printf("finish test...\n");
            rt_thread_mdelay(5000);
            continue;
        }
        if (++ac % 5 == 0) {
            printf("count: %llu\n", count);
        }
        rt_thread_mdelay(100);
    }
    return 0;
}
  • [ 临时解决办法 ]
    以下是目前探索找到的临时的解决办法:
    -等待一定时间获取不到锁主动释放
    -在互斥锁获取与释放之间添加rt_thread_mdelay
@Yaochenger Yaochenger changed the title SMP下中断与互斥锁同时使用的存在死锁的场景 SMP下中断与互斥锁同时使用存在死锁的场景 Jan 16, 2024
@Rbb666 Rbb666 added the bug This PR/issue is a bug in the current code. label Jan 16, 2024
@polarvid
Copy link
Contributor

看上去更像是调度器的冲突区保护存在问题。

@Rbb666 Rbb666 added the v5.1.0 version 5.1.0 (planned to be released by the end of 2023) label Jan 22, 2024
@mysterywolf mysterywolf linked a pull request Feb 20, 2024 that will close this issue
9 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This PR/issue is a bug in the current code. v5.1.0 version 5.1.0 (planned to be released by the end of 2023)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants