mirror of
https://github.com/ossrs/srs.git
synced 2025-02-13 11:51:57 +00:00
SRT: add srs_core_lock, support scope lock guard
This commit is contained in:
parent
fd74b2f6e1
commit
d03c6793b8
3 changed files with 66 additions and 1 deletions
2
trunk/configure
vendored
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[@]}"
|
||||
#
|
||||
|
|
8
trunk/src/core/srs_core_lock.cpp
Normal file
8
trunk/src/core/srs_core_lock.cpp
Normal file
|
@ -0,0 +1,8 @@
|
|||
//
|
||||
// Copyright (c) 2013-2021 The SRS Authors
|
||||
//
|
||||
// SPDX-License-Identifier: MIT or MulanPSL-2.0
|
||||
//
|
||||
|
||||
#include <srs_core_autofree.hpp>
|
||||
|
57
trunk/src/core/srs_core_lock.hpp
Normal file
57
trunk/src/core/srs_core_lock.hpp
Normal file
|
@ -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…
Reference in a new issue