#include <pthread.h>
#include "barrier.h"
#include "Platform.h"
Go to the source code of this file.
Functions | |
| CXXCAPI int | desperado_portable_barrier () |
Definition in file barrier.cpp.
|
|
Defines an interface for a generic memory barrier, then tries to implement it in a platform and target independent manner. This is not a thread synchronization barrier. It is a memory fence to insure that memory writes are not reordered past it ("release" semantics) and memory reads are not reordered before it ("acquire" semantics)." This function uses a POSIX mutex allocated on the stack solely for its side effect of calling the underlying platform's memory barrier mechanism (if any) solely as a side effect. This is portable but expensive compared to compiler and target specific mechanisms using macros like smp_mb(), mb(), or barrier() that are defined to the appropriate assembler instructions, at least on Linux systems. You can try using those macros, but besides the portability issues, it is a real trick to get the native mechanisms to work under both C and C++, and under both the 2.4 kernel with gcc 3.3.2, and the 2.6 kernel with gcc 3.3.4. Note that this is expensive enough, you may be better off taking the simple, safe approach and just using a mutex to synchronize access to any data shared between threads, even on a uniprocessor system. However, it is not as expensive as it might seem, since the mutex is guaranteed never to block the calling thread. I was interested in experimenting with a portable memory barrier implementation. Since I haven't figured out how to adequately unit test this, this must be considered highly experimental. This is usable from either a C or C++ translation unit.
Definition at line 74 of file barrier.cpp. Referenced by MemoryBarrier::MemoryBarrier(), unittestbarrier(), and unittestbarrier2(). |