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

[Bug] mutex causes task priority error #10049

Open
wycwyhwyq opened this issue Feb 27, 2025 · 2 comments
Open

[Bug] mutex causes task priority error #10049

wycwyhwyq opened this issue Feb 27, 2025 · 2 comments

Comments

@wycwyhwyq
Copy link
Contributor

wycwyhwyq commented Feb 27, 2025

RT-Thread Version

58e587b

Hardware Type/Architectures

risc-v/K230

Develop Toolchain

GCC

Describe the bug

创建两个线程,其中一个设置为高优先级,两个线程同时获取mutex时,会导致高优先级线程被重新改为默认优先级。具体复现case见代码。

Other additional context

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <pthread.h>
#include <unistd.h>
#include <errno.h>
#include <time.h>

#define rt_mutex_create  22
#define rt_mutex_delete  23
#define rt_mutex_take    24
#define rt_mutex_release 25

pthread_t tid;
void* mutex;

static void* subthread(void* arg)
{
    int ret;
    int policy;
    struct sched_param param;

    ret = pthread_getschedparam(pthread_self(), &policy, &param);
    printf("%s get prio: %d, %d\n", __func__, ret, param.sched_priority);

    param.sched_priority = 0;
    ret = pthread_setschedparam(pthread_self(), policy, &param);

    ret = pthread_getschedparam(pthread_self(), &policy, &param);
    printf("%s get prio: %d, %d\n", __func__, ret, param.sched_priority);

    syscall(rt_mutex_take, mutex, -1);
    sleep(1);
    syscall(rt_mutex_release, mutex);

    ret = pthread_getschedparam(pthread_self(), &policy, &param);
    printf("%s get prio: %d, %d\n", __func__, ret, param.sched_priority);

    return 0;
}

int main(int argc, char** argv)
{
    int ret;
    int policy;
    struct sched_param param;

    if (argc != 2) {
        printf("0: test main thread\n1: test subthread\n");
        return -1;
    }

    mutex = (void*)syscall(rt_mutex_create, "driver_test", 0);

    ret = pthread_getschedparam(pthread_self(), &policy, &param);
    printf("%s get prio: %d, %d\n", __func__, ret, param.sched_priority);

    param.sched_priority = 0;
    ret = pthread_setschedparam(pthread_self(), policy, &param);

    ret = pthread_getschedparam(pthread_self(), &policy, &param);
    printf("%s get prio: %d, %d\n", __func__, ret, param.sched_priority);

    pthread_create(&tid, NULL, subthread, 0);

    if (atoi(argv[1]))
        usleep(100000);
    syscall(rt_mutex_take, mutex, -1);
    sleep(1);
    syscall(rt_mutex_release, mutex);

    ret = pthread_getschedparam(pthread_self(), &policy, &param);
    printf("%s get prio: %d, %d\n", __func__, ret, param.sched_priority);

    pthread_join(tid, 0);
    syscall(rt_mutex_delete, mutex);

    return 0;
}
@wycwyhwyq wycwyhwyq mentioned this issue Feb 27, 2025
10 tasks
@BernardXiong
Copy link
Member

不是,这个为什么是混用呢,为什么不是如果是rt_mutex的,就在内核中直接使用;如果是pthread_mutex,就按pthread的方式来。

@wycwyhwyq
Copy link
Contributor Author

wycwyhwyq commented Feb 28, 2025

不是,这个为什么是混用呢,为什么不是如果是rt_mutex的,就在内核中直接使用;如果是pthread_mutex,就按pthread的方式来。

用户程序使用pthread_mutex完全没有问题,上述case只是一个模拟,比如某些设备驱动在kernel中调用了rt_mutex_take,那对应的用户态线程使用了这个设备也相当于使用了rt_mutex。
这个问题不只是在用户态会存在,kernel中的task也会存在同样的问题,如果task在运行过程中调用rt_thread_control来改变优先级,在使用rt_mutex后,也会导致task的优先级被重新调整为init_priority。
@BernardXiong

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants