Skip to content

[Bug] mutex causes task priority error #10049

Closed
@wycwyhwyq

Description

@wycwyhwyq

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;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions