Skip to content

Commit

Permalink
SRT: Refine the lock for log.
Browse files Browse the repository at this point in the history
  • Loading branch information
winlinvip committed Jun 14, 2022
1 parent edc291a commit d7dcf6d
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 90 deletions.
4 changes: 2 additions & 2 deletions trunk/Dockerfile.builds
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion trunk/auto/options.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion trunk/configure
Original file line number Diff line number Diff line change
Expand Up @@ -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[@]}"
#
Expand Down
40 changes: 20 additions & 20 deletions trunk/src/app/srs_app_log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -37,7 +37,7 @@ SrsFileLog::SrsFileLog()
log_to_file_tank = false;
utc = false;

pthread_mutex_init(&mutex_, NULL);
mutex_ = new SrsMutex();
}

SrsFileLog::~SrsFileLog()
Expand All @@ -53,7 +53,7 @@ SrsFileLog::~SrsFileLog()
_srs_config->unsubscribe(this);
}

pthread_mutex_destroy(&mutex_);
srs_freep(mutex_);
}

srs_error_t SrsFileLog::initialize()
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 3 additions & 1 deletion trunk/src/app/srs_app_log.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -41,7 +43,7 @@ class SrsFileLog : public ISrsLog, public ISrsReloadHandler
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();
Expand Down
36 changes: 36 additions & 0 deletions trunk/src/app/srs_app_threads.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <srs_app_srt_source.hpp>
#endif

#include <stdlib.h>
#include <string>
using namespace std;

Expand Down Expand Up @@ -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();
}

25 changes: 25 additions & 0 deletions trunk/src/app/srs_app_threads.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

#include <srs_app_hourglass.hpp>

#include <pthread.h>

// Protect server in high load.
class SrsCircuitBreaker : public ISrsFastTimer
{
Expand Down Expand Up @@ -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

8 changes: 0 additions & 8 deletions trunk/src/core/srs_core_lock.cpp

This file was deleted.

57 changes: 0 additions & 57 deletions trunk/src/core/srs_core_lock.hpp

This file was deleted.

0 comments on commit d7dcf6d

Please sign in to comment.