/usr/include/NTL/thread.h is in libntl-dev 10.5.0-2.
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 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 | #ifndef NTL_thread__H
#define NTL_thread__H
#include <NTL/tools.h>
#include <string>
#ifdef NTL_THREADS
#include <atomic>
#include <mutex>
#endif
NTL_OPEN_NNS
#ifdef NTL_THREADS
class AtomicLong {
private:
NTL_SNS atomic_long data;
AtomicLong(const AtomicLong& other); // disabled
AtomicLong& operator=(const AtomicLong& other); // disabled
public:
explicit AtomicLong(const long& _data) : data(_data) { }
AtomicLong& operator=(const long& _data)
{
data.store(_data, NTL_SNS memory_order_release);
return *this;
}
operator long() const { return data.load( NTL_SNS memory_order_acquire); }
};
class AtomicBool {
private:
NTL_SNS atomic_bool data;
AtomicBool(const AtomicBool& other); // disabled
AtomicBool& operator=(const AtomicBool& other); // disabled
public:
explicit AtomicBool(const bool& _data) : data(_data) { }
AtomicBool& operator=(const bool& _data)
{
data.store(_data, NTL_SNS memory_order_release);
return *this;
}
operator bool() const { return data.load( NTL_SNS memory_order_acquire); }
};
class AtomicCounter {
private:
NTL_SNS atomic_ulong cnt;
public:
AtomicCounter() : cnt(0) { }
unsigned long inc()
{
return cnt.fetch_add(1UL, NTL_SNS memory_order_relaxed);
}
};
class AtomicRefCount {
private:
NTL_SNS atomic_long cnt;
public:
AtomicRefCount() : cnt(0) { }
void inc() { cnt.fetch_add(1, NTL_SNS memory_order_relaxed); }
bool dec()
{
if (cnt.fetch_sub(1, NTL_SNS memory_order_release) == 1) {
NTL_SNS atomic_thread_fence(NTL_SNS memory_order_acquire);
return true;
}
else
return false;
}
long get_count() const { return cnt; }
// mainly for debugging
};
class MutexProxy {
private:
NTL_SNS mutex mtx;
MutexProxy(const MutexProxy&); // disabled
void operator=(const MutexProxy&); // disabled
public:
MutexProxy() { }
friend class GuardProxy;
};
class GuardProxy {
private:
NTL_SNS unique_lock<NTL_SNS mutex> lck;
GuardProxy(const GuardProxy&); // disabled
void operator=(const GuardProxy&); // disabled
public:
GuardProxy(MutexProxy& mtx) : lck(mtx.mtx, NTL_SNS defer_lock) { }
void lock() { lck.lock(); }
};
#else
class AtomicLong {
private:
long data;
AtomicLong(const AtomicLong& other); // disabled
AtomicLong& operator=(const AtomicLong& other); // disabled
public:
explicit AtomicLong(const long& _data) : data(_data) { }
AtomicLong& operator=(const long& _data) { data = _data; return *this; }
operator long() const { return data; }
};
class AtomicBool {
private:
bool data;
AtomicBool(const AtomicBool& other); // disabled
AtomicBool& operator=(const AtomicBool& other); // disabled
public:
explicit AtomicBool(const bool& _data) : data(_data) { }
AtomicBool& operator=(const bool& _data) { data = _data; return *this; }
operator bool() const { return data; }
};
class AtomicCounter {
private:
unsigned long cnt;
AtomicCounter(const AtomicCounter&); // disabled
void operator=(const AtomicCounter&); // disabled
public:
AtomicCounter() : cnt(0) { }
unsigned long inc() { return cnt++; }
};
class AtomicRefCount {
private:
long cnt;
AtomicRefCount(const AtomicRefCount&); // disabled
void operator=(const AtomicRefCount&); // disabled
public:
AtomicRefCount() : cnt(0) { }
void inc() { cnt++; }
bool dec() { cnt--; return cnt == 0; }
long get_count() const { return cnt; }
// mainly for debugging
};
class MutexProxy {
private:
MutexProxy(const MutexProxy&); // disabled
void operator=(const MutexProxy&); // disabled
public:
MutexProxy() { }
};
class GuardProxy {
private:
GuardProxy(const GuardProxy&); // disabled
void operator=(const GuardProxy&); // disabled
public:
GuardProxy(MutexProxy&) { }
void lock() { }
};
#endif
const NTL_SNS string& CurrentThreadID();
/*********************************************************************
NOTES: See
http://preshing.com/20120913/acquire-and-release-semantics/
http://preshing.com/20130922/acquire-and-release-fences/
http://preshing.com/20130930/double-checked-locking-is-fixed-in-cpp11/
http://preshing.com/20131125/acquire-and-release-fences-dont-work-the-way-youd-expect/
for general information on C++11 atomics.
Also see
http://www.chaoticmind.net/~hcb/projects/boost.atomic/doc/atomic/usage_examples.html#boost_atomic.usage_examples.example_reference_counters
for reference counting in a multi-threaded environment.
*********************************************************************/
NTL_CLOSE_NNS
#endif
|