1 /*
2     Copyright 2005-2014 Intel Corporation.  All Rights Reserved.
3 
4     This file is part of Threading Building Blocks. Threading Building Blocks is free software;
5     you can redistribute it and/or modify it under the terms of the GNU General Public License
6     version 2  as  published  by  the  Free Software Foundation.  Threading Building Blocks is
7     distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the
8     implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
9     See  the GNU General Public License for more details.   You should have received a copy of
10     the  GNU General Public License along with Threading Building Blocks; if not, write to the
11     Free Software Foundation, Inc.,  51 Franklin St,  Fifth Floor,  Boston,  MA 02110-1301 USA
12 
13     As a special exception,  you may use this file  as part of a free software library without
14     restriction.  Specifically,  if other files instantiate templates  or use macros or inline
15     functions from this file, or you compile this file and link it with other files to produce
16     an executable,  this file does not by itself cause the resulting executable to be covered
17     by the GNU General Public License. This exception does not however invalidate any other
18     reasons why the executable file might be covered by the GNU General Public License.
19 */
20 
21 #if !defined(__TBB_machine_H) || defined(__TBB_machine_linux_intel64_H)
22 #error Do not #include this internal file directly; use public TBB headers instead.
23 #endif
24 
25 #define __TBB_machine_linux_intel64_H
26 
27 #include <stdint.h>
28 #include "gcc_ia32_common.h"
29 
30 #define __TBB_WORDSIZE 8
31 #define __TBB_ENDIANNESS __TBB_ENDIAN_LITTLE
32 
33 #define __TBB_compiler_fence() __asm__ __volatile__("": : :"memory")
34 #define __TBB_control_consistency_helper() __TBB_compiler_fence()
35 #define __TBB_acquire_consistency_helper() __TBB_compiler_fence()
36 #define __TBB_release_consistency_helper() __TBB_compiler_fence()
37 
38 #ifndef __TBB_full_memory_fence
39 #define __TBB_full_memory_fence() __asm__ __volatile__("mfence": : :"memory")
40 #endif
41 
42 #define __TBB_MACHINE_DEFINE_ATOMICS(S,T,X)                                          \
43 static inline T __TBB_machine_cmpswp##S (volatile void *ptr, T value, T comparand )  \
44 {                                                                                    \
45     T result;                                                                        \
46                                                                                      \
47     __asm__ __volatile__("lock\ncmpxchg" X " %2,%1"                                  \
48                           : "=a"(result), "=m"(*(volatile T*)ptr)                    \
49                           : "q"(value), "0"(comparand), "m"(*(volatile T*)ptr)       \
50                           : "memory");                                               \
51     return result;                                                                   \
52 }                                                                                    \
53                                                                                      \
54 static inline T __TBB_machine_fetchadd##S(volatile void *ptr, T addend)              \
55 {                                                                                    \
56     T result;                                                                        \
57     __asm__ __volatile__("lock\nxadd" X " %0,%1"                                     \
58                           : "=r"(result),"=m"(*(volatile T*)ptr)                     \
59                           : "0"(addend), "m"(*(volatile T*)ptr)                      \
60                           : "memory");                                               \
61     return result;                                                                   \
62 }                                                                                    \
63                                                                                      \
64 static inline  T __TBB_machine_fetchstore##S(volatile void *ptr, T value)            \
65 {                                                                                    \
66     T result;                                                                        \
67     __asm__ __volatile__("lock\nxchg" X " %0,%1"                                     \
68                           : "=r"(result),"=m"(*(volatile T*)ptr)                     \
69                           : "0"(value), "m"(*(volatile T*)ptr)                       \
70                           : "memory");                                               \
71     return result;                                                                   \
72 }                                                                                    \
73 
74 __TBB_MACHINE_DEFINE_ATOMICS(1,int8_t,"")
75 __TBB_MACHINE_DEFINE_ATOMICS(2,int16_t,"")
76 __TBB_MACHINE_DEFINE_ATOMICS(4,int32_t,"")
77 __TBB_MACHINE_DEFINE_ATOMICS(8,int64_t,"q")
78 
79 #undef __TBB_MACHINE_DEFINE_ATOMICS
80 
__TBB_machine_or(volatile void * ptr,uint64_t value)81 static inline void __TBB_machine_or( volatile void *ptr, uint64_t value ) {
82     __asm__ __volatile__("lock\norq %1,%0" : "=m"(*(volatile uint64_t*)ptr) : "r"(value), "m"(*(volatile uint64_t*)ptr) : "memory");
83 }
84 
__TBB_machine_and(volatile void * ptr,uint64_t value)85 static inline void __TBB_machine_and( volatile void *ptr, uint64_t value ) {
86     __asm__ __volatile__("lock\nandq %1,%0" : "=m"(*(volatile uint64_t*)ptr) : "r"(value), "m"(*(volatile uint64_t*)ptr) : "memory");
87 }
88 
89 #define __TBB_AtomicOR(P,V) __TBB_machine_or(P,V)
90 #define __TBB_AtomicAND(P,V) __TBB_machine_and(P,V)
91 
92 #define __TBB_USE_FETCHSTORE_AS_FULL_FENCED_STORE           1
93 #define __TBB_USE_GENERIC_HALF_FENCED_LOAD_STORE            1
94 #define __TBB_USE_GENERIC_RELAXED_LOAD_STORE                1
95 #define __TBB_USE_GENERIC_SEQUENTIAL_CONSISTENCY_LOAD_STORE 1
96 
97