interlocked_variable.h

Go to the documentation of this file.
00001 /*
00002 **  ClanLib SDK
00003 **  Copyright (c) 1997-2011 The ClanLib Team
00004 **
00005 **  This software is provided 'as-is', without any express or implied
00006 **  warranty.  In no event will the authors be held liable for any damages
00007 **  arising from the use of this software.
00008 **
00009 **  Permission is granted to anyone to use this software for any purpose,
00010 **  including commercial applications, and to alter it and redistribute it
00011 **  freely, subject to the following restrictions:
00012 **
00013 **  1. The origin of this software must not be misrepresented; you must not
00014 **     claim that you wrote the original software. If you use this software
00015 **     in a product, an acknowledgment in the product documentation would be
00016 **     appreciated but is not required.
00017 **  2. Altered source versions must be plainly marked as such, and must not be
00018 **     misrepresented as being the original software.
00019 **  3. This notice may not be removed or altered from any source distribution.
00020 **
00021 **  Note: Some of the libraries ClanLib may link to may have additional
00022 **  requirements or restrictions.
00023 **
00024 **  File Author(s):
00025 **
00026 **    Magnus Norddahl
00027 */
00028 
00031 
00032 #pragma once
00033 
00034 #include "../api_core.h"
00035 #include "system.h"
00036 
00037 #ifdef WIN32
00038 #include <windows.h>
00039 #else
00040 #include "../System/cl_platform.h"
00041 #endif
00042 
00043 // __sync_val_compare_and_swap was not introduced until gcc 4.1
00044 #if defined(WIN32) || __GNUC__ > 4 ||  (__GNUC__ == 4 & __GNUC_MINOR__ >= 1)
00045 
00049 class CL_InterlockedVariable
00050 {
00051 public:
00052 #ifdef WIN32
00053         CL_InterlockedVariable()
00054         : val((LONG*)CL_System::aligned_alloc(sizeof(LONG), 4))
00055         {
00056                 set(0);
00057         }
00058 
00059         CL_InterlockedVariable(const CL_InterlockedVariable &src)
00060         : val((LONG*)CL_System::aligned_alloc(sizeof(LONG), 4))
00061         {
00062                 set(src.get());
00063         }
00064 
00065         ~CL_InterlockedVariable()
00066         {
00067                 CL_System::aligned_free((void*)val);
00068         }
00069 
00070         CL_InterlockedVariable &operator =(const CL_InterlockedVariable &src)
00071         {
00072                 set(src.get());
00073                 return *this;
00074         }
00075 
00076         LONG get() const
00077         {
00078                 return InterlockedCompareExchange(val, 0, 0);
00079         }
00080 
00081         void set(LONG new_value)
00082         {
00083                 InterlockedExchange(val, new_value);
00084         }
00085 
00086         LONG increment()
00087         {
00088                 return InterlockedIncrement(val);
00089         }
00090 
00091         LONG decrement()
00092         {
00093                 return InterlockedDecrement(val);
00094         }
00095 
00096         bool compare_and_swap(LONG expected_value, LONG new_value)
00097         {
00098                 return InterlockedCompareExchange(val, new_value, expected_value) == expected_value;
00099         }
00100 
00101 private:
00102         volatile LONG *val;
00103 
00104 #else
00105         CL_InterlockedVariable()
00106         : val((cl_byte32*)CL_System::aligned_alloc(sizeof(cl_byte32), 4))
00107         {
00108                 set(0);
00109         }
00110 
00111         CL_InterlockedVariable(const CL_InterlockedVariable &src)
00112         : val((cl_byte32*)CL_System::aligned_alloc(sizeof(cl_byte32), 4))
00113         {
00114                 set(src.get());
00115         }
00116 
00117         ~CL_InterlockedVariable()
00118         {
00119                 CL_System::aligned_free((void*)val);
00120         }
00121 
00122         CL_InterlockedVariable &operator =(const CL_InterlockedVariable &src)
00123         {
00124                 set(src.get());
00125                 return *this;
00126         }
00127 
00128         int get() const
00129         {
00130                 return __sync_val_compare_and_swap(val, 0, 0);
00131         }
00132 
00133         void set(int new_value)
00134         {
00135                 __sync_lock_test_and_set(val, new_value);
00136         }
00137 
00138         int increment()
00139         {
00140                 return __sync_add_and_fetch(val, 1);
00141         }
00142 
00143         int decrement()
00144         {
00145                 return __sync_sub_and_fetch(val, 1);
00146         }
00147 
00148         bool compare_and_swap(int expected_value, int new_value)
00149         {
00150                 return __sync_bool_compare_and_swap(val, expected_value,  new_value);
00151         }
00152 
00153 private:
00154         volatile cl_byte32 *val;
00155 
00156 #endif
00157 };
00158 
00159 #endif
00160