178 lines
3.9 KiB
C
178 lines
3.9 KiB
C
/*
|
|
* =====================================================================================
|
|
*
|
|
* Filename: atomic_gcc.h
|
|
*
|
|
* Description: atomic_gcc class definition.
|
|
*
|
|
* Version: 1.0
|
|
* Created: 09/08/2018
|
|
* Revision: none
|
|
* Compiler: gcc
|
|
*
|
|
* Author: zhulin, shzhulin3@jd.com
|
|
* Company: JD.com, Inc.
|
|
*
|
|
* =====================================================================================
|
|
*/
|
|
|
|
#ifndef __ARCH_I386_ATOMIC__
|
|
#define __ARCH_I386_ATOMIC__
|
|
|
|
#include <sys/cdefs.h>
|
|
__BEGIN_DECLS
|
|
|
|
/*
|
|
* Make sure gcc doesn't try to be clever and move things around
|
|
* on us. We need to use _exactly_ the address the user gave us,
|
|
* not some alias that contains the same information.
|
|
*/
|
|
typedef volatile int atomic_t;
|
|
|
|
#define ATOMIC_INIT(i) { (i) }
|
|
|
|
/**
|
|
* atomic_read - read atomic variable
|
|
* @v: pointer of type atomic_t
|
|
*
|
|
* Atomically reads the value of @v.
|
|
*/
|
|
#define atomic_read(v) (*(v))
|
|
|
|
/**
|
|
* atomic_set - set atomic variable
|
|
* @v: pointer of type atomic_t
|
|
* @i: required value
|
|
*
|
|
* Atomically sets the value of @v to @i.
|
|
*/
|
|
#define atomic_set(v,i) (*(v) = (i))
|
|
|
|
static __inline__ int atomic_clear(atomic_t *v)
|
|
{
|
|
return __sync_fetch_and_and(v, 0);
|
|
}
|
|
|
|
/**
|
|
* atomic_add - add integer to atomic variable
|
|
* @i: integer value to add
|
|
* @v: pointer of type atomic_t
|
|
*
|
|
* Atomically adds @i to @v.
|
|
*/
|
|
static __inline__ int atomic_add(int i, atomic_t *v)
|
|
{
|
|
return __sync_fetch_and_add(v, i);
|
|
}
|
|
|
|
/**
|
|
* atomic_sub - subtract the atomic variable
|
|
* @i: integer value to subtract
|
|
* @v: pointer of type atomic_t
|
|
*
|
|
* Atomically subtracts @i from @v.
|
|
*/
|
|
static __inline__ int atomic_sub(int i, atomic_t *v)
|
|
{
|
|
return __sync_fetch_and_sub(v, i);
|
|
}
|
|
|
|
/**
|
|
* atomic_sub_and_test - subtract value from variable and test result
|
|
* @i: integer value to subtract
|
|
* @v: pointer of type atomic_t
|
|
*
|
|
* Atomically subtracts @i from @v and returns
|
|
* true if the result is zero, or false for all
|
|
* other cases.
|
|
*/
|
|
static __inline__ int atomic_sub_and_test(int i, atomic_t *v)
|
|
{
|
|
return __sync_sub_and_fetch(v, i) == 0;
|
|
}
|
|
|
|
/**
|
|
* atomic_inc - increment atomic variable
|
|
* @v: pointer of type atomic_t
|
|
*
|
|
* Atomically increments @v by 1.
|
|
*/
|
|
static __inline__ int atomic_inc(atomic_t *v)
|
|
{
|
|
return __sync_fetch_and_add(v, 1);
|
|
}
|
|
|
|
/**
|
|
* atomic_dec - decrement atomic variable
|
|
* @v: pointer of type atomic_t
|
|
*
|
|
* Atomically decrements @v by 1.
|
|
*/
|
|
static __inline__ int atomic_dec(atomic_t *v)
|
|
{
|
|
return __sync_fetch_and_sub(v, 1);
|
|
}
|
|
|
|
/**
|
|
* atomic_dec_and_test - decrement and test
|
|
* @v: pointer of type atomic_t
|
|
*
|
|
* Atomically decrements @v by 1 and
|
|
* returns true if the result is 0, or false for all other
|
|
* cases.
|
|
*/
|
|
static __inline__ int atomic_dec_and_test(atomic_t *v)
|
|
{
|
|
return __sync_sub_and_fetch(v, 1) == 0;
|
|
}
|
|
|
|
/**
|
|
* atomic_inc_and_test - increment and test
|
|
* @v: pointer of type atomic_t
|
|
*
|
|
* Atomically increments @v by 1
|
|
* and returns true if the result is zero, or false for all
|
|
* other cases.
|
|
*/
|
|
static __inline__ int atomic_inc_and_test(atomic_t *v)
|
|
{
|
|
return __sync_add_and_fetch(v, 1) == 0;
|
|
}
|
|
|
|
/**
|
|
* atomic_add_negative - add and test if negative
|
|
* @v: pointer of type atomic_t
|
|
* @i: integer value to add
|
|
*
|
|
* Atomically adds @i to @v and returns true
|
|
* if the result is negative, or false when
|
|
* result is greater than or equal to zero.
|
|
*/
|
|
static __inline__ int atomic_add_negative(int i, atomic_t *v)
|
|
{
|
|
return __sync_add_and_fetch(v, i) < 0;
|
|
}
|
|
|
|
/**
|
|
* atomic_add_return - add and return
|
|
* @v: pointer of type atomic_t
|
|
* @i: integer value to add
|
|
*
|
|
* Atomically adds @i to @v and returns @i + @v
|
|
*/
|
|
static __inline__ int atomic_add_return(int i, atomic_t *v)
|
|
{
|
|
return __sync_add_and_fetch(v, i);
|
|
}
|
|
|
|
static __inline__ int atomic_sub_return(int i, atomic_t *v)
|
|
{
|
|
return __sync_sub_and_fetch(v, i);
|
|
}
|
|
|
|
#define atomic_inc_return(v) (atomic_add_return(1,v))
|
|
#define atomic_dec_return(v) (atomic_sub_return(1,v))
|
|
|
|
__END_DECLS
|
|
#endif
|