Linux内核源码(asm/atomic.h)学习

来源:互联网 发布:淘宝宾卡达手表怎么样 编辑:程序博客网 时间:2024/06/09 13:47

由于现在正在看Linux下的内核同步方法,其中第一个提到的就是原子变量,其中会有原子操作.其中原子变量被定义在linux/types.h头文件中,在这一篇博客中,主要学习原子操作,这些原子操作的函数被定义在asm/atomic.h文件中,其中包括,初始化,原子读,原子更改等操作,下面我们来看一下内核源码,其中,有我的一些注释,这个是比较简单的,因为,该原子变量的操作是由体系结构的指令操作的.只因为他支持这样的原子操作,才使得使用起来比较简单.

话不多说,直接上代码:

/* * Generic C implementation of atomic counter operations * Originally implemented for MN10300. * * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved. * Written by David Howells (dhowells@redhat.com) * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public Licence * as published by the Free Software Foundation; either version * 2 of the Licence, or (at your option) any later version. * * 该程序是自由软件;你可以重新发布它或者是在GNU协议下修改它. * 该程序实现了一些原子操作的函数. */#ifndef __ASM_GENERIC_ATOMIC_H#define __ASM_GENERIC_ATOMIC_H#ifdef CONFIG_SMP#error not SMP safe#endif/* * Atomic operations that C can't guarantee us.  Useful for * resource counting etc.. * C不能保证给我们的原子操作.对于资源计数等是有用的. *//* * 初始化atomic_t的值 * 举例: * atomic_t v = ATOMIC_INIT(0); //定义v并把它初始化为0 */#define ATOMIC_INIT(i)  { (i) }#ifdef __KERNEL__/** * atomic_read - read atomic variable * @v: pointer of type atomic_t * atomic_read - 读取atomic变量 * @v: 指向类型atomic_t的指针 * * Atomically reads the value of @v.  Note that the guaranteed * useful range of an atomic_t is only 24 bits. * 原子的读取v的值.注意,一个atomic_t保证的有用的范围只有24位  * */#define atomic_read(v)  ((v)->counter)/** * atomic_set - set atomic variable * @v: pointer of type atomic_t * @i: required value * * atomic_set - 设置atomic变量 * @v: 指向类型atomic_t的指针 * @i: 需要的值 * * Atomically sets the value of @v to @i.  Note that the guaranteed * useful range of an atomic_t is only 24 bits. * 原子的设置v的值为i. 注意,一个atomic_t保证的有用的范围只有24位  * */#define atomic_set(v, i) (((v)->counter) = (i))#include <asm/system.h>/** * atomic_add_return - add integer to atomic variable * @i: integer value to add * @v: pointer of type atomic_t * aotmic_Add_return - 向atomic变量中加一个整数 * @i: 要添加的整数 * @v: 指向类型atomic_t的指针 * * Atomically adds @i to @v and returns the result * Note that the guaranteed useful range of an atomic_t is only 24 bits. * 原子的将i加到v中,返回结果 * 注意,一个atomic_t保证的有用的范围只有24位  */static inline int atomic_add_return(int i, atomic_t *v){    unsigned long flags;    int temp;    local_irq_save(flags);    temp = v->counter;    temp += i;    v->counter = temp;    local_irq_restore(flags);    return temp;}/** * atomic_sub_return - subtract integer from atomic variable * @i: integer value to subtract * @v: pointer of type atomic_t * atomic_sub_return - 从原子变量中减去一个整数 * @i: 要被减去的整数 * @v: 指向类型atomic_t的指针 * * Atomically subtracts @i from @v and returns the result * Note that the guaranteed useful range of an atomic_t is only 24 bits. */static inline int atomic_sub_return(int i, atomic_t *v){    unsigned long flags;    int temp;    local_irq_save(flags);    temp = v->counter;    temp -= i;    v->counter = temp;    local_irq_restore(flags);    return temp;}static inline int atomic_add_negative(int i, atomic_t *v){    return atomic_add_return(i, v) < 0;}static inline void atomic_add(int i, atomic_t *v){    atomic_add_return(i, v);}static inline void atomic_sub(int i, atomic_t *v){    atomic_sub_return(i, v);}//自加1static inline void atomic_inc(atomic_t *v){    atomic_add_return(1, v);}//自减1static inline void atomic_dec(atomic_t *v){    atomic_sub_return(1, v);}#define atomic_dec_return(v)        atomic_sub_return(1, (v))#define atomic_inc_return(v)        atomic_add_return(1, (v))#define atomic_sub_and_test(i, v)   (atomic_sub_return((i), (v)) == 0)#define atomic_dec_and_test(v)      (atomic_sub_return(1, (v)) == 0)#define atomic_inc_and_test(v)      (atomic_add_return(1, (v)) == 0)#define atomic_add_unless(v, a, u)              \({                              \    int c, old;                     \    c = atomic_read(v);                 \    while (c != (u) && (old = atomic_cmpxchg((v), c, c + (a))) != c) \        c = old;                    \    c != (u);                       \})#define atomic_inc_not_zero(v) atomic_add_unless((v), 1, 0)static inline void atomic_clear_mask(unsigned long mask, unsigned long *addr){    unsigned long flags;    mask = ~mask;    local_irq_save(flags);    *addr &= mask;    local_irq_restore(flags);}#define atomic_xchg(ptr, v)     (xchg(&(ptr)->counter, (v)))#define atomic_cmpxchg(v, old, new) (cmpxchg(&((v)->counter), (old), (new)))#define cmpxchg_local(ptr, o, n)                           \    ((__typeof__(*(ptr)))__cmpxchg_local_generic((ptr), (unsigned long)(o),\            (unsigned long)(n), sizeof(*(ptr))))#define cmpxchg64_local(ptr, o, n) __cmpxchg64_local_generic((ptr), (o), (n))/* Assume that atomic operations are already serializing *//* 假设原子操作已经被序列化了 */#define smp_mb__before_atomic_dec() barrier()#define smp_mb__after_atomic_dec()  barrier()#define smp_mb__before_atomic_inc() barrier()#define smp_mb__after_atomic_inc()  barrier()#include <asm-generic/atomic-long.h>#endif /* __KERNEL__ */#endif /* __ASM_GENERIC_ATOMIC_H */

通过这个,基本上就可以使用原子操作了.下面列举一个原子操作的例子.

#include <asm/atomic.h>#include <linux/types.h>int main(){  atomic_t v = ATOMIC_INIT(0); //新建一个atomic_t变量,并且初始化为0 int i = atomic_sub_return(2, &v); return 0;}

这个仅仅就是一个例子程序,不一定能运行的起来,因为这个是在内核中使用的代码.

0 0
原创粉丝点击