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
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
FROM ossrs/srs:ubuntu16 AS ubuntu16-cross-aarch64
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

View file

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

2
trunk/configure vendored
View file

@ -199,7 +199,7 @@ MODULE_ID="CORE"
MODULE_DEPENDS=()
ModuleLibIncs=(${SRS_OBJS_DIR})
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_OBJS="${MODULE_OBJS[@]}"
#

View file

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

View file

@ -15,6 +15,8 @@
#include <srs_app_reload.hpp>
#include <srs_protocol_log.hpp>
class SrsMutex;
// For log TAGs.
#define TAG_MAIN "MAIN"
#define TAG_MAYBE "MAYBE"
@ -41,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.
pthread_mutex_t mutex_;
SrsMutex* mutex_;
public:
SrsFileLog();
virtual ~SrsFileLog();

View file

@ -26,6 +26,7 @@
#include <srs_app_srt_source.hpp>
#endif
#include <stdlib.h>
#include <string>
using namespace std;
@ -427,3 +428,38 @@ srs_error_t srs_thread_initialize()
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 <pthread.h>
// Protect server in high load.
class SrsCircuitBreaker : public ISrsFastTimer
{
@ -49,5 +51,28 @@ extern SrsCircuitBreaker* _srs_circuit_breaker;
// Initialize global or thread-local variables.
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

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