Stefan Scholz / ETL
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers atomic_windows.h Source File

atomic_windows.h

00001 /******************************************************************************
00002 The MIT License(MIT)
00003 
00004 Embedded Template Library.
00005 https://github.com/ETLCPP/etl
00006 https://www.etlcpp.com
00007 
00008 Copyright(c) 2017 jwellbelove
00009 
00010 Permission is hereby granted, free of charge, to any person obtaining a copy
00011 of this software and associated documentation files(the "Software"), to deal
00012 in the Software without restriction, including without limitation the rights
00013 to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
00014 copies of the Software, and to permit persons to whom the Software is
00015 furnished to do so, subject to the following conditions :
00016 
00017 The above copyright notice and this permission notice shall be included in all
00018 copies or substantial portions of the Software.
00019 
00020 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00021 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00022 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
00023 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00024 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00025 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
00026 SOFTWARE.
00027 ******************************************************************************/
00028 
00029 #ifndef __ETL_ATOMIC_WINDOWS__
00030 #define __ETL_ATOMIC_WINDOWS__
00031 
00032 #include "../platform.h"
00033 
00034 #include <stdint.h>
00035 #include <Windows.h>
00036 
00037 namespace etl
00038 {
00039   class atomic_uint32_t
00040   {
00041   public:
00042 
00043     atomic_uint32_t()
00044     {
00045       InterlockedExchange(&value, 0);
00046     }
00047 
00048     atomic_uint32_t(uint32_t v)
00049     {
00050       InterlockedExchange(&value, v);
00051     }
00052 
00053     atomic_uint32_t& operator =(uint32_t v)
00054     {
00055       InterlockedExchange(&value, v);
00056 
00057       return *this;
00058     }
00059 
00060     atomic_uint32_t& operator ++()
00061     {
00062       InterlockedIncrement(&value);
00063 
00064       return *this;
00065     }
00066 
00067     volatile atomic_uint32_t& operator ++() volatile
00068     {
00069       InterlockedIncrement(&value);
00070 
00071       return *this;
00072     }
00073 
00074     atomic_uint32_t& operator --()
00075     {
00076       InterlockedDecrement(&value);
00077 
00078       return *this;
00079     }
00080 
00081     volatile atomic_uint32_t& operator --() volatile
00082     {
00083       InterlockedDecrement(&value);
00084 
00085       return *this;
00086     }
00087 
00088     operator uint32_t () const
00089     {
00090       return InterlockedAdd((volatile long*)&value, 0);
00091     }
00092 
00093     operator uint32_t() volatile const
00094     {
00095       return InterlockedAdd((volatile long*)&value, 0);
00096     }
00097 
00098   private:
00099 
00100     uint32_t value;
00101   };
00102 }
00103 
00104 #endif
00105