SRT: add srs_core_lock, support scope lock guard

pull/3089/head
hondaxiao 3 years ago committed by winlin
parent fd74b2f6e1
commit d03c6793b8

2
trunk/configure vendored

@ -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_time" "srs_core_platform" "srs_core_lock")
CORE_INCS="src/core"; MODULE_DIR=${CORE_INCS} . auto/modules.sh
CORE_OBJS="${MODULE_OBJS[@]}"
#

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

@ -0,0 +1,57 @@
//
// 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
Loading…
Cancel
Save