代码的实现位于文件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_taddr);

69int32_t android_atomic_dec(volatile int32_taddr);

70int32_t android_atomic_add(int32_t value, volatile int32_taddr);

71int32_t android_atomic_and(int32_t value, volatile int32_taddr);

72int32_t android_atomic_or(int32_t value, volatile int32_taddr);

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_taddr);

81int32_t android_atomic_release_load(volatile const int32_taddr);

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_taddr);

90void android_atomic_release_store(int32_t value, volatile int32_taddr);

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 oldvalueint32_t newvalue,

105        volatile int32_taddr);

106int android_atomic_release_cas(int32_t oldvalueint32_t newvalue,

107        volatile int32_taddr);

这是个跟处理器相关的一个函数,它执行原子性的加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

Android系统中提供的原子操作的更多相关文章

  1. Android系统中的广播(Broadcast)机制简要介绍和学习计划

    在Android系统中,广播(Broadcast)是在组件之间传播数据(Intent)的一种机制:这些组件甚至是可以位于不同的进程中,这样它就像Binder机制一样,起到进程间通信的作用:本文通过一个 ...

  2. Android系统中是否开启定位及定位模式的判断

    1.关于Android系统中不同的定位模式 Android系统中包括3中定位模式:   使用GPS.WLAN和移动网络 使用WLAN和移动网络 仅使用GPS 截图 特点 同时使用GPS.WIFI及基站 ...

  3. android系统中对ffmpeg封装最好的免费SDK

    android系统中对ffmpeg封装最好的免费SDK; 无论个人还是公司,都免费商用, 欢迎下载. https://github.com/LanSoSdk/LanSoEditor_common 可能 ...

  4. AIDL在android系统中的作用

    AIDL,Android Interface definition language的缩写,它是一种android内部进程通信接口的描述语言,通过它我们可以定义进程间的通信接口.最近看了下AIDL在A ...

  5. Android系统中的6种模式

    Android系统中的6种模式 1:一般启动模式(normal mode):    功能是正常启动手机,方法为关机状态下按电源键启动. 2:安全模式(safe mode):    此模式和正常启动一样 ...

  6. [原创]Android系统中常用JAVA类源码浅析之HashMap

    由于是浅析,所以我只分析常用的接口,注意是Android系统中的JAVA类,可能和JDK的源码有区别. 首先从构造函数开始, /** * Min capacity (other than zero) ...

  7. Android系统中 setprop,getprop,watchprops命令的使用

    如:在frameworks/opt/net/ims/src/java/com/android/ims/ImsManager.java if (SystemProperties.get("pe ...

  8. 用adb pull命令从android系统中读取文件失败的原因及解决办法

    问题:使用adb pull命令从android系统中读取文件失败.显示:Permission denied   原因:是由于文件权限原因引起.       使用ls -l命令查看android系统中的 ...

  9. Android系统中的dp和px的转换

    android系统中DP和SP的转化:1.首先分析TypedValue.java 可以调用以下代码获得dp的值 TypedValue.applyDimension(TypedValue.COMPLEX ...

随机推荐

  1. 我的VisualStudio工具箱

    代码神器 ReSharper 毫无疑问,我认为R#是目前VS插件中有史以来最强大的,各种快捷生成代码的方式, 代码重构, 很多很多的快捷键支持.相比较原生VS的,VS的智能功能简直弱爆了. dimec ...

  2. 教你50招提升ASP.NET性能(二十四):ORM小窍门

    ORM TipsORM小窍门 More and more people are using Object to Relational Mapping (ORM) tools to jump the d ...

  3. 【转】Android 4.4前后版本读取图库图片和拍照完美解决方案

    http://blog.csdn.net/zbjdsbj/article/details/42387551 4.3或以下,选了图片之后,根据Uri来做处理,很多帖子都有了,我就不详细说了.主要是4.4 ...

  4. Hyper-V网络配置

    Hyper-V虚拟交换机类型应用: 外部虚拟网络: 可以实现虚拟机之间.虚拟机和物理机.虚拟机和外部网络的通信. 生产环境不勾选“允许管理操作系统共享此网络适配器”,勾选之后会为主机创建虚拟网卡,会实 ...

  5. 《C语言编写 学生成绩管理系统》

    /* (程序头部凝视開始) * 程序的版权和版本号声明部分 * Copyright (c) 2011, 烟台大学计算机学院学生 * All rights reserved. * 文件名: 学生成绩管理 ...

  6. Codeforces Round #330 (Div. 1) C. Edo and Magnets 暴力

    C. Edo and Magnets Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/594/pr ...

  7. 实现一个跨平台的mysock库(windows、linux)

     源码下载 1.首先确定自己的操作系统为32位还是64位: root@bfq:~/mysock# uname -a Linux bfq 3.11.0-26-generic#45~precise1- ...

  8. iOS与日期相关的操作

    // Do any additional setup after loading the view, typically from a nib. //得到当前的日期 注意week1是星期天 NSDat ...

  9. iOS开发——动画篇Swift篇&常用动画总结

    UIView动画: UIView动画时最基本的动画,是直接对我们界面上控件进行简单的动画效果实现,如果你只需要用到一些简单的效果,那么这个很适合你,关于UIView动画实现恨简单, UIKit直接将动 ...

  10. Masonry+Infinite-Scroll实现无刷新无分页完美瀑布流(转)

    一.Masonry 是基于Jquery插件,用于对CSS布局的可移动层进行重新布局.Masonry愿意石工,可以这样形象的理解,页面上很多大小不一的移动层可以想象成散乱的石头,经过Masonry这个石 ...