Skip to content

Commit

Permalink
Threads-Log: Refine thread lock type to ERRORCHECK.
Browse files Browse the repository at this point in the history
1. Set lock attribute type to PTHREAD_MUTEX_ERRORCHECK.
2. If dead lock, the pthread_mutex_lock return EDEADLK.
3. We assert fail if lock failed.
  • Loading branch information
winlinvip committed Apr 26, 2021
1 parent 668c9c1 commit 0cea382
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
30 changes: 26 additions & 4 deletions trunk/src/app/srs_app_threads.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,34 @@ using namespace std;

SrsThreadMutex::SrsThreadMutex()
{
// https://man7.org/linux/man-pages/man3/pthread_mutexattr_init.3.html
int r0 = pthread_mutexattr_init(&attr_);
srs_assert(!r0);

// https://man7.org/linux/man-pages/man3/pthread_mutexattr_gettype.3p.html
r0 = pthread_mutexattr_settype(&attr_, PTHREAD_MUTEX_ERRORCHECK);
srs_assert(!r0);

// https://michaelkerrisk.com/linux/man-pages/man3/pthread_mutex_init.3p.html
int r0 = pthread_mutex_init(&lock_, NULL);
r0 = pthread_mutex_init(&lock_, &attr_);
srs_assert(!r0);
}

SrsThreadMutex::~SrsThreadMutex()
{
int r0 = pthread_mutex_destroy(&lock_);
srs_assert(!r0);

r0 = pthread_mutexattr_destroy(&attr_);
srs_assert(!r0);
}

void SrsThreadMutex::lock()
{
// https://man7.org/linux/man-pages/man3/pthread_mutex_lock.3p.html
// EDEADLK
// The mutex type is PTHREAD_MUTEX_ERRORCHECK and the current
// thread already owns the mutex.
int r0 = pthread_mutex_lock(&lock_);
srs_assert(!r0);
}
Expand All @@ -68,6 +82,10 @@ SrsThreadEntry::SrsThreadEntry()
err = srs_success;
}

SrsThreadEntry::~SrsThreadEntry()
{
}

SrsThreadPool::SrsThreadPool()
{
entry_ = NULL;
Expand Down Expand Up @@ -108,10 +126,9 @@ srs_error_t SrsThreadPool::execute(string label, srs_error_t (*start)(void* arg)
{
srs_error_t err = srs_success;

static int num = entry_->num + 1;

SrsThreadEntry* entry = new SrsThreadEntry();

// To protect the threads_ for executing thread-safe.
if (true) {
SrsThreadLocker(lock_);
threads_.push_back(entry);
Expand All @@ -121,13 +138,18 @@ srs_error_t SrsThreadPool::execute(string label, srs_error_t (*start)(void* arg)
entry->label = label;
entry->start = start;
entry->arg = arg;

// The id of thread, should equal to the debugger thread id.
// For gdb, it's: info threads
// For lldb, it's: thread list
static int num = entry_->num + 1;
entry->num = num++;

// https://man7.org/linux/man-pages/man3/pthread_create.3.html
pthread_t trd;
int r0 = pthread_create(&trd, NULL, SrsThreadPool::start, entry);
if (r0 != 0) {
entry->err = srs_error_new(ERROR_THREAD_CREATE, "create thread %s", label.c_str());
entry->err = srs_error_new(ERROR_THREAD_CREATE, "create thread %s, r0=%d", label.c_str(), r0);
return srs_error_copy(entry->err);
}

Expand Down
4 changes: 3 additions & 1 deletion trunk/src/app/srs_app_threads.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class SrsThreadMutex
{
private:
pthread_mutex_t lock_;
pthread_mutexattr_t attr_;
public:
SrsThreadMutex();
virtual ~SrsThreadMutex();
Expand Down Expand Up @@ -78,8 +79,9 @@ class SrsThreadEntry
pthread_t trd;
// The exit error of thread.
srs_error_t err;

public:
SrsThreadEntry();
virtual ~SrsThreadEntry();
};

// Allocate a(or almost) fixed thread poll to execute tasks,
Expand Down

0 comments on commit 0cea382

Please sign in to comment.