mirror of
https://github.com/ossrs/srs.git
synced 2025-03-09 15:49:59 +00:00
Thread: Refine SrsThreadMutex and SrsThreadLocker.
This commit is contained in:
parent
339d3b31cc
commit
cb6a4d0143
4 changed files with 56 additions and 41 deletions
|
@ -37,7 +37,7 @@ SrsFileLog::SrsFileLog()
|
|||
log_to_file_tank = false;
|
||||
utc = false;
|
||||
|
||||
mutex_ = new SrsMutex();
|
||||
mutex_ = new SrsThreadMutex();
|
||||
}
|
||||
|
||||
SrsFileLog::~SrsFileLog()
|
||||
|
@ -84,7 +84,7 @@ void SrsFileLog::reopen()
|
|||
|
||||
void SrsFileLog::verbose(const char* tag, SrsContextId context_id, const char* fmt, ...)
|
||||
{
|
||||
SrsAutoLock sl(mutex_);
|
||||
SrsThreadLocker(mutex_);
|
||||
|
||||
if (level > SrsLogLevelVerbose) {
|
||||
return;
|
||||
|
@ -106,7 +106,7 @@ void SrsFileLog::verbose(const char* tag, SrsContextId context_id, const char* f
|
|||
|
||||
void SrsFileLog::info(const char* tag, SrsContextId context_id, const char* fmt, ...)
|
||||
{
|
||||
SrsAutoLock sl(mutex_);
|
||||
SrsThreadLocker(mutex_);
|
||||
|
||||
if (level > SrsLogLevelInfo) {
|
||||
return;
|
||||
|
@ -128,7 +128,7 @@ void SrsFileLog::info(const char* tag, SrsContextId context_id, const char* fmt,
|
|||
|
||||
void SrsFileLog::trace(const char* tag, SrsContextId context_id, const char* fmt, ...)
|
||||
{
|
||||
SrsAutoLock sl(mutex_);
|
||||
SrsThreadLocker(mutex_);
|
||||
|
||||
if (level > SrsLogLevelTrace) {
|
||||
return;
|
||||
|
@ -150,7 +150,7 @@ void SrsFileLog::trace(const char* tag, SrsContextId context_id, const char* fmt
|
|||
|
||||
void SrsFileLog::warn(const char* tag, SrsContextId context_id, const char* fmt, ...)
|
||||
{
|
||||
SrsAutoLock sl(mutex_);
|
||||
SrsThreadLocker(mutex_);
|
||||
|
||||
if (level > SrsLogLevelWarn) {
|
||||
return;
|
||||
|
@ -172,7 +172,7 @@ void SrsFileLog::warn(const char* tag, SrsContextId context_id, const char* fmt,
|
|||
|
||||
void SrsFileLog::error(const char* tag, SrsContextId context_id, const char* fmt, ...)
|
||||
{
|
||||
SrsAutoLock sl(mutex_);
|
||||
SrsThreadLocker(mutex_);
|
||||
|
||||
if (level > SrsLogLevelError) {
|
||||
return;
|
||||
|
|
|
@ -15,7 +15,7 @@
|
|||
#include <srs_app_reload.hpp>
|
||||
#include <srs_protocol_log.hpp>
|
||||
|
||||
class SrsMutex;
|
||||
class SrsThreadMutex;
|
||||
|
||||
// For log TAGs.
|
||||
#define TAG_MAIN "MAIN"
|
||||
|
@ -43,7 +43,7 @@ private:
|
|||
bool utc;
|
||||
// TODO: FIXME: use macro define like SRS_MULTI_THREAD_LOG to switch enable log mutex or not.
|
||||
// Mutex for multithread log.
|
||||
SrsMutex* mutex_;
|
||||
SrsThreadMutex* mutex_;
|
||||
public:
|
||||
SrsFileLog();
|
||||
virtual ~SrsFileLog();
|
||||
|
|
|
@ -428,38 +428,43 @@ srs_error_t srs_thread_initialize()
|
|||
return err;
|
||||
}
|
||||
|
||||
SrsMutex::SrsMutex()
|
||||
SrsThreadMutex::SrsThreadMutex()
|
||||
{
|
||||
int rc = pthread_mutex_init(&mutex_, NULL);
|
||||
srs_assert(!rc);
|
||||
// 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
|
||||
r0 = pthread_mutex_init(&lock_, &attr_);
|
||||
srs_assert(!r0);
|
||||
}
|
||||
|
||||
SrsMutex::~SrsMutex()
|
||||
SrsThreadMutex::~SrsThreadMutex()
|
||||
{
|
||||
int rc = pthread_mutex_destroy(&mutex_);
|
||||
srs_assert(!rc);
|
||||
int r0 = pthread_mutex_destroy(&lock_);
|
||||
srs_assert(!r0);
|
||||
|
||||
r0 = pthread_mutexattr_destroy(&attr_);
|
||||
srs_assert(!r0);
|
||||
}
|
||||
|
||||
void SrsMutex::lock()
|
||||
void SrsThreadMutex::lock()
|
||||
{
|
||||
int rc = pthread_mutex_lock(&mutex_);
|
||||
srs_assert(!rc);
|
||||
// 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);
|
||||
}
|
||||
|
||||
void SrsMutex::unlock()
|
||||
void SrsThreadMutex::unlock()
|
||||
{
|
||||
int rc = pthread_mutex_unlock(&mutex_);
|
||||
srs_assert(!rc);
|
||||
}
|
||||
|
||||
SrsAutoLock::SrsAutoLock(SrsMutex* mutex)
|
||||
{
|
||||
mutex_ = mutex;
|
||||
mutex_->lock();
|
||||
}
|
||||
|
||||
SrsAutoLock::~SrsAutoLock()
|
||||
{
|
||||
mutex_->unlock();
|
||||
int r0 = pthread_mutex_unlock(&lock_);
|
||||
srs_assert(!r0);
|
||||
}
|
||||
|
||||
|
|
|
@ -51,27 +51,37 @@ extern SrsCircuitBreaker* _srs_circuit_breaker;
|
|||
// Initialize global or thread-local variables.
|
||||
extern srs_error_t srs_thread_initialize();
|
||||
|
||||
// Wrapper for mutex.
|
||||
class SrsMutex
|
||||
// The thread mutex wrapper, without error.
|
||||
class SrsThreadMutex
|
||||
{
|
||||
private:
|
||||
pthread_mutex_t mutex_;
|
||||
pthread_mutex_t lock_;
|
||||
pthread_mutexattr_t attr_;
|
||||
public:
|
||||
SrsMutex();
|
||||
~SrsMutex();
|
||||
SrsThreadMutex();
|
||||
virtual ~SrsThreadMutex();
|
||||
public:
|
||||
void lock();
|
||||
void unlock();
|
||||
};
|
||||
|
||||
// Lock the mutex when enter current scope, and unlock it when out.
|
||||
class SrsAutoLock
|
||||
// The thread mutex locker.
|
||||
// TODO: FIXME: Rename _SRS to _srs
|
||||
#define SrsThreadLocker(instance) \
|
||||
impl__SrsThreadLocker _SRS_free_##instance(instance)
|
||||
|
||||
class impl__SrsThreadLocker
|
||||
{
|
||||
private:
|
||||
SrsMutex* mutex_;
|
||||
SrsThreadMutex* lock;
|
||||
public:
|
||||
SrsAutoLock(SrsMutex* mutex);
|
||||
~SrsAutoLock();
|
||||
impl__SrsThreadLocker(SrsThreadMutex* l) {
|
||||
lock = l;
|
||||
lock->lock();
|
||||
}
|
||||
virtual ~impl__SrsThreadLocker() {
|
||||
lock->unlock();
|
||||
}
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue