Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
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
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