1 // ============================================================================
2 // Copyright (c) 2010 Faustino Frechilla
3 // All rights reserved.
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are met:
8 // 1. Redistributions of source code must retain the above copyright notice,
9 // this list of conditions and the following disclaimer.
10 // 2. Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // 3. The name of the author may not be used to endorse or promote products
14 // derived from this software without specific prior written permission.
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 // POSSIBILITY OF SUCH DAMAGE.
28 /// @file atomic_ops.h
29 /// @brief This file contains functions to wrap the built-in atomic operations
30 /// defined for your compiler
32 /// @author Faustino Frechilla
35 /// Faustino Frechilla 11-Jul-2010 Original development. GCC support
38 // ============================================================================
40 #ifndef __ATOMIC_OPS_H
41 #define __ATOMIC_OPS_H
44 // Atomic functions in GCC are present from version 4.1.0 on
45 // http://gcc.gnu.org/onlinedocs/gcc-4.1.0/gcc/Atomic-Builtins.html
47 // Test for GCC >= 4.1.0
48 #if (__GNUC__ < 4) || \
49 ((__GNUC__ == 4) && ((__GNUC_MINOR__ < 1) || \
50 ((__GNUC_MINOR__ == 1) && \
51 (__GNUC_PATCHLEVEL__ < 0))) )
53 #error Atomic built-in functions are only available in GCC in versions >= 4.1.0
54 #endif // end of check for GCC 4.1.0
56 /// @brief atomically adds a_count to the variable pointed by a_ptr
57 /// @return the value that had previously been in memory
58 #define AtomicAdd(a_ptr,a_count) __sync_fetch_and_add (a_ptr, a_count)
60 /// @brief atomically substracts a_count from the variable pointed by a_ptr
61 /// @return the value that had previously been in memory
62 #define AtomicSub(a_ptr,a_count) __sync_fetch_and_sub (a_ptr, a_count)
64 /// @brief Compare And Swap
65 /// If the current value of *a_ptr is a_oldVal, then write a_newVal into *a_ptr
66 /// @return true if the comparison is successful and a_newVal was written
67 #define CAS(a_ptr, a_oldVal, a_newVal) __sync_bool_compare_and_swap(a_ptr, a_oldVal, a_newVal)
69 /// @brief Compare And Swap
70 /// If the current value of *a_ptr is a_oldVal, then write a_newVal into *a_ptr
71 /// @return the contents of *a_ptr before the operation
72 #define CASVal(a_ptr, a_oldVal, a_newVal) __sync_val_compare_and_swap(a_ptr, a_oldVal, a_newVal)
75 #error Atomic functions such as CAS or AtomicAdd are not defined for your compiler. Please add them in atomic_ops.h
79 #endif // __ATOMIC_OPS_H