UCommon
atomic.h
Go to the documentation of this file.
1 // Copyright (C) 2006-2014 David Sugar, Tycho Softworks.
2 // Copyright (C) 2015 Cherokees of Idaho.
3 //
4 // This file is part of GNU uCommon C++.
5 //
6 // GNU uCommon C++ is free software: you can redistribute it and/or modify
7 // it under the terms of the GNU Lesser General Public License as published
8 // by the Free Software Foundation, either version 3 of the License, or
9 // (at your option) any later version.
10 //
11 // GNU uCommon C++ is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public License
17 // along with GNU uCommon C++. If not, see <http://www.gnu.org/licenses/>.
18 
26 #ifndef _UCOMMON_ATOMIC_H_
27 #define _UCOMMON_ATOMIC_H_
28 
29 #ifndef _UCOMMON_CONFIG_H_
30 #include <ucommon/platform.h>
31 #endif
32 
33 #if defined(_MSWINDOWS_)
34 typedef LONG atomic_t;
35 #else
36 typedef int atomic_t;
37 #endif
38 
39 namespace ucommon {
40 
49 class __EXPORT Atomic
50 {
51 private:
52  __DELETE_DEFAULTS(Atomic);
53 
54 public:
60  class __EXPORT counter
61  {
62  private:
63  mutable volatile atomic_t value;
64 
65  __DELETE_COPY(counter);
66 
67  public:
68  counter(atomic_t initial = 0);
69 
70  // optimized reference count semantics
71  atomic_t fetch_retain() volatile;
72  atomic_t fetch_release() volatile;
73 
74  // fetch add/sub optimized semantics
75  atomic_t fetch_add(atomic_t offset = 1) volatile;
76  atomic_t fetch_sub(atomic_t offset = 1) volatile;
77 
78  atomic_t operator++() volatile;
79  atomic_t operator--() volatile;
80  atomic_t operator+=(atomic_t offset) volatile;
81  atomic_t operator-=(atomic_t offset) volatile;
82  atomic_t get() volatile;
83  void clear() volatile;
84 
85  inline operator atomic_t() volatile {
86  return get();
87  }
88 
89  inline atomic_t operator*() volatile {
90  return get();
91  }
92  };
93 
99  class __EXPORT spinlock
100  {
101  private:
102 #ifdef __GNUC__
103  mutable volatile atomic_t value __attribute__ ((aligned(16)));
104 #else
105  mutable volatile atomic_t value;
106 #endif
107  __DELETE_COPY(spinlock);
108 
109  public:
113  spinlock();
114 
120  bool acquire(void) volatile;
121 
125  void wait(void) volatile;
126 
130  void release(void) volatile;
131  };
132 
133  class __EXPORT Aligned
134  {
135  private:
136  __DELETE_DEFAULTS(Aligned);
137 
138  protected:
139  void *address;
140  size_t offset;
141 
142  Aligned(size_t object, size_t offset = 0);
143 
144  public:
145  virtual ~Aligned();
146  };
147 
148  template<typename T, unsigned alignment = 0>
149  class aligned : public Aligned
150  {
151  protected:
152  inline T* get() const {
153  return static_cast<T*>(address);
154  }
155 
156  public:
157  inline aligned() : Aligned(sizeof(T), alignment) {
158  new((caddr_t)address) T;
159  }
160 
161  inline T& operator*() const {
162  return *(static_cast<T*>(address));
163  }
164 
165  inline operator T&() {
166  return *get();
167  }
168 
169  inline void operator()(T value) {
170  *get() = value;
171  }
172  };
173 
174  static bool is_lockfree(void);
175 };
176 
177 } // namespace ucommon
178 
179 #endif
Atomic counter class.
Definition: atomic.h:60
Atomic spinlock class.
Definition: atomic.h:99
Various miscellaneous platform specific headers and defines.
Common namespace for all ucommon objects.
Definition: access.h:47
Generic atomic class for referencing atomic objects and static functions.
Definition: atomic.h:49