-
Notifications
You must be signed in to change notification settings - Fork 110
/
Copy pathguard.h
81 lines (73 loc) · 2.13 KB
/
guard.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/***********************************************************************
* Copyright (c) 2012, Baidu Inc. All rights reserved.
*
* Licensed under the BSD License
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* license.txt
*********************************************************************/
#ifndef _BGCC2_GUARD_H_
#define _BGCC2_GUARD_H_
#include <stdlib.h>
namespace bgcc {
/**
* @brief
*
* @tparam LockType
* @see
* @note
* @author liuxupeng(liuxupeng@baidu.com)
* @date 2012年05月30日 19时19分33秒
*/
template <typename LockType>
class Guard {
public:
/**
* @brief Guard 构造函数,对Mutex加锁
*
* @param lock 待加锁指针
* @see
* @note
* @author liuxupeng(liuxupeng@baidu.com)
* @date 2012年05月30日 19时26分09秒
*/
Guard(LockType* lock) : _locked(false), _lock(lock) {
if (NULL != _lock) {
if (0 == _lock->lock()) {
_locked = true;
}
}
}
/**
* @brief is_locked 检测加锁是否成功
*
* @return 加锁成功返回true;否则返回false
* @see
* @note
* @author liuxupeng(liuxupeng@baidu.com)
* @date 2012年05月30日 19时26分33秒
*/
bool is_locked() const {
return _locked;
}
/**
* @brief ~Guard 析构函数,对Mutex解锁
* @see
* @note
* @author liuxupeng(liuxupeng@baidu.com)
* @date 2012年05月30日 19时27分12秒
*/
~Guard() {
if (NULL != _lock) {
if (0 == _lock->unlock()) {
_locked = false;
}
}
}
private:
mutable bool _locked;
LockType* _lock;
};
}
#endif // _BGCC2_GUARD_H_