Linux基础 - Linux ARM 原子读写

背景

在Linux Arm kernel实现原子读写64位数据;

接口代码

c 复制代码
#include <linux/atomic.h>
atomic64_t my_atomic_var = ATOMIC64_INIT(0);

void atomic_write_64(atomic64_t *v, int64_t new_value) {
    atomic64_set(v, new_value);
}

int64_t atomic_read_64(atomic64_t *v) {
    return atomic64_read(v);
}

使用自旋锁

c 复制代码
#include <linux/spinlock.h>

spinlock_t my_lock = SPIN_LOCK_UNLOCKED;
uint64_t my_shared_var = 0;

void write_64(uint64_t new_value) {
    spin_lock(&my_lock);
    my_shared_var = new_value;
    spin_unlock(&my_lock);
}

uint64_t read_64(void) {
    uint64_t value;
    spin_lock(&my_lock);
    value = my_shared_var;
    spin_unlock(&my_lock);
    return value;
}

使用读写锁

c 复制代码
#include <linux/rwlock.h>

rwlock_t my_rwlock = RW_LOCK_UNLOCKED;
uint64_t my_shared_var = 0;

void write_64(uint64_t new_value) {
    write_lock(&my_rwlock);
    my_shared_var = new_value;
    write_unlock(&my_rwlock);
}

uint64_t read_64(void) {
    uint64_t value;
    read_lock(&my_rwlock);
    value = my_shared_var;
    read_unlock(&my_rwlock);
    return value;
}
相关推荐
严谨的麻辣烫1 天前
Linux sysctl 网络参数调优实战:高并发与长连接场景下的内核配置
linux·网络·php
阡陌..1 天前
Ubuntu 22.04 离线环境完全配置指南:从 GCC 到 NVIDIA 驱动再到 Samba 共享
linux·运维·ubuntu
toooooop81 天前
如何用 ss + ps 精准定位本机 Redis 的“隐形”消费者?
linux·数据库·redis·缓存
重生的黑客1 天前
Linux 进程程序替换与自定义 Shell:从 exec 函数族到命令行解释器
linux·运维·服务器·shell
北极糊的狐1 天前
阿里云服务器-命令2-Linux 系统实时资源监视器 top 命令详解(进程级实时资源监控)
linux·运维·服务器
三言老师1 天前
clear与history历史命令管理实操
linux·运维·服务器·网络·centos
味悲1 天前
Linux 环境下 DNS 服务器搭建
linux·运维·服务器
dok121 天前
Johannes 《Linux内核模块与设备驱动开发:编写Linux驱动程序》 (7)open,release (8) write read
linux
皮卡狮1 天前
进程间通信:认识匿名管道和进程池实现
linux
feasibility.1 天前
wsl安装Ubuntu方法(含网络不稳定处理)
linux·运维·windows·ubuntu