/usr/include/ucommon/atomic.h is in libucommon-dev 7.0.0-9.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 | // Copyright (C) 2006-2014 David Sugar, Tycho Softworks.
// Copyright (C) 2015 Cherokees of Idaho.
//
// This file is part of GNU uCommon C++.
//
// GNU uCommon C++ is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// GNU uCommon C++ is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with GNU uCommon C++. If not, see <http://www.gnu.org/licenses/>.
/**
* Atomic pointers and locks. These are meant to use atomic CPU operations
* and hence offer maximum performance.
* @file ucommon/atomic.h
* @author David Sugar <dyfet@gnutelephony.org>
*/
#ifndef _UCOMMON_ATOMIC_H_
#define _UCOMMON_ATOMIC_H_
#ifndef _UCOMMON_CONFIG_H_
#include <ucommon/platform.h>
#endif
#if defined(_MSWINDOWS_)
typedef LONG atomic_t;
#else
typedef int atomic_t;
#endif
namespace ucommon {
/**
* Generic atomic class for referencing atomic objects and static functions.
* We have an atomic counter and spinlock, and in the future we may add
* other atomic classes and atomic manipulations needed to create lockfree
* data structures. The atomic classes use mutexes if no suitable atomic
* code is available.
* @author David Sugar <dyfet@gnutelephony.org>
*/
class __EXPORT Atomic
{
private:
__DELETE_DEFAULTS(Atomic);
public:
/**
* Atomic counter class. Can be used to manipulate value of an
* atomic counter without requiring explicit thread locking.
* @author David Sugar <dyfet@gnutelephony.org>
*/
class __EXPORT counter
{
private:
mutable volatile atomic_t value;
__DELETE_COPY(counter);
public:
counter(atomic_t initial = 0);
// optimized reference count semantics
atomic_t fetch_retain() volatile;
atomic_t fetch_release() volatile;
// fetch add/sub optimized semantics
atomic_t fetch_add(atomic_t offset = 1) volatile;
atomic_t fetch_sub(atomic_t offset = 1) volatile;
atomic_t operator++() volatile;
atomic_t operator--() volatile;
atomic_t operator+=(atomic_t offset) volatile;
atomic_t operator-=(atomic_t offset) volatile;
atomic_t get() volatile;
void clear() volatile;
inline operator atomic_t() volatile {
return get();
}
inline atomic_t operator*() volatile {
return get();
}
};
/**
* Atomic spinlock class. Used as high-performance sync lock between
* threads.
* @author David Sugar <dyfet@gnutelephony.org>
*/
class __EXPORT spinlock
{
private:
#ifdef __GNUC__
mutable volatile atomic_t value __attribute__ ((aligned(16)));
#else
mutable volatile atomic_t value;
#endif
__DELETE_COPY(spinlock);
public:
/**
* Construct and initialize spinlock.
*/
spinlock();
/**NAMESPACE_UCOMMON
* Acquire the lock. If the lock is not acquired, one "spins"
* by doing something else. One suggestion is using thread::yield.
* @return true if acquired.
*/
bool acquire(void) volatile;
/**
* Wait for and aquire spinlock.
*/
void wait(void) volatile;
/**
* Release an acquired spinlock.
*/
void release(void) volatile;
};
class __EXPORT Aligned
{
private:
__DELETE_DEFAULTS(Aligned);
protected:
void *address;
size_t offset;
Aligned(size_t object, size_t offset = 0);
public:
virtual ~Aligned();
};
template<typename T, unsigned alignment = 0>
class aligned : public Aligned
{
protected:
inline T* get() const {
return static_cast<T*>(address);
}
public:
inline aligned() : Aligned(sizeof(T), alignment) {
new((caddr_t)address) T;
}
inline T& operator*() const {
return *(static_cast<T*>(address));
}
inline operator T&() {
return *get();
}
inline void operator()(T value) {
*get() = value;
}
};
static bool is_lockfree(void);
};
} // namespace ucommon
#endif
|