problem with atomic_cmpxchg

Hi, I need to implement a semaphore, I write this code, but gives me error, the code is this:

#pragma OPENCL EXTENSION cl_khr_global_int32_base_atomics : enable

#pragma OPENCL EXTENSION cl_khr_local_int32_base_atomics : enable

#pragma OPENCL EXTENSION cl_khr_global_int32_extended_atomics : enable

#pragma OPENCL EXTENSION cl_khr_local_int32_extended_atomics : enable

__kernel void Name(……){
volatile __global int *mutex;
int id = get_global_id(0);
int begin, end,cont;

			if (atomic_cmpxchg(&mutex, 0, 1) == 0) 
				{
																				
																	
					if(id==0)
					{
						begin=0;
						end=4*M;
						cont=0;
					}
					else
					{
						begin=id*(2*M);
						end=begin+(4*M);
						if(end>length)
							end=length;
						cont=id*(4*M);
					}
							
							
						atomic_xchg(&mutex, 0);
				}

…….
}
And the error is: CL_BUILD_PROGRAM_FAILURE
Please Help Meeeeeeeeee

Try atomic_cmpxchg(mutex, …)

thank you!!! that resolve the problem!!!