arch_atomic.h 源码
2026/7/20大约 2 分钟源码参考架构层原子操作头文件
arch_atomic.h — 原子操作接口头文件
源文件路径: arch/arm/armv7m/include/arch_atomic.h
引用章节: 4.3.6 自旋锁 | 4.8 SMP 多核支持
完整源码
/**
***********************************************************************************************************************
* Copyright (c) 2020, China Mobile Communications Group Co.,Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* @file arch_atomic.h
*
* @brief Header file for atomic interface.
*
* @revision
* Date Author Notes
* 2021-01-08 OneOS Team First version.
***********************************************************************************************************************
*/
#ifndef __ARCH_ATOMIC_H__
#define __ARCH_ATOMIC_H__
#include <os_types.h>
#ifdef __cplusplus
extern "C" {
#endif
struct os_atomic
{
volatile int32_t counter;
};
typedef struct os_atomic os_atomic_t;
/*
* Apply to initializtion of variate which is defined by the "os_atomic_t".
*/
#define OS_ATOMIC_INIT(val) \
{ \
(val) \
}
#define os_atomic_read(ptr) ((ptr)->counter)
#define os_atomic_set(ptr, value) (((ptr)->counter) = (value))
extern void os_atomic_add(os_atomic_t *mem, int32_t value);
extern void os_atomic_sub(os_atomic_t *mem, int32_t value);
extern void os_atomic_inc(os_atomic_t *mem);
extern void os_atomic_dec(os_atomic_t *mem);
extern int32_t os_atomic_add_return(os_atomic_t *mem, int32_t value);
extern int32_t os_atomic_sub_return(os_atomic_t *mem, int32_t value);
extern int32_t os_atomic_inc_return(os_atomic_t *mem);
extern int32_t os_atomic_dec_return(os_atomic_t *mem);
extern int32_t os_atomic_xchg(os_atomic_t *mem, int32_t value);
extern os_bool_t os_atomic_cmpxchg(os_atomic_t *mem, int32_t old, int32_t new);
extern void os_atomic_and(os_atomic_t *mem, int32_t value);
extern void os_atomic_or(os_atomic_t *mem, int32_t value);
extern void os_atomic_nand(os_atomic_t *mem, int32_t value);
extern void os_atomic_xor(os_atomic_t *mem, int32_t value);
extern os_bool_t os_atomic_test_bit(os_atomic_t *mem, int32_t nr);
extern void os_atomic_set_bit(os_atomic_t *mem, int32_t nr);
extern void os_atomic_clear_bit(os_atomic_t *mem, int32_t nr);
extern void os_atomic_change_bit(os_atomic_t *mem, int32_t nr);
#ifdef __cplusplus
}
#endif
#endif /* __ARCH_ATOMIC_H__ */关键说明
| 模块 | 说明 |
|---|---|
| 数据结构 | os_atomic_t 包含 volatile int32_t counter,使用 volatile 防止编译器优化 |
| 初始化 | OS_ATOMIC_INIT(val) 宏用于静态初始化,os_atomic_read/set 宏用于读写 |
| 算术操作 | add/sub/inc/dec 实现原子加减,*_return 版本返回操作后的值 |
| 交换操作 | os_atomic_xchg 原子交换值,os_atomic_cmpxchg 原子比较并交换(CAS) |
| 位操作 | and/or/nand/xor 原子位逻辑操作,test/set/clear/change_bit 原子位操作 |
| 实现 | ARMv7-M 单核上通过 os_irq_lock/unlock 关中断实现原子性;ARMv7-R/A 多核使用 LDREX/STREX 指令 |