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.
29 #ifndef __TBB_machine_H
30 #error Do not include this file directly; include tbb_machine.h instead
34 #define __TBB_Yield() sched_yield()
37 // __TBB_HardwareConcurrency
39 #include <sys/types.h>
40 #include <sys/sysctl.h>
42 static inline int __TBB_macos_available_cpu() {
43 int name[2] = {CTL_HW, HW_AVAILCPU};
45 size_t size = sizeof(ncpu);
46 sysctl( name, 2, &ncpu, &size, NULL, 0 );
50 #define __TBB_HardwareConcurrency() __TBB_macos_available_cpu()
53 #ifndef __TBB_WORDSIZE
54 #define __TBB_WORDSIZE 4
57 #ifndef __TBB_BIG_ENDIAN
59 #define __TBB_BIG_ENDIAN 1
61 #define __TBB_BIG_ENDIAN 0
66 #if !defined(__TBB_CompareAndSwap4) || !defined(__TBB_CompareAndSwap8)
68 // Implementation of atomic operations based on OS provided primitives
69 #include <libkern/OSAtomic.h>
71 #define __TBB_release_consistency_helper() OSMemoryBarrier()
72 #define __TBB_full_memory_fence() OSMemoryBarrier()
74 static inline int32_t __TBB_macos_cmpswp4(volatile void *ptr, int32_t value, int32_t comparand)
76 __TBB_ASSERT( !((uintptr_t)ptr&0x3), "address not properly aligned for Mac OS atomics");
77 int32_t* address = (int32_t*)ptr;
78 while( !OSAtomicCompareAndSwap32Barrier(comparand, value, address) ){
79 int32_t snapshot = *address;
80 if( snapshot!=comparand ) return snapshot;
85 static inline int64_t __TBB_macos_cmpswp8(volatile void *ptr, int64_t value, int64_t comparand)
87 __TBB_ASSERT( !((uintptr_t)ptr&0x7), "address not properly aligned for Mac OS atomics");
88 int64_t* address = (int64_t*)ptr;
89 while( !OSAtomicCompareAndSwap64Barrier(comparand, value, address) ){
91 int64_t snapshot = *address;
93 int64_t snapshot = OSAtomicAdd64( 0, address );
95 if( snapshot!=comparand ) return snapshot;
100 #define __TBB_CompareAndSwap4(P,V,C) __TBB_macos_cmpswp4(P,V,C)
101 #define __TBB_CompareAndSwap8(P,V,C) __TBB_macos_cmpswp8(P,V,C)
103 static inline int32_t __TBB_macos_fetchadd4(volatile void *ptr, int32_t addend)
105 __TBB_ASSERT( !((uintptr_t)ptr&0x3), "address not properly aligned for Mac OS atomics");
106 return OSAtomicAdd32Barrier(addend, (int32_t*)ptr) - addend;
109 static inline int64_t __TBB_macos_fetchadd8(volatile void *ptr, int64_t addend)
111 __TBB_ASSERT( !((uintptr_t)ptr&0x7), "address not properly aligned for Mac OS atomics");
112 return OSAtomicAdd64Barrier(addend, (int64_t*)ptr) - addend;
115 #define __TBB_FetchAndAdd4(P,V) __TBB_macos_fetchadd4(P,V)
116 #define __TBB_FetchAndAdd8(P,V) __TBB_macos_fetchadd8(P,V)
118 #if __TBB_WORDSIZE==4
119 #define __TBB_CompareAndSwapW(P,V,C) __TBB_CompareAndSwap4(P,V,C)
120 #define __TBB_FetchAndAddW(P,V) __TBB_FetchAndAdd4(P,V)
122 #define __TBB_CompareAndSwapW(P,V,C) __TBB_CompareAndSwap8(P,V,C)
123 #define __TBB_FetchAndAddW(P,V) __TBB_FetchAndAdd8(P,V)
126 #endif /* !defined(__TBB_CompareAndSwap4) || !defined(__TBB_CompareAndSwap8) */