forked from abcdls0905/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathios_lockval.h
69 lines (58 loc) · 1 KB
/
ios_lockval.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
//--------------------------------------------------------------------
// 文件名: ios_lockval.h
// 内 容:
// 说 明:
// 创建日期: 2010年12月15日
// 创建人: 陆利民
// 版权所有: 苏州蜗牛电子有限公司
//--------------------------------------------------------------------
#ifndef _SYSTEM_IOS_LOCKVAL_H
#define _SYSTEM_IOS_LOCKVAL_H
#include <libkern/osatomic.h>
// 带锁的整数
class CLockVal
{
public:
CLockVal()
{
m_value = 0;
}
// 加一
int Inc()
{
OSAtomicIncrement32(&m_value);
return m_value;
}
// 减一
int Dec()
{
OSAtomicDecrement32(&m_value);
return m_value;
}
// 设置
int Set(int val)
{
m_value = val;
return m_value;
}
// 获取
int Get() const
{
return m_value;
}
// 比较后交换,返回原值(若当前值等于oldv,则将当前值设为newv,并返回oldv)
int CompareExchange(int oldv, int newv)
{
if (OSAtomicCompareAndSwap32(oldv, newv, &m_value))
{
return oldv;
}
else
{
return m_value;
}
}
public:
volatile int32_t m_value;
};
#endif // _SYSTEM_IOS_LOCKVAL_H