原子操作
所谓原子操作,就是该操作绝不会在执行完毕前被任何其他任务或事件打断,也就说,它的最小的执行单位,不可能有比它更小的执行单位,因此这里的原子实际是使用了物理学里的物质微粒的概念。
原子操作需要硬件的支持,因此是架构相关的,其API和原子类型的定义都定义在内核源码树的include/asm/atomic.h文件中,它们都使用汇编语言实现,因为C语言并不能实现这样的操作。
在linux中提供了两种形式的原子操作:
一种是对整数进行的操作
一种是对单独的位进行操作
原子操作主要用于实现资源计数,很多引用计数(refcnt)就是通过原子操作实现的。原子类型定义如下:
typedef struct {
volatile int counter;
} atomic_t;
volatile修饰字段告诉gcc不要对该类型的数据做优化处理,对它的访问都是对内存的访问,而不是对寄存器的访问。
原子整数操作的使用:
常见的用途是计数器,因为计数器是一个很简单的操作,所以无需复杂的锁机制;
能使用原子操作的地方,尽量不使用复杂的锁机制;
对atomic_t类型的变量的使用方法以及对其所能进行的操作:
下面是相应的函数及其原型:
小提示:
在其函数的实现体中,有一个LOCK_PREFIX宏定义,如果选了CONFIG_SMP,就会定义这么一个宏,与SMP相关的一些设置,否则LOCK_PREFIX的定义就为空;
原子操作API包括:
//原子的读取atomic_t变量的值,v是这个变量的地址
#define atomic_read(v) ((v)->counter)
//原子的设置atomic_t变量的值,v是这个变量的地址,i要设置的新值;
#define atomic_set(v,i) (((v)->counter) = (i))
//原子的增加atomic_t变量的值,i是要增加的数值,v是这个变量的地址
static __inline__ void atomic_add(int i, atomic_t *v)
{
__asm__ __volatile__(
LOCK_PREFIX "addl %1,%0"
:"=m" (v->counter)
:"ir" (i), "m" (v->counter));
}
//原子的减少atomic_t变量的值,i是要减少的数值,v是这个变量的地址;
static __inline__ void atomic_sub(int i, atomic_t *v)
{
__asm__ __volatile__(
LOCK_PREFIX "subl %1,%0"
:"=m" (v->counter)
:"ir" (i), "m" (v->counter));
}
//原子的对atomic_t变量的值进行加1的操作,v是这个变量的地址;
static __inline__ void atomic_inc(atomic_t *v)
{
__asm__ __volatile__(
LOCK_PREFIX "incl %0"
:"=m" (v->counter)
:"m" (v->counter));
}
//原子的对atomic_t变量的值进行减1的操作,v是这个变量的地址;
static __inline__ void atomic_dec(atomic_t *v)
{
__asm__ __volatile__(
LOCK_PREFIX "decl %0"
:"=m" (v->counter)
:"m" (v->counter));
}
//原子的对atomic_t变量的值进行减少i的操作,并且检测其结果是否为0;若为0,返回true,否则,返回false;
//i是要减少的数值,v是这个变量的地址;
static __inline__ int atomic_sub_and_test(int i, atomic_t *v)
{
unsigned char c;
__asm__ __volatile__(
LOCK_PREFIX "subl %2,%0; sete %1"
:"=m" (v->counter), "=qm" (c)
:"ir" (i), "m" (v->counter) : "memory");
return c;
}
//原子的对atomic_t变量的值进行减少1的操作,并且检测其结果是否为0;若为0,返回true,否则,返回false;
//v是这个变量的地址;
static __inline__ int atomic_dec_and_test(atomic_t *v)
{
unsigned char c;
__asm__ __volatile__(
LOCK_PREFIX "decl %0; sete %1"
:"=m" (v->counter), "=qm" (c)
:"m" (v->counter) : "memory");
return c != 0;
}
//原子的对atomic_t变量的值进行加1的操作,并且检测其结果是否为0;若为0,返回true,否则,返回false;
//v是这个变量的地址;
static __inline__ int atomic_inc_and_test(atomic_t *v)
{
unsigned char c;
__asm__ __volatile__(
LOCK_PREFIX "incl %0; sete %1"
:"=m" (v->counter), "=qm" (c)
:"m" (v->counter) : "memory");
return c != 0;
}
//原子的对atomic_t变量的值进行加i的操作,并且检测其结果是否为负;若为负,返回true,否则,返回false;
//i是要增加的数值,v是这个变量的地址;
static __inline__ int atomic_add_negative(int i, atomic_t *v)
{
unsigned char c;
__asm__ __volatile__(
LOCK_PREFIX "addl %2,%0; sets %1"
:"=m" (v->counter), "=qm" (c)
:"ir" (i), "m" (v->counter) : "memory");
return c;
}
//原子的对atomic_t变量的值进行加i的操作,并且将所得结果返回;
//i是要增加的数值,v是这个变量的地址;
static __inline__ int atomic_add_return(int i, atomic_t *v)
{
int __i;
#ifdef CONFIG_M386
unsigned long flags;
if(unlikely(boot_cpu_data.x86==3))
goto no_xadd;
#endif
/* Modern 486+ processor */
__i = i;
__asm__ __volatile__(
LOCK_PREFIX "xaddl %0, %1;"
:"=r"(i)
:"m"(v->counter), "0"(i));
return i + __i;
#ifdef CONFIG_M386
no_xadd: /* Legacy 386 processor */
local_irq_save(flags);
__i = atomic_read(v);
atomic_set(v, i + __i);
local_irq_restore(flags);
return i + __i;
#endif
}
//原子的对atomic_t变量的值进行减i的操作,并且将所得结果返回;
//i是要减少的数值,v是这个变量的地址;
static __inline__ int atomic_sub_return(int i, atomic_t *v)
{
return atomic_add_return(-i,v);
}
//原子的比较old与v是否相等,若相等,则把new的值写入到v中,并且返回old的值;
#define atomic_cmpxchg(v, old, new) ((int)cmpxchg(&((v)->counter), old, new))
//原子的比较
#define atomic_xchg(v, new) (xchg(&((v)->counter), new))
/*
*atomic_add_unless(v, a, u)
* atomic_add_unless - add unless the number is a given value
* @v: pointer of type atomic_t
* @a: the amount to add to v...
* @u: ...unless v is equal to u.
*
* Atomically adds @a to @v, so long as it was not @u.
* Returns non-zero if @v was not @u, and zero otherwise.
*/
//原子的对atomic_t变量的值进行加a的操作,直到v等于u,如果v与u不等,返回非零值;否则返回0;
//a是要增加的数值,v是这个变量的地址,u是要比较的值;
#define atomic_add_unless(v, a, u) \
({ \
int c, old; \
c = atomic_read(v); \
for (;;) { \
if (unlikely(c == (u))) \
break; \
old = atomic_cmpxchg((v), c, c + (a)); \
if (likely(old == c)) \
break; \
c = old; \
} \
c != (u); \
})
#define atomic_inc_not_zero(v) atomic_add_unless((v), 1, 0)
#define atomic_inc_return(v) (atomic_add_return(1,v))
#define atomic_dec_return(v) (atomic_sub_return(1,v))
//掩码
/* These are x86-specific, used by some header files */
#define atomic_clear_mask(mask, addr) \
__asm__ __volatile__(LOCK_PREFIX "andl %0,%1" \
: : "r" (~(mask)),"m" (*addr) : "memory")
#define atomic_set_mask(mask, addr) \
__asm__ __volatile__(LOCK_PREFIX "orl %0,%1" \
: : "r" (mask),"m" (*(addr)) : "memory")
下面举例说明原子操作的用法:
定义一个atomic_c类型的数据很简单,还可以定义时给它设定初值:
(1) atomic_t u; /*定义 u*/
(2) atomic_t v = ATOMIC_INIT(0) /*定义 v 并把它初始化为0*/
对其操作:
(1) atomic_set(&v,4) /* v = 4 ( 原子地)*/
(2) atomic_add(2,&v) /* v = v + 2 = 6 (原子地) */
(3) atomic_inc(&v) /* v = v + 1 =7(原子地)*/
如果需要将atomic_t转换成int型,可以使用atomic_read()来完成:
printk(“%d\n”,atomic_read(&v)); /* 会打印7*/
原子整数操作最常见的用途就是实现计数器。使用复杂的锁机制来保护一个单纯的计数器是很笨拙的,所以,开发者最好使用atomic_inc()和atomic_dec()这两个相对来说轻便一点的操作。
还可以用原子整数操作原子地执行一个操作并检查结果。一个常见的例子是原子的减操作和检查。
int atomic_dec_and_test(atomic_t *v)
这个函数让给定的原子变量减1,如果结果为0,就返回1;否则返回0