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 #ifdef _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:
58  static const bool simulated;
59 
65  class __EXPORT counter
66  {
67  private:
68  mutable volatile atomic_t value;
69 
70  __DELETE_COPY(counter);
71 
72  public:
73  counter(atomic_t initial = 0);
74 
75  // fetch add/sub optimized semantics
76  atomic_t fetch_add(atomic_t offset = 1) volatile;
77  atomic_t fetch_sub(atomic_t offset = 1) volatile;
78 
79  atomic_t operator++() volatile;
80  atomic_t operator--() volatile;
81  atomic_t operator+=(atomic_t offset) volatile;
82  atomic_t operator-=(atomic_t offset) volatile;
83  atomic_t get() volatile;
84  void clear() volatile;
85 
86  inline operator atomic_t() volatile {
87  return get();
88  }
89 
90  inline atomic_t operator*() volatile {
91  return get();
92  }
93  };
94 
100  class __EXPORT spinlock
101  {
102  private:
103 #ifdef __GNUC__
104  mutable volatile atomic_t value __attribute__ ((aligned(16)));
105 #else
106  mutable volatile atomic_t value;
107 #endif
108  __DELETE_COPY(spinlock);
109 
110  public:
114  spinlock();
115 
121  bool acquire(void) volatile;
122 
126  void wait(void) volatile;
127 
131  void release(void) volatile;
132  };
133 
139  static void *alloc(size_t size);
140 };
141 
142 // for abi7
143 typedef atomic Atomic;
144 
145 } // namespace ucommon
146 
147 #endif
void release(SharedAccess &object)
Convenience function to unlock shared object through it's protocol.
Definition: access.h:280
Various miscellaneous platform specific headers and defines.
Atomic spinlock class.
Definition: atomic.h:100
void acquire(mutex_t &mutex)
Convenience function to acquire a mutex.
Definition: thread.h:1943
Common namespace for all ucommon objects.
Definition: access.h:47
void wait(barrier_t &barrier)
Convenience function to wait on a barrier.
Definition: thread.h:1921
Atomic counter class.
Definition: atomic.h:65
static const bool simulated
Set to true if atomics have to be simulated with mutexes.
Definition: atomic.h:58
Generic atomic class for referencing atomic objects and static functions.
Definition: atomic.h:49