Android系统中提供的原子操作
代码的实现位于文件system/core/include/cutils中
http://androidxref.com/4.4.3_r1.1/xref/system/core/include/cutils/atomic.h
16
17#ifndef ANDROID_CUTILS_ATOMIC_H
18#define ANDROID_CUTILS_ATOMIC_H
19
20#include <stdint.h>
21#include <sys/types.h>
22
23#ifdef __cplusplus
24extern "C" {
25#endif
26
27/*
28 * A handful of basic atomic operations. The appropriate pthread
29 * functions should be used instead of these whenever possible.
30 *
31 * The "acquire" and "release" terms can be defined intuitively in terms
32 * of the placement of memory barriers in a simple lock implementation:
33 * - wait until compare-and-swap(lock-is-free --> lock-is-held) succeeds
34 * - barrier
35 * - [do work]
36 * - barrier
37 * - store(lock-is-free)
38 * In very crude terms, the initial (acquire) barrier prevents any of the
39 * "work" from happening before the lock is held, and the later (release)
40 * barrier ensures that all of the work happens before the lock is released.
41 * (Think of cached writes, cache read-ahead, and instruction reordering
42 * around the CAS and store instructions.)
43 *
44 * The barriers must apply to both the compiler and the CPU. Note it is
45 * legal for instructions that occur before an "acquire" barrier to be
46 * moved down below it, and for instructions that occur after a "release"
47 * barrier to be moved up above it.
48 *
49 * The ARM-driven implementation we use here is short on subtlety,
50 * and actually requests a full barrier from the compiler and the CPU.
51 * The only difference between acquire and release is in whether they
52 * are issued before or after the atomic operation with which they
53 * are associated. To ease the transition to C/C++ atomic intrinsics,
54 * you should not rely on this, and instead assume that only the minimal
55 * acquire/release protection is provided.
56 *
57 * NOTE: all int32_t* values are expected to be aligned on 32-bit boundaries.
58 * If they are not, atomicity is not guaranteed.
59 */
60
61/*
62 * Basic arithmetic and bitwise operations. These all provide a
63 * barrier with "release" ordering, and return the previous value.
64 *
65 * These have the same characteristics (e.g. what happens on overflow)
66 * as the equivalent non-atomic C operations.
67 */
68int32_t android_atomic_inc(volatile int32_t* addr);
69int32_t android_atomic_dec(volatile int32_t* addr);
70int32_t android_atomic_add(int32_t value, volatile int32_t* addr);
71int32_t android_atomic_and(int32_t value, volatile int32_t* addr);
72int32_t android_atomic_or(int32_t value, volatile int32_t* addr);
73
74/*
75 * Perform an atomic load with "acquire" or "release" ordering.
76 *
77 * This is only necessary if you need the memory barrier. A 32-bit read
78 * from a 32-bit aligned address is atomic on all supported platforms.
79 */
80int32_t android_atomic_acquire_load(volatile const int32_t* addr);
81int32_t android_atomic_release_load(volatile const int32_t* addr);
82
83/*
84 * Perform an atomic store with "acquire" or "release" ordering.
85 *
86 * This is only necessary if you need the memory barrier. A 32-bit write
87 * to a 32-bit aligned address is atomic on all supported platforms.
88 */
89void android_atomic_acquire_store(int32_t value, volatile int32_t* addr);
90void android_atomic_release_store(int32_t value, volatile int32_t* addr);
91
92/*
93 * Compare-and-set operation with "acquire" or "release" ordering.
94 *
95 * This returns zero if the new value was successfully stored, which will
96 * only happen when *addr == oldvalue.
97 *
98 * (The return value is inverted from implementations on other platforms,
99 * but matches the ARM ldrex/strex result.)
100 *
101 * Implementations that use the release CAS in a loop may be less efficient
102 * than possible, because we re-issue the memory barrier on each iteration.
103 */
104int android_atomic_acquire_cas(int32_t oldvalue, int32_t newvalue,
105 volatile int32_t* addr);
106int android_atomic_release_cas(int32_t oldvalue, int32_t newvalue,
107 volatile int32_t* addr);
这是个跟处理器相关的一个函数,它执行原子性的加1操作可能更有效率。它的用法是这样,如果oldvalue等于* addr则赋值给* addr,返回0,否则返回1
108
109/*
110 * Aliases for code using an older version of this header. These are now
111 * deprecated and should not be used. The definitions will be removed
112 * in a future release.
113 */
114#define android_atomic_write android_atomic_release_store
115#define android_atomic_cmpxchg android_atomic_release_cas
116
117#ifdef __cplusplus
118} // extern "C"
119#endif
120
121#endif // ANDROID_CUTILS_ATOMIC_H
QQ群 计算机科学与艺术 272583193
加群链接:http://jq.qq.com/?_wv=1027&k=Q9OxMv
转载于:https://www.cnblogs.com/fly-fish/p/3854142.html
Android系统中提供的原子操作相关推荐
- Android系统中模拟GPS位置,Android系统中模拟GPS位置
8种机械键盘轴体对比 本人程序员,要买一个写代码的键盘,请问红轴和茶轴怎么选? Android系统中提供了模拟GPS坐标的功能,可以很方便的帮助我们测试不同地理位置下应用中各个功能效果. 模拟器中模拟 ...
- Android 系统到底提供了哪些东西,供我们可以开发出优秀的应用程序(文末送书)
Android 系统到底提供了哪些东西,供我们可以开发出优秀的应用程序. 作者:坚果 公众号:"大前端之旅" 华为云享专家,InfoQ签约作者,阿里云专家博主,51CTO博客首席体 ...
- Android系统中的进程管理:内存的回收
本文是Android系统进程管理的第三篇文章.进程管理的前面两篇文章,请参见这里: Android系统中的进程管理:进程的创建 Android系统中的进程管理:进程的优先级 本文适合Android平台 ...
- Android系统中的进程管理:进程的创建
对于操作系统来说,进程管理是其最重要的职责之一. 考虑到这部分的内容较多,因此会拆分成几篇文章来讲解. 本文是进程管理系统文章的第一篇,会讲解Android系统中的进程创建. 本文适合Android平 ...
- Android系统中的进程管理:进程的优先级
本文是Android进程管理系列文章的第二篇,会讲解进程管理中的优先级管理. 进程管理的第一篇文章:<进程的创建>请跳转至这里. 本文适合Android平台的应用程序开发者,也适合对于An ...
- Android 系统中 Location Service 的实现与架构
定位服务是移动设备上最常用的功能之一,本文以 Android 源码为基础,详细分析了 Android 系统中定位服务的架构和实现. 在 Android 系统中,所有系统服务的实现都是类似的.只要明白其 ...
- android属于数据库管理系统,详细谈谈Android系统中的SQLite数据库的应用
数据库是按照数据结构来组织.存储和管理数据的仓库,而在信息话的社会,数据库又不单单仅限与数据的相关内容,现在数据库技术是管理信息系统.办公自动化系统.决策支持系统等各类信息系统的核心部分,而SQL是结 ...
- 向Android系统中添加模块及产品流程
添加Android模块 一.基础知识: (1)在Android系统中,编译都是以模块(而不是文件)作为单位的,每个模块都有一个唯一的名称: (2)为了将模块编译到Android系统中,每个模块都需 ...
- android 音频播放过程,一种Android系统中的音频播放方法与流程
本申请涉及android系统技术,特别涉及一种android系统中的音频播放方法. 背景技术: 在android系统中,现有的使用audiotrack进行音频播放时,audiotrack应用与andr ...
最新文章
- 你了解SpringBoot启动时API相关信息是用什么数据结构存储的吗?(上篇)
- 科大星云诗社动态20210601
- Java未被捕获的异常
- 【论文党福利】如何提取图像中的数据
- 长安链技术架构与共识模块介绍
- 电脑必备必装的软件工具神器,强烈推荐
- VMware ESXi6.0注入8060阵列卡驱动过程记录
- 基于大数据的房价数据可视化分析预测系统
- 24种设计模式-单例模式-懒汉模式详解
- 超级计算机预测未来,超级计算机预测未来
- uc7.5java下载,ucweb手机浏览器7.5 java版 UC浏览器Java
- 正则表达式在线生成网站推荐
- 游戏开发入门系列(目录)
- android来电没有弹窗
- 字典(dict)的反转
- RecyclerView Adapter 优雅封装搞定所有列表
- 我的奋斗之黑马第一天
- JAVA和MySQL实现公交管理_基于JSP公交管理系统的设计与实现(SSH,MySQL)(含录像)
- 刚开始学java ,java代码开头的几行都是什么意思?
- 2020年,PS平面设计快捷键最新最全使用攻略