1 // The MIT License (MIT) 2 // 3 // Copyright (c) 2015 Sergey Makeev, Vadim Slyusarev 4 // 5 // Permission is hereby granted, free of charge, to any person obtaining a copy 6 // of this software and associated documentation files (the "Software"), to deal 7 // in the Software without restriction, including without limitation the rights 8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 // copies of the Software, and to permit persons to whom the Software is 10 // furnished to do so, subject to the following conditions: 11 // 12 // The above copyright notice and this permission notice shall be included in 13 // all copies or substantial portions of the Software. 14 // 15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 // THE SOFTWARE. 22 23 #pragma once 24 25 26 #ifdef _WIN32 27 #include <Platform/Windows/MTAtomic.h> 28 #else 29 #include <Platform/Posix/MTAtomic.h> 30 #endif 31 32 33 34 namespace MT 35 { 36 inline bool IsPointerAligned( const volatile void* p, const uint32 align ) 37 { 38 return !((uintptr_t)p & (align - 1)); 39 } 40 41 42 //compile time POD type check 43 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 44 static_assert(std::is_pod<AtomicInt32Base>::value == true, "AtomicInt32Base must be a POD (plain old data type)"); 45 46 47 // 48 // Atomic int (type with constructor) 49 // 50 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 51 class AtomicInt32 : public AtomicInt32Base 52 { 53 54 public: 55 56 AtomicInt32() 57 { 58 _value = 0; 59 static_assert(sizeof(AtomicInt32) == 4, "Invalid type size"); 60 MT_ASSERT(IsPointerAligned(&_value, 4), "Invalid atomic int alignment"); 61 } 62 63 explicit AtomicInt32(int v) 64 { 65 _value = v; 66 static_assert(sizeof(AtomicInt32) == 4, "Invalid type size"); 67 MT_ASSERT(IsPointerAligned(&_value, 4), "Invalid atomic int alignment"); 68 } 69 }; 70 71 72 //compile time POD type check 73 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 74 static_assert(std::is_pod<AtomicPtrBase>::value == true, "AtomicPtrBase must be a POD (plain old data type)"); 75 76 77 // 78 // Atomic pointer (type with constructor) 79 // 80 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 81 template<typename T> 82 class AtomicPtr : protected AtomicPtrBase 83 { 84 public: 85 AtomicPtr() 86 { 87 _value = nullptr; 88 MT_ASSERT(IsPointerAligned(&_value, sizeof(void*)), "Invalid atomic ptr alignment"); 89 } 90 91 explicit AtomicPtr(T* v) 92 { 93 _value = v; 94 MT_ASSERT(IsPointerAligned(&_value, sizeof(void*)), "Invalid atomic ptr alignment"); 95 } 96 97 T* Load() const 98 { 99 return (T*)AtomicPtrBase::Load(); 100 } 101 102 T* Store(T* val) 103 { 104 return (T*)AtomicPtrBase::Store(val); 105 } 106 107 108 T* CompareAndSwap(T* compareValue, T* newValue) 109 { 110 return (T*)AtomicPtrBase::CompareAndSwap(compareValue, newValue); 111 } 112 113 T* LoadRelaxed() const 114 { 115 return (T*)AtomicPtrBase::LoadRelaxed(); 116 } 117 118 void StoreRelaxed(T* val) 119 { 120 AtomicPtrBase::StoreRelaxed(val); 121 } 122 }; 123 }