Created
October 15, 2013 14:06
-
-
Save BaiGang/6992118 to your computer and use it in GitHub Desktop.
Atomicity encapsulation for android arm devices.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef _VISION_PLAY_ARMV6_ATOMIC_H_ | |
#define _VISION_PLAY_ARMV6_ATOMIC_H_ | |
#if defined(__ARMEL__) && defined(__linux__) | |
typedef void (*LinuxKernelMemoryBarrierFunc)(void); | |
// Using the highly optimized device-specific memory barrier function. | |
inline void MemoryBarrier() { | |
(*(LinuxKernelMemoryBarrierFunc)0xffff0fa0)(); | |
} | |
#else | |
//# error Not on Android ARM linux devices. | |
// Make a compiler error here | |
int a[-1]; | |
#endif | |
namespace visionplay { | |
// @class A templated atomicity encapsulation. | |
// T should be POD type, as simple as possible. | |
// Best practice is using char, int, void* only. | |
template <typename T> | |
class Atomic { | |
public: | |
Atomic() : value_((T)0) {} | |
explicit Atomic(const T& t) : value_(t) {} | |
T NoBarrier_Load() const {return value_;} | |
void NoBarrier_Store(const T& t) {value_ = t;} | |
T Acquire_Load() const { | |
T res = value_; | |
MemoryBarrier(); | |
return res; | |
} | |
void Release_Store(const T& t) { | |
MemoryBarrier(); | |
value_ = t; | |
} | |
private: | |
T value_; | |
}; | |
} // namespace visionplay | |
#endif // _VISION_PLAY_ARMV6_ATOMIC_H_ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment