2 Copyright 2005-2011 Intel Corporation. All Rights Reserved.
4 This file is part of Threading Building Blocks.
6 Threading Building Blocks is free software; you can redistribute it
7 and/or modify it under the terms of the GNU General Public License
8 version 2 as published by the Free Software Foundation.
10 Threading Building Blocks is distributed in the hope that it will be
11 useful, but WITHOUT ANY WARRANTY; without even the implied warranty
12 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with Threading Building Blocks; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 As a special exception, you may use this file as part of a free software
20 library without restriction. Specifically, if other files instantiate
21 templates or use macros or inline functions from this file, or you compile
22 this file and link it with other files to produce an executable, this
23 file does not by itself cause the resulting executable to be covered by
24 the GNU General Public License. This exception does not however
25 invalidate any other reasons why the executable file might be covered by
26 the GNU General Public License.
30 #ifndef __TBB_machine_H
31 #error Do not include this file directly; include tbb_machine.h instead
37 #define __TBB_WORDSIZE 8
38 #define __TBB_BIG_ENDIAN 1
40 #define __TBB_release_consistency_helper() __asm__ __volatile__ ("": : :"memory")
41 #define __TBB_full_memory_fence() __asm__ __volatile__("membar #LoadLoad|#LoadStore|#StoreStore|#StoreLoad": : : "memory")
43 //--------------------------------------------------
45 //--------------------------------------------------
48 * Atomic CAS for 32 bit values, if *ptr==comparand, then *ptr=value, returns *ptr
49 * @param ptr pointer to value in memory to be swapped with value if *ptr==comparand
50 * @param value value to assign *ptr to if *ptr==comparand
51 * @param comparand value to compare with *ptr
52 ( @return value originally in memory at ptr, regardless of success
54 static inline int32_t __TBB_machine_cmpswp4(volatile void *ptr, int32_t value, int32_t comparand ){
58 : "=m"(*(int32_t *)ptr), "=r"(result)
59 : "m"(*(int32_t *)ptr), "1"(value), "r"(comparand), "r"(ptr)
65 * Atomic CAS for 64 bit values, if *ptr==comparand, then *ptr=value, returns *ptr
66 * @param ptr pointer to value in memory to be swapped with value if *ptr==comparand
67 * @param value value to assign *ptr to if *ptr==comparand
68 * @param comparand value to compare with *ptr
69 ( @return value originally in memory at ptr, regardless of success
71 static inline int64_t __TBB_machine_cmpswp8(volatile void *ptr, int64_t value, int64_t comparand ){
75 : "=m"(*(int64_t *)ptr), "=r"(result)
76 : "m"(*(int64_t *)ptr), "1"(value), "r"(comparand), "r"(ptr)
81 //---------------------------------------------------
83 //---------------------------------------------------
86 * Atomic fetch and add for 32 bit values, in this case implemented by continuously checking success of atomicity
87 * @param ptr pointer to value to add addend to
88 * @param addened value to add to *ptr
89 * @return value at ptr before addened was added
91 static inline int32_t __TBB_machine_fetchadd4(volatile void *ptr, int32_t addend){
93 __asm__ __volatile__ (
94 "0:\t add\t %3, %4, %0\n" // do addition
95 "\t cas\t [%2], %3, %0\n" // cas to store result in memory
96 "\t cmp\t %3, %0\n" // check if value from memory is original
97 "\t bne,a,pn\t %%icc, 0b\n" // if not try again
98 "\t mov %0, %3\n" // use branch delay slot to move new value in memory to be added
99 : "=&r"(result), "=m"(*(int32_t *)ptr)
100 : "r"(ptr), "r"(*(int32_t *)ptr), "r"(addend), "m"(*(int32_t *)ptr)
106 * Atomic fetch and add for 64 bit values, in this case implemented by continuously checking success of atomicity
107 * @param ptr pointer to value to add addend to
108 * @param addened value to add to *ptr
109 * @return value at ptr before addened was added
111 static inline int64_t __TBB_machine_fetchadd8(volatile void *ptr, int64_t addend){
113 __asm__ __volatile__ (
114 "0:\t add\t %3, %4, %0\n" // do addition
115 "\t casx\t [%2], %3, %0\n" // cas to store result in memory
116 "\t cmp\t %3, %0\n" // check if value from memory is original
117 "\t bne,a,pn\t %%xcc, 0b\n" // if not try again
118 "\t mov %0, %3\n" // use branch delay slot to move new value in memory to be added
119 : "=&r"(result), "=m"(*(int64_t *)ptr)
120 : "r"(ptr), "r"(*(int64_t *)ptr), "r"(addend), "m"(*(int64_t *)ptr)
125 //--------------------------------------------------------
126 // Logarithm (base two, integer)
127 //--------------------------------------------------------
129 static inline int64_t __TBB_machine_lg( uint64_t x ) {
139 __asm__ ("popc %1, %0" : "=r"(count) : "r"(x) );
143 //--------------------------------------------------------
145 static inline void __TBB_machine_or( volatile void *ptr, uint64_t addend ) {
146 __asm__ __volatile__ (
147 "0:\t or\t %2, %3, %%g1\n" // do addition
148 "\t casx\t [%1], %2, %%g1\n" // cas to store result in memory
149 "\t cmp\t %2, %%g1\n" // check if value from memory is original
150 "\t bne,a,pn\t %%xcc, 0b\n" // if not try again
151 "\t mov %%g1, %2\n" // use branch delay slot to move new value in memory to be added
152 : "=m"(*(int64_t *)ptr)
153 : "r"(ptr), "r"(*(int64_t *)ptr), "r"(addend), "m"(*(int64_t *)ptr)
154 : "ccr", "g1", "memory");
157 static inline void __TBB_machine_and( volatile void *ptr, uint64_t addend ) {
158 __asm__ __volatile__ (
159 "0:\t and\t %2, %3, %%g1\n" // do addition
160 "\t casx\t [%1], %2, %%g1\n" // cas to store result in memory
161 "\t cmp\t %2, %%g1\n" // check if value from memory is original
162 "\t bne,a,pn\t %%xcc, 0b\n" // if not try again
163 "\t mov %%g1, %2\n" // use branch delay slot to move new value in memory to be added
164 : "=m"(*(int64_t *)ptr)
165 : "r"(ptr), "r"(*(int64_t *)ptr), "r"(addend), "m"(*(int64_t *)ptr)
166 : "ccr", "g1", "memory");
170 static inline void __TBB_machine_pause( int32_t delay ) {
171 // do nothing, inlined, doesnt matter
174 // put 0xff in memory location, return memory value,
175 // generic trylockbyte puts 0x01, however this is fine
176 // because all that matters is that 0 is unlocked
177 static inline bool __TBB_machine_trylockbyte(unsigned char &flag){
178 unsigned char result;
179 __asm__ __volatile__ (
180 "ldstub\t [%2], %0\n"
181 : "=r"(result), "=m"(flag)
182 : "r"(&flag), "m"(flag)
188 // Machine specific atomic operations
190 //#define __TBB_CompareAndSwap1(P,V,C) __TBB_machine_cmpswp1(P,V,C) // use generic version in tbb_machine.h
191 //#define __TBB_CompareAndSwap2(P,V,C) __TBB_machine_cmpswp2(P,V,C) // use generic version in tbb_machine.h
192 #define __TBB_CompareAndSwap4(P,V,C) __TBB_machine_cmpswp4(P,V,C)
193 #define __TBB_CompareAndSwap8(P,V,C) __TBB_machine_cmpswp8(P,V,C)
194 #define __TBB_CompareAndSwapW(P,V,C) __TBB_machine_cmpswp8(P,V,C)
196 //#define __TBB_FetchAndAdd1(P,V) __TBB_machine_fetchadd1(P,V) // use generic version in tbb_machine.h
197 //#define __TBB_FetchAndAdd2(P,V) __TBB_machine_fetchadd2(P,V) // use generic version in tbb_machine.h
198 #define __TBB_FetchAndAdd4(P,V) __TBB_machine_fetchadd4(P,V)
199 #define __TBB_FetchAndAdd8(P,V) __TBB_machine_fetchadd8(P,V)
200 #define __TBB_FetchAndAddW(P,V) __TBB_machine_fetchadd8(P,V)
202 // use generic version in tbb_machine.h
203 //#define __TBB_FetchAndStore1(P,V) __TBB_machine_fetchstore1(P,V)
204 //#define __TBB_FetchAndStore2(P,V) __TBB_machine_fetchstore2(P,V)
205 //#define __TBB_FetchAndStore4(P,V) __TBB_machine_fetchstore4(P,V)
206 //#define __TBB_FetchAndStore8(P,V) __TBB_machine_fetchstore8(P,V)
207 //#define __TBB_FetchAndStoreW(P,V) __TBB_machine_fetchstore8(P,V)
212 #define __TBB_AtomicOR(P,V) __TBB_machine_or(P,V)
213 #define __TBB_AtomicAND(P,V) __TBB_machine_and(P,V)
215 // Definition of other functions
216 #define __TBB_Pause(V) __TBB_machine_pause(V)
217 #define __TBB_Log2(V) __TBB_machine_lg(V)
219 // Special atomic functions
220 #define __TBB_FetchAndAddWrelease(P,V) __TBB_FetchAndAddW(P,V)
221 #define __TBB_FetchAndIncrementWacquire(P) __TBB_FetchAndAddW(P,1)
222 #define __TBB_FetchAndDecrementWrelease(P) __TBB_FetchAndAddW(P,-1)
224 // Definition of Lock functions
225 // Repeatedly runs TryLockByte, no need to implement
226 #undef __TBB_LockByte
228 #define __TBB_TryLockByte(P) __TBB_machine_trylockbyte(P)