-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlock_int.hh
97 lines (89 loc) · 3.19 KB
/
lock_int.hh
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#ifndef DOZERG_LOCK_INT_H_20070916
#define DOZERG_LOCK_INT_H_20070916
/*
提供加锁保护的整数类型模板
CLockInt 提供加锁的整数类型
CLockIntMax 有统计最大值功能的加锁整数类型
CLockIntMinMax 有统计最小/最大值功能的加锁整数类型
History
20070925 把pthread_mutex_t改成CMutex
20080509 使用guard_type;加入CLockIntRange
20080920 使用模板参数决定锁类型
20130612 重写,去掉CLockIntRange,增加CLockIntMinMax
//*/
#include "impl/lock_int_impl.hh"
NS_SERVER_BEGIN
template<typename T, class LockT = CSpinLock>
class CLockInt : public NS_IMPL::CLockIntBase<T, LockT, NS_IMPL::__ExtraEmpty<T> >
{
typedef NS_IMPL::CLockIntBase<T, LockT, NS_IMPL::__ExtraEmpty<T> > __MyBase;
typedef CLockInt<T, LockT> __Myt;
public:
typedef typename __MyBase::value_type value_type;
explicit CLockInt(value_type c = value_type()):__MyBase(c){}
CLockInt(const __Myt & c):__MyBase(c.load()){}
value_type operator =(value_type c) volatile{
__MyBase::operator =(c);
return *this;
}
};
template<typename T, class LockT = CSpinLock>
class CLockIntMax : public NS_IMPL::CLockIntBase<T, LockT, NS_IMPL::__ExtraMax<T> >
{
typedef NS_IMPL::CLockIntBase<T, LockT, NS_IMPL::__ExtraMax<T> > __MyBase;
typedef CLockIntMax<T, LockT> __Myt;
public:
typedef typename __MyBase::value_type value_type;
explicit CLockIntMax(value_type c = value_type()):__MyBase(c){}
CLockIntMax(const __Myt & c):__MyBase(c.load()){}
value_type operator =(value_type c) volatile{
__MyBase::operator =(c);
return *this;
}
value_type max() const volatile{
typename __MyBase::guard_type g(__MyBase::lock_);
return __MyBase::e_.m_;
}
//重置max值,返回之前的max值
value_type resetMax() volatile{
typename __MyBase::guard_type g(__MyBase::lock_);
value_type ret = __MyBase::e_.m_;
__MyBase::e_.m_ = __MyBase::v_;
return ret;
}
};
template<typename T, class LockT = CSpinLock>
class CLockIntMinMax : public NS_IMPL::CLockIntBase<T, LockT, NS_IMPL::__ExtraMinMax<T> >
{
typedef NS_IMPL::CLockIntBase<T, LockT, NS_IMPL::__ExtraMinMax<T> > __MyBase;
typedef CLockIntMinMax<T, LockT> __Myt;
public:
typedef typename __MyBase::value_type value_type;
explicit CLockIntMinMax(value_type c = value_type()):__MyBase(c){}
CLockIntMinMax(const __Myt & c):__MyBase(c.load()){}
value_type operator =(value_type c) volatile{
__MyBase::operator =(c);
return *this;
}
value_type min() const volatile{
typename __MyBase::guard_type g(__MyBase::lock_);
return __MyBase::e_.mi_;
}
value_type max() const volatile{
typename __MyBase::guard_type g(__MyBase::lock_);
return __MyBase::e_.ma_;
}
//重置min, max值
//oldMin: 返回之前的min值
//oldMax: 返回之前的max值
void resetMinMax(value_type * oldMin, value_type * oldMax) volatile{
typename __MyBase::guard_type g(__MyBase::lock_);
if(oldMin)
*oldMin = __MyBase::e_.mi_;
if(oldMax)
*oldMax = __MyBase::e_.ma_;
__MyBase::e_.mi_ = __MyBase::e_.ma_ = __MyBase::v_;
}
};
NS_SERVER_END
#endif