Skip to content

Commit b7aad03

Browse files
committed
SRT: add srs_core_lock, support scope lock guard
1 parent 9d97402 commit b7aad03

File tree

3 files changed

+66
-1
lines changed

3 files changed

+66
-1
lines changed

trunk/configure

+1-1
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ MODULE_ID="CORE"
199199
MODULE_DEPENDS=()
200200
ModuleLibIncs=(${SRS_OBJS_DIR})
201201
MODULE_FILES=("srs_core" "srs_core_version5" "srs_core_autofree" "srs_core_performance"
202-
"srs_core_time" "srs_core_platform")
202+
"srs_core_time" "srs_core_platform" "srs_core_lock")
203203
CORE_INCS="src/core"; MODULE_DIR=${CORE_INCS} . auto/modules.sh
204204
CORE_OBJS="${MODULE_OBJS[@]}"
205205
#

trunk/src/core/srs_core_lock.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
//
2+
// Copyright (c) 2013-2021 The SRS Authors
3+
//
4+
// SPDX-License-Identifier: MIT or MulanPSL-2.0
5+
//
6+
7+
#include <srs_core_autofree.hpp>
8+

trunk/src/core/srs_core_lock.hpp

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
//
2+
// Copyright (c) 2013-2021 The SRS Authors
3+
//
4+
// SPDX-License-Identifier: MIT or MulanPSL-2.0
5+
//
6+
7+
#ifndef SRS_CORE_LOCK_HPP
8+
#define SRS_CORE_LOCK_HPP
9+
10+
#include <pthread.h>
11+
12+
#include <srs_core.hpp>
13+
14+
#include <stdlib.h>
15+
16+
class SrsScopeLock
17+
{
18+
private:
19+
pthread_mutex_t* mutex_;
20+
bool locked_;
21+
22+
public:
23+
explicit SrsScopeLock(pthread_mutex_t* mutex)
24+
{
25+
mutex_ = mutex;
26+
locked_ = false;
27+
28+
lock();
29+
}
30+
31+
~SrsScopeLock()
32+
{
33+
unlock();
34+
}
35+
36+
void lock()
37+
{
38+
if (locked_) {
39+
return;
40+
}
41+
42+
locked_ = true;
43+
pthread_mutex_lock(mutex_);
44+
}
45+
46+
void unlock()
47+
{
48+
if (! locked_) {
49+
return;
50+
}
51+
52+
locked_ = false;
53+
pthread_mutex_unlock(mutex_);
54+
}
55+
};
56+
57+
#endif

0 commit comments

Comments
 (0)