---
title: "atomic笔记"
author: "raojl"
date: "2013-12-11T11:43:14Z"
tags: []
categories: ["编程语言"]
slug: "atomic"
draft: false
lastmod: "2013-12-11T11:43:13Z"
id: 1988044
url: "https://raojl.iteye.com/blog/1988044"
---

inline void ice\_atomic\_set(ice\_atomic\_t* v, int i)

{

    v-&gt;counter = i;

}

 

/*

 * ice\_atomic\_inc - increment ice\_atomic variable

 * @v: pointer of type ice\_atomic\_t

 * 

 * Atomically increments @v by 1. Note that the guaranteed useful

 * range of an ice\_atomic\_t is only 24 bits.

 *

 * Inlined because this operation is performance critical.

 \*/

inline void ice\_atomic\_inc(ice\_atomic\_t \*v)

{

    \_\_asm\__ \_\_volatile\_\_(

        "lock ; incl %0"

        :"=m" (v-&gt;counter)

        :"m" (v-&gt;counter));

}

 

/\**

 * ice\_atomic\_dec\_and\_test - decrement and test

 * @v: pointer of type ice\_atomic\_t

 * 

 * Atomically decrements @v by 1 and returns true if the result is 0,

 * or false for all other cases. Note that the guaranteed useful

 * range of an ice\_atomic\_t is only 24 bits.

 *

 * Inlined because this operation is performance critical.

 \*/

inline int ice\_atomic\_dec\_and\_test(ice\_atomic\_t \*v)

{

    unsigned char c;

    \_\_asm\__ \_\_volatile\_\_(

        "lock ; decl %0; sete %1"

        :"=m" (v-&gt;counter), "=qm" (c)

        :"m" (v-&gt;counter) : "memory");

    return c != 0;

}

 

/\**

 * ice\_atomic\_exchange\_add - same as InterlockedExchangeAdd. This

 * didn't come from atomic.h (the code was derived from similar code

 * in /usr/include/asm/rwsem.h)

 *

 * Inlined because this operation is performance critical.

 \*/

inline int ice\_atomic\_exchange\_add(int i, ice\_atomic\_t* v)

{

    int tmp = i;

    \_\_asm\__ \_\_volatile\_\_(

        "lock ; xadd %0,(%2)"

        :"+r"(tmp), "=m"(v-&gt;counter)

        :"r"(v), "m"(v-&gt;counter)

        : "memory");

    return tmp + i;

}