interlocked_variable.h

Go to the documentation of this file.
00001 /*
00002 **  ClanLib SDK
00003 **  Copyright (c) 1997-2009 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 #ifndef WIN32
00035 #include "../IOData/datatypes.h"
00036 #endif
00037 
00041 class CL_InterlockedVariable
00042 {
00043 public:
00044         CL_InterlockedVariable()
00045         {
00046                 set(0);
00047         }
00048 
00049 #ifdef WIN32
00050         LONG get()
00051         {
00052                 return InterlockedCompareExchange(&val, 0, 0);
00053         }
00054 
00055         void set(LONG new_value)
00056         {
00057                 InterlockedExchange(&val, new_value);
00058         }
00059 
00060         LONG increment()
00061         {
00062                 return InterlockedIncrement(&val);
00063         }
00064 
00065         LONG decrement()
00066         {
00067                 return InterlockedDecrement(&val);
00068         }
00069 
00070         bool compare_and_swap(LONG expected_value, LONG new_value)
00071         {
00072                 return InterlockedCompareExchange(&val, new_value, expected_value) == expected_value;
00073         }
00074 
00075 private:
00076         __declspec(align(32)) volatile LONG val;
00077 
00078 #else
00079         int get()
00080         {
00081                 return __sync_val_compare_and_swap(&val, 0, 0);
00082         }
00083 
00084         void set(int new_value)
00085         {
00086                 __sync_lock_test_and_set(&val, new_value);
00087         }
00088 
00089         int increment()
00090         {
00091                 return __sync_fetch_and_add(&val, 1) + 1;
00092         }
00093 
00094         int decrement()
00095         {
00096                 return __sync_fetch_and_add(&val, -1) - 1;
00097         }
00098 
00099         bool compare_and_swap(int expected_value, int new_value)
00100         {
00101                 return __sync_val_compare_and_swap(&val, new_value, expected_value) == expected_value;
00102         }
00103 
00104 private:
00105         __attribute__ ((aligned(32))) volatile cl_int32 val;
00106 
00107 #endif
00108 };
00109 

Generated on Thu Dec 3 02:39:29 2009 for ClanLib by  doxygen 1.4.6