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

简介: 版权声明:您好,转载请留下本人博客的地址,谢谢 https://blog.csdn.net/hongbochen1223/article/details/49072767 由于现在正...
版权声明:您好,转载请留下本人博客的地址,谢谢 https://blog.csdn.net/hongbochen1223/article/details/49072767

由于现在正在看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);
}

//自加1
static inline void atomic_inc(atomic_t *v)
{
    atomic_add_return(1, v);
}

//自减1
static 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;
}

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

目录
相关文章
|
22小时前
|
存储 算法 Linux
【Linux】线程的内核级理解&&详谈页表以及虚拟地址到物理地址之间的转化
【Linux】线程的内核级理解&&详谈页表以及虚拟地址到物理地址之间的转化
|
1天前
|
Linux 编译器 调度
xenomai内核解析--双核系统调用(二)--应用如何区分xenomai/linux系统调用或服务
本文介绍了如何将POSIX应用程序编译为在Xenomai实时内核上运行的程序。
15 1
xenomai内核解析--双核系统调用(二)--应用如何区分xenomai/linux系统调用或服务
|
1天前
|
算法 Linux 调度
xenomai内核解析--xenomai与普通linux进程之间通讯XDDP(一)--实时端socket创建流程
xenomai与普通linux进程之间通讯XDDP(一)--实时端socket创建流程
6 1
xenomai内核解析--xenomai与普通linux进程之间通讯XDDP(一)--实时端socket创建流程
|
1天前
|
Linux 调度 数据库
|
1天前
|
存储 缓存 Linux
xenomai内核解析--xenomai与普通linux进程之间通讯XDDP(三)--实时与非实时数据交互
本文介绍了Xenomai中的XDDP(Xenomai Distributed Data Protocol)通信机制,XDDP用于实时和非实时进程之间的数据交换。XDDP在Xenomai内核中涉及的数据结构和管理方式,以及创建XDDP通道后的实时端和非实时端连接过程。
7 0
xenomai内核解析--xenomai与普通linux进程之间通讯XDDP(三)--实时与非实时数据交互
|
2天前
|
自然语言处理 Java Linux
【Linux】开始学习进程替换吧!
通过学习进程替换,我们可以体会到多语言混搭的快乐,可以从C语言直接蹦到python ,也可以从c++里运行java代码。是不是很厉害!这是通过调度多个进程的效果,联系我们之前学习的进程,进程控制等概念。我们可以想要运行其他代码可以通过创建子进程来实现,但是这样也肯定是同一种语言,如果想要运行其他语言,那是不是有种方法可以调度一个进程来当做子进程呢??? 我们开始今天的学习吧!
9 0
|
2天前
|
缓存 安全 网络协议
Linux内核详解,什么是linux内核?
Linux内核详解,什么是linux内核?
12 0
|
3天前
|
存储 缓存 安全
Linux系统内核面试题
Linux系统内核面试题
18 3
|
4天前
|
缓存 运维 算法
深入理解Linux内核的虚拟内存管理
【5月更文挑战第6天】 在现代操作系统中,尤其是类Unix系统如Linux中,虚拟内存管理是一项核心功能,它不仅支持了多任务环境,还提供了内存保护和抽象。本文将深入探讨Linux操作系统的虚拟内存子系统,包括分页机制、虚拟地址空间布局、页面置换算法以及内存分配策略。通过对这些概念的剖析,我们旨在为读者揭示Linux如何有效地管理和优化物理内存资源,并确保系统的稳定运行与高效性能。
|
4天前
|
Linux 调度 开发者
探索Linux内核调度:公平与效率的平衡艺术
【5月更文挑战第6天】 随着多核处理器的普及,操作系统的进程调度策略对系统性能的影响愈加显著。Linux作为广泛应用的开源操作系统,其内核调度器的设计哲学和实现细节一直是系统研究领域的热点。本文将深入分析Linux内核调度器的工作原理,探讨如何在保证公平性和效率之间取得平衡,并考察最新的调度器CFS(Completely Fair Scheduler)如何适应现代硬件架构的需求。