ucommon  7.0.0
About: GNU uCommon C++ is a portable and optimized class framework for writing C++ applications that need to use threads and support concurrent synchronization, and that use sockets, XML parsing, object serialization, thread-optimized string and data structure classes, etc..
  Fossies Dox: ucommon-7.0.0.tar.gz  ("inofficial" and yet experimental doxygen-generated source code documentation)  

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 
50 {
51 private:
53 
54 public:
61  {
62  private:
63  mutable volatile atomic_t value;
64 
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 
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
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 
134  {
135  private:
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
ucommon::Atomic::Aligned::offset
size_t offset
Definition: atomic.h:140
ucommon
Definition: access.cpp:23
ucommon::Atomic
Definition: atomic.h:49
ucommon::Atomic::Aligned::address
void * address
Definition: atomic.h:139
ucommon::Atomic::aligned::get
T * get() const
Definition: atomic.h:152
caddr_t
#define caddr_t
Definition: file.h:86
ucommon::Atomic::counter
Definition: atomic.h:60
ucommon::clear
T & clear(T &o)
Definition: generics.h:416
ucommon::Atomic::counter::operator*
atomic_t operator*() volatile
Definition: atomic.h:89
__EXPORT
#define __EXPORT
Definition: config.h:49
ucommon::Atomic::aligned::operator*
T & operator*() const
Definition: atomic.h:161
ucommon::Atomic::aligned::operator()
void operator()(T value)
Definition: atomic.h:169
__DELETE_COPY
#define __DELETE_COPY(x)
Definition: platform.h:160
platform.h
__DELETE_DEFAULTS
#define __DELETE_DEFAULTS(x)
Definition: platform.h:162
ucommon::Atomic::aligned::aligned
aligned()
Definition: atomic.h:157
ucommon::Atomic::spinlock::value
volatile atomic_t value
Definition: atomic.h:105
ucommon::Atomic::aligned
Definition: atomic.h:149
ucommon::Atomic::counter::value
volatile atomic_t value
Definition: atomic.h:63
ucommon::Atomic::Aligned
Definition: atomic.h:133
ucommon::operator+=
std::string & operator+=(std::string &target, String &source)
Definition: stream.h:581
atomic_t
int atomic_t
Definition: atomic.h:36
ucommon::Atomic::spinlock
Definition: atomic.h:99