| 1 |
/* |
| 2 |
Copyright (c) 2017, Michael Kazhdan |
| 3 |
All rights reserved. |
| 4 |
|
| 5 |
Redistribution and use in source and binary forms, with or without modification, |
| 6 |
are permitted provided that the following conditions are met: |
| 7 |
|
| 8 |
Redistributions of source code must retain the above copyright notice, this list of |
| 9 |
conditions and the following disclaimer. Redistributions in binary form must reproduce |
| 10 |
the above copyright notice, this list of conditions and the following disclaimer |
| 11 |
in the documentation and/or other materials provided with the distribution. |
| 12 |
|
| 13 |
Neither the name of the Johns Hopkins University nor the names of its contributors |
| 14 |
may be used to endorse or promote products derived from this software without specific |
| 15 |
prior written permission. |
| 16 |
|
| 17 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY |
| 18 |
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO THE IMPLIED WARRANTIES |
| 19 |
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT |
| 20 |
SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 21 |
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED |
| 22 |
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 23 |
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 24 |
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
| 25 |
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH |
| 26 |
DAMAGE. |
| 27 |
*/ |
| 28 |
#ifndef MY_ATOMIC_INCLUDED |
| 29 |
#define MY_ATOMIC_INCLUDED |
| 30 |
|
| 31 |
#if defined( _WIN32 ) || defined( _WIN64 ) |
| 32 |
#include <io.h> |
| 33 |
#include <Windows.h> |
| 34 |
#include <Psapi.h> |
| 35 |
#else // !_WIN32 && !_WIN64 |
| 36 |
#include <unistd.h> |
| 37 |
#include <sys/time.h> |
| 38 |
#include <sys/resource.h> |
| 39 |
#endif // _WIN32 || _WIN64 |
| 40 |
#include <mutex> |
| 41 |
|
| 42 |
namespace PoissonRecon |
| 43 |
{ |
| 44 |
template< typename Value > Value ReadAtomic8_( const volatile Value * value ); |
| 45 |
template< typename Value > Value ReadAtomic32_( const volatile Value * value ); |
| 46 |
template< typename Value > Value ReadAtomic64_( const volatile Value * value ); |
| 47 |
|
| 48 |
template< typename Value > Value SetAtomic8_ ( volatile Value * value , Value newValue ); |
| 49 |
template< typename Value > Value SetAtomic32_( volatile Value * value , Value newValue ); |
| 50 |
template< typename Value > Value SetAtomic64_( volatile Value * value , Value newValue ); |
| 51 |
|
| 52 |
template< typename Value > bool SetAtomic8_ ( volatile Value * value , Value newValue , Value oldValue ); |
| 53 |
template< typename Value > bool SetAtomic32_( volatile Value * value , Value newValue , Value oldValue ); |
| 54 |
template< typename Value > bool SetAtomic64_( volatile Value * value , Value newValue , Value oldValue ); |
| 55 |
|
| 56 |
template< typename Value > void AddAtomic8_( volatile Value * a , Value b ); |
| 57 |
template< typename Value > void AddAtomic32_( volatile Value * a , Value b ); |
| 58 |
template< typename Value > void AddAtomic64_( volatile Value * a , Value b ); |
| 59 |
|
| 60 |
template< typename Value > |
| 61 |
Value SetAtomic( volatile Value & value , Value newValue ) |
| 62 |
{ |
| 63 |
if constexpr( sizeof(Value)==1 ) return SetAtomic8_ ( &value , newValue ); |
| 64 |
else if constexpr( sizeof(Value)==4 ) return SetAtomic32_( &value , newValue ); |
| 65 |
else if constexpr( sizeof(Value)==8 ) return SetAtomic64_( &value , newValue ); |
| 66 |
else |
| 67 |
{ |
| 68 |
MK_WARN_ONCE( "should not use this function: " , sizeof(Value) ); |
| 69 |
static std::mutex setAtomicMutex; |
| 70 |
std::lock_guard< std::mutex > lock( setAtomicMutex ); |
| 71 |
Value oldValue = *(Value*)&value; |
| 72 |
*(Value*)&value = newValue; |
| 73 |
return oldValue; |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
template< typename Value > |
| 78 |
bool SetAtomic( volatile Value & value , Value newValue , Value oldValue ) |
| 79 |
{ |
| 80 |
if constexpr( sizeof(Value)==1 ) return SetAtomic8_ ( &value , newValue , oldValue ); |
| 81 |
else if constexpr( sizeof(Value)==4 ) return SetAtomic32_( &value , newValue , oldValue ); |
| 82 |
else if constexpr( sizeof(Value)==8 ) return SetAtomic64_( &value , newValue , oldValue ); |
| 83 |
else |
| 84 |
{ |
| 85 |
MK_WARN_ONCE( "should not use this function: " , sizeof(Value) ); |
| 86 |
static std::mutex setAtomicMutex; |
| 87 |
std::lock_guard< std::mutex > lock( setAtomicMutex ); |
| 88 |
if( value==oldValue ){ value = newValue ; return true; } |
| 89 |
else return false; |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
template< typename Value > |
| 94 |
void AddAtomic( volatile Value & a , Value b ) |
| 95 |
{ |
| 96 |
if constexpr( sizeof(Value)==1 ) return AddAtomic8_ ( &a , b ); |
| 97 |
else if constexpr( sizeof(Value)==4 ) return AddAtomic32_( &a , b ); |
| 98 |
else if constexpr( sizeof(Value)==8 ) return AddAtomic64_( &a , b ); |
| 99 |
else |
| 100 |
{ |
| 101 |
MK_WARN_ONCE( "should not use this function: " , sizeof(Value) ); |
| 102 |
static std::mutex addAtomicMutex; |
| 103 |
std::lock_guard< std::mutex > lock( addAtomicMutex ); |
| 104 |
*(Value*)&a += b; |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
template< typename Value > |
| 109 |
Value ReadAtomic( const volatile Value & value ) |
| 110 |
{ |
| 111 |
if constexpr( sizeof(Value)==1 ) return ReadAtomic8_( &value ); |
| 112 |
else if constexpr( sizeof(Value)==4 ) return ReadAtomic32_( &value ); |
| 113 |
else if constexpr( sizeof(Value)==8 ) return ReadAtomic64_( &value ); |
| 114 |
else |
| 115 |
{ |
| 116 |
MK_WARN_ONCE( "should not use this function: " , sizeof(Value) ); |
| 117 |
static std::mutex readAtomicMutex; |
| 118 |
std::lock_guard< std::mutex > lock( readAtomicMutex ); |
| 119 |
return *(Value*)&value; |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
template< typename Value > |
| 124 |
struct Atomic |
| 125 |
{ |
| 126 |
static void Add( volatile Value &a , const Value &b ) |
| 127 |
{ |
| 128 |
if constexpr( std::is_pod_v< Value > ) AddAtomic( a , b ); |
| 129 |
else |
| 130 |
{ |
| 131 |
MK_WARN_ONCE( "should not use this function: " , typeid(Value).name() ); |
| 132 |
static std::mutex addAtomicMutex; |
| 133 |
std::lock_guard< std::mutex > lock( addAtomicMutex ); |
| 134 |
*(Value*)&a += b; |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
static Value Set( volatile Value & value , Value newValue ) |
| 139 |
{ |
| 140 |
if constexpr( std::is_pod_v< Value > ) return SetAtomic( value , newValue ); |
| 141 |
else |
| 142 |
{ |
| 143 |
MK_WARN_ONCE( "should not use this function: " , typeid(Value).name() ); |
| 144 |
static std::mutex setAtomicMutex; |
| 145 |
std::lock_guard< std::mutex > lock( setAtomicMutex ); |
| 146 |
Value oldValue = *(Value*)&value; |
| 147 |
*(Value*)&value = newValue; |
| 148 |
return oldValue; |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
static bool Set( volatile Value & value , Value newValue , Value oldValue ) |
| 153 |
{ |
| 154 |
if constexpr( std::is_pod_v< Value > ) return SetAtomic( value , newValue , oldValue ); |
| 155 |
else |
| 156 |
{ |
| 157 |
MK_WARN_ONCE( "should not use this function: " , typeid(Value).name() , " , " , sizeof(Value) ); |
| 158 |
static std::mutex setAtomicMutex; |
| 159 |
std::lock_guard< std::mutex > lock( setAtomicMutex ); |
| 160 |
if( value==oldValue ){ value = newValue ; return true; } |
| 161 |
else return false; |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
static Value Read( const volatile Value & value ) |
| 166 |
{ |
| 167 |
if constexpr( std::is_pod_v< Value > ) return ReadAtomic( value ); |
| 168 |
else |
| 169 |
{ |
| 170 |
MK_WARN_ONCE( "should not use this function: " , typeid(Value).name() , " , " , sizeof(Value) ); |
| 171 |
static std::mutex readAtomicMutex; |
| 172 |
std::lock_guard< std::mutex > lock( readAtomicMutex ); |
| 173 |
return *(Value*)&value; |
| 174 |
} |
| 175 |
} |
| 176 |
}; |
| 177 |
|
| 178 |
/////////////////////////////////////////////// |
| 179 |
/////////////////////////////////////////////// |
| 180 |
/////////////////////////////////////////////// |
| 181 |
|
| 182 |
template< typename Value > |
| 183 |
Value ReadAtomic8_( const volatile Value * value ) |
| 184 |
{ |
| 185 |
#if defined( _WIN32 ) || defined( _WIN64 ) |
| 186 |
char _value = InterlockedExchangeAdd8( (char*)value , 0 ); |
| 187 |
return *(Value*)(&_value); |
| 188 |
#else // !_WIN32 && !_WIN64 |
| 189 |
uint8_t _value = __atomic_load_n( (uint8_t *)value , __ATOMIC_SEQ_CST ); |
| 190 |
#endif // _WIN32 || _WIN64 |
| 191 |
return *(Value*)(&_value); |
| 192 |
} |
| 193 |
|
| 194 |
template< typename Value > |
| 195 |
Value ReadAtomic32_( const volatile Value * value ) |
| 196 |
{ |
| 197 |
#if defined( _WIN32 ) || defined( _WIN64 ) |
| 198 |
long _value = InterlockedExchangeAdd( (long*)value , 0 ); |
| 199 |
return *(Value*)(&_value); |
| 200 |
#else // !_WIN32 && !_WIN64 |
| 201 |
uint32_t _value = __atomic_load_n( (uint32_t *)value , __ATOMIC_SEQ_CST ); |
| 202 |
#endif // _WIN32 || _WIN64 |
| 203 |
return *(Value*)(&_value); |
| 204 |
} |
| 205 |
|
| 206 |
template< typename Value > |
| 207 |
Value ReadAtomic64_( const volatile Value * value ) |
| 208 |
{ |
| 209 |
#if defined( _WIN32 ) || defined( _WIN64 ) |
| 210 |
__int64 _value = InterlockedExchangeAdd64( (__int64*)value , 0 ); |
| 211 |
#else // !_WIN32 && !_WIN64 |
| 212 |
uint64_t _value = __atomic_load_n( (uint64_t *)value , __ATOMIC_SEQ_CST ); |
| 213 |
#endif // _WIN32 || _WIN64 |
| 214 |
return *(Value*)(&_value); |
| 215 |
} |
| 216 |
|
| 217 |
template< typename Value > |
| 218 |
Value SetAtomic8_( volatile Value *value , Value newValue ) |
| 219 |
{ |
| 220 |
#if defined( _WIN32 ) || defined( _WIN64 ) |
| 221 |
char *_newValue = (char *)&newValue; |
| 222 |
char oldValue = InterlockedExchange( (char*)value , *_newValue ); |
| 223 |
#else // !_WIN32 && !_WIN64 |
| 224 |
uint8_t *_newValue = (uint8_t *)&newValue; |
| 225 |
uint8_t oldValue = __atomic_exchange_n( (uint8_t *)value , *_newValue , __ATOMIC_SEQ_CST ); |
| 226 |
#endif // _WIN32 || _WIN64 |
| 227 |
return *(Value*)&oldValue; |
| 228 |
} |
| 229 |
|
| 230 |
template< typename Value > |
| 231 |
Value SetAtomic32_( volatile Value *value , Value newValue ) |
| 232 |
{ |
| 233 |
#if defined( _WIN32 ) || defined( _WIN64 ) |
| 234 |
long *_newValue = (long *)&newValue; |
| 235 |
long oldValue = InterlockedExchange( (long*)value , *_newValue ); |
| 236 |
#else // !_WIN32 && !_WIN64 |
| 237 |
uint32_t *_newValue = (uint32_t *)&newValue; |
| 238 |
long oldValue = __atomic_exchange_n( (uint32_t *)value , *_newValue , __ATOMIC_SEQ_CST ); |
| 239 |
#endif // _WIN32 || _WIN64 |
| 240 |
return *(Value*)&oldValue; |
| 241 |
} |
| 242 |
|
| 243 |
template< typename Value > |
| 244 |
Value SetAtomic64_( volatile Value * value , Value newValue ) |
| 245 |
{ |
| 246 |
#if defined( _WIN32 ) || defined( _WIN64 ) |
| 247 |
__int64 *_newValue = (__int64 *)&newValue; |
| 248 |
__int64 oldValue = InterlockedExchange64( (__int64*)value , *_newValue ); |
| 249 |
#else // !_WIN32 && !_WIN64 |
| 250 |
uint64_t *_newValue = (uint64_t *)&newValue; |
| 251 |
uint64_t oldValue = __atomic_exchange_n( (uint64_t *)value , *_newValue , __ATOMIC_SEQ_CST );; |
| 252 |
#endif // _WIN32 || _WIN64 |
| 253 |
return *(Value*)&oldValue; |
| 254 |
} |
| 255 |
|
| 256 |
template< typename Value > |
| 257 |
bool SetAtomic8_( volatile Value *value , Value newValue , Value oldValue ) |
| 258 |
{ |
| 259 |
#if defined( _WIN32 ) || defined( _WIN64 ) |
| 260 |
char *_oldValue = (char *)&oldValue; |
| 261 |
char *_newValue = (char *)&newValue; |
| 262 |
return _InterlockedCompareExchange8( (char*)value , *_newValue , *_oldValue )==*_oldValue; |
| 263 |
#else // !_WIN32 && !_WIN64 |
| 264 |
uint8_t *_newValue = (uint8_t *)&newValue; |
| 265 |
return __atomic_compare_exchange_n( (uint8_t *)value , (uint8_t *)&oldValue , *_newValue , false , __ATOMIC_SEQ_CST , __ATOMIC_SEQ_CST ); |
| 266 |
#endif // _WIN32 || _WIN64 |
| 267 |
} |
| 268 |
|
| 269 |
template< typename Value > |
| 270 |
bool SetAtomic32_( volatile Value *value , Value newValue , Value oldValue ) |
| 271 |
{ |
| 272 |
#if defined( _WIN32 ) || defined( _WIN64 ) |
| 273 |
long *_oldValue = (long *)&oldValue; |
| 274 |
long *_newValue = (long *)&newValue; |
| 275 |
return InterlockedCompareExchange( (long*)value , *_newValue , *_oldValue )==*_oldValue; |
| 276 |
#else // !_WIN32 && !_WIN64 |
| 277 |
uint32_t *_newValue = (uint32_t *)&newValue; |
| 278 |
return __atomic_compare_exchange_n( (uint32_t *)value , (uint32_t *)&oldValue , *_newValue , false , __ATOMIC_SEQ_CST , __ATOMIC_SEQ_CST ); |
| 279 |
#endif // _WIN32 || _WIN64 |
| 280 |
} |
| 281 |
|
| 282 |
template< typename Value > |
| 283 |
bool SetAtomic64_( volatile Value * value , Value newValue , Value oldValue ) |
| 284 |
{ |
| 285 |
#if defined( _WIN32 ) || defined( _WIN64 ) |
| 286 |
__int64 *_oldValue = (__int64 *)&oldValue; |
| 287 |
__int64 *_newValue = (__int64 *)&newValue; |
| 288 |
return InterlockedCompareExchange64( (__int64*)value , *_newValue , *_oldValue )==*_oldValue; |
| 289 |
#else // !_WIN32 && !_WIN64 |
| 290 |
uint64_t *_newValue = (uint64_t *)&newValue; |
| 291 |
return __atomic_compare_exchange_n( (uint64_t *)value , (uint64_t *)&oldValue , *_newValue , false , __ATOMIC_SEQ_CST , __ATOMIC_SEQ_CST ); |
| 292 |
#endif // _WIN32 || _WIN64 |
| 293 |
} |
| 294 |
|
| 295 |
template< typename Value > |
| 296 |
void AddAtomic8_( volatile Value *a , Value b ) |
| 297 |
{ |
| 298 |
Value current = ReadAtomic8_( a ); |
| 299 |
Value sum = current+b; |
| 300 |
#if defined( _WIN32 ) || defined( _WIN64 ) |
| 301 |
char *_current = (char *)¤t; |
| 302 |
char *_sum = (char *)∑ |
| 303 |
while( InterlockedCompareExchange( (char*)a , *_sum , *_current )!=*_current ) |
| 304 |
{ |
| 305 |
current = ReadAtomic8_( a ); |
| 306 |
sum = current + b; |
| 307 |
} |
| 308 |
#else // !_WIN32 && !_WIN64 |
| 309 |
uint8_t *_current = (uint8_t *)¤t; |
| 310 |
uint8_t *_sum = (uint8_t *)∑ |
| 311 |
while( __sync_val_compare_and_swap( (uint8_t *)a , *_current , *_sum )!=*_current ) |
| 312 |
{ |
| 313 |
current = ReadAtomic8_( a ); |
| 314 |
sum = current+b; |
| 315 |
} |
| 316 |
#endif // _WIN32 || _WIN64 |
| 317 |
} |
| 318 |
|
| 319 |
template< typename Value > |
| 320 |
void AddAtomic32_( volatile Value *a , Value b ) |
| 321 |
{ |
| 322 |
Value current = ReadAtomic32_( a ); |
| 323 |
Value sum = current+b; |
| 324 |
#if defined( _WIN32 ) || defined( _WIN64 ) |
| 325 |
long *_current = (long *)¤t; |
| 326 |
long *_sum = (long *)∑ |
| 327 |
while( InterlockedCompareExchange( (long*)a , *_sum , *_current )!=*_current ) |
| 328 |
{ |
| 329 |
current = ReadAtomic32_( a ); |
| 330 |
sum = current + b; |
| 331 |
} |
| 332 |
#else // !_WIN32 && !_WIN64 |
| 333 |
uint32_t *_current = (uint32_t *)¤t; |
| 334 |
uint32_t *_sum = (uint32_t *)∑ |
| 335 |
while( __sync_val_compare_and_swap( (uint32_t *)a , *_current , *_sum )!=*_current ) |
| 336 |
{ |
| 337 |
current = ReadAtomic32_( a ); |
| 338 |
sum = current+b; |
| 339 |
} |
| 340 |
#endif // _WIN32 || _WIN64 |
| 341 |
} |
| 342 |
|
| 343 |
template< typename Value > |
| 344 |
void AddAtomic64_( volatile Value * a , Value b ) |
| 345 |
{ |
| 346 |
#if 1 |
| 347 |
Value current = ReadAtomic64_( a ); |
| 348 |
Value sum = current+b; |
| 349 |
while( !SetAtomic64_( a , sum , current ) ) |
| 350 |
{ |
| 351 |
current = ReadAtomic64_( a ); |
| 352 |
sum = current+b; |
| 353 |
} |
| 354 |
#else |
| 355 |
Value current = ReadAtomic64_( a ); |
| 356 |
Value sum = current+b; |
| 357 |
#if defined( _WIN32 ) || defined( _WIN64 ) |
| 358 |
__int64 *_current = (__int64 *)¤t; |
| 359 |
__int64 *_sum = (__int64 *)∑ |
| 360 |
while( InterlockedCompareExchange64( (__int64*)a , *_sum , *_current )!=*_current ) |
| 361 |
{ |
| 362 |
current = ReadAtomic64_( a ); |
| 363 |
sum = current+b; |
| 364 |
} |
| 365 |
#else // !_WIN32 && !_WIN64 |
| 366 |
uint64_t *_current = (uint64_t *)¤t; |
| 367 |
uint64_t *_sum = (uint64_t *)∑ |
| 368 |
while( __sync_val_compare_and_swap( (uint64_t *)a , *_current , *_sum )!=*_current ) |
| 369 |
{ |
| 370 |
current = ReadAtomic64_( a); |
| 371 |
sum = current+b; |
| 372 |
} |
| 373 |
#endif // _WIN32 || _WIN64 |
| 374 |
#endif |
| 375 |
} |
| 376 |
} |
| 377 |
#endif // MY_ATOMIC_INCLUDED |