Torque3D Documentation / _generateds / platformIntrinsics.gcc.h

platformIntrinsics.gcc.h

Engine/source/platform/platformIntrinsics.gcc.h

Compiler intrinsics for GCC.

More...

Public Functions

dAtomicRead(volatile U32 & ref)

Performs an atomic read operation.

bool
dCompareAndSwap(volatile U32 & ref, U32 oldVal, U32 newVal)
bool
dCompareAndSwap(volatile U64 & ref, U64 oldVal, U64 newVal)
dFetchAndAdd(volatile S32 & ref, S32 val)
dFetchAndAdd(volatile U32 & ref, U32 val)

Detailed Description

Compiler intrinsics for GCC.

Public Functions

dAtomicRead(volatile U32 & ref)

Performs an atomic read operation.

dCompareAndSwap(volatile U32 & ref, U32 oldVal, U32 newVal)

dCompareAndSwap(volatile U64 & ref, U64 oldVal, U64 newVal)

dFetchAndAdd(volatile S32 & ref, S32 val)

dFetchAndAdd(volatile U32 & ref, U32 val)

 1
 2//-----------------------------------------------------------------------------
 3// Copyright (c) 2012 GarageGames, LLC
 4//
 5// Permission is hereby granted, free of charge, to any person obtaining a copy
 6// of this software and associated documentation files (the "Software"), to
 7// deal in the Software without restriction, including without limitation the
 8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 9// sell copies of the Software, and to permit persons to whom the Software is
10// furnished to do so, subject to the following conditions:
11//
12// The above copyright notice and this permission notice shall be included in
13// all copies or substantial portions of the Software.
14//
15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21// IN THE SOFTWARE.
22//-----------------------------------------------------------------------------
23
24#ifndef _TORQUE_PLATFORM_PLATFORMINTRINSICS_GCC_H_
25#define _TORQUE_PLATFORM_PLATFORMINTRINSICS_GCC_H_
26
27/// @file
28/// Compiler intrinsics for GCC.
29
30#ifdef TORQUE_OS_MAC
31#include <libkern/OSAtomic.h>
32#endif
33
34// Fetch-And-Add
35//
36// NOTE: These do not return the pre-add value because
37// not all platforms (damn you OSX) can do that.
38//
39inline void dFetchAndAdd( volatile U32& ref, U32 val )
40{
41   #if !defined(TORQUE_OS_MAC)
42      __sync_fetch_and_add(&ref, val );
43   #else
44      OSAtomicAdd32( val, (int32_t* ) &ref);
45   #endif
46}
47
48inline void dFetchAndAdd( volatile S32& ref, S32 val )
49{
50   #if !defined(TORQUE_OS_MAC)
51      __sync_fetch_and_add( &ref, val );
52   #else
53      OSAtomicAdd32( val, (int32_t* ) &ref);
54   #endif
55}
56
57// Compare-And-Swap
58
59inline bool dCompareAndSwap( volatile U32& ref, U32 oldVal, U32 newVal )
60{
61   // bool
62   //OSAtomicCompareAndSwap32(int32_t oldValue, int32_t newValue, volatile int32_t *theValue);
63   #if !defined(TORQUE_OS_MAC)
64      return ( __sync_val_compare_and_swap( &ref, oldVal, newVal ) == oldVal );
65   #else
66      return OSAtomicCompareAndSwap32(oldVal, newVal, (int32_t *) &ref);
67   #endif
68}
69
70inline bool dCompareAndSwap( volatile U64& ref, U64 oldVal, U64 newVal )
71{
72   #if !defined(TORQUE_OS_MAC)
73      return ( __sync_val_compare_and_swap( &ref, oldVal, newVal ) == oldVal );
74   #else
75      return OSAtomicCompareAndSwap64(oldVal, newVal, (int64_t *) &ref);
76   #endif
77}
78
79/// Performs an atomic read operation.
80inline U32 dAtomicRead( volatile U32 &ref )
81{
82   #if !defined(TORQUE_OS_MAC)
83      return __sync_fetch_and_add( ( volatile long* ) &ref, 0 );
84   #else
85      return OSAtomicAdd32( 0, (int32_t* ) &ref);
86   #endif
87}
88
89#endif // _TORQUE_PLATFORM_PLATFORMINTRINSICS_GCC_H_
90