linux_xiong 发表于 2011-03-23 10:35

原子操作

本帖最后由 linux_xiong 于 2011-03-23 10:38 编辑

static atomic_t xxx_available = ATOMIC_INIT(1);

static int xxx_open(struct inode *inode,struct file *filp)
{
...
if(!atomic_dec_and_test(&xxx_available)){   //这里的测试原子会减吗?
atomic_inc(&xxx_available);   //如果减了这里又加了
return - EBUSY;
}
static int xxx_release(struct inode *inode,struct file *filp)
{
atomic_inc(&xxx_available);   //这里又加了
return 0;
}

新手求解

dreamice 发表于 2011-03-24 11:22

回复 1# linux_xiong /**
* 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)
{
        unsigned char c;

      /* 对v->counter减一 ,同时赋值给c */
        asm volatile(LOCK_PREFIX "decl %0; sete %1"
                     : "+m" (v->counter), "=qm" (c)
                     : : "memory");
       /* 如果结果为0,返回真,非0为假*/
        return c != 0;
}上面是这个函数在x86结构中的实现。

灌水菜鸟 发表于 2011-03-25 22:18

页: [1]
查看完整版本: 原子操作