Pyrogenesis  13997
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ia32.cpp
Go to the documentation of this file.
1 /* Copyright (c) 2011 Wildfire Games
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining
4  * a copy of this software and associated documentation files (the
5  * "Software"), to deal in the Software without restriction, including
6  * without limitation the rights to use, copy, modify, merge, publish,
7  * distribute, sublicense, and/or sell copies of the Software, and to
8  * permit persons to whom the Software is furnished to do so, subject to
9  * the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included
12  * in all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  */
22 
23 /*
24  * routines specific to IA-32
25  */
26 
27 #include "precompiled.h"
28 
29 #include "lib/sysdep/cpu.h"
31 
32 #if MSC_VERSION
33 
34 // VC 2008 and ICC 12 differ in their declaration of _Interlocked*
35 #if ICC_VERSION
36 typedef long* P32;
37 typedef __int64* P64;
38 #else
39 typedef volatile long* P32;
40 typedef volatile __int64* P64;
41 #endif
42 
43 bool cpu_CAS(volatile intptr_t* location, intptr_t expected, intptr_t newValue)
44 {
45  const intptr_t initial = _InterlockedCompareExchange((P32)location, newValue, expected);
46  return initial == expected;
47 }
48 
49 bool cpu_CAS64(volatile i64* location, i64 expected, i64 newValue)
50 {
51  const i64 initial = _InterlockedCompareExchange64((P64)location, newValue, expected);
52  return initial == expected;
53 }
54 
55 intptr_t cpu_AtomicAdd(volatile intptr_t* location, intptr_t increment)
56 {
57  return _InterlockedExchangeAdd((P32)location, increment);
58 }
59 
60 #elif OS_MACOSX
61 
62 #include <libkern/OSAtomic.h>
63 
64 intptr_t cpu_AtomicAdd(volatile intptr_t* location, intptr_t increment)
65 {
66  cassert(sizeof(intptr_t) == sizeof(int32_t));
67  return OSAtomicAdd32Barrier(increment, (volatile int32_t*)location);
68 }
69 
70 bool cpu_CAS(volatile intptr_t* location, intptr_t expected, intptr_t newValue)
71 {
72  cassert(sizeof(intptr_t) == sizeof(void*));
73  return OSAtomicCompareAndSwapPtrBarrier((void*)expected, (void*)newValue, (void* volatile*)location);
74 }
75 
76 bool cpu_CAS64(volatile i64* location, i64 expected, i64 newValue)
77 {
78  return OSAtomicCompareAndSwap64Barrier(expected, newValue, location);
79 }
80 
81 #elif GCC_VERSION
82 
83 intptr_t cpu_AtomicAdd(volatile intptr_t* location, intptr_t increment)
84 {
85  return __sync_fetch_and_add(location, increment);
86 }
87 
88 bool cpu_CAS(volatile intptr_t* location, intptr_t expected, intptr_t newValue)
89 {
90  return __sync_bool_compare_and_swap(location, expected, newValue);
91 }
92 
93 bool cpu_CAS64(volatile i64* location, i64 expected, i64 newValue)
94 {
95  return __sync_bool_compare_and_swap(location, expected, newValue);
96 }
97 
98 #endif
intptr_t cpu_AtomicAdd(volatile intptr_t *location, intptr_t increment)
add a signed value to a variable without the possibility of interference from other threads/CPUs...
Definition: arm.cpp:31
bool cpu_CAS64(volatile i64 *location, i64 expected, i64 newValue)
Definition: arm.cpp:41
#define i64
Definition: types.h:37
bool cpu_CAS(volatile intptr_t *location, intptr_t expected, intptr_t newValue)
atomic &quot;compare and swap&quot;.
Definition: arm.cpp:36
#define cassert(expr)
Compile-time assertion.