linux 原子整数操作详解 及 volatile (二)
2019獨(dú)角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>
原子操作,顧名思義,就是說像原子一樣不可再細(xì)分不可被中途打斷。一個操作是原子操作,意思就是說這個操作是以原子的方式被執(zhí)行,要一口氣執(zhí)行完,執(zhí)行過程不能夠被OS的其他行為打斷,是一個整體的過程,在其執(zhí)行過程中,OS的其它行為是插不進(jìn)來的。
在linux中提供了兩種形式的原子操作:
??? 一種是對整數(shù)進(jìn)行的操作
??? 一種是對單獨(dú)的位進(jìn)行操作
在linux中有一個專門的atomic_t類型(一個24位原子訪問計(jì)數(shù)器)和一些對atomic類型變量進(jìn)行相應(yīng)操作的的函數(shù)
其atomic_t原型如下:
??? typedef struct { volatile int counter; } atomic_t;
它是一個只含有一個volatile類型的成員變量的結(jié)構(gòu)體;因此編譯器不對相應(yīng)的值進(jìn)行訪問優(yōu)化(因?yàn)槭莢olatile類型的)。
原子整數(shù)操作的使用:
??? 常見的用途是計(jì)數(shù)器,因?yàn)橛?jì)數(shù)器是一個很簡單的操作,所以無需復(fù)雜的鎖機(jī)制;
??? 能使用原子操作的地方,盡量不使用復(fù)雜的鎖機(jī)制;
對atomic_t類型的變量的使用方法以及對其所能進(jìn)行的操作:
下面是相應(yīng)的函數(shù)及其原型:
小提示:
******
在其函數(shù)的實(shí)現(xiàn)體中,有一個LOCK_PREFIX宏定義,如果選了CONFIG_SMP,就會定義這么一個宏,與SMP相關(guān)的一些設(shè)置,否則LOCK_PREFIX的定義就為空;
******
//原子的讀取atomic_t變量的值,v是這個變量的地址
#define atomic_read(v)??????? ((v)->counter)
//原子的設(shè)置atomic_t變量的值,v是這個變量的地址,i要設(shè)置的新值;
#define atomic_set(v,i)??????? (((v)->counter) = (i))
//原子的增加atomic_t變量的值,i是要增加的數(shù)值,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是要減少的數(shù)值,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變量的值進(jìn)行減少i的操作,并且檢測其結(jié)果是否為0;若為0,返回true,否則,返回false;
//i是要減少的數(shù)值,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變量的值進(jìn)行加1的操作,v是這個變量的地址;
static __inline__ void atomic_inc(atomic_t *v)
{
??? __asm__ __volatile__(
??????? LOCK_PREFIX "incl %0"
??????? :"=m" (v->counter)
??????? :"m" (v->counter));
}
//原子的對atomic_t變量的值進(jìn)行減1的操作,v是這個變量的地址;
static __inline__ void atomic_dec(atomic_t *v)
{
??? __asm__ __volatile__(
??????? LOCK_PREFIX "decl %0"
??????? :"=m" (v->counter)
??????? :"m" (v->counter));
}
//原子的對atomic_t變量的值進(jìn)行減少1的操作,并且檢測其結(jié)果是否為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變量的值進(jìn)行加1的操作,并且檢測其結(jié)果是否為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變量的值進(jìn)行加i的操作,并且檢測其結(jié)果是否為負(fù);若為負(fù),返回true,否則,返回false;
//i是要增加的數(shù)值,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變量的值進(jìn)行加i的操作,并且將所得結(jié)果返回;
//i是要增加的數(shù)值,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變量的值進(jìn)行減i的操作,并且將所得結(jié)果返回;
//i是要減少的數(shù)值,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 - 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變量的值進(jìn)行加a的操作,直到v等于u,如果v與u不等,返回非零值;否則返回0;
//a是要增加的數(shù)值,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")
轉(zhuǎn)載于:https://my.oschina.net/u/1178070/blog/317529
總結(jié)
以上是生活随笔為你收集整理的linux 原子整数操作详解 及 volatile (二)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: The Historical Accid
- 下一篇: [Linux] Centos DenyH