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;
}

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

目录
相关文章
|
安全 网络协议 Linux
深入理解Linux内核模块:加载机制、参数传递与实战开发
本文深入解析了Linux内核模块的加载机制、参数传递方式及实战开发技巧。内容涵盖模块基础概念、加载与卸载流程、生命周期管理、参数配置方法,并通过“Hello World”模块和字符设备驱动实例,带领读者逐步掌握模块开发技能。同时,介绍了调试手段、常见问题排查、开发规范及高级特性,如内核线程、模块间通信与性能优化策略。适合希望深入理解Linux内核机制、提升系统编程能力的技术人员阅读与实践。
1290 1
|
Ubuntu Linux
Ubuntu 23.04 用上 Linux 6.2 内核,预计下放到 22.04 LTS 版本
Linux 6.2 带来了多项内容更新,修复了 AMD 锐龙处理器设备在启用 fTPM 后的运行卡顿问题,还增强了文件系统。
|
Ubuntu Linux
Ubuntu 23.10 现在由Linux内核6.3提供支持
如果你想在你的个人电脑上测试一下Ubuntu 23.10的最新开发快照,你可以从官方下载服务器下载最新的每日构建ISO。然而,请记住,这是一个预发布版本,所以不要在生产机器上使用或安装它。
|
传感器 监控 Ubuntu
10 月发布,Ubuntu 23.10 已升级到 Linux Kernel 6.3 内核
硬件方面,Linux 6.3 引入了在 HID 中引入了原生的 Steam Deck 控制器接口,允许罗技 G923 Xbox 版赛车方向盘在 Linux 上运行;改善 8BitDo Pro 2 有线控制器的行为;并为一系列华硕 Ryzen 主板添加传感器监控。
|
Ubuntu Linux
Ubuntu24.04LTS默认采用Linux 6.8内核,实验性版本可通过PPA获得
IT之家提醒,当下的 Ubuntu 23.10 也是一个“短期支持版本”,该版本将在今年 7 月终止支持,而今年 4 月推出的 Ubuntu 24.04 LTS 长期支持版本将获得 5 年的更新支持。
|
监控 Ubuntu Linux
什么Linux,Linux内核及Linux操作系统
上面只是简单的介绍了一下Linux操作系统的几个核心组件,其实Linux的整体架构要复杂的多。单纯从Linux内核的角度,它要管理CPU、内存、网卡、硬盘和输入输出等设备,因此内核本身分为进程调度,内存管理,虚拟文件系统,网络接口等4个核心子系统。
1322 0
|
Web App开发 缓存 Rust
|
Ubuntu 安全 Linux
Ubuntu 发行版更新 Linux 内核,修复 17 个安全漏洞
本地攻击者可以利用上述漏洞,攻击 Ubuntu 22.10、Ubuntu 22.04、Ubuntu 20.04 LTS 发行版,导致拒绝服务(系统崩溃)或执行任意代码。
|
Ubuntu 机器人 物联网
Linux Ubuntu 22.04 LTS 测试版实时内核已可申请
请注意,在启用实时内核后您需要手动配置 grub 以恢复到原始内核。更多内容请参考: