mardi 22 octobre 2019

Read-write locks are used inside real-time threads to cause hang to live

//this is demo code,

// 1、I create two threads, one real-time thread, one normal thread; both threads have read-write locks, and we bind two threads to one CPU core to run; // 2、Two threads will hang

#include <sys/prctl.h>
#include <sched.h>
#include <stdio.h>
#include <sys/syscall.h>
#include <unistd.h>
#include <errno.h>
#include <pthread.h>

#include <iostream>
#include <thread>

pthread_rwlock_t qlock;


inline int set_cpu(int i)
{
    cpu_set_t mask;
    CPU_ZERO(&mask);

    CPU_SET(i,&mask);

    printf("thread %u, i = %d\n", pthread_self(), i);
    if(-1 == pthread_setaffinity_np(pthread_self() ,sizeof(mask),&mask))
    {
        fprintf(stderr, "pthread_setaffinity_np erro\n");
        return -1;
    }
    return 0;
}


int32_t set_rt_thread(const char *thread_name, uint32_t prio)
{
    struct sched_param cfg_param;
    int32_t ret = 0;

    if (prio > 99)
    {
        printf("%s rt prio is too big, %u.\n", thread_name, prio);
        return -EINVAL;
    }

    cfg_param.sched_priority = prio;
    ret = sched_setscheduler(0, SCHED_RR, &cfg_param);
    printf("Cur thread new scheduler = %d.\n", sched_getscheduler(0));
    if(ret < 0)
    {
        printf("%s sched_set scheduler to SCHED_RR error.\n", thread_name);
    }
    else
    {
        printf("%s tid=%ld is rt thread now.\n", thread_name, syscall(SYS_gettid));
    }

    return 0;
}

int32_t set_thread_name(const char *name)
strong text{
    return prctl(PR_SET_NAME, name);
}
//thread 1
void thead_input() {
        std::string thread_name = "thead_input";
        set_thread_name(thread_name.c_str());
        set_rt_thread(thread_name.c_str(),90);

        set_cpu(5);
        while(1) {
                pthread_rwlock_wrlock(&qlock);
                printf("thead 1\n");

                pthread_rwlock_unlock(&qlock);
                printf("thread unlock 2\n");
                usleep(100);
        }
}
//thread 2
void thead_output() {
        std::string thread_name = "thead_output";
        set_cpu(5);
        set_thread_name(thread_name.c_str());
        set_rt_thread(thread_name.c_str(),99);


        while(1) {
                pthread_rwlock_wrlock(&qlock);
                std::cout<<"thead 2"<<std::endl;
                pthread_rwlock_unlock(&qlock);
                usleep(100);
        }
}
//main function
int main(int argc, char **argv) {
        if(pthread_rwlock_init(&qlock, NULL) != 0) {
                std::cout<<"pthread rw lock init fail\n"<<std::endl;
                return -1;
        }

        std::thread t1(thead_input);
        std::thread t2(thead_output);

        t1.join();
        t2.join();

    return 0;
}

//this is debug log

[Thread debugging using libthread_db enabled] Using host libthread_db library "/lib/aarch64-linux-gnu/libthread_db.so.1". 0x0000007f899de31c in __GI___pthread_timedjoin_ex (threadid=547765662160, thread_return=0x0, abstime=0x0, block=) at pthread_join_common.c:89 89 pthread_join_common.c: No such file or directory. (gdb) thread apply all bt

Thread 3 (Thread 0x7f88e0a1d0 (LWP 21439)):

0 __pthread_rwlock_wrlock_full (abstime=0x0, rwlock=0x5577ff2020 ) at pthread_rwlock_common.c:679

1 __GI___pthread_rwlock_wrlock (rwlock=0x5577ff2020 ) at pthread_rwlock_wrlock.c:27

2 0x0000005577fdf8b8 in thead_output () at main.cpp:88

3 0x0000005577fdff64 in std::__invoke_impl (__f=@0x55a376afc8: 0x5577fdf844 )

at /usr/include/c++/7/bits/invoke.h:60

4 0x0000005577fdfd30 in std::__invoke (__fn=@0x55a376afc8: 0x5577fdf844 )

at /usr/include/c++/7/bits/invoke.h:95

5 0x0000005577fe04cc in std::thread::_Invoker >::_M_invoke<0ul> (this=0x55a376afc8)

at /usr/include/c++/7/thread:234

6 0x0000005577fe0480 in std::thread::_Invoker >::operator() (this=0x55a376afc8)

at /usr/include/c++/7/thread:243

7 0x0000005577fe044c in std::thread::_State_impl

::_M_run ( this=0x55a376afc0) at /usr/include/c++/7/thread:186

8 0x0000007f898fee14 in ?? () from /usr/lib/aarch64-linux-gnu/libstdc++.so.6

9 0x0000007f899dd088 in start_thread (arg=0x7ff794cc5f) at pthread_create.c:463

10 0x0000007f897964ec in thread_start () at ../sysdeps/unix/sysv/linux/aarch64/clone.S:78

Thread 2 (Thread 0x7f8960b1d0 (LWP 21438)):

0 0x0000007f899e1ef8 in __pthread_rwlock_wrlock_full (abstime=0x0, rwlock=0x5577ff2020 )

at pthread_rwlock_common.c:595

1 __GI___pthread_rwlock_wrlock (rwlock=0x5577ff2020 ) at pthread_rwlock_wrlock.c:27

2 0x0000005577fdf7ec in thead_input () at main.cpp:71

3 0x0000005577fdff64 in std::__invoke_impl (__f=@0x55a376ae78: 0x5577fdf788 )

at /usr/include/c++/7/bits/invoke.h:60

4 0x0000005577fdfd30 in std::__invoke (__fn=@0x55a376ae78: 0x5577fdf788 )

at /usr/include/c++/7/bits/invoke.h:95

5 0x0000005577fe04cc in std::thread::_Invoker >::_M_invoke<0ul> (this=0x55a376ae78)

at /usr/include/c++/7/thread:234

6 0x0000005577fe0480 in std::thread::_Invoker >::operator() (this=0x55a376ae78)

at /usr/include/c++/7/thread:243

7 0x0000005577fe044c in std::thread::_State_impl

::_M_run ( this=0x55a376ae70) at /usr/include/c++/7/thread:186

8 0x0000007f898fee14 in ?? () from /usr/lib/aarch64-linux-gnu/libstdc++.so.6

9 0x0000007f899dd088 in start_thread (arg=0x7ff794cc5f) at pthread_create.c:463

10 0x0000007f897964ec in thread_start () at ../sysdeps/unix/sysv/linux/aarch64/clone.S:78

Thread 1 (Thread 0x7f89a75ce0 (LWP 21436)):

0 0x0000007f899de31c in __GI___pthread_timedjoin_ex (threadid=547765662160, thread_return=0x0, abstime=0x0,

block=<optimized out>) at pthread_join_common.c:89

1 0x0000007f898ff0a8 in std::thread::join() () from /usr/lib/aarch64-linux-gnu/libstdc++.so.6

2 0x0000005577fdf9cc in main (argc=1, argv=0x7ff794ce98) at main.cpp:104

Aucun commentaire:

Enregistrer un commentaire