barrier.cpp File Reference

Implements the Desperado portable memory barrier function. More...

#include <pthread.h>
#include "barrier.h"
#include "Platform.h"

Go to the source code of this file.

Functions

CXXCAPI int desperado_portable_barrier ()


Detailed Description

Implements the Desperado portable memory barrier function.

Author:
Author
jsloan
Version:
Revision
1.4
Date:
Date
2006/10/14 21:11:59

Definition in file barrier.cpp.


Function Documentation

CXXCAPI int desperado_portable_barrier void   ) 
 

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.

See also:
desperado_native_barrier()

Mutex

CriticalSection

ISO, Information technology - Portable Operating System Interface (POSIX) - Part 1: System Application Program Interface (API) [C Language], International Organization for Standardization, ISO/IEC 9945-1:1996(E), 1996

ISO, Programming languages - C++, International Organization for Standardization, ISO/IEC 14882:1998(E), 1998

Wikipedia, "Memory barrier", http://en.wikipedia.org/wiki/Memory_barrier

D. Bacon et al., The "Double-Checked Locking Pattern is Broken" Declaration, http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html

S. Meyers at al., , "C++ and the Perils of Double-Checked Locking", Dr. Dobb's Journal, #362, July 2004 (part 1), #363, August 2004 (part 2)

S. Meyers, "Double-Checked Locking, Threads, Compiler Optimizations, and More", 2004-04-17

A. Alexandrescu et al., "Memory model for multithreaded C++", SG22 POSIX Advisory Group, WG21/N1680=J16/04-0120, 2004-09-10

M. Hill, "Multiprocessors Should Support Simple Memory- Consistency Models", IEEE Computer, August 1998

S. Adve et al., "Shared Memory Consistency Models: A Tutorial", IEEE Computer, December 1996

V. Morrison, "The DOTNET Memory Model", DOTNET Archives, March 2002, week 2, #6

H. Boehm, "Threads Cannot be Implemented as a Library", Hewlett-Packard, HPL-2004-209, 2004-11-12

H. Boehm, "Multithreaded Programming Issues for Application Programs", Hewlett-Packard, 2004

Returns:
zero if successful, non-zero otherwise.
Author:
coverclock@diag.com (Chip Overclock)

Definition at line 74 of file barrier.cpp.

Referenced by MemoryBarrier::MemoryBarrier(), unittestbarrier(), and unittestbarrier2().

Copyright © 2006 Digital Aggregates Corp., Arvada CO 80001-0587, USA.
Distributed under the terms of the GNU Free Documentation License.
http://www.diag.com/navigation/downloads/Desperado.html