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 #if !defined(__TBB_machine_H) || defined(__TBB_machine_macos_common_H)
30 #error Do not include this file directly; include tbb_machine.h instead
33 #define __TBB_machine_macos_common_H
36 #define __TBB_Yield() sched_yield()
38 // __TBB_HardwareConcurrency
40 #include <sys/types.h>
41 #include <sys/sysctl.h>
43 static inline int __TBB_macos_available_cpu() {
44 int name[2] = {CTL_HW, HW_AVAILCPU};
46 size_t size = sizeof(ncpu);
47 sysctl( name, 2, &ncpu, &size, NULL, 0 );
51 #define __TBB_HardwareConcurrency() __TBB_macos_available_cpu()
53 #ifndef __TBB_full_memory_fence
54 // TBB has not recognized the architecture (none of the architecture abstraction
55 // headers was included).
56 #define __TBB_UnknownArchitecture 1
59 #if __TBB_UnknownArchitecture || __TBB_WORDSIZE==4
60 // In case of IA32 this is a workaround for compiler bugs triggered by inline
61 // assembly implementation of __TBB_machine_cmpswp8 in linux_ia32.h, which may
62 // lead to incorrect codegen (gcc) or compilation failures (any icc including 12.0.4).
64 // Implementation of atomic operations based on OS provided primitives
65 #include <libkern/OSAtomic.h>
67 static inline int64_t __TBB_machine_cmpswp8_OsX(volatile void *ptr, int64_t value, int64_t comparand)
69 __TBB_ASSERT( !((uintptr_t)ptr&0x7), "address not properly aligned for Mac OS atomics");
70 int64_t* address = (int64_t*)ptr;
71 while( !OSAtomicCompareAndSwap64Barrier(comparand, value, address) ){
73 int64_t snapshot = *address;
75 int64_t snapshot = OSAtomicAdd64( 0, address );
77 if( snapshot!=comparand ) return snapshot;
82 #define __TBB_machine_cmpswp8 __TBB_machine_cmpswp8_OsX
84 #endif /* __TBB_UnknownArchitecture || __TBB_WORDSIZE==4 */
86 #if __TBB_UnknownArchitecture
88 #ifndef __TBB_WORDSIZE
89 #define __TBB_WORDSIZE 4
92 #define __TBB_BIG_ENDIAN __BIG_ENDIAN__
94 /** As this generic implementation has absolutely no information about underlying
95 hardware, its performance most likely will be sub-optimal because of full memory
96 fence usages where a more lightweight synchronization means (or none at all)
97 could suffice. Thus if you use this header to enable TBB on a new platform,
98 consider forking it and relaxing below helpers as appropriate. **/
99 #define __TBB_control_consistency_helper() OSMemoryBarrier()
100 #define __TBB_acquire_consistency_helper() OSMemoryBarrier()
101 #define __TBB_release_consistency_helper() OSMemoryBarrier()
102 #define __TBB_full_memory_fence() OSMemoryBarrier()
104 static inline int32_t __TBB_machine_cmpswp4(volatile void *ptr, int32_t value, int32_t comparand)
106 __TBB_ASSERT( !((uintptr_t)ptr&0x3), "address not properly aligned for Mac OS atomics");
107 int32_t* address = (int32_t*)ptr;
108 while( !OSAtomicCompareAndSwap32Barrier(comparand, value, address) ){
109 int32_t snapshot = *address;
110 if( snapshot!=comparand ) return snapshot;
115 static inline int32_t __TBB_machine_fetchadd4(volatile void *ptr, int32_t addend)
117 __TBB_ASSERT( !((uintptr_t)ptr&0x3), "address not properly aligned for Mac OS atomics");
118 return OSAtomicAdd32Barrier(addend, (int32_t*)ptr) - addend;
121 static inline int64_t __TBB_machine_fetchadd8(volatile void *ptr, int64_t addend)
123 __TBB_ASSERT( !((uintptr_t)ptr&0x7), "address not properly aligned for Mac OS atomics");
124 return OSAtomicAdd64Barrier(addend, (int64_t*)ptr) - addend;
127 #define __TBB_USE_GENERIC_PART_WORD_CAS 1
128 #define __TBB_USE_GENERIC_PART_WORD_FETCH_ADD 1
129 #define __TBB_USE_GENERIC_FETCH_STORE 1
130 #define __TBB_USE_GENERIC_HALF_FENCED_LOAD_STORE 1
131 #define __TBB_USE_GENERIC_RELAXED_LOAD_STORE 1
132 #if __TBB_WORDSIZE == 4
133 #define __TBB_USE_GENERIC_DWORD_LOAD_STORE 1
136 #endif /* __TBB_UnknownArchitecture */