1
0
Fork 0
mirror of https://github.com/ossrs/srs.git synced 2025-03-09 15:49:59 +00:00

SRT: Refine the lock for log.

This commit is contained in:
winlin 2022-06-14 19:13:14 +08:00
parent 910b5945af
commit 0957cdb944
9 changed files with 88 additions and 90 deletions

View file

@ -63,10 +63,10 @@ RUN cd /srs/trunk && ./configure --jobs=2 --srt=on && make -j2
######################################################## ########################################################
FROM ossrs/srs:ubuntu16 AS ubuntu16-cross-armv7 FROM ossrs/srs:ubuntu16 AS ubuntu16-cross-armv7
COPY . /srs COPY . /srs
RUN cd /srs/trunk && ./configure --jobs=2 --cross-build --cc=arm-linux-gnueabihf-gcc --cxx=arm-linux-gnueabihf-g++ \ RUN cd /srs/trunk && ./configure --jobs=2 --srt=off --cross-build --cc=arm-linux-gnueabihf-gcc --cxx=arm-linux-gnueabihf-g++ \
--ar=arm-linux-gnueabihf-ar --ld=arm-linux-gnueabihf-ld --randlib=arm-linux-gnueabihf-randlib && make -j2 --ar=arm-linux-gnueabihf-ar --ld=arm-linux-gnueabihf-ld --randlib=arm-linux-gnueabihf-randlib && make -j2
FROM ossrs/srs:ubuntu16 AS ubuntu16-cross-aarch64 FROM ossrs/srs:ubuntu16 AS ubuntu16-cross-aarch64
COPY . /srs COPY . /srs
RUN cd /srs/trunk && ./configure --jobs=2 --cross-build --cc=aarch64-linux-gnu-gcc --cxx=aarch64-linux-gnu-g++ \ RUN cd /srs/trunk && ./configure --jobs=2 --srt=off --cross-build --cc=aarch64-linux-gnu-gcc --cxx=aarch64-linux-gnu-g++ \
--ar=aarch64-linux-gnu-ar --ld=aarch64-linux-gnu-ld --randlib=aarch64-linux-gnu-randlib && make -j2 --ar=aarch64-linux-gnu-ar --ld=aarch64-linux-gnu-ld --randlib=aarch64-linux-gnu-randlib && make -j2

View file

@ -4,7 +4,7 @@
help=no help=no
# feature options # feature options
SRS_HDS=NO SRS_HDS=NO
SRS_SRT=NO SRS_SRT=YES
SRS_RTC=YES SRS_RTC=YES
SRS_CXX11=YES SRS_CXX11=YES
SRS_CXX14=NO SRS_CXX14=NO

2
trunk/configure vendored
View file

@ -199,7 +199,7 @@ MODULE_ID="CORE"
MODULE_DEPENDS=() MODULE_DEPENDS=()
ModuleLibIncs=(${SRS_OBJS_DIR}) ModuleLibIncs=(${SRS_OBJS_DIR})
MODULE_FILES=("srs_core" "srs_core_version5" "srs_core_autofree" "srs_core_performance" MODULE_FILES=("srs_core" "srs_core_version5" "srs_core_autofree" "srs_core_performance"
"srs_core_time" "srs_core_platform" "srs_core_lock") "srs_core_time" "srs_core_platform")
CORE_INCS="src/core"; MODULE_DIR=${CORE_INCS} . auto/modules.sh CORE_INCS="src/core"; MODULE_DIR=${CORE_INCS} . auto/modules.sh
CORE_OBJS="${MODULE_OBJS[@]}" CORE_OBJS="${MODULE_OBJS[@]}"
# #

View file

@ -18,7 +18,7 @@
#include <srs_kernel_error.hpp> #include <srs_kernel_error.hpp>
#include <srs_app_utility.hpp> #include <srs_app_utility.hpp>
#include <srs_kernel_utility.hpp> #include <srs_kernel_utility.hpp>
#include <srs_core_lock.hpp> #include <srs_app_threads.hpp>
// the max size of a line of log. // the max size of a line of log.
#define LOG_MAX_SIZE 8192 #define LOG_MAX_SIZE 8192
@ -37,7 +37,7 @@ SrsFileLog::SrsFileLog()
log_to_file_tank = false; log_to_file_tank = false;
utc = false; utc = false;
pthread_mutex_init(&mutex_, NULL); mutex_ = new SrsMutex();
} }
SrsFileLog::~SrsFileLog() SrsFileLog::~SrsFileLog()
@ -53,7 +53,7 @@ SrsFileLog::~SrsFileLog()
_srs_config->unsubscribe(this); _srs_config->unsubscribe(this);
} }
pthread_mutex_destroy(&mutex_); srs_freep(mutex_);
} }
srs_error_t SrsFileLog::initialize() srs_error_t SrsFileLog::initialize()
@ -84,95 +84,95 @@ void SrsFileLog::reopen()
void SrsFileLog::verbose(const char* tag, SrsContextId context_id, const char* fmt, ...) void SrsFileLog::verbose(const char* tag, SrsContextId context_id, const char* fmt, ...)
{ {
SrsScopeLock sl(&mutex_); SrsAutoLock sl(mutex_);
if (level > SrsLogLevelVerbose) { if (level > SrsLogLevelVerbose) {
return; return;
} }
int size = 0; int size = 0;
if (!srs_log_header(log_data, LOG_MAX_SIZE, utc, false, tag, context_id, "Verb", &size)) { if (!srs_log_header(log_data, LOG_MAX_SIZE, utc, false, tag, context_id, "Verb", &size)) {
return; return;
} }
va_list ap; va_list ap;
va_start(ap, fmt); va_start(ap, fmt);
// we reserved 1 bytes for the new line. // we reserved 1 bytes for the new line.
size += vsnprintf(log_data + size, LOG_MAX_SIZE - size, fmt, ap); size += vsnprintf(log_data + size, LOG_MAX_SIZE - size, fmt, ap);
va_end(ap); va_end(ap);
write_log(fd, log_data, size, SrsLogLevelVerbose); write_log(fd, log_data, size, SrsLogLevelVerbose);
} }
void SrsFileLog::info(const char* tag, SrsContextId context_id, const char* fmt, ...) void SrsFileLog::info(const char* tag, SrsContextId context_id, const char* fmt, ...)
{ {
SrsScopeLock sl(&mutex_); SrsAutoLock sl(mutex_);
if (level > SrsLogLevelInfo) { if (level > SrsLogLevelInfo) {
return; return;
} }
int size = 0; int size = 0;
if (!srs_log_header(log_data, LOG_MAX_SIZE, utc, false, tag, context_id, "Debug", &size)) { if (!srs_log_header(log_data, LOG_MAX_SIZE, utc, false, tag, context_id, "Debug", &size)) {
return; return;
} }
va_list ap; va_list ap;
va_start(ap, fmt); va_start(ap, fmt);
// we reserved 1 bytes for the new line. // we reserved 1 bytes for the new line.
size += vsnprintf(log_data + size, LOG_MAX_SIZE - size, fmt, ap); size += vsnprintf(log_data + size, LOG_MAX_SIZE - size, fmt, ap);
va_end(ap); va_end(ap);
write_log(fd, log_data, size, SrsLogLevelInfo); write_log(fd, log_data, size, SrsLogLevelInfo);
} }
void SrsFileLog::trace(const char* tag, SrsContextId context_id, const char* fmt, ...) void SrsFileLog::trace(const char* tag, SrsContextId context_id, const char* fmt, ...)
{ {
SrsScopeLock sl(&mutex_); SrsAutoLock sl(mutex_);
if (level > SrsLogLevelTrace) { if (level > SrsLogLevelTrace) {
return; return;
} }
int size = 0; int size = 0;
if (!srs_log_header(log_data, LOG_MAX_SIZE, utc, false, tag, context_id, "Trace", &size)) { if (!srs_log_header(log_data, LOG_MAX_SIZE, utc, false, tag, context_id, "Trace", &size)) {
return; return;
} }
va_list ap; va_list ap;
va_start(ap, fmt); va_start(ap, fmt);
// we reserved 1 bytes for the new line. // we reserved 1 bytes for the new line.
size += vsnprintf(log_data + size, LOG_MAX_SIZE - size, fmt, ap); size += vsnprintf(log_data + size, LOG_MAX_SIZE - size, fmt, ap);
va_end(ap); va_end(ap);
write_log(fd, log_data, size, SrsLogLevelTrace); write_log(fd, log_data, size, SrsLogLevelTrace);
} }
void SrsFileLog::warn(const char* tag, SrsContextId context_id, const char* fmt, ...) void SrsFileLog::warn(const char* tag, SrsContextId context_id, const char* fmt, ...)
{ {
SrsScopeLock sl(&mutex_); SrsAutoLock sl(mutex_);
if (level > SrsLogLevelWarn) { if (level > SrsLogLevelWarn) {
return; return;
} }
int size = 0; int size = 0;
if (!srs_log_header(log_data, LOG_MAX_SIZE, utc, true, tag, context_id, "Warn", &size)) { if (!srs_log_header(log_data, LOG_MAX_SIZE, utc, true, tag, context_id, "Warn", &size)) {
return; return;
} }
va_list ap; va_list ap;
va_start(ap, fmt); va_start(ap, fmt);
// we reserved 1 bytes for the new line. // we reserved 1 bytes for the new line.
size += vsnprintf(log_data + size, LOG_MAX_SIZE - size, fmt, ap); size += vsnprintf(log_data + size, LOG_MAX_SIZE - size, fmt, ap);
va_end(ap); va_end(ap);
write_log(fd, log_data, size, SrsLogLevelWarn); write_log(fd, log_data, size, SrsLogLevelWarn);
} }
void SrsFileLog::error(const char* tag, SrsContextId context_id, const char* fmt, ...) void SrsFileLog::error(const char* tag, SrsContextId context_id, const char* fmt, ...)
{ {
SrsScopeLock sl(&mutex_); SrsAutoLock sl(mutex_);
if (level > SrsLogLevelError) { if (level > SrsLogLevelError) {
return; return;

View file

@ -15,6 +15,8 @@
#include <srs_app_reload.hpp> #include <srs_app_reload.hpp>
#include <srs_protocol_log.hpp> #include <srs_protocol_log.hpp>
class SrsMutex;
// For log TAGs. // For log TAGs.
#define TAG_MAIN "MAIN" #define TAG_MAIN "MAIN"
#define TAG_MAYBE "MAYBE" #define TAG_MAYBE "MAYBE"
@ -41,7 +43,7 @@ private:
bool utc; bool utc;
// TODO: FIXME: use macro define like SRS_MULTI_THREAD_LOG to switch enable log mutex or not. // TODO: FIXME: use macro define like SRS_MULTI_THREAD_LOG to switch enable log mutex or not.
// Mutex for multithread log. // Mutex for multithread log.
pthread_mutex_t mutex_; SrsMutex* mutex_;
public: public:
SrsFileLog(); SrsFileLog();
virtual ~SrsFileLog(); virtual ~SrsFileLog();

View file

@ -26,6 +26,7 @@
#include <srs_app_srt_source.hpp> #include <srs_app_srt_source.hpp>
#endif #endif
#include <stdlib.h>
#include <string> #include <string>
using namespace std; using namespace std;
@ -427,3 +428,38 @@ srs_error_t srs_thread_initialize()
return err; return err;
} }
SrsMutex::SrsMutex()
{
int rc = pthread_mutex_init(&mutex_, NULL);
srs_assert(!rc);
}
SrsMutex::~SrsMutex()
{
int rc = pthread_mutex_destroy(&mutex_);
srs_assert(!rc);
}
void SrsMutex::lock()
{
int rc = pthread_mutex_lock(&mutex_);
srs_assert(!rc);
}
void SrsMutex::unlock()
{
int rc = pthread_mutex_unlock(&mutex_);
srs_assert(!rc);
}
SrsAutoLock::SrsAutoLock(SrsMutex* mutex)
{
mutex_ = mutex;
mutex_->lock();
}
SrsAutoLock::~SrsAutoLock()
{
mutex_->unlock();
}

View file

@ -11,6 +11,8 @@
#include <srs_app_hourglass.hpp> #include <srs_app_hourglass.hpp>
#include <pthread.h>
// Protect server in high load. // Protect server in high load.
class SrsCircuitBreaker : public ISrsFastTimer class SrsCircuitBreaker : public ISrsFastTimer
{ {
@ -49,5 +51,28 @@ extern SrsCircuitBreaker* _srs_circuit_breaker;
// Initialize global or thread-local variables. // Initialize global or thread-local variables.
extern srs_error_t srs_thread_initialize(); extern srs_error_t srs_thread_initialize();
// Wrapper for mutex.
class SrsMutex
{
private:
pthread_mutex_t mutex_;
public:
SrsMutex();
~SrsMutex();
public:
void lock();
void unlock();
};
// Lock the mutex when enter current scope, and unlock it when out.
class SrsAutoLock
{
private:
SrsMutex* mutex_;
public:
SrsAutoLock(SrsMutex* mutex);
~SrsAutoLock();
};
#endif #endif

View file

@ -1,8 +0,0 @@
//
// Copyright (c) 2013-2021 The SRS Authors
//
// SPDX-License-Identifier: MIT or MulanPSL-2.0
//
#include <srs_core_autofree.hpp>

View file

@ -1,57 +0,0 @@
//
// Copyright (c) 2013-2021 The SRS Authors
//
// SPDX-License-Identifier: MIT or MulanPSL-2.0
//
#ifndef SRS_CORE_LOCK_HPP
#define SRS_CORE_LOCK_HPP
#include <pthread.h>
#include <srs_core.hpp>
#include <stdlib.h>
class SrsScopeLock
{
private:
pthread_mutex_t* mutex_;
bool locked_;
public:
explicit SrsScopeLock(pthread_mutex_t* mutex)
{
mutex_ = mutex;
locked_ = false;
lock();
}
~SrsScopeLock()
{
unlock();
}
void lock()
{
if (locked_) {
return;
}
locked_ = true;
pthread_mutex_lock(mutex_);
}
void unlock()
{
if (! locked_) {
return;
}
locked_ = false;
pthread_mutex_unlock(mutex_);
}
};
#endif