Repository navigation
Expand file tree
/
Copy pathAtom.cpp
More file actions
executable file
·114 lines (95 loc) · 1.84 KB
/
Copy pathAtom.cpp
File metadata and controls
executable file
·114 lines (95 loc) · 1.84 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include "Atom.h"
#include "Lock.h"
#include "internal/Common_internal.h"
#include <vector>
namespace ccc {
namespace internal_ {
#ifndef _WIN32
class AtomMutexMap {
public:
AtomMutexMap() {
for (int i = 0; i < LOCK_COUNT; ++i) {
mutexs_.push_back(new base::Lock);
}
}
~AtomMutexMap() {
for (int i = 0; i < LOCK_COUNT; ++i) {
delete mutexs_.at(i);
}
}
base::Lock* getMutex(void *hash) {
unsigned pos = (unsigned)hash % LOCK_COUNT;
return mutexs_.at(pos);
}
private:
enum {LOCK_COUNT = 13};
std::vector<base::Mutex*> mutexs_;
};
AtomMutexMap g_atomMutexMap;
#endif // _WIN32
}
#if !defined(_WIN32) && !defined(CCC_ATOM_LOCK)
#define CCC_ATOM_LOCK(ptr) CCC_LOCK_P(internal_::g_atomMutexMap.getMutex(ptr))
#endif
long Atom::get() const {
return val_;
}
long Atom::set(long v) {
#ifdef _WIN32
InterlockedExchange((LONG *)&val_, v);
#else
CCC_ATOM_LOCK(this);
val_ = v;
#endif
return v;
}
long Atom::add(long v) {
int tmp = v;
#ifdef _WIN32
return InterlockedExchangeAdd((LONG *)&val_, tmp) + v;
#else
CCC_ATOM_LOCK(this);
val_ += v;
return val_;
#endif
}
long Atom::sub(long v) {
return add(-v);
}
long Atom::inc() {
#ifdef _WIN32
return InterlockedIncrement((LONG *)&val_);
#else
return add(1);
#endif
}
long Atom::dec() {
#ifdef _WIN32
return InterlockedDecrement((LONG *)&val_);
#else
return add(-1);
#endif
}
long Atom::xchg(long v) {
#ifdef _WIN32
return InterlockedExchange((LONG *)&val_, v);
#else
CCC_ATOM_LOCK(this);
long ret = val_;
val_ = v;
return ret;
#endif
}
long Atom::xchgcmp(long v, long cmp) {
#ifdef _WIN32
return InterlockedCompareExchange((LONG *)&val_, v, cmp);
#else
CCC_ATOM_LOCK(this);
long ret = val_;
if (val_ == cmp) {
val_ = v;
}
return ret;
#endif
}
}