acl  3.5.3.0
atomic.hpp
浏览该文件的文档.
1 #pragma once
2 #include "../acl_cpp_define.hpp"
3 
4 namespace acl
5 {
6 
7 // internal functions being used
8 void* atomic_new(void);
9 void atomic_free(void*);
10 void atomic_set(void*, void*);
11 void* atomic_cas(void*, void*, void*);
12 void* atomic_xchg(void*, void*);
13 
14 template<typename T>
16 {
17 public:
18  atomic(T* t)
19  {
20  atomic_ = atomic_new();
21  atomic_set(atomic_, t);
22  }
23 
24  virtual ~atomic(void)
25  {
26  atomic_free(atomic_);
27  }
28 
29  T* cas(T* cmp, T* val)
30  {
31  return (T*) atomic_cas(atomic_, cmp, val);
32  }
33 
34  T* xchg(T* val)
35  {
36  return (T*) atomic_xchg(atomic_, val);
37  }
38 
39 protected:
40  void* atomic_;
41 
42 private:
43  atomic(const atomic&);
44 };
45 
46 class ACL_CPP_API atomic_long : public atomic<long long>
47 {
48 public:
49  atomic_long(long long n = 0);
50  atomic_long(const atomic_long& n);
51 
52  ~atomic_long(void) {}
53 
54  void set(long long n);
55  long long cas(long long cmp, long long n);
56  long long fetch_add(long long n);
57  long long add_fetch(long long n);
58 
59  operator long long() const
60  {
61  return n_;
62  }
63 
64  long long value(void) const
65  {
66  return n_;
67  }
68 
69  void operator=(long long n)
70  {
71  set(n);
72  }
73 
74  void operator=(const atomic_long& n)
75  {
76  set(n.n_);
77  }
78 
79  long long operator++()
80  {
81  return add_fetch(1);
82  }
83 
84  long long operator++(int)
85  {
86  return fetch_add(1);
87  }
88 
89  long long operator--()
90  {
91  return add_fetch(-1);
92  }
93 
94  long long operator--(int)
95  {
96  return fetch_add(-1);
97  }
98 
99  long long operator+=(long long n)
100  {
101  return add_fetch(n);
102  }
103 
104  long long operator+=(int n)
105  {
106  return add_fetch(n);
107  }
108 
109  long long operator-=(long long n)
110  {
111  return add_fetch(-n);
112  }
113 
114  long long operator-=(int n)
115  {
116  return add_fetch(-n);
117  }
118 
119 private:
120  long long n_;
121 };
122 
123 #include "thread.hpp"
124 
126 {
127 private:
128  atomic_long count_;
129 public:
132 
133  void run(void)
134  {
135 
136  long long n = count_++;
137  printf(">>n=%lld\r\n", n);
138 
139  n = count_;
140  printf(">>n=%lld\r\n", n);
141 
142  n = ++count_;
143  printf(">>n=%lld\r\n", n);
144 
145  n = --count_;
146  printf(">>n=%lld\r\n", n);
147 
148  n = count_--;
149  printf(">>n=%lld\r\n", n);
150 
151  n = count_;
152  printf(">>n=%lld\r\n", n);
153 
154  count_ -= 1;
155  n = count_;
156  printf(">>n=%lld\r\n", n);
157 
158  printf(">>count > 1 ? %s\r\n", count_ >= 1 ? "yes" : "no");
159  printf(">>1 > count ? %s\r\n", 1 > count_ ? "yes" : "no");
160 
161  int i = 1;
162  count_ = i;
163  n = count_;
164  printf(">>n=%lld\r\n", n);
165  }
166 
167  class mythread : public thread
168  {
169  public:
170  mythread(atomic_long_test& at) : at_(at) {}
171  ~mythread(void) {}
172  protected:
173  void* run(void)
174  {
175  for (size_t i = 0; i < 100; i++)
176  at_.run();
177  return NULL;
178  }
179  private:
180  atomic_long_test& at_;
181  };
182 
183  static void test(void)
184  {
185  atomic_long_test at;
186  mythread thr1(at), thr2(at), thr3(at);
187  thr1.set_detachable(false);
188  thr2.set_detachable(false);
189  thr3.set_detachable(false);
190  thr1.start();
191  thr2.start();
192  thr3.start();
193  thr1.wait();
194  thr2.wait();
195  thr3.wait();
196  }
197 };
198 
199 } // namespace acl
void operator=(long long n)
Definition: atomic.hpp:69
void * atomic_new(void)
void * atomic_
Definition: atomic.hpp:40
long long value(void) const
Definition: atomic.hpp:64
bool start(bool sync=false)
long long operator+=(int n)
Definition: atomic.hpp:104
static void test(void)
Definition: atomic.hpp:183
long long operator+=(long long n)
Definition: atomic.hpp:99
long long operator++(int)
Definition: atomic.hpp:84
atomic(T *t)
Definition: atomic.hpp:18
long long operator++()
Definition: atomic.hpp:79
T * xchg(T *val)
Definition: atomic.hpp:34
void * atomic_xchg(void *, void *)
T * cas(T *cmp, T *val)
Definition: atomic.hpp:29
mythread(atomic_long_test &at)
Definition: atomic.hpp:170
void * atomic_cas(void *, void *, void *)
long long operator-=(long long n)
Definition: atomic.hpp:109
thread & set_detachable(bool yes)
#define ACL_CPP_API
void atomic_free(void *)
~atomic_long(void)
Definition: atomic.hpp:52
void atomic_set(void *, void *)
virtual ~atomic(void)
Definition: atomic.hpp:24
long long operator-=(int n)
Definition: atomic.hpp:114
bool wait(void **out=NULL)
void operator=(const atomic_long &n)
Definition: atomic.hpp:74